1 /* Layer1 control code, talking L1CTL protocol with L1 on the phone */
3 /* (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * (C) 2010 by Harald Welte <laforge@gnumonks.org>
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.
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.
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.
29 #include <arpa/inet.h>
31 #include <l1ctl_proto.h>
33 #include <osmocore/signal.h>
34 #include <osmocore/logging.h>
35 #include <osmocore/timer.h>
36 #include <osmocore/msgb.h>
37 #include <osmocore/tlv.h>
38 #include <osmocore/gsm_utils.h>
39 #include <osmocore/gsmtap_util.h>
40 #include <osmocore/protocol/gsm_04_08.h>
41 #include <osmocore/protocol/gsm_08_58.h>
42 #include <osmocore/rsl.h>
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>
50 static struct msgb *osmo_l1_alloc(uint8_t msg_type)
52 struct l1ctl_hdr *l1h;
53 struct msgb *msg = msgb_alloc_headroom(256, 4, "osmo_l1");
56 LOGP(DL1C, LOGL_ERROR, "Failed to allocate memory.\n");
60 msg->l1h = msgb_put(msg, sizeof(*l1h));
61 l1h = (struct l1ctl_hdr *) msg->l1h;
62 l1h->msg_type = msg_type;
68 static int osmo_make_band_arfcn(struct osmocom_ms *ms, uint16_t arfcn)
70 /* TODO: Include the band */
74 static int rx_l1_fbsb_conf(struct osmocom_ms *ms, struct msgb *msg)
76 struct l1ctl_info_dl *dl;
77 struct l1ctl_fbsb_conf *sb;
79 struct osmobb_fbsb_res fr;
81 if (msgb_l3len(msg) < sizeof(*dl) + sizeof(*sb)) {
82 LOGP(DL1C, LOGL_ERROR, "FBSB RESP: MSG too short %u\n",
87 dl = (struct l1ctl_info_dl *) msg->l1h;
88 sb = (struct l1ctl_fbsb_conf *) dl->payload;
90 printf("snr=%04x, arfcn=%u result=%u\n", dl->snr, ntohs(dl->band_arfcn),
93 if (sb->result != 0) {
94 LOGP(DL1C, LOGL_ERROR, "FBSB RESP: result=%u\n", sb->result);
95 dispatch_signal(SS_L1CTL, S_L1CTL_FBSB_ERR, ms);
99 gsm_fn2gsmtime(&tm, ntohl(dl->frame_nr));
100 DEBUGP(DL1C, "SCH: SNR: %u TDMA: (%.4u/%.2u/%.2u) bsic: %d\n",
101 dl->snr, tm.t1, tm.t2, tm.t3, sb->bsic);
105 dispatch_signal(SS_L1CTL, S_L1CTL_FBSB_RESP, &fr);
110 static int rx_l1_rach_conf(struct osmocom_ms *ms, struct msgb *msg)
112 struct l1ctl_info_dl *dl;
114 if (msgb_l2len(msg) < sizeof(*dl)) {
115 LOGP(DL1C, LOGL_ERROR, "RACH CONF: MSG too short %u\n",
121 dl = (struct l1ctl_info_dl *) msg->l1h;
123 l2_ph_chan_conf(msg, ms, dl);
128 /* Receive L1CTL_DATA_IND (Data Indication from L1) */
129 static int rx_ph_data_ind(struct osmocom_ms *ms, struct msgb *msg)
131 struct l1ctl_info_dl *dl, dl_cpy;
132 struct l1ctl_data_ind *ccch;
133 struct lapdm_entity *le;
134 struct rx_meas_stat *meas = &ms->meas;
135 uint8_t chan_type, chan_ts, chan_ss;
136 uint8_t gsmtap_chan_type;
139 if (msgb_l3len(msg) < sizeof(*ccch)) {
140 LOGP(DL1C, LOGL_ERROR, "MSG too short Data Ind: %u\n",
146 dl = (struct l1ctl_info_dl *) msg->l1h;
147 msg->l2h = dl->payload;
148 ccch = (struct l1ctl_data_ind *) msg->l2h;
150 gsm_fn2gsmtime(&tm, ntohl(dl->frame_nr));
151 rsl_dec_chan_nr(dl->chan_nr, &chan_type, &chan_ss, &chan_ts);
152 DEBUGP(DL1C, "%s (%.4u/%.2u/%.2u) %d dBm: %s\n",
153 rsl_chan_nr_str(dl->chan_nr), tm.t1, tm.t2, tm.t3,
154 (int)dl->rx_level-110,
155 hexdump(ccch->data, sizeof(ccch->data)));
157 meas->last_fn = ntohl(dl->frame_nr);
159 meas->snr += dl->snr;
160 meas->berr += dl->num_biterr;
161 meas->rxlev += dl->rx_level;
163 if (dl->num_biterr) {
164 printf("Dropping frame with %u bit errors\n", dl->num_biterr);
165 LOGP(DL1C, LOGL_NOTICE, "Dropping frame with %u bit errors\n",
170 /* send CCCH data via GSMTAP */
171 gsmtap_chan_type = chantype_rsl2gsmtap(chan_type, dl->link_id);
172 gsmtap_sendmsg(ntohs(dl->band_arfcn), chan_ts, gsmtap_chan_type, chan_ss,
173 tm.fn, dl->rx_level-110, dl->snr, ccch->data,
176 /* determine LAPDm entity based on SACCH or not */
177 if (dl->link_id & 0x40)
178 le = &ms->l2_entity.lapdm_acch;
180 le = &ms->l2_entity.lapdm_dcch;
181 /* make local stack copy of l1ctl_info_dl, as LAPDm will
182 * overwrite skb hdr */
183 memcpy(&dl_cpy, dl, sizeof(dl_cpy));
185 /* pull the L1 header from the msgb */
186 msgb_pull(msg, msg->l2h - (msg->l1h-sizeof(struct l1ctl_hdr)));
189 /* send it up into LAPDm */
190 l2_ph_data_ind(msg, le, &dl_cpy);
195 /* Receive L1CTL_DATA_CONF (Data Confirm from L1) */
196 static int rx_ph_data_conf(struct osmocom_ms *ms, struct msgb *msg)
198 struct l1ctl_info_dl *dl;
199 struct lapdm_entity *le;
201 dl = (struct l1ctl_info_dl *) msg->l1h;
203 /* determine LAPDm entity based on SACCH or not */
204 if (dl->link_id & 0x40)
205 le = &ms->l2_entity.lapdm_acch;
207 le = &ms->l2_entity.lapdm_dcch;
209 /* send it up into LAPDm */
210 l2_ph_data_conf(msg, le);
215 /* Transmit L1CTL_DATA_REQ */
216 int l1ctl_tx_data_req(struct osmocom_ms *ms, struct msgb *msg,
217 uint8_t chan_nr, uint8_t link_id)
219 struct l1ctl_hdr *l1h;
220 struct l1ctl_info_ul *l1i_ul;
221 uint8_t chan_type, chan_ts, chan_ss;
222 uint8_t gsmtap_chan_type;
224 DEBUGP(DL1C, "(%s)\n", hexdump(msg->l2h, msgb_l2len(msg)));
226 if (msgb_l2len(msg) > 23) {
227 LOGP(DL1C, LOGL_ERROR, "L1 cannot handle message length "
228 "> 23 (%u)\n", msgb_l2len(msg));
231 } else if (msgb_l2len(msg) < 23)
232 LOGP(DL1C, LOGL_ERROR, "L1 message length < 23 (%u) "
233 "doesn't seem right!\n", msgb_l2len(msg));
235 /* send copy via GSMTAP */
236 rsl_dec_chan_nr(chan_nr, &chan_type, &chan_ss, &chan_ts);
237 gsmtap_chan_type = chantype_rsl2gsmtap(chan_type, link_id);
238 gsmtap_sendmsg(0|0x4000, chan_ts, gsmtap_chan_type, chan_ss,
239 0, 127, 255, msg->l2h, msgb_l2len(msg));
241 /* prepend uplink info header */
242 l1i_ul = (struct l1ctl_info_ul *) msgb_push(msg, sizeof(*l1i_ul));
244 l1i_ul->chan_nr = chan_nr;
245 l1i_ul->link_id = link_id;
247 /* prepend l1 header */
248 msg->l1h = msgb_push(msg, sizeof(*l1h));
249 l1h = (struct l1ctl_hdr *) msg->l1h;
250 l1h->msg_type = L1CTL_DATA_REQ;
252 return osmo_send_l1(ms, msg);
255 /* Transmit FBSB_REQ */
256 int l1ctl_tx_fbsb_req(struct osmocom_ms *ms, uint16_t arfcn,
257 uint8_t flags, uint16_t timeout, uint8_t sync_info_idx,
261 struct l1ctl_fbsb_req *req;
263 printf("Sync Req\n");
265 msg = osmo_l1_alloc(L1CTL_FBSB_REQ);
269 memset(&ms->meas, 0, sizeof(ms->meas));
271 req = (struct l1ctl_fbsb_req *) msgb_put(msg, sizeof(*req));
272 req->band_arfcn = htons(osmo_make_band_arfcn(ms, arfcn));
273 req->timeout = htons(timeout);
274 /* Threshold when to consider FB_MODE1: 4kHz - 1kHz */
275 req->freq_err_thresh1 = htons(4000 - 1000);
276 /* Threshold when to consider SCH: 1kHz - 200Hz */
277 req->freq_err_thresh2 = htons(1000 - 200);
279 req->num_freqerr_avg = 3;
281 req->sync_info_idx = sync_info_idx;
282 req->ccch_mode = ccch_mode;
284 return osmo_send_l1(ms, msg);
287 /* Transmit L1CTL_CCCH_MODE_REQ */
288 int l1ctl_tx_ccch_mode_req(struct osmocom_ms *ms, uint8_t ccch_mode)
291 struct l1ctl_ccch_mode_req *req;
293 printf("CCCH Mode Req\n");
295 msg = osmo_l1_alloc(L1CTL_CCCH_MODE_REQ);
299 req = (struct l1ctl_ccch_mode_req *) msgb_put(msg, sizeof(*req));
300 req->ccch_mode = ccch_mode;
302 return osmo_send_l1(ms, msg);
305 /* Transmit L1CTL_TCH_MODE_REQ */
306 int l1ctl_tx_tch_mode_req(struct osmocom_ms *ms, uint8_t tch_mode)
309 struct l1ctl_tch_mode_req *req;
311 printf("TCH Mode Req\n");
313 msg = osmo_l1_alloc(L1CTL_TCH_MODE_REQ);
317 req = (struct l1ctl_tch_mode_req *) msgb_put(msg, sizeof(*req));
318 req->tch_mode = tch_mode;
320 return osmo_send_l1(ms, msg);
323 /* Transmit L1CTL_PARAM_REQ */
324 int l1ctl_tx_param_req(struct osmocom_ms *ms, uint8_t ta, uint8_t tx_power)
327 struct l1ctl_info_ul *ul;
328 struct l1ctl_par_req *req;
330 msg = osmo_l1_alloc(L1CTL_PARAM_REQ);
334 DEBUGP(DL1C, "PARAM Req. ta=%d, tx_power=%d\n", ta, tx_power);
335 ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
336 req = (struct l1ctl_par_req *) msgb_put(msg, sizeof(*req));
337 req->tx_power = tx_power;
340 return osmo_send_l1(ms, msg);
343 /* Transmit L1CTL_CRYPTO_REQ */
344 int l1ctl_tx_crypto_req(struct osmocom_ms *ms, uint8_t algo, uint8_t *key,
348 struct l1ctl_info_ul *ul;
349 struct l1ctl_crypto_req *req;
351 msg = osmo_l1_alloc(L1CTL_CRYPTO_REQ);
355 DEBUGP(DL1C, "CRYPTO Req. algo=%d, len=%d\n", algo, len);
356 ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
357 req = (struct l1ctl_crypto_req *) msgb_put(msg, sizeof(*req) + len);
360 memcpy(req->key, key, len);
362 return osmo_send_l1(ms, msg);
365 /* Transmit L1CTL_RACH_REQ */
366 int l1ctl_tx_rach_req(struct osmocom_ms *ms, uint8_t ra, uint8_t fn51,
370 struct l1ctl_info_ul *ul;
371 struct l1ctl_rach_req *req;
373 msg = osmo_l1_alloc(L1CTL_RACH_REQ);
377 DEBUGP(DL1C, "RACH Req. fn51=%d, mf_off=%d\n", fn51, mf_off);
378 ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
379 req = (struct l1ctl_rach_req *) msgb_put(msg, sizeof(*req));
382 req->mf_off = mf_off;
384 return osmo_send_l1(ms, msg);
387 /* Transmit L1CTL_DM_EST_REQ */
388 int l1ctl_tx_dm_est_req_h0(struct osmocom_ms *ms, uint16_t band_arfcn,
389 uint8_t chan_nr, uint8_t tsc)
392 struct l1ctl_info_ul *ul;
393 struct l1ctl_dm_est_req *req;
395 msg = osmo_l1_alloc(L1CTL_DM_EST_REQ);
399 printf("Tx Dedic.Mode Est Req (arfcn=%u, chan_nr=0x%02x)\n",
400 band_arfcn, chan_nr);
402 memset(&ms->meas, 0, sizeof(ms->meas));
404 ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
405 ul->chan_nr = chan_nr;
408 req = (struct l1ctl_dm_est_req *) msgb_put(msg, sizeof(*req));
411 req->h0.band_arfcn = htons(band_arfcn);
413 return osmo_send_l1(ms, msg);
416 int l1ctl_tx_dm_est_req_h1(struct osmocom_ms *ms, uint8_t maio, uint8_t hsn,
417 uint16_t *ma, uint8_t ma_len,
418 uint8_t chan_nr, uint8_t tsc)
421 struct l1ctl_info_ul *ul;
422 struct l1ctl_dm_est_req *req;
425 msg = osmo_l1_alloc(L1CTL_DM_EST_REQ);
429 printf("Tx Dedic.Mode Est Req (maio=%u, hsn=%u, "
430 "chan_nr=0x%02x)\n", maio, hsn, chan_nr);
432 memset(&ms->meas, 0, sizeof(ms->meas));
434 ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
435 ul->chan_nr = chan_nr;
438 req = (struct l1ctl_dm_est_req *) msgb_put(msg, sizeof(*req));
444 for (i = 0; i < ma_len; i++)
445 req->h1.ma[i] = htons(ma[i]);
447 return osmo_send_l1(ms, msg);
450 /* Transmit L1CTL_DM_FREQ_REQ */
451 int l1ctl_tx_dm_freq_req_h0(struct osmocom_ms *ms, uint16_t band_arfcn,
452 uint8_t tsc, uint16_t fn)
455 struct l1ctl_info_ul *ul;
456 struct l1ctl_dm_freq_req *req;
458 msg = osmo_l1_alloc(L1CTL_DM_FREQ_REQ);
462 printf("Tx Dedic.Mode Freq Req (arfcn=%u, fn=%d)\n",
465 ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
469 req = (struct l1ctl_dm_freq_req *) msgb_put(msg, sizeof(*req));
473 req->h0.band_arfcn = htons(band_arfcn);
475 return osmo_send_l1(ms, msg);
478 int l1ctl_tx_dm_freq_req_h1(struct osmocom_ms *ms, uint8_t maio, uint8_t hsn,
479 uint16_t *ma, uint8_t ma_len,
480 uint8_t tsc, uint16_t fn)
483 struct l1ctl_info_ul *ul;
484 struct l1ctl_dm_freq_req *req;
487 msg = osmo_l1_alloc(L1CTL_DM_FREQ_REQ);
491 printf("Tx Dedic.Mode Freq Req (maio=%u, hsn=%u, "
492 "fn=%d)\n", maio, hsn, fn);
494 ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
498 req = (struct l1ctl_dm_freq_req *) msgb_put(msg, sizeof(*req));
505 for (i = 0; i < ma_len; i++)
506 req->h1.ma[i] = htons(ma[i]);
508 return osmo_send_l1(ms, msg);
511 /* Transmit L1CTL_DM_REL_REQ */
512 int l1ctl_tx_dm_rel_req(struct osmocom_ms *ms)
515 struct l1ctl_info_ul *ul;
517 msg = osmo_l1_alloc(L1CTL_DM_REL_REQ);
521 printf("Tx Dedic.Mode Rel Req\n");
523 memset(&ms->meas, 0, sizeof(ms->meas));
525 ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
527 return osmo_send_l1(ms, msg);
530 int l1ctl_tx_echo_req(struct osmocom_ms *ms, unsigned int len)
536 msg = osmo_l1_alloc(L1CTL_ECHO_REQ);
540 data = msgb_put(msg, len);
541 for (i = 0; i < len; i++)
544 return osmo_send_l1(ms, msg);
547 int l1ctl_tx_sim_req(struct osmocom_ms *ms, uint8_t *data, uint16_t length)
552 msg = osmo_l1_alloc(L1CTL_SIM_REQ);
556 dat = msgb_put(msg, length);
557 memcpy(dat, data, length);
559 return osmo_send_l1(ms, msg);
562 /* just forward the SIM response to the SIM handler */
563 static int rx_l1_sim_conf(struct osmocom_ms *ms, struct msgb *msg)
565 uint16_t len = msg->len - sizeof(struct l1ctl_hdr);
566 uint8_t *data = msg->data + sizeof(struct l1ctl_hdr);
568 printf("SIM %s\n", hexdump(data, len));
570 /* pull the L1 header from the msgb */
571 msgb_pull(msg, sizeof(struct l1ctl_hdr));
574 sim_apdu_resp(ms, msg);
579 /* Transmit L1CTL_PM_REQ */
580 int l1ctl_tx_pm_req_range(struct osmocom_ms *ms, uint16_t arfcn_from,
584 struct l1ctl_pm_req *pm;
586 msg = osmo_l1_alloc(L1CTL_PM_REQ);
590 printf("Tx PM Req (%u-%u)\n", arfcn_from, arfcn_to);
591 pm = (struct l1ctl_pm_req *) msgb_put(msg, sizeof(*pm));
593 pm->range.band_arfcn_from = htons(arfcn_from);
594 pm->range.band_arfcn_to = htons(arfcn_to);
596 return osmo_send_l1(ms, msg);
599 /* Transmit L1CTL_RESET_REQ */
600 int l1ctl_tx_reset_req(struct osmocom_ms *ms, uint8_t type)
603 struct l1ctl_reset *res;
605 msg = osmo_l1_alloc(L1CTL_RESET_REQ);
609 printf("Tx Reset Req (%u)\n", type);
610 res = (struct l1ctl_reset *) msgb_put(msg, sizeof(*res));
613 return osmo_send_l1(ms, msg);
616 /* Receive L1CTL_RESET_IND */
617 static int rx_l1_reset(struct osmocom_ms *ms)
619 printf("Layer1 Reset indication\n");
620 dispatch_signal(SS_L1CTL, S_L1CTL_RESET, ms);
625 /* Receive L1CTL_PM_CONF */
626 static int rx_l1_pm_conf(struct osmocom_ms *ms, struct msgb *msg)
628 struct l1ctl_pm_conf *pmr;
630 for (pmr = (struct l1ctl_pm_conf *) msg->l1h;
631 (uint8_t *) pmr < msg->tail; pmr++) {
632 struct osmobb_meas_res mr;
633 DEBUGP(DL1C, "PM MEAS: ARFCN: %4u RxLev: %3d %3d\n",
634 ntohs(pmr->band_arfcn), pmr->pm[0], pmr->pm[1]);
635 mr.band_arfcn = ntohs(pmr->band_arfcn);
636 mr.rx_lev = pmr->pm[0];
638 dispatch_signal(SS_L1CTL, S_L1CTL_PM_RES, &mr);
643 /* Receive L1CTL_CCCH_MODE_CONF */
644 static int rx_l1_ccch_mode_conf(struct osmocom_ms *ms, struct msgb *msg)
646 struct osmobb_ccch_mode_conf mc;
647 struct l1ctl_ccch_mode_conf *conf;
649 if (msgb_l3len(msg) < sizeof(*conf)) {
650 LOGP(DL1C, LOGL_ERROR, "MODE CONF: MSG too short %u\n",
655 conf = (struct l1ctl_ccch_mode_conf *) msg->l1h;
657 printf("mode=%u\n", conf->ccch_mode);
659 mc.ccch_mode = conf->ccch_mode;
661 dispatch_signal(SS_L1CTL, S_L1CTL_CCCH_MODE_CONF, &mc);
666 /* Receive L1CTL_TCH_MODE_CONF */
667 static int rx_l1_tch_mode_conf(struct osmocom_ms *ms, struct msgb *msg)
669 struct osmobb_tch_mode_conf mc;
670 struct l1ctl_tch_mode_conf *conf;
672 if (msgb_l3len(msg) < sizeof(*conf)) {
673 LOGP(DL1C, LOGL_ERROR, "MODE CONF: MSG too short %u\n",
678 conf = (struct l1ctl_tch_mode_conf *) msg->l1h;
680 printf("mode=%u\n", conf->tch_mode);
682 mc.tch_mode = conf->tch_mode;
684 dispatch_signal(SS_L1CTL, S_L1CTL_TCH_MODE_CONF, &mc);
689 /* Receive incoming data from L1 using L1CTL format */
690 int l1ctl_recv(struct osmocom_ms *ms, struct msgb *msg)
693 struct l1ctl_hdr *l1h;
694 struct l1ctl_info_dl *dl;
696 if (msgb_l2len(msg) < sizeof(*dl)) {
697 LOGP(DL1C, LOGL_ERROR, "Short Layer2 message: %u\n",
703 l1h = (struct l1ctl_hdr *) msg->l1h;
705 /* move the l1 header pointer to point _BEHIND_ l1ctl_hdr,
706 as the l1ctl header is of no interest to subsequent code */
707 msg->l1h = l1h->data;
709 switch (l1h->msg_type) {
710 case L1CTL_FBSB_CONF:
711 rc = rx_l1_fbsb_conf(ms, msg);
715 rc = rx_ph_data_ind(ms, msg);
717 case L1CTL_DATA_CONF:
718 rc = rx_ph_data_conf(ms, msg);
720 case L1CTL_RESET_IND:
721 case L1CTL_RESET_CONF:
722 rc = rx_l1_reset(ms);
726 rc = rx_l1_pm_conf(ms, msg);
728 if (l1h->flags & L1CTL_F_DONE)
729 dispatch_signal(SS_L1CTL, S_L1CTL_PM_DONE, ms);
731 case L1CTL_RACH_CONF:
732 rc = rx_l1_rach_conf(ms, msg);
734 case L1CTL_CCCH_MODE_CONF:
735 rc = rx_l1_ccch_mode_conf(ms, msg);
738 case L1CTL_TCH_MODE_CONF:
739 rc = rx_l1_tch_mode_conf(ms, msg);
743 rc = rx_l1_sim_conf(ms, msg);
746 fprintf(stderr, "Unknown MSG: %u\n", l1h->msg_type);