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