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 | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 24 | //#include <openbsc/gsm_data.h> |
| 25 | #include <osmocore/utils.h> |
| 26 | #include <osmocore/gsm_utils.h> |
| 27 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 28 | #include <stdlib.h> |
Harald Welte | 3c1ac36 | 2010-02-20 19:10:44 +0100 | [diff] [blame] | 29 | #include <stdint.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 30 | #include <string.h> |
Holger Hans Peter Freyther | 3c7caf1 | 2009-10-28 09:12:43 +0100 | [diff] [blame] | 31 | #include <stdio.h> |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 32 | #include <errno.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 33 | |
Harald Welte | c703d02 | 2010-02-20 20:14:01 +0100 | [diff] [blame^] | 34 | #include "../config.h" |
| 35 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 36 | /* GSM 03.38 6.2.1 Charachter packing */ |
Harald Welte | 3c1ac36 | 2010-02-20 19:10:44 +0100 | [diff] [blame] | 37 | int gsm_7bit_decode(char *text, const uint8_t *user_data, uint8_t length) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 38 | { |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 39 | int i = 0; |
| 40 | int l = 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 41 | |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 42 | /* FIXME: We need to account for user data headers here */ |
| 43 | i += l; |
| 44 | for (; i < length; i ++) |
| 45 | *(text ++) = |
| 46 | ((user_data[(i * 7 + 7) >> 3] << |
| 47 | (7 - ((i * 7 + 7) & 7))) | |
| 48 | (user_data[(i * 7) >> 3] >> |
| 49 | ((i * 7) & 7))) & 0x7f; |
| 50 | *text = '\0'; |
| 51 | |
| 52 | return i - l; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 53 | } |
| 54 | |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 55 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 56 | /* GSM 03.38 6.2.1 Charachter packing */ |
Harald Welte | 3c1ac36 | 2010-02-20 19:10:44 +0100 | [diff] [blame] | 57 | int gsm_7bit_encode(uint8_t *result, const char *data) |
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 | int i,j = 0; |
| 60 | unsigned char ch1, ch2; |
| 61 | int shift = 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 62 | |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 63 | for ( i=0; i<strlen(data); i++ ) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 64 | |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 65 | ch1 = data[i] & 0x7F; |
| 66 | ch1 = ch1 >> shift; |
| 67 | ch2 = data[(i+1)] & 0x7F; |
| 68 | ch2 = ch2 << (7-shift); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 69 | |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 70 | ch1 = ch1 | ch2; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 71 | |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 72 | result[j++] = ch1; |
| 73 | |
| 74 | shift++; |
| 75 | |
| 76 | if ((shift == 7) && (i+1<strlen(data))) { |
| 77 | shift = 0; |
| 78 | i++; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 82 | return i; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 83 | } |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 84 | |
| 85 | /* determine power control level for given dBm value, as indicated |
| 86 | * by the tables in chapter 4.1.1 of GSM TS 05.05 */ |
| 87 | int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm) |
| 88 | { |
| 89 | switch (band) { |
Harald Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 90 | case GSM_BAND_450: |
| 91 | case GSM_BAND_480: |
| 92 | case GSM_BAND_750: |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 93 | case GSM_BAND_900: |
Harald Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 94 | case GSM_BAND_810: |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 95 | case GSM_BAND_850: |
| 96 | if (dbm >= 39) |
| 97 | return 0; |
| 98 | else if (dbm < 5) |
| 99 | return 19; |
Harald Welte | a0d86af | 2009-12-12 14:21:16 +0100 | [diff] [blame] | 100 | else { |
| 101 | /* we are guaranteed to have (5 <= dbm < 39) */ |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 102 | return 2 + ((39 - dbm) / 2); |
Harald Welte | a0d86af | 2009-12-12 14:21:16 +0100 | [diff] [blame] | 103 | } |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 104 | break; |
| 105 | case GSM_BAND_1800: |
| 106 | if (dbm >= 36) |
| 107 | return 29; |
| 108 | else if (dbm >= 34) |
| 109 | return 30; |
| 110 | else if (dbm >= 32) |
| 111 | return 31; |
Harald Welte | a0d86af | 2009-12-12 14:21:16 +0100 | [diff] [blame] | 112 | else if (dbm == 31) |
| 113 | return 0; |
| 114 | else { |
| 115 | /* we are guaranteed to have (0 <= dbm < 31) */ |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 116 | return (30 - dbm) / 2; |
Harald Welte | a0d86af | 2009-12-12 14:21:16 +0100 | [diff] [blame] | 117 | } |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 118 | break; |
| 119 | case GSM_BAND_1900: |
| 120 | if (dbm >= 33) |
| 121 | return 30; |
| 122 | else if (dbm >= 32) |
| 123 | return 31; |
Harald Welte | a0d86af | 2009-12-12 14:21:16 +0100 | [diff] [blame] | 124 | else if (dbm == 31) |
| 125 | return 0; |
| 126 | else { |
| 127 | /* we are guaranteed to have (0 <= dbm < 31) */ |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 128 | return (30 - dbm) / 2; |
Harald Welte | a0d86af | 2009-12-12 14:21:16 +0100 | [diff] [blame] | 129 | } |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 130 | break; |
| 131 | } |
| 132 | return -EINVAL; |
| 133 | } |
| 134 | |
Harald Welte | 3c1ac36 | 2010-02-20 19:10:44 +0100 | [diff] [blame] | 135 | int ms_pwr_dbm(enum gsm_band band, uint8_t lvl) |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 136 | { |
| 137 | lvl &= 0x1f; |
| 138 | |
| 139 | switch (band) { |
Harald Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 140 | case GSM_BAND_450: |
| 141 | case GSM_BAND_480: |
| 142 | case GSM_BAND_750: |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 143 | case GSM_BAND_900: |
Harald Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 144 | case GSM_BAND_810: |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 145 | case GSM_BAND_850: |
| 146 | if (lvl < 2) |
| 147 | return 39; |
| 148 | else if (lvl < 20) |
| 149 | return 39 - ((lvl - 2) * 2) ; |
| 150 | else |
| 151 | return 5; |
| 152 | break; |
| 153 | case GSM_BAND_1800: |
| 154 | if (lvl < 16) |
| 155 | return 30 - (lvl * 2); |
| 156 | else if (lvl < 29) |
| 157 | return 0; |
| 158 | else |
| 159 | return 36 - ((lvl - 29) * 2); |
| 160 | break; |
| 161 | case GSM_BAND_1900: |
| 162 | if (lvl < 16) |
| 163 | return 30 - (lvl * 2); |
| 164 | else if (lvl < 30) |
| 165 | return -EINVAL; |
| 166 | else |
| 167 | return 33 - (lvl - 30); |
| 168 | break; |
| 169 | } |
| 170 | return -EINVAL; |
| 171 | } |
| 172 | |
Harald Welte | 1780cb8 | 2009-12-12 15:37:54 +0100 | [diff] [blame] | 173 | /* According to TS 08.05 Chapter 8.1.4 */ |
Harald Welte | 3c1ac36 | 2010-02-20 19:10:44 +0100 | [diff] [blame] | 174 | int rxlev2dbm(uint8_t rxlev) |
Harald Welte | 1780cb8 | 2009-12-12 15:37:54 +0100 | [diff] [blame] | 175 | { |
| 176 | if (rxlev > 63) |
| 177 | rxlev = 63; |
| 178 | |
| 179 | return -110 + rxlev; |
| 180 | } |
| 181 | |
| 182 | /* According to TS 08.05 Chapter 8.1.4 */ |
Harald Welte | 3c1ac36 | 2010-02-20 19:10:44 +0100 | [diff] [blame] | 183 | uint8_t dbm2rxlev(int dbm) |
Harald Welte | 1780cb8 | 2009-12-12 15:37:54 +0100 | [diff] [blame] | 184 | { |
| 185 | int rxlev = dbm + 110; |
| 186 | |
| 187 | if (rxlev > 63) |
| 188 | rxlev = 63; |
| 189 | else if (rxlev < 0) |
| 190 | rxlev = 0; |
| 191 | |
| 192 | return rxlev; |
| 193 | } |
| 194 | |
Harald Welte | c703d02 | 2010-02-20 20:14:01 +0100 | [diff] [blame^] | 195 | #ifdef HAVE_EXECINFO_H |
Harald Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 196 | #include <execinfo.h> |
Holger Hans Peter Freyther | 3c7caf1 | 2009-10-28 09:12:43 +0100 | [diff] [blame] | 197 | void generate_backtrace() |
| 198 | { |
| 199 | int i, nptrs; |
| 200 | void *buffer[100]; |
| 201 | char **strings; |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 202 | |
Holger Hans Peter Freyther | 3c7caf1 | 2009-10-28 09:12:43 +0100 | [diff] [blame] | 203 | nptrs = backtrace(buffer, ARRAY_SIZE(buffer)); |
| 204 | printf("backtrace() returned %d addresses\n", nptrs); |
| 205 | |
| 206 | strings = backtrace_symbols(buffer, nptrs); |
| 207 | if (!strings) |
| 208 | return; |
| 209 | |
| 210 | for (i = 1; i < nptrs; i++) |
| 211 | printf("%s\n", strings[i]); |
| 212 | |
| 213 | free(strings); |
| 214 | } |
Harald Welte | c703d02 | 2010-02-20 20:14:01 +0100 | [diff] [blame^] | 215 | #endif |