forked from EliasOenal/multimon-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clip.c
445 lines (404 loc) · 13.1 KB
/
clip.c
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
/*
* clip.c -- clip decoder and packet dump
*
* Copyright (C) 2007
*
* 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* ---------------------------------------------------------------------- */
#include "multimon.h"
#include <string.h>
/* ---------------------------------------------------------------------- */
static inline int check_sum256(const unsigned char *buf, int cnt)
{
unsigned int sum = 0;
for (; cnt > 0; cnt--)
sum += *buf++;
return (sum % 256);
}
/* ---------------------------------------------------------------------- */
unsigned char disp_parm(unsigned char *bp, unsigned char param_len)
{
unsigned char i, len;
len=param_len;
while (len) {
i = *bp++;
if ((i >= 32) && (i < 128))
verbprintf(0, "%c",i);
else
verbprintf(0, ".");
len--;
}
return param_len;
}
/*
* As specified in ETSI EN 300 659-3
*/
static void clip_disp_packet(struct demod_state *s, unsigned char *bp, unsigned int len)
{
unsigned char i;
unsigned char msg_len, param_len;
unsigned char *ptr;
if (!bp || len < 5)
return;
#if 1
i=check_sum256(bp, len);
// if (i != 0 && i != 0xf6) {
if (i != 0) {
// verbprintf(0, " CHKSUM=0x%02X SUM=0x%02X PLEN=%d\n", i, *(bp+len-1), len);
return;
}
#endif
len -= 1;
i = *bp++;
switch(i) {
case 0x80: /* Call Setup */
verbprintf(0, "%s: CS", s->dem_par->name);
break;
case 0x82: /* Message Waiting Indicator */
verbprintf(0, "%s: MWI", s->dem_par->name);
break;
case 0x04: /* Reserved */
case 0x84: /* Reserved */
case 0x85: /* Reserved */
verbprintf(0, "%s: RVD len=%d", s->dem_par->name, len+1);
break;
case 0x86: /* Advice of Charge */
verbprintf(0, "%s: AOC len=%d", s->dem_par->name, len+1);
break;
case 0x89: /* Short Message Service */
verbprintf(0, "%s: AOC len=%d", s->dem_par->name, len+1);
break;
default:
verbprintf(0, "%s: UNKNOWN Message type (0x%02x) len=%d ", s->dem_par->name, i, len+1);
// verbprintf(0, "\n");
return;
}
if(!len)
return;
msg_len = *bp++;
if (msg_len > len)
verbprintf(0, " broken packet len=%d\n", msg_len);
while (msg_len > 2) {
i = *bp++;
msg_len--;
switch (i) {
case 0x01: /* Date and Time */
param_len = *bp++;
msg_len -= param_len + 1;
verbprintf(0, " DATE=");
bp += disp_parm(bp, param_len);
break;
case 0x02: /* Calling Line Identity */
param_len = *bp++;
msg_len -= param_len + 1;
verbprintf(0, " CID=");
bp += disp_parm(bp, param_len);
break;
case 0x03: /* Called Line Identity */
param_len = *bp++;
msg_len -= param_len + 1;
verbprintf(0, " CDN=");
bp += disp_parm(bp, param_len);
break;
case 0x04: /* Reason for Absence of Calling Line Identity */
param_len = *bp++;
msg_len -= param_len + 1;
verbprintf(0, " RACLI=");
i = *bp;
bp += disp_parm(bp, param_len);
switch (i) {
case 'O':
verbprintf(0, " Unavailable");
break;
case 'P':
verbprintf(0, " Private (CLIR involved)");
break;
default:
verbprintf(0, " (0x%02x indicator unknown)", i);
break;
}
break;
case 0x07: /* Calling Party Name */
param_len = *bp++;
msg_len -= param_len + 1;
verbprintf(0, " CNT=");
bp += disp_parm(bp, param_len);
break;
case 0x08: /* Reason for Absence of Calling Line Identity */
param_len = *bp++;
msg_len -= param_len + 1;
verbprintf(0, " RACNT=");
i = *bp;
bp += disp_parm(bp, param_len);
switch (i) {
case 'O':
verbprintf(0, " Unavailable");
break;
case 'P':
verbprintf(0, " Private (CLIR involved)");
break;
default:
verbprintf(0, " (0x%02x indicator unknown)", i);
break;
}
break;
case 0x0B: /* Visual Indicator parameter type */
param_len = *bp++;
msg_len -= param_len + 1;
verbprintf(0, " VI=");
i = *bp;
bp += disp_parm(bp, param_len);
switch (i) {
case 0:
verbprintf(0, " Deactivation (indicator off)");
break;
case 0xff:
verbprintf(0, " Activation (indicator on)");
break;
default:
verbprintf(0, " (0x%02x indicator unknown)", i);
break;
}
break;
case 0x0D: /* Message Identification */
ptr = bp;
param_len = *bp++;
msg_len -= param_len + 1;
verbprintf(0, " MI=");
i = *bp;
bp += disp_parm(bp, param_len);
switch (i) {
case 0:
verbprintf(0, " Removed Message");
break;
case 0x55:
verbprintf(0, " Message Reference only");
break;
case 0xff:
verbprintf(0, " Added Message");
break;
default:
verbprintf(0, " (0x%02x unknown)", i);
break;
}
verbprintf(0, " Message Reference:", *(ptr+3) * 0x100 + *(ptr+4));
break;
case 0x11: /* Call type */
param_len = *bp++;
msg_len -= param_len + 1;
verbprintf(0, " CT=");
i = *bp;
bp += disp_parm(bp, param_len);
switch (i) {
case 0:
verbprintf(0, " Voice call");
break;
case 0x02:
verbprintf(0, " Ring-back-when-free call");
break;
case 0x81:
verbprintf(0, " Message waiting call");
break;
default:
verbprintf(0, " (0x%02x indicator unknown)", i);
break;
}
break;
case 0x13: /* Number of Messages (Network Message System Status parameter type) */
param_len = *bp++;
msg_len -= param_len + 1;
verbprintf(0, " NMSS=");
i = *bp++;
verbprintf(0, "%d Number of message waiting in message system",i);
param_len--;
break;
case 0x20: /* Charge */
ptr = bp;
param_len = *bp++;
msg_len -= param_len + 1;
verbprintf(0, " CH=");
i = *(ptr+6);
bp += disp_parm(bp, param_len);
verbprintf(0, " CUR=%c%c%c", *ptr, *(ptr+3), *(ptr+4) );
verbprintf(0, (i & 1)?" Free of Charge":" Normal charge" );
verbprintf(0, (i & 2)?" Subtotal (AOC-D)":" Total (AOC-E)" );
verbprintf(0, (i & 4)?" Credit/Debit Card Charging":" Normal charging" );
verbprintf(0, (i & 8)?" Charging information not available":" Charging information available" );
verbprintf(0, (i & 0x10)?" Charged units or, charged units and price per unit":" Currency amount" );
if ( (i & 0x60) == 0 ) {
verbprintf(0, " Current call charge");
}
else if ( (i & 0x60) == 1 ) {
verbprintf(0, " Accumulated charge (last call included)");
}
else if ( (i & 0x60) == 2 ) {
verbprintf(0, " Extra charge cumulated charging, e.g. call forwarded calls.");
}
else {
verbprintf(0, " (for future use)");
}
/* byte 7-16: Cost (10 digits): Digit 1 (most significant digit) or Units (5 digits): Digit 1 (most significant digit) */
break;
case 0x21: /* Additional Charge */
ptr = bp;
param_len = *bp++;
msg_len -= param_len + 1;
verbprintf(0, " ACH=");
i = *(ptr+6);
bp += disp_parm(bp, param_len);
verbprintf(0, " CUR=%c%c%c", *ptr, *(ptr+3), *(ptr+4) );
verbprintf(0, (i & 1)?" Free of Charge":" Normal charging" );
verbprintf(0, (i & 2)?" Subtotal (AOC-D)":" Total (AOC-E)" );
verbprintf(0, (i & 4)?" Credit/Debit Card Charging":" Normal billing" );
verbprintf(0, (i & 8)?" Charging information not available":" Charging information available" );
verbprintf(0, (i & 0x10)?" Charged units or, charged units and price per unit":" Currency amount" );
if ( (i & 60) == 0 ) {
verbprintf(0, " Current call charge");
}
else if ( (i & 60) == 1 ) {
verbprintf(0, " Accumulated charge (last call included)");
}
else if ( (i & 60) == 2 ) {
verbprintf(0, " Extra charge cumulated charging, e.g. call forwarded calls.");
}
else {
verbprintf(0, " (for future use)");
}
/* byte 7-16: Cost (10 digits): Digit 1 (most significant digit) or Units (5 digits): Digit 1 (most significant digit) */
break;
case 0x50: /* Display Information */
ptr = bp;
param_len = *bp++;
msg_len -= param_len + 1;
i = *bp++;
if ( (i & 0x70) == 0) {
switch ( i & 0xf ) {
case 0:
verbprintf(0, " Unknown or other");
break;
case 1:
verbprintf(0, " Positive acknowledgement");
break;
case 3:
verbprintf(0, " Negative acknowledgement");
break;
case 4:
verbprintf(0, " Advertisement");
break;
case 5:
verbprintf(0, " Network Provider Information");
break;
case 6:
verbprintf(0, " Remote User Provided information");
break;
default:
verbprintf(0, " unknown (0x%02x)", i);
break;
}
}
else if ( (i & 0x70) == 7) {
verbprintf(0, " Reserved for network operator use");
}
else {
verbprintf(0, " unknown (0x%02x)", i);
}
verbprintf(0, (i & 0x80)?" Stored information":" No stored information" );
verbprintf(0, " SMS=");
bp += disp_parm(bp, param_len-1);
break;
case 0x55: /* Service Information */
msg_len--;
break; /* This has a problem because on msg errors displays the sync burst as SI messages */
param_len = *bp++;
msg_len -= param_len + 1;
verbprintf(0, " SI=");
i = *bp;
bp += disp_parm(bp, param_len);
switch (i) {
case 0:
verbprintf(0, " Service not active");
break;
case 0xff:
verbprintf(0, " Service active");
break;
default:
verbprintf(0, " (0x%02x unknown)", i);
break;
}
break;
default:
msg_len--;
verbprintf(0, " unknown (0x%x)%c",i);
break;
}
}
if (!len) {
// verbprintf(0, "\n");
return;
}
verbprintf(0, "\n");
}
/* ---------------------------------------------------------------------- */
void clip_init(struct demod_state *s)
{
memset(&s->l2.uart, 0, sizeof(s->l2.uart));
s->l2.uart.rxptr = s->l2.uart.rxbuf;
}
/* ---------------------------------------------------------------------- */
void clip_rxbit(struct demod_state *s, int bit)
{
s->l2.uart.rxbitstream <<= 1;
s->l2.uart.rxbitstream |= !!bit;
if (!s->l2.uart.rxstate) {
switch (s->l2.uart.rxbitstream & 0x03) {
case 0x02: /* start bit */
s->l2.uart.rxstate = 1;
s->l2.uart.rxbitbuf = 0x100;
break;
case 0x00: /* no start bit */
case 0x03: /* consecutive stop bits*/
if ((s->l2.uart.rxptr - s->l2.uart.rxbuf) >= 1)
clip_disp_packet(s, s->l2.uart.rxbuf, s->l2.uart.rxptr - s->l2.uart.rxbuf);
s->l2.uart.rxptr = s->l2.uart.rxbuf;
break;
}
return;
}
if (s->l2.uart.rxbitstream & 1)
s->l2.uart.rxbitbuf |= 0x200;
// verbprintf(7, "b=%c", '0'+(s->l2.uart.rxbitstream & 1));
if (s->l2.uart.rxbitbuf & 1) {
if (s->l2.uart.rxptr >= s->l2.uart.rxbuf+sizeof(s->l2.uart.rxbuf)) {
s->l2.uart.rxstate = 0;
clip_disp_packet(s, s->l2.uart.rxbuf, s->l2.uart.rxptr - s->l2.uart.rxbuf);
verbprintf(1, "Error: packet size too large\n");
return;
}
if ( !(s->l2.uart.rxbitstream & 1) ) {
s->l2.uart.rxstate = 0;
verbprintf(1, "Error: stop bit is 0. Bad framing\n");
return;
}
*s->l2.uart.rxptr++ = s->l2.uart.rxbitbuf >> 1;
// verbprintf(6, "B=%02X ", (s->l2.uart.rxbitbuf >> 1) & 0xff);
// verbprintf(5, "%c", (s->l2.uart.rxbitbuf >> 1) & 0xff);
s->l2.uart.rxbitbuf = 0x100;
s->l2.uart.rxstate = 0;
return;
}
s->l2.uart.rxbitbuf >>= 1;
}
/* ---------------------------------------------------------------------- */