annotate d144/atrau-parse.c @ 63:4a906c2075bc

top Makefile: d144 depends on utils
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 25 Sep 2024 18:49:39 +0000
parents 323a4b75a32d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
57
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This program reads a binary file containing a capture of 64 kbit/s output
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * of a TRAU in 14.4 kbit/s data mode. Per GSM specs this output is supposed
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * to contain A-TRAU frames of TS 48.020 chapter 11 (RAA' function);
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * this program looks for such frames and parses them.
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 */
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <sys/types.h>
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdio.h>
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdint.h>
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdlib.h>
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 static void
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 decode_ra2(ts_bytes, frame_bits)
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 uint8_t *ts_bytes, *frame_bits;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 {
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 uint8_t *ip, *op;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 unsigned n;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 ip = ts_bytes;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 op = frame_bits;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 for (n = 0; n < 160; n++) {
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 *op++ = (*ip & 0x80) >> 7;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 *op++ = (*ip & 0x40) >> 6;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 ip++;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 }
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 }
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 static int
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 check_sync_zeros(frame_bits)
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 uint8_t *frame_bits;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 {
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 int i;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 for (i = 0; i < 16; i++) {
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 if (frame_bits[i])
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 return 0;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 return 1;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 }
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 static unsigned
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 gather_nibble(bits)
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 uint8_t *bits;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 {
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 unsigned n, acc;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 acc = 0;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 for (n = 0; n < 4; n++) {
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 acc <<= 1;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 acc |= *bits++;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 }
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 return acc;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 }
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 static void
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 dump_subframe(bits)
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 uint8_t *bits;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 {
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 unsigned n, d;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 printf(" %u ", *bits++);
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 for (n = 0; n < 9; n++) {
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 d = gather_nibble(bits);
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 printf("%x", d);
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 bits += 4;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 }
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 putchar('\n');
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 }
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 static void
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 dump_frame(frame_bits)
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 uint8_t *frame_bits;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 {
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 unsigned n;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76
58
a7ede064f883 atrau-parse: check sync one bit
Mychaela Falconia <falcon@freecalypso.org>
parents: 57
diff changeset
77 if (!check_sync_zeros(frame_bits) || !frame_bits[16])
57
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 printf(" Bad sync bits!\n");
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 printf(" C1-C5: %u%u%u%u%u\n", frame_bits[17], frame_bits[18],
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 frame_bits[19], frame_bits[20], frame_bits[21]);
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 printf(" M1=%u M2=%u\n", frame_bits[22], frame_bits[23]);
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 for (n = 0; n < 8; n++)
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 dump_subframe(frame_bits + 24 + n * 37);
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 }
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 main(argc, argv)
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 char **argv;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 {
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 FILE *binf;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 uint8_t frame_in[160], frame_bits[320];
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 u_long file_offset;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 int cc;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 if (argc != 2) {
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 fprintf(stderr, "usage: %s capture.bin\n", argv[0]);
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 exit(1);
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 }
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 binf = fopen(argv[1], "r");
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 if (!binf) {
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 perror(argv[1]);
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 exit(1);
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 }
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 for (file_offset = 0; ; file_offset += 160) {
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 cc = fread(frame_in, 1, 160, binf);
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 if (cc <= 0)
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 break;
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 if (cc != 160) {
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 fprintf(stderr,
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 "error: %s is not a multiple of 160 bytes\n",
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 argv[1]);
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 exit(1);
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 }
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 printf("Frame at 0x%lx:\n", file_offset);
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 decode_ra2(frame_in, frame_bits);
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 dump_frame(frame_bits);
59
323a4b75a32d atrau-parse: add blank separator lines to output
Mychaela Falconia <falcon@freecalypso.org>
parents: 58
diff changeset
116 putchar('\n');
57
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 }
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 exit(0);
fe3cdbbc96f9 d144: new program atrau-parse
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 }