blob: f9768aef6b2a034cc7ca95bb3a2565ba39a27f18 [file] [log] [blame]
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -04001/* Copyright (C) 2015 by Yves Godin <support@nuranwireless.com>
2 * based on:
3 * femto_l1_if.c
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21#include "oc2g_l1_if.h"
22
23#include <string.h>
24#include <errno.h>
25
26#include <nrw/oc2g/oc2g.h>
27#include <nrw/oc2g/gsml1prim.h>
28#include <nrw/oc2g/gsml1const.h>
29#include <nrw/oc2g/gsml1types.h>
30
31#include <osmocom/core/gsmtap.h>
32#include <osmocom/core/talloc.h>
33#include <osmocom/core/timer.h>
Minh-Quang Nguyen4a1796f2017-09-29 08:54:16 -040034#include <osmocom/gsm/protocol/gsm_04_08.h>
35
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -040036#include <gprs_debug.h>
37#include <pcu_l1_if.h>
38#include <osmocom/pcu/pcuif_proto.h>
39#include <bts.h>
40
41extern void *tall_pcu_ctx;
42
43uint32_t l1if_ts_to_hLayer2(uint8_t trx, uint8_t ts)
44{
45 return (ts << 16) | (trx << 24);
46}
47
48/* allocate a msgb containing a GsmL1_Prim_t */
49struct msgb *l1p_msgb_alloc(void)
50{
51 struct msgb *msg = msgb_alloc(sizeof(GsmL1_Prim_t), "l1_prim");
52
53 if (msg)
54 msg->l1h = msgb_put(msg, sizeof(GsmL1_Prim_t));
55
56 return msg;
57}
58
59static int l1if_req_pdch(struct oc2gl1_hdl *fl1h, struct msgb *msg)
60{
61 struct osmo_wqueue *wqueue = &fl1h->write_q[MQ_PDTCH_WRITE];
62
63 if (osmo_wqueue_enqueue(wqueue, msg) != 0) {
64 LOGP(DL1IF, LOGL_ERROR, "PDTCH queue full. dropping message.\n");
65 msgb_free(msg);
66 }
67
68 return 0;
69}
70
71static void *prim_init(GsmL1_Prim_t *prim, GsmL1_PrimId_t id, struct oc2gl1_hdl *gl1)
72{
73 prim->id = id;
74
75 switch (id) {
76 case GsmL1_PrimId_MphInitReq:
77 //prim->u.mphInitReq.hLayer1 = (HANDLE)gl1->hLayer1;
78 break;
79 case GsmL1_PrimId_MphCloseReq:
80 prim->u.mphCloseReq.hLayer1 = (HANDLE)gl1->hLayer1;
81 break;
82 case GsmL1_PrimId_MphConnectReq:
83 prim->u.mphConnectReq.hLayer1 = (HANDLE)gl1->hLayer1;
84 break;
85 case GsmL1_PrimId_MphDisconnectReq:
86 prim->u.mphDisconnectReq.hLayer1 = (HANDLE)gl1->hLayer1;
87 break;
88 case GsmL1_PrimId_MphActivateReq:
89 prim->u.mphActivateReq.hLayer1 = (HANDLE)gl1->hLayer1;
90 break;
91 case GsmL1_PrimId_MphDeactivateReq:
92 prim->u.mphDeactivateReq.hLayer1 = (HANDLE)gl1->hLayer1;
93 break;
94 case GsmL1_PrimId_MphConfigReq:
95 prim->u.mphConfigReq.hLayer1 = (HANDLE)gl1->hLayer1;
96 break;
97 case GsmL1_PrimId_MphMeasureReq:
98 prim->u.mphMeasureReq.hLayer1 = (HANDLE)gl1->hLayer1;
99 break;
100 case GsmL1_PrimId_MphInitCnf:
101 case GsmL1_PrimId_MphCloseCnf:
102 case GsmL1_PrimId_MphConnectCnf:
103 case GsmL1_PrimId_MphDisconnectCnf:
104 case GsmL1_PrimId_MphActivateCnf:
105 case GsmL1_PrimId_MphDeactivateCnf:
106 case GsmL1_PrimId_MphConfigCnf:
107 case GsmL1_PrimId_MphMeasureCnf:
108 break;
109 case GsmL1_PrimId_MphTimeInd:
110 break;
111 case GsmL1_PrimId_MphSyncInd:
112 break;
113 case GsmL1_PrimId_PhEmptyFrameReq:
114 prim->u.phEmptyFrameReq.hLayer1 = (HANDLE)gl1->hLayer1;
115 break;
116 case GsmL1_PrimId_PhDataReq:
117 prim->u.phDataReq.hLayer1 = (HANDLE)gl1->hLayer1;
118 break;
119 case GsmL1_PrimId_PhConnectInd:
120 break;
121 case GsmL1_PrimId_PhReadyToSendInd:
122 break;
123 case GsmL1_PrimId_PhDataInd:
124 break;
125 case GsmL1_PrimId_PhRaInd:
126 break;
127 default:
128 LOGP(DL1IF, LOGL_ERROR, "unknown L1 primitive %u\n", id);
129 break;
130 }
131 return &prim->u;
132}
133
134/* connect PDTCH */
135int l1if_connect_pdch(void *obj, uint8_t ts)
136{
137 struct oc2gl1_hdl *fl1h = obj;
138 struct msgb *msg = l1p_msgb_alloc();
139 GsmL1_MphConnectReq_t *cr;
140
141 cr = prim_init(msgb_l1prim(msg), GsmL1_PrimId_MphConnectReq, fl1h);
142 cr->u8Tn = ts;
143 cr->logChComb = GsmL1_LogChComb_XIII;
144
145 return l1if_req_pdch(fl1h, msg);
146}
147
148static int handle_ph_readytosend_ind(struct oc2gl1_hdl *fl1h,
149 GsmL1_PhReadyToSendInd_t *rts_ind)
150{
151 struct gsm_time g_time;
152 int rc = 0;
153
154 gsm_fn2gsmtime(&g_time, rts_ind->u32Fn);
155
156 DEBUGP(DL1IF, "Rx PH-RTS.ind %02u/%02u/%02u SAPI=%s\n",
157 g_time.t1, g_time.t2, g_time.t3,
158 get_value_string(oc2gbts_l1sapi_names, rts_ind->sapi));
159
160 switch (rts_ind->sapi) {
161 case GsmL1_Sapi_Pdtch:
162 case GsmL1_Sapi_Pacch:
163 rc = pcu_rx_rts_req_pdtch(fl1h->trx_no, rts_ind->u8Tn,
164 rts_ind->u32Fn, rts_ind->u8BlockNbr);
165 case GsmL1_Sapi_Ptcch:
166 // FIXME
167 default:
168 break;
169 }
170
171 return rc;
172}
173
174static void get_meas(struct pcu_l1_meas *meas, const GsmL1_MeasParam_t *l1_meas)
175{
176 meas->rssi = (int8_t) (l1_meas->fRssi);
177 meas->have_rssi = 1;
178 meas->ber = (uint8_t) (l1_meas->fBer * 100);
179 meas->have_ber = 1;
180 meas->bto = (int16_t) (l1_meas->i16BurstTiming);
181 meas->have_bto = 1;
182 meas->link_qual = (int16_t) (l1_meas->fLinkQuality);
183 meas->have_link_qual = 1;
184}
185
186static int handle_ph_data_ind(struct oc2gl1_hdl *fl1h,
187 GsmL1_PhDataInd_t *data_ind, struct msgb *l1p_msg)
188{
189 int rc = 0;
190 struct pcu_l1_meas meas = {0};
191
192 DEBUGP(DL1IF, "Rx PH-DATA.ind %s (hL2 %08x): %s\n",
193 get_value_string(oc2gbts_l1sapi_names, data_ind->sapi),
194 data_ind->hLayer2,
195 osmo_hexdump(data_ind->msgUnitParam.u8Buffer,
196 data_ind->msgUnitParam.u8Size));
197
198 /*
199 * TODO: Add proper bad frame handling here. This could be used
200 * to switch the used CS. Avoid a crash with the PCU right now
201 * feed "0 - 1" amount of data.
202 */
203 if (data_ind->msgUnitParam.u8Size == 0)
204 return -1;
205
206 gsmtap_send(fl1h->gsmtap, data_ind->u16Arfcn | GSMTAP_ARFCN_F_UPLINK,
207 data_ind->u8Tn, GSMTAP_CHANNEL_PACCH, 0,
208 data_ind->u32Fn, 0, 0, data_ind->msgUnitParam.u8Buffer+1,
209 data_ind->msgUnitParam.u8Size-1);
210
211 get_meas(&meas, &data_ind->measParam);
212 bts_update_tbf_ta("PH-DATA", data_ind->u32Fn, fl1h->trx_no,
Minh-Quang Nguyen2ba60842017-11-07 15:17:12 -0500213 data_ind->u8Tn, sign_qta2ta(meas.bto), false);
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -0400214
215 switch (data_ind->sapi) {
216 case GsmL1_Sapi_Pdtch:
217 case GsmL1_Sapi_Pacch:
218 /* drop incomplete UL block */
219 if (data_ind->msgUnitParam.u8Buffer[0]
220 != GsmL1_PdtchPlType_Full)
221 break;
222 /* PDTCH / PACCH frame handling */
223 pcu_rx_data_ind_pdtch(fl1h->trx_no, data_ind->u8Tn,
224 data_ind->msgUnitParam.u8Buffer + 1,
225 data_ind->msgUnitParam.u8Size - 1,
226 data_ind->u32Fn,
227 &meas);
228 break;
229 case GsmL1_Sapi_Ptcch:
230 // FIXME
231 break;
232 default:
233 LOGP(DL1IF, LOGL_NOTICE, "Rx PH-DATA.ind for unknown L1 SAPI %s\n",
234 get_value_string(oc2gbts_l1sapi_names, data_ind->sapi));
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -0400235 break;
236 }
237
238 return rc;
239}
240
241#define MIN_QUAL_RACH 5.0f
242
243static int handle_ph_ra_ind(struct oc2gl1_hdl *fl1h, GsmL1_PhRaInd_t *ra_ind)
244{
245 if (ra_ind->measParam.fLinkQuality < MIN_QUAL_RACH)
246 return 0;
247
Minh-Quang Nguyenba66ea42017-11-15 13:08:30 -0500248 LOGP(DL1IF, LOGL_DEBUG, "PH-RA-IND L1 qta=%d\n", ra_ind->measParam.i16BurstTiming);
249
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -0400250 bts_update_tbf_ta("PH-RA", ra_ind->u32Fn, fl1h->trx_no, ra_ind->u8Tn,
Minh-Quang Nguyenba66ea42017-11-15 13:08:30 -0500251 qta2ta(ra_ind->measParam.i16BurstTiming), true);
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -0400252
253 return 0;
254}
255
256
257/* handle any random indication from the L1 */
258int l1if_handle_l1prim(int wq, struct oc2gl1_hdl *fl1h, struct msgb *msg)
259{
260 GsmL1_Prim_t *l1p = msgb_l1prim(msg);
261 int rc = 0;
262
263 LOGP(DL1IF, LOGL_DEBUG, "Rx L1 prim %s on queue %d\n",
264 get_value_string(oc2gbts_l1prim_names, l1p->id), wq);
265
266 switch (l1p->id) {
267#if 0
268 case GsmL1_PrimId_MphTimeInd:
269 rc = handle_mph_time_ind(fl1h, &l1p->u.mphTimeInd);
270 break;
271 case GsmL1_PrimId_MphSyncInd:
272 break;
273 case GsmL1_PrimId_PhConnectInd:
274 break;
275#endif
276 case GsmL1_PrimId_PhReadyToSendInd:
277 rc = handle_ph_readytosend_ind(fl1h, &l1p->u.phReadyToSendInd);
278 break;
279 case GsmL1_PrimId_PhDataInd:
280 rc = handle_ph_data_ind(fl1h, &l1p->u.phDataInd, msg);
281 break;
282 case GsmL1_PrimId_PhRaInd:
283 rc = handle_ph_ra_ind(fl1h, &l1p->u.phRaInd);
284 break;
285 default:
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -0400286 break;
287 }
288
289 msgb_free(msg);
290
291 return rc;
292}
293
294int l1if_handle_sysprim(struct oc2gl1_hdl *fl1h, struct msgb *msg)
295{
296 return -ENOTSUP;
297}
298
299/* send packet data request to L1 */
300int l1if_pdch_req(void *obj, uint8_t ts, int is_ptcch, uint32_t fn,
301 uint16_t arfcn, uint8_t block_nr, uint8_t *data, uint8_t len)
302{
303 struct oc2gl1_hdl *fl1h = obj;
304 struct msgb *msg;
305 GsmL1_Prim_t *l1p;
306 GsmL1_PhDataReq_t *data_req;
307 GsmL1_MsgUnitParam_t *msu_param;
308 struct gsm_time g_time;
309
310 gsm_fn2gsmtime(&g_time, fn);
311
312 DEBUGP(DL1IF, "TX packet data %02u/%02u/%02u is_ptcch=%d ts=%d "
313 "block_nr=%d, arfcn=%d, len=%d\n", g_time.t1, g_time.t2,
314 g_time.t3, is_ptcch, ts, block_nr, arfcn, len);
315
316 msg = l1p_msgb_alloc();
317 l1p = msgb_l1prim(msg);
318 l1p->id = GsmL1_PrimId_PhDataReq;
319 data_req = &l1p->u.phDataReq;
320 data_req->hLayer1 = (HANDLE)fl1h->hLayer1;
321 data_req->sapi = (is_ptcch) ? GsmL1_Sapi_Ptcch : GsmL1_Sapi_Pdtch;
322 data_req->subCh = GsmL1_SubCh_NA;
323 data_req->u8BlockNbr = block_nr;
324 data_req->u8Tn = ts;
325 data_req->u32Fn = fn;
326 msu_param = &data_req->msgUnitParam;
327 msu_param->u8Size = len;
328 memcpy(msu_param->u8Buffer, data, len);
329
330 gsmtap_send(fl1h->gsmtap, arfcn, data_req->u8Tn, GSMTAP_CHANNEL_PACCH,
331 0, data_req->u32Fn, 0, 0,
332 data_req->msgUnitParam.u8Buffer,
333 data_req->msgUnitParam.u8Size);
334
335
336 /* transmit */
337 if (osmo_wqueue_enqueue(&fl1h->write_q[MQ_PDTCH_WRITE], msg) != 0) {
338 LOGP(DL1IF, LOGL_ERROR, "PDTCH queue full. dropping message.\n");
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -0400339 msgb_free(msg);
340 }
341
342 return 0;
343}
344
345void *l1if_open_pdch(uint8_t trx_no, uint32_t hlayer1)
346{
347 struct oc2gl1_hdl *fl1h;
348 int rc;
349
350 fl1h = talloc_zero(tall_pcu_ctx, struct oc2gl1_hdl);
351 if (!fl1h)
352 return NULL;
353
354 fl1h->hLayer1 = hlayer1;
355 fl1h->trx_no = trx_no;
356 /* hardware queues are numbered starting from 0 */
357 fl1h->hw_info.trx_nr = trx_no;
358
359 DEBUGP(DL1IF, "PCU: Using TRX HW#%u\n", fl1h->hw_info.trx_nr);
360
361 rc = l1if_transport_open(MQ_PDTCH_WRITE, fl1h);
362 if (rc < 0) {
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -0400363 talloc_free(fl1h);
364 return NULL;
365 }
366
367 fl1h->gsmtap = gsmtap_source_init("localhost", GSMTAP_UDP_PORT, 1);
368 if (fl1h->gsmtap)
369 gsmtap_source_add_sink(fl1h->gsmtap);
370
371 return fl1h;
372}
373
374int l1if_close_pdch(void *obj)
375{
376 struct oc2gl1_hdl *fl1h = obj;
377 if (fl1h)
378 l1if_transport_close(MQ_PDTCH_WRITE, fl1h);
379 talloc_free(fl1h);
380 return 0;
381}
382