FreeCalypso > hg > fc-pcsc-tools
view libcommon/cardconnect.c @ 30:84d1c31d0fad
first round of refactoring for selection among multiple readers
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 12 Feb 2021 17:09:01 +0000 |
parents | f7145c77b7fb |
children | b37fcb235848 |
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_list, *selected_reader; 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_list() { 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_list = malloc(dwReaders); if (!reader_list) { perror("malloc for readers list"); SCardReleaseContext(hContext); exit(1); } reader_list[0] = '\0'; rv = SCardListReaders(hContext, NULL, reader_list, &dwReaders); if (rv != SCARD_S_SUCCESS) { fprintf(stderr, "SCardListReaders 2nd call: %s\n", pcsc_stringify_error(rv)); SCardReleaseContext(hContext); exit(1); } return(0); } select_reader_num(select_num) unsigned select_num; { char *cp; unsigned num; for (cp = reader_list, num = 0; *cp; num++) { if (num == select_num) { selected_reader = cp; return(0); } cp += strlen(cp) + 1; } fprintf(stderr, "error: requested reader #%u, but only %u readers found\n", select_num, num); 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); }