blob: d22687e10dff0d37c2532bc63bd407806ebda593 [file] [log] [blame]
Kévin Redon9a12d682018-07-08 13:21:16 +02001/* SIMtrace 2 firmware card emulation, CCID, and sniffer application
2 *
3 * (C) 2015-2017 by Harald Welte <hwelte@hmw-consulting.de>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
Christina Quast32906bb2015-02-24 11:35:19 +010019/*------------------------------------------------------------------------------
20 * Headers
21 *------------------------------------------------------------------------------*/
22
23#include "board.h"
Harald Welte16055642016-03-03 11:02:45 +010024#include "simtrace.h"
Harald Welteebbb6452016-02-29 17:57:51 +010025#include "utils.h"
26#include "req_ctx.h"
Harald Welte9d90d282018-06-29 22:25:42 +020027#include <osmocom/core/timer.h>
Christina Quast32906bb2015-02-24 11:35:19 +010028
29/*------------------------------------------------------------------------------
Christina Quast32906bb2015-02-24 11:35:19 +010030 * Internal variables
31 *------------------------------------------------------------------------------*/
Christina Quastfb524b92015-02-27 13:39:45 +010032typedef struct {
Harald Welte7dd3dfd2016-03-03 12:32:04 +010033 /* static initialization, called whether or not the usb config is active */
34 void (*configure) (void);
35 /* initialization function after the config was selected */
36 void (*init) (void);
37 /* de-initialization before selecting new config */
38 void (*exit) (void);
39 /* main loop content for given configuration */
40 void (*run) (void);
Harald Welte3bafe432016-03-20 16:43:12 +010041 /* Interrupt handler for USART1 */
42 void (*usart0_irq) (void);
43 /* Interrupt handler for USART1 */
44 void (*usart1_irq) (void);
Christina Quastfb524b92015-02-27 13:39:45 +010045} conf_func;
46
Harald Welted4c14212015-11-07 18:25:46 +010047static const conf_func config_func_ptrs[] = {
Harald Weltefefd5712015-11-07 18:19:11 +010048 /* array slot 0 is empty, usb configs start at 1 */
Harald Welte2fb59962016-02-28 12:34:26 +010049#ifdef HAVE_SNIFFER
Harald Weltefefd5712015-11-07 18:19:11 +010050 [CFG_NUM_SNIFF] = {
51 .configure = Sniffer_configure,
52 .init = Sniffer_init,
53 .exit = Sniffer_exit,
54 .run = Sniffer_run,
55 },
Harald Welte2fb59962016-02-28 12:34:26 +010056#endif
57#ifdef HAVE_CCID
Harald Weltefefd5712015-11-07 18:19:11 +010058 [CFG_NUM_CCID] = {
59 .configure = CCID_configure,
60 .init = CCID_init,
61 .exit = CCID_exit,
62 .run = CCID_run,
63 },
Harald Welte2fb59962016-02-28 12:34:26 +010064#endif
65#ifdef HAVE_CARDEM
Harald Weltefefd5712015-11-07 18:19:11 +010066 [CFG_NUM_PHONE] = {
Harald Welte2a6d3af2016-02-28 19:29:14 +010067 .configure = mode_cardemu_configure,
68 .init = mode_cardemu_init,
69 .exit = mode_cardemu_exit,
70 .run = mode_cardemu_run,
Harald Welte3bafe432016-03-20 16:43:12 +010071 .usart0_irq = mode_cardemu_usart0_irq,
72 .usart1_irq = mode_cardemu_usart1_irq,
Harald Weltefefd5712015-11-07 18:19:11 +010073 },
Harald Welte2fb59962016-02-28 12:34:26 +010074#endif
75#ifdef HAVE_MITM
Harald Weltefefd5712015-11-07 18:19:11 +010076 [CFG_NUM_MITM] = {
77 .configure = MITM_configure,
78 .init = MITM_init,
79 .exit = MITM_exit,
Harald Welte7dd3dfd2016-03-03 12:32:04 +010080 .run = MITM_run,
Harald Weltefefd5712015-11-07 18:19:11 +010081 },
Harald Welte2fb59962016-02-28 12:34:26 +010082#endif
Christina Quastfb524b92015-02-27 13:39:45 +010083};
84
Christina Quastfb524b92015-02-27 13:39:45 +010085/*------------------------------------------------------------------------------
86 * Internal variables
87 *------------------------------------------------------------------------------*/
Harald Welte2fb59962016-02-28 12:34:26 +010088#if defined(HAVE_SNIFFER)
Harald Welte8d6a5d82015-11-07 18:27:05 +010089static volatile enum confNum simtrace_config = CFG_NUM_SNIFF;
Harald Welte2fb59962016-02-28 12:34:26 +010090#elif defined(HAVE_CARDEM)
91static volatile enum confNum simtrace_config = CFG_NUM_PHONE;
92#elif defined(HAVE_CCID)
93static volatile enum confNum simtrace_config = CFG_NUM_CCID;
94#endif
95
Harald Welte8d6a5d82015-11-07 18:27:05 +010096/*----------------------------------------------------------------------------
97 * Callbacks
98 *----------------------------------------------------------------------------*/
99
100void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum)
101{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100102 TRACE_INFO_WP("cfgChanged%d ", cfgnum);
103 simtrace_config = cfgnum;
Harald Welte8d6a5d82015-11-07 18:27:05 +0100104}
Christina Quast32906bb2015-02-24 11:35:19 +0100105
Harald Welte3bafe432016-03-20 16:43:12 +0100106void USART1_IrqHandler(void)
107{
108 config_func_ptrs[simtrace_config].usart1_irq();
109}
110
111void USART0_IrqHandler(void)
112{
113 config_func_ptrs[simtrace_config].usart0_irq();
114}
115
Harald Weltef9a182d2017-01-11 22:22:16 +0100116/* returns '1' in case we should break any endless loop */
Harald Welteaf614792017-01-12 18:59:41 +0100117static void check_exec_dbg_cmd(void)
Harald Welte006b16d2016-12-22 21:29:10 +0100118{
Harald Welteaa3b8672017-02-10 19:21:10 +0100119 int ch;
Harald Welte006b16d2016-12-22 21:29:10 +0100120
121 if (!UART_IsRxReady())
122 return;
123
Harald Welteaa3b8672017-02-10 19:21:10 +0100124 ch = UART_GetChar();
125
126 board_exec_dbg_cmd(ch);
Harald Welte006b16d2016-12-22 21:29:10 +0100127}
Harald Weltee974fbb2016-10-19 19:36:07 +0200128
Christina Quast32906bb2015-02-24 11:35:19 +0100129/*------------------------------------------------------------------------------
Christina Quast95d66162015-04-09 22:38:47 +0200130 * Main
Christina Quast32906bb2015-02-24 11:35:19 +0100131 *------------------------------------------------------------------------------*/
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100132#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
133extern int main(void)
Christina Quast32906bb2015-02-24 11:35:19 +0100134{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100135 uint8_t isUsbConnected = 0;
136 enum confNum last_simtrace_config = simtrace_config;
137 unsigned int i = 0;
Christina Quast1161b272015-02-25 14:15:57 +0100138
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100139 LED_Configure(LED_NUM_RED);
140 LED_Configure(LED_NUM_GREEN);
141 LED_Set(LED_NUM_RED);
Christina Quast32906bb2015-02-24 11:35:19 +0100142
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100143 /* Disable watchdog */
144 WDT_Disable(WDT);
Christina Quast32906bb2015-02-24 11:35:19 +0100145
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100146 req_ctx_init();
Harald Welteebbb6452016-02-29 17:57:51 +0100147
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100148 PIO_InitializeInterrupts(0);
Christina Quast32906bb2015-02-24 11:35:19 +0100149
Harald Weltee3b2de42020-01-11 12:31:51 +0100150 print_banner();
Harald Welteaa3b8672017-02-10 19:21:10 +0100151 board_main_top();
Harald Welte67322482017-02-04 14:43:41 +0100152
Harald Weltecf1c19a2016-03-20 15:18:18 +0100153 TRACE_INFO("USB init...\r\n");
Harald Weltee07aed62016-12-22 22:32:15 +0100154 SIMtrace_USB_Initialize();
155
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100156 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
Harald Welte006b16d2016-12-22 21:29:10 +0100157 check_exec_dbg_cmd();
Harald Welte67415e32016-09-01 20:57:56 +0200158#if 0
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100159 if (i >= MAX_USB_ITER * 3) {
160 TRACE_ERROR("Resetting board (USB could "
Harald Weltecf1c19a2016-03-20 15:18:18 +0100161 "not be configured)\r\n");
Harald Weltef415d712017-03-03 01:51:43 +0100162 USBD_Disconnect();
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100163 NVIC_SystemReset();
164 }
Harald Welte67415e32016-09-01 20:57:56 +0200165#endif
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100166 i++;
167 }
Christina Quastde688672015-04-12 15:20:57 +0200168
Harald Weltecf1c19a2016-03-20 15:18:18 +0100169 TRACE_INFO("calling configure of all configurations...\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100170 for (i = 1; i < sizeof(config_func_ptrs) / sizeof(config_func_ptrs[0]);
171 ++i) {
172 if (config_func_ptrs[i].configure)
173 config_func_ptrs[i].configure();
174 }
Christina Quast95d66162015-04-09 22:38:47 +0200175
Harald Weltecf1c19a2016-03-20 15:18:18 +0100176 TRACE_INFO("calling init of config %u...\r\n", simtrace_config);
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100177 config_func_ptrs[simtrace_config].init();
178 last_simtrace_config = simtrace_config;
Christina Quast95d66162015-04-09 22:38:47 +0200179
Harald Weltecf1c19a2016-03-20 15:18:18 +0100180 TRACE_INFO("entering main loop...\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100181 while (1) {
Harald Welte04e37a82016-03-20 15:15:34 +0100182#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100183 const char rotor[] = { '-', '\\', '|', '/' };
184 putchar('\b');
185 putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
Harald Welte04e37a82016-03-20 15:15:34 +0100186#endif
Harald Welte006b16d2016-12-22 21:29:10 +0100187 check_exec_dbg_cmd();
Harald Welte987f59a2017-02-04 12:34:35 +0100188 osmo_timers_prepare();
189 osmo_timers_update();
Christina Quast1161b272015-02-25 14:15:57 +0100190
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100191 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
Christina Quast1161b272015-02-25 14:15:57 +0100192
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100193 if (isUsbConnected) {
194 isUsbConnected = 0;
195 }
196 } else if (isUsbConnected == 0) {
Harald Weltecf1c19a2016-03-20 15:18:18 +0100197 TRACE_INFO("USB is now configured\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100198 LED_Set(LED_NUM_GREEN);
199 LED_Clear(LED_NUM_RED);
Christina Quast1161b272015-02-25 14:15:57 +0100200
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100201 isUsbConnected = 1;
202 }
203 if (last_simtrace_config != simtrace_config) {
204 TRACE_INFO("USB config chg %u -> %u\r\n",
205 last_simtrace_config, simtrace_config);
206 config_func_ptrs[last_simtrace_config].exit();
207 config_func_ptrs[simtrace_config].init();
208 last_simtrace_config = simtrace_config;
209 } else {
210 config_func_ptrs[simtrace_config].run();
211 }
212 }
Christina Quast32906bb2015-02-24 11:35:19 +0100213}