correct endian conversion of arfcn in tx_ph_dm_ext_req()
[osmocom-bb.git] / src / host / layer23 / src / 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 <l1a_l23_interface.h>
32
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>
42
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>
48
49 static struct msgb *osmo_l1_alloc(uint8_t msg_type)
50 {
51         struct l1ctl_hdr *l1h;
52         struct msgb *msg = msgb_alloc_headroom(256, 4, "osmo_l1");
53
54         if (!msg) {
55                 LOGP(DL1C, LOGL_ERROR, "Failed to allocate memory.\n");
56                 return NULL;
57         }
58
59         msg->l1h = msgb_put(msg, sizeof(*l1h));
60         l1h = (struct l1ctl_hdr *) msg->l1h;
61         l1h->msg_type = msg_type;
62         
63         return msg;
64 }
65
66
67 static int osmo_make_band_arfcn(struct osmocom_ms *ms, uint16_t arfcn)
68 {
69         /* TODO: Include the band */
70         return arfcn;
71 }
72
73 static int rx_l1_fbsb_resp(struct osmocom_ms *ms, struct msgb *msg)
74 {
75         struct l1ctl_info_dl *dl;
76         struct l1ctl_fbsb_resp *sb;
77         struct gsm_time tm;
78
79         if (msgb_l3len(msg) < sizeof(*dl) + sizeof(*sb)) {
80                 LOGP(DL1C, LOGL_ERROR, "FBSB RESP: MSG too short %u\n",
81                         msgb_l3len(msg));
82                 return -1;
83         }
84
85         dl = (struct l1ctl_info_dl *) msg->l1h;
86         sb = (struct l1ctl_fbsb_resp *) dl->payload;
87
88         printf("snr=%04x, arfcn=%u result=%u\n", dl->snr, ntohs(dl->band_arfcn),
89                 sb->result);
90
91         if (sb->result != 0) {
92                 LOGP(DL1C, LOGL_ERROR, "FBSB RESP: result=%u\n", sb->result);
93                 dispatch_signal(SS_L1CTL, S_L1CTL_FBSB_ERR, ms);
94                 return 0;
95         }
96
97         gsm_fn2gsmtime(&tm, ntohl(dl->frame_nr));
98         DEBUGP(DL1C, "SCH: SNR: %u TDMA: (%.4u/%.2u/%.2u) bsic: %d\n",
99                 dl->snr, tm.t1, tm.t2, tm.t3, sb->bsic);
100         dispatch_signal(SS_L1CTL, S_L1CTL_FBSB_RESP, ms);
101
102         return 0;
103 }
104
105 char *chan_nr2string(uint8_t chan_nr)
106 {
107         static char str[20];
108         uint8_t cbits = chan_nr >> 3;
109
110         str[0] = '\0';
111
112         if (cbits == 0x01)
113                 sprintf(str, "TCH/F");
114         else if ((cbits & 0x1e) == 0x02)
115                 sprintf(str, "TCH/H(%u)", cbits & 0x01);
116         else if ((cbits & 0x1c) == 0x04)
117                 sprintf(str, "SDCCH/4(%u)", cbits & 0x03);
118         else if ((cbits & 0x18) == 0x08)
119                 sprintf(str, "SDCCH/8(%u)", cbits & 0x07);
120         else if (cbits == 0x10)
121                 sprintf(str, "BCCH");
122         else if (cbits == 0x11)
123                 sprintf(str, "RACH");
124         else if (cbits == 0x12)
125                 sprintf(str, "PCH/AGCH");
126         else
127                 sprintf(str, "UNKNOWN");
128
129         return str;
130 }
131
132 /* Receive L1CTL_DATA_IND (Data Indication from L1) */
133 static int rx_ph_data_ind(struct osmocom_ms *ms, struct msgb *msg)
134 {
135         struct l1ctl_info_dl *dl, dl_cpy;
136         struct l1ctl_data_ind *ccch;
137         struct lapdm_entity *le;
138         uint8_t chan_type, chan_ts, chan_ss;
139         uint8_t gsmtap_chan_type;
140         struct gsm_time tm;
141
142         if (msgb_l3len(msg) < sizeof(*ccch)) {
143                 LOGP(DL1C, LOGL_ERROR, "MSG too short Data Ind: %u\n",
144                         msgb_l3len(msg));
145                 return -1;
146         }
147
148         dl = (struct l1ctl_info_dl *) msg->l1h;
149         msg->l2h = dl->payload;
150         ccch = (struct l1ctl_data_ind *) msg->l2h;
151
152         gsm_fn2gsmtime(&tm, ntohl(dl->frame_nr));
153         rsl_dec_chan_nr(dl->chan_nr, &chan_type, &chan_ss, &chan_ts);
154         DEBUGP(DL1C, "%s (%.4u/%.2u/%.2u) %s\n",
155                 chan_nr2string(dl->chan_nr), tm.t1, tm.t2, tm.t3,
156                 hexdump(ccch->data, sizeof(ccch->data)));
157
158         /* send CCCH data via GSMTAP */
159         gsmtap_chan_type = chantype_rsl2gsmtap(chan_type, dl->link_id);
160         gsmtap_sendmsg(dl->band_arfcn, chan_ts, gsmtap_chan_type, chan_ss,
161                         tm.fn, dl->rx_level-110, dl->snr, ccch->data,
162                         sizeof(ccch->data));
163
164         /* determine LAPDm entity based on SACCH or not */
165         if (dl->link_id & 0x40)
166                 le = &ms->l2_entity.lapdm_acch;
167         else
168                 le = &ms->l2_entity.lapdm_dcch;
169         /* make local stack copy of l1ctl_info_dl, as LAPDm will
170          * overwrite skb hdr */
171         memcpy(&dl_cpy, dl, sizeof(dl_cpy));
172
173         /* pull the L1 header from the msgb */
174         msgb_pull(msg, msg->l2h - (msg->l1h-sizeof(struct l1ctl_hdr)));
175         msg->l1h = NULL;
176
177         /* send it up into LAPDm */
178         l2_ph_data_ind(msg, le, &dl_cpy);
179
180         return 0;
181 }
182
183 /* Transmit L1CTL_DATA_REQ */
184 int tx_ph_data_req(struct osmocom_ms *ms, struct msgb *msg,
185                    uint8_t chan_nr, uint8_t link_id)
186 {
187         struct l1ctl_hdr *l1h;
188         struct l1ctl_info_ul *l1i_ul;
189         uint8_t chan_type, chan_ts, chan_ss;
190         uint8_t gsmtap_chan_type;
191
192         DEBUGP(DL1C, "(%s)\n", hexdump(msg->l2h, msgb_l2len(msg)));
193
194         if (msgb_l2len(msg) > 23) {
195                 LOGP(DL1C, LOGL_ERROR, "L1 cannot handle message length "
196                         "> 23 (%u)\n", msgb_l2len(msg));
197                 msgb_free(msg);
198                 return -EINVAL;
199         } else if (msgb_l2len(msg) < 23)
200                 LOGP(DL1C, LOGL_ERROR, "L1 message length < 23 (%u) "
201                         "doesn't seem right!\n", msgb_l2len(msg));
202
203         /* send copy via GSMTAP */
204         rsl_dec_chan_nr(chan_nr, &chan_type, &chan_ss, &chan_ts);
205         gsmtap_chan_type = chantype_rsl2gsmtap(chan_type, link_id);
206         gsmtap_sendmsg(0|0x4000, chan_ts, gsmtap_chan_type, chan_ss,
207                         0, 127, 255, msg->l2h, msgb_l2len(msg));
208
209         /* prepend uplink info header */
210         l1i_ul = (struct l1ctl_info_ul *) msgb_push(msg, sizeof(*l1i_ul));
211
212         l1i_ul->chan_nr = chan_nr;
213         l1i_ul->link_id = link_id;
214
215         /* FIXME: where to get this from? */
216         l1i_ul->tx_power = 0;
217
218         /* prepend l1 header */
219         msg->l1h = msgb_push(msg, sizeof(*l1h));
220         l1h = (struct l1ctl_hdr *) msg->l1h;
221         l1h->msg_type = L1CTL_DATA_REQ;
222
223         return osmo_send_l1(ms, msg);
224 }
225
226 /* Transmit FBSB_REQ */
227 int l1ctl_tx_fbsb_req(struct osmocom_ms *ms, uint16_t arfcn,
228                       uint8_t flags, uint16_t timeout, uint8_t sync_info_idx)
229 {
230         struct msgb *msg;
231         struct l1ctl_fbsb_req *req;
232
233         msg = osmo_l1_alloc(L1CTL_FBSB_REQ);
234         if (!msg)
235                 return -1;
236
237         req = (struct l1ctl_fbsb_req *) msgb_put(msg, sizeof(*req));
238         req->band_arfcn = htons(osmo_make_band_arfcn(ms, arfcn));
239         req->timeout = htons(timeout);
240         /* Threshold when to consider FB_MODE1: 4kHz - 1kHz */
241         req->freq_err_thresh1 = htons(4000 - 1000);
242         /* Threshold when to consider SCH: 1kHz - 200Hz */
243         req->freq_err_thresh2 = htons(1000 - 200);
244         /* not used yet! */
245         req->num_freqerr_avg = 3;
246         req->flags = flags;
247         req->sync_info_idx = sync_info_idx;
248
249         return osmo_send_l1(ms, msg);
250 }
251
252 /* Transmit L1CTL_RACH_REQ */
253 int tx_ph_rach_req(struct osmocom_ms *ms)
254 {
255         struct msgb *msg;
256         struct l1ctl_info_ul *ul;
257         struct l1ctl_rach_req *req;
258         static uint8_t i = 0;
259
260         msg = osmo_l1_alloc(L1CTL_RACH_REQ);
261         if (!msg)
262                 return -1;
263
264         DEBUGP(DL1C, "RACH Req.\n");
265         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
266         req = (struct l1ctl_rach_req *) msgb_put(msg, sizeof(*req));
267         req->ra = i++;
268
269         return osmo_send_l1(ms, msg);
270 }
271
272 /* Transmit L1CTL_DM_EST_REQ */
273 int tx_ph_dm_est_req(struct osmocom_ms *ms, uint16_t band_arfcn, uint8_t chan_nr)
274 {
275         struct msgb *msg;
276         struct l1ctl_info_ul *ul;
277         struct l1ctl_dm_est_req *req;
278
279         msg = osmo_l1_alloc(L1CTL_DM_EST_REQ);
280         if (!msg)
281                 return -1;
282
283         DEBUGP(DL1C, "Tx Dedic.Mode Est Req (arfcn=%u, chan_nr=0x%02x)\n",
284                 band_arfcn, chan_nr);
285         ul = (struct l1ctl_info_ul *) msgb_put(msg, sizeof(*ul));
286         ul->chan_nr = chan_nr;
287         ul->link_id = 0;
288         ul->tx_power = 0; /* FIXME: initial TX power */
289         req = (struct l1ctl_dm_est_req *) msgb_put(msg, sizeof(*req));
290         req->band_arfcn = htons(band_arfcn);
291
292         return osmo_send_l1(ms, msg);
293 }
294
295 int l1ctl_tx_echo_req(struct osmocom_ms *ms, unsigned int len)
296 {
297         struct msgb *msg;
298         uint8_t *data;
299         unsigned int i;
300
301         msg = osmo_l1_alloc(L1CTL_ECHO_REQ);
302         if (!msg)
303                 return -1;
304
305         data = msgb_put(msg, len);
306         for (i = 0; i < len; i++)
307                 data[i] = i % 8;
308
309         return osmo_send_l1(ms, msg);
310 }
311
312 /* Transmit L1CTL_PM_REQ */
313 int l1ctl_tx_pm_req_range(struct osmocom_ms *ms, uint16_t arfcn_from,
314                           uint16_t arfcn_to)
315 {
316         struct msgb *msg;
317         struct l1ctl_pm_req *pm;
318
319         msg = osmo_l1_alloc(L1CTL_PM_REQ);
320         if (!msg)
321                 return -1;
322
323         printf("Tx PM Req (%u-%u)\n", arfcn_from, arfcn_to);
324         pm = (struct l1ctl_pm_req *) msgb_put(msg, sizeof(*pm));
325         pm->type = 1;
326         pm->range.band_arfcn_from = htons(arfcn_from);
327         pm->range.band_arfcn_to = htons(arfcn_to);
328
329         return osmo_send_l1(ms, msg);
330 }
331
332 /* Receive L1CTL_RESET */
333 static int rx_l1_reset(struct osmocom_ms *ms)
334 {
335         printf("Layer1 Reset.\n");
336         dispatch_signal(SS_L1CTL, S_L1CTL_RESET, ms);
337 }
338
339 /* Receive L1CTL_PM_RESP */
340 static int rx_l1_pm_resp(struct osmocom_ms *ms, struct msgb *msg)
341 {
342         struct l1ctl_pm_resp *pmr;
343
344         for (pmr = (struct l1ctl_pm_resp *) msg->l1h;
345              (uint8_t *) pmr < msg->tail; pmr++) {
346                 struct osmobb_meas_res mr;
347                 DEBUGP(DL1C, "PM MEAS: ARFCN: %4u RxLev: %3d %3d\n",
348                         ntohs(pmr->band_arfcn), pmr->pm[0], pmr->pm[1]);
349                 mr.band_arfcn = ntohs(pmr->band_arfcn);
350                 mr.rx_lev = (pmr->pm[0] + pmr->pm[1]) / 2;
351                 mr.ms = ms;
352                 dispatch_signal(SS_L1CTL, S_L1CTL_PM_RES, &mr);
353         }
354         return 0;
355 }
356
357 /* Receive incoming data from L1 using L1CTL format */
358 int l1ctl_recv(struct osmocom_ms *ms, struct msgb *msg)
359 {
360         int rc = 0;
361         struct l1ctl_hdr *l1h;
362         struct l1ctl_info_dl *dl;
363
364         if (msgb_l2len(msg) < sizeof(*dl)) {
365                 LOGP(DL1C, LOGL_ERROR, "Short Layer2 message: %u\n",
366                         msgb_l2len(msg));
367                 return -1;
368         }
369
370         l1h = (struct l1ctl_hdr *) msg->l1h;
371
372         /* move the l1 header pointer to point _BEHIND_ l1ctl_hdr,
373            as the l1ctl header is of no interest to subsequent code */
374         msg->l1h = l1h->data;
375
376         switch (l1h->msg_type) {
377         case L1CTL_FBSB_RESP:
378                 rc = rx_l1_fbsb_resp(ms, msg);
379                 break;
380         case L1CTL_DATA_IND:
381                 rc = rx_ph_data_ind(ms, msg);
382                 break;
383         case L1CTL_RESET:
384                 rc = rx_l1_reset(ms);
385                 break;
386         case L1CTL_PM_RESP:
387                 rc = rx_l1_pm_resp(ms, msg);
388                 if (l1h->flags & L1CTL_F_DONE)
389                         dispatch_signal(SS_L1CTL, S_L1CTL_PM_DONE, ms);
390                 break;
391         default:
392                 fprintf(stderr, "Unknown MSG: %u\n", l1h->msg_type);
393                 break;
394         }
395
396         return rc;
397 }