blob: f26c6d86ff2e16440452c80ea0e473ae0819fe83 [file] [log] [blame]
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001/* pcu_main.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 <gprs_bssgp_pcu.h>
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +040021#include <arpa/inet.h>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040022#include <pcu_l1_if.h>
Ivan Kluchnikov61a33f72012-04-12 15:22:06 +040023#include <gsm_timer.h>
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +040024#include <gprs_debug.h>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040025
26// TODO: We should move this parameters to config file.
27#define SGSN_IP "127.0.0.1"
28#define SGSN_PORT 23000
29#define NSVCI 4
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040030#define PCU_L1_IF_PORT 5944
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040031
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040032struct l1fwd_hdl *l1fh = talloc_zero(NULL, struct l1fwd_hdl);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040033
34int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc, struct msgb *msg, uint16_t bvci)
35{
36 int rc = 0;
37 switch (event) {
38 case GPRS_NS_EVT_UNIT_DATA:
39 /* hand the message into the BSSGP implementation */
40 rc = gprs_bssgp_pcu_rcvmsg(msg);
41 break;
42 default:
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +040043 LOGP(DPCU, LOGL_ERROR, "RLCMAC: Unknown event %u from NS\n", event);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040044 if (msg)
45 talloc_free(msg);
46 rc = -EIO;
47 break;
48 }
49 return rc;
50}
51
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040052/* data has arrived on the udp socket */
53static int udp_read_cb(struct osmo_fd *ofd)
54{
55 struct msgb *msg = msgb_alloc_headroom(2048, 128, "udp_rx");
56 struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data;
57 struct femtol1_hdl *fl1h = l1fh->fl1h;
58 int rc;
59
60 if (!msg)
61 return -ENOMEM;
62
63 msg->l1h = msg->data;
64
65 l1fh->remote_sa_len = sizeof(l1fh->remote_sa);
66 rc = recvfrom(ofd->fd, msg->l1h, msgb_tailroom(msg), 0,
67 (struct sockaddr *) &l1fh->remote_sa, &l1fh->remote_sa_len);
68 if (rc < 0) {
69 perror("read from udp");
70 msgb_free(msg);
71 return rc;
72 } else if (rc == 0) {
73 perror("len=0 read from udp");
74 msgb_free(msg);
75 return rc;
76 }
77 msgb_put(msg, rc);
78
79 rc = pcu_l1if_handle_l1prim(fl1h, msg);
80 return rc;
81}
82
83/* callback when we can write to the UDP socket */
84static int udp_write_cb(struct osmo_fd *ofd, struct msgb *msg)
85{
86 int rc;
87 struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data;
88
Ivan Kluchnikov61a33f72012-04-12 15:22:06 +040089 //DEBUGP(DGPRS, "UDP: Writing %u bytes for MQ_L1_WRITE queue\n", msgb_l1len(msg));
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040090
91 rc = sendto(ofd->fd, msg->l1h, msgb_l1len(msg), 0,
92 (const struct sockaddr *)&l1fh->remote_sa, l1fh->remote_sa_len);
93 if (rc < 0) {
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +040094 LOGP(DPCU, LOGL_ERROR, "error writing to L1 msg_queue: %s\n",
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040095 strerror(errno));
96 return rc;
97 } else if (rc < msgb_l1len(msg)) {
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +040098 LOGP(DPCU, LOGL_ERROR, "short write to L1 msg_queue: "
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040099 "%u < %u\n", rc, msgb_l1len(msg));
100 return -EIO;
101 }
102
103 return 0;
104}
105
106int pcu_l1if_open()
107{
108 //struct l1fwd_hdl *l1fh;
109 struct femtol1_hdl *fl1h;
110 int rc;
111
112 /* allocate new femtol1_handle */
113 fl1h = talloc_zero(NULL, struct femtol1_hdl);
114 INIT_LLIST_HEAD(&fl1h->wlc_list);
115
116 l1fh->fl1h = fl1h;
117 fl1h->priv = l1fh;
118
Ivan Kluchnikov61a33f72012-04-12 15:22:06 +0400119 struct osmo_wqueue * queue = &((l1fh->fl1h)->write_q);
120 osmo_wqueue_init(queue, 10);
121 queue->bfd.when |= BSC_FD_READ;
122 queue->bfd.data = l1fh;
123 queue->bfd.priv_nr = 0;
124
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400125 /* Open UDP */
126 struct osmo_wqueue *wq = &l1fh->udp_wq;
127
128 osmo_wqueue_init(wq, 10);
129 wq->write_cb = udp_write_cb;
130 wq->read_cb = udp_read_cb;
131 wq->bfd.when |= BSC_FD_READ;
132 wq->bfd.data = l1fh;
133 wq->bfd.priv_nr = 0;
134 rc = osmo_sock_init_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM,
135 IPPROTO_UDP, NULL, PCU_L1_IF_PORT,
136 OSMO_SOCK_F_BIND);
137 if (rc < 0) {
138 perror("sock_init");
139 exit(1);
140 }
141}
142
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400143int main(int argc, char *argv[])
144{
145 uint16_t nsvci = NSVCI;
146 struct gprs_ns_inst *sgsn_nsi;
147 struct gprs_nsvc *nsvc;
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +0400148 osmo_init_logging(&gprs_log_info);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400149 pcu_l1if_open();
150
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400151 sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb);
152 bssgp_nsi = sgsn_nsi;
153
154 if (!bssgp_nsi)
155 {
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +0400156 LOGP(DPCU, LOGL_ERROR, "Unable to instantiate NS\n");
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400157 exit(1);
158 }
159 bctx = btsctx_alloc(BVCI, NSEI);
160 bctx->cell_id = CELL_ID;
161 bctx->nsei = NSEI;
162 bctx->ra_id.mnc = MNC;
163 bctx->ra_id.mcc = MCC;
164 bctx->ra_id.lac = PCU_LAC;
165 bctx->ra_id.rac = PCU_RAC;
166 bctx->bvci = BVCI;
167 uint8_t cause = 39;
168 gprs_ns_nsip_listen(sgsn_nsi);
169
170 struct sockaddr_in dest;
171 dest.sin_family = AF_INET;
172 dest.sin_port = htons(SGSN_PORT);
173 inet_aton(SGSN_IP, &dest.sin_addr);
174
175 nsvc = nsip_connect(sgsn_nsi, &dest, NSEI, nsvci);
176 unsigned i = 0;
177 while (1)
178 {
Ivan Kluchnikov61a33f72012-04-12 15:22:06 +0400179 osmo_gsm_timers_check();
180 osmo_gsm_timers_prepare();
181 osmo_gsm_timers_update();
182
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400183 osmo_select_main(0);
184 if (i == 7)
185 {
186 bssgp_tx_bvc_reset(bctx, BVCI, cause);
187 }
188 i++;
189 }
190}
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400191