view libcommon/cardconnect.c @ 19:72a24b8538eb

meaty function of pb-dump moved back into simtool, out of libcommon Upon further reflection, I am not going to keep any of the pb-* commands in the new version of fc-uicc-tool: they are logically incorrect for UICC/USIM anyway, as they access phonebook files via old classic SIM paths, rather than their USIM paths. OTOH, I am going to implement new SMSP commands in fc-simtool, and I do not plan to replicate them in fc-uicc-tool either. Guts of fc-simtool pb-dump belong in simtool/pbdump.c, not in libcommon, just like the guts of the future smsp-dump command will belong in its own respective implementation module.
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 12 Feb 2021 03:33:26 +0000
parents f7145c77b7fb
children 84d1c31d0fad
line wrap: on
line source

#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include <pcsclite.h>
#include <winscard.h>

SCARDCONTEXT hContext;
SCARDHANDLE hCard;
char *reader_name_buf;

setup_pcsc_context()
{
	LONG rv;

	rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
	if (rv != SCARD_S_SUCCESS) {
		fprintf(stderr, "SCardEstablishContext: %s\n",
			pcsc_stringify_error(rv));
		exit(1);
	}
	return(0);
}

get_reader_name()
{
	LONG rv;
	DWORD dwReaders;

	rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
	if (rv != SCARD_S_SUCCESS) {
		fprintf(stderr, "SCardListReaders 1st call: %s\n",
			pcsc_stringify_error(rv));
		SCardReleaseContext(hContext);
		exit(1);
	}
	if (dwReaders < 1) {
		fprintf(stderr,
	"error: dwReaders returned by SCardListReaders() is less than 1\n");
		SCardReleaseContext(hContext);
		exit(1);
	}
	reader_name_buf = malloc(dwReaders);
	if (!reader_name_buf) {
		perror("malloc for readers list");
		SCardReleaseContext(hContext);
		exit(1);
	}
	reader_name_buf[0] = '\0';
	rv = SCardListReaders(hContext, NULL, reader_name_buf, &dwReaders);
	if (rv != SCARD_S_SUCCESS) {
		fprintf(stderr, "SCardListReaders 2nd call: %s\n",
			pcsc_stringify_error(rv));
		SCardReleaseContext(hContext);
		exit(1);
	}
	if (reader_name_buf[0] == '\0') {
		fprintf(stderr,
	"error: list returned by SCardListReaders() begins with a NUL byte\n");
		SCardReleaseContext(hContext);
		exit(1);
	}
	if (!memchr(reader_name_buf, 0, dwReaders)) {
		fprintf(stderr,
"error: list returned by SCardListReaders() does not contain a NUL byte\n");
		SCardReleaseContext(hContext);
		exit(1);
	}
	return(0);
}

connect_to_card()
{
	LONG rv;
	DWORD dwActiveProtocol;

	rv = SCardConnect(hContext, reader_name_buf, SCARD_SHARE_EXCLUSIVE,
			  SCARD_PROTOCOL_T0, &hCard, &dwActiveProtocol);
	if (rv != SCARD_S_SUCCESS) {
		fprintf(stderr, "SCardConnect: %s\n", pcsc_stringify_error(rv));
		SCardReleaseContext(hContext);
		exit(1);
	}
	return(0);
}