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> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 28 | #include <errno.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 29 | |
| 30 | /* GSM 03.38 6.2.1 Charachter packing */ |
| 31 | int gsm_7bit_decode(char *text, const u_int8_t *user_data, u_int8_t length) |
| 32 | { |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 33 | int i = 0; |
| 34 | int l = 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 35 | |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 36 | /* 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; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 47 | } |
| 48 | |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 49 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 50 | /* GSM 03.38 6.2.1 Charachter packing */ |
| 51 | int gsm_7bit_encode(u_int8_t *result, const char *data) |
| 52 | { |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 53 | int i,j = 0; |
| 54 | unsigned char ch1, ch2; |
| 55 | int shift = 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 56 | |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 57 | for ( i=0; i<strlen(data); i++ ) { |
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 | ch1 = data[i] & 0x7F; |
| 60 | ch1 = ch1 >> shift; |
| 61 | ch2 = data[(i+1)] & 0x7F; |
| 62 | ch2 = ch2 << (7-shift); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 63 | |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 64 | ch1 = ch1 | ch2; |
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 | result[j++] = ch1; |
| 67 | |
| 68 | shift++; |
| 69 | |
| 70 | if ((shift == 7) && (i+1<strlen(data))) { |
| 71 | shift = 0; |
| 72 | i++; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Daniel Willmann | 8f31ed9 | 2009-08-12 21:17:06 +0200 | [diff] [blame] | 76 | return i; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 77 | } |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 78 | |
| 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 */ |
| 81 | int 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 | |
| 116 | int 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 | |