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" |
Harald Welte | 1605564 | 2016-03-03 11:02:45 +0100 | [diff] [blame] | 7 | #include "simtrace.h" |
Harald Welte | ebbb645 | 2016-02-29 17:57:51 +0100 | [diff] [blame] | 8 | #include "utils.h" |
| 9 | #include "req_ctx.h" |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 10 | |
| 11 | /*------------------------------------------------------------------------------ |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 12 | * Internal variables |
| 13 | *------------------------------------------------------------------------------*/ |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 14 | typedef struct { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 15 | /* static initialization, called whether or not the usb config is active */ |
| 16 | void (*configure) (void); |
| 17 | /* initialization function after the config was selected */ |
| 18 | void (*init) (void); |
| 19 | /* de-initialization before selecting new config */ |
| 20 | void (*exit) (void); |
| 21 | /* main loop content for given configuration */ |
| 22 | void (*run) (void); |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 23 | } conf_func; |
| 24 | |
Harald Welte | d4c1421 | 2015-11-07 18:25:46 +0100 | [diff] [blame] | 25 | static const conf_func config_func_ptrs[] = { |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 26 | /* array slot 0 is empty, usb configs start at 1 */ |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 27 | #ifdef HAVE_SNIFFER |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 28 | [CFG_NUM_SNIFF] = { |
| 29 | .configure = Sniffer_configure, |
| 30 | .init = Sniffer_init, |
| 31 | .exit = Sniffer_exit, |
| 32 | .run = Sniffer_run, |
| 33 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 34 | #endif |
| 35 | #ifdef HAVE_CCID |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 36 | [CFG_NUM_CCID] = { |
| 37 | .configure = CCID_configure, |
| 38 | .init = CCID_init, |
| 39 | .exit = CCID_exit, |
| 40 | .run = CCID_run, |
| 41 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 42 | #endif |
| 43 | #ifdef HAVE_CARDEM |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 44 | [CFG_NUM_PHONE] = { |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 45 | .configure = mode_cardemu_configure, |
| 46 | .init = mode_cardemu_init, |
| 47 | .exit = mode_cardemu_exit, |
| 48 | .run = mode_cardemu_run, |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 49 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 50 | #endif |
| 51 | #ifdef HAVE_MITM |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 52 | [CFG_NUM_MITM] = { |
| 53 | .configure = MITM_configure, |
| 54 | .init = MITM_init, |
| 55 | .exit = MITM_exit, |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 56 | .run = MITM_run, |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 57 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 58 | #endif |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 59 | }; |
| 60 | |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 61 | /*------------------------------------------------------------------------------ |
| 62 | * Internal variables |
| 63 | *------------------------------------------------------------------------------*/ |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 64 | #if defined(HAVE_SNIFFER) |
Harald Welte | 8d6a5d8 | 2015-11-07 18:27:05 +0100 | [diff] [blame] | 65 | static volatile enum confNum simtrace_config = CFG_NUM_SNIFF; |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 66 | #elif defined(HAVE_CARDEM) |
| 67 | static volatile enum confNum simtrace_config = CFG_NUM_PHONE; |
| 68 | #elif defined(HAVE_CCID) |
| 69 | static volatile enum confNum simtrace_config = CFG_NUM_CCID; |
| 70 | #endif |
| 71 | |
Harald Welte | 8d6a5d8 | 2015-11-07 18:27:05 +0100 | [diff] [blame] | 72 | /*---------------------------------------------------------------------------- |
| 73 | * Callbacks |
| 74 | *----------------------------------------------------------------------------*/ |
| 75 | |
| 76 | void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum) |
| 77 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 78 | TRACE_INFO_WP("cfgChanged%d ", cfgnum); |
| 79 | simtrace_config = cfgnum; |
Harald Welte | 8d6a5d8 | 2015-11-07 18:27:05 +0100 | [diff] [blame] | 80 | } |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 81 | |
| 82 | /*------------------------------------------------------------------------------ |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 83 | * Main |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 84 | *------------------------------------------------------------------------------*/ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 85 | #define MAX_USB_ITER BOARD_MCK/72 // This should be around a second |
| 86 | extern int main(void) |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 87 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 88 | uint8_t isUsbConnected = 0; |
| 89 | enum confNum last_simtrace_config = simtrace_config; |
| 90 | unsigned int i = 0; |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 91 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 92 | LED_Configure(LED_NUM_RED); |
| 93 | LED_Configure(LED_NUM_GREEN); |
| 94 | LED_Set(LED_NUM_RED); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 95 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 96 | /* Disable watchdog */ |
| 97 | WDT_Disable(WDT); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 98 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 99 | req_ctx_init(); |
Harald Welte | ebbb645 | 2016-02-29 17:57:51 +0100 | [diff] [blame] | 100 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 101 | PIO_InitializeInterrupts(0); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 102 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 103 | SIMtrace_USB_Initialize(); |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 104 | |
Harald Welte | 2315e6b | 2016-03-19 21:37:55 +0100 | [diff] [blame] | 105 | printf("\r\n\r\n" |
| 106 | "=============================================================================\r\n" |
| 107 | "SIMtrace2 firmware " GIT_VERSION " (C) 2010-2016 by Harald Welte\r\n" |
| 108 | "=============================================================================\r\n"); |
| 109 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame^] | 110 | TRACE_INFO("USB init...\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 111 | while (USBD_GetState() < USBD_STATE_CONFIGURED) { |
| 112 | if (i >= MAX_USB_ITER * 3) { |
| 113 | TRACE_ERROR("Resetting board (USB could " |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame^] | 114 | "not be configured)\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 115 | NVIC_SystemReset(); |
| 116 | } |
| 117 | i++; |
| 118 | } |
Christina Quast | de68867 | 2015-04-12 15:20:57 +0200 | [diff] [blame] | 119 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame^] | 120 | TRACE_INFO("calling configure of all configurations...\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 121 | for (i = 1; i < sizeof(config_func_ptrs) / sizeof(config_func_ptrs[0]); |
| 122 | ++i) { |
| 123 | if (config_func_ptrs[i].configure) |
| 124 | config_func_ptrs[i].configure(); |
| 125 | } |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 126 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame^] | 127 | TRACE_INFO("calling init of config %u...\r\n", simtrace_config); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 128 | config_func_ptrs[simtrace_config].init(); |
| 129 | last_simtrace_config = simtrace_config; |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 130 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame^] | 131 | TRACE_INFO("entering main loop...\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 132 | while (1) { |
Harald Welte | 04e37a8 | 2016-03-20 15:15:34 +0100 | [diff] [blame] | 133 | #if TRACE_LEVEL >= TRACE_LEVEL_DEBUG |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 134 | const char rotor[] = { '-', '\\', '|', '/' }; |
| 135 | putchar('\b'); |
| 136 | putchar(rotor[i++ % ARRAY_SIZE(rotor)]); |
Harald Welte | 04e37a8 | 2016-03-20 15:15:34 +0100 | [diff] [blame] | 137 | #endif |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 138 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 139 | if (USBD_GetState() < USBD_STATE_CONFIGURED) { |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 140 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 141 | if (isUsbConnected) { |
| 142 | isUsbConnected = 0; |
| 143 | } |
| 144 | } else if (isUsbConnected == 0) { |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame^] | 145 | TRACE_INFO("USB is now configured\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 146 | LED_Set(LED_NUM_GREEN); |
| 147 | LED_Clear(LED_NUM_RED); |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 148 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 149 | isUsbConnected = 1; |
| 150 | } |
| 151 | if (last_simtrace_config != simtrace_config) { |
| 152 | TRACE_INFO("USB config chg %u -> %u\r\n", |
| 153 | last_simtrace_config, simtrace_config); |
| 154 | config_func_ptrs[last_simtrace_config].exit(); |
| 155 | config_func_ptrs[simtrace_config].init(); |
| 156 | last_simtrace_config = simtrace_config; |
| 157 | } else { |
| 158 | config_func_ptrs[simtrace_config].run(); |
| 159 | } |
| 160 | } |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 161 | } |