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