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
|
/*
* Invoke gsmstack() with any kind of burst. Automaticly decode and retrieve
* information.
*/
#include "system.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include "gsmstack.h"
//#include "gsm_constants.h"
#include "interleave.h"
//#include "sch.h"
#include "cch.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <osmocore/msgb.h>
#include <osmocore/gsmtap.h>
#include <osmocore/gsmtap_util.h>
static const int USEFUL_BITS = 142;
static void out_gsmdecode(char type, int arfcn, int ts, int fn, char *data, int len);
/* encode a decoded burst (1 bit per byte) into 8-bit-per-byte */
static void burst_octify(unsigned char *dest,
const unsigned char *data, int length)
{
int bitpos = 0;
while (bitpos < USEFUL_BITS) {
unsigned char tbyte;
int i;
tbyte = 0;
for (i = 0; (i < 8) && (bitpos < length); i++) {
tbyte <<= 1;
tbyte |= data[bitpos++];
}
if (i < 8)
tbyte <<= 8 - i;
*dest++ = tbyte;
}
}
#if 0
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++;
}
}
#endif
/* TODO: handle mapping in a more elegant way or simplify the function */
uint8_t
get_chan_type(enum TIMESLOT_TYPE type, int fn, uint8_t *ss)
{
uint8_t chan_type = GSMTAP_CHANNEL_BCCH;
*ss = 0;
int mf51 = fn % 51;
if(type == TST_FCCH_SCH_BCCH_CCCH_SDCCH4)
{
if(mf51 == 22) /* SDCCH */
{
chan_type = GSMTAP_CHANNEL_SDCCH4;
*ss = 0;
}
else if(mf51 == 26) /* SDCCH */
{
chan_type = GSMTAP_CHANNEL_SDCCH4;
*ss = 1;
}
else if(mf51 == 32) /* SDCCH */
{
chan_type = GSMTAP_CHANNEL_SDCCH4;
*ss = 2;
}
else if(mf51 == 36) /* SDCCH */
{
chan_type = GSMTAP_CHANNEL_SDCCH4;
*ss = 3;
}
else if(mf51 == 42) /* SAACH */
{
chan_type = GSMTAP_CHANNEL_SDCCH4 | GSMTAP_CHANNEL_ACCH;
*ss = ((fn % 102) > 51) ? 2 : 0;
}
else if(mf51 == 46) /* SAACH */
{
chan_type = GSMTAP_CHANNEL_SDCCH4 | GSMTAP_CHANNEL_ACCH;
*ss = ((fn % 102) > 51) ? 3 : 1;
}
}
else if(type == TST_FCCH_SCH_BCCH_CCCH)
{
if(mf51 != 2) /* BCCH */
{
chan_type = GSMTAP_CHANNEL_CCCH;
*ss = 0;
}
}
else if(type == TST_SDCCH8)
{
if(mf51 == 0) /* SDCCH */
{
chan_type = GSMTAP_CHANNEL_SDCCH8;
*ss = 0;
}
else if(mf51 == 4) /* SDCCH */
{
chan_type = GSMTAP_CHANNEL_SDCCH8;
*ss = 1;
}
else if(mf51 == 8) /* SDCCH */
{
chan_type = GSMTAP_CHANNEL_SDCCH8;
*ss = 2;
}
else if(mf51 == 12) /* SDCCH */
{
chan_type = GSMTAP_CHANNEL_SDCCH8;
*ss = 3;
}
else if(mf51 == 16) /* SDCCH */
{
chan_type = GSMTAP_CHANNEL_SDCCH8;
*ss = 4;
}
else if(mf51 == 20) /* SDCCH */
{
chan_type = GSMTAP_CHANNEL_SDCCH8;
*ss = 5;
}
else if(mf51 == 24) /* SDCCH */
{
chan_type = GSMTAP_CHANNEL_SDCCH8;
*ss = 6;
}
else if(mf51 == 28) /* SDCCH */
{
chan_type = GSMTAP_CHANNEL_SDCCH8;
*ss = 7;
}
else if(mf51 == 32) /* SAACH */
{
chan_type = GSMTAP_CHANNEL_SDCCH8 | GSMTAP_CHANNEL_ACCH;
*ss = ((fn % 102) > 51) ? 4 : 0;
}
else if(mf51 == 36) /* SAACH */
{
chan_type = GSMTAP_CHANNEL_SDCCH8 | GSMTAP_CHANNEL_ACCH;
*ss = ((fn % 102) > 51) ? 5 : 1;
}
else if(mf51 == 40) /* SAACH */
{
chan_type = GSMTAP_CHANNEL_SDCCH8 | GSMTAP_CHANNEL_ACCH;
*ss = ((fn % 102) > 51) ? 6 : 2;
}
else if(mf51 == 44) /* SAACH */
{
chan_type = GSMTAP_CHANNEL_SDCCH8 | GSMTAP_CHANNEL_ACCH;
*ss = ((fn % 102) > 51) ? 7 : 3;
}
}
else if (type == TST_TCHF) {
chan_type = GSMTAP_CHANNEL_TCH_F | GSMTAP_CHANNEL_ACCH;
}
return chan_type;
}
/*
* Initialize a new GSMSTACK context.
*/
int
GS_new(GS_CTX *ctx)
{
int rc;
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(GSMTAP_UDP_PORT);
inet_aton("224.0.0.1", &sin.sin_addr);
memset(ctx, 0, sizeof *ctx);
interleave_init(&ctx->interleave_ctx, 456, 114);
interleave_init_facch_f(&ctx->interleave_facch_f1_ctx, 456, 114, 0);
interleave_init_facch_f(&ctx->interleave_facch_f2_ctx, 456, 114, 4);
ctx->fn = -1;
ctx->bsic = -1;
ctx->gsmtap_fd = -1;
rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (rc < 0) {
perror("creating UDP socket\n");
return rc;
}
ctx->gsmtap_fd = rc;
rc = connect(rc, (struct sockaddr *)&sin, sizeof(sin));
if (rc < 0) {
perror("connectiong UDP socket");
close(ctx->gsmtap_fd);
return rc;
}
return 0;
}
#define BURST_BYTES ((USEFUL_BITS/8)+1)
/*
* 142 bit
*/
int
GS_process(GS_CTX *ctx, int ts, int type, const unsigned char *src, int fn, int first_burst)
{
int bsic;
int ret;
unsigned char *data;
int len;
struct gs_ts_ctx *ts_ctx = &ctx->ts_ctx[ts];
memset(ctx->msg, 0, sizeof(ctx->msg));
if (ts_ctx->type == TST_TCHF && type == NORMAL &&
(fn % 26) != 12 && (fn % 26) != 25) {
/* Dieter: we came here because the burst might contain FACCH bits */
ctx->fn = fn;
/* get burst index to TCH bursts only */
ts_ctx->burst_count2 = fn % 26;
if (ts_ctx->burst_count2 >= 12)
ts_ctx->burst_count2--;
ts_ctx->burst_count2 = ts_ctx->burst_count2 % 8;
/* copy data bits and stealing flags to buffer */
memcpy(ts_ctx->burst2 + (116 * ts_ctx->burst_count2), src, 58);
memcpy(ts_ctx->burst2 + (116 * ts_ctx->burst_count2) + 58, src + 58 + 26, 58);
/* Return if not enough bursts for a full gsm message */
if ((ts_ctx->burst_count2 % 4) != 3)
return 0;
data = decode_facch(ctx, ts_ctx->burst2, &len, (ts_ctx->burst_count2 == 3) ? 1 : 0);
if (data == NULL) {
DEBUGF("cannot decode FACCH fnr=%d ts=%d\n", ctx->fn, ts);
return -1;
}
out_gsmdecode(0, 0, ts, ctx->fn, data, len);
if (ctx->gsmtap_fd >= 0) {
struct msgb *msg;
uint8_t chan_type = GSMTAP_CHANNEL_TCH_F;
uint8_t ss = 0;
int fn = (ctx->fn - 3); /* "- 3" for start of frame */
msg = gsmtap_makemsg(0, ts, chan_type, ss, ctx->fn, 0, 0, data, len);
if (msg)
write(ctx->gsmtap_fd, msg->data, msg->len);
}
return 0;
}
/* normal burst processing */
if (first_burst) /* Dieter: it is important to start with the correct burst */
ts_ctx->burst_count = 0;
ctx->fn = 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(ts_ctx->burst + (116 * ts_ctx->burst_count), src, 58);
memcpy(ts_ctx->burst + (116 * ts_ctx->burst_count) + 58, src + 58 + 26, 58);
ts_ctx->burst_count++;
/* Return if not enough bursts for a full gsm message */
if (ts_ctx->burst_count < 4)
return 0;
ts_ctx->burst_count = 0;
data = decode_cch(ctx, ts_ctx->burst, &len);
if (data == NULL) {
DEBUGF("cannot decode fnr=0x%06x (%6d) ts=%d\n", ctx->fn, ctx->fn, ts);
return -1;
}
//DEBUGF("OK TS %d, len %d\n", ts, len);
out_gsmdecode(0, 0, ts, ctx->fn, data, len);
if (ctx->gsmtap_fd >= 0) {
/* Dieter: set channel type according to configuration */
struct msgb *msg;
uint8_t chan_type = GSMTAP_CHANNEL_BCCH;
uint8_t ss = 0;
int fn = (ctx->fn - 3); /* "- 3" for start of frame */
chan_type = get_chan_type(ts_ctx->type, fn, &ss);
/* arfcn, ts, chan_type, ss, fn, signal, snr, data, len */
msg = gsmtap_makemsg(0, ts, chan_type, ss,
ctx->fn, 0, 0, data, len);
if (msg)
write(ctx->gsmtap_fd, msg->data,
msg->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 gsmdecode.
*/
static void
out_gsmdecode(char type, int arfcn, int ts, int fn, char *data, int len)
{
char *end = data + len;
printf("%6d %d:", (fn + 0), ts);
/* FIXME: speed this up by first printing into an array */
while (data < end)
printf(" %02.2x", (unsigned char)*data++);
printf("\n");
fflush(stdout);
}
|