[layer23] Updated layer23 to current L1 support and forthcomming hopping.
[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_h0(struct osmocom_ms *ms, uint16_t band_arfcn,
340         uint8_t chan_nr, uint8_t tsc, uint8_t tx_power)
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 = 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 int tx_ph_dm_est_req_h1(struct osmocom_ms *ms, uint8_t maio, uint8_t hsn,
367         uint16_t *ma, uint8_t ma_len, uint8_t chan_nr, uint8_t tsc,
368         uint8_t tx_power)
369 {
370         struct msgb *msg;
371         struct l1ctl_info_ul *ul;
372         struct l1ctl_dm_est_req *req;
373         int i;
374
375         msg = osmo_l1_alloc(L1CTL_DM_EST_REQ);
376         if (!msg)
377                 return -1;
378
379         DEBUGP(DL1C, "Tx Dedic.Mode Est Req (maio=%u, hsn=%u, "
380                 "chan_nr=0x%02x)\n", maio, hsn, chan_nr);
381
382         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
383         ul->chan_nr = chan_nr;
384         ul->link_id = 0;
385         ul->tx_power = tx_power;
386
387         req = (struct l1ctl_dm_est_req *) msgb_put(msg, sizeof(*req));
388         req->tsc = tsc;
389         req->h = 1;
390         req->h1.maio = maio;
391         req->h1.hsn = hsn;
392         req->h1.n = ma_len;
393         for (i = 0; i < ma_len; i++)
394                 req->h1.ma[i] = htons(ma[i]);
395
396         return osmo_send_l1(ms, msg);
397 }
398
399 /* Transmit L1CTL_DM_REL_REQ */
400 int tx_ph_dm_rel_req(struct osmocom_ms *ms)
401 {
402         struct msgb *msg;
403         struct l1ctl_info_ul *ul;
404
405         msg = osmo_l1_alloc(L1CTL_DM_REL_REQ);
406         if (!msg)
407                 return -1;
408
409         DEBUGP(DL1C, "Tx Dedic.Mode Rel Req\n");
410
411         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
412
413         return osmo_send_l1(ms, msg);
414 }
415
416 int l1ctl_tx_echo_req(struct osmocom_ms *ms, unsigned int len)
417 {
418         struct msgb *msg;
419         uint8_t *data;
420         unsigned int i;
421
422         msg = osmo_l1_alloc(L1CTL_ECHO_REQ);
423         if (!msg)
424                 return -1;
425
426         data = msgb_put(msg, len);
427         for (i = 0; i < len; i++)
428                 data[i] = i % 8;
429
430         return osmo_send_l1(ms, msg);
431 }
432
433 /* Transmit L1CTL_PM_REQ */
434 int l1ctl_tx_pm_req_range(struct osmocom_ms *ms, uint16_t arfcn_from,
435                           uint16_t arfcn_to)
436 {
437         struct msgb *msg;
438         struct l1ctl_pm_req *pm;
439
440         msg = osmo_l1_alloc(L1CTL_PM_REQ);
441         if (!msg)
442                 return -1;
443
444         printf("Tx PM Req (%u-%u)\n", arfcn_from, arfcn_to);
445         pm = (struct l1ctl_pm_req *) msgb_put(msg, sizeof(*pm));
446         pm->type = 1;
447         pm->range.band_arfcn_from = htons(arfcn_from);
448         pm->range.band_arfcn_to = htons(arfcn_to);
449
450         return osmo_send_l1(ms, msg);
451 }
452
453 /* Transmit L1CTL_RESET_REQ */
454 int l1ctl_tx_reset_req(struct osmocom_ms *ms, uint8_t type)
455 {
456         struct msgb *msg;
457         struct l1ctl_reset *res;
458
459         msg = osmo_l1_alloc(L1CTL_RESET_REQ);
460         if (!msg)
461                 return -1;
462
463         printf("Tx Reset Req (%u)\n", type);
464         res = (struct l1ctl_reset *) msgb_put(msg, sizeof(*res));
465         res->type = type;
466
467         return osmo_send_l1(ms, msg);
468 }
469
470 /* Receive L1CTL_RESET_IND */
471 static int rx_l1_reset(struct osmocom_ms *ms)
472 {
473         printf("Layer1 Reset.\n");
474         dispatch_signal(SS_L1CTL, S_L1CTL_RESET, ms);
475
476         return 0;
477 }
478
479 /* Receive L1CTL_PM_CONF */
480 static int rx_l1_pm_conf(struct osmocom_ms *ms, struct msgb *msg)
481 {
482         struct l1ctl_pm_conf *pmr;
483
484         for (pmr = (struct l1ctl_pm_conf *) msg->l1h;
485              (uint8_t *) pmr < msg->tail; pmr++) {
486                 struct osmobb_meas_res mr;
487                 DEBUGP(DL1C, "PM MEAS: ARFCN: %4u RxLev: %3d %3d\n",
488                         ntohs(pmr->band_arfcn), pmr->pm[0], pmr->pm[1]);
489                 mr.band_arfcn = ntohs(pmr->band_arfcn);
490                 mr.rx_lev = (pmr->pm[0] + pmr->pm[1]) / 2;
491                 mr.ms = ms;
492                 dispatch_signal(SS_L1CTL, S_L1CTL_PM_RES, &mr);
493         }
494         return 0;
495 }
496
497 /* Receive L1CTL_MODE_CONF */
498 static int rx_l1_ccch_mode_conf(struct osmocom_ms *ms, struct msgb *msg)
499 {
500         struct osmobb_ccch_mode_conf mc;
501         struct l1ctl_ccch_mode_conf *conf;
502
503         if (msgb_l3len(msg) < sizeof(*conf)) {
504                 LOGP(DL1C, LOGL_ERROR, "MODE CONF: MSG too short %u\n",
505                         msgb_l3len(msg));
506                 return -1;
507         }
508
509         conf = (struct l1ctl_ccch_mode_conf *) msg->l1h;
510
511         printf("mode=%u\n", conf->ccch_mode);
512
513         mc.ccch_mode = conf->ccch_mode;
514         mc.ms = ms;
515         dispatch_signal(SS_L1CTL, S_L1CTL_CCCH_MODE_CONF, &mc);
516
517         return 0;
518 }
519
520 /* Receive incoming data from L1 using L1CTL format */
521 int l1ctl_recv(struct osmocom_ms *ms, struct msgb *msg)
522 {
523         int rc = 0;
524         struct l1ctl_hdr *l1h;
525         struct l1ctl_info_dl *dl;
526
527         if (msgb_l2len(msg) < sizeof(*dl)) {
528                 LOGP(DL1C, LOGL_ERROR, "Short Layer2 message: %u\n",
529                         msgb_l2len(msg));
530                 msgb_free(msg);
531                 return -1;
532         }
533
534         l1h = (struct l1ctl_hdr *) msg->l1h;
535
536         /* move the l1 header pointer to point _BEHIND_ l1ctl_hdr,
537            as the l1ctl header is of no interest to subsequent code */
538         msg->l1h = l1h->data;
539
540         switch (l1h->msg_type) {
541         case L1CTL_FBSB_CONF:
542                 rc = rx_l1_fbsb_conf(ms, msg);
543                 msgb_free(msg);
544                 break;
545         case L1CTL_DATA_IND:
546                 rc = rx_ph_data_ind(ms, msg);
547                 break;
548         case L1CTL_DATA_CONF:
549                 rc = rx_ph_data_conf(ms, msg);
550                 break;
551         case L1CTL_RESET_IND:
552         case L1CTL_RESET_CONF:
553                 rc = rx_l1_reset(ms);
554                 msgb_free(msg);
555                 break;
556         case L1CTL_PM_CONF:
557                 rc = rx_l1_pm_conf(ms, msg);
558                 msgb_free(msg);
559                 if (l1h->flags & L1CTL_F_DONE)
560                         dispatch_signal(SS_L1CTL, S_L1CTL_PM_DONE, ms);
561                 break;
562         case L1CTL_RACH_CONF:
563                 rc = rx_l1_rach_conf(ms, msg);
564                 msgb_free(msg);
565                 break;
566         case L1CTL_CCCH_MODE_CONF:
567                 rc = rx_l1_ccch_mode_conf(ms, msg);
568                 msgb_free(msg);
569                 break;
570         default:
571                 fprintf(stderr, "Unknown MSG: %u\n", l1h->msg_type);
572                 msgb_free(msg);
573                 break;
574         }
575
576         return rc;
577 }