Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 1 | /* 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> |
| 21 | #include <Threads.h> |
| 22 | #include <Sockets.h> |
| 23 | #include <pcu_l1_if.h> |
Ivan Kluchnikov | 61a33f7 | 2012-04-12 15:22:06 +0400 | [diff] [blame] | 24 | #include <gsm_timer.h> |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 25 | |
| 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 Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 30 | #define PCU_L1_IF_PORT 5944 |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 31 | |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 32 | struct l1fwd_hdl *l1fh = talloc_zero(NULL, struct l1fwd_hdl); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 33 | |
| 34 | int 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: |
| 43 | LOGP(DGPRS, LOGL_ERROR, "RLCMAC: Unknown event %u from NS\n", event); |
| 44 | if (msg) |
| 45 | talloc_free(msg); |
| 46 | rc = -EIO; |
| 47 | break; |
| 48 | } |
| 49 | return rc; |
| 50 | } |
| 51 | |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 52 | /* data has arrived on the udp socket */ |
| 53 | static 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 */ |
| 84 | static 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 Kluchnikov | 61a33f7 | 2012-04-12 15:22:06 +0400 | [diff] [blame] | 89 | //DEBUGP(DGPRS, "UDP: Writing %u bytes for MQ_L1_WRITE queue\n", msgb_l1len(msg)); |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 90 | |
| 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) { |
| 94 | LOGP(DGPRS, LOGL_ERROR, "error writing to L1 msg_queue: %s\n", |
| 95 | strerror(errno)); |
| 96 | return rc; |
| 97 | } else if (rc < msgb_l1len(msg)) { |
| 98 | LOGP(DGPRS, LOGL_ERROR, "short write to L1 msg_queue: " |
| 99 | "%u < %u\n", rc, msgb_l1len(msg)); |
| 100 | return -EIO; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | int 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 Kluchnikov | 61a33f7 | 2012-04-12 15:22:06 +0400 | [diff] [blame] | 119 | 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 Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 125 | /* 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 Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 143 | int main(int argc, char *argv[]) |
| 144 | { |
| 145 | uint16_t nsvci = NSVCI; |
| 146 | struct gprs_ns_inst *sgsn_nsi; |
| 147 | struct gprs_nsvc *nsvc; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 148 | |
| 149 | osmo_init_logging(&log_info); |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 150 | pcu_l1if_open(); |
| 151 | |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 152 | sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb); |
| 153 | bssgp_nsi = sgsn_nsi; |
| 154 | |
| 155 | if (!bssgp_nsi) |
| 156 | { |
| 157 | LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n"); |
| 158 | exit(1); |
| 159 | } |
| 160 | bctx = btsctx_alloc(BVCI, NSEI); |
| 161 | bctx->cell_id = CELL_ID; |
| 162 | bctx->nsei = NSEI; |
| 163 | bctx->ra_id.mnc = MNC; |
| 164 | bctx->ra_id.mcc = MCC; |
| 165 | bctx->ra_id.lac = PCU_LAC; |
| 166 | bctx->ra_id.rac = PCU_RAC; |
| 167 | bctx->bvci = BVCI; |
| 168 | uint8_t cause = 39; |
| 169 | gprs_ns_nsip_listen(sgsn_nsi); |
| 170 | |
| 171 | struct sockaddr_in dest; |
| 172 | dest.sin_family = AF_INET; |
| 173 | dest.sin_port = htons(SGSN_PORT); |
| 174 | inet_aton(SGSN_IP, &dest.sin_addr); |
| 175 | |
| 176 | nsvc = nsip_connect(sgsn_nsi, &dest, NSEI, nsvci); |
| 177 | unsigned i = 0; |
| 178 | while (1) |
| 179 | { |
Ivan Kluchnikov | 61a33f7 | 2012-04-12 15:22:06 +0400 | [diff] [blame] | 180 | osmo_gsm_timers_check(); |
| 181 | osmo_gsm_timers_prepare(); |
| 182 | osmo_gsm_timers_update(); |
| 183 | |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 184 | osmo_select_main(0); |
| 185 | if (i == 7) |
| 186 | { |
| 187 | bssgp_tx_bvc_reset(bctx, BVCI, cause); |
| 188 | } |
| 189 | i++; |
| 190 | } |
| 191 | } |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 192 | |