summaryrefslogtreecommitdiffstats
path: root/src/host/layer23/src/transceiver/l1ctl.c
blob: 1c764b096122d3d6ef0ca2b2011272c27dda79bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * l1ctl.c
 *
 * Minimal L1CTL
 *
 * Copyright (C) 2013  Sylvain Munaut <tnt@246tNt.com>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <string.h>

#include <osmocom/core/select.h>
#include <osmocom/core/talloc.h>

#include <osmocom/bb/common/logging.h>

#include <errno.h>
#include <arpa/inet.h>

#include <osmocom/gsm/gsm_utils.h>

#include <l1ctl_proto.h>

#include "app.h"
#include "burst.h"
#include "demod.h"
#include "trx.h"


/* ------------------------------------------------------------------------ */
/* L1CTL exported API                                                       */
/* ------------------------------------------------------------------------ */

static struct msgb *
_l1ctl_alloc(uint8_t msg_type)
{
	struct l1ctl_hdr *l1h;
	struct msgb *msg = msgb_alloc_headroom(256, 4, "osmo_l1");

	if (!msg) {
		LOGP(DL1C, LOGL_ERROR, "Failed to allocate memory.\n");
		return NULL;
	}

	msg->l1h = msgb_put(msg, sizeof(*l1h));
	l1h = (struct l1ctl_hdr *) msg->l1h;
	l1h->msg_type = msg_type;

	return msg;
}

int
l1ctl_tx_reset_req(struct l1ctl_link *l1l, uint8_t type)
{
	struct msgb *msg;
	struct l1ctl_reset *res;

	msg = _l1ctl_alloc(L1CTL_RESET_REQ);
	if (!msg)
		return -1;

	LOGP(DL1C, LOGL_INFO, "Tx Reset Req (%u)\n", type);
	res = (struct l1ctl_reset *) msgb_put(msg, sizeof(*res));
	res->type = type;

	return l1l_send(l1l, msg);
}

int
l1ctl_tx_fbsb_req(struct l1ctl_link *l1l,
	uint16_t arfcn, uint8_t flags, uint16_t timeout,
	uint8_t sync_info_idx, uint8_t ccch_mode)
{
	struct msgb *msg;
	struct l1ctl_fbsb_req *req;

	if (l1l->sync)
		return -EIO;

	LOGP(DL1C, LOGL_INFO, "Sync Req\n");

	msg = _l1ctl_alloc(L1CTL_FBSB_REQ);
	if (!msg)
		return -1;

	req = (struct l1ctl_fbsb_req *) msgb_put(msg, sizeof(*req));
	req->band_arfcn = htons(arfcn);
	req->timeout = htons(timeout);
	/* Threshold when to consider FB_MODE1: 4kHz - 1kHz */
	req->freq_err_thresh1 = htons(11000 - 1000);
	/* Threshold when to consider SCH: 1kHz - 200Hz */
	req->freq_err_thresh2 = htons(1000 - 200);
	/* not used yet! */
	req->num_freqerr_avg = 3;
	req->flags = flags;
	req->sync_info_idx = sync_info_idx;
	req->ccch_mode = ccch_mode;

	return l1l_send(l1l, msg);
}

int
l1ctl_tx_bts_mode(struct l1ctl_link *l1l, uint8_t enabled, uint8_t *type,
	uint8_t bsic, uint16_t band_arfcn, int gain, uint8_t tx_mask,
	uint8_t rx_mask)
{
	struct msgb *msg;
	struct l1ctl_bts_mode *be;
	int i;

	if (!l1l->sync)
		return -EIO;

	msg = _l1ctl_alloc(L1CTL_BTS_MODE);
	if (!msg)
		return -1;

	LOGP(DL1C, LOGL_INFO, "BTS Mode (enabled=%u, bsic=%u, arfcn=%u "
		"gain=%d)\n", enabled, bsic, band_arfcn, gain);

	be = (struct l1ctl_bts_mode *) msgb_put(msg, sizeof(*be));
	be->enabled = enabled;
	for (i = 0; i < 8; i++)
		be->type[i] = type[i];
	be->bsic = bsic;
	be->band_arfcn = htons(band_arfcn);
	be->gain = gain;
	be->tx_mask = tx_mask;
	be->rx_mask = rx_mask;

	return l1l_send(l1l, msg);
}

int
l1ctl_tx_bts_burst_req(struct l1ctl_link *l1l,
                       uint32_t fn, uint8_t tn, struct burst_data *burst)
{
	struct msgb *msg;
	struct l1ctl_bts_burst_req *br;

	if (!l1l->sync)
		return -EIO;

	msg = _l1ctl_alloc(L1CTL_BTS_BURST_REQ);
	if (!msg)
		return -1;

	LOGP(DL1C, LOGL_INFO, "BTS Tx Burst (%d:%d)\n", fn, tn);

	br = (struct l1ctl_bts_burst_req *) msgb_put(msg, sizeof(*br));
	br->fn = htonl(fn);
	br->tn = tn;
	br->type = burst->type;

	if (burst->type == BURST_NB)
		memcpy(msgb_put(msg, 15), burst->data, 15);

	return l1l_send(l1l, msg);
}


/* ------------------------------------------------------------------------ */
/* L1CTL Receive handling                                                   */
/* ------------------------------------------------------------------------ */

static int
_l1ctl_rx_bts_burst_nb_ind(struct l1ctl_link *l1l, struct msgb *msg)
{
	struct l1ctl_bts_burst_nb_ind *bi;
	uint32_t fn;
	int rc, i;
	sbit_t data[148], steal[2];
	float toa;
	int8_t rssi;

	bi = (struct l1ctl_bts_burst_nb_ind *) msg->l1h;

	if (msgb_l1len(msg) < sizeof(*bi)) {
		LOGP(DL1C, LOGL_ERROR, "MSG too short Burst NB Ind: %u\n",
		     msgb_l2len(msg));
		rc = -EINVAL;
		goto exit;
	}

	fn = ntohl(bi->fn);

	LOGP(DL1C, LOGL_INFO, "Normal Burst Indication (fn=%d)\n", fn);

	toa = bi->toa * 1.0F;

	rssi = bi->rssi;

	memset(data, 0x00, 148);

	osmo_pbit2ubit_ext((ubit_t*)data,  3, bi->data,  0, 57, 0);
	osmo_pbit2ubit_ext((ubit_t*)data, 88, bi->data, 57, 57, 0);
	osmo_pbit2ubit_ext((ubit_t*)steal, 0, bi->data, 114, 2, 0);
	data[60] = steal[1];
	data[87] = steal[0];

	for (i=0; i<148; i++)
		data[i] = data[i] ? -127 : 127;

	trx_data_ind(l1l->trx, fn, bi->tn, data, toa, rssi);

exit:
	msgb_free(msg);

	return rc;
}

static int
_l1ctl_rx_bts_burst_ab_ind(struct l1ctl_link *l1l, struct msgb *msg)
{
	struct l1ctl_bts_burst_ab_ind *bi;
	uint32_t fn;
	int rc;
	sbit_t data[148];
	float toa;

	bi = (struct l1ctl_bts_burst_ab_ind *) msg->l1h;

	if (msgb_l1len(msg) < sizeof(*bi)) {
		LOGP(DL1C, LOGL_ERROR, "MSG too short Burst AB Ind: %u\n",
		     msgb_l2len(msg));
		rc = -EINVAL;
		goto exit;
	}

	fn = ntohl(bi->fn);

	rc = gsm_ab_ind_process(l1l->as, bi, data, &toa);
	if (rc < 0)
		goto exit;

	toa += bi->toa;

	LOGP(DL1C, LOGL_INFO, "Access Burst Indication (fn=%d iq toa=%f)\n", fn, toa);

	trx_data_ind(l1l->trx, fn, 0, data, toa, 0);
exit:
	msgb_free(msg);

	return rc;
}

static int
_l1ctl_rx_data_ind(struct l1ctl_link *l1l, struct msgb *msg)
{
	struct l1ctl_info_dl *dl;
	struct l1ctl_data_ind *di;
	int rc;

	dl = (struct l1ctl_info_dl *) msg->l1h;
	msg->l2h = dl->payload;
	di = (struct l1ctl_data_ind *) msg->l2h;

	if (msgb_l1len(msg) < sizeof(*dl)) {
		LOGP(DL1C, LOGL_ERROR, "Short Layer2 message: %u\n",
		     msgb_l2len(msg));
		rc = -EINVAL;
		goto exit;
	}

	if (msgb_l2len(msg) < sizeof(*di)) {
		LOGP(DL1C, LOGL_ERROR, "MSG too short Data Ind: %u\n",
		     msgb_l3len(msg));
		rc = -EINVAL;
		goto exit;
	}

#if 0
	if (dl->fire_crc > 2) {
		LOGP(DAPP, LOGL_INFO, "Invalid BCCH message, retry sync ...\n");
		rc = l1ctl_tx_fbsb_req(&as->l1l, as->arfcn_sync, L1CTL_FBSB_F_FB01SB, 100, 0, CCCH_MODE_NONE);
	} else {
		LOGP(DAPP, LOGL_INFO, "Valid BCCH message, sync ok -> switch to BTS mode\n");

		rc = 0;
	}
#endif

	/* forward clock of fist l1ctl_link instance only */
	if (l1l == &l1l->as->l1l[0])
		rc = trx_clk_ind(l1l->trx, ntohl(dl->frame_nr));

exit:
	msgb_free(msg);

	return rc;
}

static int
_l1ctl_rx_fbsb_conf(struct l1ctl_link *l1l, struct msgb *msg)
{
	struct l1ctl_info_dl *dl;
	struct l1ctl_fbsb_conf *sc;
	int rc;

	dl = (struct l1ctl_info_dl *) msg->l1h;
	msg->l2h = dl->payload;
	sc = (struct l1ctl_fbsb_conf *) msg->l2h;

	if (msgb_l1len(msg) < sizeof(*dl)) {
		LOGP(DL1C, LOGL_ERROR, "Short Layer2 message: %u\n",
		     msgb_l2len(msg));
		rc = -EINVAL;
		goto exit;
	}

	if (msgb_l2len(msg) < sizeof(*sc)) {
		LOGP(DL1C, LOGL_ERROR, "MSG too short FBSB Conf: %u\n",
		     msgb_l3len(msg));
		rc = -EINVAL;
		goto exit;
	}

	if (sc->result != 0) {
		LOGP(DAPP, LOGL_INFO, "Sync failed, retrying ... \n");
		rc = l1ctl_tx_fbsb_req(l1l, l1l->as->arfcn_sync, L1CTL_FBSB_F_FB01SB, 100, 0, CCCH_MODE_NONE);
	} else {
		LOGP(DAPP, LOGL_INFO, "Sync acquired, setting BTS mode ...\n");
		l1l->sync = 1;
		l1ctl_tx_bts_mode(l1l, l1l->trx->power, l1l->trx->type, l1l->trx->bsic, l1l->trx->arfcn, l1l->trx->gain, l1l->tx_mask, l1l->rx_mask);
	}

	rc = 0;

exit:
	msgb_free(msg);

	return rc;
}

int
l1ctl_recv(void *data, struct msgb *msg)
{
	struct l1ctl_link *l1l = data;
	struct l1ctl_hdr *l1h;
	int rc = 0;

	/* move the l1 header pointer to point _BEHIND_ l1ctl_hdr,
	   as the l1ctl header is of no interest to subsequent code */
	l1h = (struct l1ctl_hdr *) msg->l1h;
	msg->l1h = l1h->data;

	/* Act */
	switch (l1h->msg_type) {
	case L1CTL_BTS_BURST_AB_IND:
		rc = _l1ctl_rx_bts_burst_ab_ind(l1l, msg);
		break;

	case L1CTL_BTS_BURST_NB_IND:
		rc = _l1ctl_rx_bts_burst_nb_ind(l1l, msg);
		break;

	case L1CTL_DATA_IND:
		rc = _l1ctl_rx_data_ind(l1l, msg);
		break;

	case L1CTL_FBSB_CONF:
		rc = _l1ctl_rx_fbsb_conf(l1l, msg);
		break;

	case L1CTL_RESET_IND:
	case L1CTL_RESET_CONF:
		LOGP(DAPP, LOGL_INFO, "Reset received: Starting sync.\n");
		l1ctl_tx_fbsb_req(l1l, l1l->as->arfcn_sync, L1CTL_FBSB_F_FB01SB, 100, 0, CCCH_MODE_NONE);
		msgb_free(msg);
		break;

	default:
		LOGP(DL1C, LOGL_ERROR, "Unknown MSG: %u\n", l1h->msg_type);
		msgb_free(msg);
		break;
	}

	return rc;
}