Harald Welte | 2a8ee52 | 2009-01-05 19:01:01 +0000 | [diff] [blame] | 1 | /* TRAU frame handling according to GSM TS 08.60 */ |
| 2 | |
| 3 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 2a8ee52 | 2009-01-05 19:01:01 +0000 | [diff] [blame] | 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 14 | * GNU Affero General Public License for more details. |
Harald Welte | 2a8ee52 | 2009-01-05 19:01:01 +0000 | [diff] [blame] | 15 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 2a8ee52 | 2009-01-05 19:01:01 +0000 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <unistd.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <errno.h> |
| 26 | |
| 27 | #include <openbsc/trau_frame.h> |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 28 | #include <openbsc/subchan_demux.h> |
Harald Welte | bb8c406 | 2009-02-18 03:24:58 +0000 | [diff] [blame] | 29 | #include <openbsc/debug.h> |
Harald Welte | 2a8ee52 | 2009-01-05 19:01:01 +0000 | [diff] [blame] | 30 | |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 31 | static uint32_t get_bits(const uint8_t *bitbuf, int offset, int num) |
Harald Welte | 2a8ee52 | 2009-01-05 19:01:01 +0000 | [diff] [blame] | 32 | { |
| 33 | int i; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 34 | uint32_t ret = 0; |
Harald Welte | 2a8ee52 | 2009-01-05 19:01:01 +0000 | [diff] [blame] | 35 | |
| 36 | for (i = offset; i < offset + num; i++) { |
| 37 | ret = ret << 1; |
| 38 | if (bitbuf[i]) |
| 39 | ret |= 1; |
| 40 | } |
| 41 | return ret; |
| 42 | } |
| 43 | |
| 44 | /* Decode according to 3.1.1 */ |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 45 | static void decode_fr(struct decoded_trau_frame *fr, const uint8_t *trau_bits) |
Harald Welte | 2a8ee52 | 2009-01-05 19:01:01 +0000 | [diff] [blame] | 46 | { |
| 47 | int i; |
| 48 | int d_idx = 0; |
| 49 | |
| 50 | /* C1 .. C15 */ |
| 51 | memcpy(fr->c_bits+0, trau_bits+17, 15); |
| 52 | /* C16 .. C21 */ |
| 53 | memcpy(fr->c_bits+15, trau_bits+310, 6); |
| 54 | /* T1 .. T4 */ |
| 55 | memcpy(fr->t_bits+0, trau_bits+316, 4); |
| 56 | /* D1 .. D255 */ |
| 57 | for (i = 32; i < 304; i+= 16) { |
| 58 | memcpy(fr->d_bits + d_idx, trau_bits+i+1, 15); |
| 59 | d_idx += 15; |
| 60 | } |
| 61 | /* D256 .. D260 */ |
| 62 | memcpy(fr->d_bits + d_idx, trau_bits + 305, 5); |
| 63 | } |
| 64 | |
Harald Welte | 38e0437 | 2009-01-05 19:42:46 +0000 | [diff] [blame] | 65 | /* Decode according to 3.1.2 */ |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 66 | static void decode_amr(struct decoded_trau_frame *fr, const uint8_t *trau_bits) |
Harald Welte | 2a8ee52 | 2009-01-05 19:01:01 +0000 | [diff] [blame] | 67 | { |
| 68 | int i; |
| 69 | int d_idx = 0; |
| 70 | |
| 71 | /* C1 .. C15 */ |
| 72 | memcpy(fr->c_bits+0, trau_bits+17, 15); |
| 73 | /* C16 .. C25 */ |
| 74 | memcpy(fr->c_bits+15, trau_bits+33, 10); |
| 75 | /* T1 .. T4 */ |
| 76 | memcpy(fr->t_bits+0, trau_bits+316, 4); |
| 77 | /* D1 .. D5 */ |
| 78 | memcpy(fr->d_bits, trau_bits+43, 5); |
| 79 | /* D6 .. D245 */ |
| 80 | for (i = 48; i < 304; i += 16) { |
| 81 | memcpy(fr->d_bits + d_idx, trau_bits+i+1, 15); |
| 82 | d_idx += 15; |
| 83 | } |
| 84 | /* D246 .. D256 */ |
| 85 | memcpy(fr->d_bits + d_idx, trau_bits + 305, 11); |
| 86 | } |
| 87 | |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 88 | int decode_trau_frame(struct decoded_trau_frame *fr, const uint8_t *trau_bits) |
Harald Welte | 2a8ee52 | 2009-01-05 19:01:01 +0000 | [diff] [blame] | 89 | { |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 90 | uint8_t cbits5 = get_bits(trau_bits, 17, 5); |
Harald Welte | 2a8ee52 | 2009-01-05 19:01:01 +0000 | [diff] [blame] | 91 | |
| 92 | switch (cbits5) { |
| 93 | case TRAU_FT_FR_UP: |
| 94 | case TRAU_FT_FR_DOWN: |
Harald Welte | 38e0437 | 2009-01-05 19:42:46 +0000 | [diff] [blame] | 95 | case TRAU_FT_IDLE_UP: |
| 96 | case TRAU_FT_IDLE_DOWN: |
Harald Welte | 2a8ee52 | 2009-01-05 19:01:01 +0000 | [diff] [blame] | 97 | case TRAU_FT_EFR: |
| 98 | decode_fr(fr, trau_bits); |
| 99 | break; |
| 100 | case TRAU_FT_AMR: |
| 101 | decode_amr(fr, trau_bits); |
| 102 | break; |
| 103 | case TRAU_FT_OM_UP: |
| 104 | case TRAU_FT_OM_DOWN: |
| 105 | case TRAU_FT_DATA_UP: |
| 106 | case TRAU_FT_DATA_DOWN: |
| 107 | case TRAU_FT_D145_SYNC: |
| 108 | case TRAU_FT_EDATA: |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 109 | LOGP(DMUX, LOGL_NOTICE, "can't decode unimplemented TRAU " |
| 110 | "Frame Type 0x%02x\n", cbits5); |
Harald Welte | 2a8ee52 | 2009-01-05 19:01:01 +0000 | [diff] [blame] | 111 | return -1; |
| 112 | break; |
| 113 | default: |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 114 | LOGP(DMUX, LOGL_NOTICE, "can't decode unknown TRAU " |
| 115 | "Frame Type 0x%02x\n", cbits5); |
Harald Welte | 2a8ee52 | 2009-01-05 19:01:01 +0000 | [diff] [blame] | 116 | return -1; |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
Harald Welte | 418f5a4 | 2009-01-05 20:12:47 +0000 | [diff] [blame] | 122 | |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 123 | const uint8_t ft_fr_down_bits[] = { 1, 1, 1, 0, 0 }; |
| 124 | const uint8_t ft_idle_down_bits[] = { 0, 1, 1, 1, 0 }; |
Harald Welte | 418f5a4 | 2009-01-05 20:12:47 +0000 | [diff] [blame] | 125 | |
| 126 | /* modify an uplink TRAU frame so we can send it downlink */ |
| 127 | int trau_frame_up2down(struct decoded_trau_frame *fr) |
| 128 | { |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 129 | uint8_t cbits5 = get_bits(fr->c_bits, 0, 5); |
Harald Welte | 418f5a4 | 2009-01-05 20:12:47 +0000 | [diff] [blame] | 130 | |
| 131 | switch (cbits5) { |
| 132 | case TRAU_FT_FR_UP: |
| 133 | memcpy(fr->c_bits, ft_fr_down_bits, 5); |
| 134 | /* clear time alignment */ |
| 135 | memset(fr->c_bits+5, 0, 6); |
| 136 | /* FIXME: SP / BFI in case of DTx */ |
| 137 | /* C12 .. C21 are spare and coded as '1' */ |
Harald Welte | bb8c406 | 2009-02-18 03:24:58 +0000 | [diff] [blame] | 138 | memset(fr->c_bits+11, 0x01, 10); |
Harald Welte | 418f5a4 | 2009-01-05 20:12:47 +0000 | [diff] [blame] | 139 | break; |
| 140 | case TRAU_FT_EFR: |
| 141 | /* clear time alignment */ |
| 142 | memset(fr->c_bits+5, 0, 6); |
Dieter Spaar | f31dd86 | 2011-07-27 23:52:36 +0200 | [diff] [blame^] | 143 | /* set UFE appropriately */ |
| 144 | fr->c_bits[11] = 1; /* C12 (UFE), good frame (TODO) */ |
| 145 | /* C13 .. C15 are spare and coded as '1' */ |
| 146 | memset(fr->c_bits+12, 0x01, 3); |
| 147 | /* SP / BFI in case of DTx */ |
| 148 | fr->c_bits[15] = 1; /* C16 (SP), no DTX (TODO) */ |
| 149 | /* C17 .. C21 are spare and coded as '1' */ |
| 150 | memset(fr->c_bits+16, 0x01, 5); |
Harald Welte | 418f5a4 | 2009-01-05 20:12:47 +0000 | [diff] [blame] | 151 | break; |
| 152 | case TRAU_FT_IDLE_UP: |
| 153 | memcpy(fr->c_bits, ft_idle_down_bits, 5); |
| 154 | /* clear time alignment */ |
| 155 | memset(fr->c_bits+5, 0, 6); |
| 156 | /* FIXME: SP / BFI in case of DTx */ |
| 157 | /* C12 .. C21 are spare and coded as '1' */ |
Harald Welte | bb8c406 | 2009-02-18 03:24:58 +0000 | [diff] [blame] | 158 | memset(fr->c_bits+11, 0x01, 10); |
Harald Welte | 418f5a4 | 2009-01-05 20:12:47 +0000 | [diff] [blame] | 159 | break; |
| 160 | case TRAU_FT_FR_DOWN: |
| 161 | case TRAU_FT_IDLE_DOWN: |
| 162 | case TRAU_FT_OM_DOWN: |
| 163 | case TRAU_FT_DATA_DOWN: |
| 164 | /* we cannot convert a downlink to a downlink frame */ |
| 165 | return -EINVAL; |
| 166 | break; |
| 167 | case TRAU_FT_AMR: |
| 168 | case TRAU_FT_OM_UP: |
| 169 | case TRAU_FT_DATA_UP: |
| 170 | case TRAU_FT_D145_SYNC: |
| 171 | case TRAU_FT_EDATA: |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 172 | LOGP(DMUX, LOGL_NOTICE, "unimplemented TRAU Frame Type " |
| 173 | "0x%02x\n", cbits5); |
Harald Welte | 418f5a4 | 2009-01-05 20:12:47 +0000 | [diff] [blame] | 174 | return -1; |
| 175 | break; |
| 176 | default: |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 177 | LOGP(DMUX, LOGL_NOTICE, "unknown TRAU Frame Type " |
| 178 | "0x%02x\n", cbits5); |
Harald Welte | 418f5a4 | 2009-01-05 20:12:47 +0000 | [diff] [blame] | 179 | return -1; |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | return 0; |
| 184 | |
| 185 | } |
| 186 | |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 187 | static void encode_fr(uint8_t *trau_bits, const struct decoded_trau_frame *fr) |
Harald Welte | 418f5a4 | 2009-01-05 20:12:47 +0000 | [diff] [blame] | 188 | { |
| 189 | int i; |
| 190 | int d_idx = 0; |
| 191 | |
| 192 | trau_bits[16] = 1; |
| 193 | /* C1 .. C15 */ |
| 194 | memcpy(trau_bits+17, fr->c_bits+0, 15); |
| 195 | /* D1 .. D255 */ |
| 196 | for (i = 32; i < 304; i+= 16) { |
| 197 | trau_bits[i] = 1; |
| 198 | memcpy(trau_bits+i+1, fr->d_bits + d_idx, 15); |
| 199 | d_idx += 15; |
| 200 | } |
| 201 | /* D256 .. D260 */ |
| 202 | trau_bits[304] = 1; |
| 203 | memcpy(trau_bits + 305, fr->d_bits + d_idx, 5); |
| 204 | /* C16 .. C21 */ |
| 205 | memcpy(trau_bits+310, fr->c_bits+15, 6); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 206 | |
| 207 | /* FIXME: handle timing adjustment */ |
| 208 | |
Harald Welte | 418f5a4 | 2009-01-05 20:12:47 +0000 | [diff] [blame] | 209 | /* T1 .. T4 */ |
| 210 | memcpy(trau_bits+316, fr->t_bits+0, 4); |
| 211 | } |
| 212 | |
| 213 | |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 214 | int encode_trau_frame(uint8_t *trau_bits, const struct decoded_trau_frame *fr) |
Harald Welte | 418f5a4 | 2009-01-05 20:12:47 +0000 | [diff] [blame] | 215 | { |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 216 | uint8_t cbits5 = get_bits(fr->c_bits, 0, 5); |
Harald Welte | 418f5a4 | 2009-01-05 20:12:47 +0000 | [diff] [blame] | 217 | |
| 218 | /* 16 bits of sync header */ |
| 219 | memset(trau_bits, 0, 16); |
| 220 | |
| 221 | switch (cbits5) { |
| 222 | case TRAU_FT_FR_UP: |
| 223 | case TRAU_FT_FR_DOWN: |
| 224 | case TRAU_FT_IDLE_UP: |
| 225 | case TRAU_FT_IDLE_DOWN: |
| 226 | case TRAU_FT_EFR: |
| 227 | encode_fr(trau_bits, fr); |
| 228 | break; |
| 229 | case TRAU_FT_AMR: |
| 230 | case TRAU_FT_OM_UP: |
| 231 | case TRAU_FT_OM_DOWN: |
| 232 | case TRAU_FT_DATA_UP: |
| 233 | case TRAU_FT_DATA_DOWN: |
| 234 | case TRAU_FT_D145_SYNC: |
| 235 | case TRAU_FT_EDATA: |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 236 | LOGP(DMUX, LOGL_NOTICE, "unimplemented TRAU Frame Type " |
| 237 | "0x%02x\n", cbits5); |
Harald Welte | 418f5a4 | 2009-01-05 20:12:47 +0000 | [diff] [blame] | 238 | return -1; |
| 239 | break; |
| 240 | default: |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 241 | LOGP(DMUX, LOGL_NOTICE, "unknown TRAU Frame Type " |
| 242 | "0x%02x\n", cbits5); |
Harald Welte | 418f5a4 | 2009-01-05 20:12:47 +0000 | [diff] [blame] | 243 | return -1; |
| 244 | break; |
| 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | } |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 249 | |
| 250 | static struct decoded_trau_frame fr_idle_frame = { |
| 251 | .c_bits = { 0, 1, 1, 1, 0 }, /* IDLE DOWNLINK 3.5.5 */ |
| 252 | .t_bits = { 1, 1, 1, 1 }, |
| 253 | }; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 254 | static uint8_t encoded_idle_frame[TRAU_FRAME_BITS]; |
Dieter Spaar | f31dd86 | 2011-07-27 23:52:36 +0200 | [diff] [blame^] | 255 | static int dbits_initted = 0; |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 256 | |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 257 | uint8_t *trau_idle_frame(void) |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 258 | { |
| 259 | /* only initialize during the first call */ |
| 260 | if (!dbits_initted) { |
| 261 | /* set all D-bits to 1 */ |
| 262 | memset(&fr_idle_frame.d_bits, 0x01, 260); |
Dieter Spaar | f31dd86 | 2011-07-27 23:52:36 +0200 | [diff] [blame^] | 263 | |
| 264 | memset(&fr_idle_frame.c_bits, 0x01, 25); /* spare are set to 1 */ |
| 265 | /* set Downlink Idle Speech Frame pattern */ |
| 266 | fr_idle_frame.c_bits[0] = 0; /* C1 */ |
| 267 | fr_idle_frame.c_bits[1] = 1; /* C2 */ |
| 268 | fr_idle_frame.c_bits[2] = 1; /* C3 */ |
| 269 | fr_idle_frame.c_bits[3] = 1; /* C4 */ |
| 270 | fr_idle_frame.c_bits[4] = 0; /* C5 */ |
| 271 | /* set no Time Alignment pattern */ |
| 272 | fr_idle_frame.c_bits[5] = 0; /* C6 */ |
| 273 | fr_idle_frame.c_bits[6] = 0; /* C7 */ |
| 274 | fr_idle_frame.c_bits[7] = 0; /* C8 */ |
| 275 | fr_idle_frame.c_bits[8] = 0; /* C9 */ |
| 276 | fr_idle_frame.c_bits[9] = 0; /* C10 */ |
| 277 | fr_idle_frame.c_bits[10] = 0; /* C11 */ |
| 278 | /* already set to 1, but maybe we need to modify it in the future */ |
| 279 | fr_idle_frame.c_bits[11] = 1; /* C12 (UFE), good frame */ |
| 280 | fr_idle_frame.c_bits[15] = 1; /* C16 (SP), no DTX */ |
| 281 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 282 | encode_fr(encoded_idle_frame, &fr_idle_frame); |
Dieter Spaar | f31dd86 | 2011-07-27 23:52:36 +0200 | [diff] [blame^] | 283 | dbits_initted = 1; /* set it to 1 to not call it again */ |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 284 | } |
| 285 | return encoded_idle_frame; |
| 286 | } |