Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 1 | /* 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> |
| 29 | extern "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 Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 38 | extern void *tall_pcu_ctx; |
| 39 | |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 40 | struct 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 | |
| 64 | struct 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 | |
| 73 | struct 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 | |
| 80 | int pcu_sock_send(struct msgb *msg) |
| 81 | { |
| 82 | osmo_wqueue_enqueue(&l1fh->udp_wq, msg); |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | /* data has arrived on the udp socket */ |
| 87 | static int udp_read_cb(struct osmo_fd *ofd) |
| 88 | { |
| 89 | struct msgb *msg = msgb_alloc_headroom(2048, 128, "udp_rx"); |
| 90 | struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data; |
| 91 | struct femtol1_hdl *fl1h = l1fh->fl1h; |
| 92 | int rc; |
| 93 | |
| 94 | if (!msg) |
| 95 | return -ENOMEM; |
| 96 | |
| 97 | msg->l1h = msg->data; |
| 98 | |
| 99 | l1fh->remote_sa_len = sizeof(l1fh->remote_sa); |
| 100 | rc = recvfrom(ofd->fd, msg->l1h, msgb_tailroom(msg), 0, |
| 101 | (struct sockaddr *) &l1fh->remote_sa, &l1fh->remote_sa_len); |
| 102 | if (rc < 0) { |
| 103 | perror("read from udp"); |
| 104 | msgb_free(msg); |
| 105 | return rc; |
| 106 | } else if (rc == 0) { |
| 107 | perror("len=0 read from udp"); |
| 108 | msgb_free(msg); |
| 109 | return rc; |
| 110 | } |
| 111 | msgb_put(msg, rc); |
| 112 | |
| 113 | struct gsm_pcu_if *pcu_prim = (gsm_pcu_if *)(msg->l1h); |
| 114 | rc = pcu_rx(pcu_prim->msg_type, pcu_prim); |
| 115 | return rc; |
| 116 | } |
| 117 | |
| 118 | /* callback when we can write to the UDP socket */ |
| 119 | static int udp_write_cb(struct osmo_fd *ofd, struct msgb *msg) |
| 120 | { |
| 121 | int rc; |
| 122 | struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data; |
| 123 | |
| 124 | //LOGP(DPCU, LOGL_ERROR, "UDP: Writing %u bytes for MQ_L1_WRITE queue\n", msgb_length(msg)); |
| 125 | |
| 126 | rc = sendto(ofd->fd, msgb_data(msg), msgb_length(msg), 0, |
| 127 | (const struct sockaddr *)&l1fh->remote_sa, l1fh->remote_sa_len); |
| 128 | if (rc < 0) { |
| 129 | LOGP(DPCU, LOGL_ERROR, "error writing to L1 msg_queue: %s\n", |
| 130 | strerror(errno)); |
| 131 | return rc; |
| 132 | } else if (rc < (int)msgb_length(msg)) { |
| 133 | LOGP(DPCU, LOGL_ERROR, "short write to L1 msg_queue: " |
| 134 | "%u < %u\n", rc, msgb_length(msg)); |
| 135 | return -EIO; |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | int pcu_l1if_open() |
| 142 | { |
| 143 | struct femtol1_hdl *fl1h; |
| 144 | int rc; |
| 145 | |
| 146 | /* allocate new femtol1_handle */ |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 147 | fl1h = talloc_zero(tall_pcu_ctx, struct femtol1_hdl); |
Ivan Kluchnikov | ef7f28c | 2012-07-12 14:49:15 +0400 | [diff] [blame] | 148 | INIT_LLIST_HEAD(&fl1h->wlc_list); |
| 149 | |
| 150 | l1fh->fl1h = fl1h; |
| 151 | fl1h->priv = l1fh; |
| 152 | |
| 153 | struct osmo_wqueue * queue = &((l1fh->fl1h)->write_q); |
| 154 | osmo_wqueue_init(queue, 10); |
| 155 | queue->bfd.when |= BSC_FD_READ; |
| 156 | queue->bfd.data = l1fh; |
| 157 | queue->bfd.priv_nr = 0; |
| 158 | |
| 159 | /* Open UDP */ |
| 160 | struct osmo_wqueue *wq = &l1fh->udp_wq; |
| 161 | |
| 162 | osmo_wqueue_init(wq, 10); |
| 163 | wq->write_cb = udp_write_cb; |
| 164 | wq->read_cb = udp_read_cb; |
| 165 | wq->bfd.when |= BSC_FD_READ; |
| 166 | wq->bfd.data = l1fh; |
| 167 | wq->bfd.priv_nr = 0; |
| 168 | rc = osmo_sock_init_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM, |
| 169 | IPPROTO_UDP, NULL, PCU_L1_IF_PORT, |
| 170 | OSMO_SOCK_F_BIND); |
| 171 | if (rc < 0) { |
| 172 | perror("sock_init"); |
| 173 | exit(1); |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | void pcu_l1if_close(void) |
| 180 | { |
| 181 | gprs_bssgp_destroy(); |
| 182 | |
| 183 | /* FIXME: cleanup l1if */ |
| 184 | talloc_free(l1fh->fl1h); |
| 185 | } |