Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 1 | // FIXME: Copyright license here |
| 2 | /*------------------------------------------------------------------------------ |
| 3 | * Headers |
| 4 | *------------------------------------------------------------------------------*/ |
| 5 | |
| 6 | #include "board.h" |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 7 | |
| 8 | /*------------------------------------------------------------------------------ |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 9 | * Internal variables |
| 10 | *------------------------------------------------------------------------------*/ |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 11 | typedef struct { |
Harald Welte | 15d72cc | 2015-11-07 18:18:47 +0100 | [diff] [blame] | 12 | /* static initialization, called whether or not the usb config is active */ |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 13 | void (* configure) ( void ); |
Harald Welte | 15d72cc | 2015-11-07 18:18:47 +0100 | [diff] [blame] | 14 | /* initialization function after the config was selected */ |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 15 | void (* init) ( void ); |
Harald Welte | 15d72cc | 2015-11-07 18:18:47 +0100 | [diff] [blame] | 16 | /* de-initialization before selecting new config */ |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 17 | void (* exit) ( void ); |
Harald Welte | 15d72cc | 2015-11-07 18:18:47 +0100 | [diff] [blame] | 18 | /* main loop content for given configuration */ |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 19 | void (* run) ( void ); |
| 20 | } conf_func; |
| 21 | |
Harald Welte | d4c1421 | 2015-11-07 18:25:46 +0100 | [diff] [blame] | 22 | static const conf_func config_func_ptrs[] = { |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 23 | /* array slot 0 is empty, usb configs start at 1 */ |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame^] | 24 | #ifdef HAVE_SNIFFER |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 25 | [CFG_NUM_SNIFF] = { |
| 26 | .configure = Sniffer_configure, |
| 27 | .init = Sniffer_init, |
| 28 | .exit = Sniffer_exit, |
| 29 | .run = Sniffer_run, |
| 30 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame^] | 31 | #endif |
| 32 | #ifdef HAVE_CCID |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 33 | [CFG_NUM_CCID] = { |
| 34 | .configure = CCID_configure, |
| 35 | .init = CCID_init, |
| 36 | .exit = CCID_exit, |
| 37 | .run = CCID_run, |
| 38 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame^] | 39 | #endif |
| 40 | #ifdef HAVE_CARDEM |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 41 | [CFG_NUM_PHONE] = { |
| 42 | .configure = Phone_configure, |
| 43 | .init = Phone_init, |
| 44 | .exit = Phone_exit, |
| 45 | .run = Phone_run, |
| 46 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame^] | 47 | #endif |
| 48 | #ifdef HAVE_MITM |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 49 | [CFG_NUM_MITM] = { |
| 50 | .configure = MITM_configure, |
| 51 | .init = MITM_init, |
| 52 | .exit = MITM_exit, |
| 53 | .run = MITM_run |
| 54 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame^] | 55 | #endif |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | |
| 59 | /*------------------------------------------------------------------------------ |
| 60 | * Internal variables |
| 61 | *------------------------------------------------------------------------------*/ |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame^] | 62 | #if defined(HAVE_SNIFFER) |
Harald Welte | 8d6a5d8 | 2015-11-07 18:27:05 +0100 | [diff] [blame] | 63 | static volatile enum confNum simtrace_config = CFG_NUM_SNIFF; |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame^] | 64 | #elif defined(HAVE_CARDEM) |
| 65 | static volatile enum confNum simtrace_config = CFG_NUM_PHONE; |
| 66 | #elif defined(HAVE_CCID) |
| 67 | static volatile enum confNum simtrace_config = CFG_NUM_CCID; |
| 68 | #endif |
| 69 | |
Harald Welte | 8d6a5d8 | 2015-11-07 18:27:05 +0100 | [diff] [blame] | 70 | |
| 71 | /*---------------------------------------------------------------------------- |
| 72 | * Callbacks |
| 73 | *----------------------------------------------------------------------------*/ |
| 74 | |
| 75 | void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum) |
| 76 | { |
| 77 | TRACE_INFO_WP("cfgChanged%d ", cfgnum); |
| 78 | simtrace_config = cfgnum; |
| 79 | } |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 80 | |
| 81 | /*------------------------------------------------------------------------------ |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 82 | * Main |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 83 | *------------------------------------------------------------------------------*/ |
Christina Quast | de68867 | 2015-04-12 15:20:57 +0200 | [diff] [blame] | 84 | #define MAX_USB_ITER BOARD_MCK/72 // This should be around a second |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 85 | extern int main( void ) |
| 86 | { |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 87 | uint8_t isUsbConnected = 0; |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 88 | enum confNum last_simtrace_config = simtrace_config; |
Christina Quast | 2379ac8 | 2015-04-16 11:09:37 +0200 | [diff] [blame] | 89 | unsigned int i = 0; |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 90 | |
Christina Quast | b5ebebe | 2015-03-05 15:43:01 +0100 | [diff] [blame] | 91 | LED_Configure(LED_NUM_RED); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 92 | LED_Configure(LED_NUM_GREEN); |
Christina Quast | 9dbf1c9 | 2015-05-06 08:46:32 +0200 | [diff] [blame] | 93 | LED_Set(LED_NUM_RED); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 94 | |
| 95 | /* Disable watchdog*/ |
| 96 | WDT_Disable( WDT ) ; |
| 97 | |
| 98 | PIO_InitializeInterrupts(0); |
| 99 | |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 100 | SIMtrace_USB_Initialize(); |
| 101 | |
| 102 | printf("%s", "USB init\n\r"); |
Christina Quast | de68867 | 2015-04-12 15:20:57 +0200 | [diff] [blame] | 103 | while(USBD_GetState() < USBD_STATE_CONFIGURED){ |
Christina Quast | 3d8c093 | 2015-06-24 16:00:13 +0200 | [diff] [blame] | 104 | if(i >= MAX_USB_ITER*3) { |
Christina Quast | de68867 | 2015-04-12 15:20:57 +0200 | [diff] [blame] | 105 | TRACE_ERROR("Resetting board (USB could not be configured)\n"); |
| 106 | NVIC_SystemReset(); |
| 107 | } |
| 108 | i++; |
| 109 | } |
| 110 | |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 111 | for (i = 1; i < sizeof(config_func_ptrs)/sizeof(config_func_ptrs[0]); ++i) |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 112 | { |
| 113 | config_func_ptrs[i].configure(); |
| 114 | } |
| 115 | |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 116 | config_func_ptrs[simtrace_config].init(); |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 117 | last_simtrace_config = simtrace_config; |
| 118 | |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 119 | printf("%s", "Start\n\r"); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 120 | while(1) { |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 121 | |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 122 | if (USBD_GetState() < USBD_STATE_CONFIGURED) { |
| 123 | |
| 124 | if (isUsbConnected) { |
| 125 | isUsbConnected = 0; |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | else if (isUsbConnected == 0) { |
| 129 | printf("USB is now configured\n\r"); |
Christina Quast | 9dbf1c9 | 2015-05-06 08:46:32 +0200 | [diff] [blame] | 130 | LED_Set(LED_NUM_GREEN); |
| 131 | LED_Clear(LED_NUM_RED); |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 132 | |
| 133 | isUsbConnected = 1; |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 134 | } |
Christina Quast | cafde00 | 2015-03-02 16:12:16 +0100 | [diff] [blame] | 135 | |
Christina Quast | cafde00 | 2015-03-02 16:12:16 +0100 | [diff] [blame] | 136 | |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 137 | if (last_simtrace_config != simtrace_config) { |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 138 | config_func_ptrs[last_simtrace_config].exit(); |
| 139 | config_func_ptrs[simtrace_config].init(); |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 140 | last_simtrace_config = simtrace_config; |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 141 | } else { |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 142 | config_func_ptrs[simtrace_config].run(); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | } |