df0eb1d40401e9b8958ca6f77480e25315f06d58
[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_resp(struct osmocom_ms *ms, struct msgb *msg)
75 {
76         struct l1ctl_info_dl *dl;
77         struct l1ctl_fbsb_resp *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_resp *) 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 char *chan_nr2string(uint8_t chan_nr)
107 {
108         static char str[20];
109         uint8_t cbits = chan_nr >> 3;
110
111         str[0] = '\0';
112
113         if (cbits == 0x01)
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");
127         else
128                 sprintf(str, "UNKNOWN");
129
130         return str;
131 }
132
133 /* Receive L1CTL_DATA_IND (Data Indication from L1) */
134 static int rx_ph_data_ind(struct osmocom_ms *ms, struct msgb *msg)
135 {
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;
141         struct gsm_time tm;
142
143         if (msgb_l3len(msg) < sizeof(*ccch)) {
144                 LOGP(DL1C, LOGL_ERROR, "MSG too short Data Ind: %u\n",
145                         msgb_l3len(msg));
146                 msgb_free(msg);
147                 return -1;
148         }
149
150         dl = (struct l1ctl_info_dl *) msg->l1h;
151         msg->l2h = dl->payload;
152         ccch = (struct l1ctl_data_ind *) msg->l2h;
153
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)));
159
160         if (dl->num_biterr) {
161                 LOGP(DL1C, LOGL_NOTICE, "Dropping frame with %u bit errors\n",
162                         dl->num_biterr);
163                 return 0;
164         }
165
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,
170                         sizeof(ccch->data));
171
172         /* determine LAPDm entity based on SACCH or not */
173         if (dl->link_id & 0x40)
174                 le = &ms->l2_entity.lapdm_acch;
175         else
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));
180
181         /* pull the L1 header from the msgb */
182         msgb_pull(msg, msg->l2h - (msg->l1h-sizeof(struct l1ctl_hdr)));
183         msg->l1h = NULL;
184
185         /* send it up into LAPDm */
186         l2_ph_data_ind(msg, le, &dl_cpy);
187
188         return 0;
189 }
190
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)
194 {
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;
199
200         DEBUGP(DL1C, "(%s)\n", hexdump(msg->l2h, msgb_l2len(msg)));
201
202         if (msgb_l2len(msg) > 23) {
203                 LOGP(DL1C, LOGL_ERROR, "L1 cannot handle message length "
204                         "> 23 (%u)\n", msgb_l2len(msg));
205                 msgb_free(msg);
206                 return -EINVAL;
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));
210
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));
216
217         /* prepend uplink info header */
218         l1i_ul = (struct l1ctl_info_ul *) msgb_push(msg, sizeof(*l1i_ul));
219
220         l1i_ul->chan_nr = chan_nr;
221         l1i_ul->link_id = link_id;
222
223         /* FIXME: where to get this from? */
224         l1i_ul->tx_power = 0;
225
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;
230
231         return osmo_send_l1(ms, msg);
232 }
233
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)
237 {
238         struct msgb *msg;
239         struct l1ctl_fbsb_req *req;
240
241         msg = osmo_l1_alloc(L1CTL_FBSB_REQ);
242         if (!msg)
243                 return -1;
244
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);
252         /* not used yet! */
253         req->num_freqerr_avg = 3;
254         req->flags = flags;
255         req->sync_info_idx = sync_info_idx;
256
257         return osmo_send_l1(ms, msg);
258 }
259
260 /* Transmit L1CTL_RACH_REQ */
261 int tx_ph_rach_req(struct osmocom_ms *ms)
262 {
263         struct msgb *msg;
264         struct l1ctl_info_ul *ul;
265         struct l1ctl_rach_req *req;
266         static uint8_t i = 0;
267
268         msg = osmo_l1_alloc(L1CTL_RACH_REQ);
269         if (!msg)
270                 return -1;
271
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));
275         req->ra = i++;
276
277         return osmo_send_l1(ms, msg);
278 }
279
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)
282 {
283         struct msgb *msg;
284         struct l1ctl_info_ul *ul;
285         struct l1ctl_dm_est_req *req;
286
287         msg = osmo_l1_alloc(L1CTL_DM_EST_REQ);
288         if (!msg)
289                 return -1;
290
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;
295         ul->link_id = 0;
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);
299
300         return osmo_send_l1(ms, msg);
301 }
302
303 int l1ctl_tx_echo_req(struct osmocom_ms *ms, unsigned int len)
304 {
305         struct msgb *msg;
306         uint8_t *data;
307         unsigned int i;
308
309         msg = osmo_l1_alloc(L1CTL_ECHO_REQ);
310         if (!msg)
311                 return -1;
312
313         data = msgb_put(msg, len);
314         for (i = 0; i < len; i++)
315                 data[i] = i % 8;
316
317         return osmo_send_l1(ms, msg);
318 }
319
320 /* Transmit L1CTL_PM_REQ */
321 int l1ctl_tx_pm_req_range(struct osmocom_ms *ms, uint16_t arfcn_from,
322                           uint16_t arfcn_to)
323 {
324         struct msgb *msg;
325         struct l1ctl_pm_req *pm;
326
327         msg = osmo_l1_alloc(L1CTL_PM_REQ);
328         if (!msg)
329                 return -1;
330
331         printf("Tx PM Req (%u-%u)\n", arfcn_from, arfcn_to);
332         pm = (struct l1ctl_pm_req *) msgb_put(msg, sizeof(*pm));
333         pm->type = 1;
334         pm->range.band_arfcn_from = htons(arfcn_from);
335         pm->range.band_arfcn_to = htons(arfcn_to);
336
337         return osmo_send_l1(ms, msg);
338 }
339
340 /* Receive L1CTL_RESET_IND */
341 static int rx_l1_reset(struct osmocom_ms *ms)
342 {
343         printf("Layer1 Reset.\n");
344         dispatch_signal(SS_L1CTL, S_L1CTL_RESET, ms);
345
346         return 0;
347 }
348
349 /* Receive L1CTL_PM_RESP */
350 static int rx_l1_pm_resp(struct osmocom_ms *ms, struct msgb *msg)
351 {
352         struct l1ctl_pm_resp *pmr;
353
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;
361                 mr.ms = ms;
362                 dispatch_signal(SS_L1CTL, S_L1CTL_PM_RES, &mr);
363         }
364         return 0;
365 }
366
367 /* Receive incoming data from L1 using L1CTL format */
368 int l1ctl_recv(struct osmocom_ms *ms, struct msgb *msg)
369 {
370         int rc = 0;
371         struct l1ctl_hdr *l1h;
372         struct l1ctl_info_dl *dl;
373
374         if (msgb_l2len(msg) < sizeof(*dl)) {
375                 LOGP(DL1C, LOGL_ERROR, "Short Layer2 message: %u\n",
376                         msgb_l2len(msg));
377                 msgb_free(msg);
378                 return -1;
379         }
380
381         l1h = (struct l1ctl_hdr *) msg->l1h;
382
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;
386
387         switch (l1h->msg_type) {
388         case L1CTL_FBSB_RESP:
389                 rc = rx_l1_fbsb_resp(ms, msg);
390                 msgb_free(msg);
391                 break;
392         case L1CTL_DATA_IND:
393                 rc = rx_ph_data_ind(ms, msg);
394                 break;
395         case L1CTL_RESET_IND:
396                 rc = rx_l1_reset(ms);
397                 msgb_free(msg);
398                 break;
399         case L1CTL_PM_RESP:
400                 rc = rx_l1_pm_resp(ms, msg);
401                 msgb_free(msg);
402                 if (l1h->flags & L1CTL_F_DONE)
403                         dispatch_signal(SS_L1CTL, S_L1CTL_PM_DONE, ms);
404                 break;
405         default:
406                 fprintf(stderr, "Unknown MSG: %u\n", l1h->msg_type);
407                 msgb_free(msg);
408                 break;
409         }
410
411         return rc;
412 }