blob: a30a211940f93eacb44f36e6330ade137302af46 [file] [log] [blame]
Kévin Redon9a12d682018-07-08 13:21:16 +02001/* SIMtrace 2 firmware card emulation application
2 *
3 * (C) 2015-2017 by Harald Welte <hwelte@hmw-consulting.de>
Kévin Redon9b367872019-02-07 17:52:08 +01004 * (C) 2018-2019, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
Kévin Redon9a12d682018-07-08 13:21:16 +02005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
Christina Quast32906bb2015-02-24 11:35:19 +010020/*------------------------------------------------------------------------------
21 * Headers
22 *------------------------------------------------------------------------------*/
23
24#include "board.h"
Harald Welte16055642016-03-03 11:02:45 +010025#include "simtrace.h"
Harald Welteebbb6452016-02-29 17:57:51 +010026#include "utils.h"
Harald Weltee3b2de42020-01-11 12:31:51 +010027#include "main_common.h"
Harald Welte9d90d282018-06-29 22:25:42 +020028#include <osmocom/core/timer.h>
Christina Quast32906bb2015-02-24 11:35:19 +010029
30/*------------------------------------------------------------------------------
Christina Quast32906bb2015-02-24 11:35:19 +010031 * Internal variables
32 *------------------------------------------------------------------------------*/
Christina Quastfb524b92015-02-27 13:39:45 +010033typedef struct {
Harald Welte7dd3dfd2016-03-03 12:32:04 +010034 /* static initialization, called whether or not the usb config is active */
35 void (*configure) (void);
36 /* initialization function after the config was selected */
37 void (*init) (void);
38 /* de-initialization before selecting new config */
39 void (*exit) (void);
40 /* main loop content for given configuration */
41 void (*run) (void);
Harald Welte34200e42020-01-11 12:50:32 +010042 /* Interrupt handler for USART0 */
Harald Welte3bafe432016-03-20 16:43:12 +010043 void (*usart0_irq) (void);
44 /* Interrupt handler for USART1 */
45 void (*usart1_irq) (void);
Christina Quastfb524b92015-02-27 13:39:45 +010046} conf_func;
47
Harald Welted4c14212015-11-07 18:25:46 +010048static const conf_func config_func_ptrs[] = {
Harald Weltefefd5712015-11-07 18:19:11 +010049 /* array slot 0 is empty, usb configs start at 1 */
Harald Welte2fb59962016-02-28 12:34:26 +010050#ifdef HAVE_SNIFFER
Harald Weltefefd5712015-11-07 18:19:11 +010051 [CFG_NUM_SNIFF] = {
52 .configure = Sniffer_configure,
53 .init = Sniffer_init,
54 .exit = Sniffer_exit,
55 .run = Sniffer_run,
Harald Welte34200e42020-01-11 12:50:32 +010056 .usart0_irq = Sniffer_usart0_irq,
57 .usart1_irq = Sniffer_usart1_irq,
Harald Weltefefd5712015-11-07 18:19:11 +010058 },
Harald Welte2fb59962016-02-28 12:34:26 +010059#endif
60#ifdef HAVE_CCID
Harald Weltefefd5712015-11-07 18:19:11 +010061 [CFG_NUM_CCID] = {
62 .configure = CCID_configure,
63 .init = CCID_init,
64 .exit = CCID_exit,
65 .run = CCID_run,
66 },
Harald Welte2fb59962016-02-28 12:34:26 +010067#endif
68#ifdef HAVE_CARDEM
Harald Weltefefd5712015-11-07 18:19:11 +010069 [CFG_NUM_PHONE] = {
Harald Welte2a6d3af2016-02-28 19:29:14 +010070 .configure = mode_cardemu_configure,
71 .init = mode_cardemu_init,
72 .exit = mode_cardemu_exit,
73 .run = mode_cardemu_run,
Harald Welte3bafe432016-03-20 16:43:12 +010074 .usart0_irq = mode_cardemu_usart0_irq,
75 .usart1_irq = mode_cardemu_usart1_irq,
Harald Weltefefd5712015-11-07 18:19:11 +010076 },
Harald Welte2fb59962016-02-28 12:34:26 +010077#endif
78#ifdef HAVE_MITM
Harald Weltefefd5712015-11-07 18:19:11 +010079 [CFG_NUM_MITM] = {
80 .configure = MITM_configure,
81 .init = MITM_init,
82 .exit = MITM_exit,
Harald Welte7dd3dfd2016-03-03 12:32:04 +010083 .run = MITM_run,
Harald Weltefefd5712015-11-07 18:19:11 +010084 },
Harald Welte2fb59962016-02-28 12:34:26 +010085#endif
Christina Quastfb524b92015-02-27 13:39:45 +010086};
87
Christina Quastfb524b92015-02-27 13:39:45 +010088/*------------------------------------------------------------------------------
89 * Internal variables
90 *------------------------------------------------------------------------------*/
Harald Welte2fb59962016-02-28 12:34:26 +010091#if defined(HAVE_SNIFFER)
Harald Welte8d6a5d82015-11-07 18:27:05 +010092static volatile enum confNum simtrace_config = CFG_NUM_SNIFF;
Harald Welte2fb59962016-02-28 12:34:26 +010093#elif defined(HAVE_CARDEM)
94static volatile enum confNum simtrace_config = CFG_NUM_PHONE;
95#elif defined(HAVE_CCID)
96static volatile enum confNum simtrace_config = CFG_NUM_CCID;
97#endif
98
Harald Welte8d6a5d82015-11-07 18:27:05 +010099/*----------------------------------------------------------------------------
100 * Callbacks
101 *----------------------------------------------------------------------------*/
102
103void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum)
104{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100105 TRACE_INFO_WP("cfgChanged%d ", cfgnum);
Kévin Redon7d5d0112018-09-06 22:46:54 +0200106 if (cfgnum < ARRAY_SIZE(config_func_ptrs)) {
107 simtrace_config = cfgnum;
108 } else {
109 TRACE_ERROR("trying to set out of bounds config %u\r\n", cfgnum);
110 }
Harald Welte8d6a5d82015-11-07 18:27:05 +0100111}
Christina Quast32906bb2015-02-24 11:35:19 +0100112
Harald Welte3bafe432016-03-20 16:43:12 +0100113void USART1_IrqHandler(void)
114{
Harald Welte2afd57f2017-11-28 22:52:56 +0100115 if (config_func_ptrs[simtrace_config].usart1_irq)
116 config_func_ptrs[simtrace_config].usart1_irq();
Harald Welte3bafe432016-03-20 16:43:12 +0100117}
118
119void USART0_IrqHandler(void)
120{
Harald Welte2afd57f2017-11-28 22:52:56 +0100121 if (config_func_ptrs[simtrace_config].usart0_irq)
122 config_func_ptrs[simtrace_config].usart0_irq();
Harald Welte3bafe432016-03-20 16:43:12 +0100123}
124
Harald Weltef9a182d2017-01-11 22:22:16 +0100125/* returns '1' in case we should break any endless loop */
Harald Welteaf614792017-01-12 18:59:41 +0100126static void check_exec_dbg_cmd(void)
Harald Welte006b16d2016-12-22 21:29:10 +0100127{
Harald Welteaa3b8672017-02-10 19:21:10 +0100128 int ch;
Harald Welte006b16d2016-12-22 21:29:10 +0100129
130 if (!UART_IsRxReady())
131 return;
132
Harald Welteaa3b8672017-02-10 19:21:10 +0100133 ch = UART_GetChar();
Kévin Redon4f3a0352018-09-06 22:48:11 +0200134 /* We must echo the character to make python fdexpect happy, which we use in factory testing */
Harald Welte46893452018-07-03 22:30:30 +0200135 fputc(ch, stdout);
Harald Welteaa3b8672017-02-10 19:21:10 +0100136 board_exec_dbg_cmd(ch);
Harald Welte006b16d2016-12-22 21:29:10 +0100137}
Harald Weltee974fbb2016-10-19 19:36:07 +0200138
Christina Quast32906bb2015-02-24 11:35:19 +0100139/*------------------------------------------------------------------------------
Christina Quast95d66162015-04-09 22:38:47 +0200140 * Main
Christina Quast32906bb2015-02-24 11:35:19 +0100141 *------------------------------------------------------------------------------*/
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100142#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
143extern int main(void)
Christina Quast32906bb2015-02-24 11:35:19 +0100144{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100145 uint8_t isUsbConnected = 0;
146 enum confNum last_simtrace_config = simtrace_config;
147 unsigned int i = 0;
Christina Quast1161b272015-02-25 14:15:57 +0100148
Harald Welteabba8a82017-03-06 16:58:00 +0100149 led_init();
Kévin Redona1579ff2019-02-07 17:53:18 +0100150 led_blink(LED_RED, BLINK_ALWAYS_ON);
151 led_blink(LED_GREEN, BLINK_ALWAYS_ON);
Christina Quast32906bb2015-02-24 11:35:19 +0100152
Kévin Redond8ebd6a2018-07-28 19:04:23 +0200153 /* Enable watchdog for 2000ms, with no window */
Harald Welte45ebe452017-03-03 19:01:53 +0100154 WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT |
Kévin Redond8ebd6a2018-07-28 19:04:23 +0200155 (WDT_GetPeriod(2000) << 16) | WDT_GetPeriod(2000));
Christina Quast32906bb2015-02-24 11:35:19 +0100156
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100157 PIO_InitializeInterrupts(0);
Christina Quast32906bb2015-02-24 11:35:19 +0100158
Harald Weltee3b2de42020-01-11 12:31:51 +0100159 print_banner();
Harald Welteaa3b8672017-02-10 19:21:10 +0100160 board_main_top();
Harald Welte67322482017-02-04 14:43:41 +0100161
Harald Welteec0837c2017-03-03 01:33:24 +0100162 TRACE_INFO("USB init...\n\r");
Harald Weltee07aed62016-12-22 22:32:15 +0100163 SIMtrace_USB_Initialize();
164
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100165 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
Harald Welte45ebe452017-03-03 19:01:53 +0100166 WDT_Restart(WDT);
Harald Welte006b16d2016-12-22 21:29:10 +0100167 check_exec_dbg_cmd();
Harald Welte67415e32016-09-01 20:57:56 +0200168#if 0
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100169 if (i >= MAX_USB_ITER * 3) {
170 TRACE_ERROR("Resetting board (USB could "
Harald Welteec0837c2017-03-03 01:33:24 +0100171 "not be configured)\n\r");
Harald Weltef415d712017-03-03 01:51:43 +0100172 USBD_Disconnect();
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100173 NVIC_SystemReset();
174 }
Harald Welte67415e32016-09-01 20:57:56 +0200175#endif
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100176 i++;
177 }
Christina Quastde688672015-04-12 15:20:57 +0200178
Harald Welteec0837c2017-03-03 01:33:24 +0100179 TRACE_INFO("calling configure of all configurations...\n\r");
Harald Welte34200e42020-01-11 12:50:32 +0100180 for (i = 1; i < ARRAY_SIZE(config_func_ptrs); i++) {
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100181 if (config_func_ptrs[i].configure)
182 config_func_ptrs[i].configure();
183 }
Christina Quast95d66162015-04-09 22:38:47 +0200184
Harald Welteec0837c2017-03-03 01:33:24 +0100185 TRACE_INFO("calling init of config %u...\n\r", simtrace_config);
Kévin Redon7d5d0112018-09-06 22:46:54 +0200186 if (config_func_ptrs[simtrace_config].init) {
187 config_func_ptrs[simtrace_config].init();
188 }
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100189 last_simtrace_config = simtrace_config;
Christina Quast95d66162015-04-09 22:38:47 +0200190
Harald Welteec0837c2017-03-03 01:33:24 +0100191 TRACE_INFO("entering main loop...\n\r");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100192 while (1) {
Harald Welte45ebe452017-03-03 19:01:53 +0100193 WDT_Restart(WDT);
Harald Welte04e37a82016-03-20 15:15:34 +0100194#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100195 const char rotor[] = { '-', '\\', '|', '/' };
196 putchar('\b');
197 putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
Harald Welte04e37a82016-03-20 15:15:34 +0100198#endif
Harald Welte006b16d2016-12-22 21:29:10 +0100199 check_exec_dbg_cmd();
Harald Welte987f59a2017-02-04 12:34:35 +0100200 osmo_timers_prepare();
201 osmo_timers_update();
Christina Quast1161b272015-02-25 14:15:57 +0100202
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100203 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
Christina Quast1161b272015-02-25 14:15:57 +0100204
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100205 if (isUsbConnected) {
206 isUsbConnected = 0;
207 }
208 } else if (isUsbConnected == 0) {
Harald Welteec0837c2017-03-03 01:33:24 +0100209 TRACE_INFO("USB is now configured\n\r");
Christina Quast1161b272015-02-25 14:15:57 +0100210
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100211 isUsbConnected = 1;
212 }
213 if (last_simtrace_config != simtrace_config) {
Harald Welteec0837c2017-03-03 01:33:24 +0100214 TRACE_INFO("USB config chg %u -> %u\n\r",
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100215 last_simtrace_config, simtrace_config);
Kévin Redon7d5d0112018-09-06 22:46:54 +0200216 if (config_func_ptrs[last_simtrace_config].exit) {
217 config_func_ptrs[last_simtrace_config].exit();
218 }
219 if (config_func_ptrs[simtrace_config].init) {
220 config_func_ptrs[simtrace_config].init();
221 }
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100222 last_simtrace_config = simtrace_config;
223 } else {
Kévin Redon7d5d0112018-09-06 22:46:54 +0200224 if (config_func_ptrs[simtrace_config].run) {
225 config_func_ptrs[simtrace_config].run();
226 }
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100227 }
228 }
Christina Quast32906bb2015-02-24 11:35:19 +0100229}