blob: de18dba266bd80bf6454d5b80ce95d8bd55c8751 [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>
Harald Welte12247c62009-05-21 07:23:02 +000026#include <stdlib.h>
Holger Freytherce668962009-02-17 23:42:45 +000027#include <string.h>
Harald Welte66b6a8d2009-08-09 14:45:18 +020028#include <errno.h>
Holger Freyther76c95692009-02-17 20:31:30 +000029
Holger Freytherce668962009-02-17 23:42:45 +000030/* GSM 03.38 6.2.1 Charachter packing */
Harald Welte7e310b12009-03-30 20:56:32 +000031int gsm_7bit_decode(char *text, const u_int8_t *user_data, u_int8_t length)
Holger Freyther76c95692009-02-17 20:31:30 +000032{
Daniel Willmann6b1e8222009-08-12 21:17:06 +020033 int i = 0;
34 int l = 0;
Holger Freyther76c95692009-02-17 20:31:30 +000035
Daniel Willmann6b1e8222009-08-12 21:17:06 +020036 /* FIXME: We need to account for user data headers here */
37 i += l;
38 for (; i < length; i ++)
39 *(text ++) =
40 ((user_data[(i * 7 + 7) >> 3] <<
41 (7 - ((i * 7 + 7) & 7))) |
42 (user_data[(i * 7) >> 3] >>
43 ((i * 7) & 7))) & 0x7f;
44 *text = '\0';
45
46 return i - l;
Holger Freyther76c95692009-02-17 20:31:30 +000047}
Holger Freytherce668962009-02-17 23:42:45 +000048
Daniel Willmann6b1e8222009-08-12 21:17:06 +020049
Holger Freytherce668962009-02-17 23:42:45 +000050/* GSM 03.38 6.2.1 Charachter packing */
Harald Welte7e310b12009-03-30 20:56:32 +000051int gsm_7bit_encode(u_int8_t *result, const char *data)
Holger Freytherce668962009-02-17 23:42:45 +000052{
Daniel Willmann6b1e8222009-08-12 21:17:06 +020053 int i,j = 0;
54 unsigned char ch1, ch2;
55 int shift = 0;
Harald Welte93d93032009-03-30 09:11:45 +000056
Daniel Willmann6b1e8222009-08-12 21:17:06 +020057 for ( i=0; i<strlen(data); i++ ) {
Holger Freytherce668962009-02-17 23:42:45 +000058
Daniel Willmann6b1e8222009-08-12 21:17:06 +020059 ch1 = data[i] & 0x7F;
60 ch1 = ch1 >> shift;
61 ch2 = data[(i+1)] & 0x7F;
62 ch2 = ch2 << (7-shift);
Holger Freytherce668962009-02-17 23:42:45 +000063
Daniel Willmann6b1e8222009-08-12 21:17:06 +020064 ch1 = ch1 | ch2;
Holger Freytherce668962009-02-17 23:42:45 +000065
Daniel Willmann6b1e8222009-08-12 21:17:06 +020066 result[j++] = ch1;
67
68 shift++;
69
70 if ((shift == 7) && (i+1<strlen(data))) {
71 shift = 0;
72 i++;
Holger Freytherce668962009-02-17 23:42:45 +000073 }
74 }
75
Daniel Willmann6b1e8222009-08-12 21:17:06 +020076 return i;
Holger Freytherce668962009-02-17 23:42:45 +000077}
Harald Welte66b6a8d2009-08-09 14:45:18 +020078
79/* determine power control level for given dBm value, as indicated
80 * by the tables in chapter 4.1.1 of GSM TS 05.05 */
81int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm)
82{
83 switch (band) {
84 case GSM_BAND_400:
85 case GSM_BAND_900:
86 case GSM_BAND_850:
87 if (dbm >= 39)
88 return 0;
89 else if (dbm < 5)
90 return 19;
91 else
92 return 2 + ((39 - dbm) / 2);
93 break;
94 case GSM_BAND_1800:
95 if (dbm >= 36)
96 return 29;
97 else if (dbm >= 34)
98 return 30;
99 else if (dbm >= 32)
100 return 31;
101 else
102 return (30 - dbm) / 2;
103 break;
104 case GSM_BAND_1900:
105 if (dbm >= 33)
106 return 30;
107 else if (dbm >= 32)
108 return 31;
109 else
110 return (30 - dbm) / 2;
111 break;
112 }
113 return -EINVAL;
114}
115
116int ms_pwr_dbm(enum gsm_band band, u_int8_t lvl)
117{
118 lvl &= 0x1f;
119
120 switch (band) {
121 case GSM_BAND_400:
122 case GSM_BAND_900:
123 case GSM_BAND_850:
124 if (lvl < 2)
125 return 39;
126 else if (lvl < 20)
127 return 39 - ((lvl - 2) * 2) ;
128 else
129 return 5;
130 break;
131 case GSM_BAND_1800:
132 if (lvl < 16)
133 return 30 - (lvl * 2);
134 else if (lvl < 29)
135 return 0;
136 else
137 return 36 - ((lvl - 29) * 2);
138 break;
139 case GSM_BAND_1900:
140 if (lvl < 16)
141 return 30 - (lvl * 2);
142 else if (lvl < 30)
143 return -EINVAL;
144 else
145 return 33 - (lvl - 30);
146 break;
147 }
148 return -EINVAL;
149}
150
151