annotate target-utils/simagent/exchange.c @ 816:b3724fe6c581

fc-tmsh bsim commands implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 14 May 2021 17:16:43 +0000
parents 56506e9165bc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
783
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements our main function: exchange of command
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * and response APDUs.
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include "types.h"
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 extern int sim_if_state;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 static void
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 finish_sw(sw1, data, datalen)
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 unsigned sw1, datalen;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 u8 *data;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 {
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 unsigned sw2, n;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 int rc;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 rc = rx_sim_byte_hl();
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 if (rc < 0)
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 return;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 sw2 = rc;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 for (n = 0; n < datalen; n++)
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 printf("%02X", data[n]);
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 printf("%02X%02X\n", sw1, sw2);
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 }
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 static void
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 exchange_data_out(ins, data, datalen)
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 unsigned ins, datalen;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 u8 *data;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 {
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 int rc;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 unsigned null_count, bytes_sent, ack, ack1;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 ack = ins & 0xFE;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 ack1 = ~ins & 0xFE;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 bytes_sent = 0;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 null_count = 0;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 for (;;) {
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 rc = rx_sim_byte_hl();
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 if (rc < 0)
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 return;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 if (rc == 0x60) {
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 null_count++;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 if (null_count >= 32) {
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 printf(
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 "ERROR: too many stalling NULL bytes received from SIM\n");
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 return;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 }
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 continue;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 }
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 if ((rc & 0xF0) == 0x60 || (rc & 0xF0) == 0x90) {
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 finish_sw(rc, 0, 0);
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 return;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 }
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 if ((rc & 0xFE) == ack) {
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 if (bytes_sent >= datalen) {
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 bad_xfer_req: printf(
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 "ERROR: SIM requests more xfer after we sent everything\n");
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 return;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 }
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 rc = send_to_sim(data + bytes_sent,
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 datalen - bytes_sent);
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 if (rc < 0)
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 return;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 bytes_sent = datalen;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 continue;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 }
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 if ((rc & 0xFE) == ack1) {
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 if (bytes_sent >= datalen)
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 goto bad_xfer_req;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 rc = send_to_sim(data + bytes_sent, 1);
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 if (rc < 0)
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 return;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 bytes_sent++;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 continue;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 }
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 printf("ERROR: non-understood procedure byte %02X\n", rc);
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 return;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 }
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 }
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 static void
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 exchange_data_in(ins, datalen)
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 unsigned ins, datalen;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 {
785
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
87 int rc;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
88 unsigned null_count, bytes_rcvd, ack, ack1;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
89 u8 data[256];
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
90
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
91 if (!datalen)
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
92 datalen = 256;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
93 ack = ins & 0xFE;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
94 ack1 = ~ins & 0xFE;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
95 bytes_rcvd = 0;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
96 null_count = 0;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
97 for (;;) {
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
98 rc = rx_sim_byte_hl();
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
99 if (rc < 0)
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
100 return;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
101 if (rc == 0x60) {
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
102 null_count++;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
103 if (null_count >= 32) {
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
104 printf(
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
105 "ERROR: too many stalling NULL bytes received from SIM\n");
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
106 return;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
107 }
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
108 continue;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
109 }
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
110 if ((rc & 0xF0) == 0x60 || (rc & 0xF0) == 0x90) {
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
111 finish_sw(rc, data, bytes_rcvd);
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
112 return;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
113 }
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
114 if ((rc & 0xFE) == ack) {
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
115 if (bytes_rcvd >= datalen) {
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
116 bad_xfer_req: printf(
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
117 "ERROR: SIM requests more xfer after we received all expected data\n");
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
118 return;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
119 }
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
120 while (bytes_rcvd < datalen) {
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
121 rc = rx_sim_byte_hl();
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
122 if (rc < 0)
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
123 return;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
124 data[bytes_rcvd++] = rc;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
125 }
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
126 continue;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
127 }
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
128 if ((rc & 0xFE) == ack1) {
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
129 if (bytes_rcvd >= datalen)
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
130 goto bad_xfer_req;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
131 rc = rx_sim_byte_hl();
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
132 if (rc < 0)
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
133 return;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
134 data[bytes_rcvd++] = rc;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
135 continue;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
136 }
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
137 printf("ERROR: non-understood procedure byte %02X\n", rc);
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
138 return;
56506e9165bc simagent: Rx data direction implemented for exchange
Mychaela Falconia <falcon@freecalypso.org>
parents: 784
diff changeset
139 }
783
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 }
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 void
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 cmd_exchange(argstr)
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 char *argstr;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 {
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146 u8 cmd[260];
784
839bf41e7be0 simagent: X command parsing bugfix
Mychaela Falconia <falcon@freecalypso.org>
parents: 783
diff changeset
147 unsigned count;
783
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 int rc;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
149
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
150 if (sim_if_state != 2) {
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
151 printf("ERROR: SIM interface is not up\n");
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
152 return;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
153 }
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
154 rc = decode_hex_string_arg(argstr, cmd, 260);
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
155 if (rc < 0)
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
156 return;
784
839bf41e7be0 simagent: X command parsing bugfix
Mychaela Falconia <falcon@freecalypso.org>
parents: 783
diff changeset
157 count = rc;
839bf41e7be0 simagent: X command parsing bugfix
Mychaela Falconia <falcon@freecalypso.org>
parents: 783
diff changeset
158 if (count < 5) {
783
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
159 printf("ERROR: command APDU is shorter than 5 bytes\n");
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
160 return;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
161 }
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
162 rc = flush_rx_fifo();
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
163 if (rc < 0)
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
164 return;
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
165 rc = send_to_sim(cmd, 5);
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
166 if (rc < 0)
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
167 return;
784
839bf41e7be0 simagent: X command parsing bugfix
Mychaela Falconia <falcon@freecalypso.org>
parents: 783
diff changeset
168 if (count > 5)
839bf41e7be0 simagent: X command parsing bugfix
Mychaela Falconia <falcon@freecalypso.org>
parents: 783
diff changeset
169 exchange_data_out(cmd[1], cmd + 5, count - 5);
783
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
170 else
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
171 exchange_data_in(cmd[1], cmd[4]);
c136a1a2474b simagent: initial implementation of APDU exchange
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
172 }