L1A/L23 interface (L1CTL) cleanup
[osmocom-bb.git] / src / host / layer23 / src / l1ctl.c
1 /* Layer1 control code, talking L1CTL protocol with L1 on the phone */
2
3 /* (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
4  * (C) 2010 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
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #include <arpa/inet.h>
30
31 #include <l1a_l23_interface.h>
32
33 #include <osmocore/timer.h>
34 #include <osmocore/msgb.h>
35 #include <osmocore/tlv.h>
36 #include <osmocore/gsm_utils.h>
37 #include <osmocore/protocol/gsm_04_08.h>
38 #include <osmocore/protocol/gsm_08_58.h>
39 #include <osmocore/rsl.h>
40
41 #include <osmocom/l1ctl.h>
42 #include <osmocom/osmocom_data.h>
43 #include <osmocom/lapdm.h>
44 #include <osmocom/debug.h>
45 #include <osmocom/gsmtap_util.h>
46
47 static struct msgb *osmo_l1_alloc(uint8_t msg_type)
48 {
49         struct l1ctl_hdr *l1h;
50         struct msgb *msg = msgb_alloc_headroom(256, 4, "osmo_l1");
51
52         if (!msg) {
53                 fprintf(stderr, "Failed to allocate memory.\n");
54                 return NULL;
55         }
56
57         msg->l1h = msgb_put(msg, sizeof(*l1h));
58         l1h = (struct l1ctl_hdr *) msg->l1h;
59         l1h->msg_type = msg_type;
60         
61         return msg;
62 }
63
64
65 static int osmo_make_band_arfcn(struct osmocom_ms *ms)
66 {
67         /* TODO: Include the band */
68         return ms->arfcn;
69 }
70
71 static int rx_l1_ccch_resp(struct osmocom_ms *ms, struct msgb *msg)
72 {
73         struct l1ctl_info_dl *dl;
74         struct l1ctl_sync_new_ccch_resp *sb;
75         struct gsm_time tm;
76
77         if (msgb_l3len(msg) < sizeof(*sb)) {
78                 fprintf(stderr, "MSG too short for CCCH RESP: %u\n", msgb_l3len(msg));
79                 return -1;
80         }
81
82         dl = (struct l1ctl_info_dl *) msg->l1h;
83         sb = (struct l1ctl_sync_new_ccch_resp *) dl->payload;
84
85         gsm_fn2gsmtime(&tm, ntohl(dl->frame_nr));
86         printf("SCH: SNR: %u TDMA: (%.4u/%.2u/%.2u) bsic: %d\n",
87                 dl->snr, tm.t1, tm.t2, tm.t3, sb->bsic);
88
89         return 0;
90 }
91
92 char *chan_nr2string(uint8_t chan_nr)
93 {
94         static char str[20];
95         uint8_t cbits = chan_nr >> 3;
96
97         str[0] = '\0';
98
99         if (cbits == 0x01)
100                 sprintf(str, "TCH/F");
101         else if ((cbits & 0x1e) == 0x02)
102                 sprintf(str, "TCH/H(%u)", cbits & 0x01);
103         else if ((cbits & 0x1c) == 0x04)
104                 sprintf(str, "SDCCH/4(%u)", cbits & 0x03);
105         else if ((cbits & 0x18) == 0x08)
106                 sprintf(str, "SDCCH/8(%u)", cbits & 0x07);
107         else if (cbits == 0x10)
108                 sprintf(str, "BCCH");
109         else if (cbits == 0x11)
110                 sprintf(str, "RACH");
111         else if (cbits == 0x12)
112                 sprintf(str, "PCH/AGCH");
113         else
114                 sprintf(str, "UNKNOWN");
115
116         return str;
117 }
118
119 /* Receive L1CTL_DATA_IND (Data Indication from L1) */
120 static int rx_ph_data_ind(struct osmocom_ms *ms, struct msgb *msg)
121 {
122         struct l1ctl_info_dl *dl, dl_cpy;
123         struct l1ctl_data_ind *ccch;
124         struct lapdm_entity *le;
125         uint8_t chan_type, chan_ts, chan_ss;
126         uint8_t gsmtap_chan_type;
127         struct gsm_time tm;
128
129         if (msgb_l3len(msg) < sizeof(*ccch)) {
130                 fprintf(stderr, "MSG too short Data Ind: %u\n", msgb_l3len(msg));
131                 return -1;
132         }
133
134         dl = (struct l1ctl_info_dl *) msg->l1h;
135         msg->l2h = dl->payload;
136         ccch = (struct l1ctl_data_ind *) msg->l2h;
137
138         gsm_fn2gsmtime(&tm, ntohl(dl->frame_nr));
139         rsl_dec_chan_nr(dl->chan_nr, &chan_type, &chan_ss, &chan_ts);
140         printf("%s (%.4u/%.2u/%.2u) %s\n",
141                 chan_nr2string(dl->chan_nr), tm.t1, tm.t2, tm.t3,
142                 hexdump(ccch->data, sizeof(ccch->data)));
143
144         /* send CCCH data via GSMTAP */
145         gsmtap_chan_type = chantype_rsl2gsmtap(chan_type, dl->link_id);
146         gsmtap_sendmsg(dl->band_arfcn, chan_ts, gsmtap_chan_type, chan_ss,
147                         tm.fn, dl->rx_level-110, dl->snr, ccch->data, sizeof(ccch->data));
148
149         /* determine LAPDm entity based on SACCH or not */
150         if (dl->link_id & 0x40)
151                 le = &ms->lapdm_acch;
152         else
153                 le = &ms->lapdm_dcch;
154         /* make local stack copy of l1ctl_info_dl, as LAPDm will overwrite skb hdr */
155         memcpy(&dl_cpy, dl, sizeof(dl_cpy));
156
157         /* pull the L1 header from the msgb */
158         msgb_pull(msg, msg->l2h - (msg->l1h-sizeof(struct l1ctl_hdr)));
159         msg->l1h = NULL;
160
161         /* send it up into LAPDm */
162         l2_ph_data_ind(msg, le, &dl_cpy);
163
164         return 0;
165 }
166
167 /* Transmit L1CTL_DATA_REQ */
168 int tx_ph_data_req(struct osmocom_ms *ms, struct msgb *msg,
169                    uint8_t chan_nr, uint8_t link_id)
170 {
171         struct l1ctl_hdr *l1h;
172         struct l1ctl_info_ul *l1i_ul;
173         uint8_t chan_type, chan_ts, chan_ss;
174         uint8_t gsmtap_chan_type;
175
176         printf("tx_ph_data_req(%s)\n", hexdump(msg->l2h, msgb_l2len(msg)));
177
178         if (msgb_l2len(msg) > 23) {
179                 printf("L1 cannot handle message length > 23 (%u)\n", msgb_l2len(msg));
180                 msgb_free(msg);
181                 return -EINVAL;
182         } else if (msgb_l2len(msg) < 23)
183                 printf("L1 message length < 23 (%u) doesn't seem right!\n", msgb_l2len(msg));
184
185         /* send copy via GSMTAP */
186         rsl_dec_chan_nr(chan_nr, &chan_type, &chan_ss, &chan_ts);
187         gsmtap_chan_type = chantype_rsl2gsmtap(chan_type, link_id);
188         gsmtap_sendmsg(0|0x4000, chan_ts, gsmtap_chan_type, chan_ss,
189                         0, 127, 255, msg->l2h, msgb_l2len(msg));
190
191         /* prepend uplink info header */
192         l1i_ul = (struct l1ctl_info_ul *) msgb_push(msg, sizeof(*l1i_ul));
193
194         l1i_ul->chan_nr = chan_nr;
195         l1i_ul->link_id = link_id;
196
197         /* FIXME: where to get this from? */
198         l1i_ul->tx_power = 0;
199
200         /* prepend l1 header */
201         msg->l1h = msgb_push(msg, sizeof(*l1h));
202         l1h = (struct l1ctl_hdr *) msg->l1h;
203         l1h->msg_type = L1CTL_DATA_REQ;
204
205         return osmo_send_l1(ms, msg);
206 }
207
208 /* Receive L1CTL_RESET */
209 static int rx_l1_reset(struct osmocom_ms *ms)
210 {
211         struct msgb *msg;
212         struct l1ctl_sync_new_ccch_req *req;
213
214         msg = osmo_l1_alloc(L1CTL_NEW_CCCH_REQ);
215         if (!msg)
216                 return -1;
217
218         printf("Layer1 Reset.\n");
219         req = (struct l1ctl_sync_new_ccch_req *) msgb_put(msg, sizeof(*req));
220         req->band_arfcn = osmo_make_band_arfcn(ms);
221
222         return osmo_send_l1(ms, msg);
223 }
224
225 /* Transmit L1CTL_RACH_REQ */
226 int tx_ph_rach_req(struct osmocom_ms *ms)
227 {
228         struct msgb *msg;
229         struct l1ctl_info_ul *ul;
230         struct l1ctl_rach_req *req;
231         static uint8_t i = 0;
232
233         msg = osmo_l1_alloc(L1CTL_RACH_REQ);
234         if (!msg)
235                 return -1;
236
237         printf("RACH Req.\n");
238         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
239         req = (struct l1ctl_rach_req *) msgb_put(msg, sizeof(*req));
240         req->ra = i++;
241
242         return osmo_send_l1(ms, msg);
243 }
244
245 /* Transmit L1CTL_DM_EST_REQ */
246 int tx_ph_dm_est_req(struct osmocom_ms *ms, uint16_t band_arfcn, uint8_t chan_nr)
247 {
248         struct msgb *msg;
249         struct l1ctl_info_ul *ul;
250         struct l1ctl_dm_est_req *req;
251
252         msg = osmo_l1_alloc(L1CTL_DM_EST_REQ);
253         if (!msg)
254                 return -1;
255
256         printf("Tx Dedic.Mode Est Req (arfcn=%u, chan_nr=0x%02x)\n",
257                 band_arfcn, chan_nr);
258         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
259         ul->chan_nr = chan_nr;
260         ul->link_id = 0;
261         ul->tx_power = 0; /* FIXME: initial TX power */
262         req = (struct l1ctl_dm_est_req *) msgb_put(msg, sizeof(*req));
263         req->band_arfcn = band_arfcn;
264
265         return osmo_send_l1(ms, msg);
266 }
267
268 /* Receive incoming data from L1 using L1CTL format */
269 int l1ctl_recv(struct osmocom_ms *ms, struct msgb *msg)
270 {
271         int rc = 0;
272         struct l1ctl_hdr *l1h;
273         struct l1ctl_info_dl *dl;
274
275         if (msgb_l2len(msg) < sizeof(*dl)) {
276                 fprintf(stderr, "Short Layer2 message: %u\n", msgb_l2len(msg));
277                 return -1;
278         }
279
280         l1h = (struct l1ctl_info_dl *) msg->l1h;
281
282         /* move the l1 header pointer to point _BEHIND_ l1ctl_hdr,
283            as the l1ctl header is of no interest to subsequent code */
284         msg->l1h = l1h->data;
285
286         switch (l1h->msg_type) {
287         case L1CTL_NEW_CCCH_RESP:
288                 rc = rx_l1_ccch_resp(ms, msg);
289                 break;
290         case L1CTL_DATA_IND:
291                 rc = rx_ph_data_ind(ms, msg);
292                 break;
293         case L1CTL_RESET:
294                 rc = rx_l1_reset(ms);
295                 break;
296         default:
297                 fprintf(stderr, "Unknown MSG: %u\n", l1h->msg_type);
298                 break;
299         }
300
301         return rc;
302 }