blob: fa6b10244eb893b2318aace1d59c4a1c8cb2beb9 [file] [log] [blame]
Harald Welte3dcdd202019-03-09 13:06:46 +01001/* (C) 2018-2019 by Harald Welte <laforge@gnumonks.org>
2 * (C) 2018 by sysmocom - s.f.m.c. GmbH, Author: Kevin Redon
3 *
4 * All Rights Reserved
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
Kévin Redone0b837c2018-10-10 00:39:25 +020024
25#include <errno.h>
26#include <string.h>
27
28#include <talloc.h>
29
30#include <osmocom/core/msgb.h>
31#include <osmocom/core/fsm.h>
32#include <osmocom/core/utils.h>
33#include <osmocom/core/logging.h>
34#include <osmocom/core/application.h>
35
36#include <osmocom/abis/ipa.h>
37#include <osmocom/gsm/protocol/ipaccess.h>
38
39#include "rspro_util.h"
40#include "client.h"
Harald Welte61d98e92019-03-03 15:43:07 +010041#include "debug.h"
Kévin Redone0b837c2018-10-10 00:39:25 +020042
43#include <unistd.h>
44#include <stdio.h>
Kévin Redon3428e412018-10-11 19:14:00 +020045#include <linux/limits.h>
46#include <sys/stat.h>
47#include <fcntl.h>
Kévin Redone0b837c2018-10-10 00:39:25 +020048#include <signal.h>
49#include <getopt.h>
50
51#include <libusb.h>
52
Harald Weltefd5dafc2019-12-15 21:14:14 +010053#include <osmocom/usb/libusb.h>
54#include <osmocom/simtrace2/simtrace_prot.h>
55#include <osmocom/simtrace2/simtrace_usb.h>
56#include <osmocom/simtrace2/apdu_dispatch.h>
Kévin Redone0b837c2018-10-10 00:39:25 +020057
58#include <osmocom/core/gsmtap.h>
59#include <osmocom/core/gsmtap_util.h>
60#include <osmocom/core/utils.h>
61#include <osmocom/core/socket.h>
62#include <osmocom/core/msgb.h>
63#include <osmocom/sim/class_tables.h>
64#include <osmocom/sim/sim.h>
65
66/* transport to a SIMtrace device */
67struct st_transport {
68 /* USB */
69 struct libusb_device_handle *usb_devh;
70 struct {
71 uint8_t in;
72 uint8_t out;
73 uint8_t irq_in;
74 } usb_ep;
Kévin Redone0b837c2018-10-10 00:39:25 +020075};
76
77/* a SIMtrace slot; communicates over a transport */
78struct st_slot {
79 /* transport through which the slot can be reached */
80 struct st_transport *transp;
81 /* number of the slot within the transport */
82 uint8_t slot_nr;
83};
84
85/* One istance of card emulation */
86struct cardem_inst {
87 /* slot on which this card emulation instance runs */
88 struct st_slot *slot;
89};
90
91/* global GSMTAP instance */
92static struct gsmtap_inst *g_gti;
93
94static struct bankd_client *g_client;
95static void *g_tall_ctx;
96void __thread *talloc_asn1_ctx;
97int asn_debug;
98
Harald Welte3f09f632019-03-31 15:51:13 +020099__attribute__((unused)) static int gsmtap_send_sim(const uint8_t *apdu, unsigned int len)
Kévin Redone0b837c2018-10-10 00:39:25 +0200100{
101 struct gsmtap_hdr *gh;
102 unsigned int gross_len = len + sizeof(*gh);
103 uint8_t *buf = malloc(gross_len);
104 int rc;
105
106 if (!buf)
107 return -ENOMEM;
108
109 memset(buf, 0, sizeof(*gh));
110 gh = (struct gsmtap_hdr *) buf;
111 gh->version = GSMTAP_VERSION;
112 gh->hdr_len = sizeof(*gh)/4;
113 gh->type = GSMTAP_TYPE_SIM;
114
115 memcpy(buf + sizeof(*gh), apdu, len);
116
117 rc = write(gsmtap_inst_fd(g_gti), buf, gross_len);
118 if (rc < 0) {
119 perror("write gsmtap");
120 free(buf);
121 return rc;
122 }
123
124 free(buf);
125 return 0;
126}
127
128/***********************************************************************
Harald Weltef14dc042019-03-28 20:29:36 +0100129 * SIMTRACE core protocol
Kévin Redone0b837c2018-10-10 00:39:25 +0200130 ***********************************************************************/
131
132/*! \brief allocate a message buffer for simtrace use */
133static struct msgb *st_msgb_alloc(void)
134{
135 return msgb_alloc_headroom(1024+32, 32, "SIMtrace");
136}
137
138#if 0
139static void apdu_out_cb(uint8_t *buf, unsigned int len, void *user_data)
140{
141 printf("APDU: %s\n", osmo_hexdump(buf, len));
142 gsmtap_send_sim(buf, len);
143}
144#endif
145
Harald Welte32e2e002019-12-15 23:01:54 +0100146static void usb_out_xfer_cb(struct libusb_transfer *xfer)
147{
148 struct msgb *msg = xfer->user_data;
149
150 switch (xfer->status) {
151 case LIBUSB_TRANSFER_COMPLETED:
152 break;
153 case LIBUSB_TRANSFER_NO_DEVICE:
154 fprintf(stderr, "USB device disappeared\n");
155 exit(23);
156 break;
157 default:
158 osmo_panic("USB OUT transfer failed, status=%u\n", xfer->status);
159 break;
160 }
161
162 msgb_free(msg);
163 libusb_free_transfer(xfer);
164}
165
Kévin Redone0b837c2018-10-10 00:39:25 +0200166/*! \brief Transmit a given command to the SIMtrace2 device */
167int st_transp_tx_msg(struct st_transport *transp, struct msgb *msg)
168{
Harald Welte32e2e002019-12-15 23:01:54 +0100169 struct libusb_transfer *xfer;
Kévin Redone0b837c2018-10-10 00:39:25 +0200170 int rc;
171
Kévin Redon21e31de2018-10-11 17:25:58 +0200172 printf("SIMtrace <- %s\n", msgb_hexdump(msg));
Kévin Redone0b837c2018-10-10 00:39:25 +0200173
Harald Welte32e2e002019-12-15 23:01:54 +0100174 xfer = libusb_alloc_transfer(0);
175 OSMO_ASSERT(xfer);
176 xfer->dev_handle = transp->usb_devh;
177 xfer->flags = 0;
178 xfer->type = LIBUSB_TRANSFER_TYPE_BULK;
179 xfer->endpoint = transp->usb_ep.out;
180 xfer->timeout = 1000;
181 xfer->user_data = msg;
182 xfer->length = msgb_length(msg);
183 xfer->buffer = msgb_data(msg);
184 xfer->callback = usb_out_xfer_cb;
Kévin Redone0b837c2018-10-10 00:39:25 +0200185
Harald Welte32e2e002019-12-15 23:01:54 +0100186 /* submit the OUT transfer */
187 rc = libusb_submit_transfer(xfer);
188 OSMO_ASSERT(rc == 0);
Kévin Redone0b837c2018-10-10 00:39:25 +0200189
Kévin Redone0b837c2018-10-10 00:39:25 +0200190 return rc;
191}
192
193static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type,
194 uint8_t slot_nr)
195{
196 struct simtrace_msg_hdr *sh;
197
198 sh = (struct simtrace_msg_hdr *) msgb_push(msg, sizeof(*sh));
199 memset(sh, 0, sizeof(*sh));
200 sh->msg_class = msg_class;
201 sh->msg_type = msg_type;
202 sh->slot_nr = slot_nr;
203 sh->msg_len = msgb_length(msg);
204
205 return sh;
206}
207
208/* transmit a given message to a specified slot. Expects all headers
209 * present before calling the function */
210int st_slot_tx_msg(struct st_slot *slot, struct msgb *msg,
211 uint8_t msg_class, uint8_t msg_type)
212{
213 st_push_hdr(msg, msg_class, msg_type, slot->slot_nr);
214
215 return st_transp_tx_msg(slot->transp, msg);
216}
217
218/***********************************************************************
219 * Card Emulation protocol
220 ***********************************************************************/
221
222
223/*! \brief Request the SIMtrace2 to generate a card-insert signal */
224static int cardem_request_card_insert(struct cardem_inst *ci, bool inserted)
225{
226 struct msgb *msg = st_msgb_alloc();
227 struct cardemu_usb_msg_cardinsert *cins;
228
229 cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins));
230 memset(cins, 0, sizeof(*cins));
231 if (inserted)
232 cins->card_insert = 1;
233
234 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT);
235}
236
237/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */
238static int cardem_request_pb_and_rx(struct cardem_inst *ci, uint8_t pb, uint8_t le)
239{
240 struct msgb *msg = st_msgb_alloc();
241 struct cardemu_usb_msg_tx_data *txd;
242 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
243
Kévin Redon21e31de2018-10-11 17:25:58 +0200244 printf("SIMtrace <= %s(%02x, %d)\n", __func__, pb, le);
Kévin Redone0b837c2018-10-10 00:39:25 +0200245
246 memset(txd, 0, sizeof(*txd));
247 txd->data_len = 1;
248 txd->flags = CEMU_DATA_F_PB_AND_RX;
249 /* one data byte */
250 msgb_put_u8(msg, pb);
251
252 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
253}
254
255/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */
256static int cardem_request_pb_and_tx(struct cardem_inst *ci, uint8_t pb,
Kévin Redonf120b642018-10-15 19:53:02 +0200257 const uint8_t *data, uint16_t data_len_in)
Kévin Redone0b837c2018-10-10 00:39:25 +0200258{
259 struct msgb *msg = st_msgb_alloc();
260 struct cardemu_usb_msg_tx_data *txd;
261 uint8_t *cur;
262
263 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
264
Kévin Redon21e31de2018-10-11 17:25:58 +0200265 printf("SIMtrace <= %s(%02x, %s, %d)\n", __func__, pb,
Kévin Redone0b837c2018-10-10 00:39:25 +0200266 osmo_hexdump(data, data_len_in), data_len_in);
267
268 memset(txd, 0, sizeof(*txd));
269 txd->data_len = 1 + data_len_in;
270 txd->flags = CEMU_DATA_F_PB_AND_TX;
271 /* procedure byte */
272 msgb_put_u8(msg, pb);
273 /* data */
274 cur = msgb_put(msg, data_len_in);
275 memcpy(cur, data, data_len_in);
276
277 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
278}
279
280/*! \brief Request the SIMtrace2 to send a Status Word */
281static int cardem_request_sw_tx(struct cardem_inst *ci, const uint8_t *sw)
282{
283 struct msgb *msg = st_msgb_alloc();
284 struct cardemu_usb_msg_tx_data *txd;
285 uint8_t *cur;
286
287 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
288
Kévin Redon21e31de2018-10-11 17:25:58 +0200289 printf("SIMtrace <= %s(%02x %02x)\n", __func__, sw[0], sw[1]);
Kévin Redone0b837c2018-10-10 00:39:25 +0200290
291 memset(txd, 0, sizeof(*txd));
292 txd->data_len = 2;
293 txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL;
294 cur = msgb_put(msg, 2);
295 cur[0] = sw[0];
296 cur[1] = sw[1];
297
298 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
299}
300
Kévin Redon206c3d72018-11-12 22:51:37 +0100301// FIXME check if the ATR actually includes a checksum
Harald Welte3f09f632019-03-31 15:51:13 +0200302__attribute__((unused)) static void atr_update_csum(uint8_t *atr, unsigned int atr_len)
Kévin Redone0b837c2018-10-10 00:39:25 +0200303{
304 uint8_t csum = 0;
305 int i;
306
307 for (i = 1; i < atr_len - 1; i++)
308 csum = csum ^ atr[i];
309
310 atr[atr_len-1] = csum;
311}
312
313static int cardem_request_set_atr(struct cardem_inst *ci, const uint8_t *atr, unsigned int atr_len)
314{
315 struct msgb *msg = st_msgb_alloc();
316 struct cardemu_usb_msg_set_atr *satr;
317 uint8_t *cur;
318
319 satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr));
320
Kévin Redon21e31de2018-10-11 17:25:58 +0200321 printf("SIMtrace <= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
Kévin Redone0b837c2018-10-10 00:39:25 +0200322
323 memset(satr, 0, sizeof(*satr));
324 satr->atr_len = atr_len;
325 cur = msgb_put(msg, atr_len);
326 memcpy(cur, atr, atr_len);
327
328 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR);
329}
330
331/***********************************************************************
332 * Modem Control protocol
333 ***********************************************************************/
334
335static int _modem_reset(struct st_slot *slot, uint8_t asserted, uint16_t pulse_ms)
336{
337 struct msgb *msg = st_msgb_alloc();
338 struct st_modem_reset *sr ;
339
340 sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr));
341 sr->asserted = asserted;
342 sr->pulse_duration_msec = pulse_ms;
343
344 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET);
345}
346
347/*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/
348int st_modem_reset_pulse(struct st_slot *slot, uint16_t duration_ms)
349{
350 return _modem_reset(slot, 2, duration_ms);
351}
352
353/*! \brief assert the RESET line of the modem */
354int st_modem_reset_active(struct st_slot *slot)
355{
356 return _modem_reset(slot, 1, 0);
357}
358
359/*! \brief de-assert the RESET line of the modem */
360int st_modem_reset_inactive(struct st_slot *slot)
361{
362 return _modem_reset(slot, 0, 0);
363}
364
365static int _modem_sim_select(struct st_slot *slot, uint8_t remote_sim)
366{
367 struct msgb *msg = st_msgb_alloc();
368 struct st_modem_sim_select *ss;
369
370 ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss));
371 ss->remote_sim = remote_sim;
372
373 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT);
374}
375
376/*! \brief select local (physical) SIM for given slot */
377int st_modem_sim_select_local(struct st_slot *slot)
378{
379 return _modem_sim_select(slot, 0);
380}
381
382/*! \brief select remote (emulated/forwarded) SIM for given slot */
383int st_modem_sim_select_remote(struct st_slot *slot)
384{
385 return _modem_sim_select(slot, 1);
386}
387
388/*! \brief Request slot to send us status information about the modem */
389int st_modem_get_status(struct st_slot *slot)
390{
391 struct msgb *msg = st_msgb_alloc();
392
393 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS);
394}
395
396
397/***********************************************************************
398 * Incoming Messages
399 ***********************************************************************/
400
401/*! \brief Process a STATUS message from the SIMtrace2 */
402static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len)
403{
404 struct cardemu_usb_msg_status *status;
405 status = (struct cardemu_usb_msg_status *) buf;
406
Kévin Redon21e31de2018-10-11 17:25:58 +0200407 printf("SIMtrace => STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
Kévin Redone0b837c2018-10-10 00:39:25 +0200408 status->flags, status->fi, status->di, status->wi,
409 status->waiting_time);
410
411 return 0;
412}
413
414/*! \brief Process a PTS indication message from the SIMtrace2 */
415static int process_do_pts(struct cardem_inst *ci, uint8_t *buf, int len)
416{
417 struct cardemu_usb_msg_pts_info *pts;
418 pts = (struct cardemu_usb_msg_pts_info *) buf;
419
Kévin Redon21e31de2018-10-11 17:25:58 +0200420 printf("SIMtrace => PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req)));
Kévin Redone0b837c2018-10-10 00:39:25 +0200421
422 return 0;
423}
424
425/*! \brief Process a ERROR indication message from the SIMtrace2 */
Harald Welte3f09f632019-03-31 15:51:13 +0200426__attribute__((unused)) static int process_do_error(struct cardem_inst *ci, uint8_t *buf, int len)
Kévin Redone0b837c2018-10-10 00:39:25 +0200427{
428 struct cardemu_usb_msg_error *err;
429 err = (struct cardemu_usb_msg_error *) buf;
430
Kévin Redon21e31de2018-10-11 17:25:58 +0200431 printf("SIMtrace => ERROR: %u/%u/%u: %s\n",
Kévin Redone0b837c2018-10-10 00:39:25 +0200432 err->severity, err->subsystem, err->code,
433 err->msg_len ? (char *)err->msg : "");
434
435 return 0;
436}
437
Harald Weltefd5dafc2019-12-15 21:14:14 +0100438static struct osmo_apdu_context ac; // this will hold the complete APDU (across calls)
Kévin Redonbc08db52018-10-11 08:41:00 +0200439
Kévin Redone0b837c2018-10-10 00:39:25 +0200440/*! \brief Process a RX-DATA indication message from the SIMtrace2 */
441static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len)
442{
Kévin Redonbc08db52018-10-11 08:41:00 +0200443 struct cardemu_usb_msg_rx_data *data = (struct cardemu_usb_msg_rx_data *) buf; // cast the data from the USB message
Kévin Redone0b837c2018-10-10 00:39:25 +0200444 int rc;
445
Kévin Redon21e31de2018-10-11 17:25:58 +0200446 printf("SIMtrace => DATA: flags=%x, %s: ", data->flags,
Kévin Redone0b837c2018-10-10 00:39:25 +0200447 osmo_hexdump(data->data, data->data_len));
448
Harald Weltefd5dafc2019-12-15 21:14:14 +0100449 rc = osmo_apdu_segment_in(&ac, data->data, data->data_len,
450 data->flags & CEMU_DATA_F_TPDU_HDR); // parse the APDU data in the USB message
Kévin Redone0b837c2018-10-10 00:39:25 +0200451
Kévin Redonbc08db52018-10-11 08:41:00 +0200452 if (rc & APDU_ACT_TX_CAPDU_TO_CARD) { // there is no pending data coming from the modem
Harald Welte2ea20b92019-03-30 08:33:49 +0100453 uint8_t apdu_command[sizeof(ac.hdr) + ac.lc.tot]; // to store the APDU command to send
Kévin Redonbc08db52018-10-11 08:41:00 +0200454 memcpy(apdu_command, &ac.hdr, sizeof(ac.hdr)); // copy APDU command header
Kévin Redone0b837c2018-10-10 00:39:25 +0200455 if (ac.lc.tot) {
Kévin Redonbc08db52018-10-11 08:41:00 +0200456 memcpy(apdu_command + sizeof(ac.hdr), ac.dc, ac.lc.tot); // copy APDU command data
Kévin Redone0b837c2018-10-10 00:39:25 +0200457 }
Kévin Redonbc08db52018-10-11 08:41:00 +0200458 // send APDU to card
Harald Welte9cf013a2019-03-11 22:19:19 +0100459 BankSlot_t bslot;
460 bank_slot2rspro(&bslot, &g_client->bankd_slot);
461 RsproPDU_t *pdu = rspro_gen_TpduModem2Card(g_client->srv_conn.clslot, &bslot, apdu_command, sizeof(ac.hdr) + ac.lc.tot); // create RSPRO packet
Harald Welte3e9860b2019-12-02 23:04:54 +0100462 server_conn_send_rspro(&g_client->bankd_conn, pdu);
Kévin Redonbc08db52018-10-11 08:41:00 +0200463 // the response will come separately
Kévin Redonbc08db52018-10-11 08:41:00 +0200464 } else if (ac.lc.tot > ac.lc.cur) { // there is pending data from the modem
465 cardem_request_pb_and_rx(ci, ac.hdr.ins, ac.lc.tot - ac.lc.cur); // send procedure byte to get remaining data
Kévin Redone0b837c2018-10-10 00:39:25 +0200466 }
467 return 0;
468}
469
470#if 0
471 case SIMTRACE_CMD_DO_ERROR
472 rc = process_do_error(ci, buf, len);
473 break;
474#endif
475
476/*! \brief Process an incoming message from the SIMtrace2 */
477static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len)
478{
479 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
480 int rc;
481
Kévin Redon21e31de2018-10-11 17:25:58 +0200482 printf("SIMtrace -> %s\n", osmo_hexdump(buf, len));
Kévin Redone0b837c2018-10-10 00:39:25 +0200483
484 buf += sizeof(*sh);
485
486 switch (sh->msg_type) {
487 case SIMTRACE_MSGT_BD_CEMU_STATUS:
488 rc = process_do_status(ci, buf, len);
489 break;
490 case SIMTRACE_MSGT_DO_CEMU_PTS:
491 rc = process_do_pts(ci, buf, len);
492 break;
493 case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
494 rc = process_do_rx_da(ci, buf, len);
495 break;
496 default:
497 printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
498 rc = -1;
499 break;
500 }
501
502 return rc;
503}
504
Harald Welte32e2e002019-12-15 23:01:54 +0100505static void usb_in_xfer_cb(struct libusb_transfer *xfer)
Kévin Redone0b837c2018-10-10 00:39:25 +0200506{
Harald Welte32e2e002019-12-15 23:01:54 +0100507 struct cardem_inst *ci = xfer->user_data;
Kévin Redone0b837c2018-10-10 00:39:25 +0200508 int rc;
509
Harald Welte32e2e002019-12-15 23:01:54 +0100510 switch (xfer->status) {
511 case LIBUSB_TRANSFER_COMPLETED:
512 /* hand the message up the stack */
513 process_usb_msg(ci, xfer->buffer, xfer->actual_length);
514 break;
515 case LIBUSB_TRANSFER_NO_DEVICE:
516 fprintf(stderr, "USB device disappeared\n");
517 exit(23);
518 break;
519 default:
520 osmo_panic("USB IN transfer failed, status=%u\n", xfer->status);
521 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200522 }
Harald Welte32e2e002019-12-15 23:01:54 +0100523
524 /* re-submit the IN transfer */
525 rc = libusb_submit_transfer(xfer);
526 OSMO_ASSERT(rc == 0);
Kévin Redone0b837c2018-10-10 00:39:25 +0200527}
528
Harald Welte32e2e002019-12-15 23:01:54 +0100529
530static void allocate_and_submit_in(struct cardem_inst *ci)
531{
532 struct st_transport *transp = ci->slot->transp;
533 struct libusb_transfer *xfer;
534 int rc;
535
536 xfer = libusb_alloc_transfer(0);
537 OSMO_ASSERT(xfer);
538 xfer->dev_handle = transp->usb_devh;
539 xfer->flags = 0;
540 xfer->type = LIBUSB_TRANSFER_TYPE_BULK;
541 xfer->endpoint = transp->usb_ep.in;
542 xfer->timeout = 0;
543 xfer->user_data = ci;
544 xfer->length = 16*256;
545
546 xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length);
547 OSMO_ASSERT(xfer->buffer);
548 xfer->callback = usb_in_xfer_cb;
549
550 /* submit the IN transfer */
551 rc = libusb_submit_transfer(xfer);
552 OSMO_ASSERT(rc == 0);
553}
554
555
556static void usb_irq_xfer_cb(struct libusb_transfer *xfer)
557{
558 int rc;
559
560 switch (xfer->status) {
561 case LIBUSB_TRANSFER_COMPLETED:
562 /* FIXME: do something with the received data */
563 break;
564 case LIBUSB_TRANSFER_NO_DEVICE:
565 fprintf(stderr, "USB device disappeared\n");
566 exit(23);
567 break;
568 default:
569 osmo_panic("USB IRQ transfer failed, status=%u\n", xfer->status);
570 break;
571 }
572
573 /* re-submit the IN transfer */
574 rc = libusb_submit_transfer(xfer);
575 OSMO_ASSERT(rc == 0);
576}
577
578
579static void allocate_and_submit_irq(struct cardem_inst *ci)
580{
581 struct st_transport *transp = ci->slot->transp;
582 struct libusb_transfer *xfer;
583 int rc;
584
585 xfer = libusb_alloc_transfer(0);
586 OSMO_ASSERT(xfer);
587 xfer->dev_handle = transp->usb_devh;
588 xfer->flags = 0;
589 xfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
590 xfer->endpoint = transp->usb_ep.irq_in;
591 xfer->timeout = 0;
592 xfer->user_data = ci;
593 xfer->length = 64;
594
595 xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length);
596 OSMO_ASSERT(xfer->buffer);
597 xfer->callback = usb_irq_xfer_cb;
598
599 /* submit the IN transfer */
600 rc = libusb_submit_transfer(xfer);
601 OSMO_ASSERT(rc == 0);
602}
603
604
Kévin Redone0b837c2018-10-10 00:39:25 +0200605static struct st_transport _transp;
606
607static struct st_slot _slot = {
608 .transp = &_transp,
609 .slot_nr = 0,
610};
611
612struct cardem_inst _ci = {
613 .slot = &_slot,
614};
615
616struct cardem_inst *ci = &_ci;
617
618static void signal_handler(int signal)
619{
620 switch (signal) {
621 case SIGINT:
622 cardem_request_card_insert(ci, false);
623 exit(0);
624 break;
625 default:
626 break;
627 }
628}
629
630/** remsim_client **/
631
Harald Welte3e9860b2019-12-02 23:04:54 +0100632static int bankd_handle_tpduCardToModem(struct bankd_client *bc, const RsproPDU_t *pdu)
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200633{
634 OSMO_ASSERT(pdu);
635 OSMO_ASSERT(RsproPDUchoice_PR_tpduCardToModem == pdu->msg.present);
636
637 const struct TpduCardToModem *card2modem = &pdu->msg.choice.tpduCardToModem;
638 if (card2modem->data.size < 2) { // at least the two SW bytes are needed
639 return -1;
640 }
641
642 // save SW to our current APDU context
643 ac.sw[0] = card2modem->data.buf[card2modem->data.size - 2];
644 ac.sw[1] = card2modem->data.buf[card2modem->data.size - 1];
Kévin Redon21e31de2018-10-11 17:25:58 +0200645 printf("SIMtrace <= SW=0x%02x%02x, len_rx=%d\n", ac.sw[0], ac.sw[1], card2modem->data.size - 2);
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200646 if (card2modem->data.size > 2) { // send PB and data to modem
647 cardem_request_pb_and_tx(ci, ac.hdr.ins, card2modem->data.buf, card2modem->data.size - 2);
648 }
649 cardem_request_sw_tx(ci, ac.sw); // send SW to modem
650
651 return 0;
652}
653
Harald Welte3e9860b2019-12-02 23:04:54 +0100654static int bankd_handle_setAtrReq(struct bankd_client *bc, const RsproPDU_t *pdu)
Harald Weltefa365592019-03-28 20:28:57 +0100655{
656 RsproPDU_t *resp;
657 int rc;
658
659 OSMO_ASSERT(pdu);
660 OSMO_ASSERT(RsproPDUchoice_PR_setAtrReq == pdu->msg.present);
661
662 /* FIXME: is this permitted at any time by the SIMtrace2 cardemfirmware? */
663 rc = cardem_request_set_atr(ci, pdu->msg.choice.setAtrReq.atr.buf,
664 pdu->msg.choice.setAtrReq.atr.size);
665 if (rc == 0)
666 resp = rspro_gen_SetAtrRes(ResultCode_ok);
667 else
668 resp = rspro_gen_SetAtrRes(ResultCode_cardTransmissionError);
669 if (!resp)
670 return -ENOMEM;
Harald Welte3e9860b2019-12-02 23:04:54 +0100671 server_conn_send_rspro(&g_client->bankd_conn, resp);
Harald Weltefa365592019-03-28 20:28:57 +0100672
673 return 0;
674}
675
Harald Welte3e9860b2019-12-02 23:04:54 +0100676/* handle incoming message from bankd */
677static int bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu)
Kévin Redone0b837c2018-10-10 00:39:25 +0200678{
Kévin Redone0b837c2018-10-10 00:39:25 +0200679 switch (pdu->msg.present) {
680 case RsproPDUchoice_PR_connectClientRes:
Harald Weltee56f2b92019-03-02 17:02:13 +0100681 /* Store 'identity' of bankd to in peer_comp_id */
Harald Welte3e9860b2019-12-02 23:04:54 +0100682 rspro_comp_id_retrieve(&bankdc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
683 osmo_fsm_inst_dispatch(bankdc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
Kévin Redone0b837c2018-10-10 00:39:25 +0200684 break;
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200685 case RsproPDUchoice_PR_tpduCardToModem: // APDU response from card received
Harald Welte3e9860b2019-12-02 23:04:54 +0100686 bankd_handle_tpduCardToModem(g_client, pdu);
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200687 break;
Harald Weltefa365592019-03-28 20:28:57 +0100688 case RsproPDUchoice_PR_setAtrReq:
Harald Welte3e9860b2019-12-02 23:04:54 +0100689 bankd_handle_setAtrReq(g_client, pdu);
Harald Weltefa365592019-03-28 20:28:57 +0100690 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200691 default:
Harald Welte3e9860b2019-12-02 23:04:54 +0100692 LOGPFSML(bankdc->fi, LOGL_ERROR, "Unknown/Unsuppoerted RSPRO PDU %s\n",
693 rspro_msgt_name(pdu));
Kévin Redone0b837c2018-10-10 00:39:25 +0200694 return -1;
695 }
696
697 return 0;
698}
699
Harald Weltee56f2b92019-03-02 17:02:13 +0100700/* handle incoming messages from server */
701static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
702{
703 RsproPDU_t *resp;
704
705 switch (pdu->msg.present) {
706 case RsproPDUchoice_PR_connectClientRes:
707 /* Store 'identity' of server in srvc->peer_comp_id */
708 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
709 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
710 break;
Harald Welted571a3e2019-03-11 22:09:50 +0100711 case RsproPDUchoice_PR_configClientIdReq:
Harald Weltee56f2b92019-03-02 17:02:13 +0100712 /* store/set the clientID as instructed by the server */
Harald Welteec628e92019-03-08 22:18:31 +0100713 if (!g_client->srv_conn.clslot)
714 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
Harald Welted571a3e2019-03-11 22:09:50 +0100715 *g_client->srv_conn.clslot = pdu->msg.choice.configClientIdReq.clientSlot;
Harald Welte3e9860b2019-12-02 23:04:54 +0100716 if (!g_client->bankd_conn.clslot)
717 g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t);
718 *g_client->bankd_conn.clslot = *g_client->srv_conn.clslot;
Harald Welted571a3e2019-03-11 22:09:50 +0100719 /* send response to server */
720 resp = rspro_gen_ConfigClientIdRes(ResultCode_ok);
721 server_conn_send_rspro(srvc, resp);
722 break;
723 case RsproPDUchoice_PR_configClientBankReq:
Harald Weltee56f2b92019-03-02 17:02:13 +0100724 /* store/set the bankd ip/port as instructed by the server */
Harald Welte3e9860b2019-12-02 23:04:54 +0100725 osmo_talloc_replace_string(g_client, &g_client->bankd_conn.server_host,
Harald Welted571a3e2019-03-11 22:09:50 +0100726 rspro_IpAddr2str(&pdu->msg.choice.configClientBankReq.bankd.ip));
Harald Welte9cf013a2019-03-11 22:19:19 +0100727 rspro2bank_slot(&g_client->bankd_slot, &pdu->msg.choice.configClientBankReq.bankSlot);
Harald Welte3e9860b2019-12-02 23:04:54 +0100728 g_client->bankd_conn.server_port = pdu->msg.choice.configClientBankReq.bankd.port;
Harald Weltee56f2b92019-03-02 17:02:13 +0100729 /* instruct bankd FSM to connect */
Harald Welte3e9860b2019-12-02 23:04:54 +0100730 osmo_fsm_inst_dispatch(g_client->bankd_conn.fi, SRVC_E_ESTABLISH, NULL);
Harald Weltee56f2b92019-03-02 17:02:13 +0100731 /* send response to server */
Harald Welted571a3e2019-03-11 22:09:50 +0100732 resp = rspro_gen_ConfigClientBankRes(ResultCode_ok);
Harald Weltea844bb02019-03-09 13:38:50 +0100733 server_conn_send_rspro(srvc, resp);
Harald Weltee56f2b92019-03-02 17:02:13 +0100734 break;
735 default:
Harald Welte8d8d4f12019-03-27 22:50:39 +0100736 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %s\n",
737 rspro_msgt_name(pdu));
Harald Weltee56f2b92019-03-02 17:02:13 +0100738 return -1;
739 }
740
741 return 0;
742}
743
Harald Weltece638d82019-03-17 09:36:04 +0100744static void handle_sig_usr1(int signal)
745{
746 OSMO_ASSERT(signal == SIGUSR1);
Harald Welteb54a51e2019-03-31 15:57:59 +0200747 talloc_report_full(g_tall_ctx, stderr);
Harald Weltece638d82019-03-17 09:36:04 +0100748}
Harald Weltee56f2b92019-03-02 17:02:13 +0100749
Kévin Redon3ec265b2018-10-11 17:30:33 +0200750static void print_welcome(void)
751{
752 printf("simtrace2-remsim-client - Remote SIM card client for SIMtrace\n"
Harald Welte32e2e002019-12-15 23:01:54 +0100753 "(C) 2010-2019, Harald Welte <laforge@gnumonks.org>\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200754 "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
755}
756
757static void print_help(void)
758{
Harald Weltee56f2b92019-03-02 17:02:13 +0100759 printf( "\t-s\t--server-host HOST\n"
Kévin Redonda1854c2019-09-17 14:16:54 +0200760 "\t-p\t--server-port PORT\n"
Harald Welte72cde102019-03-30 10:43:06 +0100761 "\t-c\t--client-id <0-65535>\n"
Kévin Redonda1854c2019-09-17 14:16:54 +0200762 "\t-n\t--client-slot <0-65535>\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200763 "\t-h\t--help\n"
Harald Weltecd7fcd72019-12-03 21:29:07 +0100764 "\t-v\t--version\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200765 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
766 "\t-k\t--keep-running\n"
767 "\t-V\t--usb-vendor\tVENDOR_ID\n"
768 "\t-P\t--usb-product\tPRODUCT_ID\n"
769 "\t-C\t--usb-config\tCONFIG_ID\n"
770 "\t-I\t--usb-interface\tINTERFACE_ID\n"
771 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
772 "\t-A\t--usb-address\tADDRESS\n"
773 "\t-H\t--usb-path\tPATH\n"
Kévin Redon206c3d72018-11-12 22:51:37 +0100774 "\t-a\t--atr\tATR\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200775 "\n"
776 );
777}
778
779static const struct option opts[] = {
Harald Weltee56f2b92019-03-02 17:02:13 +0100780 { "server-host", 1, 0, 's' },
781 { "server-port", 1, 0, 'p' },
Harald Welte72cde102019-03-30 10:43:06 +0100782 { "client-id", 1, 0, 'c' },
783 { "client-slot", 1, 0, 'n' },
Kévin Redon3ec265b2018-10-11 17:30:33 +0200784 { "help", 0, 0, 'h' },
Harald Weltecd7fcd72019-12-03 21:29:07 +0100785 { "version", 0, 0, 'v' },
Harald Weltefd5a62a2019-03-30 09:06:39 +0100786 { "gsmtap-ip", 1, 0, 'i' },
Kévin Redon3ec265b2018-10-11 17:30:33 +0200787 { "keep-running", 0, 0, 'k' },
788 { "usb-vendor", 1, 0, 'V' },
789 { "usb-product", 1, 0, 'P' },
790 { "usb-config", 1, 0, 'C' },
791 { "usb-interface", 1, 0, 'I' },
792 { "usb-altsetting", 1, 0, 'S' },
793 { "usb-address", 1, 0, 'A' },
794 { "usb-path", 1, 0, 'H' },
Kévin Redon206c3d72018-11-12 22:51:37 +0100795 { "atr", 1, 0, 'a' },
Kévin Redon3ec265b2018-10-11 17:30:33 +0200796 { NULL, 0, 0, 0 }
797};
798
Kévin Redone0b837c2018-10-10 00:39:25 +0200799int main(int argc, char **argv)
800{
Harald Welte3e9860b2019-12-02 23:04:54 +0100801 struct rspro_server_conn *srvc, *bankdc;
Kévin Redone0b837c2018-10-10 00:39:25 +0200802 struct st_transport *transp = ci->slot->transp;
803 char *gsmtap_host = "127.0.0.1";
804 int rc;
805 int c, ret = 1;
806 int keep_running = 0;
Harald Welte7817b202019-03-30 08:34:14 +0100807 int server_port = 9998;
Kévin Redone0b837c2018-10-10 00:39:25 +0200808 int if_num = 0, vendor_id = -1, product_id = -1;
809 int config_id = -1, altsetting = 0, addr = -1;
Harald Welte72cde102019-03-30 10:43:06 +0100810 int client_id = -1, client_slot = -1;
Harald Weltee56f2b92019-03-02 17:02:13 +0100811 char *server_host = "127.0.0.1";
Kévin Redone0b837c2018-10-10 00:39:25 +0200812 char *path = NULL;
Kévin Redon206c3d72018-11-12 22:51:37 +0100813 uint8_t atr_data[33] = { 0x3B, 0x00 }; // the shortest simplest ATR possible
814 uint8_t atr_len = 2;
Kévin Redone0b837c2018-10-10 00:39:25 +0200815
816 print_welcome();
817
818 while (1) {
819 int option_index = 0;
820
Harald Weltecd7fcd72019-12-03 21:29:07 +0100821 c = getopt_long(argc, argv, "s:p:c:n:hvi:kV:P:C:I:S:A:H:a:", opts, &option_index);
Kévin Redone0b837c2018-10-10 00:39:25 +0200822 if (c == -1)
823 break;
824 switch (c) {
Harald Weltee56f2b92019-03-02 17:02:13 +0100825 case 's':
826 server_host = optarg;
Kévin Redone0b837c2018-10-10 00:39:25 +0200827 break;
828 case 'p':
Harald Weltee56f2b92019-03-02 17:02:13 +0100829 server_port = atoi(optarg);
Kévin Redonfbca97a2018-10-11 19:13:24 +0200830 break;
Harald Welte72cde102019-03-30 10:43:06 +0100831 case 'c':
832 client_id = atoi(optarg);
833 break;
834 case 'n':
835 client_slot = atoi(optarg);
836 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200837 case 'h':
838 print_help();
839 exit(0);
840 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +0100841 case 'v':
842 printf("osmo-remsim-client version %s\n", VERSION);
843 exit(0);
844 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200845 case 'i':
846 gsmtap_host = optarg;
847 break;
848 case 'k':
849 keep_running = 1;
850 break;
851 case 'V':
852 vendor_id = strtol(optarg, NULL, 16);
853 break;
854 case 'P':
855 product_id = strtol(optarg, NULL, 16);
856 break;
857 case 'C':
858 config_id = atoi(optarg);
859 break;
860 case 'I':
861 if_num = atoi(optarg);
862 break;
863 case 'S':
864 altsetting = atoi(optarg);
865 break;
866 case 'A':
867 addr = atoi(optarg);
868 break;
869 case 'H':
870 path = optarg;
871 break;
Kévin Redon206c3d72018-11-12 22:51:37 +0100872 case 'a':
873 rc = osmo_hexparse(optarg, atr_data, ARRAY_SIZE(atr_data));
874 if (rc < 2 || rc > ARRAY_SIZE(atr_data)) {
875 fprintf(stderr, "ATR matlformed\n");
876 goto do_exit;
877 }
878 atr_len = rc;
879 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200880 }
881 }
882
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200883 if (vendor_id < 0 || product_id < 0) {
Kévin Redone0b837c2018-10-10 00:39:25 +0200884 fprintf(stderr, "You have to specify the vendor and product ID\n");
885 goto do_exit;
886 }
887
Harald Weltece638d82019-03-17 09:36:04 +0100888 signal(SIGUSR1, handle_sig_usr1);
889
Harald Welte972a1e82019-03-30 08:34:30 +0100890 g_tall_ctx = talloc_named_const(NULL, 0, "global");
Harald Welteb54a51e2019-03-31 15:57:59 +0200891 talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
Harald Weltef7442b52019-07-18 18:54:05 +0200892 msgb_talloc_ctx_init(g_tall_ctx, 0);
Harald Welte972a1e82019-03-30 08:34:30 +0100893 osmo_init_logging2(g_tall_ctx, &log_info);
894
Harald Welte32e2e002019-12-15 23:01:54 +0100895 rc = osmo_libusb_init(NULL);
Kévin Redon193a8c12018-10-11 17:24:39 +0200896 if (rc < 0) {
897 fprintf(stderr, "libusb initialization failed\n");
898 goto do_exit;
Kévin Redone0b837c2018-10-10 00:39:25 +0200899 }
900
901 g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0);
902 if (!g_gti) {
903 perror("unable to open GSMTAP");
904 goto close_exit;
905 }
906 gsmtap_source_add_sink(g_gti);
907
908 signal(SIGINT, &signal_handler);
909
910 // initialize remote SIM client
Kévin Redone0b837c2018-10-10 00:39:25 +0200911
Kévin Redone0b837c2018-10-10 00:39:25 +0200912 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Harald Weltee56f2b92019-03-02 17:02:13 +0100913
Harald Welte72cde102019-03-30 10:43:06 +0100914 if (client_id != -1) {
915 /* default to client slot 0 */
916 if (client_slot == -1)
917 client_slot = 0;
918 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
919 g_client->srv_conn.clslot->clientId = client_id;
920 g_client->srv_conn.clslot->slotNr = client_slot;
Harald Welte3e9860b2019-12-02 23:04:54 +0100921 g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t);
922 *g_client->bankd_conn.clslot = *g_client->srv_conn.clslot;
Harald Welte72cde102019-03-30 10:43:06 +0100923 }
924
Harald Weltee56f2b92019-03-02 17:02:13 +0100925 srvc = &g_client->srv_conn;
926 srvc->server_host = server_host;
927 srvc->server_port = server_port;
928 srvc->handle_rx = srvc_handle_rx;
929 srvc->own_comp_id.type = ComponentType_remsimClient;
930 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "simtrace2-remsim-client");
931 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client");
932 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
933 rc = server_conn_fsm_alloc(g_client, srvc);
934 if (rc < 0) {
935 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
936 exit(1);
937 }
Harald Welted2192e22019-11-07 18:10:57 +0100938 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
Kévin Redone0b837c2018-10-10 00:39:25 +0200939
Harald Welte3e9860b2019-12-02 23:04:54 +0100940 bankdc = &g_client->bankd_conn;
941 /* server_host / server_port are configured from remsim-server */
942 bankdc->handle_rx = bankd_handle_rx;
943 memcpy(&bankdc->own_comp_id, &srvc->own_comp_id, sizeof(bankdc->own_comp_id));
944 rc = server_conn_fsm_alloc(g_client, bankdc);
945 if (rc < 0) {
946 fprintf(stderr, "Unable to create bankd conn FSM: %s\n", strerror(errno));
Kévin Redone0b837c2018-10-10 00:39:25 +0200947 exit(1);
948 }
Harald Welte82194452019-12-14 17:18:34 +0100949 osmo_fsm_inst_update_id(bankdc->fi, "bankd");
Kévin Redone0b837c2018-10-10 00:39:25 +0200950
Harald Welte3e9860b2019-12-02 23:04:54 +0100951 asn_debug = 0;
952
Kévin Redone0b837c2018-10-10 00:39:25 +0200953 // connect to SIMtrace2 cardem
954 do {
955 struct usb_interface_match _ifm, *ifm = &_ifm;
956 ifm->vendor = vendor_id;
957 ifm->product = product_id;
958 ifm->configuration = config_id;
959 ifm->interface = if_num;
960 ifm->altsetting = altsetting;
961 ifm->addr = addr;
962 if (path)
963 osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
Harald Weltefd5dafc2019-12-15 21:14:14 +0100964 transp->usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, ifm);
Kévin Redone0b837c2018-10-10 00:39:25 +0200965 if (!transp->usb_devh) {
966 fprintf(stderr, "can't open USB device\n");
967 goto close_exit;
968 }
969
970 rc = libusb_claim_interface(transp->usb_devh, if_num);
971 if (rc < 0) {
972 fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc);
973 goto close_exit;
974 }
975
Harald Weltefd5dafc2019-12-15 21:14:14 +0100976 rc = osmo_libusb_get_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
977 &transp->usb_ep.in, &transp->usb_ep.irq_in);
Kévin Redone0b837c2018-10-10 00:39:25 +0200978 if (rc < 0) {
979 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
980 goto close_exit;
981 }
982
Kévin Redon3428e412018-10-11 19:14:00 +0200983 // switch modem SIM port to emulated SIM on OWHW
984 if (USB_VENDOR_OPENMOKO == ifm->vendor && USB_PRODUCT_OWHW_SAM3 == ifm->product) { // we are on the OWHW
985 int modem = -1;
986 switch (ifm->interface) { // the USB interface indicates for which modem we want to emulate the SIM
987 case 0:
988 modem = 1;
989 break;
990 case 1:
991 modem = 2;
992 break;
993 default:
994 fprintf(stderr, "unknown GPIO for SIMtrace interface %d\n", ifm->interface);
995 goto close_exit;
996 }
997 //
998 char gpio_path[PATH_MAX];
999 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/connect_st_usim%d/value", modem);
1000 int connec_st_usim = open(gpio_path, O_WRONLY);
1001 if (-1 == connec_st_usim) {
1002 fprintf(stderr, "can't open GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
1003 goto close_exit;
1004 }
1005 if (1 != write(connec_st_usim, "1", 1)) {
1006 fprintf(stderr, "can't write GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
1007 goto close_exit;
1008 }
1009 printf("switched modem %d to emulated USIM\n", modem);
1010
1011 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/mdm%d_rst/value", modem);
1012 int mdm_rst = open(gpio_path, O_WRONLY);
1013 if (-1 == mdm_rst) {
1014 fprintf(stderr, "can't open GPIO %s to reset modem %d\n", gpio_path, modem);
1015 goto close_exit;
1016 }
1017 if (1 != write(mdm_rst, "1", 1)) {
1018 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
1019 goto close_exit;
1020 }
1021 sleep(1); // wait a bit to ensure reset is effective
1022 if (1 != write(mdm_rst, "0", 1)) {
1023 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
1024 goto close_exit;
1025 }
1026 printf("modem %d reset\n", modem);
1027 }
1028
Kévin Redone0b837c2018-10-10 00:39:25 +02001029 /* simulate card-insert to modem (owhw, not qmod) */
1030 cardem_request_card_insert(ci, true);
1031
1032 /* select remote (forwarded) SIM */
1033 st_modem_sim_select_remote(ci->slot);
1034
1035 /* set the ATR */
Kévin Redon206c3d72018-11-12 22:51:37 +01001036 //atr_update_csum(real_atr, sizeof(real_atr));
1037 cardem_request_set_atr(ci, atr_data, atr_len);
Kévin Redone0b837c2018-10-10 00:39:25 +02001038
1039 /* select remote (forwarded) SIM */
1040 st_modem_reset_pulse(ci->slot, 300);
1041
Harald Welte32e2e002019-12-15 23:01:54 +01001042 printf("Entering main loop\n");
1043
1044 allocate_and_submit_irq(ci);
1045 allocate_and_submit_in(ci);
1046
1047 while (1) {
1048 osmo_select_main(false);
1049 }
1050
Kévin Redone0b837c2018-10-10 00:39:25 +02001051 ret = 0;
1052
Kévin Redon193a8c12018-10-11 17:24:39 +02001053 libusb_release_interface(transp->usb_devh, 0);
Kévin Redone0b837c2018-10-10 00:39:25 +02001054close_exit:
1055 if (transp->usb_devh)
1056 libusb_close(transp->usb_devh);
1057 if (keep_running)
1058 sleep(1);
1059 } while (keep_running);
1060
Kévin Redon193a8c12018-10-11 17:24:39 +02001061 libusb_exit(NULL);
Kévin Redone0b837c2018-10-10 00:39:25 +02001062do_exit:
1063 return ret;
1064}