Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 1 | /* pcu_l1_if.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 | |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <string.h> |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 22 | #include <gprs_rlcmac.h> |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 23 | #include <pcu_l1_if.h> |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 24 | #include <gprs_debug.h> |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 25 | #include <bitvector.h> |
| 26 | #include <gsmL1prim.h> |
| 27 | #include <sys/socket.h> |
| 28 | #include <linux/in.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 | } |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 36 | |
| 37 | #define MAX_UDP_LENGTH 1500 |
| 38 | |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 39 | #define msgb_l1prim(msg) ((GsmL1_Prim_t *)(msg)->l1h) |
| 40 | |
| 41 | struct femtol1_hdl { |
| 42 | struct gsm_time gsm_time; |
| 43 | uint32_t hLayer1; /* handle to the L1 instance in the DSP */ |
| 44 | uint32_t dsp_trace_f; |
| 45 | uint16_t clk_cal; |
| 46 | struct llist_head wlc_list; |
| 47 | |
| 48 | void *priv; /* user reference */ |
| 49 | |
| 50 | struct osmo_timer_list alive_timer; |
| 51 | unsigned int alive_prim_cnt; |
| 52 | |
| 53 | struct osmo_fd read_ofd; /* osmo file descriptors */ |
| 54 | struct osmo_wqueue write_q; |
| 55 | |
| 56 | struct { |
| 57 | uint16_t arfcn; |
| 58 | uint8_t tn; |
| 59 | uint8_t tsc; |
| 60 | uint16_t ta; |
| 61 | } channel_info; |
| 62 | |
| 63 | }; |
| 64 | |
| 65 | struct l1fwd_hdl { |
| 66 | struct sockaddr_storage remote_sa; |
| 67 | socklen_t remote_sa_len; |
| 68 | |
| 69 | struct osmo_wqueue udp_wq; |
| 70 | |
| 71 | struct femtol1_hdl *fl1h; |
| 72 | }; |
| 73 | |
| 74 | struct l1fwd_hdl *l1fh = talloc_zero(NULL, struct l1fwd_hdl); |
| 75 | |
Ivan Kluchnikov | 4277f0c | 2012-04-12 14:24:35 +0400 | [diff] [blame] | 76 | // Variable for storage current FN. |
| 77 | int frame_number; |
| 78 | |
| 79 | int get_current_fn() |
| 80 | { |
| 81 | return frame_number; |
| 82 | } |
| 83 | |
| 84 | void set_current_fn(int fn) |
| 85 | { |
| 86 | frame_number = fn; |
| 87 | } |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 88 | |
| 89 | struct msgb *l1p_msgb_alloc(void) |
| 90 | { |
| 91 | struct msgb *msg = msgb_alloc(sizeof(GsmL1_Prim_t), "l1_prim"); |
| 92 | |
| 93 | if (msg) |
| 94 | msg->l1h = msgb_put(msg, sizeof(GsmL1_Prim_t)); |
| 95 | |
| 96 | return msg; |
| 97 | } |
| 98 | |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 99 | // Send RLC/MAC block to OpenBTS. |
| 100 | void pcu_l1if_tx_pdtch(msgb *msg, uint8_t trx, uint8_t ts, uint16_t arfcn, |
| 101 | uint32_t fn, uint8_t block_nr) |
Ivan Kluchnikov | 4277f0c | 2012-04-12 14:24:35 +0400 | [diff] [blame] | 102 | { |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 103 | struct msgb *nmsg = l1p_msgb_alloc(); |
| 104 | GsmL1_Prim_t *prim = msgb_l1prim(nmsg); |
| 105 | |
Ivan Kluchnikov | 4277f0c | 2012-04-12 14:24:35 +0400 | [diff] [blame] | 106 | prim->id = GsmL1_PrimId_PhDataReq; |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 107 | prim->u.phDataReq.sapi = GsmL1_Sapi_Pdtch; |
| 108 | memcpy(prim->u.phDataReq.msgUnitParam.u8Buffer, msg->data, msg->len); |
| 109 | prim->u.phDataReq.msgUnitParam.u8Size = msg->len; |
| 110 | osmo_wqueue_enqueue(&l1fh->udp_wq, nmsg); |
| 111 | msgb_free(msg); |
Ivan Kluchnikov | 4277f0c | 2012-04-12 14:24:35 +0400 | [diff] [blame] | 112 | } |
| 113 | |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 114 | void pcu_l1if_tx_agch(bitvec * block, int len) |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 115 | { |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 116 | struct msgb *msg = l1p_msgb_alloc(); |
| 117 | GsmL1_Prim_t *prim = msgb_l1prim(msg); |
| 118 | |
| 119 | prim->id = GsmL1_PrimId_PhDataReq; |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 120 | prim->u.phDataReq.sapi = GsmL1_Sapi_Agch; |
Ivan Kluchnikov | 835f91e | 2012-04-30 18:00:36 +0400 | [diff] [blame] | 121 | bitvec_pack(block, prim->u.phDataReq.msgUnitParam.u8Buffer); |
Ivan Kluchnikov | 5310d45 | 2012-04-17 22:00:31 +0400 | [diff] [blame] | 122 | prim->u.phDataReq.msgUnitParam.u8Size = len; |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 123 | osmo_wqueue_enqueue(&l1fh->udp_wq, msg); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 124 | } |
| 125 | |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 126 | int pcu_l1if_rx_pdch(GsmL1_PhDataInd_t *data_ind) |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 127 | { |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 128 | gprs_rlcmac_rcv_block(data_ind->msgUnitParam.u8Buffer, |
| 129 | data_ind->msgUnitParam.u8Size, data_ind->u32Fn); |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 130 | |
| 131 | return 0; |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 132 | } |
| 133 | |
Ivan Kluchnikov | 5310d45 | 2012-04-17 22:00:31 +0400 | [diff] [blame] | 134 | static int handle_ph_connect_ind(struct femtol1_hdl *fl1, GsmL1_PhConnectInd_t *connect_ind) |
| 135 | { |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 136 | struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts; |
| 137 | |
| 138 | bts->trx[0].arfcn = connect_ind->u16Arfcn; |
| 139 | bts->trx[0].pdch[connect_ind->u8Tn].enable = 1; |
| 140 | bts->trx[0].pdch[connect_ind->u8Tn].tsc = connect_ind->u8Tsc; |
Ivan Kluchnikov | 5310d45 | 2012-04-17 22:00:31 +0400 | [diff] [blame] | 141 | (l1fh->fl1h)->channel_info.arfcn = connect_ind->u16Arfcn; |
| 142 | (l1fh->fl1h)->channel_info.tn = connect_ind->u8Tn; |
| 143 | (l1fh->fl1h)->channel_info.tsc = connect_ind->u8Tsc; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 144 | LOGP(DL1IF, LOGL_NOTICE, "RX: [ PCU <- BTS ] PhConnectInd: ARFCN: %u TN: %u TSC: %u \n", |
| 145 | connect_ind->u16Arfcn, (unsigned)connect_ind->u8Tn, (unsigned)connect_ind->u8Tsc); |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 146 | |
| 147 | return 0; |
Ivan Kluchnikov | 5310d45 | 2012-04-17 22:00:31 +0400 | [diff] [blame] | 148 | } |
| 149 | |
Ivan Kluchnikov | 4277f0c | 2012-04-12 14:24:35 +0400 | [diff] [blame] | 150 | static int handle_ph_readytosend_ind(struct femtol1_hdl *fl1, GsmL1_PhReadyToSendInd_t *readytosend_ind) |
| 151 | { |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 152 | gprs_rlcmac_rcv_rts_block(0,0, (l1fh->fl1h)->channel_info.arfcn, readytosend_ind->u32Fn, 0); |
Ivan Kluchnikov | 4277f0c | 2012-04-12 14:24:35 +0400 | [diff] [blame] | 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | static int handle_ph_data_ind(struct femtol1_hdl *fl1, GsmL1_PhDataInd_t *data_ind) |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 157 | { |
| 158 | int rc = 0; |
| 159 | switch (data_ind->sapi) { |
| 160 | case GsmL1_Sapi_Rach: |
| 161 | break; |
| 162 | case GsmL1_Sapi_Pdtch: |
| 163 | case GsmL1_Sapi_Pacch: |
| 164 | pcu_l1if_rx_pdch(data_ind); |
| 165 | break; |
| 166 | case GsmL1_Sapi_Pbcch: |
| 167 | case GsmL1_Sapi_Pagch: |
| 168 | case GsmL1_Sapi_Ppch: |
| 169 | case GsmL1_Sapi_Pnch: |
| 170 | case GsmL1_Sapi_Ptcch: |
| 171 | case GsmL1_Sapi_Prach: |
| 172 | break; |
| 173 | default: |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 174 | LOGP(DL1IF, LOGL_NOTICE, "Rx PH-DATA.ind for unknown L1 SAPI %u \n", data_ind->sapi); |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 175 | break; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 176 | } |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 177 | |
| 178 | return rc; |
| 179 | } |
| 180 | |
Ivan Kluchnikov | 5310d45 | 2012-04-17 22:00:31 +0400 | [diff] [blame] | 181 | static int handle_ph_ra_ind(struct femtol1_hdl *fl1, GsmL1_PhRaInd_t *ra_ind) |
| 182 | { |
| 183 | int rc = 0; |
| 184 | (l1fh->fl1h)->channel_info.ta = ra_ind->measParam.i16BurstTiming; |
| 185 | rc = gprs_rlcmac_rcv_rach(ra_ind->msgUnitParam.u8Buffer[0], ra_ind->u32Fn, ra_ind->measParam.i16BurstTiming); |
| 186 | return rc; |
| 187 | } |
| 188 | |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 189 | /* handle any random indication from the L1 */ |
| 190 | int pcu_l1if_handle_l1prim(struct femtol1_hdl *fl1, struct msgb *msg) |
| 191 | { |
| 192 | GsmL1_Prim_t *l1p = msgb_l1prim(msg); |
| 193 | int rc = 0; |
| 194 | |
| 195 | switch (l1p->id) { |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 196 | case GsmL1_PrimId_PhConnectInd: |
Ivan Kluchnikov | 5310d45 | 2012-04-17 22:00:31 +0400 | [diff] [blame] | 197 | rc = handle_ph_connect_ind(fl1, &l1p->u.phConnectInd); |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 198 | break; |
| 199 | case GsmL1_PrimId_PhReadyToSendInd: |
Ivan Kluchnikov | 4277f0c | 2012-04-12 14:24:35 +0400 | [diff] [blame] | 200 | rc = handle_ph_readytosend_ind(fl1, &l1p->u.phReadyToSendInd); |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 201 | break; |
| 202 | case GsmL1_PrimId_PhDataInd: |
Ivan Kluchnikov | 4277f0c | 2012-04-12 14:24:35 +0400 | [diff] [blame] | 203 | rc = handle_ph_data_ind(fl1, &l1p->u.phDataInd); |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 204 | break; |
| 205 | case GsmL1_PrimId_PhRaInd: |
Ivan Kluchnikov | 5310d45 | 2012-04-17 22:00:31 +0400 | [diff] [blame] | 206 | rc = handle_ph_ra_ind(fl1, &l1p->u.phRaInd); |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 207 | break; |
| 208 | default: |
| 209 | break; |
| 210 | } |
| 211 | |
| 212 | /* Special return value '1' means: do not free */ |
| 213 | if (rc != 1) |
| 214 | msgb_free(msg); |
| 215 | |
| 216 | return rc; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 217 | } |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 218 | |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame^] | 219 | PCU will currently not work without getting a GSM time or BFI indidication. |
| 220 | In order to fix this, i will discuss this on the mailing list. |
| 221 | Andreas |
| 222 | |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 223 | /* OpenBTS socket functions */ |
| 224 | |
| 225 | // TODO: We should move this parameters to config file. |
| 226 | #define PCU_L1_IF_PORT 5944 |
| 227 | |
| 228 | /* data has arrived on the udp socket */ |
| 229 | static int udp_read_cb(struct osmo_fd *ofd) |
| 230 | { |
| 231 | struct msgb *msg = msgb_alloc_headroom(2048, 128, "udp_rx"); |
| 232 | struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data; |
| 233 | struct femtol1_hdl *fl1h = l1fh->fl1h; |
| 234 | int rc; |
| 235 | |
| 236 | if (!msg) |
| 237 | return -ENOMEM; |
| 238 | |
| 239 | msg->l1h = msg->data; |
| 240 | |
| 241 | l1fh->remote_sa_len = sizeof(l1fh->remote_sa); |
| 242 | rc = recvfrom(ofd->fd, msg->l1h, msgb_tailroom(msg), 0, |
| 243 | (struct sockaddr *) &l1fh->remote_sa, &l1fh->remote_sa_len); |
| 244 | if (rc < 0) { |
| 245 | perror("read from udp"); |
| 246 | msgb_free(msg); |
| 247 | return rc; |
| 248 | } else if (rc == 0) { |
| 249 | perror("len=0 read from udp"); |
| 250 | msgb_free(msg); |
| 251 | return rc; |
| 252 | } |
| 253 | msgb_put(msg, rc); |
| 254 | |
| 255 | rc = pcu_l1if_handle_l1prim(fl1h, msg); |
| 256 | return rc; |
| 257 | } |
| 258 | |
| 259 | /* callback when we can write to the UDP socket */ |
| 260 | static int udp_write_cb(struct osmo_fd *ofd, struct msgb *msg) |
| 261 | { |
| 262 | int rc; |
| 263 | struct l1fwd_hdl *l1fh = (l1fwd_hdl *)ofd->data; |
| 264 | |
| 265 | //DEBUGP(DGPRS, "UDP: Writing %u bytes for MQ_L1_WRITE queue\n", msgb_l1len(msg)); |
| 266 | |
| 267 | rc = sendto(ofd->fd, msg->l1h, msgb_l1len(msg), 0, |
| 268 | (const struct sockaddr *)&l1fh->remote_sa, l1fh->remote_sa_len); |
| 269 | if (rc < 0) { |
| 270 | LOGP(DPCU, LOGL_ERROR, "error writing to L1 msg_queue: %s\n", |
| 271 | strerror(errno)); |
| 272 | return rc; |
| 273 | } else if (rc < (int)msgb_l1len(msg)) { |
| 274 | LOGP(DPCU, LOGL_ERROR, "short write to L1 msg_queue: " |
| 275 | "%u < %u\n", rc, msgb_l1len(msg)); |
| 276 | return -EIO; |
| 277 | } |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | int pcu_l1if_open() |
| 283 | { |
| 284 | //struct l1fwd_hdl *l1fh; |
| 285 | struct femtol1_hdl *fl1h; |
| 286 | int rc; |
| 287 | |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 288 | /* allocate new femtol1_handle */ |
| 289 | fl1h = talloc_zero(NULL, struct femtol1_hdl); |
| 290 | INIT_LLIST_HEAD(&fl1h->wlc_list); |
| 291 | |
| 292 | l1fh->fl1h = fl1h; |
| 293 | fl1h->priv = l1fh; |
| 294 | |
| 295 | struct osmo_wqueue * queue = &((l1fh->fl1h)->write_q); |
| 296 | osmo_wqueue_init(queue, 10); |
| 297 | queue->bfd.when |= BSC_FD_READ; |
| 298 | queue->bfd.data = l1fh; |
| 299 | queue->bfd.priv_nr = 0; |
| 300 | |
| 301 | /* Open UDP */ |
| 302 | struct osmo_wqueue *wq = &l1fh->udp_wq; |
| 303 | |
| 304 | osmo_wqueue_init(wq, 10); |
| 305 | wq->write_cb = udp_write_cb; |
| 306 | wq->read_cb = udp_read_cb; |
| 307 | wq->bfd.when |= BSC_FD_READ; |
| 308 | wq->bfd.data = l1fh; |
| 309 | wq->bfd.priv_nr = 0; |
| 310 | rc = osmo_sock_init_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM, |
| 311 | IPPROTO_UDP, NULL, PCU_L1_IF_PORT, |
| 312 | OSMO_SOCK_F_BIND); |
| 313 | if (rc < 0) { |
| 314 | perror("sock_init"); |
| 315 | exit(1); |
| 316 | } |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |