aboutsummaryrefslogtreecommitdiffstats
path: root/tests/trau_sync/trau_sync_test.c
blob: d804b3effb6af7023bddb0c8ab915ab308d0a3e9 (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
#include <osmocom/core/application.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/bits.h>

#include <osmocom/trau/trau_sync.h>

static void frame_out_cb(void *user_data, const ubit_t *bits, unsigned int num_bits)
{
	char *str = user_data;
	printf("demux_bits_cb '%s': %s\n", str, osmo_ubit_dump(bits, num_bits));
}

static const uint8_t sync_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,
};

#define ASSERT_STATE(fi, x) OSMO_ASSERT(!strcmp(osmo_fsm_inst_state_name(fi), x))

static void test_body(void)
{
	struct osmo_fsm_inst *fi = osmo_trau_sync_alloc(NULL, "test", frame_out_cb, OSMO_TRAU_SYNCP_16_FR_EFR, "test");
	OSMO_ASSERT(fi);

	printf("\n==> %s\n", __func__);

	ubit_t bits[40*8];

	/* send some invalid data */
	memset(bits, 0, sizeof(bits));
	osmo_trau_sync_rx_ubits(fi, bits, sizeof(bits));
	osmo_trau_sync_rx_ubits(fi, bits, 23);

	/* first valid frame */
	osmo_pbit2ubit(bits, sync_pattern, sizeof(sync_pattern)*8);
	osmo_trau_sync_rx_ubits(fi, bits, sizeof(bits));
	ASSERT_STATE(fi, "FRAME_ALIGNED");

	/* second valid frame */
	osmo_trau_sync_rx_ubits(fi, bits, sizeof(bits));
	ASSERT_STATE(fi, "FRAME_ALIGNED");

	/* send wrong frame */
	memset(bits, 1, sizeof(bits));
	osmo_trau_sync_rx_ubits(fi, bits, sizeof(bits));
	ASSERT_STATE(fi, "FRAME_ALIGNED");

	/* intersperse a valid frame */
	osmo_pbit2ubit(bits, sync_pattern, sizeof(sync_pattern)*8);
	osmo_trau_sync_rx_ubits(fi, bits, sizeof(bits));

	/* second wrong frame - but not consecutive */
	memset(bits, 1, sizeof(bits));
	osmo_trau_sync_rx_ubits(fi, bits, sizeof(bits));
	ASSERT_STATE(fi, "FRAME_ALIGNED");

	/* third wrong frame - second consecutive */
	osmo_trau_sync_rx_ubits(fi, bits, sizeof(bits));
	ASSERT_STATE(fi, "FRAME_ALIGNED");

	/* only from third consecutive invalid frame onwards we should loose alignment */
	osmo_trau_sync_rx_ubits(fi, bits, sizeof(bits));
	ASSERT_STATE(fi, "FRAME_ALIGNMENT_LOST");
}


static const struct log_info_cat default_categories[] = {
};

const struct log_info log_info = {
	.cat = default_categories,
	.num_cat = ARRAY_SIZE(default_categories),
};

int main(int argc, char **argv)
{
	osmo_init_logging2(NULL, NULL);
	log_set_use_color(osmo_stderr_target, 0);
	osmo_fsm_log_addr(false);
	log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
	test_body();
}