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/lapdm.h>
46 #include <osmocom/logging.h>
47 #include <osmocom/gsmtap_util.h>
49 static struct msgb *osmo_l1_alloc(uint8_t msg_type)
51 struct l1ctl_hdr *l1h;
52 struct msgb *msg = msgb_alloc_headroom(256, 4, "osmo_l1");
55 LOGP(DL1C, LOGL_ERROR, "Failed to allocate memory.\n");
59 msg->l1h = msgb_put(msg, sizeof(*l1h));
60 l1h = (struct l1ctl_hdr *) msg->l1h;
61 l1h->msg_type = msg_type;
67 static int osmo_make_band_arfcn(struct osmocom_ms *ms, uint16_t arfcn)
69 /* TODO: Include the band */
73 static int rx_l1_ccch_resp(struct osmocom_ms *ms, struct msgb *msg)
75 struct l1ctl_info_dl *dl;
76 struct l1ctl_sync_new_ccch_resp *sb;
79 if (msgb_l3len(msg) < sizeof(*sb)) {
80 LOGP(DL1C, LOGL_ERROR, "MSG too short for CCCH RESP: %u\n",
85 dl = (struct l1ctl_info_dl *) msg->l1h;
86 sb = (struct l1ctl_sync_new_ccch_resp *) dl->payload;
88 gsm_fn2gsmtime(&tm, ntohl(dl->frame_nr));
89 DEBUGP(DL1C, "SCH: SNR: %u TDMA: (%.4u/%.2u/%.2u) bsic: %d\n",
90 dl->snr, tm.t1, tm.t2, tm.t3, sb->bsic);
95 char *chan_nr2string(uint8_t chan_nr)
98 uint8_t cbits = chan_nr >> 3;
103 sprintf(str, "TCH/F");
104 else if ((cbits & 0x1e) == 0x02)
105 sprintf(str, "TCH/H(%u)", cbits & 0x01);
106 else if ((cbits & 0x1c) == 0x04)
107 sprintf(str, "SDCCH/4(%u)", cbits & 0x03);
108 else if ((cbits & 0x18) == 0x08)
109 sprintf(str, "SDCCH/8(%u)", cbits & 0x07);
110 else if (cbits == 0x10)
111 sprintf(str, "BCCH");
112 else if (cbits == 0x11)
113 sprintf(str, "RACH");
114 else if (cbits == 0x12)
115 sprintf(str, "PCH/AGCH");
117 sprintf(str, "UNKNOWN");
122 /* Receive L1CTL_DATA_IND (Data Indication from L1) */
123 static int rx_ph_data_ind(struct osmocom_ms *ms, struct msgb *msg)
125 struct l1ctl_info_dl *dl, dl_cpy;
126 struct l1ctl_data_ind *ccch;
127 struct lapdm_entity *le;
128 uint8_t chan_type, chan_ts, chan_ss;
129 uint8_t gsmtap_chan_type;
132 if (msgb_l3len(msg) < sizeof(*ccch)) {
133 LOGP(DL1C, LOGL_ERROR, "MSG too short Data Ind: %u\n",
138 dl = (struct l1ctl_info_dl *) msg->l1h;
139 msg->l2h = dl->payload;
140 ccch = (struct l1ctl_data_ind *) msg->l2h;
142 gsm_fn2gsmtime(&tm, ntohl(dl->frame_nr));
143 rsl_dec_chan_nr(dl->chan_nr, &chan_type, &chan_ss, &chan_ts);
144 DEBUGP(DL1C, "%s (%.4u/%.2u/%.2u) %s\n",
145 chan_nr2string(dl->chan_nr), tm.t1, tm.t2, tm.t3,
146 hexdump(ccch->data, sizeof(ccch->data)));
148 /* send CCCH data via GSMTAP */
149 gsmtap_chan_type = chantype_rsl2gsmtap(chan_type, dl->link_id);
150 gsmtap_sendmsg(dl->band_arfcn, chan_ts, gsmtap_chan_type, chan_ss,
151 tm.fn, dl->rx_level-110, dl->snr, ccch->data,
154 /* determine LAPDm entity based on SACCH or not */
155 if (dl->link_id & 0x40)
156 le = &ms->l2_entity.lapdm_acch;
158 le = &ms->l2_entity.lapdm_dcch;
159 /* make local stack copy of l1ctl_info_dl, as LAPDm will
160 * overwrite skb hdr */
161 memcpy(&dl_cpy, dl, sizeof(dl_cpy));
163 /* pull the L1 header from the msgb */
164 msgb_pull(msg, msg->l2h - (msg->l1h-sizeof(struct l1ctl_hdr)));
167 /* send it up into LAPDm */
168 l2_ph_data_ind(msg, le, &dl_cpy);
173 /* Transmit L1CTL_DATA_REQ */
174 int tx_ph_data_req(struct osmocom_ms *ms, struct msgb *msg,
175 uint8_t chan_nr, uint8_t link_id)
177 struct l1ctl_hdr *l1h;
178 struct l1ctl_info_ul *l1i_ul;
179 uint8_t chan_type, chan_ts, chan_ss;
180 uint8_t gsmtap_chan_type;
182 DEBUGP(DL1C, "(%s)\n", hexdump(msg->l2h, msgb_l2len(msg)));
184 if (msgb_l2len(msg) > 23) {
185 LOGP(DL1C, LOGL_ERROR, "L1 cannot handle message length "
186 "> 23 (%u)\n", msgb_l2len(msg));
189 } else if (msgb_l2len(msg) < 23)
190 LOGP(DL1C, LOGL_ERROR, "L1 message length < 23 (%u) "
191 "doesn't seem right!\n", msgb_l2len(msg));
193 /* send copy via GSMTAP */
194 rsl_dec_chan_nr(chan_nr, &chan_type, &chan_ss, &chan_ts);
195 gsmtap_chan_type = chantype_rsl2gsmtap(chan_type, link_id);
196 gsmtap_sendmsg(0|0x4000, chan_ts, gsmtap_chan_type, chan_ss,
197 0, 127, 255, msg->l2h, msgb_l2len(msg));
199 /* prepend uplink info header */
200 l1i_ul = (struct l1ctl_info_ul *) msgb_push(msg, sizeof(*l1i_ul));
202 l1i_ul->chan_nr = chan_nr;
203 l1i_ul->link_id = link_id;
205 /* FIXME: where to get this from? */
206 l1i_ul->tx_power = 0;
208 /* prepend l1 header */
209 msg->l1h = msgb_push(msg, sizeof(*l1h));
210 l1h = (struct l1ctl_hdr *) msg->l1h;
211 l1h->msg_type = L1CTL_DATA_REQ;
213 return osmo_send_l1(ms, msg);
216 /* Transmit NEW_CCCH_REQ */
217 int l1ctl_tx_ccch_req(struct osmocom_ms *ms, uint16_t arfcn)
220 struct l1ctl_sync_new_ccch_req *req;
222 msg = osmo_l1_alloc(L1CTL_NEW_CCCH_REQ);
226 req = (struct l1ctl_sync_new_ccch_req *) msgb_put(msg, sizeof(*req));
227 req->band_arfcn = osmo_make_band_arfcn(ms, arfcn);
229 return osmo_send_l1(ms, msg);
232 /* Transmit L1CTL_RACH_REQ */
233 int tx_ph_rach_req(struct osmocom_ms *ms)
236 struct l1ctl_info_ul *ul;
237 struct l1ctl_rach_req *req;
238 static uint8_t i = 0;
240 msg = osmo_l1_alloc(L1CTL_RACH_REQ);
244 DEBUGP(DL1C, "RACH Req.\n");
245 ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
246 req = (struct l1ctl_rach_req *) msgb_put(msg, sizeof(*req));
249 return osmo_send_l1(ms, msg);
252 /* Transmit L1CTL_DM_EST_REQ */
253 int tx_ph_dm_est_req(struct osmocom_ms *ms, uint16_t band_arfcn, uint8_t chan_nr)
256 struct l1ctl_info_ul *ul;
257 struct l1ctl_dm_est_req *req;
259 msg = osmo_l1_alloc(L1CTL_DM_EST_REQ);
263 DEBUGP(DL1C, "Tx Dedic.Mode Est Req (arfcn=%u, chan_nr=0x%02x)\n",
264 band_arfcn, chan_nr);
265 ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
266 ul->chan_nr = chan_nr;
268 ul->tx_power = 0; /* FIXME: initial TX power */
269 req = (struct l1ctl_dm_est_req *) msgb_put(msg, sizeof(*req));
270 req->band_arfcn = band_arfcn;
272 return osmo_send_l1(ms, msg);
275 int l1ctl_tx_echo_req(struct osmocom_ms *ms, unsigned int len)
281 msg = osmo_l1_alloc(L1CTL_ECHO_REQ);
285 data = msgb_put(msg, len);
286 for (i = 0; i < len; i++)
289 return osmo_send_l1(ms, msg);
292 /* Transmit L1CTL_PM_REQ */
293 int l1ctl_tx_pm_req_range(struct osmocom_ms *ms, uint16_t arfcn_from,
297 struct l1ctl_pm_req *pm;
299 msg = osmo_l1_alloc(L1CTL_PM_REQ);
303 printf("Tx PM Req (%u-%u)\n", arfcn_from, arfcn_to);
304 pm = (struct l1ctl_pm_req *) msgb_put(msg, sizeof(*pm));
306 pm->range.band_arfcn_from = htons(arfcn_from);
307 pm->range.band_arfcn_to = htons(arfcn_to);
309 return osmo_send_l1(ms, msg);
312 /* Receive L1CTL_RESET */
313 static int rx_l1_reset(struct osmocom_ms *ms)
315 printf("Layer1 Reset.\n");
316 dispatch_signal(SS_L1CTL, S_L1CTL_RESET, ms);
319 /* Receive L1CTL_PM_RESP */
320 static int rx_l1_pm_resp(struct osmocom_ms *ms, struct msgb *msg)
322 struct l1ctl_pm_resp *pmr;
324 for (pmr = (struct l1ctl_pm_resp *) msg->l1h;
325 (uint8_t *) pmr < msg->tail; pmr++) {
326 struct osmobb_meas_res mr;
327 DEBUGP(DL1C, "PM MEAS: ARFCN: %4u RxLev: %3d %3d\n",
328 ntohs(pmr->band_arfcn), pmr->pm[0], pmr->pm[1]);
329 mr.band_arfcn = ntohs(pmr->band_arfcn);
330 mr.rx_lev = (pmr->pm[0] + pmr->pm[1]) / 2;
332 dispatch_signal(SS_L1CTL, S_L1CTL_PM_RES, &mr);
337 /* Receive incoming data from L1 using L1CTL format */
338 int l1ctl_recv(struct osmocom_ms *ms, struct msgb *msg)
341 struct l1ctl_hdr *l1h;
342 struct l1ctl_info_dl *dl;
344 if (msgb_l2len(msg) < sizeof(*dl)) {
345 LOGP(DL1C, LOGL_ERROR, "Short Layer2 message: %u\n",
350 l1h = (struct l1ctl_hdr *) msg->l1h;
352 /* move the l1 header pointer to point _BEHIND_ l1ctl_hdr,
353 as the l1ctl header is of no interest to subsequent code */
354 msg->l1h = l1h->data;
356 switch (l1h->msg_type) {
357 case L1CTL_NEW_CCCH_RESP:
358 rc = rx_l1_ccch_resp(ms, msg);
361 rc = rx_ph_data_ind(ms, msg);
364 rc = rx_l1_reset(ms);
367 rc = rx_l1_pm_resp(ms, msg);
368 if (l1h->flags & L1CTL_F_DONE)
369 dispatch_signal(SS_L1CTL, S_L1CTL_PM_DONE, ms);
372 fprintf(stderr, "Unknown MSG: %u\n", l1h->msg_type);