blob: 857278615edbfb9106eeb68ca36e17fcd9ddc729 [file] [log] [blame]
Andreas Eversberga23c7ee2012-12-18 10:47:28 +01001
2#include <string.h>
3#include <errno.h>
4
5#include <sysmocom/femtobts/superfemto.h>
6#include <sysmocom/femtobts/gsml1prim.h>
7#include <sysmocom/femtobts/gsml1const.h>
8#include <sysmocom/femtobts/gsml1types.h>
9
10#include <osmocom/core/gsmtap.h>
11#include <osmocom/core/talloc.h>
12#include <osmocom/core/timer.h>
13#include <sysmo_l1_if.h>
14#include <gprs_debug.h>
15#include <pcu_l1_if.h>
16
Andreas Eversberg7a5a67a2013-01-16 08:43:24 +010017extern void *tall_pcu_ctx;
18
Andreas Eversberga23c7ee2012-12-18 10:47:28 +010019uint32_t l1if_ts_to_hLayer2(uint8_t trx, uint8_t ts)
20{
21 return (ts << 16) | (trx << 24);
22}
23
24/* allocate a msgb containing a GsmL1_Prim_t */
25struct msgb *l1p_msgb_alloc(void)
26{
27 struct msgb *msg = msgb_alloc(sizeof(GsmL1_Prim_t), "l1_prim");
28
29 if (msg)
30 msg->l1h = msgb_put(msg, sizeof(GsmL1_Prim_t));
31
32 return msg;
33}
34
35static int l1if_req_pdch(struct femtol1_hdl *fl1h, struct msgb *msg)
36{
37 struct osmo_wqueue *wqueue = &fl1h->write_q[MQ_PDTCH_WRITE];
38
Holger Hans Peter Freytherd1cb41b2015-05-22 10:42:32 +080039 if (osmo_wqueue_enqueue(wqueue, msg) != 0) {
40 LOGP(DL1IF, LOGL_ERROR, "PDTCH queue full. dropping message.\n");
41 msgb_free(msg);
42 }
Andreas Eversberga23c7ee2012-12-18 10:47:28 +010043
44 return 0;
45}
46
47static void *prim_init(GsmL1_Prim_t *prim, GsmL1_PrimId_t id, struct femtol1_hdl *gl1)
48{
49 prim->id = id;
50
51 /* for some reason the hLayer1 field is not always at the same position
52 * in the GsmL1_Prim_t, so we have to have this ugly case statement here... */
53 switch (id) {
54 case GsmL1_PrimId_MphInitReq:
55 //prim->u.mphInitReq.hLayer1 = gl1->hLayer1;
56 break;
57 case GsmL1_PrimId_MphCloseReq:
58 prim->u.mphCloseReq.hLayer1 = gl1->hLayer1;
59 break;
60 case GsmL1_PrimId_MphConnectReq:
61 prim->u.mphConnectReq.hLayer1 = gl1->hLayer1;
62 break;
63 case GsmL1_PrimId_MphDisconnectReq:
64 prim->u.mphDisconnectReq.hLayer1 = gl1->hLayer1;
65 break;
66 case GsmL1_PrimId_MphActivateReq:
67 prim->u.mphActivateReq.hLayer1 = gl1->hLayer1;
68 break;
69 case GsmL1_PrimId_MphDeactivateReq:
70 prim->u.mphDeactivateReq.hLayer1 = gl1->hLayer1;
71 break;
72 case GsmL1_PrimId_MphConfigReq:
73 prim->u.mphConfigReq.hLayer1 = gl1->hLayer1;
74 break;
75 case GsmL1_PrimId_MphMeasureReq:
76 prim->u.mphMeasureReq.hLayer1 = gl1->hLayer1;
77 break;
78 case GsmL1_PrimId_MphInitCnf:
79 case GsmL1_PrimId_MphCloseCnf:
80 case GsmL1_PrimId_MphConnectCnf:
81 case GsmL1_PrimId_MphDisconnectCnf:
82 case GsmL1_PrimId_MphActivateCnf:
83 case GsmL1_PrimId_MphDeactivateCnf:
84 case GsmL1_PrimId_MphConfigCnf:
85 case GsmL1_PrimId_MphMeasureCnf:
86 break;
87 case GsmL1_PrimId_MphTimeInd:
88 break;
89 case GsmL1_PrimId_MphSyncInd:
90 break;
91 case GsmL1_PrimId_PhEmptyFrameReq:
92 prim->u.phEmptyFrameReq.hLayer1 = gl1->hLayer1;
93 break;
94 case GsmL1_PrimId_PhDataReq:
95 prim->u.phDataReq.hLayer1 = gl1->hLayer1;
96 break;
97 case GsmL1_PrimId_PhConnectInd:
98 break;
99 case GsmL1_PrimId_PhReadyToSendInd:
100 break;
101 case GsmL1_PrimId_PhDataInd:
102 break;
103 case GsmL1_PrimId_PhRaInd:
104 break;
105 default:
106 LOGP(DL1IF, LOGL_ERROR, "unknown L1 primitive %u\n", id);
107 break;
108 }
109 return &prim->u;
110}
111
112struct sapi_dir {
113 GsmL1_Sapi_t sapi;
114 GsmL1_Dir_t dir;
115};
116
117static const struct sapi_dir pdtch_sapis[] = {
118 { GsmL1_Sapi_Pdtch, GsmL1_Dir_TxDownlink },
119 { GsmL1_Sapi_Pdtch, GsmL1_Dir_RxUplink },
120 { GsmL1_Sapi_Ptcch, GsmL1_Dir_TxDownlink },
121 { GsmL1_Sapi_Prach, GsmL1_Dir_RxUplink },
122#if 0
123 { GsmL1_Sapi_Ptcch, GsmL1_Dir_RxUplink },
124 { GsmL1_Sapi_Pacch, GsmL1_Dir_TxDownlink },
125#endif
126};
127
128
129/* connect PDTCH */
130int l1if_connect_pdch(void *obj, uint8_t ts)
131{
132 struct femtol1_hdl *fl1h = obj;
133 struct msgb *msg = l1p_msgb_alloc();
134 GsmL1_MphConnectReq_t *cr;
135
136 cr = prim_init(msgb_l1prim(msg), GsmL1_PrimId_MphConnectReq, fl1h);
137 cr->u8Tn = ts;
138 cr->logChComb = GsmL1_LogChComb_XIII;
139
140 return l1if_req_pdch(fl1h, msg);
141}
142
143static int handle_ph_readytosend_ind(struct femtol1_hdl *fl1h,
144 GsmL1_PhReadyToSendInd_t *rts_ind)
145{
146 struct gsm_time g_time;
147 int rc = 0;
148
149 gsm_fn2gsmtime(&g_time, rts_ind->u32Fn);
150
151 DEBUGP(DL1IF, "Rx PH-RTS.ind %02u/%02u/%02u SAPI=%s\n",
152 g_time.t1, g_time.t2, g_time.t3,
153 get_value_string(femtobts_l1sapi_names, rts_ind->sapi));
154
155 switch (rts_ind->sapi) {
156 case GsmL1_Sapi_Pdtch:
157 case GsmL1_Sapi_Pacch:
158 rc = pcu_rx_rts_req_pdtch((long)fl1h->priv, rts_ind->u8Tn,
159 rts_ind->u16Arfcn, rts_ind->u32Fn, rts_ind->u8BlockNbr);
160 case GsmL1_Sapi_Ptcch:
161 // FIXME
162 default:
163 break;
164 }
165
166 return rc;
167}
168
Jacob Erlbeck20f6fd12015-06-08 11:05:45 +0200169static void get_meas(struct pcu_l1_meas *meas, const GsmL1_MeasParam_t *l1_meas)
170{
171 meas->rssi = (int8_t) (l1_meas->fRssi);
172 meas->have_rssi = 1;
173 meas->ber = (uint8_t) (l1_meas->fBer * 100);
174 meas->have_ber = 1;
175 meas->bto = (int16_t) (l1_meas->i16BurstTiming);
176 meas->have_bto = 1;
177 meas->link_qual = (int16_t) (l1_meas->fLinkQuality);
178 meas->have_link_qual = 1;
179}
180
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100181static int handle_ph_data_ind(struct femtol1_hdl *fl1h,
182 GsmL1_PhDataInd_t *data_ind, struct msgb *l1p_msg)
183{
184 int rc = 0;
Jacob Erlbeck20f6fd12015-06-08 11:05:45 +0200185 struct pcu_l1_meas meas = {0};
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100186
Jacob Erlbecka41a71e2015-02-26 15:10:58 +0100187 DEBUGP(DL1IF, "Rx PH-DATA.ind %s (hL2 %08x): %s\n",
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100188 get_value_string(femtobts_l1sapi_names, data_ind->sapi),
189 data_ind->hLayer2,
190 osmo_hexdump(data_ind->msgUnitParam.u8Buffer,
191 data_ind->msgUnitParam.u8Size));
192
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200193 pcu_rx_block_time(data_ind->u16Arfcn, data_ind->u32Fn, data_ind->u8Tn);
194
Holger Hans Peter Freytherf3405e52014-06-16 21:51:36 +0200195 /*
196 * TODO: Add proper bad frame handling here. This could be used
197 * to switch the used CS. Avoid a crash with the PCU right now
198 * feed "0 - 1" amount of data.
199 */
200 if (data_ind->msgUnitParam.u8Size == 0)
201 return -1;
202
203 gsmtap_send(fl1h->gsmtap, data_ind->u16Arfcn | GSMTAP_ARFCN_F_UPLINK,
204 data_ind->u8Tn, GSMTAP_CHANNEL_PACCH, 0,
205 data_ind->u32Fn, 0, 0, data_ind->msgUnitParam.u8Buffer+1,
206 data_ind->msgUnitParam.u8Size-1);
207
Jacob Erlbeck20f6fd12015-06-08 11:05:45 +0200208 get_meas(&meas, &data_ind->measParam);
Holger Hans Peter Freytherf3405e52014-06-16 21:51:36 +0200209
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100210 switch (data_ind->sapi) {
211 case GsmL1_Sapi_Pdtch:
212 case GsmL1_Sapi_Pacch:
213 /* drop incomplete UL block */
214 if (data_ind->msgUnitParam.u8Buffer[0]
215 != GsmL1_PdtchPlType_Full)
216 break;
217 /* PDTCH / PACCH frame handling */
218 pcu_rx_data_ind_pdtch((long)fl1h->priv, data_ind->u8Tn,
219 data_ind->msgUnitParam.u8Buffer + 1,
220 data_ind->msgUnitParam.u8Size - 1,
Andreas Eversberg570b44b2013-03-16 16:15:01 +0100221 data_ind->u32Fn,
Jacob Erlbeck20f6fd12015-06-08 11:05:45 +0200222 &meas);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100223 break;
224 case GsmL1_Sapi_Ptcch:
225 // FIXME
226 break;
227 default:
228 LOGP(DL1IF, LOGL_NOTICE, "Rx PH-DATA.ind for unknown L1 SAPI %s\n",
229 get_value_string(femtobts_l1sapi_names, data_ind->sapi));
230 break;
231 }
232
233 return rc;
234}
235
236#define MIN_QUAL_RACH 5.0f
237
238static int handle_ph_ra_ind(struct femtol1_hdl *fl1h, GsmL1_PhRaInd_t *ra_ind)
239{
240 uint8_t acc_delay;
241
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200242 pcu_rx_ra_time(ra_ind->u16Arfcn, ra_ind->u32Fn, ra_ind->u8Tn);
243
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100244 if (ra_ind->measParam.fLinkQuality < MIN_QUAL_RACH)
245 return 0;
246
247 DEBUGP(DL1IF, "Rx PH-RA.ind");
248
249 /* check for under/overflow / sign */
250 if (ra_ind->measParam.i16BurstTiming < 0)
251 acc_delay = 0;
252 else
253 acc_delay = ra_ind->measParam.i16BurstTiming >> 2;
Jacob Erlbeckb3f713b2015-06-08 09:30:31 +0200254
255 LOGP(DL1IF, LOGL_NOTICE, "got (P)RACH request, TA = %u (ignored)\n",
256 acc_delay);
257
258#warning "The (P)RACH request is just dropped here"
259
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100260#if 0
261 if (acc_delay > bts->max_ta) {
262 LOGP(DL1C, LOGL_INFO, "ignoring RACH request %u > max_ta(%u)\n",
263 acc_delay, btsb->max_ta);
264 return 0;
265 }
266#endif
267
268 return 0;
269}
270
271
272/* handle any random indication from the L1 */
273int l1if_handle_l1prim(int wq, struct femtol1_hdl *fl1h, struct msgb *msg)
274{
275 GsmL1_Prim_t *l1p = msgb_l1prim(msg);
276 int rc = 0;
277
278 LOGP(DL1IF, LOGL_DEBUG, "Rx L1 prim %s on queue %d\n",
279 get_value_string(femtobts_l1prim_names, l1p->id), wq);
280
281 switch (l1p->id) {
282#if 0
283 case GsmL1_PrimId_MphTimeInd:
284 rc = handle_mph_time_ind(fl1h, &l1p->u.mphTimeInd);
285 break;
286 case GsmL1_PrimId_MphSyncInd:
287 break;
288 case GsmL1_PrimId_PhConnectInd:
289 break;
290#endif
291 case GsmL1_PrimId_PhReadyToSendInd:
292 rc = handle_ph_readytosend_ind(fl1h, &l1p->u.phReadyToSendInd);
293 break;
294 case GsmL1_PrimId_PhDataInd:
295 rc = handle_ph_data_ind(fl1h, &l1p->u.phDataInd, msg);
296 break;
297 case GsmL1_PrimId_PhRaInd:
298 rc = handle_ph_ra_ind(fl1h, &l1p->u.phRaInd);
299 break;
300 default:
301 break;
302 }
303
304 msgb_free(msg);
305
306 return rc;
307}
308
309int l1if_handle_sysprim(struct femtol1_hdl *fl1h, struct msgb *msg)
310{
311 return -ENOTSUP;
312}
313
314/* send packet data request to L1 */
315int l1if_pdch_req(void *obj, uint8_t ts, int is_ptcch, uint32_t fn,
316 uint16_t arfcn, uint8_t block_nr, uint8_t *data, uint8_t len)
317{
318 struct femtol1_hdl *fl1h = obj;
319 struct msgb *msg;
320 GsmL1_Prim_t *l1p;
321 GsmL1_PhDataReq_t *data_req;
322 GsmL1_MsgUnitParam_t *msu_param;
323 struct gsm_time g_time;
324
325 gsm_fn2gsmtime(&g_time, fn);
326
327 DEBUGP(DL1IF, "TX packet data %02u/%02u/%02u is_ptcch=%d ts=%d "
328 "block_nr=%d, arfcn=%d, len=%d\n", g_time.t1, g_time.t2,
329 g_time.t3, is_ptcch, ts, block_nr, arfcn, len);
330
331 msg = l1p_msgb_alloc();
332 l1p = msgb_l1prim(msg);
333 l1p->id = GsmL1_PrimId_PhDataReq;
334 data_req = &l1p->u.phDataReq;
335 data_req->hLayer1 = fl1h->hLayer1;
336 data_req->sapi = (is_ptcch) ? GsmL1_Sapi_Ptcch : GsmL1_Sapi_Pdtch;
337 data_req->subCh = GsmL1_SubCh_NA;
338 data_req->u8BlockNbr = block_nr;
339 data_req->u8Tn = ts;
340 data_req->u32Fn = fn;
341 msu_param = &data_req->msgUnitParam;
342 msu_param->u8Size = len;
343 memcpy(msu_param->u8Buffer, data, len);
344
345 gsmtap_send(fl1h->gsmtap, arfcn, data_req->u8Tn, GSMTAP_CHANNEL_PACCH,
346 0, data_req->u32Fn, 0, 0,
347 data_req->msgUnitParam.u8Buffer,
348 data_req->msgUnitParam.u8Size);
349
350
351 /* transmit */
Holger Hans Peter Freytherd1cb41b2015-05-22 10:42:32 +0800352 if (osmo_wqueue_enqueue(&fl1h->write_q[MQ_PDTCH_WRITE], msg) != 0) {
353 LOGP(DL1IF, LOGL_ERROR, "PDTCH queue full. dropping message.\n");
354 msgb_free(msg);
355 }
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100356
357 return 0;
358}
359
360void *l1if_open_pdch(void *priv, uint32_t hlayer1)
361{
362 struct femtol1_hdl *fl1h;
363 int rc;
364
Andreas Eversberg7a5a67a2013-01-16 08:43:24 +0100365 fl1h = talloc_zero(tall_pcu_ctx, struct femtol1_hdl);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100366 if (!fl1h)
367 return NULL;
368
369 fl1h->hLayer1 = hlayer1;
370 fl1h->priv = priv;
371 fl1h->clk_cal = 0;
372 /* default clock source: OCXO */
373 fl1h->clk_src = SuperFemto_ClkSrcId_Ocxo;
374
375 rc = l1if_transport_open(MQ_PDTCH_WRITE, fl1h);
376 if (rc < 0) {
377 talloc_free(fl1h);
378 return NULL;
379 }
380
381 fl1h->gsmtap = gsmtap_source_init("localhost", GSMTAP_UDP_PORT, 1);
382 if (fl1h->gsmtap)
383 gsmtap_source_add_sink(fl1h->gsmtap);
384
385 return fl1h;
386}
387
Andreas Eversberg0f4541b2013-01-16 09:17:24 +0100388int l1if_close_pdch(void *obj)
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100389{
390 struct femtol1_hdl *fl1h = obj;
391 if (fl1h)
392 l1if_transport_close(MQ_PDTCH_WRITE, fl1h);
Andreas Eversberg0f4541b2013-01-16 09:17:24 +0100393 talloc_free(fl1h);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100394 return 0;
395}
396