blob: 6d12cc08e399a2e4284e815f5d833dc19ae6cfc8 [file] [log] [blame]
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
2 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0
3 * utility functions
4 */
5
6/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
7 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
8 *
9 * All Rights Reserved
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Affero General Public License for more details.
20 *
21 * You should have received a copy of the GNU Affero General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25#include <stdio.h>
26#include <stdlib.h>
27#include <errno.h>
28#include <netinet/in.h>
29
30#include <osmocore/msgb.h>
31#include <osmocore/gsm48.h>
32
33#include <openbsc/abis_rsl.h>
34#include <openbsc/debug.h>
35#include <openbsc/gsm_04_08.h>
36#include <openbsc/transaction.h>
37#include <openbsc/paging.h>
38#include <openbsc/signal.h>
39
40/* should ip.access BTS use direct RTP streams between each other (1),
41 * or should OpenBSC always act as RTP relay/proxy in between (0) ? */
42int ipacc_rtp_direct = 1;
43
44static int gsm48_sendmsg(struct msgb *msg)
45{
46 if (msg->lchan)
47 msg->trx = msg->lchan->ts->trx;
48
49 msg->l3h = msg->data;
50 return rsl_data_request(msg, 0);
51}
52
53/* Section 9.1.8 / Table 9.9 */
54struct chreq {
55 u_int8_t val;
56 u_int8_t mask;
57 enum chreq_type type;
58};
59
60/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
61static const struct chreq chreq_type_neci1[] = {
62 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
63 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
64 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
65 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
66 { 0xe0, 0xe0, CHREQ_T_TCH_F },
67 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
68 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
69 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
70 { 0x10, 0xf0, CHREQ_T_SDCCH },
71 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY_NECI1 },
72 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
73 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
74 { 0x67, 0xff, CHREQ_T_LMU },
75 { 0x60, 0xf9, CHREQ_T_RESERVED_SDCCH },
76 { 0x61, 0xfb, CHREQ_T_RESERVED_SDCCH },
77 { 0x63, 0xff, CHREQ_T_RESERVED_SDCCH },
78 { 0x7f, 0xff, CHREQ_T_RESERVED_IGNORE },
79};
80
81/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
82static const struct chreq chreq_type_neci0[] = {
83 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
84 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
85 { 0xe0, 0xe0, CHREQ_T_TCH_F },
86 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
87 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
88 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY_NECI0 },
89 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
90 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
91 { 0x67, 0xff, CHREQ_T_LMU },
92 { 0x60, 0xf9, CHREQ_T_RESERVED_SDCCH },
93 { 0x61, 0xfb, CHREQ_T_RESERVED_SDCCH },
94 { 0x63, 0xff, CHREQ_T_RESERVED_SDCCH },
95 { 0x7f, 0xff, CHREQ_T_RESERVED_IGNORE },
96};
97
98static const enum gsm_chan_t ctype_by_chreq[] = {
99 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
100 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
101 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
102 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
103 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
104 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
105 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
106 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
107 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
108 [CHREQ_T_PAG_R_ANY_NECI1] = GSM_LCHAN_SDCCH,
109 [CHREQ_T_PAG_R_ANY_NECI0] = GSM_LCHAN_SDCCH,
110 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
111 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
112 [CHREQ_T_LMU] = GSM_LCHAN_SDCCH,
113 [CHREQ_T_RESERVED_SDCCH] = GSM_LCHAN_SDCCH,
114 [CHREQ_T_RESERVED_IGNORE] = GSM_LCHAN_UNKNOWN,
115};
116
117static const enum gsm_chreq_reason_t reason_by_chreq[] = {
118 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
119 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
120 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
121 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
122 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
123 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
124 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_CALL,
125 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
126 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
127 [CHREQ_T_PAG_R_ANY_NECI1] = GSM_CHREQ_REASON_PAG,
128 [CHREQ_T_PAG_R_ANY_NECI0] = GSM_CHREQ_REASON_PAG,
129 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
130 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
131 [CHREQ_T_LMU] = GSM_CHREQ_REASON_OTHER,
132 [CHREQ_T_RESERVED_SDCCH] = GSM_CHREQ_REASON_OTHER,
133 [CHREQ_T_RESERVED_IGNORE] = GSM_CHREQ_REASON_OTHER,
134};
135
136/* verify that the two tables match */
137static_assert(sizeof(ctype_by_chreq) ==
138 sizeof(((struct gsm_network *) NULL)->ctype_by_chreq), assert_size);
139
140/*
141 * Update channel types for request based on policy. E.g. in the
142 * case of a TCH/H network/bsc use TCH/H for the emergency calls,
143 * for early assignment assign a SDCCH and some other options.
144 */
145void gsm_net_update_ctype(struct gsm_network *network)
146{
147 /* copy over the data */
148 memcpy(network->ctype_by_chreq, ctype_by_chreq, sizeof(ctype_by_chreq));
149
150 /*
151 * Use TCH/H for emergency calls when this cell allows TCH/H. Maybe it
152 * is better to iterate over the BTS/TRX and check if no TCH/F is available
153 * and then set it to TCH/H.
154 */
155 if (network->neci)
156 network->ctype_by_chreq[CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_H;
157
158 if (network->pag_any_tch) {
159 if (network->neci) {
160 network->ctype_by_chreq[CHREQ_T_PAG_R_ANY_NECI0] = GSM_LCHAN_TCH_H;
161 network->ctype_by_chreq[CHREQ_T_PAG_R_ANY_NECI1] = GSM_LCHAN_TCH_H;
162 } else {
163 network->ctype_by_chreq[CHREQ_T_PAG_R_ANY_NECI0] = GSM_LCHAN_TCH_F;
164 network->ctype_by_chreq[CHREQ_T_PAG_R_ANY_NECI1] = GSM_LCHAN_TCH_F;
165 }
166 }
167}
168
169enum gsm_chan_t get_ctype_by_chreq(struct gsm_network *network, u_int8_t ra)
170{
171 int i;
172 int length;
173 const struct chreq *chreq;
174
175 if (network->neci) {
176 chreq = chreq_type_neci1;
177 length = ARRAY_SIZE(chreq_type_neci1);
178 } else {
179 chreq = chreq_type_neci0;
180 length = ARRAY_SIZE(chreq_type_neci0);
181 }
182
183
184 for (i = 0; i < length; i++) {
185 const struct chreq *chr = &chreq[i];
186 if ((ra & chr->mask) == chr->val)
187 return network->ctype_by_chreq[chr->type];
188 }
189 LOGP(DRR, LOGL_ERROR, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
190 return GSM_LCHAN_SDCCH;
191}
192
193enum gsm_chreq_reason_t get_reason_by_chreq(u_int8_t ra, int neci)
194{
195 int i;
196 int length;
197 const struct chreq *chreq;
198
199 if (neci) {
200 chreq = chreq_type_neci1;
201 length = ARRAY_SIZE(chreq_type_neci1);
202 } else {
203 chreq = chreq_type_neci0;
204 length = ARRAY_SIZE(chreq_type_neci0);
205 }
206
207 for (i = 0; i < length; i++) {
208 const struct chreq *chr = &chreq[i];
209 if ((ra & chr->mask) == chr->val)
210 return reason_by_chreq[chr->type];
211 }
212 LOGP(DRR, LOGL_ERROR, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
213 return GSM_CHREQ_REASON_OTHER;
214}
215
216/* 7.1.7 and 9.1.7: RR CHANnel RELease */
217int gsm48_send_rr_release(struct gsm_lchan *lchan)
218{
219 struct msgb *msg = gsm48_msgb_alloc();
220 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
221 u_int8_t *cause;
222
223 msg->lchan = lchan;
224 gh->proto_discr = GSM48_PDISC_RR;
225 gh->msg_type = GSM48_MT_RR_CHAN_REL;
226
227 cause = msgb_put(msg, 1);
228 cause[0] = GSM48_RR_CAUSE_NORMAL;
229
230 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
231 lchan->nr, lchan->type);
232
233 /* Send actual release request to MS */
234 gsm48_sendmsg(msg);
235 /* FIXME: Start Timer T3109 */
236
237 /* Deactivate the SACCH on the BTS side */
238 return rsl_deact_sacch(lchan);
239}
240
241int send_siemens_mrpci(struct gsm_lchan *lchan,
242 u_int8_t *classmark2_lv)
243{
244 struct rsl_mrpci mrpci;
245
246 if (classmark2_lv[0] < 2)
247 return -EINVAL;
248
249 mrpci.power_class = classmark2_lv[1] & 0x7;
250 mrpci.vgcs_capable = classmark2_lv[2] & (1 << 1);
251 mrpci.vbs_capable = classmark2_lv[2] & (1 <<2);
252 mrpci.gsm_phase = (classmark2_lv[1]) >> 5 & 0x3;
253
254 return rsl_siemens_mrpci(lchan, &mrpci);
255}
256
257int gsm48_extract_mi(uint8_t *classmark2_lv, int length, char *mi_string, uint8_t *mi_type)
258{
259 /* Check the size for the classmark */
260 if (length < 1 + *classmark2_lv)
261 return -1;
262
263 u_int8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
264 if (length < 2 + *classmark2_lv + mi_lv[0])
265 return -2;
266
267 *mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
268 return gsm48_mi_to_string(mi_string, GSM48_MI_SIZE, mi_lv+1, *mi_lv);
269}
270
271int gsm48_paging_extract_mi(struct gsm48_pag_resp *resp, int length,
272 char *mi_string, u_int8_t *mi_type)
273{
274 static const uint32_t classmark_offset =
275 offsetof(struct gsm48_pag_resp, classmark2);
276 u_int8_t *classmark2_lv = (uint8_t *) &resp->classmark2;
277 return gsm48_extract_mi(classmark2_lv, length - classmark_offset,
278 mi_string, mi_type);
279}
280
281int gsm48_handle_paging_resp(struct gsm_subscriber_connection *conn,
282 struct msgb *msg, struct gsm_subscriber *subscr)
283{
284 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
285 struct gsm48_hdr *gh = msgb_l3(msg);
286 u_int8_t *classmark2_lv = gh->data + 1;
287
288 if (is_siemens_bts(bts))
289 send_siemens_mrpci(msg->lchan, classmark2_lv);
290
291 if (!conn->subscr) {
292 conn->subscr = subscr;
293 } else if (conn->subscr != subscr) {
294 LOGP(DRR, LOGL_ERROR, "<- Channel already owned by someone else?\n");
295 subscr_put(subscr);
296 return -EINVAL;
297 } else {
298 DEBUGP(DRR, "<- Channel already owned by us\n");
299 subscr_put(subscr);
300 subscr = conn->subscr;
301 }
302
303 counter_inc(bts->network->stats.paging.completed);
304
305 /* Stop paging on the bts we received the paging response */
306 paging_request_stop(conn->bts, subscr, conn, msg);
307 return 0;
308}
309
310/* Chapter 9.1.9: Ciphering Mode Command */
311int gsm48_send_rr_ciph_mode(struct gsm_lchan *lchan, int want_imeisv)
312{
313 struct msgb *msg = gsm48_msgb_alloc();
314 struct gsm48_hdr *gh;
315 u_int8_t ciph_mod_set;
316
317 msg->lchan = lchan;
318
319 DEBUGP(DRR, "TX CIPHERING MODE CMD\n");
320
321 if (lchan->encr.alg_id <= RSL_ENC_ALG_A5(0))
322 ciph_mod_set = 0;
323 else
324 ciph_mod_set = (lchan->encr.alg_id-2)<<1 | 1;
325
326 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
327 gh->proto_discr = GSM48_PDISC_RR;
328 gh->msg_type = GSM48_MT_RR_CIPH_M_CMD;
329 gh->data[0] = (want_imeisv & 0x1) << 4 | (ciph_mod_set & 0xf);
330
331 return rsl_encryption_cmd(msg);
332}
333
334static void gsm48_cell_desc(struct gsm48_cell_desc *cd,
335 const struct gsm_bts *bts)
336{
337 cd->ncc = (bts->bsic >> 3 & 0x7);
338 cd->bcc = (bts->bsic & 0x7);
339 cd->arfcn_hi = bts->c0->arfcn >> 8;
340 cd->arfcn_lo = bts->c0->arfcn & 0xff;
341}
342
343void gsm48_lchan2chan_desc(struct gsm48_chan_desc *cd,
344 const struct gsm_lchan *lchan)
345{
346 u_int16_t arfcn = lchan->ts->trx->arfcn & 0x3ff;
347
348 cd->chan_nr = lchan2chan_nr(lchan);
349 if (!lchan->ts->hopping.enabled) {
350 cd->h0.tsc = lchan->ts->trx->bts->tsc;
351 cd->h0.h = 0;
352 cd->h0.arfcn_high = arfcn >> 8;
353 cd->h0.arfcn_low = arfcn & 0xff;
354 } else {
355 cd->h1.tsc = lchan->ts->trx->bts->tsc;
356 cd->h1.h = 1;
357 cd->h1.maio_high = lchan->ts->hopping.maio >> 2;
358 cd->h1.maio_low = lchan->ts->hopping.maio & 0x03;
359 cd->h1.hsn = lchan->ts->hopping.hsn;
360 }
361}
362
363#define GSM48_HOCMD_CCHDESC_LEN 16
364
365/* Chapter 9.1.15: Handover Command */
366int gsm48_send_ho_cmd(struct gsm_lchan *old_lchan, struct gsm_lchan *new_lchan,
367 u_int8_t power_command, u_int8_t ho_ref)
368{
369 struct msgb *msg = gsm48_msgb_alloc();
370 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
371 struct gsm48_ho_cmd *ho =
372 (struct gsm48_ho_cmd *) msgb_put(msg, sizeof(*ho));
373
374 msg->lchan = old_lchan;
375 gh->proto_discr = GSM48_PDISC_RR;
376 gh->msg_type = GSM48_MT_RR_HANDO_CMD;
377
378 /* mandatory bits */
379 gsm48_cell_desc(&ho->cell_desc, new_lchan->ts->trx->bts);
380 gsm48_lchan2chan_desc(&ho->chan_desc, new_lchan);
381 ho->ho_ref = ho_ref;
382 ho->power_command = power_command;
383
384 if (new_lchan->ts->hopping.enabled) {
385 struct gsm_bts *bts = new_lchan->ts->trx->bts;
386 struct gsm48_system_information_type_1 *si1;
387 uint8_t *cur;
388
389 si1 = GSM_BTS_SI(bts, SYSINFO_TYPE_1);
390 /* Copy the Cell Chan Desc (ARFCNS in this cell) */
391 msgb_put_u8(msg, GSM48_IE_CELL_CH_DESC);
392 cur = msgb_put(msg, GSM48_HOCMD_CCHDESC_LEN);
393 memcpy(cur, si1->cell_channel_description,
394 GSM48_HOCMD_CCHDESC_LEN);
395 /* Copy the Mobile Allocation */
396 msgb_tlv_put(msg, GSM48_IE_MA_BEFORE,
397 new_lchan->ts->hopping.ma_len,
398 new_lchan->ts->hopping.ma_data);
399 }
400 /* FIXME: optional bits for type of synchronization? */
401
402 return gsm48_sendmsg(msg);
403}
404
405/* Chapter 9.1.2: Assignment Command */
406int gsm48_send_rr_ass_cmd(struct gsm_lchan *dest_lchan, struct gsm_lchan *lchan, u_int8_t power_command)
407{
408 struct msgb *msg = gsm48_msgb_alloc();
409 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
410 struct gsm48_ass_cmd *ass =
411 (struct gsm48_ass_cmd *) msgb_put(msg, sizeof(*ass));
412
413 DEBUGP(DRR, "-> ASSIGNMENT COMMAND tch_mode=0x%02x\n", lchan->tch_mode);
414
415 msg->lchan = dest_lchan;
416 gh->proto_discr = GSM48_PDISC_RR;
417 gh->msg_type = GSM48_MT_RR_ASS_CMD;
418
419 /*
420 * fill the channel information element, this code
421 * should probably be shared with rsl_rx_chan_rqd(),
422 * gsm48_tx_chan_mode_modify. But beware that 10.5.2.5
423 * 10.5.2.5.a have slightly different semantic for
424 * the chan_desc. But as long as multi-slot configurations
425 * are not used we seem to be fine.
426 */
427 gsm48_lchan2chan_desc(&ass->chan_desc, lchan);
428 ass->power_command = power_command;
429
430 /* optional: cell channel description */
431
432 msgb_tv_put(msg, GSM48_IE_CHANMODE_1, lchan->tch_mode);
433
434 /* mobile allocation in case of hopping */
435 if (lchan->ts->hopping.enabled) {
436 msgb_tlv_put(msg, GSM48_IE_MA_BEFORE, lchan->ts->hopping.ma_len,
437 lchan->ts->hopping.ma_data);
438 }
439
440 /* in case of multi rate we need to attach a config */
441 if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR) {
442 if (lchan->mr_conf.ver == 0) {
443 LOGP(DRR, LOGL_ERROR, "BUG: Using multirate codec "
444 "without multirate config.\n");
445 } else {
446 u_int8_t *data = msgb_put(msg, 4);
447 data[0] = GSM48_IE_MUL_RATE_CFG;
448 data[1] = 0x2;
449 memcpy(&data[2], &lchan->mr_conf, 2);
450 }
451 }
452
453 return gsm48_sendmsg(msg);
454}
455
456/* 9.1.5 Channel mode modify: Modify the mode on the MS side */
457int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
458{
459 struct msgb *msg = gsm48_msgb_alloc();
460 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
461 struct gsm48_chan_mode_modify *cmm =
462 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
463
464 DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode);
465
466 lchan->tch_mode = mode;
467 msg->lchan = lchan;
468 gh->proto_discr = GSM48_PDISC_RR;
469 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
470
471 /* fill the channel information element, this code
472 * should probably be shared with rsl_rx_chan_rqd() */
473 gsm48_lchan2chan_desc(&cmm->chan_desc, lchan);
474 cmm->mode = mode;
475
476 /* in case of multi rate we need to attach a config */
477 if (lchan->tch_mode == GSM48_CMODE_SPEECH_AMR) {
478 if (lchan->mr_conf.ver == 0) {
479 LOGP(DRR, LOGL_ERROR, "BUG: Using multirate codec "
480 "without multirate config.\n");
481 } else {
482 u_int8_t *data = msgb_put(msg, 4);
483 data[0] = GSM48_IE_MUL_RATE_CFG;
484 data[1] = 0x2;
485 memcpy(&data[2], &lchan->mr_conf, 2);
486 }
487 }
488
489 return gsm48_sendmsg(msg);
490}
491
492int gsm48_lchan_modify(struct gsm_lchan *lchan, u_int8_t lchan_mode)
493{
494 int rc;
495
496 rc = gsm48_tx_chan_mode_modify(lchan, lchan_mode);
497 if (rc < 0)
498 return rc;
499
500 return rc;
501}
502
503int gsm48_rx_rr_modif_ack(struct msgb *msg)
504{
505 int rc;
506 struct gsm48_hdr *gh = msgb_l3(msg);
507 struct gsm48_chan_mode_modify *mod =
508 (struct gsm48_chan_mode_modify *) gh->data;
509
510 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
511
512 if (mod->mode != msg->lchan->tch_mode) {
513 LOGP(DRR, LOGL_ERROR, "CHANNEL MODE change failed. Wanted: %d Got: %d\n",
514 msg->lchan->tch_mode, mod->mode);
515 return -1;
516 }
517
518 /* update the channel type */
519 switch (mod->mode) {
520 case GSM48_CMODE_SIGN:
521 msg->lchan->rsl_cmode = RSL_CMOD_SPD_SIGN;
522 break;
523 case GSM48_CMODE_SPEECH_V1:
524 case GSM48_CMODE_SPEECH_EFR:
525 case GSM48_CMODE_SPEECH_AMR:
526 msg->lchan->rsl_cmode = RSL_CMOD_SPD_SPEECH;
527 break;
528 case GSM48_CMODE_DATA_14k5:
529 case GSM48_CMODE_DATA_12k0:
530 case GSM48_CMODE_DATA_6k0:
531 case GSM48_CMODE_DATA_3k6:
532 msg->lchan->rsl_cmode = RSL_CMOD_SPD_DATA;
533 break;
534 }
535
536 /* We've successfully modified the MS side of the channel,
537 * now go on to modify the BTS side of the channel */
538 rc = rsl_chan_mode_modify_req(msg->lchan);
539
540 /* FIXME: we not only need to do this after mode modify, but
541 * also after channel activation */
542 if (is_ipaccess_bts(msg->lchan->ts->trx->bts) && mod->mode != GSM48_CMODE_SIGN)
543 rsl_ipacc_crcx(msg->lchan);
544 return rc;
545}
546
547int gsm48_parse_meas_rep(struct gsm_meas_rep *rep, struct msgb *msg)
548{
549 struct gsm48_hdr *gh = msgb_l3(msg);
550 u_int8_t *data = gh->data;
551 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
552 struct bitvec *nbv = &bts->si_common.neigh_list;
553 struct gsm_meas_rep_cell *mrc;
554
555 if (gh->msg_type != GSM48_MT_RR_MEAS_REP)
556 return -EINVAL;
557
558 if (data[0] & 0x80)
559 rep->flags |= MEAS_REP_F_BA1;
560 if (data[0] & 0x40)
561 rep->flags |= MEAS_REP_F_UL_DTX;
562 if ((data[1] & 0x40) == 0x00)
563 rep->flags |= MEAS_REP_F_DL_VALID;
564
565 rep->dl.full.rx_lev = data[0] & 0x3f;
566 rep->dl.sub.rx_lev = data[1] & 0x3f;
567 rep->dl.full.rx_qual = (data[3] >> 4) & 0x7;
568 rep->dl.sub.rx_qual = (data[3] >> 1) & 0x7;
569
570 rep->num_cell = ((data[3] >> 6) & 0x3) | ((data[2] & 0x01) << 2);
571 if (rep->num_cell < 1 || rep->num_cell > 6)
572 return 0;
573
574 /* an encoding nightmare in perfection */
575 mrc = &rep->cell[0];
576 mrc->rxlev = data[3] & 0x3f;
577 mrc->neigh_idx = data[4] >> 3;
578 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
579 mrc->bsic = ((data[4] & 0x07) << 3) | (data[5] >> 5);
580 if (rep->num_cell < 2)
581 return 0;
582
583 mrc = &rep->cell[1];
584 mrc->rxlev = ((data[5] & 0x1f) << 1) | (data[6] >> 7);
585 mrc->neigh_idx = (data[6] >> 2) & 0x1f;
586 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
587 mrc->bsic = ((data[6] & 0x03) << 4) | (data[7] >> 4);
588 if (rep->num_cell < 3)
589 return 0;
590
591 mrc = &rep->cell[2];
592 mrc->rxlev = ((data[7] & 0x0f) << 2) | (data[8] >> 6);
593 mrc->neigh_idx = (data[8] >> 1) & 0x1f;
594 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
595 mrc->bsic = ((data[8] & 0x01) << 5) | (data[9] >> 3);
596 if (rep->num_cell < 4)
597 return 0;
598
599 mrc = &rep->cell[3];
600 mrc->rxlev = ((data[9] & 0x07) << 3) | (data[10] >> 5);
601 mrc->neigh_idx = data[10] & 0x1f;
602 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
603 mrc->bsic = data[11] >> 2;
604 if (rep->num_cell < 5)
605 return 0;
606
607 mrc = &rep->cell[4];
608 mrc->rxlev = ((data[11] & 0x03) << 4) | (data[12] >> 4);
609 mrc->neigh_idx = ((data[12] & 0xf) << 1) | (data[13] >> 7);
610 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
611 mrc->bsic = (data[13] >> 1) & 0x3f;
612 if (rep->num_cell < 6)
613 return 0;
614
615 mrc = &rep->cell[5];
616 mrc->rxlev = ((data[13] & 0x01) << 5) | (data[14] >> 3);
617 mrc->neigh_idx = ((data[14] & 0x07) << 2) | (data[15] >> 6);
618 mrc->arfcn = bitvec_get_nth_set_bit(nbv, mrc->neigh_idx + 1);
619 mrc->bsic = data[15] & 0x3f;
620
621 return 0;
622}
623
624struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value)
625{
626 struct msgb *msg;
627 struct gsm48_hdr *gh;
628
629 msg = gsm48_msgb_alloc();
630 if (!msg)
631 return NULL;
632
633 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
634 gh->proto_discr = GSM48_PDISC_MM;
635 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
636 gh->data[0] = value;
637
638 return msg;
639}
640
641struct msgb *gsm48_create_loc_upd_rej(uint8_t cause)
642{
643 struct gsm48_hdr *gh;
644 struct msgb *msg;
645
646 msg = gsm48_msgb_alloc();
647 if (!msg)
648 return NULL;
649
650 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
651 gh->proto_discr = GSM48_PDISC_MM;
652 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
653 gh->data[0] = cause;
654 return msg;
655}