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" |
Harald Welte | 9d90d28 | 2018-06-29 22:25:42 +0200 | [diff] [blame] | 10 | #include <osmocom/core/timer.h> |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 11 | |
Harald Welte | d525231 | 2017-02-27 20:34:20 +0100 | [diff] [blame] | 12 | unsigned int g_unique_id[4]; |
Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 13 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 14 | /*------------------------------------------------------------------------------ |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 15 | * Internal variables |
| 16 | *------------------------------------------------------------------------------*/ |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 17 | typedef struct { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 18 | /* static initialization, called whether or not the usb config is active */ |
| 19 | void (*configure) (void); |
| 20 | /* initialization function after the config was selected */ |
| 21 | void (*init) (void); |
| 22 | /* de-initialization before selecting new config */ |
| 23 | void (*exit) (void); |
| 24 | /* main loop content for given configuration */ |
| 25 | void (*run) (void); |
Harald Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 26 | /* Interrupt handler for USART1 */ |
| 27 | void (*usart0_irq) (void); |
| 28 | /* Interrupt handler for USART1 */ |
| 29 | void (*usart1_irq) (void); |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 30 | } conf_func; |
| 31 | |
Harald Welte | d4c1421 | 2015-11-07 18:25:46 +0100 | [diff] [blame] | 32 | static const conf_func config_func_ptrs[] = { |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 33 | /* array slot 0 is empty, usb configs start at 1 */ |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 34 | #ifdef HAVE_SNIFFER |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 35 | [CFG_NUM_SNIFF] = { |
| 36 | .configure = Sniffer_configure, |
| 37 | .init = Sniffer_init, |
| 38 | .exit = Sniffer_exit, |
| 39 | .run = Sniffer_run, |
| 40 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 41 | #endif |
| 42 | #ifdef HAVE_CCID |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 43 | [CFG_NUM_CCID] = { |
| 44 | .configure = CCID_configure, |
| 45 | .init = CCID_init, |
| 46 | .exit = CCID_exit, |
| 47 | .run = CCID_run, |
| 48 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 49 | #endif |
| 50 | #ifdef HAVE_CARDEM |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 51 | [CFG_NUM_PHONE] = { |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 52 | .configure = mode_cardemu_configure, |
| 53 | .init = mode_cardemu_init, |
| 54 | .exit = mode_cardemu_exit, |
| 55 | .run = mode_cardemu_run, |
Harald Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 56 | .usart0_irq = mode_cardemu_usart0_irq, |
| 57 | .usart1_irq = mode_cardemu_usart1_irq, |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 58 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 59 | #endif |
| 60 | #ifdef HAVE_MITM |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 61 | [CFG_NUM_MITM] = { |
| 62 | .configure = MITM_configure, |
| 63 | .init = MITM_init, |
| 64 | .exit = MITM_exit, |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 65 | .run = MITM_run, |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 66 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 67 | #endif |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 68 | }; |
| 69 | |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 70 | /*------------------------------------------------------------------------------ |
| 71 | * Internal variables |
| 72 | *------------------------------------------------------------------------------*/ |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 73 | #if defined(HAVE_SNIFFER) |
Harald Welte | 8d6a5d8 | 2015-11-07 18:27:05 +0100 | [diff] [blame] | 74 | static volatile enum confNum simtrace_config = CFG_NUM_SNIFF; |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 75 | #elif defined(HAVE_CARDEM) |
| 76 | static volatile enum confNum simtrace_config = CFG_NUM_PHONE; |
| 77 | #elif defined(HAVE_CCID) |
| 78 | static volatile enum confNum simtrace_config = CFG_NUM_CCID; |
| 79 | #endif |
| 80 | |
Harald Welte | 8d6a5d8 | 2015-11-07 18:27:05 +0100 | [diff] [blame] | 81 | /*---------------------------------------------------------------------------- |
| 82 | * Callbacks |
| 83 | *----------------------------------------------------------------------------*/ |
| 84 | |
| 85 | void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum) |
| 86 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 87 | TRACE_INFO_WP("cfgChanged%d ", cfgnum); |
| 88 | simtrace_config = cfgnum; |
Harald Welte | 8d6a5d8 | 2015-11-07 18:27:05 +0100 | [diff] [blame] | 89 | } |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 90 | |
Harald Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 91 | void USART1_IrqHandler(void) |
| 92 | { |
| 93 | config_func_ptrs[simtrace_config].usart1_irq(); |
| 94 | } |
| 95 | |
| 96 | void USART0_IrqHandler(void) |
| 97 | { |
| 98 | config_func_ptrs[simtrace_config].usart0_irq(); |
| 99 | } |
| 100 | |
Harald Welte | f9a182d | 2017-01-11 22:22:16 +0100 | [diff] [blame] | 101 | /* returns '1' in case we should break any endless loop */ |
Harald Welte | af61479 | 2017-01-12 18:59:41 +0100 | [diff] [blame] | 102 | static void check_exec_dbg_cmd(void) |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 103 | { |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 104 | int ch; |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 105 | |
| 106 | if (!UART_IsRxReady()) |
| 107 | return; |
| 108 | |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 109 | ch = UART_GetChar(); |
| 110 | |
| 111 | board_exec_dbg_cmd(ch); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 112 | } |
Harald Welte | e974fbb | 2016-10-19 19:36:07 +0200 | [diff] [blame] | 113 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 114 | /*------------------------------------------------------------------------------ |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 115 | * Main |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 116 | *------------------------------------------------------------------------------*/ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 117 | #define MAX_USB_ITER BOARD_MCK/72 // This should be around a second |
| 118 | extern int main(void) |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 119 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 120 | uint8_t isUsbConnected = 0; |
| 121 | enum confNum last_simtrace_config = simtrace_config; |
| 122 | unsigned int i = 0; |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 123 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 124 | LED_Configure(LED_NUM_RED); |
| 125 | LED_Configure(LED_NUM_GREEN); |
| 126 | LED_Set(LED_NUM_RED); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 127 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 128 | /* Disable watchdog */ |
| 129 | WDT_Disable(WDT); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 130 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 131 | req_ctx_init(); |
Harald Welte | ebbb645 | 2016-02-29 17:57:51 +0100 | [diff] [blame] | 132 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 133 | PIO_InitializeInterrupts(0); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 134 | |
Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 135 | EEFC_ReadUniqueID(g_unique_id); |
| 136 | |
Harald Welte | 2315e6b | 2016-03-19 21:37:55 +0100 | [diff] [blame] | 137 | printf("\r\n\r\n" |
| 138 | "=============================================================================\r\n" |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 139 | "SIMtrace2 firmware " GIT_REVISION " (C) 2010-2017 by Harald Welte\r\n" |
Harald Welte | 2315e6b | 2016-03-19 21:37:55 +0100 | [diff] [blame] | 140 | "=============================================================================\r\n"); |
| 141 | |
Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 142 | TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\r\n", |
| 143 | g_unique_id[0], g_unique_id[1], |
| 144 | g_unique_id[2], g_unique_id[3]); |
| 145 | |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 146 | board_main_top(); |
Harald Welte | 6732248 | 2017-02-04 14:43:41 +0100 | [diff] [blame] | 147 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 148 | TRACE_INFO("USB init...\r\n"); |
Harald Welte | e07aed6 | 2016-12-22 22:32:15 +0100 | [diff] [blame] | 149 | SIMtrace_USB_Initialize(); |
| 150 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 151 | while (USBD_GetState() < USBD_STATE_CONFIGURED) { |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 152 | check_exec_dbg_cmd(); |
Harald Welte | 67415e3 | 2016-09-01 20:57:56 +0200 | [diff] [blame] | 153 | #if 0 |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 154 | if (i >= MAX_USB_ITER * 3) { |
| 155 | TRACE_ERROR("Resetting board (USB could " |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 156 | "not be configured)\r\n"); |
Harald Welte | f415d71 | 2017-03-03 01:51:43 +0100 | [diff] [blame] | 157 | USBD_Disconnect(); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 158 | NVIC_SystemReset(); |
| 159 | } |
Harald Welte | 67415e3 | 2016-09-01 20:57:56 +0200 | [diff] [blame] | 160 | #endif |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 161 | i++; |
| 162 | } |
Christina Quast | de68867 | 2015-04-12 15:20:57 +0200 | [diff] [blame] | 163 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 164 | TRACE_INFO("calling configure of all configurations...\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 165 | for (i = 1; i < sizeof(config_func_ptrs) / sizeof(config_func_ptrs[0]); |
| 166 | ++i) { |
| 167 | if (config_func_ptrs[i].configure) |
| 168 | config_func_ptrs[i].configure(); |
| 169 | } |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 170 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 171 | TRACE_INFO("calling init of config %u...\r\n", simtrace_config); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 172 | config_func_ptrs[simtrace_config].init(); |
| 173 | last_simtrace_config = simtrace_config; |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 174 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 175 | TRACE_INFO("entering main loop...\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 176 | while (1) { |
Harald Welte | 04e37a8 | 2016-03-20 15:15:34 +0100 | [diff] [blame] | 177 | #if TRACE_LEVEL >= TRACE_LEVEL_DEBUG |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 178 | const char rotor[] = { '-', '\\', '|', '/' }; |
| 179 | putchar('\b'); |
| 180 | putchar(rotor[i++ % ARRAY_SIZE(rotor)]); |
Harald Welte | 04e37a8 | 2016-03-20 15:15:34 +0100 | [diff] [blame] | 181 | #endif |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 182 | check_exec_dbg_cmd(); |
Harald Welte | 987f59a | 2017-02-04 12:34:35 +0100 | [diff] [blame] | 183 | osmo_timers_prepare(); |
| 184 | osmo_timers_update(); |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 185 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 186 | if (USBD_GetState() < USBD_STATE_CONFIGURED) { |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 187 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 188 | if (isUsbConnected) { |
| 189 | isUsbConnected = 0; |
| 190 | } |
| 191 | } else if (isUsbConnected == 0) { |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 192 | TRACE_INFO("USB is now configured\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 193 | LED_Set(LED_NUM_GREEN); |
| 194 | LED_Clear(LED_NUM_RED); |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 195 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 196 | isUsbConnected = 1; |
| 197 | } |
| 198 | if (last_simtrace_config != simtrace_config) { |
| 199 | TRACE_INFO("USB config chg %u -> %u\r\n", |
| 200 | last_simtrace_config, simtrace_config); |
| 201 | config_func_ptrs[last_simtrace_config].exit(); |
| 202 | config_func_ptrs[simtrace_config].init(); |
| 203 | last_simtrace_config = simtrace_config; |
| 204 | } else { |
| 205 | config_func_ptrs[simtrace_config].run(); |
| 206 | } |
| 207 | } |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 208 | } |