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