Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 1 | /* Code to switch between local (physical) and remote (emulated) SIM |
| 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 | */ |
Harald Welte | 5c583d3 | 2017-05-09 06:46:47 +0200 | [diff] [blame] | 19 | #include "board.h" |
| 20 | #include "trace.h" |
Harald Welte | c47fc5f | 2017-05-11 16:51:57 +0200 | [diff] [blame] | 21 | #include "led.h" |
Harald Welte | 5c583d3 | 2017-05-09 06:46:47 +0200 | [diff] [blame] | 22 | #include "sim_switch.h" |
| 23 | |
| 24 | #ifdef PIN_SIM_SWITCH1 |
| 25 | static const Pin pin_conn_usim1 = {PIO_PA20, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}; |
| 26 | #endif |
| 27 | #ifdef PIN_SIM_SWITCH2 |
| 28 | static const Pin pin_conn_usim2 = {PIO_PA28, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}; |
| 29 | #endif |
| 30 | |
| 31 | static int initialized = 0; |
| 32 | |
| 33 | int sim_switch_use_physical(unsigned int nr, int physical) |
| 34 | { |
| 35 | const Pin *pin; |
Harald Welte | c47fc5f | 2017-05-11 16:51:57 +0200 | [diff] [blame] | 36 | enum led led; |
Harald Welte | 5c583d3 | 2017-05-09 06:46:47 +0200 | [diff] [blame] | 37 | |
| 38 | if (!initialized) { |
| 39 | TRACE_ERROR("Somebody forgot to call sim_switch_init()\r\n"); |
| 40 | sim_switch_init(); |
| 41 | } |
| 42 | |
| 43 | TRACE_INFO("Modem %d: %s SIM\n\r", nr, |
| 44 | physical ? "physical" : "virtual"); |
| 45 | |
| 46 | switch (nr) { |
| 47 | #ifdef PIN_SIM_SWITCH1 |
Harald Welte | 44622df | 2017-05-11 00:04:50 +0200 | [diff] [blame] | 48 | case 0: |
Harald Welte | 5c583d3 | 2017-05-09 06:46:47 +0200 | [diff] [blame] | 49 | pin = &pin_conn_usim1; |
Harald Welte | c47fc5f | 2017-05-11 16:51:57 +0200 | [diff] [blame] | 50 | led = LED_USIM1; |
Harald Welte | 5c583d3 | 2017-05-09 06:46:47 +0200 | [diff] [blame] | 51 | break; |
| 52 | #endif |
| 53 | #ifdef PIN_SIM_SWITCH2 |
Harald Welte | 44622df | 2017-05-11 00:04:50 +0200 | [diff] [blame] | 54 | case 1: |
Harald Welte | 5c583d3 | 2017-05-09 06:46:47 +0200 | [diff] [blame] | 55 | pin = &pin_conn_usim2; |
Harald Welte | c47fc5f | 2017-05-11 16:51:57 +0200 | [diff] [blame] | 56 | led = LED_USIM2; |
Harald Welte | 5c583d3 | 2017-05-09 06:46:47 +0200 | [diff] [blame] | 57 | break; |
| 58 | #endif |
| 59 | default: |
| 60 | TRACE_ERROR("Invalid SIM%u\n\r", nr); |
| 61 | return -1; |
| 62 | } |
| 63 | |
Harald Welte | 0709d2d | 2017-05-11 00:06:00 +0200 | [diff] [blame] | 64 | if (physical) { |
| 65 | TRACE_INFO("%u: Use local/physical SIM\r\n", nr); |
Harald Welte | 5c583d3 | 2017-05-09 06:46:47 +0200 | [diff] [blame] | 66 | PIO_Clear(pin); |
Harald Welte | c47fc5f | 2017-05-11 16:51:57 +0200 | [diff] [blame] | 67 | led_blink(led, BLINK_ALWAYS_ON); |
Harald Welte | 0709d2d | 2017-05-11 00:06:00 +0200 | [diff] [blame] | 68 | } else { |
| 69 | TRACE_INFO("%u: Use remote/emulated SIM\r\n", nr); |
Harald Welte | 5c583d3 | 2017-05-09 06:46:47 +0200 | [diff] [blame] | 70 | PIO_Set(pin); |
Harald Welte | c47fc5f | 2017-05-11 16:51:57 +0200 | [diff] [blame] | 71 | led_blink(led, BLINK_ALWAYS_OFF); |
Harald Welte | 0709d2d | 2017-05-11 00:06:00 +0200 | [diff] [blame] | 72 | } |
Harald Welte | 5c583d3 | 2017-05-09 06:46:47 +0200 | [diff] [blame] | 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | int sim_switch_init(void) |
| 78 | { |
| 79 | int num_switch = 0; |
| 80 | #ifdef PIN_SIM_SWITCH1 |
| 81 | PIO_Configure(&pin_conn_usim1, 1); |
| 82 | num_switch++; |
| 83 | #endif |
| 84 | #ifdef PIN_SIM_SWITCH2 |
| 85 | PIO_Configure(&pin_conn_usim2, 1); |
| 86 | num_switch++; |
| 87 | #endif |
Harald Welte | eb50c9f | 2017-11-03 20:50:47 +0100 | [diff] [blame] | 88 | initialized = 1; |
Harald Welte | 5c583d3 | 2017-05-09 06:46:47 +0200 | [diff] [blame] | 89 | return num_switch; |
| 90 | } |