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_resp(struct osmocom_ms *ms, struct msgb *msg)
76 struct l1ctl_info_dl *dl;
77 struct l1ctl_fbsb_resp *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_resp *) 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 char *chan_nr2string(uint8_t chan_nr)
109 uint8_t cbits = chan_nr >> 3;
114 sprintf(str, "TCH/F");
115 else if ((cbits & 0x1e) == 0x02)
116 sprintf(str, "TCH/H(%u)", cbits & 0x01);
117 else if ((cbits & 0x1c) == 0x04)
118 sprintf(str, "SDCCH/4(%u)", cbits & 0x03);
119 else if ((cbits & 0x18) == 0x08)
120 sprintf(str, "SDCCH/8(%u)", cbits & 0x07);
121 else if (cbits == 0x10)
122 sprintf(str, "BCCH");
123 else if (cbits == 0x11)
124 sprintf(str, "RACH");
125 else if (cbits == 0x12)
126 sprintf(str, "PCH/AGCH");
128 sprintf(str, "UNKNOWN");
133 /* Receive L1CTL_DATA_IND (Data Indication from L1) */
134 static int rx_ph_data_ind(struct osmocom_ms *ms, struct msgb *msg)
136 struct l1ctl_info_dl *dl, dl_cpy;
137 struct l1ctl_data_ind *ccch;
138 struct lapdm_entity *le;
139 uint8_t chan_type, chan_ts, chan_ss;
140 uint8_t gsmtap_chan_type;
143 if (msgb_l3len(msg) < sizeof(*ccch)) {
144 LOGP(DL1C, LOGL_ERROR, "MSG too short Data Ind: %u\n",
150 dl = (struct l1ctl_info_dl *) msg->l1h;
151 msg->l2h = dl->payload;
152 ccch = (struct l1ctl_data_ind *) msg->l2h;
154 gsm_fn2gsmtime(&tm, ntohl(dl->frame_nr));
155 rsl_dec_chan_nr(dl->chan_nr, &chan_type, &chan_ss, &chan_ts);
156 DEBUGP(DL1C, "%s (%.4u/%.2u/%.2u) %s\n",
157 chan_nr2string(dl->chan_nr), tm.t1, tm.t2, tm.t3,
158 hexdump(ccch->data, sizeof(ccch->data)));
160 if (dl->num_biterr) {
161 LOGP(DL1C, LOGL_NOTICE, "Dropping frame with %u bit errors\n",
166 /* send CCCH data via GSMTAP */
167 gsmtap_chan_type = chantype_rsl2gsmtap(chan_type, dl->link_id);
168 gsmtap_sendmsg(ntohs(dl->band_arfcn), chan_ts, gsmtap_chan_type, chan_ss,
169 tm.fn, dl->rx_level-110, dl->snr, ccch->data,
172 /* determine LAPDm entity based on SACCH or not */
173 if (dl->link_id & 0x40)
174 le = &ms->l2_entity.lapdm_acch;
176 le = &ms->l2_entity.lapdm_dcch;
177 /* make local stack copy of l1ctl_info_dl, as LAPDm will
178 * overwrite skb hdr */
179 memcpy(&dl_cpy, dl, sizeof(dl_cpy));
181 /* pull the L1 header from the msgb */
182 msgb_pull(msg, msg->l2h - (msg->l1h-sizeof(struct l1ctl_hdr)));
185 /* send it up into LAPDm */
186 l2_ph_data_ind(msg, le, &dl_cpy);
191 /* Transmit L1CTL_DATA_REQ */
192 int tx_ph_data_req(struct osmocom_ms *ms, struct msgb *msg,
193 uint8_t chan_nr, uint8_t link_id)
195 struct l1ctl_hdr *l1h;
196 struct l1ctl_info_ul *l1i_ul;
197 uint8_t chan_type, chan_ts, chan_ss;
198 uint8_t gsmtap_chan_type;
200 DEBUGP(DL1C, "(%s)\n", hexdump(msg->l2h, msgb_l2len(msg)));
202 if (msgb_l2len(msg) > 23) {
203 LOGP(DL1C, LOGL_ERROR, "L1 cannot handle message length "
204 "> 23 (%u)\n", msgb_l2len(msg));
207 } else if (msgb_l2len(msg) < 23)
208 LOGP(DL1C, LOGL_ERROR, "L1 message length < 23 (%u) "
209 "doesn't seem right!\n", msgb_l2len(msg));
211 /* send copy via GSMTAP */
212 rsl_dec_chan_nr(chan_nr, &chan_type, &chan_ss, &chan_ts);
213 gsmtap_chan_type = chantype_rsl2gsmtap(chan_type, link_id);
214 gsmtap_sendmsg(0|0x4000, chan_ts, gsmtap_chan_type, chan_ss,
215 0, 127, 255, msg->l2h, msgb_l2len(msg));
217 /* prepend uplink info header */
218 l1i_ul = (struct l1ctl_info_ul *) msgb_push(msg, sizeof(*l1i_ul));
220 l1i_ul->chan_nr = chan_nr;
221 l1i_ul->link_id = link_id;
223 /* FIXME: where to get this from? */
224 l1i_ul->tx_power = 0;
226 /* prepend l1 header */
227 msg->l1h = msgb_push(msg, sizeof(*l1h));
228 l1h = (struct l1ctl_hdr *) msg->l1h;
229 l1h->msg_type = L1CTL_DATA_REQ;
231 return osmo_send_l1(ms, msg);
234 /* Transmit FBSB_REQ */
235 int l1ctl_tx_fbsb_req(struct osmocom_ms *ms, uint16_t arfcn,
236 uint8_t flags, uint16_t timeout, uint8_t sync_info_idx)
239 struct l1ctl_fbsb_req *req;
241 msg = osmo_l1_alloc(L1CTL_FBSB_REQ);
245 req = (struct l1ctl_fbsb_req *) msgb_put(msg, sizeof(*req));
246 req->band_arfcn = htons(osmo_make_band_arfcn(ms, arfcn));
247 req->timeout = htons(timeout);
248 /* Threshold when to consider FB_MODE1: 4kHz - 1kHz */
249 req->freq_err_thresh1 = htons(4000 - 1000);
250 /* Threshold when to consider SCH: 1kHz - 200Hz */
251 req->freq_err_thresh2 = htons(1000 - 200);
253 req->num_freqerr_avg = 3;
255 req->sync_info_idx = sync_info_idx;
257 return osmo_send_l1(ms, msg);
260 /* Transmit L1CTL_RACH_REQ */
261 int tx_ph_rach_req(struct osmocom_ms *ms)
264 struct l1ctl_info_ul *ul;
265 struct l1ctl_rach_req *req;
266 static uint8_t i = 0;
268 msg = osmo_l1_alloc(L1CTL_RACH_REQ);
272 DEBUGP(DL1C, "RACH Req.\n");
273 ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
274 req = (struct l1ctl_rach_req *) msgb_put(msg, sizeof(*req));
277 return osmo_send_l1(ms, msg);
280 /* Transmit L1CTL_DM_EST_REQ */
281 int tx_ph_dm_est_req(struct osmocom_ms *ms, uint16_t band_arfcn, uint8_t chan_nr)
284 struct l1ctl_info_ul *ul;
285 struct l1ctl_dm_est_req *req;
287 msg = osmo_l1_alloc(L1CTL_DM_EST_REQ);
291 DEBUGP(DL1C, "Tx Dedic.Mode Est Req (arfcn=%u, chan_nr=0x%02x)\n",
292 band_arfcn, chan_nr);
293 ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
294 ul->chan_nr = chan_nr;
296 ul->tx_power = 0; /* FIXME: initial TX power */
297 req = (struct l1ctl_dm_est_req *) msgb_put(msg, sizeof(*req));
298 req->band_arfcn = htons(band_arfcn);
300 return osmo_send_l1(ms, msg);
303 int l1ctl_tx_echo_req(struct osmocom_ms *ms, unsigned int len)
309 msg = osmo_l1_alloc(L1CTL_ECHO_REQ);
313 data = msgb_put(msg, len);
314 for (i = 0; i < len; i++)
317 return osmo_send_l1(ms, msg);
320 /* Transmit L1CTL_PM_REQ */
321 int l1ctl_tx_pm_req_range(struct osmocom_ms *ms, uint16_t arfcn_from,
325 struct l1ctl_pm_req *pm;
327 msg = osmo_l1_alloc(L1CTL_PM_REQ);
331 printf("Tx PM Req (%u-%u)\n", arfcn_from, arfcn_to);
332 pm = (struct l1ctl_pm_req *) msgb_put(msg, sizeof(*pm));
334 pm->range.band_arfcn_from = htons(arfcn_from);
335 pm->range.band_arfcn_to = htons(arfcn_to);
337 return osmo_send_l1(ms, msg);
340 /* Receive L1CTL_RESET_IND */
341 static int rx_l1_reset(struct osmocom_ms *ms)
343 printf("Layer1 Reset.\n");
344 dispatch_signal(SS_L1CTL, S_L1CTL_RESET, ms);
349 /* Receive L1CTL_PM_RESP */
350 static int rx_l1_pm_resp(struct osmocom_ms *ms, struct msgb *msg)
352 struct l1ctl_pm_resp *pmr;
354 for (pmr = (struct l1ctl_pm_resp *) msg->l1h;
355 (uint8_t *) pmr < msg->tail; pmr++) {
356 struct osmobb_meas_res mr;
357 DEBUGP(DL1C, "PM MEAS: ARFCN: %4u RxLev: %3d %3d\n",
358 ntohs(pmr->band_arfcn), pmr->pm[0], pmr->pm[1]);
359 mr.band_arfcn = ntohs(pmr->band_arfcn);
360 mr.rx_lev = (pmr->pm[0] + pmr->pm[1]) / 2;
362 dispatch_signal(SS_L1CTL, S_L1CTL_PM_RES, &mr);
367 /* Receive incoming data from L1 using L1CTL format */
368 int l1ctl_recv(struct osmocom_ms *ms, struct msgb *msg)
371 struct l1ctl_hdr *l1h;
372 struct l1ctl_info_dl *dl;
374 if (msgb_l2len(msg) < sizeof(*dl)) {
375 LOGP(DL1C, LOGL_ERROR, "Short Layer2 message: %u\n",
381 l1h = (struct l1ctl_hdr *) msg->l1h;
383 /* move the l1 header pointer to point _BEHIND_ l1ctl_hdr,
384 as the l1ctl header is of no interest to subsequent code */
385 msg->l1h = l1h->data;
387 switch (l1h->msg_type) {
388 case L1CTL_FBSB_RESP:
389 rc = rx_l1_fbsb_resp(ms, msg);
393 rc = rx_ph_data_ind(ms, msg);
395 case L1CTL_RESET_IND:
396 rc = rx_l1_reset(ms);
400 rc = rx_l1_pm_resp(ms, msg);
402 if (l1h->flags & L1CTL_F_DONE)
403 dispatch_signal(SS_L1CTL, S_L1CTL_PM_DONE, ms);
406 fprintf(stderr, "Unknown MSG: %u\n", l1h->msg_type);