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