add sim switch and modem reset for OWHW

Change-Id: I9d395c2c7e10a0e7e5f7c84bc7d951431bfc68ba
diff --git a/src/simtrace2-remsim_client.c b/src/simtrace2-remsim_client.c
index 4e9b0a9..3fc5da0 100644
--- a/src/simtrace2-remsim_client.c
+++ b/src/simtrace2-remsim_client.c
@@ -18,6 +18,9 @@
 
 #include <unistd.h>
 #include <stdio.h>
+#include <linux/limits.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include <signal.h>
 #include <getopt.h>
 
@@ -25,6 +28,7 @@
 
 #include "simtrace2/libusb_util.h"
 #include "simtrace2/simtrace_prot.h"
+#include "simtrace2/simtrace_usb.h"
 #include "simtrace2/apdu_dispatch.h"
 #include "simtrace2/simtrace2-discovery.h"
 
@@ -797,6 +801,52 @@
 			goto close_exit;
 		}
 
+		// switch modem SIM port to emulated SIM on OWHW
+		if (USB_VENDOR_OPENMOKO == ifm->vendor && USB_PRODUCT_OWHW_SAM3 == ifm->product) { // we are on the OWHW
+			int modem = -1;
+			switch (ifm->interface) { // the USB interface indicates for which modem we want to emulate the SIM
+			case 0:
+				modem = 1;
+				break;
+			case 1:
+				modem = 2;
+				break;
+			default:
+				fprintf(stderr, "unknown GPIO for SIMtrace interface %d\n", ifm->interface);
+				goto close_exit;
+			}
+			//
+			char gpio_path[PATH_MAX];
+			snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/connect_st_usim%d/value", modem);
+			int connec_st_usim = open(gpio_path, O_WRONLY);
+			if (-1 == connec_st_usim) {
+				fprintf(stderr, "can't open GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
+				goto close_exit;
+			}
+			if (1 != write(connec_st_usim, "1", 1)) {
+				fprintf(stderr, "can't write GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
+				goto close_exit;
+			}
+			printf("switched modem %d to emulated USIM\n", modem);
+
+			snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/mdm%d_rst/value", modem);
+			int mdm_rst = open(gpio_path, O_WRONLY);
+			if (-1 == mdm_rst) {
+				fprintf(stderr, "can't open GPIO %s to reset modem %d\n", gpio_path, modem);
+				goto close_exit;
+			}
+			if (1 != write(mdm_rst, "1", 1)) {
+				fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
+				goto close_exit;
+			}
+			sleep(1); // wait a bit to ensure reset is effective
+			if (1 != write(mdm_rst, "0", 1)) {
+				fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
+				goto close_exit;
+			}
+			printf("modem %d reset\n", modem);
+		}
+
 		/* simulate card-insert to modem (owhw, not qmod) */
 		cardem_request_card_insert(ci, true);