[layer23] DTMF support
[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 int gsm_settings_init(struct osmocom_ms *ms)
32 {
33         struct gsm_settings *set = &ms->settings;
34         struct gsm_support *sup = &ms->support;
35
36         /* IMEI */
37         sprintf(set->imei,   "000000000000000");
38         sprintf(set->imeisv, "0000000000000000");
39
40         /* test sim */
41         strcpy(set->test_imsi, "001010000000000");
42         set->test_rplmn_mcc = set->test_rplmn_mnc = 1;
43
44         /* set all supported features */
45         set->sms_ptp = sup->sms_ptp;
46         set->a5_1 = sup->a5_1;
47         set->a5_2 = sup->a5_2;
48         set->a5_3 = sup->a5_3;
49         set->a5_4 = sup->a5_4;
50         set->a5_5 = sup->a5_5;
51         set->a5_6 = sup->a5_6;
52         set->a5_7 = sup->a5_7;
53         set->p_gsm = sup->p_gsm;
54         set->e_gsm = sup->e_gsm;
55         set->r_gsm = sup->r_gsm;
56         set->dcs = sup->dcs;
57         set->class_900 = sup->class_900;
58         set->class_dcs = sup->class_dcs;
59         set->full_v1 = sup->full_v1;
60         set->full_v2 = sup->full_v2;
61         set->full_v3 = sup->full_v3;
62         set->half_v1 = sup->half_v1;
63         set->half_v3 = sup->half_v3;
64         set->ch_cap = sup->ch_cap;
65         set->min_rxlev_db = sup->min_rxlev_db;
66         set->dsc_max = sup->dsc_max;
67
68         if (sup->half_v1 || sup->half_v3)
69                 set->half = 1;
70
71         /* software features */
72         set->cc_dtmf = 1;
73
74         INIT_LLIST_HEAD(&set->abbrev);
75
76         return 0;
77 }
78
79 int gsm_settings_exit(struct osmocom_ms *ms)
80 {
81         struct gsm_settings *set = &ms->settings;
82         struct gsm_settings_abbrev *abbrev;
83
84         while (!llist_empty(&set->abbrev)) {
85                 abbrev = llist_entry(set->abbrev.next,
86                         struct gsm_settings_abbrev, list);
87                 llist_del(&abbrev->list);
88                 talloc_free(abbrev);
89         }
90
91         return 0;
92 }
93
94 char *gsm_check_imei(const char *imei, const char *sv)
95 {
96         int i;
97
98         if (!imei || strlen(imei) != 15)
99                 return "IMEI must have 15 digits!";
100
101         for (i = 0; i < strlen(imei); i++) {
102                 if (imei[i] < '0' || imei[i] > '9')
103                         return "IMEI must have digits 0 to 9 only!";
104         }
105
106         if (!sv || strlen(sv) != 1)
107                 return "Software version must have 1 digit!";
108
109         if (sv[0] < '0' || sv[0] > '9')
110                 return "Software version must have digits 0 to 9 only!";
111
112         return NULL;
113 }
114
115 int gsm_random_imei(struct gsm_settings *set)
116 {
117         int digits = set->imei_random;
118         char rand[16];
119
120         if (digits <= 0)
121                 return 0;
122         if (digits > 15)
123                 digits = 15;
124
125         sprintf(rand, "%08ld", random() % 100000000);
126         sprintf(rand + 8, "%07ld", random() % 10000000);
127
128         strcpy(set->imei + 15 - digits, rand + 15 - digits);
129         strncpy(set->imeisv, set->imei, 15);
130         
131         return 0;
132 }
133