# HG changeset patch # User Mychaela Falconia # Date 1611422468 0 # Node ID 1d7d8615d6282e373e4dca9026f17cd24aa555a6 # Parent d7a1e7a6d6ba16ef21c4af46a833b1e4c062a210 pcsc-test: some experiments to get pcsc-lite working diff -r d7a1e7a6d6ba -r 1d7d8615d628 .hgignore --- a/.hgignore Sun Dec 06 02:55:40 2020 +0000 +++ b/.hgignore Sat Jan 23 17:21:08 2021 +0000 @@ -22,3 +22,5 @@ ^lunalcd/gen-st-init$ ^lunalcd/ppmtocmd$ ^lunalcd/ppmtoimg$ + +^pcsc-test/pcsc-test[12]$ diff -r d7a1e7a6d6ba -r 1d7d8615d628 pcsc-test/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pcsc-test/Makefile Sat Jan 23 17:21:08 2021 +0000 @@ -0,0 +1,14 @@ +CC= gcc +CFLAGS= -O2 -I/usr/include/PCSC +PROGS= pcsc-test1 pcsc-test2 + +all: ${PROGS} + +pcsc-test1: pcsc-test1.c + ${CC} ${CFLAGS} -o $@ $@.c -lpcsclite + +pcsc-test2: pcsc-test2.c + ${CC} ${CFLAGS} -o $@ $@.c -lpcsclite + +clean: + rm -f ${PROGS} *.o diff -r d7a1e7a6d6ba -r 1d7d8615d628 pcsc-test/pcsc-test1.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pcsc-test/pcsc-test1.c Sat Jan 23 17:21:08 2021 +0000 @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include +#include + +main(argc, argv) + char **argv; +{ + SCARDCONTEXT hContext; + LONG rv; + DWORD dwReaders; + LPSTR mszReaders; + char *cp; + + rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext); + if (rv != SCARD_S_SUCCESS) { + fprintf(stderr, "SCardEstablishContext: %s\n", + pcsc_stringify_error(rv)); + exit(1); + } + 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); + } + printf("dwReaders = %u\n", (unsigned) dwReaders); + if (dwReaders < 1) { + fprintf(stderr, + "error: dwReaders returned by SCardListReaders() is less than 1\n"); + SCardReleaseContext(hContext); + exit(1); + } + mszReaders = malloc(dwReaders); + if (!mszReaders) { + perror("malloc for readers list"); + SCardReleaseContext(hContext); + exit(1); + } + *mszReaders = '\0'; + rv = SCardListReaders(hContext, NULL, mszReaders, &dwReaders); + if (rv != SCARD_S_SUCCESS) { + fprintf(stderr, "SCardListReaders 2nd call: %s\n", + pcsc_stringify_error(rv)); + SCardReleaseContext(hContext); + exit(1); + } + for (cp = mszReaders; *cp; ) { + printf("Reader: %s\n", cp); + cp += strlen(cp) + 1; + } + printf("End of list of readers\n"); + SCardReleaseContext(hContext); + exit(0); +} diff -r d7a1e7a6d6ba -r 1d7d8615d628 pcsc-test/pcsc-test2.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pcsc-test/pcsc-test2.c Sat Jan 23 17:21:08 2021 +0000 @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX_ATR_BYTES 33 + +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); +} + +retrieve_atr() +{ + u_char atrbuf[MAX_ATR_BYTES]; + LONG rv; + DWORD dwAttrLen; + unsigned n; + + dwAttrLen = MAX_ATR_BYTES; + rv = SCardGetAttrib(hCard, SCARD_ATTR_ATR_STRING, atrbuf, &dwAttrLen); + if (rv != SCARD_S_SUCCESS) { + fprintf(stderr, "SCardGetAttrib for ATR: %s\n", + pcsc_stringify_error(rv)); + return(-1); + } + printf("ATR:"); + for (n = 0; n < dwAttrLen; n++) + printf(" %02X", atrbuf[n]); + putchar('\n'); + return(0); +} + +main(argc, argv) + char **argv; +{ + setup_pcsc_context(); + get_reader_name(); + printf("Card reader name: %s\n", reader_name_buf); + connect_to_card(); + retrieve_atr(); + SCardDisconnect(hCard, SCARD_UNPOWER_CARD); + SCardReleaseContext(hContext); + exit(0); +}