blob: b36e7c18fd4cc021770a6af048b689977c19eee4 [file] [log] [blame]
Harald Welte78861c02020-05-14 13:28:07 +02001/* GSM A-bis TRAU frame synchronization as per TS 48.060 / 48.061 */
2
3/* (C) 2020 by Harald Welte <laforge@gnumonks.org>
4 * All Rights Reserved
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <stdint.h>
24
25#include <osmocom/core/msgb.h>
26#include <osmocom/core/bits.h>
27#include <osmocom/core/fsm.h>
28
29#include "ubit_buf.h"
30#include <osmocom/trau/trau_sync.h>
31
32#define S(x) (1 << (x))
33
34#define MAX_TRAU_BYTES 40
35
36#define T_SYNC 1
37
38struct sync_pattern {
39 /* provided by user */
40 const char *name; /*!< human-readable name */
41 const uint8_t byte_pattern[MAX_TRAU_BYTES]; /*!< bytes to match against */
42 const uint8_t byte_mask[MAX_TRAU_BYTES]; /*!< mask applied before matching */
43 uint8_t byte_len; /*!< length of mask in bytes */
44
45 /* generated by code */
46 ubit_t ubit_pattern[MAX_TRAU_BYTES*8]; /*!< bits to match against */
47 ubit_t ubit_mask[MAX_TRAU_BYTES*8]; /*!< mask applied before matching */
48 uint8_t bitcount; /*!< number of high bits in mask */
49};
50
51static struct sync_pattern sync_patterns[] = {
52 [OSMO_TRAU_SYNCP_16_FR_EFR] = {
53 /* TS 08.60 Section 4.8.1 */
54 .name = "FR/EFR",
55 .byte_pattern = {
56 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
57 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
58 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
59 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
60 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
61 },
62 .byte_mask = {
63 0xff, 0xff, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
64 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
65 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
66 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
67 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
68 },
69 .byte_len = 40,
70 },
71 [OSMO_TRAU_SYNCP_8_HR] = {
72 /* TS 08.61 Section 6.8.2.1.1 */
73 .name = "HR8",
74 .byte_pattern = {
75 0x00, 0x80, 0x40, 0x80,
76 0x80, 0x80, 0x80, 0x80,
77 0x80, 0x80, 0x80, 0x80,
78 0x80, 0x80, 0x80, 0x80,
79 0x80, 0x80, 0x80, 0x80,
80 },
81 .byte_mask = {
82 0xff, 0x80, 0xC0, 0x80,
83 0x80, 0x80, 0x80, 0x80,
84 0x80, 0x80, 0x80, 0x80,
85 0x80, 0x80, 0x80, 0x80,
86 0x80, 0x80, 0x80, 0x80,
87 },
88 .byte_len = 20,
89 },
90 [OSMO_TRAU_SYNCP_8_AMR_LOW] = {
91 /* TS 08.61 Section 6.8.2.1.2 */
92 /* The frame synchronisation for No_Speech frames and the speech frames of the three lower codec modes */
93 .name = "AMR8_LOW",
94 .byte_pattern = {
95 0x00, 0x80, 0x80, 0x40,
96 0x80, 0x80, 0x80, 0x80,
97 0x80, 0x80, 0x80, 0x80,
98 0x80, 0x80, 0x80, 0x80,
99 0x80, 0x80, 0x80, 0x80,
100 },
101 .byte_mask = {
102 0xff, 0x80, 0x80, 0xC0,
103 0x80, 0x80, 0x80, 0x80,
104 0x80, 0x80, 0x80, 0x80,
105 0x80, 0x80, 0x80, 0x80,
106 0x80, 0x80, 0x80, 0x80,
107 },
108 .byte_len = 20,
109 },
110 [OSMO_TRAU_SYNCP_8_AMR_6K7] = {
111 /* The frame synchronisation for the speech frames for codec mode 6,70 kBit/s */
112 .name = "AMR8_67",
113 .byte_pattern = {
114 0x00, 0x80, 0x80, 0x80,
115 0x80, 0x00, 0x80, 0x00,
116 0x80, 0x00, 0x80, 0x00,
117 0x80, 0x00, 0x80, 0x00,
118 0x80, 0x00, 0x80, 0x00,
119 },
120 .byte_mask = {
121 0xff, 0x80, 0x80, 0x80,
122 0x80, 0x80, 0x80, 0x00,
123 0x80, 0x00, 0x80, 0x00,
124 0x80, 0x00, 0x80, 0x00,
125 0x80, 0x00, 0x80, 0x00,
126 },
127 .byte_len = 20
128 },
129 [OSMO_TRAU_SYNCP_8_AMR_7K4] = {
130 /* The frame synchronisation for the speech frames for codec mode 7,40 kBit/s */
131 .name = "AMR8_74",
132 .byte_pattern = {
133 0x20, 0x00, 0x80, 0x00,
134 0x00, 0x00, 0x00, 0x00,
135 0x00, 0x00, 0x00, 0x00,
136 0x00, 0x00, 0x00, 0x00,
137 0x00, 0x00, 0x00, 0x00,
138 },
139 .byte_mask = {
140 0xe0, 0x80, 0x80, 0x80,
141 0x00, 0x00, 0x00, 0x00,
142 0x00, 0x00, 0x00, 0x00,
143 0x00, 0x00, 0x00, 0x00,
144 0x00, 0x00, 0x00, 0x00,
145 },
146 .byte_len = 20,
147 },
Harald Welte9d90eb92022-11-29 20:46:52 +0100148 [OSMO_TRAU_SYNCP_V110] = {
149 /* See Table 2 of ITU-T V.110 */
150 .name = "V110",
151 .byte_pattern = {
152 0x00, 0x80, 0x80, 0x80, 0x80,
153 0x80, 0x80, 0x80, 0x80, 0x80,
154 },
155 .byte_mask = {
156 0xff, 0x80, 0x80, 0x80, 0x80,
157 0x80, 0x80, 0x80, 0x80, 0x80,
158 },
159 .byte_len = 10,
160 },
Harald Welte78861c02020-05-14 13:28:07 +0200161};
162
163#if 0
164static struct sync_pattern rbs_ccu_sync_ind_16_pattern = {
165 .name ="RBS_CCU_SYNC_IND_16",
166 .byte_pattern = {
167 0x00, 0x00, 0x01, 0x00,
168 0x00, 0x00, 0x01, 0x00,
169 0x01, 0x00, 0x01, 0x00,
170 },
171 .byte_mask = {
172 0xff, 0xff, 0x01, 0x00,
173 0x01, 0x00, 0x01, 0x00,
174 0x01, 0x00, 0x01, 0x00,
175 },
176};
177
178static struct sync_pattern rbs_ccu_data_ind_16_pattern = {
179 .name ="RBS_CCU_DATA_IND_16",
180 .byte_pattern = {
181 0x00, 0x00, 0x01, 0x00,
182 },
183 .byte_mask = {
184 0xff, 0xff, 0x01, 0x00,
185 },
186};
187#endif
188
189
190static void expand_sync_pattern(struct sync_pattern *pat)
191{
192 osmo_pbit2ubit(pat->ubit_pattern, pat->byte_pattern, pat->byte_len*8);
193 osmo_pbit2ubit(pat->ubit_mask, pat->byte_mask, pat->byte_len*8);
194}
195
196static unsigned int count_one_bits(const ubit_t *in, unsigned int in_bits)
197{
198 unsigned int i, count = 0;
199
200 for (i = 0; i < in_bits; i++) {
201 if (in[i])
202 count++;
203 }
204 return count;
205}
206
207static void sync_pattern_register(struct sync_pattern *p)
208{
209 expand_sync_pattern(p);
210 p->bitcount = count_one_bits(p->ubit_mask, p->byte_len*8);
211}
212
213#if 0
214/*! correlate pattern with unpacked bits from buffer.
215 * \param[in] pattern sync_pattern against which we shall compare
216 * \param[in] bits unpacked bits to compare against pattern
217 * \param[in] num_bits number of unpacked bits
218 * \returns number of bits not matching pattern; -1 if insufficient bits available. */
219static int correlate_pattern_ubits(const struct sync_pattern *pattern,
220 const ubit_t *bits, size_t num_bits)
221{
222 int i, num_wrong = 0;
223
224 if (num_bits < pattern->byte_len*8)
225 return -1; /* insufficient data */
226
227 for (i = 0; i < pattern->byte_len *8; i++) {
228 /* if mask doesn't contain '1', we can skip this octet */
229 if (!pattern->ubit_mask)
230 continue;
231 if (bits[i] != pattern->ubit_pattern[i])
232 num_wrong++;
233 }
234
235 return num_wrong;
236}
237#endif
238
239struct trau_rx_sync_state {
240 /*! call-back to be called for every TRAU frame (called with
241 * bits=NULL in case of frame sync loss */
242 frame_out_cb_t out_cb;
243 /*! opaque user data; passed to out_cb */
244 void *user_data;
245
246 /*! history of received bits */
247 ubit_t history[MAX_TRAU_BYTES*8+1]; /* +1 not required, but helps to expose bugs */
248 /*! index of next-to-be-written ubit in history */
249 unsigned int history_idx;
250 /*! the pattern we are trying to sync to */
251 const struct sync_pattern *pattern;
252 /*! number of consecutive frames without sync */
253 unsigned int num_consecutive_errors;
254};
255
256/* correlate the history (up to the last received bit) against the pattern */
257static int correlate_history_against_pattern(struct trau_rx_sync_state *tss)
258{
259 const struct sync_pattern *pattern = tss->pattern;
260 int i, start, num_wrong = 0;
261
262 /* compute index of first bit in history array */
263 start = (ARRAY_SIZE(tss->history) + tss->history_idx - pattern->byte_len*8)
264 % ARRAY_SIZE(tss->history);
265
266 OSMO_ASSERT(ARRAY_SIZE(tss->history) >= pattern->byte_len*8);
267
268 for (i = 0; i < pattern->byte_len*8; i++) {
269 unsigned int pos = (start + i) % ARRAY_SIZE(tss->history);
270
271 /* if mask doesn't contain '1', we can skip this octet */
272 if (!pattern->ubit_mask[i])
273 continue;
274 if (tss->history[pos] != pattern->ubit_pattern[i])
275 num_wrong++;
276 }
277
278 return num_wrong;
279}
280
281/* add (append) one ubit to the history; wrap as needed */
282static void rx_history_add_bit(struct trau_rx_sync_state *tss, ubit_t bit)
283{
284 tss->history[tss->history_idx] = bit;
285 /* simply wrap around at the end */
286 tss->history_idx = (tss->history_idx + 1) % ARRAY_SIZE(tss->history);
287}
288
289/* append bits to history. We assume that this does NOT wrap */
290static void rx_history_add_bits(struct trau_rx_sync_state *tss, const ubit_t *bits, size_t n_bits)
291{
292 unsigned int frame_bits_remaining = tss->pattern->byte_len*8 - tss->history_idx;
293 OSMO_ASSERT(frame_bits_remaining >= n_bits);
294 memcpy(&tss->history[tss->history_idx], bits, n_bits);
295 tss->history_idx = tss->history_idx + n_bits;
296}
297
298/* align the history, i.e. next received bit is start of frame */
299static void rx_history_align(struct trau_rx_sync_state *tss)
300{
301 ubit_t tmp[sizeof(tss->history)];
302 size_t history_size = sizeof(tss->history);
303 size_t pattern_bits = tss->pattern->byte_len*8;
304 size_t first_bit = (history_size + tss->history_idx - pattern_bits) % history_size;
305 int i;
306
307 /* we need to shift the last received frame to the start of the history buffer;
308 * do this in two steps: First copy to a local buffer on the stack, using modulo-arithmetic
309 * as index into the history. Second, copy it back to history */
310
311 for (i = 0; i < pattern_bits; i++)
312 tmp[i] = tss->history[(first_bit + i) % history_size];
313
314 memcpy(tss->history, tmp, history_size);
315 tss->history_idx = 0;
316}
317
318enum trau_sync_state {
319 WAIT_FRAME_ALIGN,
320 FRAME_ALIGNED,
321 /* if at least 3 consecutive frames with each at least one framing error have been received */
322 FRAME_ALIGNMENT_LOST,
323};
324
325enum trau_sync_event {
326 TRAUSYNC_E_RESET,
327 /*! a buffer of bits was received (msgb with ubits) */
328 TRAUSYNC_E_RX_BITS,
329};
330
331static const struct value_string trau_sync_event_names[] = {
332 { TRAUSYNC_E_RESET, "RESET" },
333 { TRAUSYNC_E_RX_BITS, "RX_BITS" },
334 { 0, NULL }
335};
336
337
338static void trau_sync_wait_align(struct osmo_fsm_inst *fi, uint32_t event, void *data)
339{
340 struct trau_rx_sync_state *tss = (struct trau_rx_sync_state *) fi->priv;
341 struct ubit_buf *ubb;
342
343 switch (event) {
344 case TRAUSYNC_E_RX_BITS:
345 ubb = data;
346 /* append every bit individually + check if we have sync */
347 while (ubb_length(ubb) > 0) {
348 ubit_t bit = ubb_pull_ubit(ubb);
349 int rc;
350
351 rx_history_add_bit(tss, bit);
352 rc = correlate_history_against_pattern(tss);
353 if (!rc) {
354 osmo_fsm_inst_state_chg(fi, FRAME_ALIGNED, 0, 0);
355 /* treat remainder of input bits in correct state */
356 osmo_fsm_inst_dispatch(fi, TRAUSYNC_E_RX_BITS, ubb);
357 return;
358 }
359 }
360 break;
361 default:
362 OSMO_ASSERT(0);
363 }
364}
365
366static void trau_sync_aligned_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
367{
368 struct trau_rx_sync_state *tss = (struct trau_rx_sync_state *) fi->priv;
369 /* dispatch aligned frame to user */
370 rx_history_align(tss);
371 tss->out_cb(tss->user_data, tss->history, tss->pattern->byte_len*8);
372}
373
374static void trau_sync_aligned(struct osmo_fsm_inst *fi, uint32_t event, void *data)
375{
376 struct trau_rx_sync_state *tss = (struct trau_rx_sync_state *) fi->priv;
377 struct ubit_buf *ubb;
378 int rc;
379
380 switch (event) {
381 case TRAUSYNC_E_RX_BITS:
382 ubb = data;
383 while (ubb_length(ubb)) {
384 unsigned int frame_bits_remaining = tss->pattern->byte_len*8 - tss->history_idx;
385 if (ubb_length(ubb) < frame_bits_remaining) {
386 /* frame not filled by this message; just add data */
387 rx_history_add_bits(tss, ubb_data(ubb), ubb_length(ubb));
388 ubb_pull(ubb, ubb_length(ubb));
389 } else {
390 /* append as many bits as are missing in the current frame */
391 rx_history_add_bits(tss, ubb_data(ubb), frame_bits_remaining);
392 ubb_pull(ubb, frame_bits_remaining);
393
394 /* check if we still have frame sync */
395 rc = correlate_history_against_pattern(tss);
396 if (rc > 0) {
397 tss->num_consecutive_errors++;
398 if (tss->num_consecutive_errors >= 3) {
399 tss->history_idx = 0;
400 /* send NULL frame to user */
401 tss->out_cb(tss->user_data, NULL, 0);
402 osmo_fsm_inst_state_chg(fi, FRAME_ALIGNMENT_LOST, 1, T_SYNC);
403 osmo_fsm_inst_dispatch(fi, TRAUSYNC_E_RX_BITS, ubb);
404 return;
405 }
406 } else
407 tss->num_consecutive_errors = 0;
408
409 /* dispatch aligned frame to user */
410 tss->out_cb(tss->user_data, tss->history, tss->history_idx);
411 tss->history_idx = 0;
412 }
413 }
414 break;
415 default:
416 OSMO_ASSERT(0);
417 }
418}
419
420static void trau_sync_alignment_lost(struct osmo_fsm_inst *fi, uint32_t event, void *data)
421{
422 /* we try to restore sync for some amount of time before generating an error */
423
424 switch (event) {
425 case TRAUSYNC_E_RX_BITS:
426 trau_sync_wait_align(fi, event, data);
427 break;
428 default:
429 OSMO_ASSERT(0);
430 }
431}
432
433static void trau_sync_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data)
434{
435 switch (event) {
436 case TRAUSYNC_E_RESET:
437 osmo_fsm_inst_state_chg(fi, WAIT_FRAME_ALIGN, 0, 0);
438 break;
439 default:
440 OSMO_ASSERT(0);
441 }
442}
443
444static int trau_sync_timeout(struct osmo_fsm_inst *fi)
445{
446 switch (fi->T) {
447 case T_SYNC:
448 /* if Tsync expires before frame synchronization is
449 * again obtained the TRAU initiates sending of the
450 * urgent alarm pattern described in clause 4.10.2. */
451 osmo_fsm_inst_state_chg(fi, WAIT_FRAME_ALIGN, 0, 0);
452 break;
453 default:
454 OSMO_ASSERT(0);
455 }
456 return 0;
457}
458
459static const struct osmo_fsm_state trau_sync_states[] = {
460 [WAIT_FRAME_ALIGN] = {
461 .name = "WAIT_FRAME_ALIGN",
462 .in_event_mask = S(TRAUSYNC_E_RX_BITS),
463 .out_state_mask = S(FRAME_ALIGNED),
464 .action = trau_sync_wait_align,
465 },
466 [FRAME_ALIGNED] = {
467 .name = "FRAME_ALIGNED",
468 .in_event_mask = S(TRAUSYNC_E_RX_BITS),
469 .out_state_mask = S(FRAME_ALIGNMENT_LOST) | S(WAIT_FRAME_ALIGN),
470 .action = trau_sync_aligned,
471 .onenter = trau_sync_aligned_onenter,
472 },
473 [FRAME_ALIGNMENT_LOST] = {
474 .name = "FRAME_ALIGNMENT_LOST",
475 .in_event_mask = S(TRAUSYNC_E_RX_BITS),
476 .out_state_mask = S(WAIT_FRAME_ALIGN) | S(FRAME_ALIGNED),
477 .action = trau_sync_alignment_lost,
478 },
479};
480
481static struct osmo_fsm trau_sync_fsm = {
482 .name = "trau_sync",
483 .states = trau_sync_states,
484 .num_states = ARRAY_SIZE(trau_sync_states),
485 .allstate_event_mask = S(TRAUSYNC_E_RESET),
486 .allstate_action = trau_sync_allstate,
487 .timer_cb = trau_sync_timeout,
488 .log_subsys = DLGLOBAL,
489 .event_names = trau_sync_event_names,
490};
491
492
493struct osmo_fsm_inst *
494osmo_trau_sync_alloc(void *ctx, const char *name, frame_out_cb_t frame_out_cb,
495 enum osmo_tray_sync_pat_id pat_id, void *user_data)
496{
497 struct trau_rx_sync_state *tss;
498 struct osmo_fsm_inst *fi;
499
500 if (pat_id >= ARRAY_SIZE(sync_patterns))
501 return NULL;
502
Keithbbff3042021-04-28 21:33:00 -0500503 fi = osmo_fsm_inst_alloc(&trau_sync_fsm, ctx, NULL, LOGL_INFO, name);
Harald Welte78861c02020-05-14 13:28:07 +0200504 if (!fi)
505 return NULL;
506 tss = talloc_zero(fi, struct trau_rx_sync_state);
507 if (!tss) {
508 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
509 return NULL;
510 }
511 fi->priv = tss;
512
513 tss->out_cb = frame_out_cb;
514 tss->user_data = user_data;
Harald Welte78861c02020-05-14 13:28:07 +0200515 tss->pattern = &sync_patterns[pat_id];
516
Philipp Maier8eae96f2020-07-31 20:01:42 +0200517 /* An unusued E1 timeslot normally would send an idle signal that
518 * has all bits set to one. In order to prevent false-positive
519 * synchronization on startup we set all history bits to 1, to make
520 * it look like a signal from an unused timeslot. */
521 memset(tss->history, 1, sizeof(tss->history));
522
Harald Welte78861c02020-05-14 13:28:07 +0200523 return fi;
524}
525
Philipp Maier8d150012020-08-07 17:13:57 +0200526void osmo_trau_sync_set_pat(struct osmo_fsm_inst *fi, enum osmo_tray_sync_pat_id pat_id)
527{
528 struct trau_rx_sync_state *tss = fi->priv;
529
530 tss->pattern = &sync_patterns[pat_id];
531 osmo_fsm_inst_state_chg(fi, FRAME_ALIGNMENT_LOST, 0, 0);
532}
533
Harald Welte78861c02020-05-14 13:28:07 +0200534void osmo_trau_sync_rx_ubits(struct osmo_fsm_inst *fi, const ubit_t *bits, size_t n_bits)
535{
536 struct ubit_buf ubb;
537 ubb_init(&ubb, bits, n_bits);
538 osmo_fsm_inst_dispatch(fi, TRAUSYNC_E_RX_BITS, &ubb);
539}
540
541static void __attribute__((constructor)) on_dso_load_sync(void)
542{
543 int i;
544
545 for (i = 0; i < ARRAY_SIZE(sync_patterns); i++)
546 sync_pattern_register(&sync_patterns[i]);
Harald Welteab8e0662020-08-06 11:56:52 +0200547 OSMO_ASSERT(osmo_fsm_register(&trau_sync_fsm) == 0);
Harald Welte78861c02020-05-14 13:28:07 +0200548}