blob: 0652a1c69cd697653e019bb0420b576a52e378c2 [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>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040023#include <pcu_l1_if.h>
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +040024#include <gprs_debug.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020025#include <bitvector.h>
26#include <gsmL1prim.h>
27#include <sys/socket.h>
28#include <linux/in.h>
29extern "C" {
30#include <osmocom/core/talloc.h>
31#include <osmocom/core/write_queue.h>
32#include <osmocom/core/socket.h>
33#include <osmocom/core/timer.h>
34#include <osmocom/gsm/gsm_utils.h>
35}
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040036
37#define MAX_UDP_LENGTH 1500
38
Andreas Eversberg0aed6542012-06-23 10:33:16 +020039#define msgb_l1prim(msg) ((GsmL1_Prim_t *)(msg)->l1h)
40
41struct femtol1_hdl {
42 struct gsm_time gsm_time;
43 uint32_t hLayer1; /* handle to the L1 instance in the DSP */
44 uint32_t dsp_trace_f;
45 uint16_t clk_cal;
46 struct llist_head wlc_list;
47
48 void *priv; /* user reference */
49
50 struct osmo_timer_list alive_timer;
51 unsigned int alive_prim_cnt;
52
53 struct osmo_fd read_ofd; /* osmo file descriptors */
54 struct osmo_wqueue write_q;
55
56 struct {
57 uint16_t arfcn;
58 uint8_t tn;
59 uint8_t tsc;
60 uint16_t ta;
61 } channel_info;
62
63};
64
65struct l1fwd_hdl {
66 struct sockaddr_storage remote_sa;
67 socklen_t remote_sa_len;
68
69 struct osmo_wqueue udp_wq;
70
71 struct femtol1_hdl *fl1h;
72};
73
74struct l1fwd_hdl *l1fh = talloc_zero(NULL, struct l1fwd_hdl);
75
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +040076// Variable for storage current FN.
77int frame_number;
78
79int get_current_fn()
80{
81 return frame_number;
82}
83
84void set_current_fn(int fn)
85{
86 frame_number = fn;
87}
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040088
89struct msgb *l1p_msgb_alloc(void)
90{
91 struct msgb *msg = msgb_alloc(sizeof(GsmL1_Prim_t), "l1_prim");
92
93 if (msg)
94 msg->l1h = msgb_put(msg, sizeof(GsmL1_Prim_t));
95
96 return msg;
97}
98
Andreas Eversberg0aed6542012-06-23 10:33:16 +020099// Send RLC/MAC block to OpenBTS.
100void pcu_l1if_tx_pdtch(msgb *msg, uint8_t trx, uint8_t ts, uint16_t arfcn,
101 uint32_t fn, uint8_t block_nr)
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400102{
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200103 struct msgb *nmsg = l1p_msgb_alloc();
104 GsmL1_Prim_t *prim = msgb_l1prim(nmsg);
105
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400106 prim->id = GsmL1_PrimId_PhDataReq;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200107 prim->u.phDataReq.sapi = GsmL1_Sapi_Pdtch;
108 memcpy(prim->u.phDataReq.msgUnitParam.u8Buffer, msg->data, msg->len);
109 prim->u.phDataReq.msgUnitParam.u8Size = msg->len;
110 osmo_wqueue_enqueue(&l1fh->udp_wq, nmsg);
111 msgb_free(msg);
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400112}
113
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200114void pcu_l1if_tx_agch(bitvec * block, int len)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400115{
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400116 struct msgb *msg = l1p_msgb_alloc();
117 GsmL1_Prim_t *prim = msgb_l1prim(msg);
118
119 prim->id = GsmL1_PrimId_PhDataReq;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200120 prim->u.phDataReq.sapi = GsmL1_Sapi_Agch;
Ivan Kluchnikov835f91e2012-04-30 18:00:36 +0400121 bitvec_pack(block, prim->u.phDataReq.msgUnitParam.u8Buffer);
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400122 prim->u.phDataReq.msgUnitParam.u8Size = len;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200123 osmo_wqueue_enqueue(&l1fh->udp_wq, msg);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400124}
125
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400126int pcu_l1if_rx_pdch(GsmL1_PhDataInd_t *data_ind)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400127{
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200128 gprs_rlcmac_rcv_block(data_ind->msgUnitParam.u8Buffer,
129 data_ind->msgUnitParam.u8Size, data_ind->u32Fn);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200130
131 return 0;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400132}
133
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400134static int handle_ph_connect_ind(struct femtol1_hdl *fl1, GsmL1_PhConnectInd_t *connect_ind)
135{
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200136 struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
137
138 bts->trx[0].arfcn = connect_ind->u16Arfcn;
139 bts->trx[0].pdch[connect_ind->u8Tn].enable = 1;
140 bts->trx[0].pdch[connect_ind->u8Tn].tsc = connect_ind->u8Tsc;
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400141 (l1fh->fl1h)->channel_info.arfcn = connect_ind->u16Arfcn;
142 (l1fh->fl1h)->channel_info.tn = connect_ind->u8Tn;
143 (l1fh->fl1h)->channel_info.tsc = connect_ind->u8Tsc;
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +0400144 LOGP(DL1IF, LOGL_NOTICE, "RX: [ PCU <- BTS ] PhConnectInd: ARFCN: %u TN: %u TSC: %u \n",
145 connect_ind->u16Arfcn, (unsigned)connect_ind->u8Tn, (unsigned)connect_ind->u8Tsc);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200146
147 return 0;
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400148}
149
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400150static int handle_ph_readytosend_ind(struct femtol1_hdl *fl1, GsmL1_PhReadyToSendInd_t *readytosend_ind)
151{
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200152 gprs_rlcmac_rcv_rts_block(0,0, (l1fh->fl1h)->channel_info.arfcn, readytosend_ind->u32Fn, 0);
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400153 return 1;
154}
155
156static int handle_ph_data_ind(struct femtol1_hdl *fl1, GsmL1_PhDataInd_t *data_ind)
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400157{
158 int rc = 0;
159 switch (data_ind->sapi) {
160 case GsmL1_Sapi_Rach:
161 break;
162 case GsmL1_Sapi_Pdtch:
163 case GsmL1_Sapi_Pacch:
164 pcu_l1if_rx_pdch(data_ind);
165 break;
166 case GsmL1_Sapi_Pbcch:
167 case GsmL1_Sapi_Pagch:
168 case GsmL1_Sapi_Ppch:
169 case GsmL1_Sapi_Pnch:
170 case GsmL1_Sapi_Ptcch:
171 case GsmL1_Sapi_Prach:
172 break;
173 default:
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +0400174 LOGP(DL1IF, LOGL_NOTICE, "Rx PH-DATA.ind for unknown L1 SAPI %u \n", data_ind->sapi);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400175 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400176 }
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400177
178 return rc;
179}
180
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400181static int handle_ph_ra_ind(struct femtol1_hdl *fl1, GsmL1_PhRaInd_t *ra_ind)
182{
183 int rc = 0;
184 (l1fh->fl1h)->channel_info.ta = ra_ind->measParam.i16BurstTiming;
185 rc = gprs_rlcmac_rcv_rach(ra_ind->msgUnitParam.u8Buffer[0], ra_ind->u32Fn, ra_ind->measParam.i16BurstTiming);
186 return rc;
187}
188
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400189/* handle any random indication from the L1 */
190int pcu_l1if_handle_l1prim(struct femtol1_hdl *fl1, struct msgb *msg)
191{
192 GsmL1_Prim_t *l1p = msgb_l1prim(msg);
193 int rc = 0;
194
195 switch (l1p->id) {
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400196 case GsmL1_PrimId_PhConnectInd:
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400197 rc = handle_ph_connect_ind(fl1, &l1p->u.phConnectInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400198 break;
199 case GsmL1_PrimId_PhReadyToSendInd:
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400200 rc = handle_ph_readytosend_ind(fl1, &l1p->u.phReadyToSendInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400201 break;
202 case GsmL1_PrimId_PhDataInd:
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400203 rc = handle_ph_data_ind(fl1, &l1p->u.phDataInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400204 break;
205 case GsmL1_PrimId_PhRaInd:
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400206 rc = handle_ph_ra_ind(fl1, &l1p->u.phRaInd);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400207 break;
208 default:
209 break;
210 }
211
212 /* Special return value '1' means: do not free */
213 if (rc != 1)
214 msgb_free(msg);
215
216 return rc;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400217}
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200218
Andreas Eversberge6228b32012-07-03 13:36:03 +0200219PCU will currently not work without getting a GSM time or BFI indidication.
220In order to fix this, i will discuss this on the mailing list.
221Andreas
222
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200223/* OpenBTS socket functions */
224
225// TODO: We should move this parameters to config file.
226#define PCU_L1_IF_PORT 5944
227
228/* data has arrived on the udp socket */
229static int udp_read_cb(struct osmo_fd *ofd)
230{
231 struct msgb *msg = msgb_alloc_headroom(2048, 128, "udp_rx");
232 struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data;
233 struct femtol1_hdl *fl1h = l1fh->fl1h;
234 int rc;
235
236 if (!msg)
237 return -ENOMEM;
238
239 msg->l1h = msg->data;
240
241 l1fh->remote_sa_len = sizeof(l1fh->remote_sa);
242 rc = recvfrom(ofd->fd, msg->l1h, msgb_tailroom(msg), 0,
243 (struct sockaddr *) &l1fh->remote_sa, &l1fh->remote_sa_len);
244 if (rc < 0) {
245 perror("read from udp");
246 msgb_free(msg);
247 return rc;
248 } else if (rc == 0) {
249 perror("len=0 read from udp");
250 msgb_free(msg);
251 return rc;
252 }
253 msgb_put(msg, rc);
254
255 rc = pcu_l1if_handle_l1prim(fl1h, msg);
256 return rc;
257}
258
259/* callback when we can write to the UDP socket */
260static int udp_write_cb(struct osmo_fd *ofd, struct msgb *msg)
261{
262 int rc;
263 struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data;
264
265 //DEBUGP(DGPRS, "UDP: Writing %u bytes for MQ_L1_WRITE queue\n", msgb_l1len(msg));
266
267 rc = sendto(ofd->fd, msg->l1h, msgb_l1len(msg), 0,
268 (const struct sockaddr *)&l1fh->remote_sa, l1fh->remote_sa_len);
269 if (rc < 0) {
270 LOGP(DPCU, LOGL_ERROR, "error writing to L1 msg_queue: %s\n",
271 strerror(errno));
272 return rc;
273 } else if (rc < (int)msgb_l1len(msg)) {
274 LOGP(DPCU, LOGL_ERROR, "short write to L1 msg_queue: "
275 "%u < %u\n", rc, msgb_l1len(msg));
276 return -EIO;
277 }
278
279 return 0;
280}
281
282int pcu_l1if_open()
283{
284 //struct l1fwd_hdl *l1fh;
285 struct femtol1_hdl *fl1h;
286 int rc;
287
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200288 /* allocate new femtol1_handle */
289 fl1h = talloc_zero(NULL, struct femtol1_hdl);
290 INIT_LLIST_HEAD(&fl1h->wlc_list);
291
292 l1fh->fl1h = fl1h;
293 fl1h->priv = l1fh;
294
295 struct osmo_wqueue * queue = &((l1fh->fl1h)->write_q);
296 osmo_wqueue_init(queue, 10);
297 queue->bfd.when |= BSC_FD_READ;
298 queue->bfd.data = l1fh;
299 queue->bfd.priv_nr = 0;
300
301 /* Open UDP */
302 struct osmo_wqueue *wq = &l1fh->udp_wq;
303
304 osmo_wqueue_init(wq, 10);
305 wq->write_cb = udp_write_cb;
306 wq->read_cb = udp_read_cb;
307 wq->bfd.when |= BSC_FD_READ;
308 wq->bfd.data = l1fh;
309 wq->bfd.priv_nr = 0;
310 rc = osmo_sock_init_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM,
311 IPPROTO_UDP, NULL, PCU_L1_IF_PORT,
312 OSMO_SOCK_F_BIND);
313 if (rc < 0) {
314 perror("sock_init");
315 exit(1);
316 }
317
318 return 0;
319}
320