cardem: use USART timeout for waiting time

the reset/ATR handling has been heavily updated/fixed.
instead of using the timer counter peripheral to handle
the waiting time and corresponding timeout, the USART peripheral
internal timeout mechanism is used.
this is particularly important for the SIMtrace board since the
clock signal is not connected to the timer counter.
thus this change adds card emulation support for SIMtrace boards.

Fi and Di have been properly rename to F and D since the "i"
stands only for an "indicated" value, not the actual value.
this does not change the USB protocol (the variable have just been
renamed).
additional variables store more information about the card
capabilities

NOTE: it has only be tested for the SIMtrace board

Change-Id: Ibcb2c8cace9137695adf5fb3de43566f7cfb93b5
This commit is contained in:
Kévin Redon
2019-06-13 15:41:52 +02:00
parent e3d516745d
commit 76c2eebae2
9 changed files with 296 additions and 119 deletions

View File

@@ -25,7 +25,7 @@ export PKG_CONFIG_PATH="$inst/lib/pkgconfig:$PKG_CONFIG_PATH"
export LD_LIBRARY_PATH="$inst/lib"
BUILDS=""
BUILDS+="simtrace/dfu simtrace/trace "
BUILDS+="simtrace/dfu simtrace/trace simtrace/cardem "
BUILDS+="qmod/dfu qmod/cardem "
BUILDS+="owhw/dfu owhw/cardem "

View File

@@ -1,3 +1,3 @@
C_FILES += $(C_LIBUSB_RT)
C_FILES += card_emu.c iso7816_3.c iso7816_4.c iso7816_fidi.c mode_cardemu.c simtrace_iso7816.c tc_etu.c usb.c
C_FILES += card_emu.c iso7816_3.c iso7816_4.c mode_cardemu.c simtrace_iso7816.c usb.c

View File

@@ -147,7 +147,7 @@
#endif
/* SIMtrace board supports card emulation mode */
#ifdef APPLICATION_cardem
//#define HAVE_CARDEM
#define HAVE_CARDEM
#endif
/* SIMtrace board supports man-in-the-middle mode */
#ifdef APPLICATION_mitm

View File

@@ -48,10 +48,41 @@ struct llist_head *card_emu_get_uart_tx_queue(struct card_handle *ch);
void card_emu_have_new_uart_tx(struct card_handle *ch);
void card_emu_report_status(struct card_handle *ch);
/*! call when the waiting time has half-expired
* param[in] ch card for which the waiting time half expired
*/
void card_emu_wt_halfed(struct card_handle *ch);
/*! call when the waiting time has expired
* param[in] ch card for which the waiting time expired
*/
void card_emu_wt_expired(struct card_handle *ch);
#define ENABLE_TX 0x01
#define ENABLE_RX 0x02
int card_emu_uart_update_fidi(uint8_t uart_chan, unsigned int fidi);
// the following functions are callbacks implement in mode_cardemu.c
/*! update F and D on USART peripheral
* @param[in] usart USART peripheral to configure
* @param[in] f clock rate conversion integer F value
* @param[in] d baud rate adjustment factor D value
* @note this should happen after reset and protocol select (through PPS or implicit)
*/
void card_emu_uart_update_fd(uint8_t uart_chan, uint16_t f, uint8_t d);
/*! update WT on USART peripheral
* @param[in] usart USART peripheral to configure
* @param[in] wt inactivity Waiting Time before card_emu_wt_expired is called (0 to disable)
*/
void card_emu_uart_update_wt(uint8_t uart_chan, uint32_t wt);
/*! reset waiting timeout count down on USART peripheral
* @param[in] usart USART peripheral to configure
*/
void card_emu_uart_reset_wt(uint8_t uart_chan);
/*! set I/O line high
* @param[in] usart USART peripheral to configure
* @param[in] set if I/O line should be set high (true), or cleared low (false)
*/
void card_emu_uart_io_set(uint8_t uart_chan, bool set);
int card_emu_uart_tx(uint8_t uart_chan, uint8_t byte);
void card_emu_uart_enable(uint8_t uart_chan, uint8_t rxtx);
void card_emu_uart_wait_tx_idle(uint8_t uart_chan);

View File

@@ -228,11 +228,10 @@ struct cardemu_usb_msg_status {
uint32_t flags;
/* phone-applied target voltage in mV */
uint16_t voltage_mv;
/* Fi/Di related information */
uint8_t fi;
uint8_t di;
uint8_t wi;
uint32_t waiting_time;
uint8_t f; /*!< index of F and f_max values as encoded in ISO/IEC 7816-3:2006(E) Table 7 */
uint8_t d; /*!< index of D value as encoded in ISO/IEC 7816-3:2006(E) Table 8 */
uint8_t wi; /*!< Waiting Integer as defined in ISO/IEC 7816-3:2006(E) Section 10.2 */
uint32_t wt; /*!< Waiting Time in ETU as defined in ISO/IEC 7816-3:2006(E) Section 8.1 */
} __attribute__ ((packed));
/* CEMU_USB_MSGT_DO_PTS */

View File

@@ -1,7 +1,7 @@
/* ISO7816-3 state machine for the card side
*
* (C) 2010-2017 by Harald Welte <laforge@gnumonks.org>
* (C) 2018 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
* (C) 2018-2019 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -26,15 +26,13 @@
#include "utils.h"
#include "trace.h"
#include "iso7816_fidi.h"
#include "tc_etu.h"
#include "iso7816_3.h"
#include "card_emu.h"
#include "simtrace_prot.h"
#include "usb_buf.h"
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/msgb.h>
#define NUM_SLOTS 2
#define ISO7816_3_INIT_WTIME 9600
@@ -186,18 +184,53 @@ struct card_handle {
uint8_t in_reset; /* 1 = RST low, 0 = RST high */
uint8_t clocked; /* 1 = active, 0 = inactive */
/* timing parameters, from PTS */
uint8_t fi;
uint8_t di;
uint8_t wi;
uint8_t tc_chan; /* TC channel number */
uint8_t uart_chan; /* UART channel */
uint8_t in_ep; /* USB IN EP */
uint8_t irq_ep; /* USB IN EP */
uint32_t waiting_time; /* in clocks */
/*! clock rate conversion integer F
* @implements ISO/IEC 7816-3:2006(E) section 7.1
* @note this represents the current value used
*/
uint16_t f;
/*! baud rate adjustment factor D
* @implements ISO/IEC 7816-3:2006(E) section 7.1
* @note this represents the current value used
*/
uint8_t d;
/*! clock frequency in Hz
* @implements ISO/IEC 7816-3:2006(E) section 7.1
* @note the USART peripheral in slave mode does not provide the current value. we could measure it but this is not really useful. instead we remember the maximum possible value corresponding to the selected F value
*/
uint32_t f_cur;
/*! clock rate conversion integer Fi
* @implements ISO/IEC 7816-3:2006(E) Table 7
* @note this represents the maximum value supported by the card, and can be indicated in TA1
* @note this value can be set in TA1
*/
uint16_t fi;
/*! baud rate adjustment factor Di
* @implements ISO/IEC 7816-3:2006(E) Table 8
* @note this represents the maximum value supported by the card, and can be indicated in TA1
*/
uint8_t di;
/*! clock frequency, in Hz
* @implements ISO/IEC 7816-3:2006(E) Table 7
* @note this represents the maximum value supported by the card, and can be indicated in TA1
*/
uint32_t f_max;
/*! Waiting Integer
* @implements ISO/IEC 7816-3:2006(E) Section 10.2
* @note this value can be set in TA2
*/
uint8_t wi;
/*! Waiting Time, in ETU
* @implements ISO/IEC 7816-3:2006(E) Section 8.1
* @note this depends on Fi, Di, and WI if T=0 is used
*/
uint32_t wt;
/* ATR state machine */
struct {
@@ -345,23 +378,6 @@ static void flush_pts(struct card_handle *ch)
usb_buf_upd_len_and_submit(msg);
}
static void emu_update_fidi(struct card_handle *ch)
{
int rc;
rc = compute_fidi_ratio(ch->fi, ch->di);
if (rc > 0 && rc < 0x400) {
TRACE_INFO("%u: computed Fi(%u) Di(%u) ratio: %d\r\n",
ch->num, ch->fi, ch->di, rc);
/* make sure UART uses new F/D ratio */
card_emu_uart_update_fidi(ch->uart_chan, rc);
/* notify ETU timer about this */
tc_etu_set_etu(ch->tc_chan, rc);
} else
TRACE_INFO("%u: computed FiDi ration %d unsupported\r\n",
ch->num, rc);
}
/* Update the ISO 7816-3 TPDU receiver state */
static void card_set_state(struct card_handle *ch,
enum iso7816_3_card_state new_state)
@@ -378,31 +394,38 @@ static void card_set_state(struct card_handle *ch,
case ISO_S_WAIT_POWER:
case ISO_S_WAIT_CLK:
case ISO_S_WAIT_RST:
/* disable Rx and Tx of UART */
card_emu_uart_enable(ch->uart_chan, 0);
card_emu_uart_enable(ch->uart_chan, 0); // disable Rx and Tx of UART
card_emu_uart_update_wt(ch->uart_chan, 0); // disable timeout
if (ISO_S_WAIT_POWER == new_state) {
card_emu_uart_io_set(ch->uart_chan, false); // pull I/O line low
} else {
card_emu_uart_io_set(ch->uart_chan, true); // pull I/O line high
}
break;
case ISO_S_WAIT_ATR:
/* Reset to initial Fi / Di ratio */
ch->fi = 1;
ch->di = 1;
emu_update_fidi(ch);
// reset the ETU-related values
ch->f = ISO7816_3_DEFAULT_FD;
ch->d = ISO7816_3_DEFAULT_DD;
card_emu_uart_update_fd(ch->uart_chan, ch->f, ch->d); // set baud rate
// reset values optionally specified in the ATR
ch->fi = ISO7816_3_DEFAULT_FI;
ch->di = ISO7816_3_DEFAULT_DI;
ch->wi = ISO7816_3_DEFAULT_WI;
int32_t wt = iso7816_3_calculate_wt(ch->wi, ch->fi, ch->di, ch->f, ch->d); // get default waiting time
if (wt <= 0) {
TRACE_FATAL("%u: invalid WT %ld\r\n", ch->num, wt);
}
ch->wt = wt;
card_emu_uart_enable(ch->uart_chan, ENABLE_TX); // enable TX to be able to use the timeout
/* the ATR should only be sent 400 to 40k clock cycles after the RESET.
* we use the tc_etu mechanism to wait this time.
* since the initial ETU is Fd=372/Dd=1 clock cycles long, we have to wait 2-107 ETU.
*/
tc_etu_set_wtime(ch->tc_chan, 2);
/* ensure the TC_ETU timer is enabled */
tc_etu_enable(ch->tc_chan);
card_emu_uart_update_wt(ch->uart_chan, 2);
break;
case ISO_S_IN_ATR:
/* initialize to default WI, this will be overwritten if we
* send TC2, and it will be programmed into hardware after
* ATR is finished */
ch->wi = ISO7816_3_DEFAULT_WI;
/* update waiting time to initial waiting time */
ch->waiting_time = ISO7816_3_INIT_WTIME;
/* set initial waiting time */
tc_etu_set_wtime(ch->tc_chan, ch->waiting_time);
// FIXME disable timeout while sending ATR
/* Set ATR sub-state to initial state */
ch->atr.idx = 0;
/* enable USART transmission to reader */
@@ -448,7 +471,6 @@ static int tx_byte_atr(struct card_handle *ch)
return 1;
} else { /* The ATR has been completely transmitted */
/* search for TC2 to updated WI */
ch->wi = ISO7816_3_DEFAULT_WI;
if (ch->atr.len >= 2 && ch->atr.atr[1] & 0xf0) { /* Y1 has some data */
uint8_t atr_td1 = 2;
if (ch->atr.atr[1] & 0x10) { /* TA1 is present */
@@ -477,9 +499,7 @@ static int tx_byte_atr(struct card_handle *ch)
}
}
}
/* update waiting time (see ISO 7816-3 10.2) */
ch->waiting_time = ch->wi * 960 * ch->fi;
tc_etu_set_wtime(ch->tc_chan, ch->waiting_time);
/* FIXME update waiting time in case of card is specific mode */
/* reset PTS to initial state */
set_pts_state(ch, PTS_S_WAIT_REQ_PTSS);
/* go to next state */
@@ -546,9 +566,12 @@ from_pts3:
return PTS_S_WAIT_REQ_PCK | is_resp;
}
static int
process_byte_pts(struct card_handle *ch, uint8_t byte)
/*! process incoming PTS byte
* @param[in] ch card handle on which the byte has been received
* @param[in] byte received PTS byte
* @return new iso7816_3_card_state or -1 at the end of PTS request
*/
static int process_byte_pts(struct card_handle *ch, uint8_t byte)
{
switch (ch->pts.state) {
case PTS_S_WAIT_REQ_PTSS:
@@ -575,7 +598,7 @@ process_byte_pts(struct card_handle *ch, uint8_t byte)
set_pts_state(ch, PTS_S_WAIT_REQ_PTSS);
return ISO_S_WAIT_TPDU;
}
/* FIXME: check if proposal matches capabilities in ATR */
/* FIXME check if proposal matches capabilities in TA1 */
memcpy(ch->pts.resp, ch->pts.req, sizeof(ch->pts.resp));
break;
default:
@@ -614,11 +637,17 @@ static int tx_byte_pts(struct card_handle *ch)
break;
case PTS_S_WAIT_RESP_PTS1:
byte = ch->pts.resp[_PTS1];
/* This must be TA1 */
ch->fi = byte >> 4;
ch->di = byte & 0xf;
TRACE_DEBUG("%u: found Fi=%u Di=%u\r\n", ch->num,
ch->fi, ch->di);
// TODO the value should have been validated when receiving the request
ch->f = iso7816_3_fi_table[byte >> 4]; // save selected Fn
if (0 == ch->f) {
TRACE_ERROR("%u: invalid F index in PPS response: %u\r\n", ch->num, byte >> 4);
// TODO become unresponsive to signal error condition
}
ch->d = iso7816_3_di_table[byte & 0xf]; // save selected Dn
if (0 == ch->d) {
TRACE_ERROR("%u: invalid D index in PPS response: %u\r\n", ch->num, byte & 0xf);
// TODO become unresponsive to signal error condition
}
break;
case PTS_S_WAIT_RESP_PTS2:
byte = ch->pts.resp[_PTS2];
@@ -643,8 +672,15 @@ static int tx_byte_pts(struct card_handle *ch)
switch (ch->pts.state) {
case PTS_S_WAIT_RESP_PCK:
card_emu_uart_wait_tx_idle(ch->uart_chan);
/* update baud rate generator with Fi/Di */
emu_update_fidi(ch);
card_emu_uart_update_fd(ch->uart_chan, ch->f, ch->d); // set selected baud rate
int32_t wt = iso7816_3_calculate_wt(ch->wi, ch->fi, ch->di, ch->f, ch->d); // get new waiting time
if (wt <= 0) {
TRACE_ERROR("%u: invalid WT calculated: %ld\r\n", ch->num, wt);
// TODO become unresponsive to signal error condition
} else {
ch->wt = wt;
}
// FIXME disable WT
/* Wait for the next TPDU */
card_set_state(ch, ISO_S_WAIT_TPDU);
set_pts_state(ch, PTS_S_WAIT_REQ_PTSS);
@@ -715,6 +751,10 @@ static void set_tpdu_state(struct card_handle *ch, enum tpdu_state new_ts)
{
if (ch->tpdu.state == new_ts)
return;
if (ISO_S_IN_TPDU != ch->state) {
TRACE_ERROR("%u: setting TPDU state in %s state\r\n", ch->num,
get_value_string(iso7816_3_card_state_names, ch->state));
}
TRACE_DEBUG("%u: 7816 TPDU state %s -> %s\r\n", ch->num,
get_value_string(tpdu_state_names, ch->tpdu.state),
@@ -722,15 +762,20 @@ static void set_tpdu_state(struct card_handle *ch, enum tpdu_state new_ts)
ch->tpdu.state = new_ts;
switch (new_ts) {
case TPDU_S_WAIT_CLA:
case TPDU_S_WAIT_RX:
card_emu_uart_enable(ch->uart_chan, ENABLE_RX);
case TPDU_S_WAIT_CLA: // we will be waiting for the next incoming TDPU
card_emu_uart_enable(ch->uart_chan, ENABLE_RX); // switch back to receiving mode
card_emu_uart_update_wt(ch->uart_chan, 0); // disable waiting time since we don't expect any data
break;
case TPDU_S_WAIT_INS: // the reader started sending the TPDU header
card_emu_uart_update_wt(ch->uart_chan, ch->wt); // start waiting for the rest of the header/body
break;
case TPDU_S_WAIT_RX: // the reader should send us the TPDU body data
card_emu_uart_enable(ch->uart_chan, ENABLE_RX); // switch to receive mode to receive the body
card_emu_uart_update_wt(ch->uart_chan, ch->wt); // start waiting for the rest body
break;
case TPDU_S_WAIT_PB:
/* we just completed the TPDU header from reader to card
* and now need to disable the receiver, enable the
* transmitter and transmit the procedure byte */
card_emu_uart_enable(ch->uart_chan, ENABLE_TX);
card_emu_uart_enable(ch->uart_chan, ENABLE_TX); // header is completely received, now we need to transmit the procedure byte
card_emu_uart_update_wt(ch->uart_chan, ch->wt); // prepare to extend the waiting time once half of it is reached
break;
default:
break;
@@ -1009,11 +1054,11 @@ void card_emu_report_status(struct card_handle *ch)
sts->flags |= CEMU_STATUS_F_CLK_ACTIVE;
if (ch->in_reset)
sts->flags |= CEMU_STATUS_F_RESET_ACTIVE;
/* FIXME: voltage + card insert */
sts->fi = ch->fi;
sts->di = ch->di;
/* FIXME set voltage and card insert values */
sts->f = ch->f;
sts->d = ch->d;
sts->wi = ch->wi;
sts->waiting_time = ch->waiting_time;
sts->wt = ch->wt;
usb_buf_upd_len_and_submit(msg);
}
@@ -1025,7 +1070,6 @@ void card_emu_io_statechg(struct card_handle *ch, enum card_io io, int active)
case CARD_IO_VCC:
if (active == 0 && ch->vcc_active == 1) {
TRACE_INFO("%u: VCC deactivated\r\n", ch->num);
tc_etu_disable(ch->tc_chan);
card_set_state(ch, ISO_S_WAIT_POWER);
} else if (active == 1 && ch->vcc_active == 0) {
TRACE_INFO("%u: VCC activated\r\n", ch->num);
@@ -1046,18 +1090,18 @@ void card_emu_io_statechg(struct card_handle *ch, enum card_io io, int active)
case CARD_IO_RST:
if (active == 0 && ch->in_reset) {
TRACE_INFO("%u: RST released\r\n", ch->num);
if (ch->vcc_active && ch->clocked) {
/* enable the TC/ETU counter once reset has been released */
tc_etu_enable(ch->tc_chan);
if (ch->vcc_active && ch->clocked && ISO_S_WAIT_RST == ch->state) {
/* prepare to send the ATR */
card_set_state(ch, ISO_S_WAIT_ATR);
}
} else if (active && !ch->in_reset) {
TRACE_INFO("%u: RST asserted\r\n", ch->num);
tc_etu_disable(ch->tc_chan);
card_set_state(ch, ISO_S_WAIT_RST);
}
ch->in_reset = active;
break;
default:
break;
}
}
@@ -1084,38 +1128,34 @@ int card_emu_set_atr(struct card_handle *ch, const uint8_t *atr, uint8_t len)
return 0;
}
/* hardware driver informs us that one (more) ETU has expired */
void tc_etu_wtime_half_expired(void *handle)
void card_emu_wt_halfed(struct card_handle *ch)
{
struct card_handle *ch = handle;
/* transmit NULL procedure byte well before waiting time expires */
switch (ch->state) {
case ISO_S_IN_TPDU:
switch (ch->tpdu.state) {
case TPDU_S_WAIT_PB:
case TPDU_S_WAIT_TX:
case TPDU_S_WAIT_PB:
putchar('N');
card_emu_uart_tx(ch->uart_chan, ISO7816_3_PB_NULL);
card_emu_uart_tx(ch->uart_chan, ISO7816_3_PB_NULL); // we are waiting for data from the user. send a procedure byte to ask the reader to wait more time
card_emu_uart_reset_wt(ch->uart_chan); // reset WT
break;
default:
break;
}
break;
default:
break;
}
}
/* hardware driver informs us that one (more) ETU has expired */
void tc_etu_wtime_expired(void *handle)
void card_emu_wt_expired(struct card_handle *ch)
{
struct card_handle *ch = handle;
switch (ch->state) {
case ISO_S_WAIT_ATR:
/* ISO 7816-3 6.2.1 time tc has passed, we can now send the ATR */
card_set_state(ch, ISO_S_IN_ATR);
break;
default:
// TODO become unresponsive
TRACE_ERROR("%u: wtime_exp\r\n", ch->num);
break;
}
@@ -1149,13 +1189,13 @@ struct card_handle *card_emu_init(uint8_t slot_num, uint8_t tc_chan, uint8_t uar
ch->in_reset = 1;
ch->clocked = 0;
ch->fi = 0;
ch->di = 1;
ch->fi = ISO7816_3_DEFAULT_FI;
ch->di = ISO7816_3_DEFAULT_DI;
ch->wi = ISO7816_3_DEFAULT_WI;
ch->wt = ISO7816_3_DEFAULT_WT;;
ch->tc_chan = tc_chan;
ch->uart_chan = uart_chan;
ch->waiting_time = ISO7816_3_INIT_WTIME;
ch->atr.idx = 0;
ch->atr.len = sizeof(default_atr);
@@ -1164,7 +1204,5 @@ struct card_handle *card_emu_init(uint8_t slot_num, uint8_t tc_chan, uint8_t uar
ch->pts.state = PTS_S_WAIT_REQ_PTSS;
ch->tpdu.state = TPDU_S_WAIT_CLA;
tc_etu_init(ch->tc_chan, ch);
return ch;
}

View File

@@ -22,7 +22,7 @@
#include "simtrace.h"
#include "ringbuffer.h"
#include "card_emu.h"
#include "iso7816_fidi.h"
#include "iso7816_3.h"
#include "utils.h"
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/msgb.h>
@@ -54,11 +54,15 @@ struct cardem_inst {
struct card_handle *ch;
struct llist_head usb_out_queue;
struct ringbuf rb;
uint32_t wt; /*!< receiver waiting time to trigger timeout (0 to deactivate it) */
uint32_t wt_remaining; /*!< remaining waiting time */
bool wt_halfed; /*!< if at least half of the waiting time passed */
struct Usart_info usart_info;
int usb_pending_old;
uint8_t ep_out;
uint8_t ep_in;
uint8_t ep_int;
const Pin pin_io;
const Pin pin_insert;
uint32_t vcc_uv;
uint32_t vcc_uv_last;
@@ -75,6 +79,7 @@ struct cardem_inst cardem_inst[] = {
.ep_out = SIMTRACE_CARDEM_USB_EP_USIM1_DATAOUT,
.ep_in = SIMTRACE_CARDEM_USB_EP_USIM1_DATAIN,
.ep_int = SIMTRACE_CARDEM_USB_EP_USIM1_INT,
.pin_io = PIN_USIM1_IO,
#ifdef PIN_SET_USIM1_PRES
.pin_insert = PIN_SET_USIM1_PRES,
#endif
@@ -90,6 +95,7 @@ struct cardem_inst cardem_inst[] = {
.ep_out = SIMTRACE_CARDEM_USB_EP_USIM2_DATAOUT,
.ep_in = SIMTRACE_CARDEM_USB_EP_USIM2_DATAIN,
.ep_int = SIMTRACE_CARDEM_USB_EP_USIM2_INT,
.pin_io = PIN_USIM2_IO,
#ifdef PIN_SET_USIM2_PRES
.pin_insert = PIN_SET_USIM2_PRES,
#endif
@@ -140,7 +146,7 @@ void card_emu_uart_enable(uint8_t uart_chan, uint8_t rxtx)
* receiver enabled during transmit */
USART_SetReceiverEnabled(usart, 1);
usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
USART_EnableIt(usart, US_IER_TXRDY);
USART_EnableIt(usart, US_IER_TXRDY | US_IER_TIMEOUT);
USART_SetTransmitterEnabled(usart, 1);
break;
case ENABLE_RX:
@@ -150,7 +156,7 @@ void card_emu_uart_enable(uint8_t uart_chan, uint8_t rxtx)
USART_SetTransmitterEnabled(usart, 1);
wait_tx_idle(usart);
usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
USART_EnableIt(usart, US_IER_RXRDY);
USART_EnableIt(usart, US_IER_RXRDY | US_IER_TIMEOUT);
USART_SetReceiverEnabled(usart, 1);
break;
case 0:
@@ -191,7 +197,10 @@ int card_emu_uart_tx(uint8_t uart_chan, uint8_t byte)
/* FIXME: integrate this with actual irq handler */
static void usart_irq_rx(uint8_t inst_num)
{
OSMO_ASSERT(inst_num < ARRAY_SIZE(cardem_inst));
if (inst_num >= ARRAY_SIZE(cardem_inst)) {
TRACE_ERROR("%u: UART channel out of bounds\r\n", inst_num);
return;
}
Usart *usart = get_usart_by_chan(inst_num);
struct cardem_inst *ci = &cardem_inst[inst_num];
uint32_t csr;
@@ -210,10 +219,32 @@ static void usart_irq_rx(uint8_t inst_num)
USART_DisableIt(usart, US_IER_TXRDY); // stop the TX ready signal if not byte has been transmitted
}
if (csr & (US_CSR_OVRE|US_CSR_FRAME|US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK|(1<<10))) { // error flag set
if (csr & (US_CSR_OVRE | US_CSR_FRAME | US_CSR_PARE | US_CSR_NACK | (1 << 10))) { // error flag set
usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK; // reset UART state to clear flag
TRACE_ERROR("%u USART error on 0x%x status: 0x%lx\n", ci->num, byte, csr); // warn user about error
}
// handle timeout
if (csr & US_CSR_TIMEOUT) { // RX has been inactive for some time
if (ci->wt_remaining <= (usart->US_RTOR & 0xffff)) { // waiting time has passed
ci->wt_remaining = 0; // timeout reached (will stop the timer)
} else {
ci->wt_remaining -= (usart->US_RTOR & 0xffff); // be sure to subtract the actual timeout since the new might not have been set and reloaded yet
}
if (0 == ci->wt_remaining) {
card_emu_wt_expired(ci->ch); // let the state know WT has expired
} else if (ci->wt_remaining <= ci->wt / 2 && !ci->wt_halfed) {
ci->wt_halfed = true;
card_emu_wt_halfed(ci->ch); // let the state know WT has half expired
}
if (ci->wt_remaining > 0xffff) { // value exceeds the USART TO range
usart->US_RTOR = 0xffff; // use the MAX
} else {
usart->US_RTOR = ci->wt_remaining;
}
usart->US_CR |= US_CR_STTTO; // clear timeout flag (and stop timeout until next character is received)
usart->US_CR |= US_CR_RETTO; // restart the counter (it wt is 0, the timeout is not started)
}
}
/*! ISR called for USART0 */
@@ -230,16 +261,90 @@ void mode_cardemu_usart1_irq(void)
usart_irq_rx(0);
}
/* call-back from card_emu.c to change UART baud rate */
int card_emu_uart_update_fidi(uint8_t uart_chan, unsigned int fidi)
{
int rc;
Usart *usart = get_usart_by_chan(uart_chan);
// call-back from card_emu.c to change UART baud rate
usart->US_CR |= US_CR_RXDIS | US_CR_RSTRX;
usart->US_FIDI = fidi & 0x3ff;
usart->US_CR |= US_CR_RXEN | US_CR_STTTO;
return 0;
void card_emu_uart_update_fd(uint8_t uart_chan, uint16_t f, uint8_t d)
{
Usart *usart = get_usart_by_chan(uart_chan); // get the USART based on the card handle
if (NULL == usart) {
TRACE_ERROR("%u: USART not found by chan\r\n", uart_chan);
return;
}
if (!iso7816_3_valid_f(f)) {
TRACE_ERROR("%u: invalid F: %u\r\n", uart_chan, f);
return;
}
if (!iso7816_3_valid_d(d)) {
TRACE_ERROR("%u: invalid D: %u\r\n", uart_chan, d);
return;
}
uint16_t ratio = f / d;
if (ratio > 0 && ratio < 2048) {
/* make sure USART uses new F/D ratio */
usart->US_CR |= US_CR_RXDIS | US_CR_RSTRX; // disable USART before changing baud rate
usart->US_FIDI = (ratio & 0x7ff); // change baud rate (ratio)
usart->US_CR |= US_CR_RXEN | US_CR_STTTO; // re-enable USART (and stop timeout)
TRACE_INFO("%u: USART F/D set to %u/%u\r\n", uart_chan, f, d);
} else {
TRACE_ERROR("%u: USART could not set F/D to %u/%u\r\n", uart_chan, f, d);
// TODO become unresponsive
}
}
void card_emu_uart_update_wt(uint8_t uart_chan, uint32_t wt)
{
if (uart_chan >= ARRAY_SIZE(cardem_inst)) {
TRACE_ERROR("%u: UART channel out of bounds\r\n", uart_chan);
return;
}
struct cardem_inst *ci = &cardem_inst[uart_chan];
Usart *usart = get_usart_by_chan(uart_chan); // get the USART based on the card handle
if (NULL == usart) {
TRACE_ERROR("%u: USART not found by chan\r\n", uart_chan);
return;
}
ci->wt = wt; // save value
card_emu_uart_reset_wt(uart_chan); // reset and start timer
TRACE_INFO("%u: USART WT set to %lu ETU\r\n", uart_chan, wt);
}
void card_emu_uart_reset_wt(uint8_t uart_chan)
{
if (uart_chan >= ARRAY_SIZE(cardem_inst)) {
TRACE_ERROR("%u: UART channel out of bounds\r\n", uart_chan);
return;
}
struct cardem_inst *ci = &cardem_inst[uart_chan];
Usart *usart = get_usart_by_chan(uart_chan); // get the USART based on the card handle
if (NULL == usart) {
TRACE_ERROR("%u: USART not found by chan\r\n", uart_chan);
return;
}
ci->wt_remaining = ci->wt; // reload WT value
ci->wt_halfed = false; // reset half expired
if (ci->wt_remaining > 0xffff) { // value exceeds the USART TO range
usart->US_RTOR = 0xffff; // use the MAX
} else {
usart->US_RTOR = ci->wt_remaining;
}
usart->US_CR |= US_CR_RETTO; // restart the counter (if wt is 0, the timeout is not started)
}
void card_emu_uart_io_set(uint8_t uart_chan, bool set)
{
if (uart_chan >= ARRAY_SIZE(cardem_inst)) {
TRACE_ERROR("%u: UART channel out of bounds\r\n", uart_chan);
return;
}
struct cardem_inst *ci = &cardem_inst[uart_chan];
if (set) {
PIO_Set(&ci->pin_io);
} else {
PIO_Clear(&ci->pin_io);
}
}
/* call-back from card_emu.c to force a USART interrupt */
@@ -425,6 +530,10 @@ void mode_cardemu_init(void)
#endif /* DETECT_VCC_BY_ADC */
cardem_inst[0].ch = card_emu_init(0, 2, 0, SIMTRACE_CARDEM_USB_EP_USIM1_DATAIN, SIMTRACE_CARDEM_USB_EP_USIM1_INT);
sim_switch_use_physical(0, 1);
#ifndef DETECT_VCC_BY_ADC
usim1_vcc_irqhandler(NULL); // check VCC/CLK state
#endif
usim1_rst_irqhandler(NULL); // force RST state
#ifdef CARDEMU_SECOND_UART
INIT_LLIST_HEAD(&cardem_inst[1].usb_out_queue);

View File

@@ -36,7 +36,7 @@ const char *get_value_string_or_null(const struct value_string *vs,
int get_string_value(const struct value_string *vs, const char *str);
char osmo_bcd2char(uint8_t bcd);
/* only works for numbers in ascci */
/* only works for numbers in ASCII */
uint8_t osmo_char2bcd(char c);
int osmo_hexparse(const char *str, uint8_t *b, int max_len);
@@ -60,7 +60,7 @@ do { \
rem -= ret; \
} while (0)
/*! Helper macro to terminate when an assertion failes
/*! Helper macro to terminate when an assertion fails
* \param[in] exp Predicate to verify
* This function will generate a backtrace and terminate the program if
* the predicate evaluates to false (0).
@@ -75,7 +75,7 @@ do { \
/*! duplicate a string using talloc and release its prior content (if any)
* \param[in] ctx Talloc context to use for allocation
* \param[out] dst pointer to string, will be updated with ptr to new string
* \param[in] newstr String that will be copieed to newly allocated string */
* \param[in] newstr String that will be copied to newly allocated string */
static inline void osmo_talloc_replace_string(void *ctx, char **dst, const char *newstr)
{
if (*dst)

View File

@@ -367,9 +367,9 @@ static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len)
struct cardemu_usb_msg_status *status;
status = (struct cardemu_usb_msg_status *) buf;
printf("=> STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
status->flags, status->fi, status->di, status->wi,
status->waiting_time);
printf("=> STATUS: flags=0x%x, F=%u, D=%u, WI=%u WT=%u\n",
status->flags, status->f, status->d, status->wi,
status->wt);
return 0;
}