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