blob: a61b2cf0733799d45782b2429d851562dd0623e0 [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>
Harald Weltedcfea282021-06-02 09:26:21 +020047#include <osmocom/core/logging.h>
Harald Welte964cda32019-11-24 22:27:10 +010048#include <osmocom/sim/class_tables.h>
49#include <osmocom/sim/sim.h>
50
Harald Weltedcfea282021-06-02 09:26:21 +020051#define LOGSLOT(slot, lvl, fmt, args...) \
52 LOGP(DLINP, lvl, "[%u] " fmt, (slot)->slot_nr, ## args)
53
Harald Welte964cda32019-11-24 22:27:10 +010054/***********************************************************************
55 * SIMTRACE core protocol
56 ***********************************************************************/
57
58/*! \brief allocate a message buffer for simtrace use */
59static struct msgb *st_msgb_alloc(void)
60{
61 return msgb_alloc_headroom(1024+32, 32, "SIMtrace");
62}
63
Harald Welte859f1b02020-02-22 16:45:05 +010064
65static void usb_out_xfer_cb(struct libusb_transfer *xfer)
Harald Welte964cda32019-11-24 22:27:10 +010066{
Harald Welte859f1b02020-02-22 16:45:05 +010067 struct msgb *msg = xfer->user_data;
Harald Welte964cda32019-11-24 22:27:10 +010068
Harald Welte859f1b02020-02-22 16:45:05 +010069 switch (xfer->status) {
70 case LIBUSB_TRANSFER_COMPLETED:
71 break;
72 case LIBUSB_TRANSFER_NO_DEVICE:
73 fprintf(stderr, "USB device disappeared\n");
74 exit(1);
75 break;
76 default:
77 fprintf(stderr, "USB OUT transfer failed, status=%u\n", xfer->status);
78 exit(1);
79 break;
Harald Welte964cda32019-11-24 22:27:10 +010080 }
81
82 msgb_free(msg);
Harald Welte859f1b02020-02-22 16:45:05 +010083 libusb_free_transfer(xfer);
84}
85
86
87static int st2_transp_tx_msg_usb_async(struct osmo_st2_transport *transp, struct msgb *msg)
88{
89 struct libusb_transfer *xfer;
90 int rc;
91
92 xfer = libusb_alloc_transfer(0);
93 OSMO_ASSERT(xfer);
94 xfer->dev_handle = transp->usb_devh;
95 xfer->flags = 0;
96 xfer->type = LIBUSB_TRANSFER_TYPE_BULK;
97 xfer->endpoint = transp->usb_ep.out;
98 xfer->timeout = 100000;
99 xfer->user_data = msg;
100 xfer->length = msgb_length(msg);
101 xfer->buffer = msgb_data(msg);
102 xfer->callback = usb_out_xfer_cb;
103
104 rc = libusb_submit_transfer(xfer);
105 OSMO_ASSERT(rc == 0);
106
107 return rc;
108}
109
110/*! \brief Transmit a given command to the SIMtrace2 device */
111static int st2_transp_tx_msg_usb_sync(struct osmo_st2_transport *transp, struct msgb *msg)
112{
113 int rc;
114 int xfer_len;
115 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.out,
116 msgb_data(msg), msgb_length(msg),
117 &xfer_len, 100000);
118 msgb_free(msg);
Harald Welte964cda32019-11-24 22:27:10 +0100119 return rc;
120}
121
122static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type,
123 uint8_t slot_nr)
124{
125 struct simtrace_msg_hdr *sh;
126
127 sh = (struct simtrace_msg_hdr *) msgb_push(msg, sizeof(*sh));
128 memset(sh, 0, sizeof(*sh));
129 sh->msg_class = msg_class;
130 sh->msg_type = msg_type;
131 sh->slot_nr = slot_nr;
132 sh->msg_len = msgb_length(msg);
133
134 return sh;
135}
136
137/* transmit a given message to a specified slot. Expects all headers
138 * present before calling the function */
Harald Welte208890a2019-11-24 22:46:51 +0100139int osmo_st2_slot_tx_msg(struct osmo_st2_slot *slot, struct msgb *msg,
140 uint8_t msg_class, uint8_t msg_type)
Harald Welte964cda32019-11-24 22:27:10 +0100141{
Harald Welte859f1b02020-02-22 16:45:05 +0100142 struct osmo_st2_transport *transp = slot->transp;
143 int rc;
Harald Welte964cda32019-11-24 22:27:10 +0100144
Harald Welte859f1b02020-02-22 16:45:05 +0100145 OSMO_ASSERT(transp);
146
147 st_push_hdr(msg, msg_class, msg_type, slot->slot_nr);
Harald Welte859f1b02020-02-22 16:45:05 +0100148
149 if (transp->udp_fd < 0) {
150 if (transp->usb_async)
151 rc = st2_transp_tx_msg_usb_async(transp, msg);
152 else
153 rc = st2_transp_tx_msg_usb_sync(transp, msg);
154 } else {
155 rc = write(transp->udp_fd, msgb_data(msg), msgb_length(msg));
156 msgb_free(msg);
157 }
158 return rc;
Harald Welte964cda32019-11-24 22:27:10 +0100159}
160
161/***********************************************************************
162 * Card Emulation protocol
163 ***********************************************************************/
164
165
166/*! \brief Request the SIMtrace2 to generate a card-insert signal */
Harald Welte208890a2019-11-24 22:46:51 +0100167int osmo_st2_cardem_request_card_insert(struct osmo_st2_cardem_inst *ci, bool inserted)
Harald Welte964cda32019-11-24 22:27:10 +0100168{
169 struct msgb *msg = st_msgb_alloc();
170 struct cardemu_usb_msg_cardinsert *cins;
171
Harald Weltedcfea282021-06-02 09:26:21 +0200172 LOGSLOT(ci->slot, LOGL_NOTICE, "<= %s(inserted=%d)\n", __func__, inserted);
173
Harald Welte964cda32019-11-24 22:27:10 +0100174 cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins));
175 memset(cins, 0, sizeof(*cins));
176 if (inserted)
177 cins->card_insert = 1;
178
Harald Welte208890a2019-11-24 22:46:51 +0100179 return osmo_st2_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT);
Harald Welte964cda32019-11-24 22:27:10 +0100180}
181
182/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */
Harald Welte208890a2019-11-24 22:46:51 +0100183int 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 +0100184{
185 struct msgb *msg = st_msgb_alloc();
186 struct cardemu_usb_msg_tx_data *txd;
187 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
188
Harald Weltedcfea282021-06-02 09:26:21 +0200189 LOGSLOT(ci->slot, LOGL_DEBUG, "<= %s(pb=%02x, le=%u)\n", __func__, pb, le);
Harald Welte964cda32019-11-24 22:27:10 +0100190
191 memset(txd, 0, sizeof(*txd));
192 txd->data_len = 1;
193 txd->flags = CEMU_DATA_F_PB_AND_RX;
194 /* one data byte */
195 msgb_put_u8(msg, pb);
196
Harald Welte208890a2019-11-24 22:46:51 +0100197 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 +0100198}
199
200/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */
Harald Welte208890a2019-11-24 22:46:51 +0100201int osmo_st2_cardem_request_pb_and_tx(struct osmo_st2_cardem_inst *ci, uint8_t pb,
202 const uint8_t *data, uint16_t data_len_in)
Harald Welte964cda32019-11-24 22:27:10 +0100203{
204 struct msgb *msg = st_msgb_alloc();
205 struct cardemu_usb_msg_tx_data *txd;
206 uint8_t *cur;
207
208 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
209
Harald Weltedcfea282021-06-02 09:26:21 +0200210 LOGSLOT(ci->slot, LOGL_DEBUG, "<= %s(pb=%02x, tx=%s, len=%d)\n", __func__, pb,
Harald Welte964cda32019-11-24 22:27:10 +0100211 osmo_hexdump(data, data_len_in), data_len_in);
212
213 memset(txd, 0, sizeof(*txd));
214 txd->data_len = 1 + data_len_in;
215 txd->flags = CEMU_DATA_F_PB_AND_TX;
216 /* procedure byte */
217 msgb_put_u8(msg, pb);
218 /* data */
219 cur = msgb_put(msg, data_len_in);
220 memcpy(cur, data, data_len_in);
221
Harald Welte208890a2019-11-24 22:46:51 +0100222 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 +0100223}
224
225/*! \brief Request the SIMtrace2 to send a Status Word */
Harald Welte208890a2019-11-24 22:46:51 +0100226int osmo_st2_cardem_request_sw_tx(struct osmo_st2_cardem_inst *ci, const uint8_t *sw)
Harald Welte964cda32019-11-24 22:27:10 +0100227{
228 struct msgb *msg = st_msgb_alloc();
229 struct cardemu_usb_msg_tx_data *txd;
230 uint8_t *cur;
231
232 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
233
Harald Weltedcfea282021-06-02 09:26:21 +0200234 LOGSLOT(ci->slot, LOGL_DEBUG, "<= %s(sw=%02x%02x)\n", __func__, sw[0], sw[1]);
Harald Welte964cda32019-11-24 22:27:10 +0100235
236 memset(txd, 0, sizeof(*txd));
237 txd->data_len = 2;
238 txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL;
239 cur = msgb_put(msg, 2);
240 cur[0] = sw[0];
241 cur[1] = sw[1];
242
Harald Welte208890a2019-11-24 22:46:51 +0100243 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 +0100244}
245
Harald Welte208890a2019-11-24 22:46:51 +0100246int 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 +0100247{
248 struct msgb *msg = st_msgb_alloc();
249 struct cardemu_usb_msg_set_atr *satr;
250 uint8_t *cur;
251
252 satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr));
253
Harald Weltedcfea282021-06-02 09:26:21 +0200254 LOGSLOT(ci->slot, LOGL_NOTICE, "<= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
Harald Welte964cda32019-11-24 22:27:10 +0100255
256 memset(satr, 0, sizeof(*satr));
257 satr->atr_len = atr_len;
258 cur = msgb_put(msg, atr_len);
259 memcpy(cur, atr, atr_len);
260
Harald Welte208890a2019-11-24 22:46:51 +0100261 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 +0100262}
263
Harald Welte02712372020-02-22 21:53:50 +0100264int osmo_st2_cardem_request_config(struct osmo_st2_cardem_inst *ci, uint32_t features)
265{
266 struct msgb *msg = st_msgb_alloc();
267 struct cardemu_usb_msg_config *cfg;
268
269 cfg = (struct cardemu_usb_msg_config *) msgb_put(msg, sizeof(*cfg));
270
Harald Weltedcfea282021-06-02 09:26:21 +0200271 LOGSLOT(ci->slot, LOGL_NOTICE, "<= %s(features=%08x)\n", __func__, features);
Harald Welte02712372020-02-22 21:53:50 +0100272
273 memset(cfg, 0, sizeof(*cfg));
274 cfg->features = features;
275
276 return osmo_st2_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_BD_CEMU_CONFIG);
277}
278
Harald Welte964cda32019-11-24 22:27:10 +0100279/***********************************************************************
280 * Modem Control protocol
281 ***********************************************************************/
282
Harald Welte208890a2019-11-24 22:46:51 +0100283static int _modem_reset(struct osmo_st2_slot *slot, uint8_t asserted, uint16_t pulse_ms)
Harald Welte964cda32019-11-24 22:27:10 +0100284{
285 struct msgb *msg = st_msgb_alloc();
286 struct st_modem_reset *sr ;
287
Harald Weltedcfea282021-06-02 09:26:21 +0200288 LOGSLOT(slot, LOGL_NOTICE, "<= %s(asserted=%u, pulse_ms=%u)\n", __func__,
289 asserted, pulse_ms);
290
Harald Welte964cda32019-11-24 22:27:10 +0100291 sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr));
292 sr->asserted = asserted;
293 sr->pulse_duration_msec = pulse_ms;
294
Harald Welte208890a2019-11-24 22:46:51 +0100295 return osmo_st2_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET);
Harald Welte964cda32019-11-24 22:27:10 +0100296}
297
298/*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/
Harald Welte208890a2019-11-24 22:46:51 +0100299int osmo_st2_modem_reset_pulse(struct osmo_st2_slot *slot, uint16_t duration_ms)
Harald Welte964cda32019-11-24 22:27:10 +0100300{
301 return _modem_reset(slot, 2, duration_ms);
302}
303
304/*! \brief assert the RESET line of the modem */
Harald Welte208890a2019-11-24 22:46:51 +0100305int osmo_st2_modem_reset_active(struct osmo_st2_slot *slot)
Harald Welte964cda32019-11-24 22:27:10 +0100306{
307 return _modem_reset(slot, 1, 0);
308}
309
310/*! \brief de-assert the RESET line of the modem */
Harald Welte208890a2019-11-24 22:46:51 +0100311int osmo_st2_modem_reset_inactive(struct osmo_st2_slot *slot)
Harald Welte964cda32019-11-24 22:27:10 +0100312{
313 return _modem_reset(slot, 0, 0);
314}
315
Harald Welte208890a2019-11-24 22:46:51 +0100316static int _modem_sim_select(struct osmo_st2_slot *slot, uint8_t remote_sim)
Harald Welte964cda32019-11-24 22:27:10 +0100317{
318 struct msgb *msg = st_msgb_alloc();
319 struct st_modem_sim_select *ss;
320
Harald Weltedcfea282021-06-02 09:26:21 +0200321 LOGSLOT(slot, LOGL_NOTICE, "<= %s(remote_sim=%u)\n", __func__, remote_sim);
322
Harald Welte964cda32019-11-24 22:27:10 +0100323 ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss));
324 ss->remote_sim = remote_sim;
325
Harald Welte208890a2019-11-24 22:46:51 +0100326 return osmo_st2_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT);
Harald Welte964cda32019-11-24 22:27:10 +0100327}
328
329/*! \brief select local (physical) SIM for given slot */
Harald Welte208890a2019-11-24 22:46:51 +0100330int osmo_st2_modem_sim_select_local(struct osmo_st2_slot *slot)
Harald Welte964cda32019-11-24 22:27:10 +0100331{
332 return _modem_sim_select(slot, 0);
333}
334
335/*! \brief select remote (emulated/forwarded) SIM for given slot */
Harald Welte208890a2019-11-24 22:46:51 +0100336int osmo_st2_modem_sim_select_remote(struct osmo_st2_slot *slot)
Harald Welte964cda32019-11-24 22:27:10 +0100337{
338 return _modem_sim_select(slot, 1);
339}
340
341/*! \brief Request slot to send us status information about the modem */
Harald Welte208890a2019-11-24 22:46:51 +0100342int osmo_st2_modem_get_status(struct osmo_st2_slot *slot)
Harald Welte964cda32019-11-24 22:27:10 +0100343{
344 struct msgb *msg = st_msgb_alloc();
345
Harald Welte208890a2019-11-24 22:46:51 +0100346 return osmo_st2_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS);
Harald Welte964cda32019-11-24 22:27:10 +0100347}