[layer23] Adding LAC and TMSI (both optional) to test card (rplmn)
[osmocom-bb.git] / src / host / layer23 / src / mobile / settings.c
1 /*
2  * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
3  *
4  * All Rights Reserved
5  *
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 along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  */
21
22 #include <stdint.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <osmocore/talloc.h>
26
27 #include <osmocom/bb/common/logging.h>
28 #include <osmocom/bb/common/osmocom_data.h>
29 #include <osmocom/bb/common/networks.h>
30
31 static char *layer2_socket_path = "/tmp/osmocom_l2";
32 static char *sap_socket_path = "/tmp/osmocom_sap";
33
34 int gsm_settings_init(struct osmocom_ms *ms)
35 {
36         struct gsm_settings *set = &ms->settings;
37         struct gsm_support *sup = &ms->support;
38
39         strcpy(set->layer2_socket_path, layer2_socket_path);
40         strcpy(set->sap_socket_path, sap_socket_path);
41
42         /* IMEI */
43         sprintf(set->imei,   "000000000000000");
44         sprintf(set->imeisv, "0000000000000000");
45
46         /* SIM type */
47 #warning TODO: Enable after SIM reader is available in master branch.
48 //      set->sim_type = SIM_TYPE_READER;
49
50         /* test SIM */
51         strcpy(set->test_imsi, "001010000000000");
52         set->test_rplmn_mcc = set->test_rplmn_mnc = 1;
53         set->test_lac = 0x0000;
54         set->test_tmsi = 0xffffffff;
55
56         /* set all supported features */
57         set->sms_ptp = sup->sms_ptp;
58         set->a5_1 = sup->a5_1;
59         set->a5_2 = sup->a5_2;
60         set->a5_3 = sup->a5_3;
61         set->a5_4 = sup->a5_4;
62         set->a5_5 = sup->a5_5;
63         set->a5_6 = sup->a5_6;
64         set->a5_7 = sup->a5_7;
65         set->p_gsm = sup->p_gsm;
66         set->e_gsm = sup->e_gsm;
67         set->r_gsm = sup->r_gsm;
68         set->dcs = sup->dcs;
69         set->class_900 = sup->class_900;
70         set->class_dcs = sup->class_dcs;
71         set->full_v1 = sup->full_v1;
72         set->full_v2 = sup->full_v2;
73         set->full_v3 = sup->full_v3;
74         set->half_v1 = sup->half_v1;
75         set->half_v3 = sup->half_v3;
76         set->ch_cap = sup->ch_cap;
77         set->min_rxlev_db = sup->min_rxlev_db;
78         set->dsc_max = sup->dsc_max;
79
80         if (sup->half_v1 || sup->half_v3)
81                 set->half = 1;
82
83         /* software features */
84         set->cc_dtmf = 1;
85
86         INIT_LLIST_HEAD(&set->abbrev);
87
88         return 0;
89 }
90
91 int gsm_settings_exit(struct osmocom_ms *ms)
92 {
93         struct gsm_settings *set = &ms->settings;
94         struct gsm_settings_abbrev *abbrev;
95
96         while (!llist_empty(&set->abbrev)) {
97                 abbrev = llist_entry(set->abbrev.next,
98                         struct gsm_settings_abbrev, list);
99                 llist_del(&abbrev->list);
100                 talloc_free(abbrev);
101         }
102
103         return 0;
104 }
105
106 char *gsm_check_imei(const char *imei, const char *sv)
107 {
108         int i;
109
110         if (!imei || strlen(imei) != 15)
111                 return "IMEI must have 15 digits!";
112
113         for (i = 0; i < strlen(imei); i++) {
114                 if (imei[i] < '0' || imei[i] > '9')
115                         return "IMEI must have digits 0 to 9 only!";
116         }
117
118         if (!sv || strlen(sv) != 1)
119                 return "Software version must have 1 digit!";
120
121         if (sv[0] < '0' || sv[0] > '9')
122                 return "Software version must have digits 0 to 9 only!";
123
124         return NULL;
125 }
126
127 int gsm_random_imei(struct gsm_settings *set)
128 {
129         int digits = set->imei_random;
130         char rand[16];
131
132         if (digits <= 0)
133                 return 0;
134         if (digits > 15)
135                 digits = 15;
136
137         sprintf(rand, "%08ld", random() % 100000000);
138         sprintf(rand + 8, "%07ld", random() % 10000000);
139
140         strcpy(set->imei + 15 - digits, rand + 15 - digits);
141         strncpy(set->imeisv, set->imei, 15);
142         
143         return 0;
144 }
145