File: pocsag.c

package info (click to toggle)
multimon 1.0-7.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 576 kB
  • ctags: 924
  • sloc: ansic: 8,144; makefile: 233; perl: 41
file content (476 lines) | stat: -rw-r--r-- 12,635 bytes parent folder | download | duplicates (7)
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
/*
 *      pocsag.c -- POCSAG protocol decoder
 *
 *      Copyright (C) 1996  
 *          Thomas Sailer (sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu)
 *
 *      POCSAG (Post Office Code Standard Advisory Group)
 *      Radio Paging Decoder
 *
 *	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>

/* ---------------------------------------------------------------------- */

#define CHARSET_LATIN1

/* ---------------------------------------------------------------------- */

/*
 * the code used by POCSAG is a (n=31,k=21) BCH Code with dmin=5,
 * thus it could correct two bit errors in a 31-Bit codeword.
 * It is a systematic code.
 * The generator polynomial is: 
 *   g(x) = x^10+x^9+x^8+x^6+x^5+x^3+1
 * The parity check polynomial is: 
 *   h(x) = x^21+x^20+x^18+x^16+x^14+x^13+x^12+x^11+x^8+x^5+x^3+1
 * g(x) * h(x) = x^n+1
 */
#define BCH_POLY 03551 /* octal */
#define BCH_N    31
#define BCH_K    21

/*
 * some codewords with special POCSAG meaning
 */
#define POCSAG_SYNC     0x7cd215d8
#define POCSAG_SYNCINFO 0x7cf21436
#define POCSAG_IDLE     0x7a89c197

#define POCSAG_SYNC_WORDS ((2000000 >> 3) << 13)

/* ---------------------------------------------------------------------- */

static unsigned char service_mask = 0x87;

/* ---------------------------------------------------------------------- */

static inline unsigned char even_parity(unsigned long data) 
{
	unsigned int temp = data ^ (data >> 16);
	
	temp = temp ^ (temp >> 8);
	temp = temp ^ (temp >> 4);
	temp = temp ^ (temp >> 2);
	temp = temp ^ (temp >> 1);
	return temp & 1;
}

/* ---------------------------------------------------------------------- */

static unsigned long pocsag_code(unsigned long data) 
{
	unsigned long ret = data << (BCH_N-BCH_K), shreg = ret;
	unsigned long mask = 1L << (BCH_N-1), coeff = BCH_POLY << (BCH_K-1);
	int n = BCH_K;
	
	for(; n > 0; mask >>= 1, coeff >>= 1, n--)
		if (shreg & mask)
			shreg ^= coeff;
	ret ^= shreg;
	ret = (ret << 1) | even_parity(ret);
	verbprintf(9, "BCH coder: data: %08lx shreg: %08lx ret: %08lx\n", 
		   data, shreg, ret);
	return ret;
}

/* ---------------------------------------------------------------------- */

static unsigned int pocsag_syndrome(unsigned long data) 
{
	unsigned long shreg = data >> 1; /* throw away parity bit */
	unsigned long mask = 1L << (BCH_N-1), coeff = BCH_POLY << (BCH_K-1);
	int n = BCH_K;
	
	for(; n > 0; mask >>= 1, coeff >>= 1, n--)
		if (shreg & mask)
			shreg ^= coeff;
	if (even_parity(data))
		shreg |= (1 << (BCH_N - BCH_K));
	verbprintf(9, "BCH syndrome: data: %08lx syn: %08lx\n", data, shreg);
	return shreg;
}

/* ---------------------------------------------------------------------- */

static void print_msg_numeric(struct l2_pocsag_rx *rx) 
{
	static const char *conv_table = "084 2.6]195-3U7[";
	unsigned char *bp = rx->buffer;
	int len = rx->numnibbles;
	char buf[256], *cp = buf;
       
	if (len >= sizeof(buf)) 
		len = sizeof(buf)-1;
	for (; len > 0; bp++, len -= 2) {
		*cp++ = conv_table[(*bp >> 4) & 0xf];
		if (len > 1)
			*cp++ = conv_table[*bp & 0xf];
	}
	*cp = '\0';
	verbprintf(0, "%s\n", buf);
}

/* ---------------------------------------------------------------------- */

static char *translate_alpha(unsigned char chr) 
{
	static const struct trtab {
		unsigned char code;
		char *str;
	} trtab[] = {{  0, "<NUL>" },
		     {  1, "<SOH>" }, 
		     {  2, "<STX>" }, 
		     {  3, "<ETX>" }, 
		     {  4, "<EOT>" }, 
		     {  5, "<ENQ>" }, 
		     {  6, "<ACK>" }, 
		     {  7, "<BEL>" }, 
		     {  8, "<BS>" }, 
		     {  9, "<HT>" }, 
		     { 10, "<LF>" }, 
		     { 11, "<VT>" }, 
		     { 12, "<FF>" }, 
		     { 13, "<CR>" }, 
		     { 14, "<SO>" }, 
		     { 15, "<SI>" }, 
		     { 16, "<DLE>" }, 
		     { 17, "<DC1>" }, 
		     { 18, "<DC2>" }, 
		     { 19, "<DC3>" }, 
		     { 20, "<DC4>" }, 
		     { 21, "<NAK>" }, 
		     { 22, "<SYN>" }, 
		     { 23, "<ETB>" }, 
		     { 24, "<CAN>" }, 
		     { 25, "<EM>" }, 
		     { 26, "<SUB>" }, 
		     { 27, "<ESC>" }, 
		     { 28, "<FS>" }, 
		     { 29, "<GS>" }, 
		     { 30, "<RS>" }, 
		     { 31, "<US>" }, 
#ifdef CHARSET_LATIN1
		     { 0x5b, "\304" }, /* upper case A dieresis */
		     { 0x5c, "\326" }, /* upper case O dieresis */
		     { 0x5d, "\334" }, /* upper case U dieresis */ 
		     { 0x7b, "\344" }, /* lower case a dieresis */
		     { 0x7c, "\366" }, /* lower case o dieresis */ 
		     { 0x7d, "\374" }, /* lower case u dieresis */ 
		     { 0x7e, "\337" }}; /* sharp s */
#else /* CHARSET_LATIN1 */
		     { 0x5b, "AE" }, /* upper case A dieresis */
		     { 0x5c, "OE" }, /* upper case O dieresis */
		     { 0x5d, "UE" }, /* upper case U dieresis */ 
		     { 0x7b, "ae" }, /* lower case a dieresis */
		     { 0x7c, "oe" }, /* lower case o dieresis */ 
		     { 0x7d, "ue" }, /* lower case u dieresis */ 
		     { 0x7e, "ss" }}; /* sharp s */
#endif /* CHARSET_LATIN1 */
	int min = 0, max = (sizeof(trtab) / sizeof(trtab[0])) - 1;
	
	/*
	 * binary search, list must be ordered!
	 */
	for (;;) {
		int mid = (min+max) >> 1;
		const struct trtab *tb = trtab + mid;
		int cmp = ((int) tb->code) - ((int) chr);

		if (!cmp)
			return tb->str;
		if (cmp < 0) {
			min = mid+1;
			if (min > max)
				return NULL;
		}
		if (cmp > 0) {
			max = mid-1;
			if (max < min)
				return NULL;
		}
	}
}				

/* ---------------------------------------------------------------------- */

static void print_msg_alpha(struct l2_pocsag_rx *rx) 
{
	unsigned long data = 0;
	int datalen = 0;
	unsigned char *bp = rx->buffer;
	int len = rx->numnibbles;
	char buf[256], *cp = buf;
	int buffree = sizeof(buf)-1;
	unsigned char curchr;
	char *tstr;
	
	while (len > 0) {
		while (datalen < 7 && len > 0) {
			if (len == 1) {
				data = (data << 4) | ((*bp >> 4) & 0xf);
				datalen += 4;
				len = 0;
			} else {
				data = (data << 8) | *bp++;
				datalen += 8;
				len -= 2;
			}
		}
		if (datalen < 7)
			continue;
		datalen -= 7;
		curchr = ((data >> datalen) & 0x7f) << 1;
		curchr = ((curchr & 0xf0) >> 4) | ((curchr & 0x0f) << 4);
		curchr = ((curchr & 0xcc) >> 2) | ((curchr & 0x33) << 2);
		curchr = ((curchr & 0xaa) >> 1) | ((curchr & 0x55) << 1);
		tstr = translate_alpha(curchr);
		if (tstr) {
			int tlen = strlen(tstr);
			if (buffree >= tlen) {
				memcpy(cp, tstr, tlen);
				cp += tlen;
				buffree -= tlen;
			}
		} else if (buffree > 0) {
			*cp++ = curchr;
			buffree--;
		}
	}
	*cp = '\0';
	verbprintf(0, "%s\n", buf);
}

/* ---------------------------------------------------------------------- */

static void print_msg_skyper(struct l2_pocsag_rx *rx) 
{
	unsigned long data = 0;
	int datalen = 0;
	unsigned char *bp = rx->buffer;
	int len = rx->numnibbles;
	char buf[256], *cp = buf;
	int buffree = sizeof(buf)-1;
	unsigned char curchr;
	char *tstr;
	
	while (len > 0) {
		while (datalen < 7 && len > 0) {
			if (len == 1) {
				data = (data << 4) | ((*bp >> 4) & 0xf);
				datalen += 4;
				len = 0;
			} else {
				data = (data << 8) | *bp++;
				datalen += 8;
				len -= 2;
			}
		}
		if (datalen < 7)
			continue;
		datalen -= 7;
		curchr = ((data >> datalen) & 0x7f) << 1;
		curchr = ((curchr & 0xf0) >> 4) | ((curchr & 0x0f) << 4);
		curchr = ((curchr & 0xcc) >> 2) | ((curchr & 0x33) << 2);
		curchr = ((curchr & 0xaa) >> 1) | ((curchr & 0x55) << 1);
		tstr = translate_alpha(curchr-1);
		if (tstr) {
			int tlen = strlen(tstr);
			if (buffree >= tlen) {
				memcpy(cp, tstr, tlen);
				cp += tlen;
				buffree -= tlen;
			}
		} else if (buffree > 0) {
			*cp++ = curchr-1;
			buffree--;
		}
	}
	*cp = '\0';
	verbprintf(0, "%s\n", buf);
}

/* ---------------------------------------------------------------------- */

static void pocsag_printmessage(struct demod_state *s, struct l2_pocsag_rx *rx, 
				const char *add_name) 
{
	verbprintf(0, "%s%s: Address: %7lu  Function: %1u\n",
		   s->dem_par->name, add_name, rx->adr, rx->func);
	if (!rx->numnibbles)
		return;
	if (service_mask & (0x01 << rx->func)) {
		verbprintf(0, "%s%s: Numeric: ", s->dem_par->name, add_name);
		print_msg_numeric(rx);
	}
	if (service_mask & (0x10 << rx->func)) {
		if (rx->func == 3 && rx->adr >= 4000 &&
		    rx->adr <= 5000) {
			verbprintf(0, "%s%s: Alpha (SKYPER): ", s->dem_par->name, add_name);
			print_msg_skyper(rx);
		} else {
			verbprintf(0, "%s%s: Alpha: ", s->dem_par->name, add_name);
			print_msg_alpha(rx);
		}
	}
}

/* ---------------------------------------------------------------------- */

void pocsag_init(struct demod_state *s)
{
	memset(&s->l2.pocsag, 0, sizeof(s->l2.pocsag));
}

/* ---------------------------------------------------------------------- */

static void do_one_bit(struct demod_state *s, struct l2_pocsag_rx *rx, 
		       unsigned long rx_data, const char *add_name)
{
	unsigned char rxword;

	if (!rx->rx_sync) {
		if (rx_data == POCSAG_SYNC || rx_data == POCSAG_SYNCINFO) {
			rx->rx_sync = 2;
			rx->rx_bit = rx->rx_word = 0;
			rx->func = -1;
			return;
		}
		return;
	}
   	
	if ((++(rx->rx_bit)) < 32) 
		return;
	/*
	 * one complete word received
	 */
	rx->rx_bit = 0;
	/*
	 * check codeword
	 */
	if (pocsag_syndrome(rx_data)) {
		/*
		 * codeword not valid
		 */
		rx->rx_sync--;
		verbprintf(7, "%s: Bad codeword: %08lx%s\n", 
			   s->dem_par->name, rx_data, 
			   rx->rx_sync ? "" : "sync lost");
		if (!(rx->func & (~3))) {
			verbprintf(0, "%s%s: Warning: message garbled\n",
				   s->dem_par->name, add_name);
			pocsag_printmessage(s, rx, add_name);
		}
		rx->func = -1; /* invalidate message */
		return;
	}
	/* do something with the data */
	verbprintf(8, "%s%s: Codeword: %08lx\n", s->dem_par->name, add_name, rx_data);
	rxword = rx->rx_word++;
	if (rxword >= 16) {
		/*
		 * received word shoud be a
		 * frame synch
		 */
		rx->rx_word = 0;
		if ((rx_data == POCSAG_SYNC) ||
		    (rx_data == POCSAG_SYNCINFO))
			rx->rx_sync = 10;
		else
			rx->rx_sync -= 2;
		return;	
	}
	if (rx_data == POCSAG_IDLE) {
		/*
		 * it seems that we can output the message right here
		 */
		if (!(rx->func & (~3)))
			pocsag_printmessage(s, rx, add_name);
		rx->func = -1; /* invalidate message */
		return;
	}
	if (rx_data & 0x80000000) {
		/*
		 * this is a data word
		 */
		unsigned long data;
		unsigned char *bp;
		
		if (rx->func & (~3)) { 
			/*
			 * no message being received 
			 */
			verbprintf(7, "%s%s: Lonesome data codeword: %08lx\n",
				   s->dem_par->name, add_name, rx_data);
			return;
		}
		if (rx->numnibbles > sizeof(rx->buffer)*2 - 5) {
			verbprintf(0, "%s%s: Warning: Message too long\n",
				   s->dem_par->name, add_name);
			pocsag_printmessage(s, rx, add_name);
			rx->func = -1;
			return;
		}
		bp = rx->buffer + (rx->numnibbles >> 1);
		data = rx_data >> 11; 
		if (rx->numnibbles & 1) {
			bp[0] = (bp[0] & 0xf0) | ((data >> 16) & 0xf);
			bp[1] = data >> 8;
			bp[2] = data;
		} else {
			bp[0] = data >> 12;
			bp[1] = data >> 4;
			bp[2] = data << 4;
		}
		rx->numnibbles += 5;
		return;
	}
	/*
	 * process address codeword
	 */
	if (rx_data >= POCSAG_SYNC_WORDS) {
		unsigned char func = (rx_data >> 11) & 3;
		unsigned long adr = ((rx_data >> 10) & 0x1ffff8) | 
			((rxword >> 1) & 7);

		verbprintf(0, "%s%s: Nonstandard address codeword: %08lx "
			   "func %1u adr %08lx\n", s->dem_par->name, add_name, rx_data, 
			   func, adr);
		return;
	}
	if (!(rx->func & (~3))) 
		pocsag_printmessage(s, rx, add_name);
	rx->func = (rx_data >> 11) & 3;
	rx->adr = ((rx_data >> 10) & 0x1ffff8) | ((rxword >> 1) & 7);
	rx->numnibbles = 0;
}

/* ---------------------------------------------------------------------- */

void pocsag_rxbit(struct demod_state *s, int bit)
{
	s->l2.pocsag.rx_data <<= 1;
	s->l2.pocsag.rx_data |= !bit;
	verbprintf(9, " %c ", '1'-(s->l2.pocsag.rx_data & 1));
	do_one_bit(s, s->l2.pocsag.rx, ~(s->l2.pocsag.rx_data), "+");
	do_one_bit(s, s->l2.pocsag.rx+1, s->l2.pocsag.rx_data, "-");
}

/* ---------------------------------------------------------------------- */