blob: b2b2829fb1b6ff634280f3cf0a8379fb6a1963fd [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 Welte964cda32019-11-24 22:27:10 +010060/*! \brief Transmit a given command to the SIMtrace2 device */
Harald Welte208890a2019-11-24 22:46:51 +010061int osmo_st2_transp_tx_msg(struct osmo_st2_transport *transp, struct msgb *msg)
Harald Welte964cda32019-11-24 22:27:10 +010062{
63 int rc;
64
65 printf("<- %s\n", msgb_hexdump(msg));
66
67 if (transp->udp_fd < 0) {
68 int xfer_len;
69
70 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.out,
71 msgb_data(msg), msgb_length(msg),
72 &xfer_len, 100000);
73 } else {
74 rc = write(transp->udp_fd, msgb_data(msg), msgb_length(msg));
75 }
76
77 msgb_free(msg);
78 return rc;
79}
80
81static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type,
82 uint8_t slot_nr)
83{
84 struct simtrace_msg_hdr *sh;
85
86 sh = (struct simtrace_msg_hdr *) msgb_push(msg, sizeof(*sh));
87 memset(sh, 0, sizeof(*sh));
88 sh->msg_class = msg_class;
89 sh->msg_type = msg_type;
90 sh->slot_nr = slot_nr;
91 sh->msg_len = msgb_length(msg);
92
93 return sh;
94}
95
96/* transmit a given message to a specified slot. Expects all headers
97 * present before calling the function */
Harald Welte208890a2019-11-24 22:46:51 +010098int osmo_st2_slot_tx_msg(struct osmo_st2_slot *slot, struct msgb *msg,
99 uint8_t msg_class, uint8_t msg_type)
Harald Welte964cda32019-11-24 22:27:10 +0100100{
101 st_push_hdr(msg, msg_class, msg_type, slot->slot_nr);
102
Harald Welte208890a2019-11-24 22:46:51 +0100103 return osmo_st2_transp_tx_msg(slot->transp, msg);
Harald Welte964cda32019-11-24 22:27:10 +0100104}
105
106/***********************************************************************
107 * Card Emulation protocol
108 ***********************************************************************/
109
110
111/*! \brief Request the SIMtrace2 to generate a card-insert signal */
Harald Welte208890a2019-11-24 22:46:51 +0100112int osmo_st2_cardem_request_card_insert(struct osmo_st2_cardem_inst *ci, bool inserted)
Harald Welte964cda32019-11-24 22:27:10 +0100113{
114 struct msgb *msg = st_msgb_alloc();
115 struct cardemu_usb_msg_cardinsert *cins;
116
117 cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins));
118 memset(cins, 0, sizeof(*cins));
119 if (inserted)
120 cins->card_insert = 1;
121
Harald Welte208890a2019-11-24 22:46:51 +0100122 return osmo_st2_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT);
Harald Welte964cda32019-11-24 22:27:10 +0100123}
124
125/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */
Harald Welte208890a2019-11-24 22:46:51 +0100126int 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 +0100127{
128 struct msgb *msg = st_msgb_alloc();
129 struct cardemu_usb_msg_tx_data *txd;
130 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
131
132 printf("<= %s(%02x, %d)\n", __func__, pb, le);
133
134 memset(txd, 0, sizeof(*txd));
135 txd->data_len = 1;
136 txd->flags = CEMU_DATA_F_PB_AND_RX;
137 /* one data byte */
138 msgb_put_u8(msg, pb);
139
Harald Welte208890a2019-11-24 22:46:51 +0100140 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 +0100141}
142
143/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */
Harald Welte208890a2019-11-24 22:46:51 +0100144int osmo_st2_cardem_request_pb_and_tx(struct osmo_st2_cardem_inst *ci, uint8_t pb,
145 const uint8_t *data, uint16_t data_len_in)
Harald Welte964cda32019-11-24 22:27:10 +0100146{
147 struct msgb *msg = st_msgb_alloc();
148 struct cardemu_usb_msg_tx_data *txd;
149 uint8_t *cur;
150
151 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
152
153 printf("<= %s(%02x, %s, %d)\n", __func__, pb,
154 osmo_hexdump(data, data_len_in), data_len_in);
155
156 memset(txd, 0, sizeof(*txd));
157 txd->data_len = 1 + data_len_in;
158 txd->flags = CEMU_DATA_F_PB_AND_TX;
159 /* procedure byte */
160 msgb_put_u8(msg, pb);
161 /* data */
162 cur = msgb_put(msg, data_len_in);
163 memcpy(cur, data, data_len_in);
164
Harald Welte208890a2019-11-24 22:46:51 +0100165 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 +0100166}
167
168/*! \brief Request the SIMtrace2 to send a Status Word */
Harald Welte208890a2019-11-24 22:46:51 +0100169int osmo_st2_cardem_request_sw_tx(struct osmo_st2_cardem_inst *ci, const uint8_t *sw)
Harald Welte964cda32019-11-24 22:27:10 +0100170{
171 struct msgb *msg = st_msgb_alloc();
172 struct cardemu_usb_msg_tx_data *txd;
173 uint8_t *cur;
174
175 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
176
177 printf("<= %s(%02x %02x)\n", __func__, sw[0], sw[1]);
178
179 memset(txd, 0, sizeof(*txd));
180 txd->data_len = 2;
181 txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL;
182 cur = msgb_put(msg, 2);
183 cur[0] = sw[0];
184 cur[1] = sw[1];
185
Harald Welte208890a2019-11-24 22:46:51 +0100186 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 +0100187}
188
Harald Welte208890a2019-11-24 22:46:51 +0100189int 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 +0100190{
191 struct msgb *msg = st_msgb_alloc();
192 struct cardemu_usb_msg_set_atr *satr;
193 uint8_t *cur;
194
195 satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr));
196
197 printf("<= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
198
199 memset(satr, 0, sizeof(*satr));
200 satr->atr_len = atr_len;
201 cur = msgb_put(msg, atr_len);
202 memcpy(cur, atr, atr_len);
203
Harald Welte208890a2019-11-24 22:46:51 +0100204 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 +0100205}
206
207/***********************************************************************
208 * Modem Control protocol
209 ***********************************************************************/
210
Harald Welte208890a2019-11-24 22:46:51 +0100211static int _modem_reset(struct osmo_st2_slot *slot, uint8_t asserted, uint16_t pulse_ms)
Harald Welte964cda32019-11-24 22:27:10 +0100212{
213 struct msgb *msg = st_msgb_alloc();
214 struct st_modem_reset *sr ;
215
216 sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr));
217 sr->asserted = asserted;
218 sr->pulse_duration_msec = pulse_ms;
219
Harald Welte208890a2019-11-24 22:46:51 +0100220 return osmo_st2_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET);
Harald Welte964cda32019-11-24 22:27:10 +0100221}
222
223/*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/
Harald Welte208890a2019-11-24 22:46:51 +0100224int osmo_st2_modem_reset_pulse(struct osmo_st2_slot *slot, uint16_t duration_ms)
Harald Welte964cda32019-11-24 22:27:10 +0100225{
226 return _modem_reset(slot, 2, duration_ms);
227}
228
229/*! \brief assert the RESET line of the modem */
Harald Welte208890a2019-11-24 22:46:51 +0100230int osmo_st2_modem_reset_active(struct osmo_st2_slot *slot)
Harald Welte964cda32019-11-24 22:27:10 +0100231{
232 return _modem_reset(slot, 1, 0);
233}
234
235/*! \brief de-assert the RESET line of the modem */
Harald Welte208890a2019-11-24 22:46:51 +0100236int osmo_st2_modem_reset_inactive(struct osmo_st2_slot *slot)
Harald Welte964cda32019-11-24 22:27:10 +0100237{
238 return _modem_reset(slot, 0, 0);
239}
240
Harald Welte208890a2019-11-24 22:46:51 +0100241static int _modem_sim_select(struct osmo_st2_slot *slot, uint8_t remote_sim)
Harald Welte964cda32019-11-24 22:27:10 +0100242{
243 struct msgb *msg = st_msgb_alloc();
244 struct st_modem_sim_select *ss;
245
246 ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss));
247 ss->remote_sim = remote_sim;
248
Harald Welte208890a2019-11-24 22:46:51 +0100249 return osmo_st2_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT);
Harald Welte964cda32019-11-24 22:27:10 +0100250}
251
252/*! \brief select local (physical) SIM for given slot */
Harald Welte208890a2019-11-24 22:46:51 +0100253int osmo_st2_modem_sim_select_local(struct osmo_st2_slot *slot)
Harald Welte964cda32019-11-24 22:27:10 +0100254{
255 return _modem_sim_select(slot, 0);
256}
257
258/*! \brief select remote (emulated/forwarded) SIM for given slot */
Harald Welte208890a2019-11-24 22:46:51 +0100259int osmo_st2_modem_sim_select_remote(struct osmo_st2_slot *slot)
Harald Welte964cda32019-11-24 22:27:10 +0100260{
261 return _modem_sim_select(slot, 1);
262}
263
264/*! \brief Request slot to send us status information about the modem */
Harald Welte208890a2019-11-24 22:46:51 +0100265int osmo_st2_modem_get_status(struct osmo_st2_slot *slot)
Harald Welte964cda32019-11-24 22:27:10 +0100266{
267 struct msgb *msg = st_msgb_alloc();
268
Harald Welte208890a2019-11-24 22:46:51 +0100269 return osmo_st2_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS);
Harald Welte964cda32019-11-24 22:27:10 +0100270}