blob: 21e454b8df029793bc71ec91ac225304864801f7 [file] [log] [blame]
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001/* pcu_l1_if.cpp
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
Andreas Eversberg0aed6542012-06-23 10:33:16 +020020#include <errno.h>
21#include <string.h>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040022#include <gprs_rlcmac.h>
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +020023#include <gprs_bssgp_pcu.h>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040024#include <pcu_l1_if.h>
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +040025#include <gprs_debug.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020026#include <bitvector.h>
27#include <gsmL1prim.h>
28#include <sys/socket.h>
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +020029#include <arpa/inet.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020030extern "C" {
31#include <osmocom/core/talloc.h>
32#include <osmocom/core/write_queue.h>
33#include <osmocom/core/socket.h>
34#include <osmocom/core/timer.h>
35#include <osmocom/gsm/gsm_utils.h>
36}
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040037
38#define MAX_UDP_LENGTH 1500
39
Andreas Eversberg0aed6542012-06-23 10:33:16 +020040#define msgb_l1prim(msg) ((GsmL1_Prim_t *)(msg)->l1h)
41
42struct femtol1_hdl {
43 struct gsm_time gsm_time;
44 uint32_t hLayer1; /* handle to the L1 instance in the DSP */
45 uint32_t dsp_trace_f;
46 uint16_t clk_cal;
47 struct llist_head wlc_list;
48
49 void *priv; /* user reference */
50
51 struct osmo_timer_list alive_timer;
52 unsigned int alive_prim_cnt;
53
54 struct osmo_fd read_ofd; /* osmo file descriptors */
55 struct osmo_wqueue write_q;
56
57 struct {
58 uint16_t arfcn;
59 uint8_t tn;
60 uint8_t tsc;
61 uint16_t ta;
62 } channel_info;
63
64};
65
66struct l1fwd_hdl {
67 struct sockaddr_storage remote_sa;
68 socklen_t remote_sa_len;
69
70 struct osmo_wqueue udp_wq;
71
72 struct femtol1_hdl *fl1h;
73};
74
75struct l1fwd_hdl *l1fh = talloc_zero(NULL, struct l1fwd_hdl);
76
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +040077// Variable for storage current FN.
78int frame_number;
79
80int get_current_fn()
81{
82 return frame_number;
83}
84
85void set_current_fn(int fn)
86{
87 frame_number = fn;
88}
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040089
90struct msgb *l1p_msgb_alloc(void)
91{
92 struct msgb *msg = msgb_alloc(sizeof(GsmL1_Prim_t), "l1_prim");
93
94 if (msg)
95 msg->l1h = msgb_put(msg, sizeof(GsmL1_Prim_t));
96
97 return msg;
98}
99
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200100// Send RLC/MAC block to OpenBTS.
101void pcu_l1if_tx_pdtch(msgb *msg, uint8_t trx, uint8_t ts, uint16_t arfcn,
102 uint32_t fn, uint8_t block_nr)
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400103{
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200104 struct msgb *nmsg = l1p_msgb_alloc();
105 GsmL1_Prim_t *prim = msgb_l1prim(nmsg);
106
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400107 prim->id = GsmL1_PrimId_PhDataReq;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200108 prim->u.phDataReq.sapi = GsmL1_Sapi_Pdtch;
109 memcpy(prim->u.phDataReq.msgUnitParam.u8Buffer, msg->data, msg->len);
110 prim->u.phDataReq.msgUnitParam.u8Size = msg->len;
111 osmo_wqueue_enqueue(&l1fh->udp_wq, nmsg);
112 msgb_free(msg);
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400113}
114
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200115void pcu_l1if_tx_agch(bitvec * block, int len)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400116{
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400117 struct msgb *msg = l1p_msgb_alloc();
118 GsmL1_Prim_t *prim = msgb_l1prim(msg);
119
120 prim->id = GsmL1_PrimId_PhDataReq;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200121 prim->u.phDataReq.sapi = GsmL1_Sapi_Agch;
Ivan Kluchnikov835f91e2012-04-30 18:00:36 +0400122 bitvec_pack(block, prim->u.phDataReq.msgUnitParam.u8Buffer);
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400123 prim->u.phDataReq.msgUnitParam.u8Size = len;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200124 osmo_wqueue_enqueue(&l1fh->udp_wq, msg);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400125}
126
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400127int pcu_l1if_rx_pdch(GsmL1_PhDataInd_t *data_ind)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400128{
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200129 gprs_rlcmac_rcv_block(data_ind->msgUnitParam.u8Buffer,
130 data_ind->msgUnitParam.u8Size, data_ind->u32Fn);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200131
132 return 0;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400133}
134
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400135static int handle_ph_connect_ind(struct femtol1_hdl *fl1, GsmL1_PhConnectInd_t *connect_ind)
136{
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200137 struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
138
139 bts->trx[0].arfcn = connect_ind->u16Arfcn;
140 bts->trx[0].pdch[connect_ind->u8Tn].enable = 1;
141 bts->trx[0].pdch[connect_ind->u8Tn].tsc = connect_ind->u8Tsc;
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400142 (l1fh->fl1h)->channel_info.arfcn = connect_ind->u16Arfcn;
143 (l1fh->fl1h)->channel_info.tn = connect_ind->u8Tn;
144 (l1fh->fl1h)->channel_info.tsc = connect_ind->u8Tsc;
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +0400145 LOGP(DL1IF, LOGL_NOTICE, "RX: [ PCU <- BTS ] PhConnectInd: ARFCN: %u TN: %u TSC: %u \n",
146 connect_ind->u16Arfcn, (unsigned)connect_ind->u8Tn, (unsigned)connect_ind->u8Tsc);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200147
148 return 0;
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400149}
150
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400151static int handle_ph_readytosend_ind(struct femtol1_hdl *fl1, GsmL1_PhReadyToSendInd_t *readytosend_ind)
152{
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200153 gprs_rlcmac_rcv_rts_block(0,0, (l1fh->fl1h)->channel_info.arfcn, readytosend_ind->u32Fn, 0);
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400154 return 1;
155}
156
157static int handle_ph_data_ind(struct femtol1_hdl *fl1, GsmL1_PhDataInd_t *data_ind)
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400158{
159 int rc = 0;
160 switch (data_ind->sapi) {
161 case GsmL1_Sapi_Rach:
162 break;
163 case GsmL1_Sapi_Pdtch:
164 case GsmL1_Sapi_Pacch:
165 pcu_l1if_rx_pdch(data_ind);
166 break;
167 case GsmL1_Sapi_Pbcch:
168 case GsmL1_Sapi_Pagch:
169 case GsmL1_Sapi_Ppch:
170 case GsmL1_Sapi_Pnch:
171 case GsmL1_Sapi_Ptcch:
172 case GsmL1_Sapi_Prach:
173 break;
174 default:
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +0400175 LOGP(DL1IF, LOGL_NOTICE, "Rx PH-DATA.ind for unknown L1 SAPI %u \n", data_ind->sapi);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400176 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400177 }
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400178
179 return rc;
180}
181
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400182static int handle_ph_ra_ind(struct femtol1_hdl *fl1, GsmL1_PhRaInd_t *ra_ind)
183{
184 int rc = 0;
185 (l1fh->fl1h)->channel_info.ta = ra_ind->measParam.i16BurstTiming;
186 rc = gprs_rlcmac_rcv_rach(ra_ind->msgUnitParam.u8Buffer[0], ra_ind->u32Fn, ra_ind->measParam.i16BurstTiming);
187 return rc;
188}
189
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400190/* handle any random indication from the L1 */
191int pcu_l1if_handle_l1prim(struct femtol1_hdl *fl1, struct msgb *msg)
192{
193 GsmL1_Prim_t *l1p = msgb_l1prim(msg);
194 int rc = 0;
195
196 switch (l1p->id) {
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400197 case GsmL1_PrimId_PhConnectInd:
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400198 rc = handle_ph_connect_ind(fl1, &l1p->u.phConnectInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400199 break;
200 case GsmL1_PrimId_PhReadyToSendInd:
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400201 rc = handle_ph_readytosend_ind(fl1, &l1p->u.phReadyToSendInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400202 break;
203 case GsmL1_PrimId_PhDataInd:
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400204 rc = handle_ph_data_ind(fl1, &l1p->u.phDataInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400205 break;
206 case GsmL1_PrimId_PhRaInd:
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400207 rc = handle_ph_ra_ind(fl1, &l1p->u.phRaInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400208 break;
209 default:
210 break;
211 }
212
213 /* Special return value '1' means: do not free */
214 if (rc != 1)
215 msgb_free(msg);
216
217 return rc;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400218}
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200219
Andreas Eversberge6228b32012-07-03 13:36:03 +0200220PCU will currently not work without getting a GSM time or BFI indidication.
221In order to fix this, i will discuss this on the mailing list.
222Andreas
223
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200224/* OpenBTS socket functions */
225
226// TODO: We should move this parameters to config file.
227#define PCU_L1_IF_PORT 5944
228
229/* data has arrived on the udp socket */
230static int udp_read_cb(struct osmo_fd *ofd)
231{
232 struct msgb *msg = msgb_alloc_headroom(2048, 128, "udp_rx");
233 struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data;
234 struct femtol1_hdl *fl1h = l1fh->fl1h;
235 int rc;
236
237 if (!msg)
238 return -ENOMEM;
239
240 msg->l1h = msg->data;
241
242 l1fh->remote_sa_len = sizeof(l1fh->remote_sa);
243 rc = recvfrom(ofd->fd, msg->l1h, msgb_tailroom(msg), 0,
244 (struct sockaddr *) &l1fh->remote_sa, &l1fh->remote_sa_len);
245 if (rc < 0) {
246 perror("read from udp");
247 msgb_free(msg);
248 return rc;
249 } else if (rc == 0) {
250 perror("len=0 read from udp");
251 msgb_free(msg);
252 return rc;
253 }
254 msgb_put(msg, rc);
255
256 rc = pcu_l1if_handle_l1prim(fl1h, msg);
257 return rc;
258}
259
260/* callback when we can write to the UDP socket */
261static int udp_write_cb(struct osmo_fd *ofd, struct msgb *msg)
262{
263 int rc;
264 struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data;
265
266 //DEBUGP(DGPRS, "UDP: Writing %u bytes for MQ_L1_WRITE queue\n", msgb_l1len(msg));
267
268 rc = sendto(ofd->fd, msg->l1h, msgb_l1len(msg), 0,
269 (const struct sockaddr *)&l1fh->remote_sa, l1fh->remote_sa_len);
270 if (rc < 0) {
271 LOGP(DPCU, LOGL_ERROR, "error writing to L1 msg_queue: %s\n",
272 strerror(errno));
273 return rc;
274 } else if (rc < (int)msgb_l1len(msg)) {
275 LOGP(DPCU, LOGL_ERROR, "short write to L1 msg_queue: "
276 "%u < %u\n", rc, msgb_l1len(msg));
277 return -EIO;
278 }
279
280 return 0;
281}
282
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200283// TODO: We should move this parameters to config file.
284#define SGSN_IP 127.0.0.1
285#define SGSN_PORT 23000
286#define NSEI 3
287#define NSVCI 4
288
289#define BVCI 7
290
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200291#define CELL_ID 0
292#define MNC 2
293#define MCC 262
294#define PCU_LAC 1
295#define PCU_RAC 0
296
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200297int pcu_l1if_open()
298{
299 //struct l1fwd_hdl *l1fh;
300 struct femtol1_hdl *fl1h;
301 int rc;
302
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200303 struct sockaddr_in dest;
304 dest.sin_family = AF_INET;
305 dest.sin_port = htons(SGSN_PORT);
306 inet_aton(SGSN_IP, &dest.sin_addr);
307
308 rc = gprs_bssgp_create(ntohl(dest.sin_addr.s_addr), SGSN_PORT, NSEI, NSVCI, BVCI, MCC, MNC, PCU_LAC, PCU_RAC, CELL_ID);
309 if (rc < 0)
310 return rc;
311
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200312 /* allocate new femtol1_handle */
313 fl1h = talloc_zero(NULL, struct femtol1_hdl);
314 INIT_LLIST_HEAD(&fl1h->wlc_list);
315
316 l1fh->fl1h = fl1h;
317 fl1h->priv = l1fh;
318
319 struct osmo_wqueue * queue = &((l1fh->fl1h)->write_q);
320 osmo_wqueue_init(queue, 10);
321 queue->bfd.when |= BSC_FD_READ;
322 queue->bfd.data = l1fh;
323 queue->bfd.priv_nr = 0;
324
325 /* Open UDP */
326 struct osmo_wqueue *wq = &l1fh->udp_wq;
327
328 osmo_wqueue_init(wq, 10);
329 wq->write_cb = udp_write_cb;
330 wq->read_cb = udp_read_cb;
331 wq->bfd.when |= BSC_FD_READ;
332 wq->bfd.data = l1fh;
333 wq->bfd.priv_nr = 0;
334 rc = osmo_sock_init_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM,
335 IPPROTO_UDP, NULL, PCU_L1_IF_PORT,
336 OSMO_SOCK_F_BIND);
337 if (rc < 0) {
338 perror("sock_init");
339 exit(1);
340 }
341
342 return 0;
343}
344
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200345void pcu_l1if_close(void)
346{
347 gprs_bssgp_destroy();
348
349 /* FIXME: cleanup l1if */
350 talloc_free(fl1h);
351}