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 <l1a_l23_interface.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/protocol/gsm_04_08.h>
40 #include <osmocore/protocol/gsm_08_58.h>
41 #include <osmocore/rsl.h>
43 #include <osmocom/l1ctl.h>
44 #include <osmocom/osmocom_data.h>
45 #include <osmocom/l1l2_interface.h>
46 #include <osmocom/lapdm.h>
47 #include <osmocom/logging.h>
48 #include <osmocom/gsmtap_util.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;
80 if (msgb_l3len(msg) < sizeof(*dl) + sizeof(*sb)) {
81 LOGP(DL1C, LOGL_ERROR, "FBSB RESP: MSG too short %u\n",
86 dl = (struct l1ctl_info_dl *) msg->l1h;
87 sb = (struct l1ctl_fbsb_conf *) dl->payload;
89 printf("snr=%04x, arfcn=%u result=%u\n", dl->snr, ntohs(dl->band_arfcn),
92 if (sb->result != 0) {
93 LOGP(DL1C, LOGL_ERROR, "FBSB RESP: result=%u\n", sb->result);
94 dispatch_signal(SS_L1CTL, S_L1CTL_FBSB_ERR, ms);
98 gsm_fn2gsmtime(&tm, ntohl(dl->frame_nr));
99 DEBUGP(DL1C, "SCH: SNR: %u TDMA: (%.4u/%.2u/%.2u) bsic: %d\n",
100 dl->snr, tm.t1, tm.t2, tm.t3, sb->bsic);
101 dispatch_signal(SS_L1CTL, S_L1CTL_FBSB_RESP, ms);
106 static int rx_l1_rach_conf(struct osmocom_ms *ms, struct msgb *msg)
108 struct l1ctl_info_dl *dl;
109 struct osmobb_rach_conf rc;
111 if (msgb_l3len(msg) < sizeof(*dl)) {
112 LOGP(DL1C, LOGL_ERROR, "RACH CONF: MSG too short %u\n",
117 dl = (struct l1ctl_info_dl *) msg->l1h;
119 rc.fn = htonl(dl->frame_nr);
121 dispatch_signal(SS_L1CTL, S_L1CTL_RACH_CONF, &rc);
126 char *chan_nr2string(uint8_t chan_nr)
129 uint8_t cbits = chan_nr >> 3;
134 sprintf(str, "TCH/F");
135 else if ((cbits & 0x1e) == 0x02)
136 sprintf(str, "TCH/H(%u)", cbits & 0x01);
137 else if ((cbits & 0x1c) == 0x04)
138 sprintf(str, "SDCCH/4(%u)", cbits & 0x03);
139 else if ((cbits & 0x18) == 0x08)
140 sprintf(str, "SDCCH/8(%u)", cbits & 0x07);
141 else if (cbits == 0x10)
142 sprintf(str, "BCCH");
143 else if (cbits == 0x11)
144 sprintf(str, "RACH");
145 else if (cbits == 0x12)
146 sprintf(str, "PCH/AGCH");
148 sprintf(str, "UNKNOWN");
153 /* Receive L1CTL_DATA_IND (Data Indication from L1) */
154 static int rx_ph_data_ind(struct osmocom_ms *ms, struct msgb *msg)
156 struct l1ctl_info_dl *dl, dl_cpy;
157 struct l1ctl_data_ind *ccch;
158 struct lapdm_entity *le;
159 uint8_t chan_type, chan_ts, chan_ss;
160 uint8_t gsmtap_chan_type;
163 if (msgb_l3len(msg) < sizeof(*ccch)) {
164 LOGP(DL1C, LOGL_ERROR, "MSG too short Data Ind: %u\n",
170 dl = (struct l1ctl_info_dl *) msg->l1h;
171 msg->l2h = dl->payload;
172 ccch = (struct l1ctl_data_ind *) msg->l2h;
174 gsm_fn2gsmtime(&tm, ntohl(dl->frame_nr));
175 rsl_dec_chan_nr(dl->chan_nr, &chan_type, &chan_ss, &chan_ts);
176 DEBUGP(DL1C, "%s (%.4u/%.2u/%.2u) %s\n",
177 chan_nr2string(dl->chan_nr), tm.t1, tm.t2, tm.t3,
178 hexdump(ccch->data, sizeof(ccch->data)));
180 if (dl->num_biterr) {
181 LOGP(DL1C, LOGL_NOTICE, "Dropping frame with %u bit errors\n",
186 /* send CCCH data via GSMTAP */
187 gsmtap_chan_type = chantype_rsl2gsmtap(chan_type, dl->link_id);
188 gsmtap_sendmsg(ntohs(dl->band_arfcn), chan_ts, gsmtap_chan_type, chan_ss,
189 tm.fn, dl->rx_level-110, dl->snr, ccch->data,
192 /* determine LAPDm entity based on SACCH or not */
193 if (dl->link_id & 0x40)
194 le = &ms->l2_entity.lapdm_acch;
196 le = &ms->l2_entity.lapdm_dcch;
197 /* make local stack copy of l1ctl_info_dl, as LAPDm will
198 * overwrite skb hdr */
199 memcpy(&dl_cpy, dl, sizeof(dl_cpy));
201 /* pull the L1 header from the msgb */
202 msgb_pull(msg, msg->l2h - (msg->l1h-sizeof(struct l1ctl_hdr)));
205 /* send it up into LAPDm */
206 l2_ph_data_ind(msg, le, &dl_cpy);
211 /* Receive L1CTL_DATA_CONF (Data Confirm from L1) */
212 static int rx_ph_data_conf(struct osmocom_ms *ms, struct msgb *msg)
214 struct l1ctl_info_dl *dl;
215 struct lapdm_entity *le;
217 dl = (struct l1ctl_info_dl *) msg->l1h;
219 /* determine LAPDm entity based on SACCH or not */
220 if (dl->link_id & 0x40)
221 le = &ms->l2_entity.lapdm_acch;
223 le = &ms->l2_entity.lapdm_dcch;
225 /* send it up into LAPDm */
226 l2_ph_data_conf(msg, le);
231 /* Transmit L1CTL_DATA_REQ */
232 int tx_ph_data_req(struct osmocom_ms *ms, struct msgb *msg,
233 uint8_t chan_nr, uint8_t link_id)
235 struct l1ctl_hdr *l1h;
236 struct l1ctl_info_ul *l1i_ul;
237 uint8_t chan_type, chan_ts, chan_ss;
238 uint8_t gsmtap_chan_type;
240 DEBUGP(DL1C, "(%s)\n", hexdump(msg->l2h, msgb_l2len(msg)));
242 if (msgb_l2len(msg) > 23) {
243 LOGP(DL1C, LOGL_ERROR, "L1 cannot handle message length "
244 "> 23 (%u)\n", msgb_l2len(msg));
247 } else if (msgb_l2len(msg) < 23)
248 LOGP(DL1C, LOGL_ERROR, "L1 message length < 23 (%u) "
249 "doesn't seem right!\n", msgb_l2len(msg));
251 /* send copy via GSMTAP */
252 rsl_dec_chan_nr(chan_nr, &chan_type, &chan_ss, &chan_ts);
253 gsmtap_chan_type = chantype_rsl2gsmtap(chan_type, link_id);
254 gsmtap_sendmsg(0|0x4000, chan_ts, gsmtap_chan_type, chan_ss,
255 0, 127, 255, msg->l2h, msgb_l2len(msg));
257 /* prepend uplink info header */
258 l1i_ul = (struct l1ctl_info_ul *) msgb_push(msg, sizeof(*l1i_ul));
260 l1i_ul->chan_nr = chan_nr;
261 l1i_ul->link_id = link_id;
263 /* FIXME: where to get this from? */
264 l1i_ul->tx_power = 0;
266 /* prepend l1 header */
267 msg->l1h = msgb_push(msg, sizeof(*l1h));
268 l1h = (struct l1ctl_hdr *) msg->l1h;
269 l1h->msg_type = L1CTL_DATA_REQ;
271 return osmo_send_l1(ms, msg);
274 /* Transmit FBSB_REQ */
275 int l1ctl_tx_fbsb_req(struct osmocom_ms *ms, uint16_t arfcn,
276 uint8_t flags, uint16_t timeout, uint8_t sync_info_idx,
280 struct l1ctl_fbsb_req *req;
282 msg = osmo_l1_alloc(L1CTL_FBSB_REQ);
286 req = (struct l1ctl_fbsb_req *) msgb_put(msg, sizeof(*req));
287 req->band_arfcn = htons(osmo_make_band_arfcn(ms, arfcn));
288 req->timeout = htons(timeout);
289 /* Threshold when to consider FB_MODE1: 4kHz - 1kHz */
290 req->freq_err_thresh1 = htons(4000 - 1000);
291 /* Threshold when to consider SCH: 1kHz - 200Hz */
292 req->freq_err_thresh2 = htons(1000 - 200);
294 req->num_freqerr_avg = 3;
296 req->sync_info_idx = sync_info_idx;
297 req->ccch_mode = ccch_mode;
299 return osmo_send_l1(ms, msg);
302 /* Transmit L1CTL_CCCH_MODE_REQ */
303 int l1ctl_tx_ccch_mode_req(struct osmocom_ms *ms, uint8_t ccch_mode)
306 struct l1ctl_ccch_mode_req *req;
308 msg = osmo_l1_alloc(L1CTL_CCCH_MODE_REQ);
312 req = (struct l1ctl_ccch_mode_req *) msgb_put(msg, sizeof(*req));
313 req->ccch_mode = ccch_mode;
315 return osmo_send_l1(ms, msg);
318 /* Transmit L1CTL_RACH_REQ */
319 int tx_ph_rach_req(struct osmocom_ms *ms)
322 struct l1ctl_info_ul *ul;
323 struct l1ctl_rach_req *req;
324 static uint8_t i = 0;
326 msg = osmo_l1_alloc(L1CTL_RACH_REQ);
330 DEBUGP(DL1C, "RACH Req.\n");
331 ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
332 req = (struct l1ctl_rach_req *) msgb_put(msg, sizeof(*req));
335 return osmo_send_l1(ms, msg);
338 /* Transmit L1CTL_DM_EST_REQ */
339 int tx_ph_dm_est_req_h0(struct osmocom_ms *ms, uint16_t band_arfcn,
340 uint8_t chan_nr, uint8_t tsc, uint8_t tx_power)
343 struct l1ctl_info_ul *ul;
344 struct l1ctl_dm_est_req *req;
346 msg = osmo_l1_alloc(L1CTL_DM_EST_REQ);
350 DEBUGP(DL1C, "Tx Dedic.Mode Est Req (arfcn=%u, chan_nr=0x%02x)\n",
351 band_arfcn, chan_nr);
353 ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
354 ul->chan_nr = chan_nr;
356 ul->tx_power = tx_power;
358 req = (struct l1ctl_dm_est_req *) msgb_put(msg, sizeof(*req));
361 req->h0.band_arfcn = htons(band_arfcn);
363 return osmo_send_l1(ms, msg);
366 int tx_ph_dm_est_req_h1(struct osmocom_ms *ms, uint8_t maio, uint8_t hsn,
367 uint16_t *ma, uint8_t ma_len, uint8_t chan_nr, uint8_t tsc,
371 struct l1ctl_info_ul *ul;
372 struct l1ctl_dm_est_req *req;
375 msg = osmo_l1_alloc(L1CTL_DM_EST_REQ);
379 DEBUGP(DL1C, "Tx Dedic.Mode Est Req (maio=%u, hsn=%u, "
380 "chan_nr=0x%02x)\n", maio, hsn, chan_nr);
382 ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
383 ul->chan_nr = chan_nr;
385 ul->tx_power = tx_power;
387 req = (struct l1ctl_dm_est_req *) msgb_put(msg, sizeof(*req));
393 for (i = 0; i < ma_len; i++)
394 req->h1.ma[i] = htons(ma[i]);
396 return osmo_send_l1(ms, msg);
399 /* Transmit L1CTL_DM_REL_REQ */
400 int tx_ph_dm_rel_req(struct osmocom_ms *ms)
403 struct l1ctl_info_ul *ul;
405 msg = osmo_l1_alloc(L1CTL_DM_REL_REQ);
409 DEBUGP(DL1C, "Tx Dedic.Mode Rel Req\n");
411 ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
413 return osmo_send_l1(ms, msg);
416 int l1ctl_tx_echo_req(struct osmocom_ms *ms, unsigned int len)
422 msg = osmo_l1_alloc(L1CTL_ECHO_REQ);
426 data = msgb_put(msg, len);
427 for (i = 0; i < len; i++)
430 return osmo_send_l1(ms, msg);
433 /* Transmit L1CTL_PM_REQ */
434 int l1ctl_tx_pm_req_range(struct osmocom_ms *ms, uint16_t arfcn_from,
438 struct l1ctl_pm_req *pm;
440 msg = osmo_l1_alloc(L1CTL_PM_REQ);
444 printf("Tx PM Req (%u-%u)\n", arfcn_from, arfcn_to);
445 pm = (struct l1ctl_pm_req *) msgb_put(msg, sizeof(*pm));
447 pm->range.band_arfcn_from = htons(arfcn_from);
448 pm->range.band_arfcn_to = htons(arfcn_to);
450 return osmo_send_l1(ms, msg);
453 /* Transmit L1CTL_RESET_REQ */
454 int l1ctl_tx_reset_req(struct osmocom_ms *ms, uint8_t type)
457 struct l1ctl_reset *res;
459 msg = osmo_l1_alloc(L1CTL_RESET_REQ);
463 printf("Tx Reset Req (%u)\n", type);
464 res = (struct l1ctl_reset *) msgb_put(msg, sizeof(*res));
467 return osmo_send_l1(ms, msg);
470 /* Receive L1CTL_RESET_IND */
471 static int rx_l1_reset(struct osmocom_ms *ms)
473 printf("Layer1 Reset.\n");
474 dispatch_signal(SS_L1CTL, S_L1CTL_RESET, ms);
479 /* Receive L1CTL_PM_CONF */
480 static int rx_l1_pm_conf(struct osmocom_ms *ms, struct msgb *msg)
482 struct l1ctl_pm_conf *pmr;
484 for (pmr = (struct l1ctl_pm_conf *) msg->l1h;
485 (uint8_t *) pmr < msg->tail; pmr++) {
486 struct osmobb_meas_res mr;
487 DEBUGP(DL1C, "PM MEAS: ARFCN: %4u RxLev: %3d %3d\n",
488 ntohs(pmr->band_arfcn), pmr->pm[0], pmr->pm[1]);
489 mr.band_arfcn = ntohs(pmr->band_arfcn);
490 mr.rx_lev = (pmr->pm[0] + pmr->pm[1]) / 2;
492 dispatch_signal(SS_L1CTL, S_L1CTL_PM_RES, &mr);
497 /* Receive L1CTL_MODE_CONF */
498 static int rx_l1_ccch_mode_conf(struct osmocom_ms *ms, struct msgb *msg)
500 struct osmobb_ccch_mode_conf mc;
501 struct l1ctl_ccch_mode_conf *conf;
503 if (msgb_l3len(msg) < sizeof(*conf)) {
504 LOGP(DL1C, LOGL_ERROR, "MODE CONF: MSG too short %u\n",
509 conf = (struct l1ctl_ccch_mode_conf *) msg->l1h;
511 printf("mode=%u\n", conf->ccch_mode);
513 mc.ccch_mode = conf->ccch_mode;
515 dispatch_signal(SS_L1CTL, S_L1CTL_CCCH_MODE_CONF, &mc);
520 /* Receive incoming data from L1 using L1CTL format */
521 int l1ctl_recv(struct osmocom_ms *ms, struct msgb *msg)
524 struct l1ctl_hdr *l1h;
525 struct l1ctl_info_dl *dl;
527 if (msgb_l2len(msg) < sizeof(*dl)) {
528 LOGP(DL1C, LOGL_ERROR, "Short Layer2 message: %u\n",
534 l1h = (struct l1ctl_hdr *) msg->l1h;
536 /* move the l1 header pointer to point _BEHIND_ l1ctl_hdr,
537 as the l1ctl header is of no interest to subsequent code */
538 msg->l1h = l1h->data;
540 switch (l1h->msg_type) {
541 case L1CTL_FBSB_CONF:
542 rc = rx_l1_fbsb_conf(ms, msg);
546 rc = rx_ph_data_ind(ms, msg);
548 case L1CTL_DATA_CONF:
549 rc = rx_ph_data_conf(ms, msg);
551 case L1CTL_RESET_IND:
552 case L1CTL_RESET_CONF:
553 rc = rx_l1_reset(ms);
557 rc = rx_l1_pm_conf(ms, msg);
559 if (l1h->flags & L1CTL_F_DONE)
560 dispatch_signal(SS_L1CTL, S_L1CTL_PM_DONE, ms);
562 case L1CTL_RACH_CONF:
563 rc = rx_l1_rach_conf(ms, msg);
566 case L1CTL_CCCH_MODE_CONF:
567 rc = rx_l1_ccch_mode_conf(ms, msg);
571 fprintf(stderr, "Unknown MSG: %u\n", l1h->msg_type);