comparison trau-decode/parse-hex16.c @ 84:5173515e1cc8

trau-decode: new program trau-parse-hex
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 25 Feb 2025 05:36:25 +0000
parents trau-decode/trau-hr-dump-hex.c@b518ab15b518
children
comparison
equal deleted inserted replaced
83:5e2ecfd45fc3 84:5173515e1cc8
1 /*
2 * This program reads a line-based text file in an ad hoc hex format
3 * where each line represents a TRAU-16k frame. These TRAU-16k frames
4 * are then decoded.
5 */
6
7 #include <ctype.h>
8 #include <stdio.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <strings.h>
13
14 static int
15 decode_hex_digit(c)
16 {
17 if (isdigit(c))
18 return c - '0';
19 else if (isupper(c))
20 return c - 'A' + 10;
21 else
22 return c - 'a' + 10;
23 }
24
25 static void
26 hex2bits(hex, bits)
27 char *hex;
28 uint8_t *bits;
29 {
30 unsigned n, m, x;
31
32 for (n = 0; n < 80; n++) {
33 x = decode_hex_digit(hex[n]);
34 for (m = 8; m; m >>= 1) {
35 if (x & m)
36 *bits++ = 1;
37 else
38 *bits++ = 0;
39 }
40 }
41 }
42
43 static unsigned
44 bits_to_num(bits, nbits)
45 uint8_t *bits;
46 unsigned nbits;
47 {
48 unsigned accum;
49 unsigned n;
50
51 accum = 0;
52 for (n = 0; n < nbits; n++) {
53 accum <<= 1;
54 if (*bits)
55 accum |= 1;
56 bits++;
57 }
58 return accum;
59 }
60
61 static void
62 process_frame(frame_bits)
63 uint8_t *frame_bits;
64 {
65 unsigned c1_5, c6_11;
66
67 printf(" C1-C5: %u%u%u%u%u", frame_bits[17], frame_bits[18],
68 frame_bits[19], frame_bits[20], frame_bits[21]);
69 c1_5 = bits_to_num(frame_bits + 17, 5);
70 switch (c1_5) {
71 case 0x02:
72 fputs(" (FR UL)", stdout);
73 break;
74 case 0x1C:
75 fputs(" (FR DL)", stdout);
76 break;
77 case 0x1A:
78 fputs(" (EFR)", stdout);
79 break;
80 case 0x10:
81 fputs(" (idle UL)", stdout);
82 break;
83 case 0x0E:
84 fputs(" (idle DL)", stdout);
85 break;
86 case 0x08:
87 fputs(" (data UL)", stdout);
88 break;
89 case 0x16:
90 fputs(" (data DL)", stdout);
91 break;
92 case 0x09:
93 fputs(" (HR data UL)", stdout);
94 break;
95 case 0x17:
96 fputs(" (HR data DL)", stdout);
97 break;
98 case 0x14:
99 fputs(" (D144 sync)", stdout);
100 break;
101 case 0x1F:
102 fputs(" (E-TRAU)", stdout);
103 break;
104 case 0x06:
105 fputs(" (AMR)", stdout);
106 break;
107 }
108 putchar('\n');
109 switch (c1_5) {
110 case 0x02:
111 case 0x1C:
112 case 0x1A:
113 case 0x10:
114 case 0x0E:
115 c6_11 = bits_to_num(frame_bits + 22, 6);
116 printf(" C6-C11: %u\n", c6_11);
117 printf(" C12=%u C13=%u C14=%u C15=%u\n", frame_bits[28],
118 frame_bits[29], frame_bits[30], frame_bits[31]);
119 print_fr_efr_frame(frame_bits, c1_5);
120 printf(" C16=%u C17=%u C18=%u C19=%u C20=%u C21=%u\n",
121 frame_bits[310], frame_bits[311], frame_bits[312],
122 frame_bits[313], frame_bits[314], frame_bits[315]);
123 printf(" T1=%u T2=%u T3=%u T4=%u\n", frame_bits[316],
124 frame_bits[317], frame_bits[318], frame_bits[319]);
125 break;
126 case 0x08:
127 case 0x09:
128 case 0x16:
129 case 0x17:
130 case 0x14:
131 print_data_frame(frame_bits);
132 break;
133 case 0x1F:
134 print_edata_frame(frame_bits);
135 break;
136 case 0x06:
137 print_amr_frame(frame_bits);
138 break;
139 default:
140 printf(" C6-C15: %u%u%u%u%u%u%u%u%u%u\n", frame_bits[22],
141 frame_bits[23], frame_bits[24], frame_bits[25],
142 frame_bits[26], frame_bits[27], frame_bits[28],
143 frame_bits[29], frame_bits[30], frame_bits[31]);
144 printf(" C16=%u C17=%u C18=%u C19=%u C20=%u C21=%u\n",
145 frame_bits[310], frame_bits[311], frame_bits[312],
146 frame_bits[313], frame_bits[314], frame_bits[315]);
147 printf(" T1=%u T2=%u T3=%u T4=%u\n", frame_bits[316],
148 frame_bits[317], frame_bits[318], frame_bits[319]);
149 }
150 }
151
152 static void
153 process_record(hex_frame, lineno)
154 char *hex_frame;
155 {
156 uint8_t frame_bits[320];
157
158 printf("line %d:\n%s\n", lineno, hex_frame);
159 hex2bits(hex_frame, frame_bits);
160 process_frame(frame_bits);
161 }
162
163 static void
164 process_line(linebuf, filename, lineno)
165 char *linebuf, *filename;
166 {
167 char *cp, *np;
168 unsigned n;
169
170 for (cp = linebuf; isspace(*cp); cp++)
171 ;
172 if (*cp == '\0' || *cp == '#')
173 return;
174 np = cp;
175 for (n = 0; n < 80; n++) {
176 if (!isxdigit(*cp))
177 goto inv;
178 cp++;
179 }
180 if (*cp) {
181 if (!isspace(*cp))
182 goto inv;
183 *cp++ = '\0';
184 }
185 while (isspace(*cp))
186 cp++;
187 if (*cp != '\0' && *cp != '#')
188 goto inv;
189 process_record(np, lineno);
190 return;
191
192 inv: fprintf(stderr, "%s line %d: invalid syntax\n", filename, lineno);
193 exit(1);
194 }
195
196 main(argc, argv)
197 char **argv;
198 {
199 FILE *inf;
200 char linebuf[128];
201 int lineno;
202
203 if (argc != 2) {
204 fprintf(stderr, "usage: %s hex-file\n", argv[0]);
205 exit(1);
206 }
207 inf = fopen(argv[1], "r");
208 if (!inf) {
209 perror(argv[1]);
210 exit(1);
211 }
212 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++)
213 process_line(linebuf, argv[1], lineno);
214 exit(0);
215 }