blob: ddfd7f3de5875a9b8d257ea802c035a490336110 [file] [log] [blame]
Holger Freyther76c95692009-02-17 20:31:30 +00001/*
2 * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de>
3 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte7e310b12009-03-30 20:56:32 +00004 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
Holger Freyther76c95692009-02-17 20:31:30 +00005 *
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
Harald Welte66b6a8d2009-08-09 14:45:18 +020024#include <openbsc/gsm_data.h>
Holger Freyther76c95692009-02-17 20:31:30 +000025#include <openbsc/gsm_utils.h>
Holger Hans Peter Freyther25f30ba2009-10-28 09:12:43 +010026#include <execinfo.h>
Harald Welte12247c62009-05-21 07:23:02 +000027#include <stdlib.h>
Holger Freytherce668962009-02-17 23:42:45 +000028#include <string.h>
Holger Hans Peter Freyther25f30ba2009-10-28 09:12:43 +010029#include <stdio.h>
Harald Welte66b6a8d2009-08-09 14:45:18 +020030#include <errno.h>
Holger Freyther76c95692009-02-17 20:31:30 +000031
Holger Freytherce668962009-02-17 23:42:45 +000032/* GSM 03.38 6.2.1 Charachter packing */
Harald Welte7e310b12009-03-30 20:56:32 +000033int gsm_7bit_decode(char *text, const u_int8_t *user_data, u_int8_t length)
Holger Freyther76c95692009-02-17 20:31:30 +000034{
Daniel Willmann6b1e8222009-08-12 21:17:06 +020035 int i = 0;
36 int l = 0;
Holger Freyther76c95692009-02-17 20:31:30 +000037
Daniel Willmann6b1e8222009-08-12 21:17:06 +020038 /* FIXME: We need to account for user data headers here */
39 i += l;
40 for (; i < length; i ++)
41 *(text ++) =
42 ((user_data[(i * 7 + 7) >> 3] <<
43 (7 - ((i * 7 + 7) & 7))) |
44 (user_data[(i * 7) >> 3] >>
45 ((i * 7) & 7))) & 0x7f;
46 *text = '\0';
47
48 return i - l;
Holger Freyther76c95692009-02-17 20:31:30 +000049}
Holger Freytherce668962009-02-17 23:42:45 +000050
Daniel Willmann6b1e8222009-08-12 21:17:06 +020051
Holger Freytherce668962009-02-17 23:42:45 +000052/* GSM 03.38 6.2.1 Charachter packing */
Harald Welte7e310b12009-03-30 20:56:32 +000053int gsm_7bit_encode(u_int8_t *result, const char *data)
Holger Freytherce668962009-02-17 23:42:45 +000054{
Daniel Willmann6b1e8222009-08-12 21:17:06 +020055 int i,j = 0;
56 unsigned char ch1, ch2;
57 int shift = 0;
Harald Welte93d93032009-03-30 09:11:45 +000058
Daniel Willmann6b1e8222009-08-12 21:17:06 +020059 for ( i=0; i<strlen(data); i++ ) {
Holger Freytherce668962009-02-17 23:42:45 +000060
Daniel Willmann6b1e8222009-08-12 21:17:06 +020061 ch1 = data[i] & 0x7F;
62 ch1 = ch1 >> shift;
63 ch2 = data[(i+1)] & 0x7F;
64 ch2 = ch2 << (7-shift);
Holger Freytherce668962009-02-17 23:42:45 +000065
Daniel Willmann6b1e8222009-08-12 21:17:06 +020066 ch1 = ch1 | ch2;
Holger Freytherce668962009-02-17 23:42:45 +000067
Daniel Willmann6b1e8222009-08-12 21:17:06 +020068 result[j++] = ch1;
69
70 shift++;
71
72 if ((shift == 7) && (i+1<strlen(data))) {
73 shift = 0;
74 i++;
Holger Freytherce668962009-02-17 23:42:45 +000075 }
76 }
77
Daniel Willmann6b1e8222009-08-12 21:17:06 +020078 return i;
Holger Freytherce668962009-02-17 23:42:45 +000079}
Harald Welte66b6a8d2009-08-09 14:45:18 +020080
81/* determine power control level for given dBm value, as indicated
82 * by the tables in chapter 4.1.1 of GSM TS 05.05 */
83int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm)
84{
85 switch (band) {
86 case GSM_BAND_400:
87 case GSM_BAND_900:
88 case GSM_BAND_850:
89 if (dbm >= 39)
90 return 0;
91 else if (dbm < 5)
92 return 19;
93 else
94 return 2 + ((39 - dbm) / 2);
95 break;
96 case GSM_BAND_1800:
97 if (dbm >= 36)
98 return 29;
99 else if (dbm >= 34)
100 return 30;
101 else if (dbm >= 32)
102 return 31;
103 else
104 return (30 - dbm) / 2;
105 break;
106 case GSM_BAND_1900:
107 if (dbm >= 33)
108 return 30;
109 else if (dbm >= 32)
110 return 31;
111 else
112 return (30 - dbm) / 2;
113 break;
114 }
115 return -EINVAL;
116}
117
118int ms_pwr_dbm(enum gsm_band band, u_int8_t lvl)
119{
120 lvl &= 0x1f;
121
122 switch (band) {
123 case GSM_BAND_400:
124 case GSM_BAND_900:
125 case GSM_BAND_850:
126 if (lvl < 2)
127 return 39;
128 else if (lvl < 20)
129 return 39 - ((lvl - 2) * 2) ;
130 else
131 return 5;
132 break;
133 case GSM_BAND_1800:
134 if (lvl < 16)
135 return 30 - (lvl * 2);
136 else if (lvl < 29)
137 return 0;
138 else
139 return 36 - ((lvl - 29) * 2);
140 break;
141 case GSM_BAND_1900:
142 if (lvl < 16)
143 return 30 - (lvl * 2);
144 else if (lvl < 30)
145 return -EINVAL;
146 else
147 return 33 - (lvl - 30);
148 break;
149 }
150 return -EINVAL;
151}
152
Holger Hans Peter Freyther25f30ba2009-10-28 09:12:43 +0100153void generate_backtrace()
154{
155 int i, nptrs;
156 void *buffer[100];
157 char **strings;
Harald Welte66b6a8d2009-08-09 14:45:18 +0200158
Holger Hans Peter Freyther25f30ba2009-10-28 09:12:43 +0100159 nptrs = backtrace(buffer, ARRAY_SIZE(buffer));
160 printf("backtrace() returned %d addresses\n", nptrs);
161
162 strings = backtrace_symbols(buffer, nptrs);
163 if (!strings)
164 return;
165
166 for (i = 1; i < nptrs; i++)
167 printf("%s\n", strings[i]);
168
169 free(strings);
170}