blob: 4e16fd1ff8957eb59b3471a3c2416120d884c8c3 [file] [log] [blame]
Harald Welte964cda32019-11-24 22:27:10 +01001
2/* simtrace2-protocol - USB protocol library code for SIMtrace2
3 *
4 * (C) 2016-2019 by Harald Welte <hwelte@hmw-consulting.de>
5 * (C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22#include <errno.h>
23#include <unistd.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <stdint.h>
28#include <signal.h>
29#include <time.h>
30#define _GNU_SOURCE
31#include <getopt.h>
32
33#include <sys/time.h>
34#include <sys/types.h>
35#include <sys/socket.h>
36#include <netinet/in.h>
37#include <arpa/inet.h>
38
39#include <libusb.h>
40
Harald Welte964cda32019-11-24 22:27:10 +010041#include <osmocom/simtrace2/simtrace_prot.h>
42#include <osmocom/simtrace2/simtrace2_api.h>
Harald Welte964cda32019-11-24 22:27:10 +010043
44#include <osmocom/core/utils.h>
45#include <osmocom/core/socket.h>
46#include <osmocom/core/msgb.h>
47#include <osmocom/sim/class_tables.h>
48#include <osmocom/sim/sim.h>
49
50/***********************************************************************
51 * SIMTRACE core protocol
52 ***********************************************************************/
53
54/*! \brief allocate a message buffer for simtrace use */
55static struct msgb *st_msgb_alloc(void)
56{
57 return msgb_alloc_headroom(1024+32, 32, "SIMtrace");
58}
59
Harald Welte859f1b02020-02-22 16:45:05 +010060
61static void usb_out_xfer_cb(struct libusb_transfer *xfer)
Harald Welte964cda32019-11-24 22:27:10 +010062{
Harald Welte859f1b02020-02-22 16:45:05 +010063 struct msgb *msg = xfer->user_data;
Harald Welte964cda32019-11-24 22:27:10 +010064
Harald Welte859f1b02020-02-22 16:45:05 +010065 switch (xfer->status) {
66 case LIBUSB_TRANSFER_COMPLETED:
67 break;
68 case LIBUSB_TRANSFER_NO_DEVICE:
69 fprintf(stderr, "USB device disappeared\n");
70 exit(1);
71 break;
72 default:
73 fprintf(stderr, "USB OUT transfer failed, status=%u\n", xfer->status);
74 exit(1);
75 break;
Harald Welte964cda32019-11-24 22:27:10 +010076 }
77
78 msgb_free(msg);
Harald Welte859f1b02020-02-22 16:45:05 +010079 libusb_free_transfer(xfer);
80}
81
82
83static int st2_transp_tx_msg_usb_async(struct osmo_st2_transport *transp, struct msgb *msg)
84{
85 struct libusb_transfer *xfer;
86 int rc;
87
88 xfer = libusb_alloc_transfer(0);
89 OSMO_ASSERT(xfer);
90 xfer->dev_handle = transp->usb_devh;
91 xfer->flags = 0;
92 xfer->type = LIBUSB_TRANSFER_TYPE_BULK;
93 xfer->endpoint = transp->usb_ep.out;
94 xfer->timeout = 100000;
95 xfer->user_data = msg;
96 xfer->length = msgb_length(msg);
97 xfer->buffer = msgb_data(msg);
98 xfer->callback = usb_out_xfer_cb;
99
100 rc = libusb_submit_transfer(xfer);
101 OSMO_ASSERT(rc == 0);
102
103 return rc;
104}
105
106/*! \brief Transmit a given command to the SIMtrace2 device */
107static int st2_transp_tx_msg_usb_sync(struct osmo_st2_transport *transp, struct msgb *msg)
108{
109 int rc;
110 int xfer_len;
111 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.out,
112 msgb_data(msg), msgb_length(msg),
113 &xfer_len, 100000);
114 msgb_free(msg);
Harald Welte964cda32019-11-24 22:27:10 +0100115 return rc;
116}
117
118static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type,
119 uint8_t slot_nr)
120{
121 struct simtrace_msg_hdr *sh;
122
123 sh = (struct simtrace_msg_hdr *) msgb_push(msg, sizeof(*sh));
124 memset(sh, 0, sizeof(*sh));
125 sh->msg_class = msg_class;
126 sh->msg_type = msg_type;
127 sh->slot_nr = slot_nr;
128 sh->msg_len = msgb_length(msg);
129
130 return sh;
131}
132
133/* transmit a given message to a specified slot. Expects all headers
134 * present before calling the function */
Harald Welte208890a2019-11-24 22:46:51 +0100135int osmo_st2_slot_tx_msg(struct osmo_st2_slot *slot, struct msgb *msg,
136 uint8_t msg_class, uint8_t msg_type)
Harald Welte964cda32019-11-24 22:27:10 +0100137{
Harald Welte859f1b02020-02-22 16:45:05 +0100138 struct osmo_st2_transport *transp = slot->transp;
139 int rc;
Harald Welte964cda32019-11-24 22:27:10 +0100140
Harald Welte859f1b02020-02-22 16:45:05 +0100141 OSMO_ASSERT(transp);
142
143 st_push_hdr(msg, msg_class, msg_type, slot->slot_nr);
144 printf("SIMtrace <- %s\n", msgb_hexdump(msg));
145
146 if (transp->udp_fd < 0) {
147 if (transp->usb_async)
148 rc = st2_transp_tx_msg_usb_async(transp, msg);
149 else
150 rc = st2_transp_tx_msg_usb_sync(transp, msg);
151 } else {
152 rc = write(transp->udp_fd, msgb_data(msg), msgb_length(msg));
153 msgb_free(msg);
154 }
155 return rc;
Harald Welte964cda32019-11-24 22:27:10 +0100156}
157
158/***********************************************************************
159 * Card Emulation protocol
160 ***********************************************************************/
161
162
163/*! \brief Request the SIMtrace2 to generate a card-insert signal */
Harald Welte208890a2019-11-24 22:46:51 +0100164int osmo_st2_cardem_request_card_insert(struct osmo_st2_cardem_inst *ci, bool inserted)
Harald Welte964cda32019-11-24 22:27:10 +0100165{
166 struct msgb *msg = st_msgb_alloc();
167 struct cardemu_usb_msg_cardinsert *cins;
168
169 cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins));
170 memset(cins, 0, sizeof(*cins));
171 if (inserted)
172 cins->card_insert = 1;
173
Harald Welte208890a2019-11-24 22:46:51 +0100174 return osmo_st2_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT);
Harald Welte964cda32019-11-24 22:27:10 +0100175}
176
177/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */
Harald Welte208890a2019-11-24 22:46:51 +0100178int osmo_st2_cardem_request_pb_and_rx(struct osmo_st2_cardem_inst *ci, uint8_t pb, uint8_t le)
Harald Welte964cda32019-11-24 22:27:10 +0100179{
180 struct msgb *msg = st_msgb_alloc();
181 struct cardemu_usb_msg_tx_data *txd;
182 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
183
184 printf("<= %s(%02x, %d)\n", __func__, pb, le);
185
186 memset(txd, 0, sizeof(*txd));
187 txd->data_len = 1;
188 txd->flags = CEMU_DATA_F_PB_AND_RX;
189 /* one data byte */
190 msgb_put_u8(msg, pb);
191
Harald Welte208890a2019-11-24 22:46:51 +0100192 return osmo_st2_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
Harald Welte964cda32019-11-24 22:27:10 +0100193}
194
195/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */
Harald Welte208890a2019-11-24 22:46:51 +0100196int osmo_st2_cardem_request_pb_and_tx(struct osmo_st2_cardem_inst *ci, uint8_t pb,
197 const uint8_t *data, uint16_t data_len_in)
Harald Welte964cda32019-11-24 22:27:10 +0100198{
199 struct msgb *msg = st_msgb_alloc();
200 struct cardemu_usb_msg_tx_data *txd;
201 uint8_t *cur;
202
203 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
204
205 printf("<= %s(%02x, %s, %d)\n", __func__, pb,
206 osmo_hexdump(data, data_len_in), data_len_in);
207
208 memset(txd, 0, sizeof(*txd));
209 txd->data_len = 1 + data_len_in;
210 txd->flags = CEMU_DATA_F_PB_AND_TX;
211 /* procedure byte */
212 msgb_put_u8(msg, pb);
213 /* data */
214 cur = msgb_put(msg, data_len_in);
215 memcpy(cur, data, data_len_in);
216
Harald Welte208890a2019-11-24 22:46:51 +0100217 return osmo_st2_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
Harald Welte964cda32019-11-24 22:27:10 +0100218}
219
220/*! \brief Request the SIMtrace2 to send a Status Word */
Harald Welte208890a2019-11-24 22:46:51 +0100221int osmo_st2_cardem_request_sw_tx(struct osmo_st2_cardem_inst *ci, const uint8_t *sw)
Harald Welte964cda32019-11-24 22:27:10 +0100222{
223 struct msgb *msg = st_msgb_alloc();
224 struct cardemu_usb_msg_tx_data *txd;
225 uint8_t *cur;
226
227 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
228
229 printf("<= %s(%02x %02x)\n", __func__, sw[0], sw[1]);
230
231 memset(txd, 0, sizeof(*txd));
232 txd->data_len = 2;
233 txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL;
234 cur = msgb_put(msg, 2);
235 cur[0] = sw[0];
236 cur[1] = sw[1];
237
Harald Welte208890a2019-11-24 22:46:51 +0100238 return osmo_st2_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
Harald Welte964cda32019-11-24 22:27:10 +0100239}
240
Harald Welte208890a2019-11-24 22:46:51 +0100241int osmo_st2_cardem_request_set_atr(struct osmo_st2_cardem_inst *ci, const uint8_t *atr, unsigned int atr_len)
Harald Welte964cda32019-11-24 22:27:10 +0100242{
243 struct msgb *msg = st_msgb_alloc();
244 struct cardemu_usb_msg_set_atr *satr;
245 uint8_t *cur;
246
247 satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr));
248
249 printf("<= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
250
251 memset(satr, 0, sizeof(*satr));
252 satr->atr_len = atr_len;
253 cur = msgb_put(msg, atr_len);
254 memcpy(cur, atr, atr_len);
255
Harald Welte208890a2019-11-24 22:46:51 +0100256 return osmo_st2_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR);
Harald Welte964cda32019-11-24 22:27:10 +0100257}
258
Harald Welte02712372020-02-22 21:53:50 +0100259int osmo_st2_cardem_request_config(struct osmo_st2_cardem_inst *ci, uint32_t features)
260{
261 struct msgb *msg = st_msgb_alloc();
262 struct cardemu_usb_msg_config *cfg;
263
264 cfg = (struct cardemu_usb_msg_config *) msgb_put(msg, sizeof(*cfg));
265
266 printf("<= %s(%08x)\n", __func__, features);
267
268 memset(cfg, 0, sizeof(*cfg));
269 cfg->features = features;
270
271 return osmo_st2_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_BD_CEMU_CONFIG);
272}
273
Harald Welte964cda32019-11-24 22:27:10 +0100274/***********************************************************************
275 * Modem Control protocol
276 ***********************************************************************/
277
Harald Welte208890a2019-11-24 22:46:51 +0100278static int _modem_reset(struct osmo_st2_slot *slot, uint8_t asserted, uint16_t pulse_ms)
Harald Welte964cda32019-11-24 22:27:10 +0100279{
280 struct msgb *msg = st_msgb_alloc();
281 struct st_modem_reset *sr ;
282
283 sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr));
284 sr->asserted = asserted;
285 sr->pulse_duration_msec = pulse_ms;
286
Harald Welte208890a2019-11-24 22:46:51 +0100287 return osmo_st2_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET);
Harald Welte964cda32019-11-24 22:27:10 +0100288}
289
290/*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/
Harald Welte208890a2019-11-24 22:46:51 +0100291int osmo_st2_modem_reset_pulse(struct osmo_st2_slot *slot, uint16_t duration_ms)
Harald Welte964cda32019-11-24 22:27:10 +0100292{
293 return _modem_reset(slot, 2, duration_ms);
294}
295
296/*! \brief assert the RESET line of the modem */
Harald Welte208890a2019-11-24 22:46:51 +0100297int osmo_st2_modem_reset_active(struct osmo_st2_slot *slot)
Harald Welte964cda32019-11-24 22:27:10 +0100298{
299 return _modem_reset(slot, 1, 0);
300}
301
302/*! \brief de-assert the RESET line of the modem */
Harald Welte208890a2019-11-24 22:46:51 +0100303int osmo_st2_modem_reset_inactive(struct osmo_st2_slot *slot)
Harald Welte964cda32019-11-24 22:27:10 +0100304{
305 return _modem_reset(slot, 0, 0);
306}
307
Harald Welte208890a2019-11-24 22:46:51 +0100308static int _modem_sim_select(struct osmo_st2_slot *slot, uint8_t remote_sim)
Harald Welte964cda32019-11-24 22:27:10 +0100309{
310 struct msgb *msg = st_msgb_alloc();
311 struct st_modem_sim_select *ss;
312
313 ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss));
314 ss->remote_sim = remote_sim;
315
Harald Welte208890a2019-11-24 22:46:51 +0100316 return osmo_st2_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT);
Harald Welte964cda32019-11-24 22:27:10 +0100317}
318
319/*! \brief select local (physical) SIM for given slot */
Harald Welte208890a2019-11-24 22:46:51 +0100320int osmo_st2_modem_sim_select_local(struct osmo_st2_slot *slot)
Harald Welte964cda32019-11-24 22:27:10 +0100321{
322 return _modem_sim_select(slot, 0);
323}
324
325/*! \brief select remote (emulated/forwarded) SIM for given slot */
Harald Welte208890a2019-11-24 22:46:51 +0100326int osmo_st2_modem_sim_select_remote(struct osmo_st2_slot *slot)
Harald Welte964cda32019-11-24 22:27:10 +0100327{
328 return _modem_sim_select(slot, 1);
329}
330
331/*! \brief Request slot to send us status information about the modem */
Harald Welte208890a2019-11-24 22:46:51 +0100332int osmo_st2_modem_get_status(struct osmo_st2_slot *slot)
Harald Welte964cda32019-11-24 22:27:10 +0100333{
334 struct msgb *msg = st_msgb_alloc();
335
Harald Welte208890a2019-11-24 22:46:51 +0100336 return osmo_st2_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS);
Harald Welte964cda32019-11-24 22:27:10 +0100337}