aboutsummaryrefslogtreecommitdiffstats
path: root/src/pcu_l1_if.cpp
blob: 4186e292476abab3f29ded4b7a307704ffb558c7 (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
/* pcu_l1_if.cpp
 *
 * Copyright (C) 2012 Ivan Klyuchnikov
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <errno.h>
#include <string.h>
#include <gprs_rlcmac.h>
#include <pcu_l1_if.h>
#include <gprs_debug.h>
#include <bitvector.h>
#include <gsmL1prim.h>
#include <sys/socket.h>
#include <linux/in.h>
extern "C" {
#include <osmocom/core/talloc.h>
#include <osmocom/core/write_queue.h>
#include <osmocom/core/socket.h>
#include <osmocom/core/timer.h>
#include <osmocom/gsm/gsm_utils.h>
}

#define MAX_UDP_LENGTH 1500

#define msgb_l1prim(msg)	((GsmL1_Prim_t *)(msg)->l1h)

struct femtol1_hdl {
	struct gsm_time gsm_time;
	uint32_t hLayer1;			/* handle to the L1 instance in the DSP */
	uint32_t dsp_trace_f;
	uint16_t clk_cal;
	struct llist_head wlc_list;

	void *priv;			/* user reference */

	struct osmo_timer_list alive_timer;
	unsigned int alive_prim_cnt;

	struct osmo_fd read_ofd;	/* osmo file descriptors */
	struct osmo_wqueue write_q;

	struct {
		uint16_t arfcn;
		uint8_t tn;
		uint8_t tsc;
		uint16_t ta;
	} channel_info;

};

struct l1fwd_hdl {
	struct sockaddr_storage remote_sa;
	socklen_t remote_sa_len;

	struct osmo_wqueue udp_wq;

	struct femtol1_hdl *fl1h;
};

struct l1fwd_hdl *l1fh = talloc_zero(NULL, struct l1fwd_hdl);

struct pcu_l1if_bts pcu_l1if_bts;

// Variable for storage current FN.
int frame_number;

int get_current_fn()
{
	return frame_number;
}

void set_current_fn(int fn)
{
	frame_number = fn;
}

struct msgb *l1p_msgb_alloc(void)
{
	struct msgb *msg = msgb_alloc(sizeof(GsmL1_Prim_t), "l1_prim");

	if (msg)
		msg->l1h = msgb_put(msg, sizeof(GsmL1_Prim_t));

	return msg;
}

// Send RLC/MAC block to OpenBTS.
void pcu_l1if_tx_pdtch(msgb *msg, uint8_t trx, uint8_t ts, uint16_t arfcn,
        uint32_t fn, uint8_t block_nr)
{
	struct msgb *nmsg = l1p_msgb_alloc();
	GsmL1_Prim_t *prim = msgb_l1prim(nmsg);
	
	prim->id = GsmL1_PrimId_PhDataReq;
	prim->u.phDataReq.sapi = GsmL1_Sapi_Pdtch;
	memcpy(prim->u.phDataReq.msgUnitParam.u8Buffer, msg->data, msg->len);
	prim->u.phDataReq.msgUnitParam.u8Size = msg->len;
	osmo_wqueue_enqueue(&l1fh->udp_wq, nmsg);
	msgb_free(msg);
}

void pcu_l1if_tx_agch(bitvec * block, int len)
{
	struct msgb *msg = l1p_msgb_alloc();
	GsmL1_Prim_t *prim = msgb_l1prim(msg);
	
	prim->id = GsmL1_PrimId_PhDataReq;
	prim->u.phDataReq.sapi = GsmL1_Sapi_Agch;
	bitvec_pack(block, prim->u.phDataReq.msgUnitParam.u8Buffer);
	prim->u.phDataReq.msgUnitParam.u8Size = len;
	osmo_wqueue_enqueue(&l1fh->udp_wq, msg);
}

void pcu_l1if_tx_pch(bitvec * block, int len)
{
	struct msgb *msg = l1p_msgb_alloc();
	GsmL1_Prim_t *prim = msgb_l1prim(msg);
	prim->id = GsmL1_PrimId_PhDataReq;
	prim->u.phDataReq.sapi = GsmL1_Sapi_Pch;
	bitvec_pack(block, prim->u.phDataReq.msgUnitParam.u8Buffer);
	prim->u.phDataReq.msgUnitParam.u8Size = len;
	osmo_wqueue_enqueue(&l1fh->udp_wq, msg);
}

int pcu_l1if_rx_pdch(GsmL1_PhDataInd_t *data_ind)
{
	bitvec *block = bitvec_alloc(data_ind->msgUnitParam.u8Size);
	bitvec_unpack(block, data_ind->msgUnitParam.u8Buffer);
	gprs_rlcmac_rcv_block(block);
	bitvec_free(block);

	return 0;
}

static int handle_ph_connect_ind(struct femtol1_hdl *fl1, GsmL1_PhConnectInd_t *connect_ind)
{
	pcu_l1if_bts.trx[0].arfcn = connect_ind->u16Arfcn;
	pcu_l1if_bts.trx[0].ts[connect_ind->u8Tn].enable = 1;
	pcu_l1if_bts.trx[0].ts[connect_ind->u8Tn].tsc = connect_ind->u8Tsc;
	(l1fh->fl1h)->channel_info.arfcn = connect_ind->u16Arfcn;
	(l1fh->fl1h)->channel_info.tn = connect_ind->u8Tn;
	(l1fh->fl1h)->channel_info.tsc = connect_ind->u8Tsc;
	LOGP(DL1IF, LOGL_NOTICE, "RX: [ PCU <- BTS ] PhConnectInd: ARFCN: %u TN: %u TSC: %u \n",
	        connect_ind->u16Arfcn, (unsigned)connect_ind->u8Tn, (unsigned)connect_ind->u8Tsc);

	return 0;
}

static int handle_ph_readytosend_ind(struct femtol1_hdl *fl1, GsmL1_PhReadyToSendInd_t *readytosend_ind)
{
	gprs_rlcmac_rcv_rts_block(0,0, (l1fh->fl1h)->channel_info.arfcn, readytosend_ind->u32Fn, 0);
	return 1;
}

static int handle_ph_data_ind(struct femtol1_hdl *fl1, GsmL1_PhDataInd_t *data_ind)
{
	int rc = 0;
	switch (data_ind->sapi) {
	case GsmL1_Sapi_Rach:
		break;
	case GsmL1_Sapi_Pdtch:
	case GsmL1_Sapi_Pacch:
		pcu_l1if_rx_pdch(data_ind);
		break;
	case GsmL1_Sapi_Pbcch:
	case GsmL1_Sapi_Pagch:
	case GsmL1_Sapi_Ppch:
	case GsmL1_Sapi_Pnch:
	case GsmL1_Sapi_Ptcch:
	case GsmL1_Sapi_Prach:
		break;
	default:
		LOGP(DL1IF, LOGL_NOTICE, "Rx PH-DATA.ind for unknown L1 SAPI %u \n", data_ind->sapi);
		break;
	}

	return rc;
}

static int handle_ph_ra_ind(struct femtol1_hdl *fl1, GsmL1_PhRaInd_t *ra_ind)
{
	int rc = 0;
	(l1fh->fl1h)->channel_info.ta = ra_ind->measParam.i16BurstTiming;
	rc = gprs_rlcmac_rcv_rach(ra_ind->msgUnitParam.u8Buffer[0], ra_ind->u32Fn, ra_ind->measParam.i16BurstTiming);
	return rc;
}

/* handle any random indication from the L1 */
int pcu_l1if_handle_l1prim(struct femtol1_hdl *fl1, struct msgb *msg)
{
	GsmL1_Prim_t *l1p = msgb_l1prim(msg);
	int rc = 0;

	switch (l1p->id) {
	case GsmL1_PrimId_PhConnectInd:
		rc = handle_ph_connect_ind(fl1, &l1p->u.phConnectInd);
		break;
	case GsmL1_PrimId_PhReadyToSendInd:
		rc = handle_ph_readytosend_ind(fl1, &l1p->u.phReadyToSendInd);
		break;
	case GsmL1_PrimId_PhDataInd:
		rc = handle_ph_data_ind(fl1, &l1p->u.phDataInd);
		break;
	case GsmL1_PrimId_PhRaInd:
		rc = handle_ph_ra_ind(fl1, &l1p->u.phRaInd);
		break;
	default:
		break;
	}

	/* Special return value '1' means: do not free */
	if (rc != 1)
		msgb_free(msg);

	return rc;
}

/* OpenBTS socket functions */

// TODO: We should move this parameters to config file.
#define PCU_L1_IF_PORT 5944

/* data has arrived on the udp socket */
static int udp_read_cb(struct osmo_fd *ofd)
{
	struct msgb *msg = msgb_alloc_headroom(2048, 128, "udp_rx");
	struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data;
	struct femtol1_hdl *fl1h = l1fh->fl1h;
	int rc;

	if (!msg)
		return -ENOMEM;

	msg->l1h = msg->data;

	l1fh->remote_sa_len = sizeof(l1fh->remote_sa);
	rc = recvfrom(ofd->fd, msg->l1h, msgb_tailroom(msg), 0,
			(struct sockaddr *) &l1fh->remote_sa, &l1fh->remote_sa_len);
	if (rc < 0) {
		perror("read from udp");
		msgb_free(msg);
		return rc;
	} else if (rc == 0) {
		perror("len=0 read from udp");
		msgb_free(msg);
		return rc;
	}
	msgb_put(msg, rc);

	rc = pcu_l1if_handle_l1prim(fl1h, msg);
	return rc;
}

/* callback when we can write to the UDP socket */
static int udp_write_cb(struct osmo_fd *ofd, struct msgb *msg)
{
	int rc;
	struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data;

	//DEBUGP(DGPRS, "UDP: Writing %u bytes for MQ_L1_WRITE queue\n", msgb_l1len(msg));

	rc = sendto(ofd->fd, msg->l1h, msgb_l1len(msg), 0,
			(const struct sockaddr *)&l1fh->remote_sa, l1fh->remote_sa_len);
	if (rc < 0) {
		LOGP(DPCU, LOGL_ERROR, "error writing to L1 msg_queue: %s\n",
			strerror(errno));
		return rc;
	} else if (rc < (int)msgb_l1len(msg)) {
		LOGP(DPCU, LOGL_ERROR, "short write to L1 msg_queue: "
			"%u < %u\n", rc, msgb_l1len(msg));
		return -EIO;
	}

	return 0;
}

int pcu_l1if_open()
{
	//struct l1fwd_hdl *l1fh;
	struct femtol1_hdl *fl1h;
	int rc;

	memset(&pcu_l1if_bts, 0, sizeof(pcu_l1if_bts));

	/* allocate new femtol1_handle */
	fl1h = talloc_zero(NULL, struct femtol1_hdl);
	INIT_LLIST_HEAD(&fl1h->wlc_list);

	l1fh->fl1h = fl1h;
	fl1h->priv = l1fh;

	struct osmo_wqueue * queue = &((l1fh->fl1h)->write_q);
	osmo_wqueue_init(queue, 10);
	queue->bfd.when |= BSC_FD_READ;
	queue->bfd.data = l1fh;
	queue->bfd.priv_nr = 0;

	/* Open UDP */
	struct osmo_wqueue *wq = &l1fh->udp_wq;

	osmo_wqueue_init(wq, 10);
	wq->write_cb = udp_write_cb;
	wq->read_cb = udp_read_cb;
	wq->bfd.when |= BSC_FD_READ;
	wq->bfd.data = l1fh;
	wq->bfd.priv_nr = 0;
	rc = osmo_sock_init_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM,
				IPPROTO_UDP, NULL, PCU_L1_IF_PORT,
				OSMO_SOCK_F_BIND);
	if (rc < 0) {
		perror("sock_init");
		exit(1);
	}

	return 0;
}