-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunpack.cc
559 lines (489 loc) · 10.9 KB
/
unpack.cc
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//
// unpack 87-bit JS8 messages
//
// Robert Morris, AB1HL
//
#include <string>
#include <vector>
#include <stdio.h>
#include <assert.h>
#include <string.h>
std::vector<std::string> words;
//
// load the (s,c)-dense compression words from words.txt
// from JS8Call's jsc_list.cpp
//
void
load_words()
{
if(words.size() > 0)
return;
FILE *fp = fopen("words.txt", "r");
if(fp == 0){
fprintf(stderr, "cannot open words.txt\n");
exit(1);
}
while(1){
char buf[512];
if(fgets(buf, sizeof(buf)-1, fp) == 0)
break;
int n = strlen(buf);
if(n < 1){
fprintf(stderr, "zero length buffer for words.txt\n");
exit(1);
}
if(buf[n-1] == '\n')
buf[n-1] = 0;
words.push_back(std::string(buf));
}
fclose(fp);
}
//
// a87[] holds the entire 87 decoded bits.
// really 72, omitting 12-bit CRC and 3 unused bits.
// first two are 1, 1.
//
std::string
unpack_dense(const int a87[87])
{
//
// (s,c)-dense compression.
// a sequence of 4-bit nibbles.
// combine the nibbles to produce indices into words[].
// from JS8Call's jsc.cpp
//
int s = 7;
int c = 9;
int base[8];
base[0] = 0;
base[1] = s;
base[2] = base[1] + s*c;
base[3] = base[2] + s*c*c;
base[4] = base[3] + s*c*c*c;
base[5] = base[4] + s*c*c*c*c;
base[6] = base[5] + s*c*c*c*c*c;
base[7] = base[6] + s*c*c*c*c*c*c;
std::string ret;
int i = 2; // a87[i]
int index = 0;
int k = 0; // nibble number in this index.
while(i + 4 <= 72){
int nibble =
(a87[i+0] << 3) |
(a87[i+1] << 2) |
(a87[i+2] << 1) |
(a87[i+3] << 0);
i += 4;
if(nibble >= s){
// not the last nibble of the index.
index = index*c + nibble - s;
k++;
} else {
// last nibble of the index.
index = index*s + nibble + base[k];
if(index < 0 || index >= words.size()){
fprintf(stderr, "unpack_dense: oops index %d words.size() %d\n",
index, (int)words.size());
} else {
ret = ret + words[index];
}
if(a87[i]){
ret = ret + " ";
}
i++;
index = 0;
k = 0;
}
}
if(a87[73])
ret += "<>";
return ret;
}
//
// from JS8Call's varicode.cpp
//
struct htht {
const char *a;
const char *b;
};
struct htht ht[] = {
{ " ", "01" },
{ "E", "100" },
{ "T", "1101" },
{ "A", "0011" },
{ "O", "11111" },
{ "I", "11100" },
{ "N", "10111" },
{ "S", "10100" },
{ "H", "00011" },
{ "R", "00000" },
{ "D", "111011" },
{ "L", "110011" },
{ "C", "110001" },
{ "U", "101101" },
{ "M", "101011" },
{ "W", "001011" },
{ "F", "001001" },
{ "G", "000101" },
{ "Y", "000011" },
{ "P", "1111011" },
{ "B", "1111001" },
{ ".", "1110100" },
{ "V", "1100101" },
{ "K", "1100100" },
{ "-", "1100001" },
{ "+", "1100000" },
{ "?", "1011001" },
{ "!", "1011000" },
{ "\"", "1010101" },
{ "X", "1010100" },
{ "0", "0010101" },
{ "J", "0010100" },
{ "1", "0010001" },
{ "Q", "0010000" },
{ "2", "0001001" },
{ "Z", "0001000" },
{ "3", "0000101" },
{ "5", "0000100" },
{ "4", "11110101" },
{ "9", "11110100" },
{ "8", "11110001" },
{ "6", "11110000" },
{ "7", "11101011" },
{ "/", "11101010" },
{ 0, 0 }
};
// padding is often needed; the receiver looks
// back from the end for the last 0-bit,
// and treats it and the 1's after it as padding.
std::string
unpack_huffman(const int a87[87])
{
int end = 71;
while(end > 0 && a87[end] != 0)
end -= 1;
std::string ret;
int i = 2;
while(i < end){
int j;
for(j = 0; ht[j].a; j++){
int k;
for(k = 0; ht[j].b[k] && i + k < 72; k++){
if(ht[j].b[k] == '1'){
if(a87[i+k] != 1)
break;
} else {
if(a87[i+k] != 0)
break;
}
}
if(ht[j].b[k] == '\0'){
break;
}
}
if(ht[j].a){
ret += ht[j].a;
i += strlen(ht[j].b);
} else {
ret += "?";
i++;
}
}
if(a87[73])
ret += "<>"; // end of the over
return ret;
}
//
// extract a run of decoded bits, turn into an int,
// most significant bit first.
//
unsigned long long
un(const int a87[87], int start, int n)
{
assert(n > 0 && n <= 64 && start + n <= 72);
unsigned long long x = 0;
for(int i = 0; i < n; i++){
x <<= 1;
x += a87[start+i];
}
return x;
}
//
// given a 28-bit integer, return a call sign.
//
std::string
unpack_call(unsigned int x)
{
char s[6+1];
char c1[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
char c2[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char c3[] = "0123456789";
char c4[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
s[6] = '\0';
s[5] = c4[x % 27];
x /= 27;
s[4] = c4[x % 27];
x /= 27;
s[3] = c4[x % 27];
x /= 27;
s[2] = c3[x % 10];
x /= 10;
s[1] = c2[x % 36];
x /= 36;
if(x < sizeof(c1)-1){
s[0] = c1[x];
} else {
s[0] = '?';
}
std::string ss(s);
// trim white space
while(ss.size() > 0 && ss[0] == ' '){
ss.erase(0, 1);
}
while(ss.size() > 0 && ss[ss.size()-1] == ' '){
ss.erase(ss.size()-1, 1);
}
return ss;
}
//
// directed_cmds[] from JS8Call's varicode.cpp
//
const char *directed_cmds[] = {
"SNR?", // query snr
"DIT DIT", // unused
"NACK", // negative acknowledge
"HEARING?", // query station calls heard
"GRID?", // query grid
">", // relay message
"STATUS?", // query idle message
"STATUS", // this is my status
"HEARING", // these are the stations i'm hearing
"MSG", // this is a complete message
"MSG TO:", // store message at a station
"QUERY", // generic query
"QUERY MSGS?", // do you have any stored messages?
"QUERY CALL", // can you transmit a ping to callsign?
"RESERVED", // reserved
"GRID", // this is my current grid locator
"INFO?", // what is your info message?
"INFO", // this is my info message
"FB", // fine business
"HW CPY?", // how do you copy?
"SK", // end of contact
"RR", // roger roger
"QSL?", // do you copy?
"QSL", // i copy
"CMD", // command
"SNR", // seen a station at the provided snr
"NO", // negative confirm
"YES", // confirm
"73", // best regards, end of contact
"ACK", // acknowledge
"AGN?", // repeat message
"TEXT", // 31: send freetext
};
//
// 3 bits at 0: type=011
// 28 bits at 3: from callsign
// 28 bits at 31: to callsign
// 5 bits at 59: cmd
// 1 bit at 64: from portable (/P)
// 1 bit at 65: to portable
// 6 bits at 66: e.g. SNR
//
std::string
unpack_directed(const int a87[87], std::string &other_call)
{
int portable_from = un(a87, 64, 1);
int portable_to = un(a87, 65, 1);
std::string call1 = unpack_call(un(a87, 3, 28)); // from
std::string call2 = unpack_call(un(a87, 31, 28)); // to
int cmd = un(a87, 59, 5);
int extra = un(a87, 66, 6);
other_call = call1;
std::string cmd_text;
if(cmd >= 0 && cmd < sizeof(directed_cmds) / sizeof(directed_cmds[0])){
cmd_text = directed_cmds[cmd];
}
std::string ret;
ret = "[";
ret += call1; // from
ret += " ";
ret += call2; // to
ret += " ";
if(cmd_text.size() > 0){
ret += cmd_text;
} else {
ret += std::to_string(cmd);
}
ret += " ";
if(cmd == 25){
// SNR
ret += std::to_string(extra - 31);
} else {
ret += std::to_string(extra);
}
ret += "]";
return ret;
}
//
// turn a 50-bit number into a callsign.
// from JS8Call's unpackAlphaNumeric50().
// used for callsign in heartbeat and CQ msgs.
//
std::string
unpack_50(unsigned long long x)
{
const char *v = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ /@";
char s[12];
int o = 11;
s[o--] = '\0';
s[o--] = v[x % 38];
x /= 38;
s[o--] = v[x % 38];
x /= 38;
s[o--] = v[x % 38];
x /= 38;
s[o--] = (x & 1) ? '/' : ' ';
x /= 2;
s[o--] = v[x % 38];
x /= 38;
s[o--] = v[x % 38];
x /= 38;
s[o--] = v[x % 38];
x /= 38;
s[o--] = (x & 1) ? '/' : ' ';
x /= 2;
s[o--] = v[x % 38];
x /= 38;
s[o--] = v[x % 38];
x /= 38;
s[o--] = v[x % 39];
// squeeze out spaces.
std::string ss;
for(int i = 0; s[i] != '\0'; i++){
if(s[i] != ' '){
ss.push_back(s[i]);
}
}
return ss;
}
//
// heartbeat/CQ message.
//
// 3 bits at 0: 0 0 0
// 50 bits at 3: callsign
// 16 bits at 53: 16-bit "num"
// 3 bits at 69: "bits3"
//
// if high (15th) bit of num is clear, it's a heartbeat,
// and bits3 indexes hbs[].
//
// if high (15th) bit of num is set, it's a CQ.
// and bits3 indexes cqs[].
//
const char *hbs[] = {
"HB",
"HB AUTO",
"HB AUTO RELAY",
"HB AUTO RELAY SPOT",
"HB RELAY",
"HB RELAY SPOT",
"HB SPOT",
"HB AUTO SPOT",
};
const char *cqs[] = {
"CQ CQ CQ",
"CQ DX",
"CQ QRP",
"CQ CONTEST",
"CQ FIELD",
"CQ FD",
"CQ CQ",
"CQ",
};
// unpack a 15-bit grid square.
std::string
unpack_grid(int ng)
{
// maidenhead grid system:
// latitude from south pole to north pole.
// longitude eastward from anti-meridian.
// first: 20 degrees longitude.
// second: 10 degrees latitude.
// third: 2 degrees longitude.
// fourth: 1 degree latitude.
// the latitude is in the low % 180 of the 15-bit value,
// in one-degree units.
// the longitude is * 180, in 2-degree units.
int lat = (ng % 180);
int lng = ((ng / 180) * 2) - 180;
char tmp[5];
tmp[0] = 'A' + (179 - lng) / 20;
tmp[1] = 'A' + lat / 10;
tmp[2] = '0' + ((179 - lng) % 20) / 2;
tmp[3] = '0' + lat % 10;
tmp[4] = '\0';
return tmp;
}
std::string
unpack_heartbeat(const int a87[87], std::string &other_call)
{
other_call = unpack_50(un(a87, 3, 50));
unsigned int num = un(a87, 53, 16);
unsigned int bits3 = un(a87, 69, 3);
std::string ret;
ret += other_call;
ret += ": ";
if(num & 0x8000){
// cq
ret += cqs[bits3];
} else {
// heartbeat
ret += hbs[bits3];
}
if((num & 0x7fff) != 0x7fff){
ret += " ";
ret += unpack_grid(num & 0x7fff);
}
return ret;
}
//
// a87[] holds the entire 87 decoded bits.
// really 72, omitting 12-bit CRC and 3 unused bits.
//
std::string
unpack(const int a87[87], std::string &other_call)
{
other_call.erase();
load_words();
std::string msg;
if(a87[0] == 0 && a87[1] == 1 && a87[2] == 1){
msg = unpack_directed(a87, other_call);
} else if(a87[0] == 0 && a87[1] == 0 && a87[2] == 0){
msg = unpack_heartbeat(a87, other_call);
} else if(a87[0] == 1 && a87[1] == 0){
msg = unpack_huffman(a87);
} else if(a87[0] == 1 && a87[1] == 1){
msg = unpack_dense(a87);
} else if(a87[0] == 0 && a87[1] == 0 && a87[2] == 1){
// Compound
return "Compound";
} else if(a87[0] == 0 && a87[1] == 1 && a87[2] == 0){
// Compound Directed
return "CompoundDirected";
} else {
char buf[512];
sprintf(buf, "can't parse, starts with %d %d %d\n",
a87[0], a87[1], a87[2]);
return std::string(buf);
}
#if 0
char anno[64];
sprintf(anno, " <%d%d%d, %d%d%d>",
a87[0], a87[1], a87[2],
a87[72], a87[73], a87[74]);
msg += std::string(anno);
#endif
return msg;
}