2015-02-24 10:35:19 +00:00
|
|
|
// FIXME: Copyright license here
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* Headers
|
|
|
|
*------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#include "board.h"
|
|
|
|
|
2015-02-27 12:39:45 +00:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* Internal variables
|
|
|
|
*------------------------------------------------------------------------------*/
|
|
|
|
typedef struct {
|
2015-11-07 17:18:47 +00:00
|
|
|
/* static initialization, called whether or not the usb config is active */
|
2015-04-09 20:38:47 +00:00
|
|
|
void (* configure) ( void );
|
2015-11-07 17:18:47 +00:00
|
|
|
/* initialization function after the config was selected */
|
2015-02-27 12:39:45 +00:00
|
|
|
void (* init) ( void );
|
2015-11-07 17:18:47 +00:00
|
|
|
/* de-initialization before selecting new config */
|
2015-04-09 20:38:47 +00:00
|
|
|
void (* exit) ( void );
|
2015-11-07 17:18:47 +00:00
|
|
|
/* main loop content for given configuration */
|
2015-02-27 12:39:45 +00:00
|
|
|
void (* run) ( void );
|
|
|
|
} 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,
|
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,
|
|
|
|
.run = MITM_run
|
|
|
|
},
|
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)
|
|
|
|
{
|
|
|
|
TRACE_INFO_WP("cfgChanged%d ", cfgnum);
|
|
|
|
simtrace_config = cfgnum;
|
|
|
|
}
|
2015-02-24 10:35:19 +00:00
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
2015-04-09 20:38:47 +00:00
|
|
|
* Main
|
2015-02-24 10:35:19 +00:00
|
|
|
*------------------------------------------------------------------------------*/
|
2015-04-12 13:20:57 +00:00
|
|
|
#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
|
2015-02-24 10:35:19 +00:00
|
|
|
extern int main( void )
|
|
|
|
{
|
2015-02-25 13:15:57 +00:00
|
|
|
uint8_t isUsbConnected = 0;
|
2015-04-09 20:38:47 +00:00
|
|
|
enum confNum last_simtrace_config = simtrace_config;
|
2015-04-16 09:09:37 +00:00
|
|
|
unsigned int i = 0;
|
2015-02-25 13:15:57 +00:00
|
|
|
|
2015-03-05 14:43:01 +00:00
|
|
|
LED_Configure(LED_NUM_RED);
|
2015-02-24 10:35:19 +00:00
|
|
|
LED_Configure(LED_NUM_GREEN);
|
2015-05-06 06:46:32 +00:00
|
|
|
LED_Set(LED_NUM_RED);
|
2015-02-24 10:35:19 +00:00
|
|
|
|
|
|
|
/* Disable watchdog*/
|
|
|
|
WDT_Disable( WDT ) ;
|
|
|
|
|
|
|
|
PIO_InitializeInterrupts(0);
|
|
|
|
|
2015-02-25 13:15:57 +00:00
|
|
|
SIMtrace_USB_Initialize();
|
|
|
|
|
|
|
|
printf("%s", "USB init\n\r");
|
2015-04-12 13:20:57 +00:00
|
|
|
while(USBD_GetState() < USBD_STATE_CONFIGURED){
|
2015-06-24 14:00:13 +00:00
|
|
|
if(i >= MAX_USB_ITER*3) {
|
2015-04-12 13:20:57 +00:00
|
|
|
TRACE_ERROR("Resetting board (USB could not be configured)\n");
|
|
|
|
NVIC_SystemReset();
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2015-11-07 17:19:11 +00:00
|
|
|
for (i = 1; i < sizeof(config_func_ptrs)/sizeof(config_func_ptrs[0]); ++i)
|
2015-04-09 20:38:47 +00:00
|
|
|
{
|
|
|
|
config_func_ptrs[i].configure();
|
|
|
|
}
|
|
|
|
|
2015-11-07 17:19:11 +00:00
|
|
|
config_func_ptrs[simtrace_config].init();
|
2015-04-09 20:38:47 +00:00
|
|
|
last_simtrace_config = simtrace_config;
|
|
|
|
|
2015-02-25 13:15:57 +00:00
|
|
|
printf("%s", "Start\n\r");
|
2015-02-24 10:35:19 +00:00
|
|
|
while(1) {
|
2015-02-25 13:15:57 +00:00
|
|
|
|
|
|
|
if (USBD_GetState() < USBD_STATE_CONFIGURED) {
|
|
|
|
|
|
|
|
if (isUsbConnected) {
|
|
|
|
isUsbConnected = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (isUsbConnected == 0) {
|
|
|
|
printf("USB is now configured\n\r");
|
2015-05-06 06:46:32 +00:00
|
|
|
LED_Set(LED_NUM_GREEN);
|
|
|
|
LED_Clear(LED_NUM_RED);
|
2015-02-25 13:15:57 +00:00
|
|
|
|
|
|
|
isUsbConnected = 1;
|
2015-04-09 20:38:47 +00:00
|
|
|
}
|
2015-03-02 15:12:16 +00:00
|
|
|
|
|
|
|
|
2015-04-09 20:38:47 +00:00
|
|
|
if (last_simtrace_config != simtrace_config) {
|
2015-11-07 17:19:11 +00:00
|
|
|
config_func_ptrs[last_simtrace_config].exit();
|
|
|
|
config_func_ptrs[simtrace_config].init();
|
2015-04-09 20:38:47 +00:00
|
|
|
last_simtrace_config = simtrace_config;
|
2015-02-27 12:39:45 +00:00
|
|
|
} else {
|
2015-11-07 17:19:11 +00:00
|
|
|
config_func_ptrs[simtrace_config].run();
|
2015-02-24 10:35:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|