38d4f5d2220f144bacede4be699a6d5edd810b1a
[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/signal.h>
34 #include <osmocore/logging.h>
35 #include <osmocore/timer.h>
36 #include <osmocore/msgb.h>
37 #include <osmocore/tlv.h>
38 #include <osmocore/gsm_utils.h>
39 #include <osmocore/protocol/gsm_04_08.h>
40 #include <osmocore/protocol/gsm_08_58.h>
41 #include <osmocore/rsl.h>
42
43 #include <osmocom/l1ctl.h>
44 #include <osmocom/osmocom_data.h>
45 #include <osmocom/l1l2_interface.h>
46 #include <osmocom/lapdm.h>
47 #include <osmocom/logging.h>
48 #include <osmocom/gsmtap_util.h>
49
50 static struct msgb *osmo_l1_alloc(uint8_t msg_type)
51 {
52         struct l1ctl_hdr *l1h;
53         struct msgb *msg = msgb_alloc_headroom(256, 4, "osmo_l1");
54
55         if (!msg) {
56                 LOGP(DL1C, LOGL_ERROR, "Failed to allocate memory.\n");
57                 return NULL;
58         }
59
60         msg->l1h = msgb_put(msg, sizeof(*l1h));
61         l1h = (struct l1ctl_hdr *) msg->l1h;
62         l1h->msg_type = msg_type;
63         
64         return msg;
65 }
66
67
68 static int osmo_make_band_arfcn(struct osmocom_ms *ms, uint16_t arfcn)
69 {
70         /* TODO: Include the band */
71         return arfcn;
72 }
73
74 static int rx_l1_fbsb_conf(struct osmocom_ms *ms, struct msgb *msg)
75 {
76         struct l1ctl_info_dl *dl;
77         struct l1ctl_fbsb_conf *sb;
78         struct gsm_time tm;
79
80         if (msgb_l3len(msg) < sizeof(*dl) + sizeof(*sb)) {
81                 LOGP(DL1C, LOGL_ERROR, "FBSB RESP: MSG too short %u\n",
82                         msgb_l3len(msg));
83                 return -1;
84         }
85
86         dl = (struct l1ctl_info_dl *) msg->l1h;
87         sb = (struct l1ctl_fbsb_conf *) dl->payload;
88
89         printf("snr=%04x, arfcn=%u result=%u\n", dl->snr, ntohs(dl->band_arfcn),
90                 sb->result);
91
92         if (sb->result != 0) {
93                 LOGP(DL1C, LOGL_ERROR, "FBSB RESP: result=%u\n", sb->result);
94                 dispatch_signal(SS_L1CTL, S_L1CTL_FBSB_ERR, ms);
95                 return 0;
96         }
97
98         gsm_fn2gsmtime(&tm, ntohl(dl->frame_nr));
99         DEBUGP(DL1C, "SCH: SNR: %u TDMA: (%.4u/%.2u/%.2u) bsic: %d\n",
100                 dl->snr, tm.t1, tm.t2, tm.t3, sb->bsic);
101         dispatch_signal(SS_L1CTL, S_L1CTL_FBSB_RESP, ms);
102
103         return 0;
104 }
105
106 static int rx_l1_rach_conf(struct osmocom_ms *ms, struct msgb *msg)
107 {
108         struct l1ctl_info_dl *dl;
109         struct osmobb_rach_conf rc;
110
111         if (msgb_l3len(msg) < sizeof(*dl)) {
112                 LOGP(DL1C, LOGL_ERROR, "RACH CONF: MSG too short %u\n",
113                         msgb_l3len(msg));
114                 return -1;
115         }
116
117         dl = (struct l1ctl_info_dl *) msg->l1h;
118
119         rc.fn = htonl(dl->frame_nr);
120         rc.ms = ms;
121         dispatch_signal(SS_L1CTL, S_L1CTL_RACH_CONF, &rc);
122
123         return 0;
124 }
125
126 char *chan_nr2string(uint8_t chan_nr)
127 {
128         static char str[20];
129         uint8_t cbits = chan_nr >> 3;
130
131         str[0] = '\0';
132
133         if (cbits == 0x01)
134                 sprintf(str, "TCH/F");
135         else if ((cbits & 0x1e) == 0x02)
136                 sprintf(str, "TCH/H(%u)", cbits & 0x01);
137         else if ((cbits & 0x1c) == 0x04)
138                 sprintf(str, "SDCCH/4(%u)", cbits & 0x03);
139         else if ((cbits & 0x18) == 0x08)
140                 sprintf(str, "SDCCH/8(%u)", cbits & 0x07);
141         else if (cbits == 0x10)
142                 sprintf(str, "BCCH");
143         else if (cbits == 0x11)
144                 sprintf(str, "RACH");
145         else if (cbits == 0x12)
146                 sprintf(str, "PCH/AGCH");
147         else
148                 sprintf(str, "UNKNOWN");
149
150         return str;
151 }
152
153 /* Receive L1CTL_DATA_IND (Data Indication from L1) */
154 static int rx_ph_data_ind(struct osmocom_ms *ms, struct msgb *msg)
155 {
156         struct l1ctl_info_dl *dl, dl_cpy;
157         struct l1ctl_data_ind *ccch;
158         struct lapdm_entity *le;
159         uint8_t chan_type, chan_ts, chan_ss;
160         uint8_t gsmtap_chan_type;
161         struct gsm_time tm;
162
163         if (msgb_l3len(msg) < sizeof(*ccch)) {
164                 LOGP(DL1C, LOGL_ERROR, "MSG too short Data Ind: %u\n",
165                         msgb_l3len(msg));
166                 msgb_free(msg);
167                 return -1;
168         }
169
170         dl = (struct l1ctl_info_dl *) msg->l1h;
171         msg->l2h = dl->payload;
172         ccch = (struct l1ctl_data_ind *) msg->l2h;
173
174         gsm_fn2gsmtime(&tm, ntohl(dl->frame_nr));
175         rsl_dec_chan_nr(dl->chan_nr, &chan_type, &chan_ss, &chan_ts);
176         DEBUGP(DL1C, "%s (%.4u/%.2u/%.2u) %s\n",
177                 chan_nr2string(dl->chan_nr), tm.t1, tm.t2, tm.t3,
178                 hexdump(ccch->data, sizeof(ccch->data)));
179
180         if (dl->num_biterr) {
181                 LOGP(DL1C, LOGL_NOTICE, "Dropping frame with %u bit errors\n",
182                         dl->num_biterr);
183                 return 0;
184         }
185
186         /* send CCCH data via GSMTAP */
187         gsmtap_chan_type = chantype_rsl2gsmtap(chan_type, dl->link_id);
188         gsmtap_sendmsg(ntohs(dl->band_arfcn), chan_ts, gsmtap_chan_type, chan_ss,
189                         tm.fn, dl->rx_level-110, dl->snr, ccch->data,
190                         sizeof(ccch->data));
191
192         /* determine LAPDm entity based on SACCH or not */
193         if (dl->link_id & 0x40)
194                 le = &ms->l2_entity.lapdm_acch;
195         else
196                 le = &ms->l2_entity.lapdm_dcch;
197         /* make local stack copy of l1ctl_info_dl, as LAPDm will
198          * overwrite skb hdr */
199         memcpy(&dl_cpy, dl, sizeof(dl_cpy));
200
201         /* pull the L1 header from the msgb */
202         msgb_pull(msg, msg->l2h - (msg->l1h-sizeof(struct l1ctl_hdr)));
203         msg->l1h = NULL;
204
205         /* send it up into LAPDm */
206         l2_ph_data_ind(msg, le, &dl_cpy);
207
208         return 0;
209 }
210
211 /* Receive L1CTL_DATA_CONF (Data Confirm from L1) */
212 static int rx_ph_data_conf(struct osmocom_ms *ms, struct msgb *msg)
213 {
214         struct l1ctl_info_dl *dl;
215         struct lapdm_entity *le;
216
217         dl = (struct l1ctl_info_dl *) msg->l1h;
218
219         /* determine LAPDm entity based on SACCH or not */
220         if (dl->link_id & 0x40)
221                 le = &ms->l2_entity.lapdm_acch;
222         else
223                 le = &ms->l2_entity.lapdm_dcch;
224
225         /* send it up into LAPDm */
226         l2_ph_data_conf(msg, le);
227
228         return 0;
229 }
230
231 /* Transmit L1CTL_DATA_REQ */
232 int tx_ph_data_req(struct osmocom_ms *ms, struct msgb *msg,
233                    uint8_t chan_nr, uint8_t link_id)
234 {
235         struct l1ctl_hdr *l1h;
236         struct l1ctl_info_ul *l1i_ul;
237         uint8_t chan_type, chan_ts, chan_ss;
238         uint8_t gsmtap_chan_type;
239
240         DEBUGP(DL1C, "(%s)\n", hexdump(msg->l2h, msgb_l2len(msg)));
241
242         if (msgb_l2len(msg) > 23) {
243                 LOGP(DL1C, LOGL_ERROR, "L1 cannot handle message length "
244                         "> 23 (%u)\n", msgb_l2len(msg));
245                 msgb_free(msg);
246                 return -EINVAL;
247         } else if (msgb_l2len(msg) < 23)
248                 LOGP(DL1C, LOGL_ERROR, "L1 message length < 23 (%u) "
249                         "doesn't seem right!\n", msgb_l2len(msg));
250
251         /* send copy via GSMTAP */
252         rsl_dec_chan_nr(chan_nr, &chan_type, &chan_ss, &chan_ts);
253         gsmtap_chan_type = chantype_rsl2gsmtap(chan_type, link_id);
254         gsmtap_sendmsg(0|0x4000, chan_ts, gsmtap_chan_type, chan_ss,
255                         0, 127, 255, msg->l2h, msgb_l2len(msg));
256
257         /* prepend uplink info header */
258         l1i_ul = (struct l1ctl_info_ul *) msgb_push(msg, sizeof(*l1i_ul));
259
260         l1i_ul->chan_nr = chan_nr;
261         l1i_ul->link_id = link_id;
262
263         /* FIXME: where to get this from? */
264         l1i_ul->tx_power = 0;
265
266         /* prepend l1 header */
267         msg->l1h = msgb_push(msg, sizeof(*l1h));
268         l1h = (struct l1ctl_hdr *) msg->l1h;
269         l1h->msg_type = L1CTL_DATA_REQ;
270
271         return osmo_send_l1(ms, msg);
272 }
273
274 /* Transmit FBSB_REQ */
275 int l1ctl_tx_fbsb_req(struct osmocom_ms *ms, uint16_t arfcn,
276                       uint8_t flags, uint16_t timeout, uint8_t sync_info_idx,
277                       uint8_t ccch_mode)
278 {
279         struct msgb *msg;
280         struct l1ctl_fbsb_req *req;
281
282         msg = osmo_l1_alloc(L1CTL_FBSB_REQ);
283         if (!msg)
284                 return -1;
285
286         req = (struct l1ctl_fbsb_req *) msgb_put(msg, sizeof(*req));
287         req->band_arfcn = htons(osmo_make_band_arfcn(ms, arfcn));
288         req->timeout = htons(timeout);
289         /* Threshold when to consider FB_MODE1: 4kHz - 1kHz */
290         req->freq_err_thresh1 = htons(4000 - 1000);
291         /* Threshold when to consider SCH: 1kHz - 200Hz */
292         req->freq_err_thresh2 = htons(1000 - 200);
293         /* not used yet! */
294         req->num_freqerr_avg = 3;
295         req->flags = flags;
296         req->sync_info_idx = sync_info_idx;
297         req->ccch_mode = ccch_mode;
298
299         return osmo_send_l1(ms, msg);
300 }
301
302 /* Transmit L1CTL_CCCH_MODE_REQ */
303 int l1ctl_tx_ccch_mode_req(struct osmocom_ms *ms, uint8_t ccch_mode)
304 {
305         struct msgb *msg;
306         struct l1ctl_ccch_mode_req *req;
307
308         msg = osmo_l1_alloc(L1CTL_CCCH_MODE_REQ);
309         if (!msg)
310                 return -1;
311
312         req = (struct l1ctl_ccch_mode_req *) msgb_put(msg, sizeof(*req));
313         req->ccch_mode = ccch_mode;
314
315         return osmo_send_l1(ms, msg);
316 }
317
318 /* Transmit L1CTL_RACH_REQ */
319 int tx_ph_rach_req(struct osmocom_ms *ms)
320 {
321         struct msgb *msg;
322         struct l1ctl_info_ul *ul;
323         struct l1ctl_rach_req *req;
324         static uint8_t i = 0;
325
326         msg = osmo_l1_alloc(L1CTL_RACH_REQ);
327         if (!msg)
328                 return -1;
329
330         DEBUGP(DL1C, "RACH Req.\n");
331         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
332         req = (struct l1ctl_rach_req *) msgb_put(msg, sizeof(*req));
333         req->ra = i++;
334
335         return osmo_send_l1(ms, msg);
336 }
337
338 /* Transmit L1CTL_DM_EST_REQ */
339 int tx_ph_dm_est_req(struct osmocom_ms *ms, uint16_t band_arfcn, uint8_t chan_nr,
340                      uint8_t tsc)
341 {
342         struct msgb *msg;
343         struct l1ctl_info_ul *ul;
344         struct l1ctl_dm_est_req *req;
345
346         msg = osmo_l1_alloc(L1CTL_DM_EST_REQ);
347         if (!msg)
348                 return -1;
349
350         DEBUGP(DL1C, "Tx Dedic.Mode Est Req (arfcn=%u, chan_nr=0x%02x)\n",
351                 band_arfcn, chan_nr);
352
353         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
354         ul->chan_nr = chan_nr;
355         ul->link_id = 0;
356         ul->tx_power = 0; /* FIXME: initial TX power */
357
358         req = (struct l1ctl_dm_est_req *) msgb_put(msg, sizeof(*req));
359         req->tsc = tsc;
360         req->h = 0;
361         req->h0.band_arfcn = htons(band_arfcn);
362
363         return osmo_send_l1(ms, msg);
364 }
365
366 /* Transmit L1CTL_DM_REL_REQ */
367 int tx_ph_dm_rel_req(struct osmocom_ms *ms)
368 {
369         struct msgb *msg;
370         struct l1ctl_info_ul *ul;
371
372         msg = osmo_l1_alloc(L1CTL_DM_REL_REQ);
373         if (!msg)
374                 return -1;
375
376         DEBUGP(DL1C, "Tx Dedic.Mode Rel Req\n");
377
378         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
379
380         return osmo_send_l1(ms, msg);
381 }
382
383 int l1ctl_tx_echo_req(struct osmocom_ms *ms, unsigned int len)
384 {
385         struct msgb *msg;
386         uint8_t *data;
387         unsigned int i;
388
389         msg = osmo_l1_alloc(L1CTL_ECHO_REQ);
390         if (!msg)
391                 return -1;
392
393         data = msgb_put(msg, len);
394         for (i = 0; i < len; i++)
395                 data[i] = i % 8;
396
397         return osmo_send_l1(ms, msg);
398 }
399
400 /* Transmit L1CTL_PM_REQ */
401 int l1ctl_tx_pm_req_range(struct osmocom_ms *ms, uint16_t arfcn_from,
402                           uint16_t arfcn_to)
403 {
404         struct msgb *msg;
405         struct l1ctl_pm_req *pm;
406
407         msg = osmo_l1_alloc(L1CTL_PM_REQ);
408         if (!msg)
409                 return -1;
410
411         printf("Tx PM Req (%u-%u)\n", arfcn_from, arfcn_to);
412         pm = (struct l1ctl_pm_req *) msgb_put(msg, sizeof(*pm));
413         pm->type = 1;
414         pm->range.band_arfcn_from = htons(arfcn_from);
415         pm->range.band_arfcn_to = htons(arfcn_to);
416
417         return osmo_send_l1(ms, msg);
418 }
419
420 /* Transmit L1CTL_RESET_REQ */
421 int l1ctl_tx_reset_req(struct osmocom_ms *ms, uint8_t type)
422 {
423         struct msgb *msg;
424         struct l1ctl_reset *res;
425
426         msg = osmo_l1_alloc(L1CTL_RESET_REQ);
427         if (!msg)
428                 return -1;
429
430         printf("Tx Reset Req (%u)\n", type);
431         res = (struct l1ctl_reset *) msgb_put(msg, sizeof(*res));
432         res->type = type;
433
434         return osmo_send_l1(ms, msg);
435 }
436
437 /* Receive L1CTL_RESET_IND */
438 static int rx_l1_reset(struct osmocom_ms *ms)
439 {
440         printf("Layer1 Reset.\n");
441         dispatch_signal(SS_L1CTL, S_L1CTL_RESET, ms);
442
443         return 0;
444 }
445
446 /* Receive L1CTL_PM_CONF */
447 static int rx_l1_pm_conf(struct osmocom_ms *ms, struct msgb *msg)
448 {
449         struct l1ctl_pm_conf *pmr;
450
451         for (pmr = (struct l1ctl_pm_conf *) msg->l1h;
452              (uint8_t *) pmr < msg->tail; pmr++) {
453                 struct osmobb_meas_res mr;
454                 DEBUGP(DL1C, "PM MEAS: ARFCN: %4u RxLev: %3d %3d\n",
455                         ntohs(pmr->band_arfcn), pmr->pm[0], pmr->pm[1]);
456                 mr.band_arfcn = ntohs(pmr->band_arfcn);
457                 mr.rx_lev = (pmr->pm[0] + pmr->pm[1]) / 2;
458                 mr.ms = ms;
459                 dispatch_signal(SS_L1CTL, S_L1CTL_PM_RES, &mr);
460         }
461         return 0;
462 }
463
464 /* Receive incoming data from L1 using L1CTL format */
465 int l1ctl_recv(struct osmocom_ms *ms, struct msgb *msg)
466 {
467         int rc = 0;
468         struct l1ctl_hdr *l1h;
469         struct l1ctl_info_dl *dl;
470
471         if (msgb_l2len(msg) < sizeof(*dl)) {
472                 LOGP(DL1C, LOGL_ERROR, "Short Layer2 message: %u\n",
473                         msgb_l2len(msg));
474                 msgb_free(msg);
475                 return -1;
476         }
477
478         l1h = (struct l1ctl_hdr *) msg->l1h;
479
480         /* move the l1 header pointer to point _BEHIND_ l1ctl_hdr,
481            as the l1ctl header is of no interest to subsequent code */
482         msg->l1h = l1h->data;
483
484         switch (l1h->msg_type) {
485         case L1CTL_FBSB_CONF:
486                 rc = rx_l1_fbsb_conf(ms, msg);
487                 msgb_free(msg);
488                 break;
489         case L1CTL_DATA_IND:
490                 rc = rx_ph_data_ind(ms, msg);
491                 break;
492         case L1CTL_DATA_CONF:
493                 rc = rx_ph_data_conf(ms, msg);
494                 break;
495         case L1CTL_RESET_IND:
496         case L1CTL_RESET_CONF:
497                 rc = rx_l1_reset(ms);
498                 msgb_free(msg);
499                 break;
500         case L1CTL_PM_CONF:
501                 rc = rx_l1_pm_conf(ms, msg);
502                 msgb_free(msg);
503                 if (l1h->flags & L1CTL_F_DONE)
504                         dispatch_signal(SS_L1CTL, S_L1CTL_PM_DONE, ms);
505                 break;
506         case L1CTL_RACH_CONF:
507                 rc = rx_l1_rach_conf(ms, msg);
508                 msgb_free(msg);
509                 break;
510         default:
511                 fprintf(stderr, "Unknown MSG: %u\n", l1h->msg_type);
512                 msgb_free(msg);
513                 break;
514         }
515
516         return rc;
517 }