layer23: convert to libosmocore logging API
[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/logging.h>
34 #include <osmocore/timer.h>
35 #include <osmocore/msgb.h>
36 #include <osmocore/tlv.h>
37 #include <osmocore/gsm_utils.h>
38 #include <osmocore/protocol/gsm_04_08.h>
39 #include <osmocore/protocol/gsm_08_58.h>
40 #include <osmocore/rsl.h>
41
42 #include <osmocom/l1ctl.h>
43 #include <osmocom/osmocom_data.h>
44 #include <osmocom/lapdm.h>
45 #include <osmocom/logging.h>
46 #include <osmocom/gsmtap_util.h>
47
48 static struct msgb *osmo_l1_alloc(uint8_t msg_type)
49 {
50         struct l1ctl_hdr *l1h;
51         struct msgb *msg = msgb_alloc_headroom(256, 4, "osmo_l1");
52
53         if (!msg) {
54                 LOGP(DL1C, LOGL_ERROR, "Failed to allocate memory.\n");
55                 return NULL;
56         }
57
58         msg->l1h = msgb_put(msg, sizeof(*l1h));
59         l1h = (struct l1ctl_hdr *) msg->l1h;
60         l1h->msg_type = msg_type;
61         
62         return msg;
63 }
64
65
66 static int osmo_make_band_arfcn(struct osmocom_ms *ms)
67 {
68         /* TODO: Include the band */
69         return ms->arfcn;
70 }
71
72 static int rx_l1_ccch_resp(struct osmocom_ms *ms, struct msgb *msg)
73 {
74         struct l1ctl_info_dl *dl;
75         struct l1ctl_sync_new_ccch_resp *sb;
76         struct gsm_time tm;
77
78         if (msgb_l3len(msg) < sizeof(*sb)) {
79                 LOGP(DL1C, LOGL_ERROR, "MSG too short for CCCH RESP: %u\n",
80                         msgb_l3len(msg));
81                 return -1;
82         }
83
84         dl = (struct l1ctl_info_dl *) msg->l1h;
85         sb = (struct l1ctl_sync_new_ccch_resp *) dl->payload;
86
87         gsm_fn2gsmtime(&tm, ntohl(dl->frame_nr));
88         DEBUGP(DL1C, "SCH: SNR: %u TDMA: (%.4u/%.2u/%.2u) bsic: %d\n",
89                 dl->snr, tm.t1, tm.t2, tm.t3, sb->bsic);
90
91         return 0;
92 }
93
94 char *chan_nr2string(uint8_t chan_nr)
95 {
96         static char str[20];
97         uint8_t cbits = chan_nr >> 3;
98
99         str[0] = '\0';
100
101         if (cbits == 0x01)
102                 sprintf(str, "TCH/F");
103         else if ((cbits & 0x1e) == 0x02)
104                 sprintf(str, "TCH/H(%u)", cbits & 0x01);
105         else if ((cbits & 0x1c) == 0x04)
106                 sprintf(str, "SDCCH/4(%u)", cbits & 0x03);
107         else if ((cbits & 0x18) == 0x08)
108                 sprintf(str, "SDCCH/8(%u)", cbits & 0x07);
109         else if (cbits == 0x10)
110                 sprintf(str, "BCCH");
111         else if (cbits == 0x11)
112                 sprintf(str, "RACH");
113         else if (cbits == 0x12)
114                 sprintf(str, "PCH/AGCH");
115         else
116                 sprintf(str, "UNKNOWN");
117
118         return str;
119 }
120
121 /* Receive L1CTL_DATA_IND (Data Indication from L1) */
122 static int rx_ph_data_ind(struct osmocom_ms *ms, struct msgb *msg)
123 {
124         struct l1ctl_info_dl *dl, dl_cpy;
125         struct l1ctl_data_ind *ccch;
126         struct lapdm_entity *le;
127         uint8_t chan_type, chan_ts, chan_ss;
128         uint8_t gsmtap_chan_type;
129         struct gsm_time tm;
130
131         if (msgb_l3len(msg) < sizeof(*ccch)) {
132                 LOGP(DL1C, LOGL_ERROR, "MSG too short Data Ind: %u\n",
133                         msgb_l3len(msg));
134                 return -1;
135         }
136
137         dl = (struct l1ctl_info_dl *) msg->l1h;
138         msg->l2h = dl->payload;
139         ccch = (struct l1ctl_data_ind *) msg->l2h;
140
141         gsm_fn2gsmtime(&tm, ntohl(dl->frame_nr));
142         rsl_dec_chan_nr(dl->chan_nr, &chan_type, &chan_ss, &chan_ts);
143         DEBUGP(DL1C, "%s (%.4u/%.2u/%.2u) %s\n",
144                 chan_nr2string(dl->chan_nr), tm.t1, tm.t2, tm.t3,
145                 hexdump(ccch->data, sizeof(ccch->data)));
146
147         /* send CCCH data via GSMTAP */
148         gsmtap_chan_type = chantype_rsl2gsmtap(chan_type, dl->link_id);
149         gsmtap_sendmsg(dl->band_arfcn, chan_ts, gsmtap_chan_type, chan_ss,
150                         tm.fn, dl->rx_level-110, dl->snr, ccch->data,
151                         sizeof(ccch->data));
152
153         /* determine LAPDm entity based on SACCH or not */
154         if (dl->link_id & 0x40)
155                 le = &ms->lapdm_acch;
156         else
157                 le = &ms->lapdm_dcch;
158         /* make local stack copy of l1ctl_info_dl, as LAPDm will
159          * overwrite skb hdr */
160         memcpy(&dl_cpy, dl, sizeof(dl_cpy));
161
162         /* pull the L1 header from the msgb */
163         msgb_pull(msg, msg->l2h - (msg->l1h-sizeof(struct l1ctl_hdr)));
164         msg->l1h = NULL;
165
166         /* send it up into LAPDm */
167         l2_ph_data_ind(msg, le, &dl_cpy);
168
169         return 0;
170 }
171
172 /* Transmit L1CTL_DATA_REQ */
173 int tx_ph_data_req(struct osmocom_ms *ms, struct msgb *msg,
174                    uint8_t chan_nr, uint8_t link_id)
175 {
176         struct l1ctl_hdr *l1h;
177         struct l1ctl_info_ul *l1i_ul;
178         uint8_t chan_type, chan_ts, chan_ss;
179         uint8_t gsmtap_chan_type;
180
181         DEBUGP(DL1C, "(%s)\n", hexdump(msg->l2h, msgb_l2len(msg)));
182
183         if (msgb_l2len(msg) > 23) {
184                 LOGP(DL1C, LOGL_ERROR, "L1 cannot handle message length "
185                         "> 23 (%u)\n", msgb_l2len(msg));
186                 msgb_free(msg);
187                 return -EINVAL;
188         } else if (msgb_l2len(msg) < 23)
189                 LOGP(DL1C, LOGL_ERROR, "L1 message length < 23 (%u) "
190                         "doesn't seem right!\n", msgb_l2len(msg));
191
192         /* send copy via GSMTAP */
193         rsl_dec_chan_nr(chan_nr, &chan_type, &chan_ss, &chan_ts);
194         gsmtap_chan_type = chantype_rsl2gsmtap(chan_type, link_id);
195         gsmtap_sendmsg(0|0x4000, chan_ts, gsmtap_chan_type, chan_ss,
196                         0, 127, 255, msg->l2h, msgb_l2len(msg));
197
198         /* prepend uplink info header */
199         l1i_ul = (struct l1ctl_info_ul *) msgb_push(msg, sizeof(*l1i_ul));
200
201         l1i_ul->chan_nr = chan_nr;
202         l1i_ul->link_id = link_id;
203
204         /* FIXME: where to get this from? */
205         l1i_ul->tx_power = 0;
206
207         /* prepend l1 header */
208         msg->l1h = msgb_push(msg, sizeof(*l1h));
209         l1h = (struct l1ctl_hdr *) msg->l1h;
210         l1h->msg_type = L1CTL_DATA_REQ;
211
212         return osmo_send_l1(ms, msg);
213 }
214
215 /* Receive L1CTL_RESET */
216 static int rx_l1_reset(struct osmocom_ms *ms)
217 {
218         struct msgb *msg;
219         struct l1ctl_sync_new_ccch_req *req;
220
221         msg = osmo_l1_alloc(L1CTL_NEW_CCCH_REQ);
222         if (!msg)
223                 return -1;
224
225         LOGP(DL1C, LOGL_INFO, "Layer1 Reset.\n");
226         req = (struct l1ctl_sync_new_ccch_req *) msgb_put(msg, sizeof(*req));
227         req->band_arfcn = osmo_make_band_arfcn(ms);
228
229         return osmo_send_l1(ms, msg);
230 }
231
232 /* Transmit L1CTL_RACH_REQ */
233 int tx_ph_rach_req(struct osmocom_ms *ms)
234 {
235         struct msgb *msg;
236         struct l1ctl_info_ul *ul;
237         struct l1ctl_rach_req *req;
238         static uint8_t i = 0;
239
240         msg = osmo_l1_alloc(L1CTL_RACH_REQ);
241         if (!msg)
242                 return -1;
243
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));
247         req->ra = i++;
248
249         return osmo_send_l1(ms, msg);
250 }
251
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)
254 {
255         struct msgb *msg;
256         struct l1ctl_info_ul *ul;
257         struct l1ctl_dm_est_req *req;
258
259         msg = osmo_l1_alloc(L1CTL_DM_EST_REQ);
260         if (!msg)
261                 return -1;
262
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;
267         ul->link_id = 0;
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;
271
272         return osmo_send_l1(ms, msg);
273 }
274
275 /* Receive incoming data from L1 using L1CTL format */
276 int l1ctl_recv(struct osmocom_ms *ms, struct msgb *msg)
277 {
278         int rc = 0;
279         struct l1ctl_hdr *l1h;
280         struct l1ctl_info_dl *dl;
281
282         if (msgb_l2len(msg) < sizeof(*dl)) {
283                 LOGP(DL1C, LOGL_ERROR, "Short Layer2 message: %u\n",
284                         msgb_l2len(msg));
285                 return -1;
286         }
287
288         l1h = (struct l1ctl_info_dl *) msg->l1h;
289
290         /* move the l1 header pointer to point _BEHIND_ l1ctl_hdr,
291            as the l1ctl header is of no interest to subsequent code */
292         msg->l1h = l1h->data;
293
294         switch (l1h->msg_type) {
295         case L1CTL_NEW_CCCH_RESP:
296                 rc = rx_l1_ccch_resp(ms, msg);
297                 break;
298         case L1CTL_DATA_IND:
299                 rc = rx_ph_data_ind(ms, msg);
300                 break;
301         case L1CTL_RESET:
302                 rc = rx_l1_reset(ms);
303                 break;
304         default:
305                 fprintf(stderr, "Unknown MSG: %u\n", l1h->msg_type);
306                 break;
307         }
308
309         return rc;
310 }