blob: 174b2d49950cb3f32d715682bd2db5a6ac1cce5b [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001/*
2 * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de>
3 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24//#include <openbsc/gsm_data.h>
25#include <osmocore/utils.h>
26#include <osmocore/gsm_utils.h>
27
28#include <stdlib.h>
29#include <stdint.h>
30#include <string.h>
31#include <stdio.h>
32#include <errno.h>
Harald Welteaebe08c2010-03-04 10:39:17 +010033#include <ctype.h>
Harald Welteec8b4502010-02-20 20:34:29 +010034
35#include "../config.h"
36
37/* GSM 03.38 6.2.1 Charachter packing */
38int gsm_7bit_decode(char *text, const uint8_t *user_data, uint8_t length)
39{
40 int i = 0;
41 int l = 0;
42
43 /* FIXME: We need to account for user data headers here */
44 i += l;
45 for (; i < length; i ++)
46 *(text ++) =
47 ((user_data[(i * 7 + 7) >> 3] <<
48 (7 - ((i * 7 + 7) & 7))) |
49 (user_data[(i * 7) >> 3] >>
50 ((i * 7) & 7))) & 0x7f;
51 *text = '\0';
52
53 return i - l;
54}
55
56
57/* GSM 03.38 6.2.1 Charachter packing */
58int gsm_7bit_encode(uint8_t *result, const char *data)
59{
60 int i,j = 0;
61 unsigned char ch1, ch2;
62 int shift = 0;
63
64 for ( i=0; i<strlen(data); i++ ) {
65
66 ch1 = data[i] & 0x7F;
67 ch1 = ch1 >> shift;
68 ch2 = data[(i+1)] & 0x7F;
69 ch2 = ch2 << (7-shift);
70
71 ch1 = ch1 | ch2;
72
73 result[j++] = ch1;
74
75 shift++;
76
77 if ((shift == 7) && (i+1<strlen(data))) {
78 shift = 0;
79 i++;
80 }
81 }
82
83 return i;
84}
85
86/* determine power control level for given dBm value, as indicated
87 * by the tables in chapter 4.1.1 of GSM TS 05.05 */
88int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm)
89{
90 switch (band) {
91 case GSM_BAND_450:
92 case GSM_BAND_480:
93 case GSM_BAND_750:
94 case GSM_BAND_900:
95 case GSM_BAND_810:
96 case GSM_BAND_850:
97 if (dbm >= 39)
98 return 0;
99 else if (dbm < 5)
100 return 19;
101 else {
102 /* we are guaranteed to have (5 <= dbm < 39) */
103 return 2 + ((39 - dbm) / 2);
104 }
105 break;
106 case GSM_BAND_1800:
107 if (dbm >= 36)
108 return 29;
109 else if (dbm >= 34)
110 return 30;
111 else if (dbm >= 32)
112 return 31;
113 else if (dbm == 31)
114 return 0;
115 else {
116 /* we are guaranteed to have (0 <= dbm < 31) */
117 return (30 - dbm) / 2;
118 }
119 break;
120 case GSM_BAND_1900:
121 if (dbm >= 33)
122 return 30;
123 else if (dbm >= 32)
124 return 31;
125 else if (dbm == 31)
126 return 0;
127 else {
128 /* we are guaranteed to have (0 <= dbm < 31) */
129 return (30 - dbm) / 2;
130 }
131 break;
132 }
133 return -EINVAL;
134}
135
136int ms_pwr_dbm(enum gsm_band band, uint8_t lvl)
137{
138 lvl &= 0x1f;
139
140 switch (band) {
141 case GSM_BAND_450:
142 case GSM_BAND_480:
143 case GSM_BAND_750:
144 case GSM_BAND_900:
145 case GSM_BAND_810:
146 case GSM_BAND_850:
147 if (lvl < 2)
148 return 39;
149 else if (lvl < 20)
150 return 39 - ((lvl - 2) * 2) ;
151 else
152 return 5;
153 break;
154 case GSM_BAND_1800:
155 if (lvl < 16)
156 return 30 - (lvl * 2);
157 else if (lvl < 29)
158 return 0;
159 else
160 return 36 - ((lvl - 29) * 2);
161 break;
162 case GSM_BAND_1900:
163 if (lvl < 16)
164 return 30 - (lvl * 2);
165 else if (lvl < 30)
166 return -EINVAL;
167 else
168 return 33 - (lvl - 30);
169 break;
170 }
171 return -EINVAL;
172}
173
174/* According to TS 08.05 Chapter 8.1.4 */
175int rxlev2dbm(uint8_t rxlev)
176{
177 if (rxlev > 63)
178 rxlev = 63;
179
180 return -110 + rxlev;
181}
182
183/* According to TS 08.05 Chapter 8.1.4 */
184uint8_t dbm2rxlev(int dbm)
185{
186 int rxlev = dbm + 110;
187
188 if (rxlev > 63)
189 rxlev = 63;
190 else if (rxlev < 0)
191 rxlev = 0;
192
193 return rxlev;
194}
195
Harald Welteaebe08c2010-03-04 10:39:17 +0100196char *gsm_band_name(enum gsm_band band)
197{
198 switch (band) {
199 case GSM_BAND_450:
200 return "GSM450";
201 case GSM_BAND_480:
202 return "GSM450";
203 case GSM_BAND_750:
204 return "GSM750";
205 case GSM_BAND_810:
206 return "GSM810";
207 case GSM_BAND_850:
208 return "GSM850";
209 case GSM_BAND_900:
210 return "GSM900";
211 case GSM_BAND_1800:
212 return "DCS1800";
213 case GSM_BAND_1900:
214 return "PCS1900";
215 }
216 return "invalid";
217}
218
219enum gsm_band gsm_band_parse(const char* mhz)
220{
221 while (*mhz && !isdigit(*mhz))
222 mhz++;
223
224 if (*mhz == '\0')
225 return -EINVAL;
226
227 switch (atoi(mhz)) {
228 case 450:
229 return GSM_BAND_450;
230 case 480:
231 return GSM_BAND_480;
232 case 750:
233 return GSM_BAND_750;
234 case 810:
235 return GSM_BAND_810;
236 case 850:
237 return GSM_BAND_850;
238 case 900:
239 return GSM_BAND_900;
240 case 1800:
241 return GSM_BAND_1800;
242 case 1900:
243 return GSM_BAND_1900;
244 default:
245 return -EINVAL;
246 }
247}
248
249
Harald Welteec8b4502010-02-20 20:34:29 +0100250#ifdef HAVE_EXECINFO_H
251#include <execinfo.h>
252void generate_backtrace()
253{
254 int i, nptrs;
255 void *buffer[100];
256 char **strings;
257
258 nptrs = backtrace(buffer, ARRAY_SIZE(buffer));
259 printf("backtrace() returned %d addresses\n", nptrs);
260
261 strings = backtrace_symbols(buffer, nptrs);
262 if (!strings)
263 return;
264
265 for (i = 1; i < nptrs; i++)
266 printf("%s\n", strings[i]);
267
268 free(strings);
269}
270#endif