Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 24 | #include <openbsc/gsm_data.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 25 | #include <openbsc/gsm_utils.h> |
Holger Hans Peter Freyther | 3c7caf1 | 2009-10-28 09:12:43 +0100 | [diff] [blame] | 26 | #include <execinfo.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Holger Hans Peter Freyther | 3c7caf1 | 2009-10-28 09:12:43 +0100 | [diff] [blame] | 29 | #include <stdio.h> |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 30 | #include <errno.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 31 | |
| 32 | /* GSM 03.38 6.2.1 Charachter packing */ |
| 33 | int gsm_7bit_decode(char *text, const u_int8_t *user_data, u_int8_t length) |
| 34 | { |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 35 | int i = 0; |
| 36 | int l = 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 37 | |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 38 | /* 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; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 49 | } |
| 50 | |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 51 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 52 | /* GSM 03.38 6.2.1 Charachter packing */ |
| 53 | int gsm_7bit_encode(u_int8_t *result, const char *data) |
| 54 | { |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 55 | int i,j = 0; |
| 56 | unsigned char ch1, ch2; |
| 57 | int shift = 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 58 | |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 59 | for ( i=0; i<strlen(data); i++ ) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 60 | |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 61 | ch1 = data[i] & 0x7F; |
| 62 | ch1 = ch1 >> shift; |
| 63 | ch2 = data[(i+1)] & 0x7F; |
| 64 | ch2 = ch2 << (7-shift); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 65 | |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 66 | ch1 = ch1 | ch2; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 67 | |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 68 | result[j++] = ch1; |
| 69 | |
| 70 | shift++; |
| 71 | |
| 72 | if ((shift == 7) && (i+1<strlen(data))) { |
| 73 | shift = 0; |
| 74 | i++; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 78 | return i; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 79 | } |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 80 | |
| 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 */ |
| 83 | int 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; |
Harald Welte | a0d86af | 2009-12-12 14:21:16 +0100 | [diff] [blame] | 93 | else { |
| 94 | /* we are guaranteed to have (5 <= dbm < 39) */ |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 95 | return 2 + ((39 - dbm) / 2); |
Harald Welte | a0d86af | 2009-12-12 14:21:16 +0100 | [diff] [blame] | 96 | } |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 97 | break; |
| 98 | case GSM_BAND_1800: |
| 99 | if (dbm >= 36) |
| 100 | return 29; |
| 101 | else if (dbm >= 34) |
| 102 | return 30; |
| 103 | else if (dbm >= 32) |
| 104 | return 31; |
Harald Welte | a0d86af | 2009-12-12 14:21:16 +0100 | [diff] [blame] | 105 | else if (dbm == 31) |
| 106 | return 0; |
| 107 | else { |
| 108 | /* we are guaranteed to have (0 <= dbm < 31) */ |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 109 | return (30 - dbm) / 2; |
Harald Welte | a0d86af | 2009-12-12 14:21:16 +0100 | [diff] [blame] | 110 | } |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 111 | break; |
| 112 | case GSM_BAND_1900: |
| 113 | if (dbm >= 33) |
| 114 | return 30; |
| 115 | else if (dbm >= 32) |
| 116 | return 31; |
Harald Welte | a0d86af | 2009-12-12 14:21:16 +0100 | [diff] [blame] | 117 | else if (dbm == 31) |
| 118 | return 0; |
| 119 | else { |
| 120 | /* we are guaranteed to have (0 <= dbm < 31) */ |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 121 | return (30 - dbm) / 2; |
Harald Welte | a0d86af | 2009-12-12 14:21:16 +0100 | [diff] [blame] | 122 | } |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 123 | break; |
| 124 | } |
| 125 | return -EINVAL; |
| 126 | } |
| 127 | |
| 128 | int ms_pwr_dbm(enum gsm_band band, u_int8_t lvl) |
| 129 | { |
| 130 | lvl &= 0x1f; |
| 131 | |
| 132 | switch (band) { |
| 133 | case GSM_BAND_400: |
| 134 | case GSM_BAND_900: |
| 135 | case GSM_BAND_850: |
| 136 | if (lvl < 2) |
| 137 | return 39; |
| 138 | else if (lvl < 20) |
| 139 | return 39 - ((lvl - 2) * 2) ; |
| 140 | else |
| 141 | return 5; |
| 142 | break; |
| 143 | case GSM_BAND_1800: |
| 144 | if (lvl < 16) |
| 145 | return 30 - (lvl * 2); |
| 146 | else if (lvl < 29) |
| 147 | return 0; |
| 148 | else |
| 149 | return 36 - ((lvl - 29) * 2); |
| 150 | break; |
| 151 | case GSM_BAND_1900: |
| 152 | if (lvl < 16) |
| 153 | return 30 - (lvl * 2); |
| 154 | else if (lvl < 30) |
| 155 | return -EINVAL; |
| 156 | else |
| 157 | return 33 - (lvl - 30); |
| 158 | break; |
| 159 | } |
| 160 | return -EINVAL; |
| 161 | } |
| 162 | |
Harald Welte | 1780cb8 | 2009-12-12 15:37:54 +0100 | [diff] [blame] | 163 | /* According to TS 08.05 Chapter 8.1.4 */ |
| 164 | int rxlev2dbm(u_int8_t rxlev) |
| 165 | { |
| 166 | if (rxlev > 63) |
| 167 | rxlev = 63; |
| 168 | |
| 169 | return -110 + rxlev; |
| 170 | } |
| 171 | |
| 172 | /* According to TS 08.05 Chapter 8.1.4 */ |
| 173 | u_int8_t dbm2rxlev(int dbm) |
| 174 | { |
| 175 | int rxlev = dbm + 110; |
| 176 | |
| 177 | if (rxlev > 63) |
| 178 | rxlev = 63; |
| 179 | else if (rxlev < 0) |
| 180 | rxlev = 0; |
| 181 | |
| 182 | return rxlev; |
| 183 | } |
| 184 | |
Holger Hans Peter Freyther | 3c7caf1 | 2009-10-28 09:12:43 +0100 | [diff] [blame] | 185 | void generate_backtrace() |
| 186 | { |
| 187 | int i, nptrs; |
| 188 | void *buffer[100]; |
| 189 | char **strings; |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 190 | |
Holger Hans Peter Freyther | 3c7caf1 | 2009-10-28 09:12:43 +0100 | [diff] [blame] | 191 | nptrs = backtrace(buffer, ARRAY_SIZE(buffer)); |
| 192 | printf("backtrace() returned %d addresses\n", nptrs); |
| 193 | |
| 194 | strings = backtrace_symbols(buffer, nptrs); |
| 195 | if (!strings) |
| 196 | return; |
| 197 | |
| 198 | for (i = 1; i < nptrs; i++) |
| 199 | printf("%s\n", strings[i]); |
| 200 | |
| 201 | free(strings); |
| 202 | } |