aboutsummaryrefslogtreecommitdiffstats
path: root/src/trau/trau_sync.c
blob: 730cf0c0e59688872c1b0c530ac489e11a843d26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/* GSM A-bis TRAU frame synchronization as per TS 48.060 / 48.061 */

/* (C) 2020 by Harald Welte <laforge@gnumonks.org>
 * All Rights Reserved
 *
 * SPDX-License-Identifier: GPL-2.0+
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdint.h>

#include <osmocom/core/msgb.h>
#include <osmocom/core/bits.h>
#include <osmocom/core/fsm.h>

#include "ubit_buf.h"
#include <osmocom/trau/trau_sync.h>

#define S(x)	(1 << (x))

#define MAX_TRAU_BYTES 	40

#define T_SYNC		1

struct sync_pattern {
	/* provided by user */
	const char *name;				/*!< human-readable name */
	const uint8_t byte_pattern[MAX_TRAU_BYTES];	/*!< bytes to match against */
	const uint8_t byte_mask[MAX_TRAU_BYTES];	/*!< mask applied before matching */
	uint8_t byte_len;				/*!< length of mask in bytes */

	/* generated by code */
	ubit_t ubit_pattern[MAX_TRAU_BYTES*8];		/*!< bits to match against */
	ubit_t ubit_mask[MAX_TRAU_BYTES*8];		/*!< mask applied before matching */
	uint8_t bitcount;				/*!< number of high bits in mask */
};

static struct sync_pattern sync_patterns[] = {
	[OSMO_TRAU_SYNCP_16_FR_EFR] = {
		/* TS 08.60 Section 4.8.1 */
		.name = "FR/EFR",
		.byte_pattern = {
			0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
			0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
			0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
			0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
			0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
		},
		.byte_mask = {
			0xff, 0xff, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
			0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
			0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
			0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
			0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
		},
		.byte_len = 40,
	},
	[OSMO_TRAU_SYNCP_8_HR] = {
		/* TS 08.61 Section 6.8.2.1.1 */
		.name = "HR8",
		.byte_pattern = {
			0x00, 0x80, 0x40, 0x80,
			0x80, 0x80, 0x80, 0x80,
			0x80, 0x80, 0x80, 0x80,
			0x80, 0x80, 0x80, 0x80,
			0x80, 0x80, 0x80, 0x80,
		},
		.byte_mask = {
			0xff, 0x80, 0xC0, 0x80,
			0x80, 0x80, 0x80, 0x80,
			0x80, 0x80, 0x80, 0x80,
			0x80, 0x80, 0x80, 0x80,
			0x80, 0x80, 0x80, 0x80,
		},
		.byte_len = 20,
	},
	[OSMO_TRAU_SYNCP_8_AMR_LOW] = {
		/* TS 08.61 Section 6.8.2.1.2 */
		/* The frame synchronisation for No_Speech frames and the speech frames of the three lower codec modes */
		.name = "AMR8_LOW",
		.byte_pattern = {
			0x00, 0x80, 0x80, 0x40,
			0x80, 0x80, 0x80, 0x80,
			0x80, 0x80, 0x80, 0x80,
			0x80, 0x80, 0x80, 0x80,
			0x80, 0x80, 0x80, 0x80,
		},
		.byte_mask = {
			0xff, 0x80, 0x80, 0xC0,
			0x80, 0x80, 0x80, 0x80,
			0x80, 0x80, 0x80, 0x80,
			0x80, 0x80, 0x80, 0x80,
			0x80, 0x80, 0x80, 0x80,
		},
		.byte_len = 20,
	},
	[OSMO_TRAU_SYNCP_8_AMR_6K7] = {
		/* The frame synchronisation for the speech frames for codec mode 6,70 kBit/s */
		.name = "AMR8_67",
		.byte_pattern = {
			0x00, 0x80, 0x80, 0x80,
			0x80, 0x00, 0x80, 0x00,
			0x80, 0x00, 0x80, 0x00,
			0x80, 0x00, 0x80, 0x00,
			0x80, 0x00, 0x80, 0x00,
		},
		.byte_mask = {
			0xff, 0x80, 0x80, 0x80,
			0x80, 0x80, 0x80, 0x00,
			0x80, 0x00, 0x80, 0x00,
			0x80, 0x00, 0x80, 0x00,
			0x80, 0x00, 0x80, 0x00,
		},
		.byte_len = 20
	},
	[OSMO_TRAU_SYNCP_8_AMR_7K4] = {
		/* The frame synchronisation for the speech frames for codec mode 7,40 kBit/s */
		.name = "AMR8_74",
		.byte_pattern = {
			0x20, 0x00, 0x80, 0x00,
			0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00,
		},
		.byte_mask = {
			0xe0, 0x80, 0x80, 0x80,
			0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00,
		},
		.byte_len = 20,
	},
};

#if 0
static struct sync_pattern rbs_ccu_sync_ind_16_pattern = {
	.name ="RBS_CCU_SYNC_IND_16",
	.byte_pattern = {
		0x00, 0x00, 0x01, 0x00,
		0x00, 0x00, 0x01, 0x00,
		0x01, 0x00, 0x01, 0x00,
	},
	.byte_mask = {
		0xff, 0xff, 0x01, 0x00,
		0x01, 0x00, 0x01, 0x00,
		0x01, 0x00, 0x01, 0x00,
	},
};

static struct sync_pattern rbs_ccu_data_ind_16_pattern = {
	.name ="RBS_CCU_DATA_IND_16",
	.byte_pattern = {
		0x00, 0x00, 0x01, 0x00,
	},
	.byte_mask = {
		0xff, 0xff, 0x01, 0x00,
	},
};
#endif


static void expand_sync_pattern(struct sync_pattern *pat)
{
	osmo_pbit2ubit(pat->ubit_pattern, pat->byte_pattern, pat->byte_len*8);
	osmo_pbit2ubit(pat->ubit_mask, pat->byte_mask, pat->byte_len*8);
}

static unsigned int count_one_bits(const ubit_t *in, unsigned int in_bits)
{
	unsigned int i, count = 0;

	for (i = 0; i < in_bits; i++) {
		if (in[i])
			count++;
	}
	return count;
}

static void sync_pattern_register(struct sync_pattern *p)
{
	expand_sync_pattern(p);
	p->bitcount = count_one_bits(p->ubit_mask, p->byte_len*8);
}

#if 0
/*! correlate pattern with unpacked bits from buffer.
 *  \param[in] pattern sync_pattern against which we shall compare
 *  \param[in] bits unpacked bits to compare against pattern
 *  \param[in] num_bits number of unpacked bits
 *  \returns number of bits not matching pattern; -1 if insufficient bits available. */
static int correlate_pattern_ubits(const struct sync_pattern *pattern,
				    const ubit_t *bits, size_t num_bits)
{
	int i, num_wrong = 0;

	if (num_bits < pattern->byte_len*8)
		return -1; /* insufficient data */

	for (i = 0; i < pattern->byte_len *8; i++) {
		/* if mask doesn't contain '1', we can skip this octet */
		if (!pattern->ubit_mask)
			continue;
		if (bits[i] != pattern->ubit_pattern[i])
			num_wrong++;
	}

	return num_wrong;
}
#endif

struct trau_rx_sync_state {
	/*! call-back to be called for every TRAU frame (called with
	 * bits=NULL in case of frame sync loss */
	frame_out_cb_t out_cb;
	/*! opaque user data; passed to out_cb */
	void *user_data;

	/*! history of received bits */
	ubit_t history[MAX_TRAU_BYTES*8+1]; /* +1 not required, but helps to expose bugs */
	/*! index of next-to-be-written ubit in history */
	unsigned int history_idx;
	/*! the pattern we are trying to sync to */
	const struct sync_pattern *pattern;
	/*! number of consecutive frames without sync */
	unsigned int num_consecutive_errors;
};

/* correlate the history (up to the last received bit) against the pattern */
static int correlate_history_against_pattern(struct trau_rx_sync_state *tss)
{
	const struct sync_pattern *pattern = tss->pattern;
	int i, start, num_wrong = 0;

	/* compute index of first bit in history array */
	start = (ARRAY_SIZE(tss->history) + tss->history_idx - pattern->byte_len*8)
		% ARRAY_SIZE(tss->history);

	OSMO_ASSERT(ARRAY_SIZE(tss->history) >= pattern->byte_len*8);

	for (i = 0; i < pattern->byte_len*8; i++) {
		unsigned int pos = (start + i) % ARRAY_SIZE(tss->history);

		/* if mask doesn't contain '1', we can skip this octet */
		if (!pattern->ubit_mask[i])
			continue;
		if (tss->history[pos] != pattern->ubit_pattern[i])
			num_wrong++;
	}

	return num_wrong;
}

/* add (append) one ubit to the history; wrap as needed */
static void rx_history_add_bit(struct trau_rx_sync_state *tss, ubit_t bit)
{
	tss->history[tss->history_idx] = bit;
	/* simply wrap around at the end */
	tss->history_idx = (tss->history_idx + 1) % ARRAY_SIZE(tss->history);
}

/* append bits to history. We assume that this does NOT wrap */
static void rx_history_add_bits(struct trau_rx_sync_state *tss, const ubit_t *bits, size_t n_bits)
{
	unsigned int frame_bits_remaining = tss->pattern->byte_len*8 - tss->history_idx;
	OSMO_ASSERT(frame_bits_remaining >= n_bits);
	memcpy(&tss->history[tss->history_idx], bits, n_bits);
	tss->history_idx = tss->history_idx + n_bits;
}

/* align the history, i.e. next received bit is start of frame */
static void rx_history_align(struct trau_rx_sync_state *tss)
{
	ubit_t tmp[sizeof(tss->history)];
	size_t history_size = sizeof(tss->history);
	size_t pattern_bits = tss->pattern->byte_len*8;
	size_t first_bit = (history_size + tss->history_idx - pattern_bits) % history_size;
	int i;

	/* we need to shift the last received frame to the start of the history buffer;
	 * do this in two steps: First copy to a local buffer on the stack, using modulo-arithmetic
	 * as index into the history.  Second, copy it back to history */

	for (i = 0; i < pattern_bits; i++)
		tmp[i] = tss->history[(first_bit + i) % history_size];

	memcpy(tss->history, tmp, history_size);
	tss->history_idx = 0;
}

enum trau_sync_state {
	WAIT_FRAME_ALIGN,
	FRAME_ALIGNED,
	/* if at least 3 consecutive frames with each at least one framing error have been received */
	FRAME_ALIGNMENT_LOST,
};

enum trau_sync_event {
	TRAUSYNC_E_RESET,
	/*! a buffer of bits was received (msgb with ubits) */
	TRAUSYNC_E_RX_BITS,
};

static const struct value_string trau_sync_event_names[] = {
	{ TRAUSYNC_E_RESET, "RESET" },
	{ TRAUSYNC_E_RX_BITS, "RX_BITS" },
	{ 0, NULL }
};


static void trau_sync_wait_align(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	struct trau_rx_sync_state *tss = (struct trau_rx_sync_state *) fi->priv;
	struct ubit_buf *ubb;

	switch (event) {
	case TRAUSYNC_E_RX_BITS:
		ubb = data;
		/* append every bit individually + check if we have sync */
		while (ubb_length(ubb) > 0) {
			ubit_t bit = ubb_pull_ubit(ubb);
			int rc;

			rx_history_add_bit(tss, bit);
			rc = correlate_history_against_pattern(tss);
			if (!rc) {
				osmo_fsm_inst_state_chg(fi, FRAME_ALIGNED, 0, 0);
				/* treat remainder of input bits in correct state */
				osmo_fsm_inst_dispatch(fi, TRAUSYNC_E_RX_BITS, ubb);
				return;
			}
		}
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static void trau_sync_aligned_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct trau_rx_sync_state *tss = (struct trau_rx_sync_state *) fi->priv;
	/* dispatch aligned frame to user */
	rx_history_align(tss);
	tss->out_cb(tss->user_data, tss->history, tss->pattern->byte_len*8);
}

static void trau_sync_aligned(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	struct trau_rx_sync_state *tss = (struct trau_rx_sync_state *) fi->priv;
	struct ubit_buf *ubb;
	int rc;

	switch (event) {
	case TRAUSYNC_E_RX_BITS:
		ubb = data;
		while (ubb_length(ubb)) {
			unsigned int frame_bits_remaining = tss->pattern->byte_len*8 - tss->history_idx;
			if (ubb_length(ubb) < frame_bits_remaining) {
				/* frame not filled by this message; just add data */
				rx_history_add_bits(tss, ubb_data(ubb), ubb_length(ubb));
				ubb_pull(ubb, ubb_length(ubb));
			} else {
				/* append as many bits as are missing in the current frame */
				rx_history_add_bits(tss, ubb_data(ubb), frame_bits_remaining);
				ubb_pull(ubb, frame_bits_remaining);

				/* check if we still have frame sync */
				rc = correlate_history_against_pattern(tss);
				if (rc > 0) {
					tss->num_consecutive_errors++;
					if (tss->num_consecutive_errors >= 3) {
						tss->history_idx = 0;
						/* send NULL frame to user */
						tss->out_cb(tss->user_data, NULL, 0);
						osmo_fsm_inst_state_chg(fi, FRAME_ALIGNMENT_LOST, 1, T_SYNC);
						osmo_fsm_inst_dispatch(fi, TRAUSYNC_E_RX_BITS, ubb);
						return;
					}
				} else
					tss->num_consecutive_errors = 0;

				/* dispatch aligned frame to user */
				tss->out_cb(tss->user_data, tss->history, tss->history_idx);
				tss->history_idx = 0;
			}
		}
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static void trau_sync_alignment_lost(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	/* we try to restore sync for some amount of time before generating an error */

	switch (event) {
	case TRAUSYNC_E_RX_BITS:
		trau_sync_wait_align(fi, event, data);
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static void trau_sync_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	switch (event) {
	case TRAUSYNC_E_RESET:
		osmo_fsm_inst_state_chg(fi, WAIT_FRAME_ALIGN, 0, 0);
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static int trau_sync_timeout(struct osmo_fsm_inst *fi)
{
	switch (fi->T) {
	case T_SYNC:
		/* if Tsync expires before frame synchronization is
		 * again obtained the TRAU initiates sending of the
		 * urgent alarm pattern described in clause 4.10.2. */
		osmo_fsm_inst_state_chg(fi, WAIT_FRAME_ALIGN, 0, 0);
		break;
	default:
		OSMO_ASSERT(0);
	}
	return 0;
}

static const struct osmo_fsm_state trau_sync_states[] = {
	[WAIT_FRAME_ALIGN] = {
		.name = "WAIT_FRAME_ALIGN",
		.in_event_mask = S(TRAUSYNC_E_RX_BITS),
		.out_state_mask = S(FRAME_ALIGNED),
		.action = trau_sync_wait_align,
	},
	[FRAME_ALIGNED] = {
		.name = "FRAME_ALIGNED",
		.in_event_mask = S(TRAUSYNC_E_RX_BITS),
		.out_state_mask = S(FRAME_ALIGNMENT_LOST) | S(WAIT_FRAME_ALIGN),
		.action = trau_sync_aligned,
		.onenter = trau_sync_aligned_onenter,
	},
	[FRAME_ALIGNMENT_LOST] = {
		.name = "FRAME_ALIGNMENT_LOST",
		.in_event_mask = S(TRAUSYNC_E_RX_BITS),
		.out_state_mask = S(WAIT_FRAME_ALIGN) | S(FRAME_ALIGNED),
		.action = trau_sync_alignment_lost,
	},
};

static struct osmo_fsm trau_sync_fsm = {
	.name = "trau_sync",
	.states = trau_sync_states,
	.num_states = ARRAY_SIZE(trau_sync_states),
	.allstate_event_mask = S(TRAUSYNC_E_RESET),
	.allstate_action = trau_sync_allstate,
	.timer_cb = trau_sync_timeout,
	.log_subsys = DLGLOBAL,
	.event_names = trau_sync_event_names,
};


struct osmo_fsm_inst *
osmo_trau_sync_alloc(void *ctx, const char *name, frame_out_cb_t frame_out_cb,
		     enum osmo_tray_sync_pat_id pat_id, void *user_data)
{
	struct trau_rx_sync_state *tss;
	struct osmo_fsm_inst *fi;

	if (pat_id >= ARRAY_SIZE(sync_patterns))
		return NULL;

	fi = osmo_fsm_inst_alloc(&trau_sync_fsm, ctx, NULL, LOGL_NOTICE, name);
	if (!fi)
		return NULL;
	tss = talloc_zero(fi, struct trau_rx_sync_state);
	if (!tss) {
		osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
		return NULL;
	}
	fi->priv = tss;

	tss->out_cb = frame_out_cb;
	tss->user_data = user_data;
	/* FIXME: this must be configurable */
	tss->pattern = &sync_patterns[pat_id];

	/* An unusued E1 timeslot normally would send an idle signal that
	 * has all bits set to one. In order to prevent false-positive
	 * synchronization on startup we set all history bits to 1, to make
         * it look like a signal from an unused timeslot. */
	memset(tss->history, 1, sizeof(tss->history));

	return fi;
}

void osmo_trau_sync_rx_ubits(struct osmo_fsm_inst *fi, const ubit_t *bits, size_t n_bits)
{
	struct ubit_buf ubb;
	ubb_init(&ubb, bits, n_bits);
	osmo_fsm_inst_dispatch(fi, TRAUSYNC_E_RX_BITS, &ubb);
}

static void __attribute__((constructor)) on_dso_load_sync(void)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(sync_patterns); i++)
		sync_pattern_register(&sync_patterns[i]);
	OSMO_ASSERT(osmo_fsm_register(&trau_sync_fsm) == 0);
}