[layer23/mobile] Improvement and fixes of idle mode process
[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
373         return osmo_send_l1(ms, msg);
374 }
375
376 /* Transmit L1CTL_PARAM_REQ */
377 int l1ctl_tx_param_req(struct osmocom_ms *ms, uint8_t ta, uint8_t tx_power)
378 {
379         struct msgb *msg;
380         struct l1ctl_info_ul *ul;
381         struct l1ctl_par_req *req;
382
383         msg = osmo_l1_alloc(L1CTL_PARAM_REQ);
384         if (!msg)
385                 return -1;
386
387         DEBUGP(DL1C, "PARAM Req. ta=%d, tx_power=%d\n", ta, tx_power);
388         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
389         req = (struct l1ctl_par_req *) msgb_put(msg, sizeof(*req));
390         req->tx_power = tx_power;
391         req->ta = ta;
392
393         return osmo_send_l1(ms, msg);
394 }
395
396 /* Transmit L1CTL_CRYPTO_REQ */
397 int l1ctl_tx_crypto_req(struct osmocom_ms *ms, uint8_t algo, uint8_t *key,
398         uint8_t len)
399 {
400         struct msgb *msg;
401         struct l1ctl_info_ul *ul;
402         struct l1ctl_crypto_req *req;
403
404         msg = osmo_l1_alloc(L1CTL_CRYPTO_REQ);
405         if (!msg)
406                 return -1;
407
408         DEBUGP(DL1C, "CRYPTO Req. algo=%d, len=%d\n", algo, len);
409         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
410         req = (struct l1ctl_crypto_req *) msgb_put(msg, sizeof(*req) + len);
411         req->algo = algo;
412         if (len)
413                 memcpy(req->key, key, len);
414
415         return osmo_send_l1(ms, msg);
416 }
417
418 /* Transmit L1CTL_RACH_REQ */
419 int l1ctl_tx_rach_req(struct osmocom_ms *ms, uint8_t ra, uint16_t offset,
420         uint8_t combined)
421 {
422         struct msgb *msg;
423         struct l1ctl_info_ul *ul;
424         struct l1ctl_rach_req *req;
425
426         msg = osmo_l1_alloc(L1CTL_RACH_REQ);
427         if (!msg)
428                 return -1;
429
430         DEBUGP(DL1C, "RACH Req. offset=%d combined=%d\n", offset, combined);
431         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
432         req = (struct l1ctl_rach_req *) msgb_put(msg, sizeof(*req));
433         req->ra = ra;
434         req->offset = htons(offset);
435         req->combined = combined;
436
437         return osmo_send_l1(ms, msg);
438 }
439
440 /* Transmit L1CTL_DM_EST_REQ */
441 int l1ctl_tx_dm_est_req_h0(struct osmocom_ms *ms, uint16_t band_arfcn,
442                            uint8_t chan_nr, uint8_t tsc, uint8_t tch_mode)
443 {
444         struct msgb *msg;
445         struct l1ctl_info_ul *ul;
446         struct l1ctl_dm_est_req *req;
447
448         msg = osmo_l1_alloc(L1CTL_DM_EST_REQ);
449         if (!msg)
450                 return -1;
451
452         LOGP(DL1C, LOGL_INFO, "Tx Dedic.Mode Est Req (arfcn=%u, "
453                 "chan_nr=0x%02x)\n", band_arfcn, chan_nr);
454
455         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
456         ul->chan_nr = chan_nr;
457         ul->link_id = 0;
458
459         req = (struct l1ctl_dm_est_req *) msgb_put(msg, sizeof(*req));
460         req->tsc = tsc;
461         req->h = 0;
462         req->h0.band_arfcn = htons(band_arfcn);
463         req->tch_mode = tch_mode;
464
465         return osmo_send_l1(ms, msg);
466 }
467
468 int l1ctl_tx_dm_est_req_h1(struct osmocom_ms *ms, uint8_t maio, uint8_t hsn,
469                            uint16_t *ma, uint8_t ma_len,
470                            uint8_t chan_nr, uint8_t tsc, uint8_t tch_mode)
471 {
472         struct msgb *msg;
473         struct l1ctl_info_ul *ul;
474         struct l1ctl_dm_est_req *req;
475         int i;
476
477         msg = osmo_l1_alloc(L1CTL_DM_EST_REQ);
478         if (!msg)
479                 return -1;
480
481         LOGP(DL1C, LOGL_INFO, "Tx Dedic.Mode Est Req (maio=%u, hsn=%u, "
482                 "chan_nr=0x%02x)\n", maio, hsn, chan_nr);
483
484         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
485         ul->chan_nr = chan_nr;
486         ul->link_id = 0;
487
488         req = (struct l1ctl_dm_est_req *) msgb_put(msg, sizeof(*req));
489         req->tsc = tsc;
490         req->h = 1;
491         req->h1.maio = maio;
492         req->h1.hsn = hsn;
493         req->h1.n = ma_len;
494         for (i = 0; i < ma_len; i++)
495                 req->h1.ma[i] = htons(ma[i]);
496         req->tch_mode = tch_mode;
497
498         return osmo_send_l1(ms, msg);
499 }
500
501 /* Transmit L1CTL_DM_FREQ_REQ */
502 int l1ctl_tx_dm_freq_req_h0(struct osmocom_ms *ms, uint16_t band_arfcn,
503                             uint8_t tsc, uint16_t fn)
504 {
505         struct msgb *msg;
506         struct l1ctl_info_ul *ul;
507         struct l1ctl_dm_freq_req *req;
508
509         msg = osmo_l1_alloc(L1CTL_DM_FREQ_REQ);
510         if (!msg)
511                 return -1;
512
513         LOGP(DL1C, LOGL_INFO, "Tx Dedic.Mode Freq Req (arfcn=%u, fn=%d)\n",
514                 band_arfcn, fn);
515
516         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
517         ul->chan_nr = 0;
518         ul->link_id = 0;
519
520         req = (struct l1ctl_dm_freq_req *) msgb_put(msg, sizeof(*req));
521         req->fn = htons(fn);
522         req->tsc = tsc;
523         req->h = 0;
524         req->h0.band_arfcn = htons(band_arfcn);
525
526         return osmo_send_l1(ms, msg);
527 }
528
529 int l1ctl_tx_dm_freq_req_h1(struct osmocom_ms *ms, uint8_t maio, uint8_t hsn,
530                             uint16_t *ma, uint8_t ma_len,
531                             uint8_t tsc, uint16_t fn)
532 {
533         struct msgb *msg;
534         struct l1ctl_info_ul *ul;
535         struct l1ctl_dm_freq_req *req;
536         int i;
537
538         msg = osmo_l1_alloc(L1CTL_DM_FREQ_REQ);
539         if (!msg)
540                 return -1;
541
542         LOGP(DL1C, LOGL_INFO, "Tx Dedic.Mode Freq Req (maio=%u, hsn=%u, "
543                 "fn=%d)\n", maio, hsn, fn);
544
545         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
546         ul->chan_nr = 0;
547         ul->link_id = 0;
548
549         req = (struct l1ctl_dm_freq_req *) msgb_put(msg, sizeof(*req));
550         req->fn = htons(fn);
551         req->tsc = tsc;
552         req->h = 1;
553         req->h1.maio = maio;
554         req->h1.hsn = hsn;
555         req->h1.n = ma_len;
556         for (i = 0; i < ma_len; i++)
557                 req->h1.ma[i] = htons(ma[i]);
558
559         return osmo_send_l1(ms, msg);
560 }
561
562 /* Transmit L1CTL_DM_REL_REQ */
563 int l1ctl_tx_dm_rel_req(struct osmocom_ms *ms)
564 {
565         struct msgb *msg;
566         struct l1ctl_info_ul *ul;
567
568         msg = osmo_l1_alloc(L1CTL_DM_REL_REQ);
569         if (!msg)
570                 return -1;
571
572         LOGP(DL1C, LOGL_INFO, "Tx Dedic.Mode Rel Req\n");
573
574         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
575
576         return osmo_send_l1(ms, msg);
577 }
578
579 int l1ctl_tx_echo_req(struct osmocom_ms *ms, unsigned int len)
580 {
581         struct msgb *msg;
582         uint8_t *data;
583         unsigned int i;
584
585         msg = osmo_l1_alloc(L1CTL_ECHO_REQ);
586         if (!msg)
587                 return -1;
588
589         data = msgb_put(msg, len);
590         for (i = 0; i < len; i++)
591                 data[i] = i % 8;
592
593         return osmo_send_l1(ms, msg);
594 }
595
596 int l1ctl_tx_sim_req(struct osmocom_ms *ms, uint8_t *data, uint16_t length)
597 {
598         struct msgb *msg;
599         uint8_t *dat;
600
601         msg = osmo_l1_alloc(L1CTL_SIM_REQ);
602         if (!msg)
603                 return -1;
604
605         dat = msgb_put(msg, length);
606         memcpy(dat, data, length);
607
608         return osmo_send_l1(ms, msg);
609 }
610
611 /* just forward the SIM response to the SIM handler */
612 static int rx_l1_sim_conf(struct osmocom_ms *ms, struct msgb *msg)
613 {
614         uint16_t len = msg->len - sizeof(struct l1ctl_hdr);
615         uint8_t *data = msg->data + sizeof(struct l1ctl_hdr);
616         
617         LOGP(DL1C, LOGL_INFO, "SIM %s\n", osmo_hexdump(data, len));
618         
619         /* pull the L1 header from the msgb */
620         msgb_pull(msg, sizeof(struct l1ctl_hdr));
621         msg->l1h = NULL;
622
623         sim_apdu_resp(ms, msg);
624         
625         return 0;
626 }
627
628 /* Transmit L1CTL_PM_REQ */
629 int l1ctl_tx_pm_req_range(struct osmocom_ms *ms, uint16_t arfcn_from,
630                           uint16_t arfcn_to)
631 {
632         struct msgb *msg;
633         struct l1ctl_pm_req *pm;
634
635         msg = osmo_l1_alloc(L1CTL_PM_REQ);
636         if (!msg)
637                 return -1;
638
639         LOGP(DL1C, LOGL_INFO, "Tx PM Req (%u-%u)\n", arfcn_from, arfcn_to);
640         pm = (struct l1ctl_pm_req *) msgb_put(msg, sizeof(*pm));
641         pm->type = 1;
642         pm->range.band_arfcn_from = htons(arfcn_from);
643         pm->range.band_arfcn_to = htons(arfcn_to);
644
645         return osmo_send_l1(ms, msg);
646 }
647
648 /* Transmit L1CTL_RESET_REQ */
649 int l1ctl_tx_reset_req(struct osmocom_ms *ms, uint8_t type)
650 {
651         struct msgb *msg;
652         struct l1ctl_reset *res;
653
654         msg = osmo_l1_alloc(L1CTL_RESET_REQ);
655         if (!msg)
656                 return -1;
657
658         LOGP(DL1C, LOGL_INFO, "Tx Reset Req (%u)\n", type);
659         res = (struct l1ctl_reset *) msgb_put(msg, sizeof(*res));
660         res->type = type;
661
662         return osmo_send_l1(ms, msg);
663 }
664
665 /* Receive L1CTL_RESET_IND */
666 static int rx_l1_reset(struct osmocom_ms *ms)
667 {
668         LOGP(DL1C, LOGL_INFO, "Layer1 Reset indication\n");
669         osmo_signal_dispatch(SS_L1CTL, S_L1CTL_RESET, ms);
670
671         return 0;
672 }
673
674 /* Receive L1CTL_PM_CONF */
675 static int rx_l1_pm_conf(struct osmocom_ms *ms, struct msgb *msg)
676 {
677         struct l1ctl_pm_conf *pmr;
678
679         for (pmr = (struct l1ctl_pm_conf *) msg->l1h;
680              (uint8_t *) pmr < msg->tail; pmr++) {
681                 struct osmobb_meas_res mr;
682                 DEBUGP(DL1C, "PM MEAS: ARFCN: %4u RxLev: %3d %3d\n",
683                         ntohs(pmr->band_arfcn), pmr->pm[0], pmr->pm[1]);
684                 mr.band_arfcn = ntohs(pmr->band_arfcn);
685                 mr.rx_lev = pmr->pm[0];
686                 mr.ms = ms;
687                 osmo_signal_dispatch(SS_L1CTL, S_L1CTL_PM_RES, &mr);
688         }
689         return 0;
690 }
691
692 /* Receive L1CTL_CCCH_MODE_CONF */
693 static int rx_l1_ccch_mode_conf(struct osmocom_ms *ms, struct msgb *msg)
694 {
695         struct osmobb_ccch_mode_conf mc;
696         struct l1ctl_ccch_mode_conf *conf;
697
698         if (msgb_l3len(msg) < sizeof(*conf)) {
699                 LOGP(DL1C, LOGL_ERROR, "CCCH MODE CONF: MSG too short %u\n",
700                         msgb_l3len(msg));
701                 return -1;
702         }
703
704         conf = (struct l1ctl_ccch_mode_conf *) msg->l1h;
705
706         LOGP(DL1C, LOGL_INFO, "CCCH MODE CONF: mode=%u\n", conf->ccch_mode);
707
708         mc.ccch_mode = conf->ccch_mode;
709         mc.ms = ms;
710         osmo_signal_dispatch(SS_L1CTL, S_L1CTL_CCCH_MODE_CONF, &mc);
711
712         return 0;
713 }
714
715 /* Receive L1CTL_TCH_MODE_CONF */
716 static int rx_l1_tch_mode_conf(struct osmocom_ms *ms, struct msgb *msg)
717 {
718         struct osmobb_tch_mode_conf mc;
719         struct l1ctl_tch_mode_conf *conf;
720
721         if (msgb_l3len(msg) < sizeof(*conf)) {
722                 LOGP(DL1C, LOGL_ERROR, "TCH MODE CONF: MSG too short %u\n",
723                         msgb_l3len(msg));
724                 return -1;
725         }
726
727         conf = (struct l1ctl_tch_mode_conf *) msg->l1h;
728
729         LOGP(DL1C, LOGL_INFO, "TCH MODE CONF: mode=%u\n", conf->tch_mode);
730
731         mc.tch_mode = conf->tch_mode;
732         mc.ms = ms;
733         osmo_signal_dispatch(SS_L1CTL, S_L1CTL_TCH_MODE_CONF, &mc);
734
735         return 0;
736 }
737
738 /* Transmit L1CTL_NEIGH_PM_REQ */
739 int l1ctl_tx_neigh_pm_req(struct osmocom_ms *ms, int num, uint16_t *arfcn)
740 {
741         struct msgb *msg;
742         struct l1ctl_neigh_pm_req *pm_req;
743         int i;
744
745         msg = osmo_l1_alloc(L1CTL_NEIGH_PM_REQ);
746         if (!msg)
747                 return -1;
748
749         LOGP(DL1C, LOGL_INFO, "Tx NEIGH PM Req (num %u)\n", num);
750         pm_req = (struct l1ctl_neigh_pm_req *) msgb_put(msg, sizeof(*pm_req));
751         pm_req->n = num;
752         for (i = 0; i < num; i++)
753                 pm_req->band_arfcn[i] = htons(*arfcn++);
754
755         return osmo_send_l1(ms, msg);
756 }
757
758 /* Receive L1CTL_NEIGH_PM_IND */
759 static int rx_l1_neigh_pm_ind(struct osmocom_ms *ms, struct msgb *msg)
760 {
761         struct l1ctl_neigh_pm_ind *pm_ind;
762
763         for (pm_ind = (struct l1ctl_neigh_pm_ind *) msg->l1h;
764              (uint8_t *) pm_ind < msg->tail; pm_ind++) {
765                 struct osmobb_neigh_pm_ind mi;
766                 DEBUGP(DL1C, "NEIGH_PM IND: ARFCN: %4u RxLev: %3d %3d\n",
767                         ntohs(pm_ind->band_arfcn), pm_ind->pm[0],
768                         pm_ind->pm[1]);
769                 mi.band_arfcn = ntohs(pm_ind->band_arfcn);
770                 mi.rx_lev = pm_ind->pm[0];
771                 mi.ms = ms;
772                 osmo_signal_dispatch(SS_L1CTL, S_L1CTL_NEIGH_PM_IND, &mi);
773         }
774         return 0;
775 }
776
777 /* Receive incoming data from L1 using L1CTL format */
778 int l1ctl_recv(struct osmocom_ms *ms, struct msgb *msg)
779 {
780         int rc = 0;
781         struct l1ctl_hdr *l1h;
782         struct l1ctl_info_dl *dl;
783
784         if (msgb_l2len(msg) < sizeof(*dl)) {
785                 LOGP(DL1C, LOGL_ERROR, "Short Layer2 message: %u\n",
786                         msgb_l2len(msg));
787                 msgb_free(msg);
788                 return -1;
789         }
790
791         l1h = (struct l1ctl_hdr *) msg->l1h;
792
793         /* move the l1 header pointer to point _BEHIND_ l1ctl_hdr,
794            as the l1ctl header is of no interest to subsequent code */
795         msg->l1h = l1h->data;
796
797         switch (l1h->msg_type) {
798         case L1CTL_FBSB_CONF:
799                 rc = rx_l1_fbsb_conf(ms, msg);
800                 msgb_free(msg);
801                 break;
802         case L1CTL_DATA_IND:
803                 rc = rx_ph_data_ind(ms, msg);
804                 break;
805         case L1CTL_DATA_CONF:
806                 rc = rx_ph_data_conf(ms, msg);
807                 break;
808         case L1CTL_RESET_IND:
809         case L1CTL_RESET_CONF:
810                 rc = rx_l1_reset(ms);
811                 msgb_free(msg);
812                 break;
813         case L1CTL_PM_CONF:
814                 rc = rx_l1_pm_conf(ms, msg);
815                 if (l1h->flags & L1CTL_F_DONE)
816                         osmo_signal_dispatch(SS_L1CTL, S_L1CTL_PM_DONE, ms);
817                 msgb_free(msg);
818                 break;
819         case L1CTL_RACH_CONF:
820                 rc = rx_l1_rach_conf(ms, msg);
821                 break;
822         case L1CTL_CCCH_MODE_CONF:
823                 rc = rx_l1_ccch_mode_conf(ms, msg);
824                 msgb_free(msg);
825                 break;
826         case L1CTL_TCH_MODE_CONF:
827                 rc = rx_l1_tch_mode_conf(ms, msg);
828                 msgb_free(msg);
829                 break;
830         case L1CTL_SIM_CONF:
831                 rc = rx_l1_sim_conf(ms, msg);
832                 break;
833         case L1CTL_NEIGH_PM_IND:
834                 rc = rx_l1_neigh_pm_ind(ms, msg);
835                 msgb_free(msg);
836                 break;
837         default:
838                 LOGP(DL1C, LOGL_ERROR, "Unknown MSG: %u\n", l1h->msg_type);
839                 msgb_free(msg);
840                 break;
841         }
842
843         return rc;
844 }