blob: f95b2282ec8d160b92ad076413140483db9c1f4d [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;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040094 int rc;
95
96 if (!msg)
97 return -ENOMEM;
98
99 msg->l1h = msg->data;
100
101 l1fh->remote_sa_len = sizeof(l1fh->remote_sa);
102 rc = recvfrom(ofd->fd, msg->l1h, msgb_tailroom(msg), 0,
103 (struct sockaddr *) &l1fh->remote_sa, &l1fh->remote_sa_len);
104 if (rc < 0) {
105 perror("read from udp");
106 msgb_free(msg);
107 return rc;
108 } else if (rc == 0) {
109 perror("len=0 read from udp");
110 msgb_free(msg);
111 return rc;
112 }
113 msgb_put(msg, rc);
114
115 struct gsm_pcu_if *pcu_prim = (gsm_pcu_if *)(msg->l1h);
116 rc = pcu_rx(pcu_prim->msg_type, pcu_prim);
Ivan Kluchnikov5dc29a52013-02-04 12:57:00 +0400117 msgb_free(msg);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400118 return rc;
119}
120
121/* callback when we can write to the UDP socket */
122static int udp_write_cb(struct osmo_fd *ofd, struct msgb *msg)
123{
124 int rc;
125 struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data;
126
127 //LOGP(DPCU, LOGL_ERROR, "UDP: Writing %u bytes for MQ_L1_WRITE queue\n", msgb_length(msg));
128
129 rc = sendto(ofd->fd, msgb_data(msg), msgb_length(msg), 0,
130 (const struct sockaddr *)&l1fh->remote_sa, l1fh->remote_sa_len);
131 if (rc < 0) {
132 LOGP(DPCU, LOGL_ERROR, "error writing to L1 msg_queue: %s\n",
133 strerror(errno));
134 return rc;
135 } else if (rc < (int)msgb_length(msg)) {
136 LOGP(DPCU, LOGL_ERROR, "short write to L1 msg_queue: "
137 "%u < %u\n", rc, msgb_length(msg));
138 return -EIO;
139 }
140
141 return 0;
142}
143
144int pcu_l1if_open()
145{
146 struct femtol1_hdl *fl1h;
147 int rc;
148
Harald Welte21848272015-11-12 01:07:41 +0100149 LOGP(DL1IF, LOGL_INFO, "Opening OsmoPCU L1 interface to OpenBTS\n");
150
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400151 /* allocate new femtol1_handle */
Andreas Eversberge266bd42012-07-13 14:00:21 +0200152 fl1h = talloc_zero(tall_pcu_ctx, struct femtol1_hdl);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400153 INIT_LLIST_HEAD(&fl1h->wlc_list);
154
155 l1fh->fl1h = fl1h;
156 fl1h->priv = l1fh;
157
158 struct osmo_wqueue * queue = &((l1fh->fl1h)->write_q);
159 osmo_wqueue_init(queue, 10);
160 queue->bfd.when |= BSC_FD_READ;
161 queue->bfd.data = l1fh;
162 queue->bfd.priv_nr = 0;
163
164 /* Open UDP */
165 struct osmo_wqueue *wq = &l1fh->udp_wq;
166
167 osmo_wqueue_init(wq, 10);
168 wq->write_cb = udp_write_cb;
169 wq->read_cb = udp_read_cb;
170 wq->bfd.when |= BSC_FD_READ;
171 wq->bfd.data = l1fh;
172 wq->bfd.priv_nr = 0;
173 rc = osmo_sock_init_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM,
174 IPPROTO_UDP, NULL, PCU_L1_IF_PORT,
175 OSMO_SOCK_F_BIND);
176 if (rc < 0) {
177 perror("sock_init");
178 exit(1);
179 }
180
181 return 0;
182}
183
184void pcu_l1if_close(void)
185{
Daniel Willmann6d8884d2014-06-04 18:30:59 +0200186 gprs_bssgp_destroy();
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400187
188 /* FIXME: cleanup l1if */
189 talloc_free(l1fh->fl1h);
Daniel Willmann6d8884d2014-06-04 18:30:59 +0200190
191 exit(0);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400192}