view pcsc/connect.c @ 86:de23872796cb

fc-uicc-tool: update-rec-imm and update-rec-fill ported over from fc-simtool
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 11 Apr 2021 03:19:22 +0000
parents f7a03e53bb2c
children
line wrap: on
line source

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

extern SCARDCONTEXT hContext;
extern char *reader_list;
extern unsigned select_reader_num;

SCARDHANDLE hCard;
char *selected_reader;

select_reader_by_num()
{
	char *cp;
	unsigned num;

	for (cp = reader_list, num = 0; *cp; num++) {
		if (num == select_reader_num) {
			selected_reader = cp;
			return(0);
		}
		cp += strlen(cp) + 1;
	}
	fprintf(stderr,
		"error: requested reader #%u, but only %u reader(s) found\n",
		select_reader_num, num);
	SCardReleaseContext(hContext);
	exit(1);
}

connect_to_card()
{
	LONG rv;
	DWORD dwActiveProtocol;

	rv = SCardConnect(hContext, selected_reader, 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);
}