cell selection: Added temporary hack to sync multiple times until the sync
[osmocom-bb.git] / src / host / layer23 / src / rslms.c
1 /* RSLms - GSM 08.58 like protocol between L2 and L3 of GSM Um interface */
2
3 /* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4  *
5  * All Rights Reserved
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  */
22
23 #include <stdint.h>
24 #include <errno.h>
25 #include <stdio.h>
26
27 #include <osmocore/msgb.h>
28 #include <osmocore/rsl.h>
29 #include <osmocore/tlv.h>
30 #include <osmocore/protocol/gsm_04_08.h>
31
32 #include <osmocom/logging.h>
33 #include <osmocom/lapdm.h>
34 #include <osmocom/rslms.h>
35 #include <osmocom/layer3.h>
36 #include <osmocom/osmocom_data.h>
37 #include <osmocom/l1ctl.h>
38
39 /* Send a 'simple' RLL request to L2 */
40 int rslms_tx_rll_req(struct osmocom_ms *ms, uint8_t msg_type,
41                      uint8_t chan_nr, uint8_t link_id)
42 {
43         struct msgb *msg;
44
45         msg = rsl_rll_simple(msg_type, chan_nr, link_id, 1);
46
47         return rslms_recvmsg(msg, ms);
48 }
49
50 /* Send a RLL request (including L3 info) to L2 */
51 int rslms_tx_rll_req_l3(struct osmocom_ms *ms, uint8_t msg_type,
52                         uint8_t chan_nr, uint8_t link_id, struct msgb *msg)
53 {
54         rsl_rll_push_l3(msg, msg_type, chan_nr, link_id, 1);
55
56         return rslms_recvmsg(msg, ms);
57 }
58
59 static int rach_count = 0;
60
61 static int rslms_rx_udata_ind(struct msgb *msg, struct osmocom_ms *ms)
62 {
63         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
64         struct tlv_parsed tv;
65         int rc = 0;
66         
67         DEBUGP(DRSL, "RSLms UNIT DATA IND chan_nr=0x%02x link_id=0x%02x\n",
68                 rllh->chan_nr, rllh->link_id);
69
70         rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
71         if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
72                 DEBUGP(DRSL, "UNIT_DATA_IND without L3 INFO ?!?\n");
73                 return -EIO;
74         }
75         msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
76
77         if (rllh->chan_nr == RSL_CHAN_PCH_AGCH)
78                 rc = gsm48_rx_ccch(msg, ms);
79         else if (rllh->chan_nr == RSL_CHAN_BCCH) {
80                 rc = gsm48_rx_bcch(msg, ms);
81                 if (rach_count < 2) {
82                         tx_ph_rach_req(ms);
83                         rach_count++;
84                 }
85         }
86
87         return rc;
88 }
89
90 static int rslms_rx_rll(struct msgb *msg, struct osmocom_ms *ms)
91 {
92         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
93         int rc = 0;
94
95         switch (rllh->c.msg_type) {
96         case RSL_MT_DATA_IND:
97                 DEBUGP(DRSL, "RSLms DATA IND\n");
98                 /* FIXME: implement this */
99                 break;
100         case RSL_MT_UNIT_DATA_IND:
101                 rc = rslms_rx_udata_ind(msg, ms);
102                 break;
103         case RSL_MT_EST_IND:
104                 DEBUGP(DRSL, "RSLms EST IND\n");
105                 /* FIXME: implement this */
106                 break;
107         case RSL_MT_EST_CONF:
108                 DEBUGP(DRSL, "RSLms EST CONF\n");
109                 /* FIXME: implement this */
110                 break;
111         case RSL_MT_REL_CONF:
112                 DEBUGP(DRSL, "RSLms REL CONF\n");
113                 /* FIXME: implement this */
114                 break;
115         case RSL_MT_ERROR_IND:
116                 DEBUGP(DRSL, "RSLms ERR IND\n");
117                 /* FIXME: implement this */
118                 break;
119         default:
120                 LOGP(DRSL, LOGL_NOTICE, "unknown RSLms message type "
121                         "0x%02x\n", rllh->c.msg_type);
122                 rc = -EINVAL;
123                 break;
124         }
125         msgb_free(msg);
126         return rc;
127 }
128
129 /* input function that L2 calls when sending messages up to L3 */
130 static int layer3_from_layer2(struct msgb *msg, struct osmocom_ms *ms)
131 {
132         struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
133         int rc = 0;
134
135         switch (rslh->msg_discr & 0xfe) {
136         case ABIS_RSL_MDISC_RLL:
137                 rc = rslms_rx_rll(msg, ms);
138                 break;
139         default:
140                 /* FIXME: implement this */
141                 LOGP(DRSL, LOGL_NOTICE, "unknown RSLms msg_discr 0x%02x\n",
142                         rslh->msg_discr);
143                 msgb_free(msg);
144                 rc = -EINVAL;
145                 break;
146         }
147
148         return rc;
149 }
150
151 int layer3_init(struct osmocom_ms *ms)
152 {
153         return osmol2_register_handler(ms, &layer3_from_layer2);
154 }