blob: 2b0bed54a9f2f9d81fc6156fffcc5cbb8eaa75ff [file] [log] [blame]
Christina Quast32906bb2015-02-24 11:35:19 +01001// FIXME: Copyright license here
2/*------------------------------------------------------------------------------
3 * Headers
4 *------------------------------------------------------------------------------*/
5
6#include "board.h"
Christina Quast32906bb2015-02-24 11:35:19 +01007
8/*------------------------------------------------------------------------------
Christina Quast32906bb2015-02-24 11:35:19 +01009 * Internal variables
10 *------------------------------------------------------------------------------*/
Christina Quastfb524b92015-02-27 13:39:45 +010011typedef struct {
Harald Welte15d72cc2015-11-07 18:18:47 +010012 /* static initialization, called whether or not the usb config is active */
Christina Quast95d66162015-04-09 22:38:47 +020013 void (* configure) ( void );
Harald Welte15d72cc2015-11-07 18:18:47 +010014 /* initialization function after the config was selected */
Christina Quastfb524b92015-02-27 13:39:45 +010015 void (* init) ( void );
Harald Welte15d72cc2015-11-07 18:18:47 +010016 /* de-initialization before selecting new config */
Christina Quast95d66162015-04-09 22:38:47 +020017 void (* exit) ( void );
Harald Welte15d72cc2015-11-07 18:18:47 +010018 /* main loop content for given configuration */
Christina Quastfb524b92015-02-27 13:39:45 +010019 void (* run) ( void );
20} conf_func;
21
Harald Welted4c14212015-11-07 18:25:46 +010022static const conf_func config_func_ptrs[] = {
Harald Weltefefd5712015-11-07 18:19:11 +010023 /* array slot 0 is empty, usb configs start at 1 */
Harald Welte2fb59962016-02-28 12:34:26 +010024#ifdef HAVE_SNIFFER
Harald Weltefefd5712015-11-07 18:19:11 +010025 [CFG_NUM_SNIFF] = {
26 .configure = Sniffer_configure,
27 .init = Sniffer_init,
28 .exit = Sniffer_exit,
29 .run = Sniffer_run,
30 },
Harald Welte2fb59962016-02-28 12:34:26 +010031#endif
32#ifdef HAVE_CCID
Harald Weltefefd5712015-11-07 18:19:11 +010033 [CFG_NUM_CCID] = {
34 .configure = CCID_configure,
35 .init = CCID_init,
36 .exit = CCID_exit,
37 .run = CCID_run,
38 },
Harald Welte2fb59962016-02-28 12:34:26 +010039#endif
40#ifdef HAVE_CARDEM
Harald Weltefefd5712015-11-07 18:19:11 +010041 [CFG_NUM_PHONE] = {
42 .configure = Phone_configure,
43 .init = Phone_init,
44 .exit = Phone_exit,
45 .run = Phone_run,
46 },
Harald Welte2fb59962016-02-28 12:34:26 +010047#endif
48#ifdef HAVE_MITM
Harald Weltefefd5712015-11-07 18:19:11 +010049 [CFG_NUM_MITM] = {
50 .configure = MITM_configure,
51 .init = MITM_init,
52 .exit = MITM_exit,
53 .run = MITM_run
54 },
Harald Welte2fb59962016-02-28 12:34:26 +010055#endif
Christina Quastfb524b92015-02-27 13:39:45 +010056};
57
58
59/*------------------------------------------------------------------------------
60 * Internal variables
61 *------------------------------------------------------------------------------*/
Harald Welte2fb59962016-02-28 12:34:26 +010062#if defined(HAVE_SNIFFER)
Harald Welte8d6a5d82015-11-07 18:27:05 +010063static volatile enum confNum simtrace_config = CFG_NUM_SNIFF;
Harald Welte2fb59962016-02-28 12:34:26 +010064#elif defined(HAVE_CARDEM)
65static volatile enum confNum simtrace_config = CFG_NUM_PHONE;
66#elif defined(HAVE_CCID)
67static volatile enum confNum simtrace_config = CFG_NUM_CCID;
68#endif
69
Harald Welte8d6a5d82015-11-07 18:27:05 +010070
71/*----------------------------------------------------------------------------
72 * Callbacks
73 *----------------------------------------------------------------------------*/
74
75void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum)
76{
77 TRACE_INFO_WP("cfgChanged%d ", cfgnum);
78 simtrace_config = cfgnum;
79}
Christina Quast32906bb2015-02-24 11:35:19 +010080
81/*------------------------------------------------------------------------------
Christina Quast95d66162015-04-09 22:38:47 +020082 * Main
Christina Quast32906bb2015-02-24 11:35:19 +010083 *------------------------------------------------------------------------------*/
Christina Quastde688672015-04-12 15:20:57 +020084#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
Christina Quast32906bb2015-02-24 11:35:19 +010085extern int main( void )
86{
Christina Quast1161b272015-02-25 14:15:57 +010087 uint8_t isUsbConnected = 0;
Christina Quast95d66162015-04-09 22:38:47 +020088 enum confNum last_simtrace_config = simtrace_config;
Christina Quast2379ac82015-04-16 11:09:37 +020089 unsigned int i = 0;
Christina Quast1161b272015-02-25 14:15:57 +010090
Christina Quastb5ebebe2015-03-05 15:43:01 +010091 LED_Configure(LED_NUM_RED);
Christina Quast32906bb2015-02-24 11:35:19 +010092 LED_Configure(LED_NUM_GREEN);
Christina Quast9dbf1c92015-05-06 08:46:32 +020093 LED_Set(LED_NUM_RED);
Christina Quast32906bb2015-02-24 11:35:19 +010094
95 /* Disable watchdog*/
96 WDT_Disable( WDT ) ;
97
98 PIO_InitializeInterrupts(0);
99
Christina Quast1161b272015-02-25 14:15:57 +0100100 SIMtrace_USB_Initialize();
101
102 printf("%s", "USB init\n\r");
Christina Quastde688672015-04-12 15:20:57 +0200103 while(USBD_GetState() < USBD_STATE_CONFIGURED){
Christina Quast3d8c0932015-06-24 16:00:13 +0200104 if(i >= MAX_USB_ITER*3) {
Christina Quastde688672015-04-12 15:20:57 +0200105 TRACE_ERROR("Resetting board (USB could not be configured)\n");
106 NVIC_SystemReset();
107 }
108 i++;
109 }
110
Harald Weltefefd5712015-11-07 18:19:11 +0100111 for (i = 1; i < sizeof(config_func_ptrs)/sizeof(config_func_ptrs[0]); ++i)
Christina Quast95d66162015-04-09 22:38:47 +0200112 {
113 config_func_ptrs[i].configure();
114 }
115
Harald Weltefefd5712015-11-07 18:19:11 +0100116 config_func_ptrs[simtrace_config].init();
Christina Quast95d66162015-04-09 22:38:47 +0200117 last_simtrace_config = simtrace_config;
118
Christina Quast1161b272015-02-25 14:15:57 +0100119 printf("%s", "Start\n\r");
Christina Quast32906bb2015-02-24 11:35:19 +0100120 while(1) {
Christina Quast1161b272015-02-25 14:15:57 +0100121
Christina Quast1161b272015-02-25 14:15:57 +0100122 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
123
124 if (isUsbConnected) {
125 isUsbConnected = 0;
Christina Quast1161b272015-02-25 14:15:57 +0100126 }
127 }
128 else if (isUsbConnected == 0) {
129 printf("USB is now configured\n\r");
Christina Quast9dbf1c92015-05-06 08:46:32 +0200130 LED_Set(LED_NUM_GREEN);
131 LED_Clear(LED_NUM_RED);
Christina Quast1161b272015-02-25 14:15:57 +0100132
133 isUsbConnected = 1;
Christina Quast95d66162015-04-09 22:38:47 +0200134 }
Christina Quastcafde002015-03-02 16:12:16 +0100135
Christina Quastcafde002015-03-02 16:12:16 +0100136
Christina Quast95d66162015-04-09 22:38:47 +0200137 if (last_simtrace_config != simtrace_config) {
Harald Weltefefd5712015-11-07 18:19:11 +0100138 config_func_ptrs[last_simtrace_config].exit();
139 config_func_ptrs[simtrace_config].init();
Christina Quast95d66162015-04-09 22:38:47 +0200140 last_simtrace_config = simtrace_config;
Christina Quastfb524b92015-02-27 13:39:45 +0100141 } else {
Harald Weltefefd5712015-11-07 18:19:11 +0100142 config_func_ptrs[simtrace_config].run();
Christina Quast32906bb2015-02-24 11:35:19 +0100143 }
144 }
145}