blob: f6a5ab59f9c00cd48a27c8b0142a39da22d037cc [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 Welte3bafe432016-03-20 16:43:12 +010042 /* Interrupt handler for USART1 */
43 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,
56 },
Harald Welte2fb59962016-02-28 12:34:26 +010057#endif
58#ifdef HAVE_CCID
Harald Weltefefd5712015-11-07 18:19:11 +010059 [CFG_NUM_CCID] = {
60 .configure = CCID_configure,
61 .init = CCID_init,
62 .exit = CCID_exit,
63 .run = CCID_run,
64 },
Harald Welte2fb59962016-02-28 12:34:26 +010065#endif
66#ifdef HAVE_CARDEM
Harald Weltefefd5712015-11-07 18:19:11 +010067 [CFG_NUM_PHONE] = {
Harald Welte2a6d3af2016-02-28 19:29:14 +010068 .configure = mode_cardemu_configure,
69 .init = mode_cardemu_init,
70 .exit = mode_cardemu_exit,
71 .run = mode_cardemu_run,
Harald Welte3bafe432016-03-20 16:43:12 +010072 .usart0_irq = mode_cardemu_usart0_irq,
73 .usart1_irq = mode_cardemu_usart1_irq,
Harald Weltefefd5712015-11-07 18:19:11 +010074 },
Harald Welte2fb59962016-02-28 12:34:26 +010075#endif
76#ifdef HAVE_MITM
Harald Weltefefd5712015-11-07 18:19:11 +010077 [CFG_NUM_MITM] = {
78 .configure = MITM_configure,
79 .init = MITM_init,
80 .exit = MITM_exit,
Harald Welte7dd3dfd2016-03-03 12:32:04 +010081 .run = MITM_run,
Harald Weltefefd5712015-11-07 18:19:11 +010082 },
Harald Welte2fb59962016-02-28 12:34:26 +010083#endif
Christina Quastfb524b92015-02-27 13:39:45 +010084};
85
Christina Quastfb524b92015-02-27 13:39:45 +010086/*------------------------------------------------------------------------------
87 * Internal variables
88 *------------------------------------------------------------------------------*/
Harald Welte2fb59962016-02-28 12:34:26 +010089#if defined(HAVE_SNIFFER)
Harald Welte8d6a5d82015-11-07 18:27:05 +010090static volatile enum confNum simtrace_config = CFG_NUM_SNIFF;
Harald Welte2fb59962016-02-28 12:34:26 +010091#elif defined(HAVE_CARDEM)
92static volatile enum confNum simtrace_config = CFG_NUM_PHONE;
93#elif defined(HAVE_CCID)
94static volatile enum confNum simtrace_config = CFG_NUM_CCID;
95#endif
96
Harald Welte8d6a5d82015-11-07 18:27:05 +010097/*----------------------------------------------------------------------------
98 * Callbacks
99 *----------------------------------------------------------------------------*/
100
101void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum)
102{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100103 TRACE_INFO_WP("cfgChanged%d ", cfgnum);
Kévin Redon7d5d0112018-09-06 22:46:54 +0200104 if (cfgnum < ARRAY_SIZE(config_func_ptrs)) {
105 simtrace_config = cfgnum;
106 } else {
107 TRACE_ERROR("trying to set out of bounds config %u\r\n", cfgnum);
108 }
Harald Welte8d6a5d82015-11-07 18:27:05 +0100109}
Christina Quast32906bb2015-02-24 11:35:19 +0100110
Harald Welte3bafe432016-03-20 16:43:12 +0100111void USART1_IrqHandler(void)
112{
Harald Welte2afd57f2017-11-28 22:52:56 +0100113 if (config_func_ptrs[simtrace_config].usart1_irq)
114 config_func_ptrs[simtrace_config].usart1_irq();
Harald Welte3bafe432016-03-20 16:43:12 +0100115}
116
117void USART0_IrqHandler(void)
118{
Harald Welte2afd57f2017-11-28 22:52:56 +0100119 if (config_func_ptrs[simtrace_config].usart0_irq)
120 config_func_ptrs[simtrace_config].usart0_irq();
Harald Welte3bafe432016-03-20 16:43:12 +0100121}
122
Harald Weltef9a182d2017-01-11 22:22:16 +0100123/* returns '1' in case we should break any endless loop */
Harald Welteaf614792017-01-12 18:59:41 +0100124static void check_exec_dbg_cmd(void)
Harald Welte006b16d2016-12-22 21:29:10 +0100125{
Harald Welteaa3b8672017-02-10 19:21:10 +0100126 int ch;
Harald Welte006b16d2016-12-22 21:29:10 +0100127
128 if (!UART_IsRxReady())
129 return;
130
Harald Welteaa3b8672017-02-10 19:21:10 +0100131 ch = UART_GetChar();
Kévin Redon4f3a0352018-09-06 22:48:11 +0200132 /* We must echo the character to make python fdexpect happy, which we use in factory testing */
Harald Welte46893452018-07-03 22:30:30 +0200133 fputc(ch, stdout);
Harald Welteaa3b8672017-02-10 19:21:10 +0100134 board_exec_dbg_cmd(ch);
Harald Welte006b16d2016-12-22 21:29:10 +0100135}
Harald Weltee974fbb2016-10-19 19:36:07 +0200136
Christina Quast32906bb2015-02-24 11:35:19 +0100137/*------------------------------------------------------------------------------
Christina Quast95d66162015-04-09 22:38:47 +0200138 * Main
Christina Quast32906bb2015-02-24 11:35:19 +0100139 *------------------------------------------------------------------------------*/
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100140#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
141extern int main(void)
Christina Quast32906bb2015-02-24 11:35:19 +0100142{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100143 uint8_t isUsbConnected = 0;
144 enum confNum last_simtrace_config = simtrace_config;
145 unsigned int i = 0;
Christina Quast1161b272015-02-25 14:15:57 +0100146
Harald Welteabba8a82017-03-06 16:58:00 +0100147 led_init();
148 led_blink(LED_RED, BLINK_3O_5F);
Christina Quast32906bb2015-02-24 11:35:19 +0100149
Kévin Redond8ebd6a2018-07-28 19:04:23 +0200150 /* Enable watchdog for 2000ms, with no window */
Harald Welte45ebe452017-03-03 19:01:53 +0100151 WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT |
Kévin Redond8ebd6a2018-07-28 19:04:23 +0200152 (WDT_GetPeriod(2000) << 16) | WDT_GetPeriod(2000));
Christina Quast32906bb2015-02-24 11:35:19 +0100153
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100154 PIO_InitializeInterrupts(0);
Christina Quast32906bb2015-02-24 11:35:19 +0100155
Harald Weltee3b2de42020-01-11 12:31:51 +0100156 print_banner();
Harald Welteaa3b8672017-02-10 19:21:10 +0100157 board_main_top();
Harald Welte67322482017-02-04 14:43:41 +0100158
Harald Welteec0837c2017-03-03 01:33:24 +0100159 TRACE_INFO("USB init...\n\r");
Harald Weltee07aed62016-12-22 22:32:15 +0100160 SIMtrace_USB_Initialize();
161
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100162 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
Harald Welte45ebe452017-03-03 19:01:53 +0100163 WDT_Restart(WDT);
Harald Welte006b16d2016-12-22 21:29:10 +0100164 check_exec_dbg_cmd();
Harald Welte67415e32016-09-01 20:57:56 +0200165#if 0
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100166 if (i >= MAX_USB_ITER * 3) {
167 TRACE_ERROR("Resetting board (USB could "
Harald Welteec0837c2017-03-03 01:33:24 +0100168 "not be configured)\n\r");
Harald Weltef415d712017-03-03 01:51:43 +0100169 USBD_Disconnect();
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100170 NVIC_SystemReset();
171 }
Harald Welte67415e32016-09-01 20:57:56 +0200172#endif
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100173 i++;
174 }
Christina Quastde688672015-04-12 15:20:57 +0200175
Harald Welteec0837c2017-03-03 01:33:24 +0100176 TRACE_INFO("calling configure of all configurations...\n\r");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100177 for (i = 1; i < sizeof(config_func_ptrs) / sizeof(config_func_ptrs[0]);
178 ++i) {
179 if (config_func_ptrs[i].configure)
180 config_func_ptrs[i].configure();
181 }
Christina Quast95d66162015-04-09 22:38:47 +0200182
Harald Welteec0837c2017-03-03 01:33:24 +0100183 TRACE_INFO("calling init of config %u...\n\r", simtrace_config);
Kévin Redon7d5d0112018-09-06 22:46:54 +0200184 if (config_func_ptrs[simtrace_config].init) {
185 config_func_ptrs[simtrace_config].init();
186 }
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100187 last_simtrace_config = simtrace_config;
Christina Quast95d66162015-04-09 22:38:47 +0200188
Harald Welteec0837c2017-03-03 01:33:24 +0100189 TRACE_INFO("entering main loop...\n\r");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100190 while (1) {
Harald Welte45ebe452017-03-03 19:01:53 +0100191 WDT_Restart(WDT);
Harald Welte04e37a82016-03-20 15:15:34 +0100192#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100193 const char rotor[] = { '-', '\\', '|', '/' };
194 putchar('\b');
195 putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
Harald Welte04e37a82016-03-20 15:15:34 +0100196#endif
Harald Welte006b16d2016-12-22 21:29:10 +0100197 check_exec_dbg_cmd();
Harald Welte987f59a2017-02-04 12:34:35 +0100198 osmo_timers_prepare();
199 osmo_timers_update();
Christina Quast1161b272015-02-25 14:15:57 +0100200
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100201 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
Christina Quast1161b272015-02-25 14:15:57 +0100202
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100203 if (isUsbConnected) {
204 isUsbConnected = 0;
205 }
206 } else if (isUsbConnected == 0) {
Harald Welteec0837c2017-03-03 01:33:24 +0100207 TRACE_INFO("USB is now configured\n\r");
Christina Quast1161b272015-02-25 14:15:57 +0100208
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100209 isUsbConnected = 1;
210 }
211 if (last_simtrace_config != simtrace_config) {
Harald Welteec0837c2017-03-03 01:33:24 +0100212 TRACE_INFO("USB config chg %u -> %u\n\r",
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100213 last_simtrace_config, simtrace_config);
Kévin Redon7d5d0112018-09-06 22:46:54 +0200214 if (config_func_ptrs[last_simtrace_config].exit) {
215 config_func_ptrs[last_simtrace_config].exit();
216 }
217 if (config_func_ptrs[simtrace_config].init) {
218 config_func_ptrs[simtrace_config].init();
219 }
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100220 last_simtrace_config = simtrace_config;
221 } else {
Kévin Redon7d5d0112018-09-06 22:46:54 +0200222 if (config_func_ptrs[simtrace_config].run) {
223 config_func_ptrs[simtrace_config].run();
224 }
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100225 }
226 }
Christina Quast32906bb2015-02-24 11:35:19 +0100227}