2015-02-24 10:35:19 +00:00
|
|
|
// FIXME: Copyright license here
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* Headers
|
|
|
|
*------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#include "board.h"
|
2016-03-03 10:02:45 +00:00
|
|
|
#include "simtrace.h"
|
2016-02-29 16:57:51 +00:00
|
|
|
#include "utils.h"
|
|
|
|
#include "req_ctx.h"
|
2017-02-04 11:15:58 +00:00
|
|
|
#include "osmocom/core/timer.h"
|
2015-02-24 10:35:19 +00:00
|
|
|
|
2017-02-27 19:34:20 +00:00
|
|
|
unsigned int g_unique_id[4];
|
2016-03-20 15:00:39 +00:00
|
|
|
|
2015-02-27 12:39:45 +00:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* Internal variables
|
|
|
|
*------------------------------------------------------------------------------*/
|
|
|
|
typedef struct {
|
2016-03-03 11:32:04 +00:00
|
|
|
/* static initialization, called whether or not the usb config is active */
|
|
|
|
void (*configure) (void);
|
|
|
|
/* initialization function after the config was selected */
|
|
|
|
void (*init) (void);
|
|
|
|
/* de-initialization before selecting new config */
|
|
|
|
void (*exit) (void);
|
|
|
|
/* main loop content for given configuration */
|
|
|
|
void (*run) (void);
|
2016-03-20 15:43:12 +00:00
|
|
|
/* Interrupt handler for USART1 */
|
|
|
|
void (*usart0_irq) (void);
|
|
|
|
/* Interrupt handler for USART1 */
|
|
|
|
void (*usart1_irq) (void);
|
2015-02-27 12:39:45 +00:00
|
|
|
} conf_func;
|
|
|
|
|
2015-11-07 17:25:46 +00:00
|
|
|
static const conf_func config_func_ptrs[] = {
|
2015-11-07 17:19:11 +00:00
|
|
|
/* array slot 0 is empty, usb configs start at 1 */
|
2016-02-28 11:34:26 +00:00
|
|
|
#ifdef HAVE_SNIFFER
|
2015-11-07 17:19:11 +00:00
|
|
|
[CFG_NUM_SNIFF] = {
|
|
|
|
.configure = Sniffer_configure,
|
|
|
|
.init = Sniffer_init,
|
|
|
|
.exit = Sniffer_exit,
|
|
|
|
.run = Sniffer_run,
|
|
|
|
},
|
2016-02-28 11:34:26 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_CCID
|
2015-11-07 17:19:11 +00:00
|
|
|
[CFG_NUM_CCID] = {
|
|
|
|
.configure = CCID_configure,
|
|
|
|
.init = CCID_init,
|
|
|
|
.exit = CCID_exit,
|
|
|
|
.run = CCID_run,
|
|
|
|
},
|
2016-02-28 11:34:26 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_CARDEM
|
2015-11-07 17:19:11 +00:00
|
|
|
[CFG_NUM_PHONE] = {
|
2016-02-28 18:29:14 +00:00
|
|
|
.configure = mode_cardemu_configure,
|
|
|
|
.init = mode_cardemu_init,
|
|
|
|
.exit = mode_cardemu_exit,
|
|
|
|
.run = mode_cardemu_run,
|
2016-03-20 15:43:12 +00:00
|
|
|
.usart0_irq = mode_cardemu_usart0_irq,
|
|
|
|
.usart1_irq = mode_cardemu_usart1_irq,
|
2015-11-07 17:19:11 +00:00
|
|
|
},
|
2016-02-28 11:34:26 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_MITM
|
2015-11-07 17:19:11 +00:00
|
|
|
[CFG_NUM_MITM] = {
|
|
|
|
.configure = MITM_configure,
|
|
|
|
.init = MITM_init,
|
|
|
|
.exit = MITM_exit,
|
2016-03-03 11:32:04 +00:00
|
|
|
.run = MITM_run,
|
2015-11-07 17:19:11 +00:00
|
|
|
},
|
2016-02-28 11:34:26 +00:00
|
|
|
#endif
|
2015-02-27 12:39:45 +00:00
|
|
|
};
|
|
|
|
|
2015-02-24 10:35:19 +00:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* Internal variables
|
|
|
|
*------------------------------------------------------------------------------*/
|
2016-02-28 11:34:26 +00:00
|
|
|
#if defined(HAVE_SNIFFER)
|
2015-11-07 17:27:05 +00:00
|
|
|
static volatile enum confNum simtrace_config = CFG_NUM_SNIFF;
|
2016-02-28 11:34:26 +00:00
|
|
|
#elif defined(HAVE_CARDEM)
|
|
|
|
static volatile enum confNum simtrace_config = CFG_NUM_PHONE;
|
|
|
|
#elif defined(HAVE_CCID)
|
|
|
|
static volatile enum confNum simtrace_config = CFG_NUM_CCID;
|
|
|
|
#endif
|
|
|
|
|
2015-11-07 17:27:05 +00:00
|
|
|
/*----------------------------------------------------------------------------
|
|
|
|
* Callbacks
|
|
|
|
*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum)
|
|
|
|
{
|
2016-03-03 11:32:04 +00:00
|
|
|
TRACE_INFO_WP("cfgChanged%d ", cfgnum);
|
|
|
|
simtrace_config = cfgnum;
|
2015-11-07 17:27:05 +00:00
|
|
|
}
|
2015-02-24 10:35:19 +00:00
|
|
|
|
2016-03-20 15:43:12 +00:00
|
|
|
void USART1_IrqHandler(void)
|
|
|
|
{
|
|
|
|
config_func_ptrs[simtrace_config].usart1_irq();
|
|
|
|
}
|
|
|
|
|
|
|
|
void USART0_IrqHandler(void)
|
|
|
|
{
|
|
|
|
config_func_ptrs[simtrace_config].usart0_irq();
|
|
|
|
}
|
|
|
|
|
2017-01-11 21:22:16 +00:00
|
|
|
/* returns '1' in case we should break any endless loop */
|
2017-01-12 17:59:41 +00:00
|
|
|
static void check_exec_dbg_cmd(void)
|
2016-12-22 20:29:10 +00:00
|
|
|
{
|
2017-02-10 18:21:10 +00:00
|
|
|
int ch;
|
2016-12-22 20:29:10 +00:00
|
|
|
|
|
|
|
if (!UART_IsRxReady())
|
|
|
|
return;
|
|
|
|
|
2017-02-10 18:21:10 +00:00
|
|
|
ch = UART_GetChar();
|
|
|
|
|
|
|
|
board_exec_dbg_cmd(ch);
|
2016-12-22 20:29:10 +00:00
|
|
|
}
|
2016-10-19 17:36:07 +00:00
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* Main
|
|
|
|
*------------------------------------------------------------------------------*/
|
|
|
|
#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
|
|
|
|
extern int main(void)
|
|
|
|
{
|
|
|
|
uint8_t isUsbConnected = 0;
|
|
|
|
enum confNum last_simtrace_config = simtrace_config;
|
|
|
|
unsigned int i = 0;
|
|
|
|
|
|
|
|
LED_Configure(LED_NUM_RED);
|
|
|
|
LED_Configure(LED_NUM_GREEN);
|
|
|
|
LED_Set(LED_NUM_RED);
|
|
|
|
|
|
|
|
/* Disable watchdog */
|
|
|
|
WDT_Disable(WDT);
|
|
|
|
|
|
|
|
req_ctx_init();
|
|
|
|
|
|
|
|
PIO_InitializeInterrupts(0);
|
|
|
|
|
|
|
|
EEFC_ReadUniqueID(g_unique_id);
|
|
|
|
|
2017-03-03 00:33:24 +00:00
|
|
|
printf("\n\r\n\r"
|
|
|
|
"=============================================================================\n\r"
|
|
|
|
"SIMtrace2 firmware " GIT_VERSION " (C) 2010-2016 by Harald Welte\n\r"
|
|
|
|
"=============================================================================\n\r");
|
2016-10-19 17:36:07 +00:00
|
|
|
|
2017-03-03 00:33:24 +00:00
|
|
|
TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\n\r",
|
2016-10-19 17:36:07 +00:00
|
|
|
g_unique_id[0], g_unique_id[1],
|
|
|
|
g_unique_id[2], g_unique_id[3]);
|
|
|
|
|
2017-02-10 18:21:10 +00:00
|
|
|
board_main_top();
|
2017-02-04 13:43:41 +00:00
|
|
|
|
2017-03-03 00:33:24 +00:00
|
|
|
TRACE_INFO("USB init...\n\r");
|
2016-12-22 21:32:15 +00:00
|
|
|
SIMtrace_USB_Initialize();
|
|
|
|
|
2016-03-03 11:32:04 +00:00
|
|
|
while (USBD_GetState() < USBD_STATE_CONFIGURED) {
|
2016-12-22 20:29:10 +00:00
|
|
|
check_exec_dbg_cmd();
|
2016-09-01 18:57:56 +00:00
|
|
|
#if 0
|
2016-03-03 11:32:04 +00:00
|
|
|
if (i >= MAX_USB_ITER * 3) {
|
|
|
|
TRACE_ERROR("Resetting board (USB could "
|
2017-03-03 00:33:24 +00:00
|
|
|
"not be configured)\n\r");
|
2017-03-03 00:51:43 +00:00
|
|
|
USBD_Disconnect();
|
2016-03-03 11:32:04 +00:00
|
|
|
NVIC_SystemReset();
|
|
|
|
}
|
2016-09-01 18:57:56 +00:00
|
|
|
#endif
|
2016-03-03 11:32:04 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2017-03-03 00:33:24 +00:00
|
|
|
TRACE_INFO("calling configure of all configurations...\n\r");
|
2016-03-03 11:32:04 +00:00
|
|
|
for (i = 1; i < sizeof(config_func_ptrs) / sizeof(config_func_ptrs[0]);
|
|
|
|
++i) {
|
|
|
|
if (config_func_ptrs[i].configure)
|
|
|
|
config_func_ptrs[i].configure();
|
|
|
|
}
|
|
|
|
|
2017-03-03 00:33:24 +00:00
|
|
|
TRACE_INFO("calling init of config %u...\n\r", simtrace_config);
|
2016-03-03 11:32:04 +00:00
|
|
|
config_func_ptrs[simtrace_config].init();
|
|
|
|
last_simtrace_config = simtrace_config;
|
|
|
|
|
2017-03-03 00:33:24 +00:00
|
|
|
TRACE_INFO("entering main loop...\n\r");
|
2016-03-03 11:32:04 +00:00
|
|
|
while (1) {
|
2016-03-20 14:15:34 +00:00
|
|
|
#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
|
2016-03-03 11:32:04 +00:00
|
|
|
const char rotor[] = { '-', '\\', '|', '/' };
|
|
|
|
putchar('\b');
|
|
|
|
putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
|
2016-03-20 14:15:34 +00:00
|
|
|
#endif
|
2016-12-22 20:29:10 +00:00
|
|
|
check_exec_dbg_cmd();
|
2017-02-04 11:34:35 +00:00
|
|
|
osmo_timers_prepare();
|
|
|
|
osmo_timers_update();
|
2016-03-03 11:32:04 +00:00
|
|
|
|
|
|
|
if (USBD_GetState() < USBD_STATE_CONFIGURED) {
|
|
|
|
|
|
|
|
if (isUsbConnected) {
|
|
|
|
isUsbConnected = 0;
|
|
|
|
}
|
|
|
|
} else if (isUsbConnected == 0) {
|
2017-03-03 00:33:24 +00:00
|
|
|
TRACE_INFO("USB is now configured\n\r");
|
2016-03-03 11:32:04 +00:00
|
|
|
LED_Set(LED_NUM_GREEN);
|
|
|
|
LED_Clear(LED_NUM_RED);
|
|
|
|
|
|
|
|
isUsbConnected = 1;
|
|
|
|
}
|
|
|
|
if (last_simtrace_config != simtrace_config) {
|
2017-03-03 00:33:24 +00:00
|
|
|
TRACE_INFO("USB config chg %u -> %u\n\r",
|
2016-03-03 11:32:04 +00:00
|
|
|
last_simtrace_config, simtrace_config);
|
|
|
|
config_func_ptrs[last_simtrace_config].exit();
|
|
|
|
config_func_ptrs[simtrace_config].init();
|
|
|
|
last_simtrace_config = simtrace_config;
|
|
|
|
} else {
|
|
|
|
config_func_ptrs[simtrace_config].run();
|
|
|
|
}
|
|
|
|
}
|
2015-02-24 10:35:19 +00:00
|
|
|
}
|