diff --git a/host/simtrace2-remsim.c b/host/simtrace2-remsim.c index 0f1eab67..1f4f5d05 100644 --- a/host/simtrace2-remsim.c +++ b/host/simtrace2-remsim.c @@ -49,10 +49,8 @@ #include #include -static struct gsmtap_inst *g_gti; - /* transport to a SIMtrace device */ -static struct st_transport { +struct st_transport { /* USB */ struct libusb_device_handle *usb_devh; struct { @@ -65,13 +63,27 @@ static struct st_transport { int udp_fd; }; +/* a SIMtrace slot; communicates over a transport */ +struct st_slot { + /* transport through which the slot can be reached */ + struct st_transport *transp; + /* number of the slot within the transport */ + uint8_t slot_nr; +}; + /* One istance of card emulation */ struct cardem_inst { - struct st_transport transp; + /* slot on which this card emulation instance runs */ + struct st_slot *slot; + /* libosmosim SIM card profile */ const struct osim_cla_ins_card_profile *card_prof; + /* libosmosim SIM card channel */ struct osim_chan_hdl *chan; }; +/* global GSMTAP instance */ +static struct gsmtap_inst *g_gti; + static int gsmtap_send_sim(const uint8_t *apdu, unsigned int len) { struct gsmtap_hdr *gh; @@ -105,6 +117,7 @@ static int gsmtap_send_sim(const uint8_t *apdu, unsigned int len) * SIMTRACE pcore protocol ***********************************************************************/ +/*! \brief allocate a message buffer for simtrace use */ static struct msgb *st_msgb_alloc(void) { return msgb_alloc_headroom(1024+32, 32, "SIMtrace"); @@ -119,7 +132,7 @@ static void apdu_out_cb(uint8_t *buf, unsigned int len, void *user_data) #endif /*! \brief Transmit a given command to the SIMtrace2 device */ -static int st_transp_tx_msg(struct st_transport *transp, struct msgb *msg) +int st_transp_tx_msg(struct st_transport *transp, struct msgb *msg) { int rc; @@ -139,16 +152,33 @@ static int st_transp_tx_msg(struct st_transport *transp, struct msgb *msg) return rc; } -static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type) +static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type, + uint8_t slot_nr) { struct simtrace_msg_hdr *sh = msgb_push(msg, sizeof(*sh)); memset(sh, 0, sizeof(*sh)); sh->msg_class = msg_class; sh->msg_type = msg_type; + sh->slot_nr = slot_nr; sh->msg_len = msgb_length(msg); } +/* transmit a given message to a specified slot. Expects all headers + * present before calling the function */ +int st_slot_tx_msg(struct st_slot *slot, struct msgb *msg, + uint8_t msg_class, uint8_t msg_type) +{ + struct simtrace_msg_hdr *sh = msg->data; + + sh->slot_nr = slot->slot_nr; + + st_push_hdr(msg, msg_class, msg_type, slot->slot_nr); + + return st_transp_tx_msg(slot->transp, msg); +} + + /*********************************************************************** * Card Emulation protocol ***********************************************************************/ @@ -165,9 +195,7 @@ static int cardem_request_card_insert(struct cardem_inst *ci, bool inserted) if (inserted) cins->card_insert = 1; - st_push_hdr(msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT); - - return st_transp_tx_msg(&ci->transp, msg); + return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT); } /*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */ @@ -185,9 +213,7 @@ static int cardem_request_pb_and_rx(struct cardem_inst *ci, uint8_t pb, uint8_t /* one data byte */ msgb_put_u8(msg, pb); - st_push_hdr(msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA); - - return st_transp_tx_msg(&ci->transp, msg); + return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA); } /*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */ @@ -212,9 +238,7 @@ static int cardem_request_pb_and_tx(struct cardem_inst *ci, uint8_t pb, cur = msgb_put(msg, data_len_in); memcpy(cur, data, data_len_in); - st_push_hdr(msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA); - - return st_transp_tx_msg(&ci->transp, msg); + return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA); } /*! \brief Request the SIMtrace2 to send a Status Word */ @@ -235,9 +259,7 @@ static int cardem_request_sw_tx(struct cardem_inst *ci, const uint8_t *sw) cur[0] = sw[0]; cur[1] = sw[1]; - st_push_hdr(msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA); - - return st_transp_tx_msg(&ci->transp, msg); + return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA); } static void atr_update_csum(uint8_t *atr, unsigned int atr_len) @@ -266,12 +288,9 @@ static int cardem_request_set_atr(struct cardem_inst *ci, const uint8_t *atr, un cur = msgb_put(msg, atr_len); memcpy(cur, atr, atr_len); - st_push_hdr(msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR); - - return st_transp_tx_msg(&ci->transp, msg); + return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR); } - /*********************************************************************** * Incoming Messages ***********************************************************************/ @@ -439,7 +458,7 @@ static const struct option opts[] = { static void run_mainloop(struct cardem_inst *ci) { - struct st_transport *transp = &ci->transp; + struct st_transport *transp = ci->slot->transp; unsigned int msg_count, byte_count = 0; uint8_t buf[16*265]; int xfer_len; @@ -474,7 +493,18 @@ static void run_mainloop(struct cardem_inst *ci) } } -struct cardem_inst _ci, *ci = &_ci; +static struct st_transport _transp; + +static struct st_slot _slot = { + .transp = &_transp, + .slot_nr = 0, +}; + +struct cardem_inst _ci = { + .slot = &_slot, +}; + +struct cardem_inst *ci = &_ci; static void signal_handler(int signal) { @@ -490,7 +520,7 @@ static void signal_handler(int signal) int main(int argc, char **argv) { - struct st_transport *transp = &ci->transp; + struct st_transport *transp = ci->slot->transp; char *gsmtap_host = "127.0.0.1"; int rc; int c, ret = 1; @@ -557,7 +587,6 @@ int main(int argc, char **argv) goto do_exit; } - memset(ci, 0, sizeof(*ci)); transp->udp_fd = -1; ci->card_prof = &osim_uicc_sim_cic_profile;