changeset 45:9eb5460f51a6

main tools: support both pcsc and serial back ends
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 21 Mar 2021 01:56:49 +0000
parents 0bc89d61fc59
children e2ef4b8e4136
files libcommon/backend.c libcommon/globalopts.c
diffstat 2 files changed, 71 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/libcommon/backend.c	Sat Mar 20 21:53:31 2021 +0000
+++ b/libcommon/backend.c	Sun Mar 21 01:56:49 2021 +0000
@@ -7,12 +7,15 @@
 #include <stdlib.h>
 #include <unistd.h>
 
-extern unsigned calypso_fd, pcsc_reader_num;
+extern unsigned calypso_fd, pcsc_reader_num, serial_spenh;
+extern int use_pcsc;
+extern char *serial_device, *serial_baud;
 
 static char calypso_be_pathname[] = "/opt/freecalypso/bin/fcsim-calypso-be";
+static char serial_be_pathname[] = "/opt/freecalypso/bin/fcsim-serial-be";
 static char pcsc_be_pathname[] = "/opt/freecalypso/bin/fc-pcsc-backend";
 
-static char *backend_prog, *backend_argv[3], backend_optarg[16];
+static char *backend_prog, *backend_argv[4], backend_optarg[16];
 
 FILE *cpipeF, *rpipeF;
 
@@ -36,14 +39,36 @@
 	backend_argv[2] = 0;
 }
 
+static void
+setup_be_serial()
+{
+	backend_prog = serial_be_pathname;
+	backend_argv[0] = "fcsim-serial-be";
+	backend_argv[1] = serial_device;
+	if (serial_baud)
+		backend_argv[2] = serial_baud;
+	else if (serial_spenh) {
+		sprintf(backend_optarg, "9600,55800,%u", serial_spenh);
+		backend_argv[2] = backend_optarg;
+	} else
+		backend_argv[2] = "9600";
+	backend_argv[3] = 0;
+}
+
 launch_backend()
 {
 	int cpipe[2], rpipe[2], rc;
 
 	if (calypso_fd)
 		setup_be_calypso();
-	else
+	else if (use_pcsc)
 		setup_be_pcsc();
+	else if (serial_device)
+		setup_be_serial();
+	else {
+		fprintf(stderr, "error: no -d or -p target selected\n");
+		exit(1);
+	}
 	if (pipe(cpipe) < 0 || pipe(rpipe) < 0) {
 		perror("pipe");
 		exit(1);
--- a/libcommon/globalopts.c	Sat Mar 20 21:53:31 2021 +0000
+++ b/libcommon/globalopts.c	Sun Mar 21 01:56:49 2021 +0000
@@ -2,11 +2,14 @@
  * This module implements parsing of global command line options.
  */
 
+#include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 
-unsigned calypso_fd, pcsc_reader_num;
+unsigned calypso_fd, pcsc_reader_num, serial_spenh;
+int use_pcsc;
+char *serial_device, *serial_baud;
 
 parse_global_options(argc, argv)
 	char **argv;
@@ -14,12 +17,35 @@
 	extern char *optarg;
 	int c;
 
-	while ((c = getopt(argc, argv, "+C:p:")) != EOF) {
+	while ((c = getopt(argc, argv, "+b:C:d:e:p:")) != EOF) {
 		switch (c) {
+		case 'b':
+			serial_baud = optarg;
+			continue;
 		case 'C':
 			calypso_fd = atoi(optarg);
 			continue;
+		case 'd':
+			serial_device = optarg;
+			continue;
+		case 'e':
+			if (!isdigit(optarg[0]) || optarg[1]) {
+bad_minus_e:			fprintf(stderr, "error: bad -e argument\n");
+				exit(1);
+			}
+			serial_spenh = optarg[0] - '0';
+			switch (serial_spenh) {
+			case 1:
+			case 2:
+			case 4:
+			case 8:
+				break;
+			default:
+				goto bad_minus_e;
+			}
+			continue;
 		case 'p':
+			use_pcsc = 1;
 			pcsc_reader_num = atoi(optarg);
 			continue;
 		case '?':
@@ -28,5 +54,20 @@
 			exit(1);
 		}
 	}
+	if (serial_device && use_pcsc) {
+		fprintf(stderr,
+	"error: -d and -p target selection options are mutually exclusive\n");
+		exit(1);
+	}
+	if (!serial_device && (serial_baud || serial_spenh)) {
+		fprintf(stderr,
+		"error: -b and -e options are meaningless without -d\n");
+		exit(1);
+	}
+	if (serial_baud && serial_spenh) {
+		fprintf(stderr,
+			"error: -b and -e options are mutually exclusive\n");
+		exit(1);
+	}
 	return(0);
 }