blob: 5e6f16c65dec64288b1ae1f5c67e7bd213464e9c [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{
Holger Hans Peter Freytherd1cb41b2015-05-22 10:42:32 +080082 if (osmo_wqueue_enqueue(&l1fh->udp_wq, msg) != 0) {
83 LOGP(DPCU, LOGL_ERROR, "PCU write queue full. Dropping message.\n");
84 msgb_free(msg);
85 }
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040086 return 0;
87}
88
89/* data has arrived on the udp socket */
90static int udp_read_cb(struct osmo_fd *ofd)
91{
92 struct msgb *msg = msgb_alloc_headroom(2048, 128, "udp_rx");
93 struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data;
94 struct femtol1_hdl *fl1h = l1fh->fl1h;
95 int rc;
96
97 if (!msg)
98 return -ENOMEM;
99
100 msg->l1h = msg->data;
101
102 l1fh->remote_sa_len = sizeof(l1fh->remote_sa);
103 rc = recvfrom(ofd->fd, msg->l1h, msgb_tailroom(msg), 0,
104 (struct sockaddr *) &l1fh->remote_sa, &l1fh->remote_sa_len);
105 if (rc < 0) {
106 perror("read from udp");
107 msgb_free(msg);
108 return rc;
109 } else if (rc == 0) {
110 perror("len=0 read from udp");
111 msgb_free(msg);
112 return rc;
113 }
114 msgb_put(msg, rc);
115
116 struct gsm_pcu_if *pcu_prim = (gsm_pcu_if *)(msg->l1h);
117 rc = pcu_rx(pcu_prim->msg_type, pcu_prim);
Ivan Kluchnikov5dc29a52013-02-04 12:57:00 +0400118 msgb_free(msg);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400119 return rc;
120}
121
122/* callback when we can write to the UDP socket */
123static int udp_write_cb(struct osmo_fd *ofd, struct msgb *msg)
124{
125 int rc;
126 struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data;
127
128 //LOGP(DPCU, LOGL_ERROR, "UDP: Writing %u bytes for MQ_L1_WRITE queue\n", msgb_length(msg));
129
130 rc = sendto(ofd->fd, msgb_data(msg), msgb_length(msg), 0,
131 (const struct sockaddr *)&l1fh->remote_sa, l1fh->remote_sa_len);
132 if (rc < 0) {
133 LOGP(DPCU, LOGL_ERROR, "error writing to L1 msg_queue: %s\n",
134 strerror(errno));
135 return rc;
136 } else if (rc < (int)msgb_length(msg)) {
137 LOGP(DPCU, LOGL_ERROR, "short write to L1 msg_queue: "
138 "%u < %u\n", rc, msgb_length(msg));
139 return -EIO;
140 }
141
142 return 0;
143}
144
145int pcu_l1if_open()
146{
147 struct femtol1_hdl *fl1h;
148 int rc;
149
150 /* allocate new femtol1_handle */
Andreas Eversberge266bd42012-07-13 14:00:21 +0200151 fl1h = talloc_zero(tall_pcu_ctx, struct femtol1_hdl);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400152 INIT_LLIST_HEAD(&fl1h->wlc_list);
153
154 l1fh->fl1h = fl1h;
155 fl1h->priv = l1fh;
156
157 struct osmo_wqueue * queue = &((l1fh->fl1h)->write_q);
158 osmo_wqueue_init(queue, 10);
159 queue->bfd.when |= BSC_FD_READ;
160 queue->bfd.data = l1fh;
161 queue->bfd.priv_nr = 0;
162
163 /* Open UDP */
164 struct osmo_wqueue *wq = &l1fh->udp_wq;
165
166 osmo_wqueue_init(wq, 10);
167 wq->write_cb = udp_write_cb;
168 wq->read_cb = udp_read_cb;
169 wq->bfd.when |= BSC_FD_READ;
170 wq->bfd.data = l1fh;
171 wq->bfd.priv_nr = 0;
172 rc = osmo_sock_init_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM,
173 IPPROTO_UDP, NULL, PCU_L1_IF_PORT,
174 OSMO_SOCK_F_BIND);
175 if (rc < 0) {
176 perror("sock_init");
177 exit(1);
178 }
179
180 return 0;
181}
182
183void pcu_l1if_close(void)
184{
Daniel Willmann6d8884d2014-06-04 18:30:59 +0200185 gprs_bssgp_destroy();
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400186
187 /* FIXME: cleanup l1if */
188 talloc_free(l1fh->fl1h);
Daniel Willmann6d8884d2014-06-04 18:30:59 +0200189
190 exit(0);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400191}