2 * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 flush rach msg in all cases: during sending, after its done, and when aborted
25 debugging. (wenn dies todo erledigt ist, bitte in den anderen code moven)
26 wird beim abbruch immer der gepufferte cm-service-request entfernt, auch beim verschicken?:
28 todo rr_sync_ind when receiving ciph, re ass, channel mode modify
30 todo change procedures, release procedure
32 during procedures, like "channel assignment" or "handover", rr requests must be queued
33 they must be dequeued when complete
34 they queue must be flushed when rr fails
36 #include <osmocore/protocol/gsm_04_08.h>
37 #include <osmocore/msgb.h>
38 #include <osmocore/gsm48.h>
40 static int gsm_rr_chan2cause[4] = {
41 RR_EST_CAUSE_ANS_PAG_ANY,
42 RR_EST_CAUSE_ANS_PAG_SDCCH,
43 RR_EST_CAUSE_ANS_PAG_TCH_F,
44 RR_EST_CAUSE_ANS_PAG_TCH_ANY
47 static struct rr_names {
51 { "RR_EST_REQ", RR_EST_REQ },
52 { "RR_EST_IND", RR_EST_IND },
53 { "RR_EST_CNF", RR_EST_CNF },
54 { "RR_REL_IND", RR_REL_IND },
55 { "RR_SYNC_IND", RR_SYNC_IND },
56 { "RR_DATA_REQ", RR_DATA_REQ },
57 { "RR_DATA_IND", RR_DATA_IND },
58 { "RR_UNIT_DATA_IND", RR_UNIT_DATA_IND },
59 { "RR_ABORT_REQ", RR_ABORT_REQ },
60 { "RR_ABORT_IND", RR_ABORT_IND },
61 { "RR_ACT_REQ", RR_ACT_REQ },
66 char *get_rr_name(int value)
70 for (i = 0; rr_names[i].name; i++) {
71 if (rr_names[i].value == value)
72 return rr_names[i].name;
78 static int rr_recvmsg(struct osmocom_ms *ms,
79 int msg_type, struct gsm_mncc *rrmsg)
83 DEBUGP(DRR, "(MS %s) Sending '%s' to MM.\n", ms->name,
84 get_rr_name(msg_type));
86 rrmsg->msg_type = msg_type;
88 msg = msgb_alloc(sizeof(struct gsm_rr), "RR");
91 memcpy(msg->data, rrmsg, sizeof(struct gsm_rr));
92 msgb_enqueue(&ms->rr.upqueue, msg);
97 char *rr_state_names[] = {
103 static void new_rr_state(struct gsm_rrlayer *rr, int state)
105 if (state < 0 || state >= (sizeof(rr_state_names) / sizeof(char *)))
108 if (state == GSM_RRSTATE_IDLE) {
110 if (rr->rr_est_msg) {
111 msgb_free(rr->rr_est_msg);
112 rr->rr_est_msg = NULL;
116 DEBUGP(DRR, "new state %s -> %s\n",
117 rr_state_names[rr->state], rr_state_names[state]);
122 static void start_rr_t3122(struct gsm_rrlayer *rr, int sec, int micro)
124 DEBUGP(DRR, "starting T3122 with %d seconds\n", current, sec);
125 rr->t3122.cb = timeout_rr_t3122;
127 bsc_schedule_timer(&rr->t3122, sec, micro);
130 static void start_rr_t3126(struct gsm_rrlayer *rr, int sec, int micro)
132 DEBUGP(DRR, "starting T3126 with %d seconds\n", current, sec);
133 rr->t3126.cb = timeout_rr_t3126;
135 bsc_schedule_timer(&rr->t3126, sec, micro);
138 static void stop_rr_t3126(struct gsm_rrlayer *rr)
140 if (bsc_timer_pending(rr->t3126)) {
141 DEBUGP(DRR, "stopping pending timer T3126\n");
142 bsc_del_timer(&rr->t3126);
146 static void stop_rr_t3122(struct gsm_rrlayer *rr)
148 if (bsc_timer_pending(rr->t3122)) {
149 DEBUGP(DRR, "stopping pending timer T3122\n");
150 bsc_del_timer(&rr->t3122);
152 rr->t3122_running = 0;
155 static int gsm_match_mi(struct osmocom_ms *ms, u_int8_t mi)
162 mi_type = mi[1] & GSM_MI_TYPE_MASK;
164 case GSM_MI_TYPE_TMSI:
167 memcpy(&tmsi, mi+2, 4);
168 if (ms->subscr.tmsi == ntohl(tmsi)
169 && ms->subscr.tmsi_valid)
172 case GSM_MI_TYPE_IMSI:
173 gsm48_mi_to_string(imsi, sizeof(imsi), mi + 1, mi[0]);
174 if (!strcmp(imsi, ms->subscr.imsi))
178 DEBUGP(DRR, "paging with unsupported MI type %d.\n", mi_type);
184 static int gsm_match_ra(struct osmocom_ms *ms, struct gsm48_req_ref *req)
186 struct gsm_rrlayer *rr = ms->rrlayer;
189 for (i = 0; i < 3; i++) {
190 if (rr->cr_hist[i] >= 0
191 && ref->ra == rr->cr_hist[i]) {
200 static int gsm_rr_tx_chan_req(struct osmocom_ms *ms, int cause)
202 struct gsm_rrlayer *rr = ms->rrlayer;
205 new_rr_state(rr, GSM_RRSTATE_CONN_PEND);
207 /* number of retransmissions (without first transmission) */
208 rr->n_chan_req = ms->si.max_retrans;
210 /* generate CHAN REQ (9.1.8) */
213 case RR_EST_CAUSE_EMERGENCY:
217 DEBUGP(DRR, "CHANNEL REQUEST: %02x (Emergency call)\n", chan_req);
219 case RR_EST_CAUSE_REESTAB_TCH_F:
222 DEBUGP(DRR, "CHANNEL REQUEST: %02x (re-establish TCH/F)\n", chan_req);
224 case RR_EST_CAUSE_REESTAB_TCH_H:
228 DEBUGP(DRR, "CHANNEL REQUEST: %02x (re-establish TCH/H with NECI)\n", chan_req);
232 DEBUGP(DRR, "CHANNEL REQUEST: %02x (re-establish TCH/H no NECI)\n", chan_req);
235 case RR_EST_CAUSE_REESTAB_2_TCH_H:
239 DEBUGP(DRR, "CHANNEL REQUEST: %02x (re-establish TCH/H+TCH/H with NECI)\n", chan_req);
243 DEBUGP(DRR, "CHANNEL REQUEST: %02x (re-establish TCH/H+TCH/H no NECI)\n", chan_req);
246 case RR_EST_CAUSE_ANS_PAG_ANY:
249 DEBUGP(DRR, "CHANNEL REQUEST: %02x (PAGING Any channel)\n", chan_req);
251 case RR_EST_CAUSE_ANS_PAG_SDCCH:
254 DEBUGP(DRR, "CHANNEL REQUEST: %02x (PAGING SDCCH)\n", chan_req);
256 case RR_EST_CAUSE_ANS_PAG_TCH_F:
257 /* ms supports no dual rate */
260 DEBUGP(DRR, "CHANNEL REQUEST: %02x (PAGING TCH/F)\n", chan_req);
262 case RR_EST_CAUSE_ANS_PAG_TCH_ANY:
263 /* ms supports no dual rate */
266 DEBUGP(DRR, "CHANNEL REQUEST: %02x (PAGING TCH/H or TCH/F)\n", chan_req);
268 case RR_EST_CAUSE_ORIG_TCHF:
269 /* ms supports no dual rate */
272 DEBUGP(DRR, "CHANNEL REQUEST: %02x (Orig TCH/F)\n", chan_req);
274 case RR_EST_CAUSE_LOC_UPD:
278 DEBUGP(DRR, "CHANNEL REQUEST: %02x (Location Update with NECI)\n", chan_req);
282 DEBUGP(DRR, "CHANNEL REQUEST: %02x (Location Update no NECI)\n", chan_req);
285 case RR_EST_CAUSE_OTHER_SDCCH:
289 DEBUGP(DRR, "CHANNEL REQUEST: %02x (OHTER with NECI)\n", chan_req);
293 DEBUGP(DRR, "CHANNEL REQUEST: %02x (OTHER no NECI)\n", chan_req);
297 if (!rr->rr_est_req) /* no request from MM */
301 DEBUGP(DRR, "CHANNEL REQUEST: with unknown establishment cause: %d\n", rrmsg->cause);
302 memset(rrmsg, 0, sizeof(rrmsg));
303 rrmsg->rr_cause = RR_REL_CAUSE_UNDEFINED;
304 rr_recvmsg(ms, RR_REL_IND, rrmsg);
305 new_rr_state(rr, GSM_RRSTATE_IDLE);
311 /* create and send RACH msg */
312 memset(&dlmsg, 0, sizeof(dlmsg));
313 dlmsg->msg = msgb_alloc(1, "CHAN_REQ");
316 *msgb_put(dlmsg->msg, 1) = chan_req;
317 rr->chan_req = chan_req;
318 t = ms->si.tx_integer;
321 dlmsg->delay = random() % t;
324 rr->cr_hist[1] = chan_req;
325 return gsm_send_dl(ms, DL_RANDOM_ACCESS_REQ, dlmsg);
328 static int gsm_rr_est_req(struct osmocom_ms *ms, struct gsm_rr *rrmsg)
330 struct gsm_rrlayer *rr = ms->rrlayer;
334 if (rr->t3122_running) {
335 if (rrmsg->cause != RR_EST_CAUSE_EMERGENCY) {
336 struct gsm_rr newmsg;
338 memset(newmsg, 0, sizeof(newmsg));
339 newmsg->rr_cause = RR_REL_CAUSE_T3122_PENDING;
340 return rr_recvmsg(ms, RR_REL_IND, newmsg);
345 bsc_schedule_timer(&rr->t3122, sec, micro);
347 if (rrmsg->cause != RR_EST_CAUSE_EMERGENCY) {
348 if (!(ms->access_class & ms->si.access_class)) {
350 if (!ms->opt.access_class_override) {
351 struct gsm_rr newmsg;
353 memset(newmsg, 0, sizeof(newmsg));
354 newmsg->rr_cause = RR_REL_CAUSE_NOT_AUTHORIZED;
355 return rr_recvmsg(ms, RR_REL_IND, newmsg);
359 if (!(ms->access_class & ms->si.access_class)
360 && !ms->si.emergency)
364 /* requested by RR */
367 /* store REQUEST message */
368 rr->rr_est_msg = rrmsg->msg;
370 return gsm_rr_tx_chan_req(ms, rrmsg->cause);
373 static int gsm_rr_data_req(struct osmocom_ms *ms, struct gsm_rr *rrmsg)
378 memset(&dlmsg, 0, sizeof(dlmsg));
379 dlmsg->msg = rrmsg->msg;
380 return gsm_send_dl(ms, DL_RELEASE_REQ, dlmsg);
383 static int gsm_rr_abort_req(struct osmocom_ms *ms, struct gsm_rr *rrmsg)
385 struct gsm_rrlayer *rr = ms->rrlayer;
387 if (rr->state == GSM_RRSTATE_DEDICATED) {
390 memset(&dlmsg, 0, sizeof(dlmsg));
391 return gsm_send_dl(ms, DL_RELEASE_REQ, dlmsg);
393 new_rr_state(rr, GSM_RRSTATE_IDLE);
396 static int gsm_rr_act_req(struct osmocom_ms *ms, struct gsm_rr *rrmsg)
400 /* state trasitions for radio ressource messages (upper layer) */
401 static struct rrdownstate {
404 int (*rout) (struct osmocom_ms *ms, struct gsm_dl *rrmsg);
405 } rrdownstatelist[] = {
406 {SBIT(GSM_RRSTATE_IDLE), /* 3.3.1.1 */
407 RR_EST_REQ, gsm_rr_est_req},
408 {SBIT(GSM_RRSTATE_DEDICATED), /* 3.4.2 */
409 RR_DATA_REQ, gsm_rr_data_req},
410 {SBIT(GSM_RRSTATE_CONN_PEND) | SBIT(GSM_RRSTATE_DEDICATED),
411 RR_ABORT_REQ, gsm_rr_abort_req},
412 {SBIT(GSM_RRSTATE_DEDICATED),
413 RR_ACT_REQ, gsm_rr_act_req},
416 #define RRDOWNSLLEN \
417 (sizeof(rrdownstatelist) / sizeof(struct rrdownstate))
419 static int gsm_send_rr(struct osmocom_ms *ms, struct gsm_rr *rrmsg)
421 int msg_type = rrmsg->msg_type;
423 DEBUGP(DRR, "(ms %s) Sending '%s' to DL in state %s\n", ms->name,
424 gsm0408_rr_msg_names[msg_type], mm_state_names[mm->state]);
426 /* find function for current state and message */
427 for (i = 0; i < RRDOWNSLLEN; i++)
428 if ((msg_type == rrdownstatelist[i].type)
429 && ((1 << mm->state) & rrdownstatelist[i].states))
431 if (i == RRDOWNSLLEN) {
432 DEBUGP(DRR, "Message unhandled at this state.\n");
436 rc = rrdownstatelist[i].rout(ms, dlmsg);
441 static int gsm_rr_rx_pag_req_1(struct osmocom_ms *ms, struct gsm_msgb *msg)
443 struct gsm_rrlayer *rr = ms->rrlayer;
444 struct gsm48_hdr *gh = msgb_l3(msg);
445 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
446 int chan_first, chan_second;
448 /* 3.3.1.1.2: ignore paging while establishing */
449 if (rr->state != GSM_RRSTATE_IDLE)
452 if (payload_len < 1 + 2) {
454 DEBUGP(DRR, "Short read of paging request 1 message .\n");
459 chan_first = *gh->data & 0x03;
460 chan_second = (*gh->data >> 2) & 0x03;
463 if (payload_len - 1 < mi[0] + 1)
465 if (gsm_match_mi(ms, mi) > 0)
466 return gsm_rr_tx_chan_req(ms, gsm_rr_chan2cause[chan_first]);
468 payload_len -= 1 + mi[0] - 1;
469 mi = gh->data + 1 + mi[0] + 1;
472 if (mi[0] != GSM48_IE_MOBILE_ID)
474 if (payload_len < mi[1] + 2)
476 if (gsm_match_mi(ms, mi + 1) > 0)
477 return gsm_rr_tx_chan_req(ms, gsm_rr_chan2cause[chan_second]);
482 static int gsm_rr_rx_pag_req_2(struct osmocom_ms *ms, struct gsm_msgb *msg)
484 struct gsm_rrlayer *rr = ms->rrlayer;
485 struct gsm48_hdr *gh = msgb_l3(msg);
486 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
488 int chan_first, chan_second, chan_third;
490 /* 3.3.1.1.2: ignore paging while establishing */
491 if (rr->state != GSM_RRSTATE_IDLE)
494 if (payload_len < 1 + 4 + 4) {
496 DEBUGP(DRR, "Short read of paging request 2 message .\n");
501 chan_first = *gh->data & 0x03;
502 chan_second = (*gh->data >> 2) & 0x03;
505 memcpy(&tmsi, mi, 4);
506 if (ms->subscr.tmsi == ntohl(tmsi)
507 && ms->subscr.tmsi_valid)
508 return gsm_rr_tx_chan_req(ms, gsm_rr_chan2cause[chan_first]);
510 mi = gh->data + 1 + 4;
511 memcpy(&tmsi, mi, 4);
512 if (ms->subscr.tmsi == ntohl(tmsi)
513 && ms->subscr.tmsi_valid)
514 return gsm_rr_tx_chan_req(ms, gsm_rr_chan2cause[chan_second]);
516 payload_len -= 1 + 4 + 4;
517 mi = gh->data + 1 + 4 + 4;
520 if (mi[0] != GSM48_IE_MOBILE_ID)
522 if (payload_len < mi[1] + 2 + 1) /* must include "channel needed" */
524 chan_third = mi[mi[1] + 2] & 0x03; /* channel needed */
525 if (gsm_match_mi(ms, mi + 1) > 0)
526 return gsm_rr_tx_chan_req(ms, gsm_rr_chan2cause[chan_third]);
531 static int gsm_rr_rx_pag_req_3(struct osmocom_ms *ms, struct gsm_msgb *msg)
533 struct gsm_rrlayer *rr = ms->rrlayer;
534 struct gsm48_hdr *gh = msgb_l3(msg);
535 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
537 int chan_first, chan_second, chan_third, chan_fourth;
539 /* 3.3.1.1.2: ignore paging while establishing */
540 if (rr->state != GSM_RRSTATE_IDLE)
543 if (payload_len < 1 + 4 + 4 + 4 + 4 + 1) { /* must include "channel needed" */
545 DEBUGP(DRR, "Short read of paging request 3 message .\n");
550 chan_first = *gh->data & 0x03;
551 chan_second = (*gh->data >> 2) & 0x03;
552 chan_third = gh->data[1 + 4 + 4 + 4 + 4] & 0x03;
553 chan_fourth = (gh->data[1 + 4 + 4 + 4 + 4] >> 2) & 0x03;
556 memcpy(&tmsi, mi, 4);
557 if (ms->subscr.tmsi == ntohl(tmsi)
558 && ms->subscr.tmsi_valid)
559 return gsm_rr_tx_chan_req(ms, gsm_rr_chan2cause[chan_first]);
561 mi = gh->data + 1 + 4;
562 memcpy(&tmsi, mi, 4);
563 if (ms->subscr.tmsi == ntohl(tmsi)
564 && ms->subscr.tmsi_valid)
565 return gsm_rr_tx_chan_req(ms, gsm_rr_chan2cause[chan_second]);
567 mi = gh->data + 1 + 4 + 4;
568 memcpy(&tmsi, mi, 4);
569 if (ms->subscr.tmsi == ntohl(tmsi)
570 && ms->subscr.tmsi_valid)
571 return gsm_rr_tx_chan_req(ms, gsm_rr_chan2cause[chan_third]);
573 mi = gh->data + 1 + 4 + 4 + 4;
574 memcpy(&tmsi, mi, 4);
575 if (ms->subscr.tmsi == ntohl(tmsi)
576 && ms->subscr.tmsi_valid)
577 return gsm_rr_tx_chan_req(ms, gsm_rr_chan2cause[chan_fourth]);
582 static int gsm_rr_dl_est(struct osmocom_ms *ms, struct gsm_msgb *msg)
584 struct gsm_rrlayer *rr = ms->rrlayer;
589 /* flush pending RACH requests */
590 rr->n_chan_req = 0; // just to be safe
591 memset(&dlmsg, 0, sizeof(dlmsg));
592 gsm_send_dl(ms, DL_RANDOM_ACCESS_FLU, dlmsg);
594 /* send DL_EST_REQ */
595 memset(&dlmsg, 0, sizeof(dlmsg));
596 memcpy(dlmsg->channel_description, rr->channel_description, 3);
597 memcpy(dlmsg->mobile_alloc_lv, rr->mobile_alloc_lv, 9);
599 if (rr->rr_est_msg) {
600 dlmsg->msg = rr->rr_est_msg;
605 gsm_send_dl(ms, DL_ESTABLISH_REQ, dlmsg);
608 static int gsm_rr_rx_imm_ass(struct osmocom_ms *ms, struct gsm_msgb *msg)
610 struct gsm_rrlayer *rr = ms->rrlayer;
611 struct gsm48_hdr *gh = msgb_l3(msg);
612 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
613 struct gsm48_req_ref *ref;
615 /* 3.3.1.1.2: ignore assignment while idle */
616 if (rr->state != GSM_RRSTATE_CONN_PEND || !rr->wait_assign)
619 if (payload_len < 1 + 3 + 3 + 1 + 1) {
621 DEBUGP(DRR, "Short read of immediate assignment message.\n");
624 if (*gh->data[1 + 3 + 3 + 1] > 8) {
625 DEBUGP(DRR, "moble allocation in immediate assignment too large.\n");
630 if (gsm_match_ra(ms, (struct gsm48_req_ref *)(gh->data + 1 + 3))) {
631 /* channel description */
632 memcpy(rr->chan_descr, gh->data + 1, 3);
634 rr->timing_advance = *gh->data[1 + 3 + 3];
635 /* mobile allocation */
636 memcpy(rr->mobile_alloc_lv, gh->data + 1 + 3 + 3 + 1, *gh->data[1 + 3 + 3 + 1] + 1);
638 return gsm_rr_dl_est(ms);
644 static int gsm_rr_rx_imm_ass_ext(struct osmocom_ms *ms, struct gsm_msgb *msg)
646 struct gsm_rrlayer *rr = ms->rrlayer;
647 struct gsm48_hdr *gh = msgb_l3(msg);
648 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
650 /* 3.3.1.1.2: ignore assignment while idle */
651 if (rr->state != GSM_RRSTATE_CONN_PEND || !rr->wait_assign)
654 if (payload_len < 1 + 3 + 3 + 1 + 3 + 3 + 1 + 1) {
656 DEBUGP(DRR, "Short read of immediate assignment extended message.\n");
659 if (*gh->data[1 + 3 + 3 + 1 + 3 + 3 + 1] > 4) {
660 DEBUGP(DRR, "moble allocation in immediate assignment extended too large.\n");
665 if (gsm_match_ra(ms, (struct gsm48_req_ref *)(gh->data + 1 + 3))) {
666 /* channel description */
667 memcpy(rr->chan_descr, gh->data + 1, 3);
669 rr->timing_advance = *gh->data[1 + 3 + 3];
670 /* mobile allocation */
671 memcpy(rr->mobile_alloc_lv, gh->data + 1 + 3 + 3 + 1 + 3 + 3 + 1, *gh->data[1 + 3 + 3 + 1 + 3 + 3 + 1] + 1);
673 return gsm_rr_dl_est(ms);
676 if (gsm_match_ra(ms, (struct gsm48_req_ref *)(gh->data + 1 + 3 + 3 + 1 + 3))) {
677 /* channel description */
678 memcpy(rr->chan_descr, gh->data + 1 + 3 + 3 + 1, 3);
680 rr->timing_advance = *gh->data[1 + 3 + 3 + 1 + 3 + 3];
681 /* mobile allocation */
682 memcpy(rr->mobile_alloc_lv, gh->data + 1 + 3 + 3 + 1 + 3 + 3 + 1, *gh->data[1 + 3 + 3 + 1 + 3 + 3 + 1] + 1);
684 return gsm_rr_dl_est(ms);
690 static int gsm_rr_rx_imm_ass_rej(struct osmocom_ms *ms, struct gsm_msgb *msg)
692 struct gsm_rrlayer *rr = ms->rrlayer;
693 struct gsm48_hdr *gh = msgb_l3(msg);
694 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
697 /* 3.3.1.1.2: ignore assignment while idle */
698 if (rr->state != GSM_RRSTATE_CONN_PEND || !rr->wait_assign)
701 if (payload_len < 1 + 4 * (3 + 1)) {
703 DEBUGP(DRR, "Short read of immediate assignment reject message.\n");
707 for (i = 0; i < 4; i++) {
708 if (gsm_match_ra(ms, (struct gsm48_req_ref *)(gh->data + 1 + i * (3 + 1)))) {
710 /* wait indication */
711 t3122_value = gh->data[1 + i * (3 + 1) + 3];
713 start_rr_t3122(rr, t3122_value);
717 if (rr->rr_est_req) {
720 memset(rrmsg, 0, sizeof(rrmsg));
721 rrmsg->rr_cause = RR_REL_CAUSE_CHANNEL_REJECT;
722 rr_recvmsg(ms, RR_REL_IND, rrmsg);
724 new_rr_state(rr, GSM_RRSTATE_IDLE);
731 static int gsm_rr_rx_ass_cmd(struct osmocom_ms *ms, struct msgb *msg)
733 struct gsm_rrlayer *rr = ms->rrlayer;
734 struct gsm48_hdr *gh = msgb_l3(msg);
735 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
740 send assignment failure (ms, RR_CAUSE_NO_CELL_ALL_AVAIL);
743 send assignment failure (ms, RR_CAUSE_CHAN_MODE_UNACCEPT);
745 if (freq not supported)
746 send assignment failure (ms, RR_CAUSE_FREQ_NOT_IMPLEMENTED);
750 change into special suspension state
751 - queue messages during this state
752 - flush/send when leaving this state
755 static int gsm_rr_unit_data_ind(struct osmocom_ms *ms, struct msgb *msg)
757 struct gsm48_hdr *gh = msgb_l3(msg);
759 switch (gh->msg_type) {
760 case GSM48_MT_RR_PAG_REQ_1:
761 return gsm_rr_rx_pag_req_1(ms, dlmsg->msg);
762 case GSM48_MT_RR_PAG_REQ_2:
763 return gsm_rr_rx_pag_req_2(ms, dlmsg->msg);
764 case GSM48_MT_RR_PAG_REQ_3:
765 return gsm_rr_rx_pag_req_3(ms, dlmsg->msg);
766 case GSM48_MT_RR_IMM_ASS:
767 return gsm_rr_rx_imm_ass(ms, dlmsg->msg);
768 case GSM48_MT_RR_IMM_ASS_EXT:
769 return gsm_rr_rx_imm_ass_ext(ms, dlmsg->msg);
770 case GSM48_MT_RR_IMM_ASS_REJ:
771 return gsm_rr_rx_imm_ass_rej(ms, dlmsg->msg);
773 DEBUGP(DRR, "Message type 0x%02x unknown.\n", gh->msg_type);
778 static int gsm_rr_data_ind(struct osmocom_ms *ms, struct msbg *msg)
780 struct gsm48_hdr *gh = msgb_l3(msg);
781 u_int8_t pdisc = gh->proto_discr & 0x0f;
783 if (pdisc == GSM48_PDISC_RR) {
784 switch(gh->msg_type) {
785 case GSM48_MT_RR_ASS_CMD:
786 return gsm_rr_rx_ass_cmd(ms, msg);
788 DEBUGP(DRR, "Message type 0x%02x unknown.\n", gh->msg_type);
794 memset(&rrmsg, 0, sizeof(rrmsg));
795 rrmsg->msg = dlmsg->msg;
796 return rr_recv_msg(ms, DL_RELEASE_REQ, rrmsg);
799 static int gsm_rr_estab_cnf(struct osmocom_ms *ms, struct gsm_dl *dlmsg)
803 if (rr->state == GSM_RRSTATE_IDLE) {
806 memset(&dlmsg, 0, sizeof(dlmsg));
807 return gsm_send_dl(ms, DL_RELEASE_REQ, dlmsg);
811 new_rr_state(rr, GSM_RRSTATE_DEDICATED);
813 memset(rrmsg, 0, sizeof(rrmsg));
814 return rr_recvmsg(ms, (rr->rr_est_req) ? RR_EST_CNF : RR_EST_IND, rrmsg);
817 static int gsm_rr_estab_cnf_dedicated(struct osmocom_ms *ms, struct gsm_dl *dlmsg)
819 if (special "channel assignment" state) {
820 if (resume to last channel flag is NOT set) {
821 send assignment complete message
822 flush queued radio ressource messages
826 send assignment failure (msn, RR_CAUSE_PROTO_ERR_UNSPEC);
832 static int gsm_rr_connect_cnf(struct osmocom_ms *ms, struct gsm_dl *dlmsg)
836 static int gsm_rr_rel_ind(struct osmocom_ms *ms, struct gsm_dl *dlmsg)
840 static int gsm_rr_rel_cnf(struct osmocom_ms *ms, struct gsm_dl *dlmsg)
842 /* do nothing, because we aleady IDLE
843 * or we received the rel cnf of the last connection
844 * while already requesting a new one (CONN PEND)
848 static int gsm_rr_rel_cnf_dedicated(struct osmocom_ms *ms, struct gsm_dl *dlmsg)
850 if (special "channel assignment" state) {
857 static int gsm_rr_rand_acc_cnf(struct osmocom_ms *ms, struct gsm_dl *dlmsg)
859 struct gsm_rrlayer *rr = ms->rrlayer;
862 if (!rr->n_chan_req) {
863 start_rr_t3126(rr, GSM_T3126_MS);
868 switch(ms->si.tx_integer) {
869 case 3: case 8: case 14: case 50:
870 if (ms->si.bcch_type == GSM_NON_COMBINED_CCCH)
874 case 4: case 9: case 16:
875 if (ms->si.bcch_type == GSM_NON_COMBINED_CCCH)
879 case 5: case 10: case 20:
880 if (ms->si.bcch_type == GSM_NON_COMBINED_CCCH)
884 case 6: case 11: case 25:
885 if (ms->si.bcch_type == GSM_NON_COMBINED_CCCH)
890 if (ms->si.bcch_type == GSM_NON_COMBINED_CCCH)
895 /* resend chan_req */
896 memset(&dlmsg, 0, sizeof(dlmsg));
897 dlmsg->msg = msgb_alloc(1, "CHAN_REQ");
900 *msgb_put(dlmsg->msg, 1) = rr->chan_req;
901 dlmsg->delay = (random() % ms->si.tx_integer) + s;
902 rr->cr_hist[3] = rr->cr_hist[2];
903 rr->cr_hist[2] = rr->cr_hist[1];
904 rr->cr_hist[1] = chan_req;
905 return gsm_send_dl(ms, DL_RANDOM_ACCESS_REQ, dlmsg);
909 static int gsm_rr_mdl_error_ind(struct osmocom_ms *ms, struct gsm_dl *dlmsg)
911 if (special "channel assignment" state) {
912 if (resume to last channel flag is NOT set) {
913 send estb req with last channel
919 send abort ind to upper layer
922 /* state trasitions for link layer messages (lower layer) */
923 static struct dldatastate {
926 int (*rout) (struct osmocom_ms *ms, struct gsm_dl *dlmsg);
927 } dldatastatelist[] = {
928 {SBIT(GSM_RRSTATE_IDLE) | SBIT(GSM_RRSTATE_CONN_PEND),
929 DL_UNIT_DATA_IND, gsm_rr_unit_data_ind},
930 {SBIT(GSM_RRSTATE_DEDICATED), /* 3.4.2 */
931 DL_DATA_IND, gsm_rr_data_ind},
932 {SBIT(GSM_RRSTATE_IDLE) | SBIT(GSM_RRSTATE_CONN_PEND),
933 DL_ESTABLISH_CNF, gsm_rr_estab_cnf},
934 {SBIT(GSM_RRSTATE_DEDICATED),
935 DL_ESTABLISH_CNF, gsm_rr_estab_cnf_dedicated},
937 DL_CONNECT_CNF, gsm_rr_connect_cnf},
939 DL_RELEASE_IND, gsm_rr_rel_ind},
940 {SBIT(GSM_RRSTATE_IDLE) | SBIT(GSM_RRSTATE_CONN_PENDING),
941 DL_RELEASE_CNF, gsm_rr_rel_cnf},
942 {SBIT(GSM_RRSTATE_DEDICATED),
943 DL_RELEASE_CNF, gsm_rr_rel_cnf_dedicated},
944 {SBIT(GSM_RRSTATE_CONN_PEND), /* 3.3.1.1.2 */
945 DL_RANDOM_ACCESS_CNF, gsm_rr_rand_acc_cnf},
947 MDL_ERROR_IND, gsm_rr_mdl_error_ind},
950 #define DLDATASLLEN \
951 (sizeof(dldatastatelist) / sizeof(struct dldatastate))
953 static int gsm_rcv_dl(struct osmocom_ms *ms, struct gsm_dl *dlmsg)
955 int msg_type = dlmsg->msg_type;
957 DEBUGP(DRR, "(ms %s) Received '%s' from DL in state %s\n", ms->name,
958 gsm0408_dl_msg_names[msg_type], mm_state_names[mm->state]);
960 /* find function for current state and message */
961 for (i = 0; i < DLDATASLLEN; i++)
962 if ((msg_type == dldatastatelist[i].type)
963 && ((1 << mm->state) & dldatastatelist[i].states))
965 if (i == DLDATASLLEN) {
966 DEBUGP(DRR, "Message unhandled at this state.\n");
970 rc = dldatastatelist[i].rout(ms, dlmsg);
975 static void timeout_rr_t3122(void *arg)
977 struct gsm_rrlayer *rr = arg;
979 rr->t3122_running = 0;
982 static void timeout_rr_t3126(void *arg)
984 struct gsm_rrlayer *rr = arg;
986 if (rr->rr_est_req) {
989 memset(rrmsg, 0, sizeof(rrmsg));
990 rrmsg->rr_cause = RR_REL_CAUSE_RA_FAILURE;
991 rr_recvmsg(ms, RR_REL_IND, rrmsg);
994 new_rr_state(rr, GSM_RRSTATE_IDLE);
997 struct gsm_rrlayer *gsm_new_rr(struct osmocom_ms *ms)
999 struct gsm_rrlayer *rr;
1001 rr = calloc(1, sizeof(struct gsm_rrlayer));
1009 void gsm_destroy_rr(struct gsm_rrlayer *rr)
1013 alle timer gestoppt?:
1014 todo stop t3122 when cell change
1016 memset(rr, 0, sizeof(struct gsm_rrlayer));