src: use namespace prefix osmo_signal*
[osmocom-bb.git] / src / host / layer23 / src / common / 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 <l1ctl_proto.h>
32
33 #include <osmocom/core/signal.h>
34 #include <osmocom/core/logging.h>
35 #include <osmocom/core/timer.h>
36 #include <osmocom/core/msgb.h>
37 #include <osmocom/gsm/tlv.h>
38 #include <osmocom/gsm/gsm_utils.h>
39 #include <osmocom/core/gsmtap_util.h>
40 #include <osmocom/gsm/protocol/gsm_04_08.h>
41 #include <osmocom/gsm/protocol/gsm_08_58.h>
42 #include <osmocom/gsm/rsl.h>
43
44 #include <osmocom/bb/common/l1ctl.h>
45 #include <osmocom/bb/common/osmocom_data.h>
46 #include <osmocom/bb/common/l1l2_interface.h>
47 #include <osmocom/bb/common/lapdm.h>
48 #include <osmocom/bb/common/logging.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         struct osmobb_fbsb_res fr;
80
81         if (msgb_l3len(msg) < sizeof(*dl) + sizeof(*sb)) {
82                 LOGP(DL1C, LOGL_ERROR, "FBSB RESP: MSG too short %u\n",
83                         msgb_l3len(msg));
84                 return -1;
85         }
86
87         dl = (struct l1ctl_info_dl *) msg->l1h;
88         sb = (struct l1ctl_fbsb_conf *) dl->payload;
89
90         LOGP(DL1C, LOGL_INFO, "snr=%04x, arfcn=%u result=%u\n", dl->snr,
91                 ntohs(dl->band_arfcn), sb->result);
92
93         if (sb->result != 0) {
94                 LOGP(DL1C, LOGL_ERROR, "FBSB RESP: result=%u\n", sb->result);
95                 osmo_signal_dispatch(SS_L1CTL, S_L1CTL_FBSB_ERR, ms);
96                 return 0;
97         }
98
99         gsm_fn2gsmtime(&tm, ntohl(dl->frame_nr));
100         DEBUGP(DL1C, "SCH: SNR: %u TDMA: (%.4u/%.2u/%.2u) bsic: %d\n",
101                 dl->snr, tm.t1, tm.t2, tm.t3, sb->bsic);
102         fr.ms = ms;
103         fr.snr = dl->snr;
104         fr.bsic = sb->bsic;
105         osmo_signal_dispatch(SS_L1CTL, S_L1CTL_FBSB_RESP, &fr);
106
107         return 0;
108 }
109
110 static int rx_l1_rach_conf(struct osmocom_ms *ms, struct msgb *msg)
111 {
112         struct l1ctl_info_dl *dl;
113
114         if (msgb_l2len(msg) < sizeof(*dl)) {
115                 LOGP(DL1C, LOGL_ERROR, "RACH CONF: MSG too short %u\n",
116                         msgb_l3len(msg));
117                 msgb_free(msg);
118                 return -1;
119         }
120
121         dl = (struct l1ctl_info_dl *) msg->l1h;
122
123         l2_ph_chan_conf(msg, ms, dl);
124
125         return 0;
126 }
127
128 /* Receive L1CTL_DATA_IND (Data Indication from L1) */
129 static int rx_ph_data_ind(struct osmocom_ms *ms, struct msgb *msg)
130 {
131         struct l1ctl_info_dl *dl, dl_cpy;
132         struct l1ctl_data_ind *ccch;
133         struct lapdm_entity *le;
134         struct rx_meas_stat *meas = &ms->meas;
135         uint8_t chan_type, chan_ts, chan_ss;
136         uint8_t gsmtap_chan_type;
137         struct gsm_time tm;
138
139         if (msgb_l3len(msg) < sizeof(*ccch)) {
140                 LOGP(DL1C, LOGL_ERROR, "MSG too short Data Ind: %u\n",
141                         msgb_l3len(msg));
142                 msgb_free(msg);
143                 return -1;
144         }
145
146         dl = (struct l1ctl_info_dl *) msg->l1h;
147         msg->l2h = dl->payload;
148         ccch = (struct l1ctl_data_ind *) msg->l2h;
149
150         gsm_fn2gsmtime(&tm, ntohl(dl->frame_nr));
151         rsl_dec_chan_nr(dl->chan_nr, &chan_type, &chan_ss, &chan_ts);
152         DEBUGP(DL1C, "%s (%.4u/%.2u/%.2u) %d dBm: %s\n",
153                 rsl_chan_nr_str(dl->chan_nr), tm.t1, tm.t2, tm.t3,
154                 (int)dl->rx_level-110,
155                 hexdump(ccch->data, sizeof(ccch->data)));
156
157         meas->last_fn = ntohl(dl->frame_nr);
158         meas->frames++;
159         meas->snr += dl->snr;
160         meas->berr += dl->num_biterr;
161         meas->rxlev += dl->rx_level;
162
163         /* counting loss criteria */
164         if (!(dl->link_id & 0x40)) {
165                 switch (chan_type) {
166                 case RSL_CHAN_PCH_AGCH:
167                         if (!meas->ds_fail)
168                                 break;
169                         if (dl->fire_crc >= 2)
170                                 meas->dsc -= 4;
171                         else
172                                 meas->dsc += 1;
173                         if (meas->dsc > meas->ds_fail)
174                                 meas->dsc = meas->ds_fail;
175                         if (meas->dsc < meas->ds_fail)
176                                 printf("LOSS counter for CCCH %d\n", meas->dsc);
177                         if (meas->dsc > 0)
178                                 break;
179                         meas->ds_fail = 0;
180                         osmo_signal_dispatch(SS_L1CTL, S_L1CTL_LOSS_IND, ms);
181                         break;
182                 }
183         } else {
184                 switch (chan_type) {
185                 case RSL_CHAN_Bm_ACCHs:
186                 case RSL_CHAN_Lm_ACCHs:
187                 case RSL_CHAN_SDCCH4_ACCH:
188                 case RSL_CHAN_SDCCH8_ACCH:
189                         if (!meas->rl_fail)
190                                 break;
191                         if (dl->fire_crc >= 2)
192                                 meas->s -= 1;
193                         else
194                                 meas->s += 2;
195                         if (meas->s > meas->rl_fail)
196                                 meas->s = meas->rl_fail;
197                         if (meas->s < meas->rl_fail)
198                                 printf("LOSS counter for ACCH %d\n", meas->s);
199                         if (meas->s > 0)
200                                 break;
201                         meas->rl_fail = 0;
202                         osmo_signal_dispatch(SS_L1CTL, S_L1CTL_LOSS_IND, ms);
203                         break;
204                 }
205         }
206
207         if (dl->fire_crc >= 2) {
208 printf("Dropping frame with %u bit errors\n", dl->num_biterr);
209                 LOGP(DL1C, LOGL_NOTICE, "Dropping frame with %u bit errors\n",
210                         dl->num_biterr);
211                 msgb_free(msg);
212                 return 0;
213         }
214
215         /* send CCCH data via GSMTAP */
216         gsmtap_chan_type = chantype_rsl2gsmtap(chan_type, dl->link_id);
217         gsmtap_sendmsg(ntohs(dl->band_arfcn), chan_ts, gsmtap_chan_type, chan_ss,
218                         tm.fn, dl->rx_level-110, dl->snr, ccch->data,
219                         sizeof(ccch->data));
220
221         /* determine LAPDm entity based on SACCH or not */
222         if (dl->link_id & 0x40)
223                 le = &ms->l2_entity.lapdm_acch;
224         else
225                 le = &ms->l2_entity.lapdm_dcch;
226         /* make local stack copy of l1ctl_info_dl, as LAPDm will
227          * overwrite skb hdr */
228         memcpy(&dl_cpy, dl, sizeof(dl_cpy));
229
230         /* pull the L1 header from the msgb */
231         msgb_pull(msg, msg->l2h - (msg->l1h-sizeof(struct l1ctl_hdr)));
232         msg->l1h = NULL;
233
234         /* send it up into LAPDm */
235         l2_ph_data_ind(msg, le, &dl_cpy);
236
237         return 0;
238 }
239
240 /* Receive L1CTL_DATA_CONF (Data Confirm from L1) */
241 static int rx_ph_data_conf(struct osmocom_ms *ms, struct msgb *msg)
242 {
243         struct l1ctl_info_dl *dl;
244         struct lapdm_entity *le;
245
246         dl = (struct l1ctl_info_dl *) msg->l1h;
247
248         /* determine LAPDm entity based on SACCH or not */
249         if (dl->link_id & 0x40)
250                 le = &ms->l2_entity.lapdm_acch;
251         else
252                 le = &ms->l2_entity.lapdm_dcch;
253
254         /* send it up into LAPDm */
255         l2_ph_data_conf(msg, le);
256
257         return 0;
258 }
259
260 /* Transmit L1CTL_DATA_REQ */
261 int l1ctl_tx_data_req(struct osmocom_ms *ms, struct msgb *msg,
262                       uint8_t chan_nr, uint8_t link_id)
263 {
264         struct l1ctl_hdr *l1h;
265         struct l1ctl_info_ul *l1i_ul;
266         uint8_t chan_type, chan_ts, chan_ss;
267         uint8_t gsmtap_chan_type;
268
269         DEBUGP(DL1C, "(%s)\n", hexdump(msg->l2h, msgb_l2len(msg)));
270
271         if (msgb_l2len(msg) > 23) {
272                 LOGP(DL1C, LOGL_ERROR, "L1 cannot handle message length "
273                         "> 23 (%u)\n", msgb_l2len(msg));
274                 msgb_free(msg);
275                 return -EINVAL;
276         } else if (msgb_l2len(msg) < 23)
277                 LOGP(DL1C, LOGL_ERROR, "L1 message length < 23 (%u) "
278                         "doesn't seem right!\n", msgb_l2len(msg));
279
280         /* send copy via GSMTAP */
281         rsl_dec_chan_nr(chan_nr, &chan_type, &chan_ss, &chan_ts);
282         gsmtap_chan_type = chantype_rsl2gsmtap(chan_type, link_id);
283         gsmtap_sendmsg(0|0x4000, chan_ts, gsmtap_chan_type, chan_ss,
284                         0, 127, 255, msg->l2h, msgb_l2len(msg));
285
286         /* prepend uplink info header */
287         l1i_ul = (struct l1ctl_info_ul *) msgb_push(msg, sizeof(*l1i_ul));
288
289         l1i_ul->chan_nr = chan_nr;
290         l1i_ul->link_id = link_id;
291
292         /* prepend l1 header */
293         msg->l1h = msgb_push(msg, sizeof(*l1h));
294         l1h = (struct l1ctl_hdr *) msg->l1h;
295         l1h->msg_type = L1CTL_DATA_REQ;
296
297         return osmo_send_l1(ms, msg);
298 }
299
300 /* Transmit FBSB_REQ */
301 int l1ctl_tx_fbsb_req(struct osmocom_ms *ms, uint16_t arfcn,
302                       uint8_t flags, uint16_t timeout, uint8_t sync_info_idx,
303                       uint8_t ccch_mode)
304 {
305         struct msgb *msg;
306         struct l1ctl_fbsb_req *req;
307
308         LOGP(DL1C, LOGL_INFO, "Sync Req\n");
309
310         msg = osmo_l1_alloc(L1CTL_FBSB_REQ);
311         if (!msg)
312                 return -1;
313
314         req = (struct l1ctl_fbsb_req *) msgb_put(msg, sizeof(*req));
315         req->band_arfcn = htons(osmo_make_band_arfcn(ms, arfcn));
316         req->timeout = htons(timeout);
317         /* Threshold when to consider FB_MODE1: 4kHz - 1kHz */
318         req->freq_err_thresh1 = htons(11000 - 1000);
319         /* Threshold when to consider SCH: 1kHz - 200Hz */
320         req->freq_err_thresh2 = htons(1000 - 200);
321         /* not used yet! */
322         req->num_freqerr_avg = 3;
323         req->flags = flags;
324         req->sync_info_idx = sync_info_idx;
325         req->ccch_mode = ccch_mode;
326
327         return osmo_send_l1(ms, msg);
328 }
329
330 /* Transmit L1CTL_CCCH_MODE_REQ */
331 int l1ctl_tx_ccch_mode_req(struct osmocom_ms *ms, uint8_t ccch_mode)
332 {
333         struct msgb *msg;
334         struct l1ctl_ccch_mode_req *req;
335
336         LOGP(DL1C, LOGL_INFO, "CCCH Mode Req\n");
337
338         msg = osmo_l1_alloc(L1CTL_CCCH_MODE_REQ);
339         if (!msg)
340                 return -1;
341
342         req = (struct l1ctl_ccch_mode_req *) msgb_put(msg, sizeof(*req));
343         req->ccch_mode = ccch_mode;
344
345         return osmo_send_l1(ms, msg);
346 }
347
348 /* Transmit L1CTL_TCH_MODE_REQ */
349 int l1ctl_tx_tch_mode_req(struct osmocom_ms *ms, uint8_t tch_mode)
350 {
351         struct msgb *msg;
352         struct l1ctl_tch_mode_req *req;
353
354         LOGP(DL1C, LOGL_INFO, "TCH Mode Req\n");
355
356         msg = osmo_l1_alloc(L1CTL_TCH_MODE_REQ);
357         if (!msg)
358                 return -1;
359
360         req = (struct l1ctl_tch_mode_req *) msgb_put(msg, sizeof(*req));
361         req->tch_mode = tch_mode;
362
363         return osmo_send_l1(ms, msg);
364 }
365
366 /* Transmit L1CTL_PARAM_REQ */
367 int l1ctl_tx_param_req(struct osmocom_ms *ms, uint8_t ta, uint8_t tx_power)
368 {
369         struct msgb *msg;
370         struct l1ctl_info_ul *ul;
371         struct l1ctl_par_req *req;
372
373         msg = osmo_l1_alloc(L1CTL_PARAM_REQ);
374         if (!msg)
375                 return -1;
376
377         DEBUGP(DL1C, "PARAM Req. ta=%d, tx_power=%d\n", ta, tx_power);
378         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
379         req = (struct l1ctl_par_req *) msgb_put(msg, sizeof(*req));
380         req->tx_power = tx_power;
381         req->ta = ta;
382
383         return osmo_send_l1(ms, msg);
384 }
385
386 /* Transmit L1CTL_CRYPTO_REQ */
387 int l1ctl_tx_crypto_req(struct osmocom_ms *ms, uint8_t algo, uint8_t *key,
388         uint8_t len)
389 {
390         struct msgb *msg;
391         struct l1ctl_info_ul *ul;
392         struct l1ctl_crypto_req *req;
393
394         msg = osmo_l1_alloc(L1CTL_CRYPTO_REQ);
395         if (!msg)
396                 return -1;
397
398         DEBUGP(DL1C, "CRYPTO Req. algo=%d, len=%d\n", algo, len);
399         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
400         req = (struct l1ctl_crypto_req *) msgb_put(msg, sizeof(*req) + len);
401         req->algo = algo;
402         if (len)
403                 memcpy(req->key, key, len);
404
405         return osmo_send_l1(ms, msg);
406 }
407
408 /* Transmit L1CTL_RACH_REQ */
409 int l1ctl_tx_rach_req(struct osmocom_ms *ms, uint8_t ra, uint16_t offset,
410         uint8_t combined)
411 {
412         struct msgb *msg;
413         struct l1ctl_info_ul *ul;
414         struct l1ctl_rach_req *req;
415
416         msg = osmo_l1_alloc(L1CTL_RACH_REQ);
417         if (!msg)
418                 return -1;
419
420         DEBUGP(DL1C, "RACH Req. offset=%d combined=%d\n", offset, combined);
421         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
422         req = (struct l1ctl_rach_req *) msgb_put(msg, sizeof(*req));
423         req->ra = ra;
424         req->offset = htons(offset);
425         req->combined = combined;
426
427         return osmo_send_l1(ms, msg);
428 }
429
430 /* Transmit L1CTL_DM_EST_REQ */
431 int l1ctl_tx_dm_est_req_h0(struct osmocom_ms *ms, uint16_t band_arfcn,
432                            uint8_t chan_nr, uint8_t tsc, uint8_t tch_mode)
433 {
434         struct msgb *msg;
435         struct l1ctl_info_ul *ul;
436         struct l1ctl_dm_est_req *req;
437
438         msg = osmo_l1_alloc(L1CTL_DM_EST_REQ);
439         if (!msg)
440                 return -1;
441
442         LOGP(DL1C, LOGL_INFO, "Tx Dedic.Mode Est Req (arfcn=%u, "
443                 "chan_nr=0x%02x)\n", band_arfcn, chan_nr);
444
445         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
446         ul->chan_nr = chan_nr;
447         ul->link_id = 0;
448
449         req = (struct l1ctl_dm_est_req *) msgb_put(msg, sizeof(*req));
450         req->tsc = tsc;
451         req->h = 0;
452         req->h0.band_arfcn = htons(band_arfcn);
453         req->tch_mode = tch_mode;
454
455         return osmo_send_l1(ms, msg);
456 }
457
458 int l1ctl_tx_dm_est_req_h1(struct osmocom_ms *ms, uint8_t maio, uint8_t hsn,
459                            uint16_t *ma, uint8_t ma_len,
460                            uint8_t chan_nr, uint8_t tsc, uint8_t tch_mode)
461 {
462         struct msgb *msg;
463         struct l1ctl_info_ul *ul;
464         struct l1ctl_dm_est_req *req;
465         int i;
466
467         msg = osmo_l1_alloc(L1CTL_DM_EST_REQ);
468         if (!msg)
469                 return -1;
470
471         LOGP(DL1C, LOGL_INFO, "Tx Dedic.Mode Est Req (maio=%u, hsn=%u, "
472                 "chan_nr=0x%02x)\n", maio, hsn, chan_nr);
473
474         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
475         ul->chan_nr = chan_nr;
476         ul->link_id = 0;
477
478         req = (struct l1ctl_dm_est_req *) msgb_put(msg, sizeof(*req));
479         req->tsc = tsc;
480         req->h = 1;
481         req->h1.maio = maio;
482         req->h1.hsn = hsn;
483         req->h1.n = ma_len;
484         for (i = 0; i < ma_len; i++)
485                 req->h1.ma[i] = htons(ma[i]);
486         req->tch_mode = tch_mode;
487
488         return osmo_send_l1(ms, msg);
489 }
490
491 /* Transmit L1CTL_DM_FREQ_REQ */
492 int l1ctl_tx_dm_freq_req_h0(struct osmocom_ms *ms, uint16_t band_arfcn,
493                             uint8_t tsc, uint16_t fn)
494 {
495         struct msgb *msg;
496         struct l1ctl_info_ul *ul;
497         struct l1ctl_dm_freq_req *req;
498
499         msg = osmo_l1_alloc(L1CTL_DM_FREQ_REQ);
500         if (!msg)
501                 return -1;
502
503         LOGP(DL1C, LOGL_INFO, "Tx Dedic.Mode Freq Req (arfcn=%u, fn=%d)\n",
504                 band_arfcn, fn);
505
506         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
507         ul->chan_nr = 0;
508         ul->link_id = 0;
509
510         req = (struct l1ctl_dm_freq_req *) msgb_put(msg, sizeof(*req));
511         req->fn = htons(fn);
512         req->tsc = tsc;
513         req->h = 0;
514         req->h0.band_arfcn = htons(band_arfcn);
515
516         return osmo_send_l1(ms, msg);
517 }
518
519 int l1ctl_tx_dm_freq_req_h1(struct osmocom_ms *ms, uint8_t maio, uint8_t hsn,
520                             uint16_t *ma, uint8_t ma_len,
521                             uint8_t tsc, uint16_t fn)
522 {
523         struct msgb *msg;
524         struct l1ctl_info_ul *ul;
525         struct l1ctl_dm_freq_req *req;
526         int i;
527
528         msg = osmo_l1_alloc(L1CTL_DM_FREQ_REQ);
529         if (!msg)
530                 return -1;
531
532         LOGP(DL1C, LOGL_INFO, "Tx Dedic.Mode Freq Req (maio=%u, hsn=%u, "
533                 "fn=%d)\n", maio, hsn, fn);
534
535         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
536         ul->chan_nr = 0;
537         ul->link_id = 0;
538
539         req = (struct l1ctl_dm_freq_req *) msgb_put(msg, sizeof(*req));
540         req->fn = htons(fn);
541         req->tsc = tsc;
542         req->h = 1;
543         req->h1.maio = maio;
544         req->h1.hsn = hsn;
545         req->h1.n = ma_len;
546         for (i = 0; i < ma_len; i++)
547                 req->h1.ma[i] = htons(ma[i]);
548
549         return osmo_send_l1(ms, msg);
550 }
551
552 /* Transmit L1CTL_DM_REL_REQ */
553 int l1ctl_tx_dm_rel_req(struct osmocom_ms *ms)
554 {
555         struct msgb *msg;
556         struct l1ctl_info_ul *ul;
557
558         msg = osmo_l1_alloc(L1CTL_DM_REL_REQ);
559         if (!msg)
560                 return -1;
561
562         LOGP(DL1C, LOGL_INFO, "Tx Dedic.Mode Rel Req\n");
563
564         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
565
566         return osmo_send_l1(ms, msg);
567 }
568
569 int l1ctl_tx_echo_req(struct osmocom_ms *ms, unsigned int len)
570 {
571         struct msgb *msg;
572         uint8_t *data;
573         unsigned int i;
574
575         msg = osmo_l1_alloc(L1CTL_ECHO_REQ);
576         if (!msg)
577                 return -1;
578
579         data = msgb_put(msg, len);
580         for (i = 0; i < len; i++)
581                 data[i] = i % 8;
582
583         return osmo_send_l1(ms, msg);
584 }
585
586 int l1ctl_tx_sim_req(struct osmocom_ms *ms, uint8_t *data, uint16_t length)
587 {
588         struct msgb *msg;
589         uint8_t *dat;
590
591         msg = osmo_l1_alloc(L1CTL_SIM_REQ);
592         if (!msg)
593                 return -1;
594
595         dat = msgb_put(msg, length);
596         memcpy(dat, data, length);
597
598         return osmo_send_l1(ms, msg);
599 }
600
601 /* just forward the SIM response to the SIM handler */
602 static int rx_l1_sim_conf(struct osmocom_ms *ms, struct msgb *msg)
603 {
604         uint16_t len = msg->len - sizeof(struct l1ctl_hdr);
605         uint8_t *data = msg->data + sizeof(struct l1ctl_hdr);
606         
607         LOGP(DL1C, LOGL_INFO, "SIM %s\n", hexdump(data, len));
608         
609         /* pull the L1 header from the msgb */
610         msgb_pull(msg, sizeof(struct l1ctl_hdr));
611         msg->l1h = NULL;
612
613         sim_apdu_resp(ms, msg);
614         
615         return 0;
616 }
617
618 /* Transmit L1CTL_PM_REQ */
619 int l1ctl_tx_pm_req_range(struct osmocom_ms *ms, uint16_t arfcn_from,
620                           uint16_t arfcn_to)
621 {
622         struct msgb *msg;
623         struct l1ctl_pm_req *pm;
624
625         msg = osmo_l1_alloc(L1CTL_PM_REQ);
626         if (!msg)
627                 return -1;
628
629         LOGP(DL1C, LOGL_INFO, "Tx PM Req (%u-%u)\n", arfcn_from, arfcn_to);
630         pm = (struct l1ctl_pm_req *) msgb_put(msg, sizeof(*pm));
631         pm->type = 1;
632         pm->range.band_arfcn_from = htons(arfcn_from);
633         pm->range.band_arfcn_to = htons(arfcn_to);
634
635         return osmo_send_l1(ms, msg);
636 }
637
638 /* Transmit L1CTL_RESET_REQ */
639 int l1ctl_tx_reset_req(struct osmocom_ms *ms, uint8_t type)
640 {
641         struct msgb *msg;
642         struct l1ctl_reset *res;
643
644         msg = osmo_l1_alloc(L1CTL_RESET_REQ);
645         if (!msg)
646                 return -1;
647
648         LOGP(DL1C, LOGL_INFO, "Tx Reset Req (%u)\n", type);
649         res = (struct l1ctl_reset *) msgb_put(msg, sizeof(*res));
650         res->type = type;
651
652         return osmo_send_l1(ms, msg);
653 }
654
655 /* Receive L1CTL_RESET_IND */
656 static int rx_l1_reset(struct osmocom_ms *ms)
657 {
658         LOGP(DL1C, LOGL_INFO, "Layer1 Reset indication\n");
659         osmo_signal_dispatch(SS_L1CTL, S_L1CTL_RESET, ms);
660
661         return 0;
662 }
663
664 /* Receive L1CTL_PM_CONF */
665 static int rx_l1_pm_conf(struct osmocom_ms *ms, struct msgb *msg)
666 {
667         struct l1ctl_pm_conf *pmr;
668
669         for (pmr = (struct l1ctl_pm_conf *) msg->l1h;
670              (uint8_t *) pmr < msg->tail; pmr++) {
671                 struct osmobb_meas_res mr;
672                 DEBUGP(DL1C, "PM MEAS: ARFCN: %4u RxLev: %3d %3d\n",
673                         ntohs(pmr->band_arfcn), pmr->pm[0], pmr->pm[1]);
674                 mr.band_arfcn = ntohs(pmr->band_arfcn);
675                 mr.rx_lev = pmr->pm[0];
676                 mr.ms = ms;
677                 osmo_signal_dispatch(SS_L1CTL, S_L1CTL_PM_RES, &mr);
678         }
679         return 0;
680 }
681
682 /* Receive L1CTL_CCCH_MODE_CONF */
683 static int rx_l1_ccch_mode_conf(struct osmocom_ms *ms, struct msgb *msg)
684 {
685         struct osmobb_ccch_mode_conf mc;
686         struct l1ctl_ccch_mode_conf *conf;
687
688         if (msgb_l3len(msg) < sizeof(*conf)) {
689                 LOGP(DL1C, LOGL_ERROR, "CCCH MODE CONF: MSG too short %u\n",
690                         msgb_l3len(msg));
691                 return -1;
692         }
693
694         conf = (struct l1ctl_ccch_mode_conf *) msg->l1h;
695
696         LOGP(DL1C, LOGL_INFO, "CCCH MODE CONF: mode=%u\n", conf->ccch_mode);
697
698         mc.ccch_mode = conf->ccch_mode;
699         mc.ms = ms;
700         osmo_signal_dispatch(SS_L1CTL, S_L1CTL_CCCH_MODE_CONF, &mc);
701
702         return 0;
703 }
704
705 /* Receive L1CTL_TCH_MODE_CONF */
706 static int rx_l1_tch_mode_conf(struct osmocom_ms *ms, struct msgb *msg)
707 {
708         struct osmobb_tch_mode_conf mc;
709         struct l1ctl_tch_mode_conf *conf;
710
711         if (msgb_l3len(msg) < sizeof(*conf)) {
712                 LOGP(DL1C, LOGL_ERROR, "TCH MODE CONF: MSG too short %u\n",
713                         msgb_l3len(msg));
714                 return -1;
715         }
716
717         conf = (struct l1ctl_tch_mode_conf *) msg->l1h;
718
719         LOGP(DL1C, LOGL_INFO, "TCH MODE CONF: mode=%u\n", conf->tch_mode);
720
721         mc.tch_mode = conf->tch_mode;
722         mc.ms = ms;
723         osmo_signal_dispatch(SS_L1CTL, S_L1CTL_TCH_MODE_CONF, &mc);
724
725         return 0;
726 }
727
728 /* Receive incoming data from L1 using L1CTL format */
729 int l1ctl_recv(struct osmocom_ms *ms, struct msgb *msg)
730 {
731         int rc = 0;
732         struct l1ctl_hdr *l1h;
733         struct l1ctl_info_dl *dl;
734
735         if (msgb_l2len(msg) < sizeof(*dl)) {
736                 LOGP(DL1C, LOGL_ERROR, "Short Layer2 message: %u\n",
737                         msgb_l2len(msg));
738                 msgb_free(msg);
739                 return -1;
740         }
741
742         l1h = (struct l1ctl_hdr *) msg->l1h;
743
744         /* move the l1 header pointer to point _BEHIND_ l1ctl_hdr,
745            as the l1ctl header is of no interest to subsequent code */
746         msg->l1h = l1h->data;
747
748         switch (l1h->msg_type) {
749         case L1CTL_FBSB_CONF:
750                 rc = rx_l1_fbsb_conf(ms, msg);
751                 msgb_free(msg);
752                 break;
753         case L1CTL_DATA_IND:
754                 rc = rx_ph_data_ind(ms, msg);
755                 break;
756         case L1CTL_DATA_CONF:
757                 rc = rx_ph_data_conf(ms, msg);
758                 break;
759         case L1CTL_RESET_IND:
760         case L1CTL_RESET_CONF:
761                 rc = rx_l1_reset(ms);
762                 msgb_free(msg);
763                 break;
764         case L1CTL_PM_CONF:
765                 rc = rx_l1_pm_conf(ms, msg);
766                 if (l1h->flags & L1CTL_F_DONE)
767                         osmo_signal_dispatch(SS_L1CTL, S_L1CTL_PM_DONE, ms);
768                 msgb_free(msg);
769                 break;
770         case L1CTL_RACH_CONF:
771                 rc = rx_l1_rach_conf(ms, msg);
772                 break;
773         case L1CTL_CCCH_MODE_CONF:
774                 rc = rx_l1_ccch_mode_conf(ms, msg);
775                 msgb_free(msg);
776                 break;
777         case L1CTL_TCH_MODE_CONF:
778                 rc = rx_l1_tch_mode_conf(ms, msg);
779                 msgb_free(msg);
780                 break;
781         case L1CTL_SIM_CONF:
782                 rc = rx_l1_sim_conf(ms, msg);
783                 break;
784         default:
785                 LOGP(DL1C, LOGL_ERROR, "Unknown MSG: %u\n", l1h->msg_type);
786                 msgb_free(msg);
787                 break;
788         }
789
790         return rc;
791 }