Harald Welte | 3dcdd20 | 2019-03-09 13:06:46 +0100 | [diff] [blame] | 1 | /* (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 Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 24 | |
| 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 Welte | 61d98e9 | 2019-03-03 15:43:07 +0100 | [diff] [blame] | 41 | #include "debug.h" |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 42 | |
| 43 | #include <unistd.h> |
| 44 | #include <stdio.h> |
Kévin Redon | 3428e41 | 2018-10-11 19:14:00 +0200 | [diff] [blame] | 45 | #include <linux/limits.h> |
| 46 | #include <sys/stat.h> |
| 47 | #include <fcntl.h> |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 48 | #include <signal.h> |
| 49 | #include <getopt.h> |
| 50 | |
| 51 | #include <libusb.h> |
| 52 | |
Harald Welte | fd5dafc | 2019-12-15 21:14:14 +0100 | [diff] [blame] | 53 | #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 Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 57 | |
| 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 */ |
| 67 | struct 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 Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | /* a SIMtrace slot; communicates over a transport */ |
| 78 | struct 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 */ |
| 86 | struct cardem_inst { |
| 87 | /* slot on which this card emulation instance runs */ |
| 88 | struct st_slot *slot; |
| 89 | }; |
| 90 | |
| 91 | /* global GSMTAP instance */ |
| 92 | static struct gsmtap_inst *g_gti; |
| 93 | |
| 94 | static struct bankd_client *g_client; |
| 95 | static void *g_tall_ctx; |
| 96 | void __thread *talloc_asn1_ctx; |
| 97 | int asn_debug; |
| 98 | |
Harald Welte | 3f09f63 | 2019-03-31 15:51:13 +0200 | [diff] [blame] | 99 | __attribute__((unused)) static int gsmtap_send_sim(const uint8_t *apdu, unsigned int len) |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 100 | { |
| 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 Welte | f14dc04 | 2019-03-28 20:29:36 +0100 | [diff] [blame] | 129 | * SIMTRACE core protocol |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 130 | ***********************************************************************/ |
| 131 | |
| 132 | /*! \brief allocate a message buffer for simtrace use */ |
| 133 | static struct msgb *st_msgb_alloc(void) |
| 134 | { |
| 135 | return msgb_alloc_headroom(1024+32, 32, "SIMtrace"); |
| 136 | } |
| 137 | |
| 138 | #if 0 |
| 139 | static 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 Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 146 | static 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 Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 166 | /*! \brief Transmit a given command to the SIMtrace2 device */ |
| 167 | int st_transp_tx_msg(struct st_transport *transp, struct msgb *msg) |
| 168 | { |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 169 | struct libusb_transfer *xfer; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 170 | int rc; |
| 171 | |
Kévin Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 172 | printf("SIMtrace <- %s\n", msgb_hexdump(msg)); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 173 | |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 174 | 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 Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 185 | |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 186 | /* submit the OUT transfer */ |
| 187 | rc = libusb_submit_transfer(xfer); |
| 188 | OSMO_ASSERT(rc == 0); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 189 | |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 190 | return rc; |
| 191 | } |
| 192 | |
| 193 | static 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 */ |
| 210 | int 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 */ |
| 224 | static 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 */ |
| 238 | static 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 Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 244 | printf("SIMtrace <= %s(%02x, %d)\n", __func__, pb, le); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 245 | |
| 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 */ |
| 256 | static int cardem_request_pb_and_tx(struct cardem_inst *ci, uint8_t pb, |
Kévin Redon | f120b64 | 2018-10-15 19:53:02 +0200 | [diff] [blame] | 257 | const uint8_t *data, uint16_t data_len_in) |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 258 | { |
| 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 Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 265 | printf("SIMtrace <= %s(%02x, %s, %d)\n", __func__, pb, |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 266 | 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 */ |
| 281 | static 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 Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 289 | printf("SIMtrace <= %s(%02x %02x)\n", __func__, sw[0], sw[1]); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 290 | |
| 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 | |
Harald Welte | 0e0e932 | 2019-12-16 12:47:18 +0100 | [diff] [blame] | 301 | /*! \brief Request the SIMtrace2 to send a Status Word */ |
| 302 | static int cardem_request_config(struct cardem_inst *ci, uint32_t features) |
| 303 | { |
| 304 | struct msgb *msg = st_msgb_alloc(); |
| 305 | struct cardemu_usb_msg_config *cfg; |
| 306 | |
| 307 | cfg = (struct cardemu_usb_msg_config *) msgb_put(msg, sizeof(*cfg)); |
| 308 | |
| 309 | printf("SIMtrace <= %s(%08x)\n", __func__, features); |
| 310 | |
| 311 | memset(cfg, 0, sizeof(*cfg)); |
| 312 | cfg->features = features; |
| 313 | |
| 314 | return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_BD_CEMU_CONFIG); |
| 315 | } |
| 316 | |
Kévin Redon | 206c3d7 | 2018-11-12 22:51:37 +0100 | [diff] [blame] | 317 | // FIXME check if the ATR actually includes a checksum |
Harald Welte | 3f09f63 | 2019-03-31 15:51:13 +0200 | [diff] [blame] | 318 | __attribute__((unused)) static void atr_update_csum(uint8_t *atr, unsigned int atr_len) |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 319 | { |
| 320 | uint8_t csum = 0; |
| 321 | int i; |
| 322 | |
| 323 | for (i = 1; i < atr_len - 1; i++) |
| 324 | csum = csum ^ atr[i]; |
| 325 | |
| 326 | atr[atr_len-1] = csum; |
| 327 | } |
| 328 | |
| 329 | static int cardem_request_set_atr(struct cardem_inst *ci, const uint8_t *atr, unsigned int atr_len) |
| 330 | { |
| 331 | struct msgb *msg = st_msgb_alloc(); |
| 332 | struct cardemu_usb_msg_set_atr *satr; |
| 333 | uint8_t *cur; |
| 334 | |
| 335 | satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr)); |
| 336 | |
Kévin Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 337 | printf("SIMtrace <= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len)); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 338 | |
| 339 | memset(satr, 0, sizeof(*satr)); |
| 340 | satr->atr_len = atr_len; |
| 341 | cur = msgb_put(msg, atr_len); |
| 342 | memcpy(cur, atr, atr_len); |
| 343 | |
| 344 | return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR); |
| 345 | } |
| 346 | |
| 347 | /*********************************************************************** |
| 348 | * Modem Control protocol |
| 349 | ***********************************************************************/ |
| 350 | |
| 351 | static int _modem_reset(struct st_slot *slot, uint8_t asserted, uint16_t pulse_ms) |
| 352 | { |
| 353 | struct msgb *msg = st_msgb_alloc(); |
| 354 | struct st_modem_reset *sr ; |
| 355 | |
| 356 | sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr)); |
| 357 | sr->asserted = asserted; |
| 358 | sr->pulse_duration_msec = pulse_ms; |
| 359 | |
| 360 | return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET); |
| 361 | } |
| 362 | |
| 363 | /*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/ |
| 364 | int st_modem_reset_pulse(struct st_slot *slot, uint16_t duration_ms) |
| 365 | { |
| 366 | return _modem_reset(slot, 2, duration_ms); |
| 367 | } |
| 368 | |
| 369 | /*! \brief assert the RESET line of the modem */ |
| 370 | int st_modem_reset_active(struct st_slot *slot) |
| 371 | { |
| 372 | return _modem_reset(slot, 1, 0); |
| 373 | } |
| 374 | |
| 375 | /*! \brief de-assert the RESET line of the modem */ |
| 376 | int st_modem_reset_inactive(struct st_slot *slot) |
| 377 | { |
| 378 | return _modem_reset(slot, 0, 0); |
| 379 | } |
| 380 | |
| 381 | static int _modem_sim_select(struct st_slot *slot, uint8_t remote_sim) |
| 382 | { |
| 383 | struct msgb *msg = st_msgb_alloc(); |
| 384 | struct st_modem_sim_select *ss; |
| 385 | |
| 386 | ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss)); |
| 387 | ss->remote_sim = remote_sim; |
| 388 | |
| 389 | return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT); |
| 390 | } |
| 391 | |
| 392 | /*! \brief select local (physical) SIM for given slot */ |
| 393 | int st_modem_sim_select_local(struct st_slot *slot) |
| 394 | { |
| 395 | return _modem_sim_select(slot, 0); |
| 396 | } |
| 397 | |
| 398 | /*! \brief select remote (emulated/forwarded) SIM for given slot */ |
| 399 | int st_modem_sim_select_remote(struct st_slot *slot) |
| 400 | { |
| 401 | return _modem_sim_select(slot, 1); |
| 402 | } |
| 403 | |
| 404 | /*! \brief Request slot to send us status information about the modem */ |
| 405 | int st_modem_get_status(struct st_slot *slot) |
| 406 | { |
| 407 | struct msgb *msg = st_msgb_alloc(); |
| 408 | |
| 409 | return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS); |
| 410 | } |
| 411 | |
| 412 | |
| 413 | /*********************************************************************** |
| 414 | * Incoming Messages |
| 415 | ***********************************************************************/ |
| 416 | |
| 417 | /*! \brief Process a STATUS message from the SIMtrace2 */ |
| 418 | static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len) |
| 419 | { |
| 420 | struct cardemu_usb_msg_status *status; |
| 421 | status = (struct cardemu_usb_msg_status *) buf; |
| 422 | |
Kévin Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 423 | printf("SIMtrace => STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n", |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 424 | status->flags, status->fi, status->di, status->wi, |
| 425 | status->waiting_time); |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | /*! \brief Process a PTS indication message from the SIMtrace2 */ |
| 431 | static int process_do_pts(struct cardem_inst *ci, uint8_t *buf, int len) |
| 432 | { |
| 433 | struct cardemu_usb_msg_pts_info *pts; |
| 434 | pts = (struct cardemu_usb_msg_pts_info *) buf; |
| 435 | |
Kévin Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 436 | printf("SIMtrace => PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req))); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | /*! \brief Process a ERROR indication message from the SIMtrace2 */ |
Harald Welte | 3f09f63 | 2019-03-31 15:51:13 +0200 | [diff] [blame] | 442 | __attribute__((unused)) static int process_do_error(struct cardem_inst *ci, uint8_t *buf, int len) |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 443 | { |
| 444 | struct cardemu_usb_msg_error *err; |
| 445 | err = (struct cardemu_usb_msg_error *) buf; |
| 446 | |
Kévin Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 447 | printf("SIMtrace => ERROR: %u/%u/%u: %s\n", |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 448 | err->severity, err->subsystem, err->code, |
| 449 | err->msg_len ? (char *)err->msg : ""); |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
Harald Welte | fd5dafc | 2019-12-15 21:14:14 +0100 | [diff] [blame] | 454 | static struct osmo_apdu_context ac; // this will hold the complete APDU (across calls) |
Kévin Redon | bc08db5 | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 455 | |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 456 | /*! \brief Process a RX-DATA indication message from the SIMtrace2 */ |
| 457 | static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len) |
| 458 | { |
Kévin Redon | bc08db5 | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 459 | struct cardemu_usb_msg_rx_data *data = (struct cardemu_usb_msg_rx_data *) buf; // cast the data from the USB message |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 460 | int rc; |
| 461 | |
Kévin Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 462 | printf("SIMtrace => DATA: flags=%x, %s: ", data->flags, |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 463 | osmo_hexdump(data->data, data->data_len)); |
| 464 | |
Harald Welte | fd5dafc | 2019-12-15 21:14:14 +0100 | [diff] [blame] | 465 | rc = osmo_apdu_segment_in(&ac, data->data, data->data_len, |
| 466 | data->flags & CEMU_DATA_F_TPDU_HDR); // parse the APDU data in the USB message |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 467 | |
Kévin Redon | bc08db5 | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 468 | if (rc & APDU_ACT_TX_CAPDU_TO_CARD) { // there is no pending data coming from the modem |
Harald Welte | 2ea20b9 | 2019-03-30 08:33:49 +0100 | [diff] [blame] | 469 | uint8_t apdu_command[sizeof(ac.hdr) + ac.lc.tot]; // to store the APDU command to send |
Kévin Redon | bc08db5 | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 470 | memcpy(apdu_command, &ac.hdr, sizeof(ac.hdr)); // copy APDU command header |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 471 | if (ac.lc.tot) { |
Kévin Redon | bc08db5 | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 472 | memcpy(apdu_command + sizeof(ac.hdr), ac.dc, ac.lc.tot); // copy APDU command data |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 473 | } |
Kévin Redon | bc08db5 | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 474 | // send APDU to card |
Harald Welte | 9cf013a | 2019-03-11 22:19:19 +0100 | [diff] [blame] | 475 | BankSlot_t bslot; |
| 476 | bank_slot2rspro(&bslot, &g_client->bankd_slot); |
| 477 | RsproPDU_t *pdu = rspro_gen_TpduModem2Card(g_client->srv_conn.clslot, &bslot, apdu_command, sizeof(ac.hdr) + ac.lc.tot); // create RSPRO packet |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 478 | server_conn_send_rspro(&g_client->bankd_conn, pdu); |
Kévin Redon | bc08db5 | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 479 | // the response will come separately |
Kévin Redon | bc08db5 | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 480 | } else if (ac.lc.tot > ac.lc.cur) { // there is pending data from the modem |
| 481 | cardem_request_pb_and_rx(ci, ac.hdr.ins, ac.lc.tot - ac.lc.cur); // send procedure byte to get remaining data |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 482 | } |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | #if 0 |
| 487 | case SIMTRACE_CMD_DO_ERROR |
| 488 | rc = process_do_error(ci, buf, len); |
| 489 | break; |
| 490 | #endif |
| 491 | |
| 492 | /*! \brief Process an incoming message from the SIMtrace2 */ |
| 493 | static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len) |
| 494 | { |
| 495 | struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf; |
| 496 | int rc; |
| 497 | |
Kévin Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 498 | printf("SIMtrace -> %s\n", osmo_hexdump(buf, len)); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 499 | |
| 500 | buf += sizeof(*sh); |
| 501 | |
| 502 | switch (sh->msg_type) { |
| 503 | case SIMTRACE_MSGT_BD_CEMU_STATUS: |
| 504 | rc = process_do_status(ci, buf, len); |
| 505 | break; |
| 506 | case SIMTRACE_MSGT_DO_CEMU_PTS: |
| 507 | rc = process_do_pts(ci, buf, len); |
| 508 | break; |
| 509 | case SIMTRACE_MSGT_DO_CEMU_RX_DATA: |
| 510 | rc = process_do_rx_da(ci, buf, len); |
| 511 | break; |
Harald Welte | 0e0e932 | 2019-12-16 12:47:18 +0100 | [diff] [blame] | 512 | case SIMTRACE_MSGT_BD_CEMU_CONFIG: |
| 513 | /* firmware confirms configuration change; ignore */ |
| 514 | break; |
| 515 | default: |
| 516 | printf("unknown simtrace msg type 0x%02x\n", sh->msg_type); |
| 517 | rc = -1; |
| 518 | break; |
| 519 | } |
| 520 | |
| 521 | return rc; |
| 522 | } |
| 523 | |
| 524 | |
| 525 | /*! \brief Process a STATUS message on IRQ endpoint from the SIMtrace2 */ |
| 526 | static int process_irq_status(struct cardem_inst *ci, const uint8_t *buf, int len) |
| 527 | { |
| 528 | const struct cardemu_usb_msg_status *status = (struct cardemu_usb_msg_status *) buf; |
| 529 | |
| 530 | printf("SIMtrace IRQ STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n", |
| 531 | status->flags, status->fi, status->di, status->wi, |
| 532 | status->waiting_time); |
| 533 | |
| 534 | BankSlot_t bslot; |
| 535 | bank_slot2rspro(&bslot, &g_client->bankd_slot); |
| 536 | RsproPDU_t *pdu = rspro_gen_ClientSlotStatusInd(g_client->srv_conn.clslot, &bslot, |
| 537 | status->flags & CEMU_STATUS_F_RESET_ACTIVE, |
| 538 | status->flags & CEMU_STATUS_F_VCC_PRESENT, |
| 539 | status->flags & CEMU_STATUS_F_CLK_ACTIVE, |
| 540 | -1 /* FIXME: make this dependent on board */); |
| 541 | server_conn_send_rspro(&g_client->bankd_conn, pdu); |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | static int process_usb_msg_irq(struct cardem_inst *ci, const uint8_t *buf, unsigned int len) |
| 547 | { |
| 548 | struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf; |
| 549 | int rc; |
| 550 | |
| 551 | printf("SIMtrace IRQ %s\n", osmo_hexdump(buf, len)); |
| 552 | |
| 553 | buf += sizeof(*sh); |
| 554 | |
| 555 | switch (sh->msg_type) { |
| 556 | case SIMTRACE_MSGT_BD_CEMU_STATUS: |
| 557 | rc = process_irq_status(ci, buf, len); |
| 558 | break; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 559 | default: |
| 560 | printf("unknown simtrace msg type 0x%02x\n", sh->msg_type); |
| 561 | rc = -1; |
| 562 | break; |
| 563 | } |
| 564 | |
| 565 | return rc; |
| 566 | } |
| 567 | |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 568 | static void usb_in_xfer_cb(struct libusb_transfer *xfer) |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 569 | { |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 570 | struct cardem_inst *ci = xfer->user_data; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 571 | int rc; |
| 572 | |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 573 | switch (xfer->status) { |
| 574 | case LIBUSB_TRANSFER_COMPLETED: |
| 575 | /* hand the message up the stack */ |
| 576 | process_usb_msg(ci, xfer->buffer, xfer->actual_length); |
| 577 | break; |
| 578 | case LIBUSB_TRANSFER_NO_DEVICE: |
| 579 | fprintf(stderr, "USB device disappeared\n"); |
| 580 | exit(23); |
| 581 | break; |
| 582 | default: |
| 583 | osmo_panic("USB IN transfer failed, status=%u\n", xfer->status); |
| 584 | break; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 585 | } |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 586 | |
| 587 | /* re-submit the IN transfer */ |
| 588 | rc = libusb_submit_transfer(xfer); |
| 589 | OSMO_ASSERT(rc == 0); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 590 | } |
| 591 | |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 592 | |
| 593 | static void allocate_and_submit_in(struct cardem_inst *ci) |
| 594 | { |
| 595 | struct st_transport *transp = ci->slot->transp; |
| 596 | struct libusb_transfer *xfer; |
| 597 | int rc; |
| 598 | |
| 599 | xfer = libusb_alloc_transfer(0); |
| 600 | OSMO_ASSERT(xfer); |
| 601 | xfer->dev_handle = transp->usb_devh; |
| 602 | xfer->flags = 0; |
| 603 | xfer->type = LIBUSB_TRANSFER_TYPE_BULK; |
| 604 | xfer->endpoint = transp->usb_ep.in; |
| 605 | xfer->timeout = 0; |
| 606 | xfer->user_data = ci; |
| 607 | xfer->length = 16*256; |
| 608 | |
| 609 | xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length); |
| 610 | OSMO_ASSERT(xfer->buffer); |
| 611 | xfer->callback = usb_in_xfer_cb; |
| 612 | |
| 613 | /* submit the IN transfer */ |
| 614 | rc = libusb_submit_transfer(xfer); |
| 615 | OSMO_ASSERT(rc == 0); |
| 616 | } |
| 617 | |
| 618 | |
| 619 | static void usb_irq_xfer_cb(struct libusb_transfer *xfer) |
| 620 | { |
Harald Welte | 0e0e932 | 2019-12-16 12:47:18 +0100 | [diff] [blame] | 621 | struct cardem_inst *ci = xfer->user_data; |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 622 | int rc; |
| 623 | |
| 624 | switch (xfer->status) { |
| 625 | case LIBUSB_TRANSFER_COMPLETED: |
Harald Welte | 0e0e932 | 2019-12-16 12:47:18 +0100 | [diff] [blame] | 626 | process_usb_msg_irq(ci, xfer->buffer, xfer->actual_length); |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 627 | break; |
| 628 | case LIBUSB_TRANSFER_NO_DEVICE: |
| 629 | fprintf(stderr, "USB device disappeared\n"); |
| 630 | exit(23); |
| 631 | break; |
| 632 | default: |
| 633 | osmo_panic("USB IRQ transfer failed, status=%u\n", xfer->status); |
| 634 | break; |
| 635 | } |
| 636 | |
| 637 | /* re-submit the IN transfer */ |
| 638 | rc = libusb_submit_transfer(xfer); |
| 639 | OSMO_ASSERT(rc == 0); |
| 640 | } |
| 641 | |
| 642 | |
| 643 | static void allocate_and_submit_irq(struct cardem_inst *ci) |
| 644 | { |
| 645 | struct st_transport *transp = ci->slot->transp; |
| 646 | struct libusb_transfer *xfer; |
| 647 | int rc; |
| 648 | |
| 649 | xfer = libusb_alloc_transfer(0); |
| 650 | OSMO_ASSERT(xfer); |
| 651 | xfer->dev_handle = transp->usb_devh; |
| 652 | xfer->flags = 0; |
| 653 | xfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT; |
| 654 | xfer->endpoint = transp->usb_ep.irq_in; |
| 655 | xfer->timeout = 0; |
| 656 | xfer->user_data = ci; |
| 657 | xfer->length = 64; |
| 658 | |
| 659 | xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length); |
| 660 | OSMO_ASSERT(xfer->buffer); |
| 661 | xfer->callback = usb_irq_xfer_cb; |
| 662 | |
| 663 | /* submit the IN transfer */ |
| 664 | rc = libusb_submit_transfer(xfer); |
| 665 | OSMO_ASSERT(rc == 0); |
| 666 | } |
| 667 | |
| 668 | |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 669 | static struct st_transport _transp; |
| 670 | |
| 671 | static struct st_slot _slot = { |
| 672 | .transp = &_transp, |
| 673 | .slot_nr = 0, |
| 674 | }; |
| 675 | |
| 676 | struct cardem_inst _ci = { |
| 677 | .slot = &_slot, |
| 678 | }; |
| 679 | |
| 680 | struct cardem_inst *ci = &_ci; |
| 681 | |
| 682 | static void signal_handler(int signal) |
| 683 | { |
| 684 | switch (signal) { |
| 685 | case SIGINT: |
| 686 | cardem_request_card_insert(ci, false); |
| 687 | exit(0); |
| 688 | break; |
| 689 | default: |
| 690 | break; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | /** remsim_client **/ |
| 695 | |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 696 | static int bankd_handle_tpduCardToModem(struct bankd_client *bc, const RsproPDU_t *pdu) |
Kévin Redon | 9b69a3f | 2018-10-11 08:41:57 +0200 | [diff] [blame] | 697 | { |
| 698 | OSMO_ASSERT(pdu); |
| 699 | OSMO_ASSERT(RsproPDUchoice_PR_tpduCardToModem == pdu->msg.present); |
| 700 | |
| 701 | const struct TpduCardToModem *card2modem = &pdu->msg.choice.tpduCardToModem; |
| 702 | if (card2modem->data.size < 2) { // at least the two SW bytes are needed |
| 703 | return -1; |
| 704 | } |
| 705 | |
| 706 | // save SW to our current APDU context |
| 707 | ac.sw[0] = card2modem->data.buf[card2modem->data.size - 2]; |
| 708 | ac.sw[1] = card2modem->data.buf[card2modem->data.size - 1]; |
Kévin Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 709 | printf("SIMtrace <= SW=0x%02x%02x, len_rx=%d\n", ac.sw[0], ac.sw[1], card2modem->data.size - 2); |
Kévin Redon | 9b69a3f | 2018-10-11 08:41:57 +0200 | [diff] [blame] | 710 | if (card2modem->data.size > 2) { // send PB and data to modem |
| 711 | cardem_request_pb_and_tx(ci, ac.hdr.ins, card2modem->data.buf, card2modem->data.size - 2); |
| 712 | } |
| 713 | cardem_request_sw_tx(ci, ac.sw); // send SW to modem |
| 714 | |
| 715 | return 0; |
| 716 | } |
| 717 | |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 718 | static int bankd_handle_setAtrReq(struct bankd_client *bc, const RsproPDU_t *pdu) |
Harald Welte | fa36559 | 2019-03-28 20:28:57 +0100 | [diff] [blame] | 719 | { |
| 720 | RsproPDU_t *resp; |
| 721 | int rc; |
| 722 | |
| 723 | OSMO_ASSERT(pdu); |
| 724 | OSMO_ASSERT(RsproPDUchoice_PR_setAtrReq == pdu->msg.present); |
| 725 | |
| 726 | /* FIXME: is this permitted at any time by the SIMtrace2 cardemfirmware? */ |
| 727 | rc = cardem_request_set_atr(ci, pdu->msg.choice.setAtrReq.atr.buf, |
| 728 | pdu->msg.choice.setAtrReq.atr.size); |
| 729 | if (rc == 0) |
| 730 | resp = rspro_gen_SetAtrRes(ResultCode_ok); |
| 731 | else |
| 732 | resp = rspro_gen_SetAtrRes(ResultCode_cardTransmissionError); |
| 733 | if (!resp) |
| 734 | return -ENOMEM; |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 735 | server_conn_send_rspro(&g_client->bankd_conn, resp); |
Harald Welte | fa36559 | 2019-03-28 20:28:57 +0100 | [diff] [blame] | 736 | |
| 737 | return 0; |
| 738 | } |
| 739 | |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 740 | /* handle incoming message from bankd */ |
| 741 | static int bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu) |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 742 | { |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 743 | switch (pdu->msg.present) { |
| 744 | case RsproPDUchoice_PR_connectClientRes: |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 745 | /* Store 'identity' of bankd to in peer_comp_id */ |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 746 | rspro_comp_id_retrieve(&bankdc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity); |
| 747 | osmo_fsm_inst_dispatch(bankdc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 748 | break; |
Kévin Redon | 9b69a3f | 2018-10-11 08:41:57 +0200 | [diff] [blame] | 749 | case RsproPDUchoice_PR_tpduCardToModem: // APDU response from card received |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 750 | bankd_handle_tpduCardToModem(g_client, pdu); |
Kévin Redon | 9b69a3f | 2018-10-11 08:41:57 +0200 | [diff] [blame] | 751 | break; |
Harald Welte | fa36559 | 2019-03-28 20:28:57 +0100 | [diff] [blame] | 752 | case RsproPDUchoice_PR_setAtrReq: |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 753 | bankd_handle_setAtrReq(g_client, pdu); |
Harald Welte | fa36559 | 2019-03-28 20:28:57 +0100 | [diff] [blame] | 754 | break; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 755 | default: |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 756 | LOGPFSML(bankdc->fi, LOGL_ERROR, "Unknown/Unsuppoerted RSPRO PDU %s\n", |
| 757 | rspro_msgt_name(pdu)); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 758 | return -1; |
| 759 | } |
| 760 | |
| 761 | return 0; |
| 762 | } |
| 763 | |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 764 | /* handle incoming messages from server */ |
| 765 | static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu) |
| 766 | { |
| 767 | RsproPDU_t *resp; |
| 768 | |
| 769 | switch (pdu->msg.present) { |
| 770 | case RsproPDUchoice_PR_connectClientRes: |
| 771 | /* Store 'identity' of server in srvc->peer_comp_id */ |
| 772 | rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity); |
| 773 | osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu); |
| 774 | break; |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 775 | case RsproPDUchoice_PR_configClientIdReq: |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 776 | /* store/set the clientID as instructed by the server */ |
Harald Welte | ec628e9 | 2019-03-08 22:18:31 +0100 | [diff] [blame] | 777 | if (!g_client->srv_conn.clslot) |
| 778 | g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t); |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 779 | *g_client->srv_conn.clslot = pdu->msg.choice.configClientIdReq.clientSlot; |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 780 | if (!g_client->bankd_conn.clslot) |
| 781 | g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t); |
| 782 | *g_client->bankd_conn.clslot = *g_client->srv_conn.clslot; |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 783 | /* send response to server */ |
| 784 | resp = rspro_gen_ConfigClientIdRes(ResultCode_ok); |
| 785 | server_conn_send_rspro(srvc, resp); |
| 786 | break; |
| 787 | case RsproPDUchoice_PR_configClientBankReq: |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 788 | /* store/set the bankd ip/port as instructed by the server */ |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 789 | osmo_talloc_replace_string(g_client, &g_client->bankd_conn.server_host, |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 790 | rspro_IpAddr2str(&pdu->msg.choice.configClientBankReq.bankd.ip)); |
Harald Welte | 9cf013a | 2019-03-11 22:19:19 +0100 | [diff] [blame] | 791 | rspro2bank_slot(&g_client->bankd_slot, &pdu->msg.choice.configClientBankReq.bankSlot); |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 792 | g_client->bankd_conn.server_port = pdu->msg.choice.configClientBankReq.bankd.port; |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 793 | /* instruct bankd FSM to connect */ |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 794 | osmo_fsm_inst_dispatch(g_client->bankd_conn.fi, SRVC_E_ESTABLISH, NULL); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 795 | /* send response to server */ |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 796 | resp = rspro_gen_ConfigClientBankRes(ResultCode_ok); |
Harald Welte | a844bb0 | 2019-03-09 13:38:50 +0100 | [diff] [blame] | 797 | server_conn_send_rspro(srvc, resp); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 798 | break; |
| 799 | default: |
Harald Welte | 8d8d4f1 | 2019-03-27 22:50:39 +0100 | [diff] [blame] | 800 | LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %s\n", |
| 801 | rspro_msgt_name(pdu)); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 802 | return -1; |
| 803 | } |
| 804 | |
| 805 | return 0; |
| 806 | } |
| 807 | |
Harald Welte | ce638d8 | 2019-03-17 09:36:04 +0100 | [diff] [blame] | 808 | static void handle_sig_usr1(int signal) |
| 809 | { |
| 810 | OSMO_ASSERT(signal == SIGUSR1); |
Harald Welte | b54a51e | 2019-03-31 15:57:59 +0200 | [diff] [blame] | 811 | talloc_report_full(g_tall_ctx, stderr); |
Harald Welte | f918748 | 2019-12-14 17:21:08 +0100 | [diff] [blame^] | 812 | printf("===== NULL\n"); |
| 813 | talloc_report_full(NULL, stderr); |
Harald Welte | ce638d8 | 2019-03-17 09:36:04 +0100 | [diff] [blame] | 814 | } |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 815 | |
Kévin Redon | 3ec265b | 2018-10-11 17:30:33 +0200 | [diff] [blame] | 816 | static void print_welcome(void) |
| 817 | { |
| 818 | printf("simtrace2-remsim-client - Remote SIM card client for SIMtrace\n" |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 819 | "(C) 2010-2019, Harald Welte <laforge@gnumonks.org>\n" |
Kévin Redon | 3ec265b | 2018-10-11 17:30:33 +0200 | [diff] [blame] | 820 | "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n"); |
| 821 | } |
| 822 | |
| 823 | static void print_help(void) |
| 824 | { |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 825 | printf( "\t-s\t--server-host HOST\n" |
Kévin Redon | da1854c | 2019-09-17 14:16:54 +0200 | [diff] [blame] | 826 | "\t-p\t--server-port PORT\n" |
Harald Welte | 72cde10 | 2019-03-30 10:43:06 +0100 | [diff] [blame] | 827 | "\t-c\t--client-id <0-65535>\n" |
Kévin Redon | da1854c | 2019-09-17 14:16:54 +0200 | [diff] [blame] | 828 | "\t-n\t--client-slot <0-65535>\n" |
Kévin Redon | 3ec265b | 2018-10-11 17:30:33 +0200 | [diff] [blame] | 829 | "\t-h\t--help\n" |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 830 | "\t-v\t--version\n" |
Kévin Redon | 3ec265b | 2018-10-11 17:30:33 +0200 | [diff] [blame] | 831 | "\t-i\t--gsmtap-ip\tA.B.C.D\n" |
| 832 | "\t-k\t--keep-running\n" |
| 833 | "\t-V\t--usb-vendor\tVENDOR_ID\n" |
| 834 | "\t-P\t--usb-product\tPRODUCT_ID\n" |
| 835 | "\t-C\t--usb-config\tCONFIG_ID\n" |
| 836 | "\t-I\t--usb-interface\tINTERFACE_ID\n" |
| 837 | "\t-S\t--usb-altsetting ALTSETTING_ID\n" |
| 838 | "\t-A\t--usb-address\tADDRESS\n" |
| 839 | "\t-H\t--usb-path\tPATH\n" |
Kévin Redon | 206c3d7 | 2018-11-12 22:51:37 +0100 | [diff] [blame] | 840 | "\t-a\t--atr\tATR\n" |
Kévin Redon | 3ec265b | 2018-10-11 17:30:33 +0200 | [diff] [blame] | 841 | "\n" |
| 842 | ); |
| 843 | } |
| 844 | |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 845 | #define ATR_SIZE_MAX 55 |
| 846 | struct client_config { |
| 847 | char *server_host; |
| 848 | int server_port; |
| 849 | |
| 850 | int client_id; |
| 851 | int client_slot; |
| 852 | |
| 853 | char *gsmtap_host; |
| 854 | bool keep_running; |
| 855 | |
| 856 | struct { |
| 857 | uint8_t data[ATR_SIZE_MAX]; |
| 858 | uint8_t len; |
| 859 | } atr; |
| 860 | |
| 861 | struct { |
| 862 | int vendor_id; |
| 863 | int product_id; |
| 864 | int config_id; |
| 865 | int if_num; |
| 866 | int altsetting; |
| 867 | int addr; |
| 868 | char *path; |
| 869 | } usb; |
Kévin Redon | 3ec265b | 2018-10-11 17:30:33 +0200 | [diff] [blame] | 870 | }; |
| 871 | |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 872 | static struct client_config *client_config_init(void *ctx) |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 873 | { |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 874 | struct client_config *cfg = talloc_zero(ctx, struct client_config); |
| 875 | if (!cfg) |
| 876 | return NULL; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 877 | |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 878 | cfg->server_host = talloc_strdup(cfg, "127.0.0.1"); |
| 879 | cfg->server_port = 9998; |
| 880 | cfg->client_id = -1; |
| 881 | cfg->client_slot = -1; |
| 882 | cfg->gsmtap_host = talloc_strdup(cfg, "127.0.0.1"); |
| 883 | cfg->keep_running = false; |
| 884 | |
| 885 | cfg->usb.vendor_id = -1; |
| 886 | cfg->usb.product_id = -1; |
| 887 | cfg->usb.config_id = -1; |
| 888 | cfg->usb.if_num = -1; |
| 889 | cfg->usb.altsetting = 0; |
| 890 | cfg->usb.addr = -1; |
| 891 | cfg->usb.path = NULL; |
| 892 | |
| 893 | cfg->atr.data[0] = 0x3B; |
| 894 | cfg->atr.data[1] = 0x00; // the shortest simplest ATR possible |
| 895 | cfg->atr.len = 2; |
| 896 | |
| 897 | return cfg; |
| 898 | }; |
| 899 | |
| 900 | static void handle_options(struct client_config *cfg, int argc, char **argv) |
| 901 | { |
| 902 | const struct option opts[] = { |
| 903 | { "server-host", 1, 0, 's' }, |
| 904 | { "server-port", 1, 0, 'p' }, |
| 905 | { "client-id", 1, 0, 'c' }, |
| 906 | { "client-slot", 1, 0, 'n' }, |
| 907 | { "help", 0, 0, 'h' }, |
| 908 | { "version", 0, 0, 'v' }, |
| 909 | { "gsmtap-ip", 1, 0, 'i' }, |
| 910 | { "keep-running", 0, 0, 'k' }, |
| 911 | { "usb-vendor", 1, 0, 'V' }, |
| 912 | { "usb-product", 1, 0, 'P' }, |
| 913 | { "usb-config", 1, 0, 'C' }, |
| 914 | { "usb-interface", 1, 0, 'I' }, |
| 915 | { "usb-altsetting", 1, 0, 'S' }, |
| 916 | { "usb-address", 1, 0, 'A' }, |
| 917 | { "usb-path", 1, 0, 'H' }, |
| 918 | { "atr", 1, 0, 'a' }, |
| 919 | { NULL, 0, 0, 0 } |
| 920 | }; |
| 921 | int c, rc; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 922 | |
| 923 | while (1) { |
| 924 | int option_index = 0; |
| 925 | |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 926 | c = getopt_long(argc, argv, "s:p:c:n:hvi:kV:P:C:I:S:A:H:a:", opts, &option_index); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 927 | if (c == -1) |
| 928 | break; |
| 929 | switch (c) { |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 930 | case 's': |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 931 | osmo_talloc_replace_string(cfg, &cfg->server_host, optarg); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 932 | break; |
| 933 | case 'p': |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 934 | cfg->server_port = atoi(optarg); |
Kévin Redon | fbca97a | 2018-10-11 19:13:24 +0200 | [diff] [blame] | 935 | break; |
Harald Welte | 72cde10 | 2019-03-30 10:43:06 +0100 | [diff] [blame] | 936 | case 'c': |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 937 | cfg->client_id = atoi(optarg); |
Harald Welte | 72cde10 | 2019-03-30 10:43:06 +0100 | [diff] [blame] | 938 | break; |
| 939 | case 'n': |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 940 | cfg->client_slot = atoi(optarg); |
Harald Welte | 72cde10 | 2019-03-30 10:43:06 +0100 | [diff] [blame] | 941 | break; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 942 | case 'h': |
| 943 | print_help(); |
| 944 | exit(0); |
| 945 | break; |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 946 | case 'v': |
| 947 | printf("osmo-remsim-client version %s\n", VERSION); |
| 948 | exit(0); |
| 949 | break; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 950 | case 'i': |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 951 | osmo_talloc_replace_string(cfg, &cfg->gsmtap_host, optarg); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 952 | break; |
| 953 | case 'k': |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 954 | cfg->keep_running = 1; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 955 | break; |
| 956 | case 'V': |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 957 | cfg->usb.vendor_id = strtol(optarg, NULL, 16); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 958 | break; |
| 959 | case 'P': |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 960 | cfg->usb.product_id = strtol(optarg, NULL, 16); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 961 | break; |
| 962 | case 'C': |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 963 | cfg->usb.config_id = atoi(optarg); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 964 | break; |
| 965 | case 'I': |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 966 | cfg->usb.if_num = atoi(optarg); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 967 | break; |
| 968 | case 'S': |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 969 | cfg->usb.altsetting = atoi(optarg); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 970 | break; |
| 971 | case 'A': |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 972 | cfg->usb.addr = atoi(optarg); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 973 | break; |
| 974 | case 'H': |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 975 | cfg->usb.path = optarg; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 976 | break; |
Kévin Redon | 206c3d7 | 2018-11-12 22:51:37 +0100 | [diff] [blame] | 977 | case 'a': |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 978 | rc = osmo_hexparse(optarg, cfg->atr.data, ARRAY_SIZE(cfg->atr.data)); |
| 979 | if (rc < 2 || rc > ARRAY_SIZE(cfg->atr.data)) { |
| 980 | fprintf(stderr, "ATR malformed\n"); |
| 981 | exit(2); |
Kévin Redon | 206c3d7 | 2018-11-12 22:51:37 +0100 | [diff] [blame] | 982 | } |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 983 | cfg->atr.len = rc; |
Kévin Redon | 206c3d7 | 2018-11-12 22:51:37 +0100 | [diff] [blame] | 984 | break; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 988 | if (argc > optind) { |
| 989 | fprintf(stderr, "Unsupported positional arguments on command line\n"); |
| 990 | exit(2); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 991 | } |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 992 | } |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 993 | |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 994 | |
| 995 | int main(int argc, char **argv) |
| 996 | { |
| 997 | struct rspro_server_conn *srvc, *bankdc; |
| 998 | struct st_transport *transp = ci->slot->transp; |
| 999 | struct client_config *cfg; |
| 1000 | int rc; |
| 1001 | int ret = 1; |
| 1002 | |
| 1003 | print_welcome(); |
Harald Welte | ce638d8 | 2019-03-17 09:36:04 +0100 | [diff] [blame] | 1004 | |
Harald Welte | f918748 | 2019-12-14 17:21:08 +0100 | [diff] [blame^] | 1005 | talloc_enable_null_tracking(); |
Harald Welte | 972a1e8 | 2019-03-30 08:34:30 +0100 | [diff] [blame] | 1006 | g_tall_ctx = talloc_named_const(NULL, 0, "global"); |
Harald Welte | b54a51e | 2019-03-31 15:57:59 +0200 | [diff] [blame] | 1007 | talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1"); |
Harald Welte | f7442b5 | 2019-07-18 18:54:05 +0200 | [diff] [blame] | 1008 | msgb_talloc_ctx_init(g_tall_ctx, 0); |
Harald Welte | 972a1e8 | 2019-03-30 08:34:30 +0100 | [diff] [blame] | 1009 | osmo_init_logging2(g_tall_ctx, &log_info); |
| 1010 | |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 1011 | cfg = client_config_init(g_tall_ctx); |
| 1012 | handle_options(cfg, argc, argv); |
| 1013 | |
| 1014 | if (cfg->usb.vendor_id < 0 || cfg->usb.product_id < 0) { |
| 1015 | fprintf(stderr, "You have to specify the vendor and product ID\n"); |
| 1016 | goto do_exit; |
| 1017 | } |
| 1018 | |
| 1019 | signal(SIGUSR1, handle_sig_usr1); |
| 1020 | |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 1021 | rc = osmo_libusb_init(NULL); |
Kévin Redon | 193a8c1 | 2018-10-11 17:24:39 +0200 | [diff] [blame] | 1022 | if (rc < 0) { |
| 1023 | fprintf(stderr, "libusb initialization failed\n"); |
| 1024 | goto do_exit; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1025 | } |
| 1026 | |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 1027 | g_gti = gsmtap_source_init(cfg->gsmtap_host, GSMTAP_UDP_PORT, 0); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1028 | if (!g_gti) { |
| 1029 | perror("unable to open GSMTAP"); |
| 1030 | goto close_exit; |
| 1031 | } |
| 1032 | gsmtap_source_add_sink(g_gti); |
| 1033 | |
| 1034 | signal(SIGINT, &signal_handler); |
| 1035 | |
| 1036 | // initialize remote SIM client |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1037 | |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1038 | g_client = talloc_zero(g_tall_ctx, struct bankd_client); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 1039 | |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 1040 | if (cfg->client_id != -1) { |
Harald Welte | 72cde10 | 2019-03-30 10:43:06 +0100 | [diff] [blame] | 1041 | g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t); |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 1042 | g_client->srv_conn.clslot->clientId = cfg->client_id; |
| 1043 | /* default to client slot 0 */ |
| 1044 | if (cfg->client_slot == -1) |
| 1045 | g_client->srv_conn.clslot->slotNr = 0; |
| 1046 | else |
| 1047 | g_client->srv_conn.clslot->slotNr = cfg->client_slot; |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 1048 | g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t); |
| 1049 | *g_client->bankd_conn.clslot = *g_client->srv_conn.clslot; |
Harald Welte | 72cde10 | 2019-03-30 10:43:06 +0100 | [diff] [blame] | 1050 | } |
| 1051 | |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 1052 | /* create and [attempt to] establish connection to remsim-server */ |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 1053 | srvc = &g_client->srv_conn; |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 1054 | srvc->server_host = cfg->server_host; |
| 1055 | srvc->server_port = cfg->server_port; |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 1056 | srvc->handle_rx = srvc_handle_rx; |
| 1057 | srvc->own_comp_id.type = ComponentType_remsimClient; |
| 1058 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "simtrace2-remsim-client"); |
| 1059 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client"); |
| 1060 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION); |
| 1061 | rc = server_conn_fsm_alloc(g_client, srvc); |
| 1062 | if (rc < 0) { |
| 1063 | fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno)); |
| 1064 | exit(1); |
| 1065 | } |
Harald Welte | d2192e2 | 2019-11-07 18:10:57 +0100 | [diff] [blame] | 1066 | osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1067 | |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 1068 | /* create and not yet establish connection to remsim-bankd */ |
| 1069 | srvc = &g_client->srv_conn; |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 1070 | bankdc = &g_client->bankd_conn; |
| 1071 | /* server_host / server_port are configured from remsim-server */ |
| 1072 | bankdc->handle_rx = bankd_handle_rx; |
| 1073 | memcpy(&bankdc->own_comp_id, &srvc->own_comp_id, sizeof(bankdc->own_comp_id)); |
| 1074 | rc = server_conn_fsm_alloc(g_client, bankdc); |
| 1075 | if (rc < 0) { |
| 1076 | fprintf(stderr, "Unable to create bankd conn FSM: %s\n", strerror(errno)); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1077 | exit(1); |
| 1078 | } |
Harald Welte | 8219445 | 2019-12-14 17:18:34 +0100 | [diff] [blame] | 1079 | osmo_fsm_inst_update_id(bankdc->fi, "bankd"); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1080 | |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 1081 | asn_debug = 0; |
| 1082 | |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1083 | // connect to SIMtrace2 cardem |
| 1084 | do { |
| 1085 | struct usb_interface_match _ifm, *ifm = &_ifm; |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 1086 | ifm->vendor = cfg->usb.vendor_id; |
| 1087 | ifm->product = cfg->usb.product_id; |
| 1088 | ifm->configuration = cfg->usb.config_id; |
| 1089 | ifm->interface = cfg->usb.if_num; |
| 1090 | ifm->altsetting = cfg->usb.altsetting; |
| 1091 | ifm->addr = cfg->usb.addr; |
| 1092 | if (cfg->usb.path) |
| 1093 | osmo_strlcpy(ifm->path, cfg->usb.path, sizeof(ifm->path)); |
Harald Welte | fd5dafc | 2019-12-15 21:14:14 +0100 | [diff] [blame] | 1094 | transp->usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, ifm); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1095 | if (!transp->usb_devh) { |
| 1096 | fprintf(stderr, "can't open USB device\n"); |
| 1097 | goto close_exit; |
| 1098 | } |
| 1099 | |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 1100 | rc = libusb_claim_interface(transp->usb_devh, cfg->usb.if_num); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1101 | if (rc < 0) { |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 1102 | fprintf(stderr, "can't claim interface %d; rc=%d\n", cfg->usb.if_num, rc); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1103 | goto close_exit; |
| 1104 | } |
| 1105 | |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 1106 | rc = osmo_libusb_get_ep_addrs(transp->usb_devh, cfg->usb.if_num, &transp->usb_ep.out, |
Harald Welte | fd5dafc | 2019-12-15 21:14:14 +0100 | [diff] [blame] | 1107 | &transp->usb_ep.in, &transp->usb_ep.irq_in); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1108 | if (rc < 0) { |
| 1109 | fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc); |
| 1110 | goto close_exit; |
| 1111 | } |
| 1112 | |
Kévin Redon | 3428e41 | 2018-10-11 19:14:00 +0200 | [diff] [blame] | 1113 | // switch modem SIM port to emulated SIM on OWHW |
| 1114 | if (USB_VENDOR_OPENMOKO == ifm->vendor && USB_PRODUCT_OWHW_SAM3 == ifm->product) { // we are on the OWHW |
| 1115 | int modem = -1; |
| 1116 | switch (ifm->interface) { // the USB interface indicates for which modem we want to emulate the SIM |
| 1117 | case 0: |
| 1118 | modem = 1; |
| 1119 | break; |
| 1120 | case 1: |
| 1121 | modem = 2; |
| 1122 | break; |
| 1123 | default: |
| 1124 | fprintf(stderr, "unknown GPIO for SIMtrace interface %d\n", ifm->interface); |
| 1125 | goto close_exit; |
| 1126 | } |
| 1127 | // |
| 1128 | char gpio_path[PATH_MAX]; |
| 1129 | snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/connect_st_usim%d/value", modem); |
| 1130 | int connec_st_usim = open(gpio_path, O_WRONLY); |
| 1131 | if (-1 == connec_st_usim) { |
| 1132 | fprintf(stderr, "can't open GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem); |
| 1133 | goto close_exit; |
| 1134 | } |
| 1135 | if (1 != write(connec_st_usim, "1", 1)) { |
| 1136 | fprintf(stderr, "can't write GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem); |
| 1137 | goto close_exit; |
| 1138 | } |
| 1139 | printf("switched modem %d to emulated USIM\n", modem); |
| 1140 | |
| 1141 | snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/mdm%d_rst/value", modem); |
| 1142 | int mdm_rst = open(gpio_path, O_WRONLY); |
| 1143 | if (-1 == mdm_rst) { |
| 1144 | fprintf(stderr, "can't open GPIO %s to reset modem %d\n", gpio_path, modem); |
| 1145 | goto close_exit; |
| 1146 | } |
| 1147 | if (1 != write(mdm_rst, "1", 1)) { |
| 1148 | fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem); |
| 1149 | goto close_exit; |
| 1150 | } |
| 1151 | sleep(1); // wait a bit to ensure reset is effective |
| 1152 | if (1 != write(mdm_rst, "0", 1)) { |
| 1153 | fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem); |
| 1154 | goto close_exit; |
| 1155 | } |
| 1156 | printf("modem %d reset\n", modem); |
| 1157 | } |
| 1158 | |
Harald Welte | 0e0e932 | 2019-12-16 12:47:18 +0100 | [diff] [blame] | 1159 | /* request firmware to generate STATUS on IRQ endpoint */ |
| 1160 | cardem_request_config(ci, CEMU_FEAT_F_STATUS_IRQ); |
| 1161 | |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1162 | /* simulate card-insert to modem (owhw, not qmod) */ |
| 1163 | cardem_request_card_insert(ci, true); |
| 1164 | |
| 1165 | /* select remote (forwarded) SIM */ |
| 1166 | st_modem_sim_select_remote(ci->slot); |
| 1167 | |
| 1168 | /* set the ATR */ |
Kévin Redon | 206c3d7 | 2018-11-12 22:51:37 +0100 | [diff] [blame] | 1169 | //atr_update_csum(real_atr, sizeof(real_atr)); |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 1170 | cardem_request_set_atr(ci, cfg->atr.data, cfg->atr.len); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1171 | |
| 1172 | /* select remote (forwarded) SIM */ |
| 1173 | st_modem_reset_pulse(ci->slot, 300); |
| 1174 | |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 1175 | printf("Entering main loop\n"); |
| 1176 | |
| 1177 | allocate_and_submit_irq(ci); |
| 1178 | allocate_and_submit_in(ci); |
| 1179 | |
| 1180 | while (1) { |
| 1181 | osmo_select_main(false); |
| 1182 | } |
| 1183 | |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1184 | ret = 0; |
| 1185 | |
Kévin Redon | 193a8c1 | 2018-10-11 17:24:39 +0200 | [diff] [blame] | 1186 | libusb_release_interface(transp->usb_devh, 0); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1187 | close_exit: |
| 1188 | if (transp->usb_devh) |
| 1189 | libusb_close(transp->usb_devh); |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 1190 | if (cfg->keep_running) |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1191 | sleep(1); |
Harald Welte | 9636d2b | 2019-12-16 16:26:50 +0100 | [diff] [blame] | 1192 | } while (cfg->keep_running); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1193 | |
Kévin Redon | 193a8c1 | 2018-10-11 17:24:39 +0200 | [diff] [blame] | 1194 | libusb_exit(NULL); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1195 | do_exit: |
| 1196 | return ret; |
| 1197 | } |