aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/libboard/qmod/source/sim_switch.c
blob: 0b829b6c1bc63b7c7701c9efebc3677c25bbbe37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* Code to switch between local (physical) and remote (emulated) SIM
 *
 * (C) 2015-2017 by Harald Welte <hwelte@hmw-consulting.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
#include "board.h"
#include "trace.h"
#include "led.h"
#include "sim_switch.h"

#ifdef PIN_SIM_SWITCH1
static const Pin pin_conn_usim1 = {PIO_PA20, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
#endif
#ifdef PIN_SIM_SWITCH2
static const Pin pin_conn_usim2 = {PIO_PA28, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
#endif

static int initialized = 0;

int sim_switch_use_physical(unsigned int nr, int physical)
{
	const Pin *pin;
	enum led led;

	if (!initialized) {
		TRACE_ERROR("Somebody forgot to call sim_switch_init()\r\n");
		sim_switch_init();
	}

	TRACE_INFO("Modem %d: %s SIM\n\r", nr,
		   physical ? "physical" : "virtual");

	switch (nr) {
#ifdef PIN_SIM_SWITCH1
	case 0:
		pin = &pin_conn_usim1;
		led = LED_USIM1;
		break;
#endif
#ifdef PIN_SIM_SWITCH2
	case 1:
		pin = &pin_conn_usim2;
		led = LED_USIM2;
		break;
#endif
	default:
		TRACE_ERROR("Invalid SIM%u\n\r", nr);
		return -1;
	}

	if (physical) {
		TRACE_INFO("%u: Use local/physical SIM\r\n", nr);
		PIO_Clear(pin);
		led_blink(led, BLINK_ALWAYS_ON);
	} else {
		TRACE_INFO("%u: Use remote/emulated SIM\r\n", nr);
		PIO_Set(pin);
		led_blink(led, BLINK_ALWAYS_OFF);
	}

	return 0;
}

int sim_switch_init(void)
{
	int num_switch = 0;
#ifdef PIN_SIM_SWITCH1
	PIO_Configure(&pin_conn_usim1, 1);
	num_switch++;
#endif
#ifdef PIN_SIM_SWITCH2
	PIO_Configure(&pin_conn_usim2, 1);
	num_switch++;
#endif
	initialized = 1;
	return num_switch;
}