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