blob: 16ea292798c9a5434b26c678c91c6c9417eb1abc [file] [log] [blame]
Christina Quast32906bb2015-02-24 11:35:19 +01001// FIXME: Copyright license here
2/*------------------------------------------------------------------------------
3 * Headers
4 *------------------------------------------------------------------------------*/
5
6#include "board.h"
Harald Welte16055642016-03-03 11:02:45 +01007#include "simtrace.h"
Harald Welteebbb6452016-02-29 17:57:51 +01008#include "utils.h"
9#include "req_ctx.h"
Christina Quast32906bb2015-02-24 11:35:19 +010010
11/*------------------------------------------------------------------------------
Christina Quast32906bb2015-02-24 11:35:19 +010012 * Internal variables
13 *------------------------------------------------------------------------------*/
Christina Quastfb524b92015-02-27 13:39:45 +010014typedef struct {
Harald Welte7dd3dfd2016-03-03 12:32:04 +010015 /* 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 Quastfb524b92015-02-27 13:39:45 +010023} conf_func;
24
Harald Welted4c14212015-11-07 18:25:46 +010025static const conf_func config_func_ptrs[] = {
Harald Weltefefd5712015-11-07 18:19:11 +010026 /* array slot 0 is empty, usb configs start at 1 */
Harald Welte2fb59962016-02-28 12:34:26 +010027#ifdef HAVE_SNIFFER
Harald Weltefefd5712015-11-07 18:19:11 +010028 [CFG_NUM_SNIFF] = {
29 .configure = Sniffer_configure,
30 .init = Sniffer_init,
31 .exit = Sniffer_exit,
32 .run = Sniffer_run,
33 },
Harald Welte2fb59962016-02-28 12:34:26 +010034#endif
35#ifdef HAVE_CCID
Harald Weltefefd5712015-11-07 18:19:11 +010036 [CFG_NUM_CCID] = {
37 .configure = CCID_configure,
38 .init = CCID_init,
39 .exit = CCID_exit,
40 .run = CCID_run,
41 },
Harald Welte2fb59962016-02-28 12:34:26 +010042#endif
43#ifdef HAVE_CARDEM
Harald Weltefefd5712015-11-07 18:19:11 +010044 [CFG_NUM_PHONE] = {
Harald Welte2a6d3af2016-02-28 19:29:14 +010045 .configure = mode_cardemu_configure,
46 .init = mode_cardemu_init,
47 .exit = mode_cardemu_exit,
48 .run = mode_cardemu_run,
Harald Weltefefd5712015-11-07 18:19:11 +010049 },
Harald Welte2fb59962016-02-28 12:34:26 +010050#endif
51#ifdef HAVE_MITM
Harald Weltefefd5712015-11-07 18:19:11 +010052 [CFG_NUM_MITM] = {
53 .configure = MITM_configure,
54 .init = MITM_init,
55 .exit = MITM_exit,
Harald Welte7dd3dfd2016-03-03 12:32:04 +010056 .run = MITM_run,
Harald Weltefefd5712015-11-07 18:19:11 +010057 },
Harald Welte2fb59962016-02-28 12:34:26 +010058#endif
Christina Quastfb524b92015-02-27 13:39:45 +010059};
60
Christina Quastfb524b92015-02-27 13:39:45 +010061/*------------------------------------------------------------------------------
62 * Internal variables
63 *------------------------------------------------------------------------------*/
Harald Welte2fb59962016-02-28 12:34:26 +010064#if defined(HAVE_SNIFFER)
Harald Welte8d6a5d82015-11-07 18:27:05 +010065static volatile enum confNum simtrace_config = CFG_NUM_SNIFF;
Harald Welte2fb59962016-02-28 12:34:26 +010066#elif defined(HAVE_CARDEM)
67static volatile enum confNum simtrace_config = CFG_NUM_PHONE;
68#elif defined(HAVE_CCID)
69static volatile enum confNum simtrace_config = CFG_NUM_CCID;
70#endif
71
Harald Welte8d6a5d82015-11-07 18:27:05 +010072/*----------------------------------------------------------------------------
73 * Callbacks
74 *----------------------------------------------------------------------------*/
75
76void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum)
77{
Harald Welte7dd3dfd2016-03-03 12:32:04 +010078 TRACE_INFO_WP("cfgChanged%d ", cfgnum);
79 simtrace_config = cfgnum;
Harald Welte8d6a5d82015-11-07 18:27:05 +010080}
Christina Quast32906bb2015-02-24 11:35:19 +010081
82/*------------------------------------------------------------------------------
Christina Quast95d66162015-04-09 22:38:47 +020083 * Main
Christina Quast32906bb2015-02-24 11:35:19 +010084 *------------------------------------------------------------------------------*/
Harald Welte7dd3dfd2016-03-03 12:32:04 +010085#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
86extern int main(void)
Christina Quast32906bb2015-02-24 11:35:19 +010087{
Harald Welte7dd3dfd2016-03-03 12:32:04 +010088 uint8_t isUsbConnected = 0;
89 enum confNum last_simtrace_config = simtrace_config;
90 unsigned int i = 0;
Christina Quast1161b272015-02-25 14:15:57 +010091
Harald Welte7dd3dfd2016-03-03 12:32:04 +010092 LED_Configure(LED_NUM_RED);
93 LED_Configure(LED_NUM_GREEN);
94 LED_Set(LED_NUM_RED);
Christina Quast32906bb2015-02-24 11:35:19 +010095
Harald Welte7dd3dfd2016-03-03 12:32:04 +010096 /* Disable watchdog */
97 WDT_Disable(WDT);
Christina Quast32906bb2015-02-24 11:35:19 +010098
Harald Welte7dd3dfd2016-03-03 12:32:04 +010099 req_ctx_init();
Harald Welteebbb6452016-02-29 17:57:51 +0100100
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100101 PIO_InitializeInterrupts(0);
Christina Quast32906bb2015-02-24 11:35:19 +0100102
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100103 SIMtrace_USB_Initialize();
Christina Quast1161b272015-02-25 14:15:57 +0100104
Harald Welte2315e6b2016-03-19 21:37:55 +0100105 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 Weltecf1c19a2016-03-20 15:18:18 +0100110 TRACE_INFO("USB init...\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100111 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
112 if (i >= MAX_USB_ITER * 3) {
113 TRACE_ERROR("Resetting board (USB could "
Harald Weltecf1c19a2016-03-20 15:18:18 +0100114 "not be configured)\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100115 NVIC_SystemReset();
116 }
117 i++;
118 }
Christina Quastde688672015-04-12 15:20:57 +0200119
Harald Weltecf1c19a2016-03-20 15:18:18 +0100120 TRACE_INFO("calling configure of all configurations...\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100121 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 Quast95d66162015-04-09 22:38:47 +0200126
Harald Weltecf1c19a2016-03-20 15:18:18 +0100127 TRACE_INFO("calling init of config %u...\r\n", simtrace_config);
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100128 config_func_ptrs[simtrace_config].init();
129 last_simtrace_config = simtrace_config;
Christina Quast95d66162015-04-09 22:38:47 +0200130
Harald Weltecf1c19a2016-03-20 15:18:18 +0100131 TRACE_INFO("entering main loop...\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100132 while (1) {
Harald Welte04e37a82016-03-20 15:15:34 +0100133#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100134 const char rotor[] = { '-', '\\', '|', '/' };
135 putchar('\b');
136 putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
Harald Welte04e37a82016-03-20 15:15:34 +0100137#endif
Christina Quast1161b272015-02-25 14:15:57 +0100138
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100139 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
Christina Quast1161b272015-02-25 14:15:57 +0100140
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100141 if (isUsbConnected) {
142 isUsbConnected = 0;
143 }
144 } else if (isUsbConnected == 0) {
Harald Weltecf1c19a2016-03-20 15:18:18 +0100145 TRACE_INFO("USB is now configured\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100146 LED_Set(LED_NUM_GREEN);
147 LED_Clear(LED_NUM_RED);
Christina Quast1161b272015-02-25 14:15:57 +0100148
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100149 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 Quast32906bb2015-02-24 11:35:19 +0100161}