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 | { |
| 33 | u_int8_t d_off = 0, b_off = 0; |
| 34 | u_int8_t i; |
| 35 | |
| 36 | for (i=0;i<length;i++) { |
| 37 | text[i] = ((user_data[d_off] + (user_data[d_off+1]<<8)) & (0x7f<<b_off))>>b_off; |
| 38 | b_off += 7; |
| 39 | if (b_off >= 8) { |
| 40 | d_off += 1; |
| 41 | b_off -= 8; |
| 42 | } |
| 43 | } |
| 44 | text[i] = '\0'; |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | /* GSM 03.38 6.2.1 Charachter packing */ |
| 49 | int gsm_7bit_encode(u_int8_t *result, const char *data) |
| 50 | { |
| 51 | int i; |
| 52 | u_int8_t d_off = 0, b_off = 0; |
| 53 | const int length = strlen(data); |
| 54 | int out_length = (length * 8)/7; |
| 55 | |
| 56 | memset(result, 0, out_length); |
| 57 | |
| 58 | for (i = 0; i < length; ++i) { |
| 59 | u_int8_t first = (data[i] & 0x7f) << b_off; |
| 60 | u_int8_t second = (data[i] & 0x7f) >> (8 - b_off); |
| 61 | |
| 62 | result[d_off] |= first; |
| 63 | if (second != 0) |
| 64 | result[d_off + 1] = second; |
| 65 | |
| 66 | b_off += 7; |
| 67 | |
| 68 | if (b_off >= 8) { |
| 69 | d_off += 1; |
| 70 | b_off -= 8; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return out_length; |
| 75 | } |
Harald Welte | c4dcda0 | 2009-08-09 14:45:18 +0200 | [diff] [blame] | 76 | |
| 77 | /* determine power control level for given dBm value, as indicated |
| 78 | * by the tables in chapter 4.1.1 of GSM TS 05.05 */ |
| 79 | int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm) |
| 80 | { |
| 81 | switch (band) { |
| 82 | case GSM_BAND_400: |
| 83 | case GSM_BAND_900: |
| 84 | case GSM_BAND_850: |
| 85 | if (dbm >= 39) |
| 86 | return 0; |
| 87 | else if (dbm < 5) |
| 88 | return 19; |
| 89 | else |
| 90 | return 2 + ((39 - dbm) / 2); |
| 91 | break; |
| 92 | case GSM_BAND_1800: |
| 93 | if (dbm >= 36) |
| 94 | return 29; |
| 95 | else if (dbm >= 34) |
| 96 | return 30; |
| 97 | else if (dbm >= 32) |
| 98 | return 31; |
| 99 | else |
| 100 | return (30 - dbm) / 2; |
| 101 | break; |
| 102 | case GSM_BAND_1900: |
| 103 | if (dbm >= 33) |
| 104 | return 30; |
| 105 | else if (dbm >= 32) |
| 106 | return 31; |
| 107 | else |
| 108 | return (30 - dbm) / 2; |
| 109 | break; |
| 110 | } |
| 111 | return -EINVAL; |
| 112 | } |
| 113 | |
| 114 | int ms_pwr_dbm(enum gsm_band band, u_int8_t lvl) |
| 115 | { |
| 116 | lvl &= 0x1f; |
| 117 | |
| 118 | switch (band) { |
| 119 | case GSM_BAND_400: |
| 120 | case GSM_BAND_900: |
| 121 | case GSM_BAND_850: |
| 122 | if (lvl < 2) |
| 123 | return 39; |
| 124 | else if (lvl < 20) |
| 125 | return 39 - ((lvl - 2) * 2) ; |
| 126 | else |
| 127 | return 5; |
| 128 | break; |
| 129 | case GSM_BAND_1800: |
| 130 | if (lvl < 16) |
| 131 | return 30 - (lvl * 2); |
| 132 | else if (lvl < 29) |
| 133 | return 0; |
| 134 | else |
| 135 | return 36 - ((lvl - 29) * 2); |
| 136 | break; |
| 137 | case GSM_BAND_1900: |
| 138 | if (lvl < 16) |
| 139 | return 30 - (lvl * 2); |
| 140 | else if (lvl < 30) |
| 141 | return -EINVAL; |
| 142 | else |
| 143 | return 33 - (lvl - 30); |
| 144 | break; |
| 145 | } |
| 146 | return -EINVAL; |
| 147 | } |
| 148 | |
| 149 | |