blob: a58a1221be88ced378a1a5c47074de3851183063 [file] [log] [blame]
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001/* pcu_l1_if.cpp
2 *
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04003 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04004 *
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
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040020#include <stdio.h>
21#include <unistd.h>
22#include <stdlib.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020023#include <string.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040024#include <errno.h>
25#include <assert.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020026#include <sys/socket.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040027#include <sys/un.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020028extern "C" {
29#include <osmocom/core/talloc.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040030#include <osmocom/core/select.h>
31#include <osmocom/core/msgb.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020032}
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040033
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040034#include <gprs_rlcmac.h>
35#include <pcu_l1_if.h>
36#include <gprs_debug.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040037#include <gprs_bssgp_pcu.h>
38#include <pcuif_proto.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020039
Andreas Eversberge266bd42012-07-13 14:00:21 +020040extern void *tall_pcu_ctx;
41
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +040042// Variable for storage current FN.
43int frame_number;
44
45int get_current_fn()
46{
47 return frame_number;
48}
49
50void set_current_fn(int fn)
51{
52 frame_number = fn;
53}
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040054
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040055/*
56 * PCU messages
57 */
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040058
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040059struct msgb *pcu_msgb_alloc(uint8_t msg_type, uint8_t bts_nr)
60{
61 struct msgb *msg;
62 struct gsm_pcu_if *pcu_prim;
63
64 msg = msgb_alloc(sizeof(struct gsm_pcu_if), "pcu_sock_tx");
65 if (!msg)
66 return NULL;
67 msgb_put(msg, sizeof(struct gsm_pcu_if));
68 pcu_prim = (struct gsm_pcu_if *) msg->data;
69 pcu_prim->msg_type = msg_type;
70 pcu_prim->bts_nr = bts_nr;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040071
72 return msg;
73}
74
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040075static int pcu_tx_act_req(uint8_t trx, uint8_t ts, uint8_t activate)
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +040076{
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040077 struct msgb *msg;
78 struct gsm_pcu_if *pcu_prim;
79 struct gsm_pcu_if_act_req *act_req;
80
81 LOGP(DL1IF, LOGL_INFO, "Sending %s request: trx=%d ts=%d\n",
82 (activate) ? "activate" : "deactivate", trx, ts);
83
84 msg = pcu_msgb_alloc(PCU_IF_MSG_ACT_REQ, 0);
85 if (!msg)
86 return -ENOMEM;
87 pcu_prim = (struct gsm_pcu_if *) msg->data;
88 act_req = &pcu_prim->u.act_req;
89 act_req->activate = activate;
90 act_req->trx_nr = trx;
91 act_req->ts_nr = ts;
92
93 return pcu_sock_send(msg);
94}
95
96static int pcu_tx_data_req(uint8_t trx, uint8_t ts, uint8_t sapi,
97 uint16_t arfcn, uint32_t fn, uint8_t block_nr, uint8_t *data,
98 uint8_t len)
99{
100 struct msgb *msg;
101 struct gsm_pcu_if *pcu_prim;
102 struct gsm_pcu_if_data *data_req;
103
104 LOGP(DL1IF, LOGL_DEBUG, "Sending data request: trx=%d ts=%d sapi=%d "
105 "arfcn=%d fn=%d block=%d data=%s\n", trx, ts, sapi, arfcn, fn,
106 block_nr, osmo_hexdump(data, len));
107
108 msg = pcu_msgb_alloc(PCU_IF_MSG_DATA_REQ, 0);
109 if (!msg)
110 return -ENOMEM;
111 pcu_prim = (struct gsm_pcu_if *) msg->data;
112 data_req = &pcu_prim->u.data_req;
113
114 data_req->sapi = sapi;
115 data_req->fn = fn;
116 data_req->arfcn = arfcn;
117 data_req->trx_nr = trx;
118 data_req->ts_nr = ts;
119 data_req->block_nr = block_nr;
120 memcpy(data_req->data, data, len);
121 data_req->len = len;
122
123 return pcu_sock_send(msg);
124}
125
126void pcu_l1if_tx_pdtch(msgb *msg, uint8_t trx, uint8_t ts, uint16_t arfcn,
127 uint32_t fn, uint8_t block_nr)
128{
129 pcu_tx_data_req(trx, ts, PCU_IF_SAPI_PDTCH, arfcn, fn, block_nr,
130 msg->data, msg->len);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200131 msgb_free(msg);
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400132}
133
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400134void pcu_l1if_tx_ptcch(msgb *msg, uint8_t trx, uint8_t ts, uint16_t arfcn,
135 uint32_t fn, uint8_t block_nr)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400136{
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400137 pcu_tx_data_req(trx, ts, PCU_IF_SAPI_PTCCH, arfcn, fn, block_nr,
138 msg->data, msg->len);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400139 msgb_free(msg);
140}
141
Andreas Eversberg7b045012012-07-05 07:38:49 +0200142void pcu_l1if_tx_agch(bitvec * block, int plen)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400143{
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400144 uint8_t data[23]; /* prefix PLEN */
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400145
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400146 /* FIXME: why does OpenBTS has no PLEN and no fill in message? */
147 bitvec_pack(block, data + 1);
148 data[0] = (plen << 2) | 0x01;
149 pcu_tx_data_req(0, 0, PCU_IF_SAPI_AGCH, 0, 0, 0, data, 23);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400150}
151
Andreas Eversberge13fa2d2012-07-09 17:10:44 +0200152void pcu_l1if_tx_pch(bitvec * block, int plen, char *imsi)
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400153{
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400154 uint8_t data[23+3]; /* prefix PLEN */
155
156 /* paging group */
157 if (!imsi || strlen(imsi) < 3)
158 return;
159 imsi += strlen(imsi) - 3;
160 data[0] = imsi[0];
161 data[1] = imsi[1];
162 data[2] = imsi[2];
163
164 bitvec_pack(block, data + 3+1);
165 data[3] = (plen << 2) | 0x01;
166 pcu_tx_data_req(0, 0, PCU_IF_SAPI_PCH, 0, 0, 0, data, 23+3);
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400167}
168
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400169static void pcu_l1if_tx_bcch(uint8_t *data, int len)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400170{
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400171 pcu_tx_data_req(0, 0, PCU_IF_SAPI_BCCH, 0, 0, 0, data, len);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400172}
173
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400174static int pcu_rx_data_ind(struct gsm_pcu_if_data *data_ind)
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400175{
176 int rc = 0;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400177
178 LOGP(DL1IF, LOGL_DEBUG, "Data indication received: sapi=%d arfcn=%d "
179 "block=%d data=%s\n", data_ind->sapi,
180 data_ind->arfcn, data_ind->block_nr,
181 osmo_hexdump(data_ind->data, data_ind->len));
182
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400183 switch (data_ind->sapi) {
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400184 case PCU_IF_SAPI_PDTCH:
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200185 rc = gprs_rlcmac_rcv_block(data_ind->trx_nr, data_ind->ts_nr,
186 data_ind->data, data_ind->len, data_ind->fn);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400187 break;
188 default:
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400189 LOGP(DL1IF, LOGL_ERROR, "Received PCU data indication with "
190 "unsupported sapi %d\n", data_ind->sapi);
191 rc = -EINVAL;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400192 }
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400193
194 return rc;
195}
196
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400197static int pcu_rx_rts_req(struct gsm_pcu_if_rts_req *rts_req)
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400198{
199 int rc = 0;
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400200
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400201 LOGP(DL1IF, LOGL_DEBUG, "RTS request received: trx=%d ts=%d sapi=%d "
202 "arfcn=%d fn=%d block=%d\n", rts_req->trx_nr, rts_req->ts_nr,
203 rts_req->sapi, rts_req->arfcn, rts_req->fn, rts_req->block_nr);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400204
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400205 switch (rts_req->sapi) {
206 case PCU_IF_SAPI_PDTCH:
207 gprs_rlcmac_rcv_rts_block(rts_req->trx_nr, rts_req->ts_nr,
208 rts_req->arfcn, rts_req->fn, rts_req->block_nr);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400209 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400210 case PCU_IF_SAPI_PTCCH:
211 /* FIXME */
212 {
213 struct msgb *msg = msgb_alloc(23, "l1_prim");
214 memset(msgb_put(msg, 23), 0x2b, 23);
215 pcu_l1if_tx_ptcch(msg, rts_req->trx_nr, rts_req->ts_nr,
216 rts_req->arfcn, rts_req->fn, rts_req->block_nr);
217 }
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400218 break;
219 default:
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400220 LOGP(DL1IF, LOGL_ERROR, "Received PCU RTS request with "
221 "unsupported sapi %d\n", rts_req->sapi);
222 rc = -EINVAL;
223 }
224
225 return rc;
226}
227
228static int pcu_rx_rach_ind(struct gsm_pcu_if_rach_ind *rach_ind)
229{
230 int rc = 0;
231
232 LOGP(DL1IF, LOGL_INFO, "RACH request received: sapi=%d "
233 "qta=%d, ra=%d, fn=%d\n", rach_ind->sapi, rach_ind->qta,
234 rach_ind->ra, rach_ind->fn);
235
236 switch (rach_ind->sapi) {
237 case PCU_IF_SAPI_RACH:
238 rc = gprs_rlcmac_rcv_rach(rach_ind->ra, rach_ind->fn,
239 rach_ind->qta);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400240 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400241 default:
242 LOGP(DL1IF, LOGL_ERROR, "Received PCU rach request with "
243 "unsupported sapi %d\n", rach_ind->sapi);
244 rc = -EINVAL;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400245 }
246
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400247 return rc;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400248}
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200249
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400250static int pcu_rx_info_ind(struct gsm_pcu_if_info_ind *info_ind)
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200251{
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200252 struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400253 int rc = 0;
254 int trx, ts, tfi;
255 struct gprs_rlcmac_tbf *tbf;
256 int i;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200257
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400258 LOGP(DL1IF, LOGL_DEBUG, "Info indication received:\n");
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200259
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400260 if (!(info_ind->flags & PCU_IF_FLAG_ACTIVE)) {
261 LOGP(DL1IF, LOGL_NOTICE, "BTS not available\n");
262bssgp_failed:
263 /* free all TBF */
264 for (trx = 0; trx < 8; trx++) {
265 bts->trx[trx].arfcn = info_ind->trx[trx].arfcn;
266 for (ts = 0; ts < 8; ts++) {
267 for (tfi = 0; tfi < 32; tfi++) {
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200268 tbf = bts->trx[trx].pdch[ts].ul_tbf[tfi];
269 if (tbf)
270 tbf_free(tbf);
271 tbf = bts->trx[trx].pdch[ts].dl_tbf[tfi];
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400272 if (tbf)
273 tbf_free(tbf);
274 }
275 }
276 }
277 gprs_bssgp_destroy();
278 return 0;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200279 }
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400280 LOGP(DL1IF, LOGL_INFO, "BTS available\n");
281 LOGP(DL1IF, LOGL_DEBUG, " mcc=%d\n", info_ind->mcc);
282 LOGP(DL1IF, LOGL_DEBUG, " mnc=%d\n", info_ind->mnc);
283 LOGP(DL1IF, LOGL_DEBUG, " lac=%d\n", info_ind->lac);
284 LOGP(DL1IF, LOGL_DEBUG, " rac=%d\n", info_ind->rac);
285 LOGP(DL1IF, LOGL_DEBUG, " cell_id=%d\n", info_ind->cell_id);
286 LOGP(DL1IF, LOGL_DEBUG, " nsei=%d\n", info_ind->nsei);
287 LOGP(DL1IF, LOGL_DEBUG, " nse_timer=%d %d %d %d %d %d %d\n",
288 info_ind->nse_timer[0], info_ind->nse_timer[1],
289 info_ind->nse_timer[2], info_ind->nse_timer[3],
290 info_ind->nse_timer[4], info_ind->nse_timer[5],
291 info_ind->nse_timer[6]);
292 LOGP(DL1IF, LOGL_DEBUG, " cell_timer=%d %d %d %d %d %d %d %d %d %d "
293 "%d\n",
294 info_ind->cell_timer[0], info_ind->cell_timer[1],
295 info_ind->cell_timer[2], info_ind->cell_timer[3],
296 info_ind->cell_timer[4], info_ind->cell_timer[5],
297 info_ind->cell_timer[6], info_ind->cell_timer[7],
298 info_ind->cell_timer[8], info_ind->cell_timer[9],
299 info_ind->cell_timer[10]);
300 LOGP(DL1IF, LOGL_DEBUG, " repeat_time=%d\n", info_ind->repeat_time);
301 LOGP(DL1IF, LOGL_DEBUG, " repeat_count=%d\n", info_ind->repeat_count);
302 LOGP(DL1IF, LOGL_DEBUG, " bvci=%d\n", info_ind->bvci);
303 LOGP(DL1IF, LOGL_DEBUG, " t3142=%d\n", info_ind->t3142);
304 LOGP(DL1IF, LOGL_DEBUG, " t3169=%d\n", info_ind->t3169);
305 LOGP(DL1IF, LOGL_DEBUG, " t3191=%d\n", info_ind->t3191);
306 LOGP(DL1IF, LOGL_DEBUG, " t3193=%d (ms)\n", info_ind->t3193_10ms * 10);
307 LOGP(DL1IF, LOGL_DEBUG, " t3195=%d\n", info_ind->t3195);
308 LOGP(DL1IF, LOGL_DEBUG, " n3101=%d\n", info_ind->n3101);
309 LOGP(DL1IF, LOGL_DEBUG, " n3103=%d\n", info_ind->n3103);
310 LOGP(DL1IF, LOGL_DEBUG, " n3105=%d\n", info_ind->n3105);
311 LOGP(DL1IF, LOGL_DEBUG, " cv_countdown=%d\n", info_ind->cv_countdown);
312 LOGP(DL1IF, LOGL_DEBUG, " dl_tbf_ext=%d\n", info_ind->dl_tbf_ext);
313 LOGP(DL1IF, LOGL_DEBUG, " ul_tbf_ext=%d\n", info_ind->ul_tbf_ext);
314 for (i = 0; i < 4; i++) {
315 if ((info_ind->flags & (PCU_IF_FLAG_CS1 << i)))
316 LOGP(DL1IF, LOGL_DEBUG, " Use CS%d\n", i+1);
317 }
318 for (i = 0; i < 9; i++) {
319 if ((info_ind->flags & (PCU_IF_FLAG_MCS1 << i)))
320 LOGP(DL1IF, LOGL_DEBUG, " Use MCS%d\n", i+1);
321 }
322 LOGP(DL1IF, LOGL_DEBUG, " initial_cs=%d\n", info_ind->initial_cs);
323 LOGP(DL1IF, LOGL_DEBUG, " initial_mcs=%d\n", info_ind->initial_mcs);
324 LOGP(DL1IF, LOGL_DEBUG, " nsvci=%d\n", info_ind->nsvci[0]);
325 LOGP(DL1IF, LOGL_DEBUG, " local_port=%d\n", info_ind->local_port[0]);
326 LOGP(DL1IF, LOGL_DEBUG, " remote_port=%d\n", info_ind->remote_port[0]);
327 LOGP(DL1IF, LOGL_DEBUG, " remote_ip=%d\n", info_ind->remote_ip[0]);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200328
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400329 rc = gprs_bssgp_create(info_ind->remote_ip[0], info_ind->remote_port[0],
330 info_ind->nsei, info_ind->nsvci[0], info_ind->bvci,
331 info_ind->mcc, info_ind->mnc, info_ind->lac, info_ind->rac,
332 info_ind->cell_id);
333 if (rc < 0) {
334 LOGP(DL1IF, LOGL_NOTICE, "SGSN not available\n");
335 goto bssgp_failed;
336 }
337
338 bts->cs1 = !!(info_ind->flags & PCU_IF_FLAG_CS1);
339 bts->cs2 = !!(info_ind->flags & PCU_IF_FLAG_CS2);
340 bts->cs3 = !!(info_ind->flags & PCU_IF_FLAG_CS3);
341 bts->cs4 = !!(info_ind->flags & PCU_IF_FLAG_CS4);
342 if (!bts->cs1 && !bts->cs2 && !bts->cs3 && !bts->cs4)
343 bts->cs1 = 1;
344 if (info_ind->t3142) { /* if timer values are set */
345 bts->t3142 = info_ind->t3142;
346 bts->t3169 = info_ind->t3169;
347 bts->t3191 = info_ind->t3191;
348 bts->t3193_msec = info_ind->t3193_10ms * 10;
349 bts->t3195 = info_ind->t3195;
350 bts->n3101 = info_ind->n3101;
351 bts->n3103 = info_ind->n3103;
352 bts->n3105 = info_ind->n3105;
353 }
354 if (info_ind->initial_cs < 1 || info_ind->initial_cs > 4)
355 bts->initial_cs = 1;
356 else
357 bts->initial_cs = info_ind->initial_cs;
358
359 for (trx = 0; trx < 8; trx++) {
360 bts->trx[trx].arfcn = info_ind->trx[trx].arfcn;
361 for (ts = 0; ts < 8; ts++) {
362 if ((info_ind->trx[trx].pdch_mask & (1 << ts))) {
363 /* FIXME: activate dynamically at RLCMAC */
364 if (!bts->trx[trx].pdch[ts].enable)
365 pcu_tx_act_req(trx, ts, 1);
366 bts->trx[trx].pdch[ts].enable = 1;
367 bts->trx[trx].pdch[ts].tsc =
368 info_ind->trx[trx].tsc[ts];
369 LOGP(DL1IF, LOGL_INFO, "PDCH: trx=%d ts=%d\n",
370 trx, ts);
371 } else {
372 if (bts->trx[trx].pdch[ts].enable)
373 pcu_tx_act_req(trx, ts, 0);
374 bts->trx[trx].pdch[ts].enable = 0;
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200375 /* kick all TBF on slot */
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400376 for (tfi = 0; tfi < 32; tfi++) {
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200377 tbf = bts->trx[trx].pdch[ts].ul_tbf[tfi];
378 if (tbf)
379 tbf_free(tbf);
380 tbf = bts->trx[trx].pdch[ts].dl_tbf[tfi];
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400381 if (tbf)
382 tbf_free(tbf);
383 }
384 }
385 }
386 }
387
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200388 return rc;
389}
390
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400391static int pcu_rx_time_ind(struct gsm_pcu_if_time_ind *time_ind)
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200392{
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400393 struct gprs_rlcmac_tbf *tbf;
394 uint32_t elapsed;
395 uint8_t fn13 = time_ind->fn % 13;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200396
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400397 /* omit frame numbers not starting at a MAC block */
398 if (fn13 != 0 && fn13 != 4 && fn13 != 8)
399 return 0;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200400
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400401 LOGP(DL1IF, LOGL_DEBUG, "Time indication received: %d\n",
402 time_ind->fn % 52);
403
404 set_current_fn(time_ind->fn);
405
406 /* check for poll timeout */
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200407 llist_for_each_entry(tbf, &gprs_rlcmac_ul_tbfs, list) {
408 if (tbf->poll_state == GPRS_RLCMAC_POLL_SCHED) {
409 elapsed = (frame_number - tbf->poll_fn) % 2715648;
410 if (elapsed >= 20 && elapsed < 200)
411 gprs_rlcmac_poll_timeout(tbf);
412 }
413 }
414 llist_for_each_entry(tbf, &gprs_rlcmac_dl_tbfs, list) {
415 if (tbf->poll_state == GPRS_RLCMAC_POLL_SCHED) {
416 elapsed = (frame_number - tbf->poll_fn) % 2715648;
417 if (elapsed >= 20 && elapsed < 200)
418 gprs_rlcmac_poll_timeout(tbf);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400419 }
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200420 }
421
422 return 0;
423}
424
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400425int pcu_rx(uint8_t msg_type, struct gsm_pcu_if *pcu_prim)
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200426{
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400427 int rc = 0;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200428
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400429 switch (msg_type) {
430 case PCU_IF_MSG_DATA_IND:
431 rc = pcu_rx_data_ind(&pcu_prim->u.data_ind);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400432 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400433 case PCU_IF_MSG_RTS_REQ:
434 rc = pcu_rx_rts_req(&pcu_prim->u.rts_req);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400435 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400436 case PCU_IF_MSG_RACH_IND:
437 rc = pcu_rx_rach_ind(&pcu_prim->u.rach_ind);
438 break;
439 case PCU_IF_MSG_INFO_IND:
440 rc = pcu_rx_info_ind(&pcu_prim->u.info_ind);
441 break;
442 case PCU_IF_MSG_TIME_IND:
443 rc = pcu_rx_time_ind(&pcu_prim->u.time_ind);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400444 break;
445 default:
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400446 LOGP(DL1IF, LOGL_ERROR, "Received unknwon PCU msg type %d\n",
447 msg_type);
448 rc = -EINVAL;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200449 }
450
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400451 return rc;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200452}