changeset 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 d368051576d0
children be9984600bf8
files libcommon/cardconnect.c libcommon/cardif.h misc/fc-pcsc-atr.c simtool/main.c uicc/main.c
diffstat 5 files changed, 36 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/libcommon/cardconnect.c	Fri Feb 12 16:19:11 2021 +0000
+++ b/libcommon/cardconnect.c	Fri Feb 12 17:09:01 2021 +0000
@@ -7,7 +7,7 @@
 
 SCARDCONTEXT hContext;
 SCARDHANDLE hCard;
-char *reader_name_buf;
+char *reader_list, *selected_reader;
 
 setup_pcsc_context()
 {
@@ -22,7 +22,7 @@
 	return(0);
 }
 
-get_reader_name()
+get_reader_list()
 {
 	LONG rv;
 	DWORD dwReaders;
@@ -40,33 +40,40 @@
 		SCardReleaseContext(hContext);
 		exit(1);
 	}
-	reader_name_buf = malloc(dwReaders);
-	if (!reader_name_buf) {
+	reader_list = malloc(dwReaders);
+	if (!reader_list) {
 		perror("malloc for readers list");
 		SCardReleaseContext(hContext);
 		exit(1);
 	}
-	reader_name_buf[0] = '\0';
-	rv = SCardListReaders(hContext, NULL, reader_name_buf, &dwReaders);
+	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);
 	}
-	if (reader_name_buf[0] == '\0') {
-		fprintf(stderr,
-	"error: list returned by SCardListReaders() begins with a NUL byte\n");
-		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;
 	}
-	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);
+	fprintf(stderr,
+		"error: requested reader #%u, but only %u readers found\n",
+		select_num, num);
+	exit(1);
 }
 
 connect_to_card()
@@ -74,7 +81,7 @@
 	LONG rv;
 	DWORD dwActiveProtocol;
 
-	rv = SCardConnect(hContext, reader_name_buf, SCARD_SHARE_EXCLUSIVE,
+	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));
--- a/libcommon/cardif.h	Fri Feb 12 16:19:11 2021 +0000
+++ b/libcommon/cardif.h	Fri Feb 12 17:09:01 2021 +0000
@@ -2,4 +2,4 @@
 
 extern SCARDCONTEXT hContext;
 extern SCARDHANDLE hCard;
-extern char *reader_name_buf;
+extern char *reader_list, *selected_reader;
--- a/misc/fc-pcsc-atr.c	Fri Feb 12 16:19:11 2021 +0000
+++ b/misc/fc-pcsc-atr.c	Fri Feb 12 17:09:01 2021 +0000
@@ -10,8 +10,9 @@
 	int rc;
 
 	setup_pcsc_context();
-	get_reader_name();
-	printf("Card reader name: %s\n", reader_name_buf);
+	get_reader_list();
+	select_reader_num(0);
+	printf("Card reader name: %s\n", selected_reader);
 	connect_to_card();
 	rc = retrieve_atr();
 	if (rc)
--- a/simtool/main.c	Fri Feb 12 16:19:11 2021 +0000
+++ b/simtool/main.c	Fri Feb 12 17:09:01 2021 +0000
@@ -12,9 +12,10 @@
 	int rc;
 
 	setup_pcsc_context();
-	get_reader_name();
+	get_reader_list();
+	select_reader_num(0);
 	if (argc < 2)
-		printf("Card reader name: %s\n", reader_name_buf);
+		printf("Card reader name: %s\n", selected_reader);
 	connect_to_card();
 	if (argc >= 2) {
 		rc = dispatch_ready_argv(argc - 1, argv + 1);
--- a/uicc/main.c	Fri Feb 12 16:19:11 2021 +0000
+++ b/uicc/main.c	Fri Feb 12 17:09:01 2021 +0000
@@ -12,9 +12,10 @@
 	int rc;
 
 	setup_pcsc_context();
-	get_reader_name();
+	get_reader_list();
+	select_reader_num(0);
 	if (argc < 2)
-		printf("Card reader name: %s\n", reader_name_buf);
+		printf("Card reader name: %s\n", selected_reader);
 	connect_to_card();
 	if (argc >= 2) {
 		rc = dispatch_ready_argv(argc - 1, argv + 1);