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; |
| 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 | |
| 118 | int 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 Freyther | 3c7caf1 | 2009-10-28 09:12:43 +0100 | [diff] [blame^] | 153 | void generate_backtrace() |
| 154 | { |
| 155 | int i, nptrs; |
| 156 | void *buffer[100]; |
| 157 | char **strings; |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 158 | |
Holger Hans Peter Freyther | 3c7caf1 | 2009-10-28 09:12:43 +0100 | [diff] [blame^] | 159 | 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 | } |