summaryrefslogtreecommitdiff
path: root/gsm-tvoid/src/lib/gsmstack.c
blob: a38ffcb879c16304e35f95bf03e0309d4ce7ebc8 (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
/*
 * Invoke gsmstack() with any kind of burst. Automaticly decode and retrieve
 * information.
 */
#include "system.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "gsmstack.h"
#include "gsm_constants.h"
#include "interleave.h"
#include "sch.h"
#include "cch.h"

static void out_gsmdecode(char type, int arfcn, int ts, int fn, char *data, int len);

static void
diff_decode(char *dst, char *src, int len)
{
	const char *end = src + len;
	unsigned char last;

	src += 3;
	last = 0;
	memset(dst, 0, 3);
	dst += 3;

	while (src < end)
	{
		*dst = !*src ^ last;
		last = *dst;
		src++;
		dst++;
	}
}

/*
 * Initialize a new GSMSTACK context.
 */
int
GS_new(GS_CTX *ctx)
{
	memset(ctx, 0, sizeof *ctx);
	interleave_init(&ctx->interleave_ctx, 456, 114);
	ctx->fn = -1;
	ctx->bsic = -1;

	return 0;
}

/*
 * 156 bit
 */
int
GS_process(GS_CTX *ctx, int ts, int type, char *src)
{
	int fn;
	int bsic;
	int ret;
	char buf[156];
	unsigned char *data;
	int len;

	if (ts != 0)
		return;

	diff_decode(buf, src, 156);

	if (type == SCH)
	{
		ret = decode_sch(buf, &fn, &bsic);
		if (ret != 0)
			return 0;
		if ((ctx->bsic > 0) && (bsic != ctx->bsic))
			fprintf(stderr, "WARN: BSIC changed.\n");
		//DEBUGF("FN %d, BSIC %d\n", fn, bsic);
		ctx->fn = fn;
		ctx->bsic = bsic;
		/* Reset message concatenator */
		ctx->burst_count = 0;
		return 0;
	}

	/* If we did not get Frame Number yet then return */
	if (ctx->fn < 0)
		return 0;

	ctx->fn++;
	if (type == NORMAL)
	{
		/* Interested in these frame numbers (cch)
 		 * 2-5, 12-15, 22-25, 23-35, 42-45
 		 * 6-9, 16-19, 26-29, 36-39, 46-49
 		 */
		/* Copy content data into new array */
		//DEBUGF("burst count %d\n", ctx->burst_count);
		memcpy(ctx->burst + (116 * ctx->burst_count), buf + 3, 58);
		memcpy(ctx->burst + (116 * ctx->burst_count) + 58, buf + 3 + 58 + 26, 58);
		ctx->burst_count++;
		/* Return if not enough bursts for a full gsm message */
		if (ctx->burst_count < 4)
			return 0;

		ctx->burst_count = 0;
		data = decode_cch(ctx, ctx->burst, &len);
		if (data == NULL)
			return -1;
		//DEBUGF("OK TS %d, len %d\n", ts, len);

		out_gsmdecode(0, 0, ts, ctx->fn - 4, data, len);
#if 0
		if (ctx->fn % 51 != 0) && ( (((ctx->fn % 51 + 5) % 10 == 0) || (((ctx->fn % 51) + 1) % 10 ==0) ) )
			ready = 1;
#endif
		
		return 0;
	}
}


/*
 * Output data so that it can be parsed from gsmdeocde.
 */
static void
out_gsmdecode(char type, int arfcn, int ts, int fn, char *data, int len)
{
	char *end = data + len;

	/* FIXME: speed this up by first printing into an array */
	while (data < end)
		printf(" %02.2x", (unsigned char)*data++);
	printf("\n");
	fflush(stdout);
}

personal git repositories of Harald Welte. Your mileage may vary