changeset 785:56506e9165bc

simagent: Rx data direction implemented for exchange
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 13 Mar 2021 22:17:39 +0000
parents 839bf41e7be0
children b761029f9c03
files target-utils/simagent/exchange.c
diffstat 1 files changed, 53 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/target-utils/simagent/exchange.c	Sat Mar 13 22:08:48 2021 +0000
+++ b/target-utils/simagent/exchange.c	Sat Mar 13 22:17:39 2021 +0000
@@ -84,7 +84,59 @@
 exchange_data_in(ins, datalen)
 	unsigned ins, datalen;
 {
-	printf("ERROR: data in mode not implemented yet\n");
+	int rc;
+	unsigned null_count, bytes_rcvd, ack, ack1;
+	u8 data[256];
+
+	if (!datalen)
+		datalen = 256;
+	ack = ins & 0xFE;
+	ack1 = ~ins & 0xFE;
+	bytes_rcvd = 0;
+	null_count = 0;
+	for (;;) {
+		rc = rx_sim_byte_hl();
+		if (rc < 0)
+			return;
+		if (rc == 0x60) {
+			null_count++;
+			if (null_count >= 32) {
+				printf(
+		"ERROR: too many stalling NULL bytes received from SIM\n");
+				return;
+			}
+			continue;
+		}
+		if ((rc & 0xF0) == 0x60 || (rc & 0xF0) == 0x90) {
+			finish_sw(rc, data, bytes_rcvd);
+			return;
+		}
+		if ((rc & 0xFE) == ack) {
+			if (bytes_rcvd >= datalen) {
+bad_xfer_req:			printf(
+	"ERROR: SIM requests more xfer after we received all expected data\n");
+				return;
+			}
+			while (bytes_rcvd < datalen) {
+				rc = rx_sim_byte_hl();
+				if (rc < 0)
+					return;
+				data[bytes_rcvd++] = rc;
+			}
+			continue;
+		}
+		if ((rc & 0xFE) == ack1) {
+			if (bytes_rcvd >= datalen)
+				goto bad_xfer_req;
+			rc = rx_sim_byte_hl();
+			if (rc < 0)
+				return;
+			data[bytes_rcvd++] = rc;
+			continue;
+		}
+		printf("ERROR: non-understood procedure byte %02X\n", rc);
+		return;
+	}
 }
 
 void