view pcsc/connect.c @ 44:0bc89d61fc59

top Makefile: add serial
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 20 Mar 2021 21:53:31 +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);
}