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