blob: 2d9cae41a1ce64317703c693483f9b4c9da98ee8 [file] [log] [blame]
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04001/* openbts_sock.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
20#include <errno.h>
21#include <string.h>
22#include <gprs_rlcmac.h>
23#include <gprs_bssgp_pcu.h>
24#include <pcu_l1_if.h>
25#include <gprs_debug.h>
26#include <bitvector.h>
27#include <sys/socket.h>
28#include <arpa/inet.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#include <pcuif_proto.h>
36}
37
Andreas Eversberge266bd42012-07-13 14:00:21 +020038extern void *tall_pcu_ctx;
39
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040040struct femtol1_hdl {
41 struct gsm_time gsm_time;
42 uint32_t hLayer1; /* handle to the L1 instance in the DSP */
43 uint32_t dsp_trace_f;
44 uint16_t clk_cal;
45 struct llist_head wlc_list;
46
47 void *priv; /* user reference */
48
49 struct osmo_timer_list alive_timer;
50 unsigned int alive_prim_cnt;
51
52 struct osmo_fd read_ofd; /* osmo file descriptors */
53 struct osmo_wqueue write_q;
54
55 struct {
56 uint16_t arfcn;
57 uint8_t tn;
58 uint8_t tsc;
59 uint16_t ta;
60 } channel_info;
61
62};
63
64struct l1fwd_hdl {
65 struct sockaddr_storage remote_sa;
66 socklen_t remote_sa_len;
67
68 struct osmo_wqueue udp_wq;
69
70 struct femtol1_hdl *fl1h;
71};
72
73struct l1fwd_hdl *l1fh = talloc_zero(NULL, struct l1fwd_hdl);
74
75// TODO: We should move this parameters to config file.
76#define PCU_L1_IF_PORT 5944
77
78/* OpenBTS socket functions */
79
80int pcu_sock_send(struct msgb *msg)
81{
82 osmo_wqueue_enqueue(&l1fh->udp_wq, msg);
83 return 0;
84}
85
86/* data has arrived on the udp socket */
87static int udp_read_cb(struct osmo_fd *ofd)
88{
89 struct msgb *msg = msgb_alloc_headroom(2048, 128, "udp_rx");
90 struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data;
91 struct femtol1_hdl *fl1h = l1fh->fl1h;
92 int rc;
93
94 if (!msg)
95 return -ENOMEM;
96
97 msg->l1h = msg->data;
98
99 l1fh->remote_sa_len = sizeof(l1fh->remote_sa);
100 rc = recvfrom(ofd->fd, msg->l1h, msgb_tailroom(msg), 0,
101 (struct sockaddr *) &l1fh->remote_sa, &l1fh->remote_sa_len);
102 if (rc < 0) {
103 perror("read from udp");
104 msgb_free(msg);
105 return rc;
106 } else if (rc == 0) {
107 perror("len=0 read from udp");
108 msgb_free(msg);
109 return rc;
110 }
111 msgb_put(msg, rc);
112
113 struct gsm_pcu_if *pcu_prim = (gsm_pcu_if *)(msg->l1h);
114 rc = pcu_rx(pcu_prim->msg_type, pcu_prim);
Ivan Kluchnikov5dc29a52013-02-04 12:57:00 +0400115 msgb_free(msg);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400116 return rc;
117}
118
119/* callback when we can write to the UDP socket */
120static int udp_write_cb(struct osmo_fd *ofd, struct msgb *msg)
121{
122 int rc;
123 struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data;
124
125 //LOGP(DPCU, LOGL_ERROR, "UDP: Writing %u bytes for MQ_L1_WRITE queue\n", msgb_length(msg));
126
127 rc = sendto(ofd->fd, msgb_data(msg), msgb_length(msg), 0,
128 (const struct sockaddr *)&l1fh->remote_sa, l1fh->remote_sa_len);
129 if (rc < 0) {
130 LOGP(DPCU, LOGL_ERROR, "error writing to L1 msg_queue: %s\n",
131 strerror(errno));
132 return rc;
133 } else if (rc < (int)msgb_length(msg)) {
134 LOGP(DPCU, LOGL_ERROR, "short write to L1 msg_queue: "
135 "%u < %u\n", rc, msgb_length(msg));
136 return -EIO;
137 }
138
139 return 0;
140}
141
142int pcu_l1if_open()
143{
144 struct femtol1_hdl *fl1h;
145 int rc;
146
147 /* allocate new femtol1_handle */
Andreas Eversberge266bd42012-07-13 14:00:21 +0200148 fl1h = talloc_zero(tall_pcu_ctx, struct femtol1_hdl);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400149 INIT_LLIST_HEAD(&fl1h->wlc_list);
150
151 l1fh->fl1h = fl1h;
152 fl1h->priv = l1fh;
153
154 struct osmo_wqueue * queue = &((l1fh->fl1h)->write_q);
155 osmo_wqueue_init(queue, 10);
156 queue->bfd.when |= BSC_FD_READ;
157 queue->bfd.data = l1fh;
158 queue->bfd.priv_nr = 0;
159
160 /* Open UDP */
161 struct osmo_wqueue *wq = &l1fh->udp_wq;
162
163 osmo_wqueue_init(wq, 10);
164 wq->write_cb = udp_write_cb;
165 wq->read_cb = udp_read_cb;
166 wq->bfd.when |= BSC_FD_READ;
167 wq->bfd.data = l1fh;
168 wq->bfd.priv_nr = 0;
169 rc = osmo_sock_init_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM,
170 IPPROTO_UDP, NULL, PCU_L1_IF_PORT,
171 OSMO_SOCK_F_BIND);
172 if (rc < 0) {
173 perror("sock_init");
174 exit(1);
175 }
176
177 return 0;
178}
179
180void pcu_l1if_close(void)
181{
Daniel Willmann6d8884d2014-06-04 18:30:59 +0200182 gprs_bssgp_destroy();
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400183
184 /* FIXME: cleanup l1if */
185 talloc_free(l1fh->fl1h);
Daniel Willmann6d8884d2014-06-04 18:30:59 +0200186
187 exit(0);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400188}