ft8_lib/ft8/unpack.cpp

356 lines
9.6 KiB
C++
Raw Normal View History

#include "unpack.h"
2018-12-24 20:22:26 +08:00
#include "text.h"
#include <string.h>
namespace ft8 {
2018-12-24 20:22:26 +08:00
//const uint32_t NBASE = 37L*36L*10L*27L*27L*27L;
const uint32_t MAX22 = 4194304L;
const uint32_t NTOKENS = 2063592L;
const uint16_t MAXGRID4 = 32400L;
// n28 is a 28-bit integer, e.g. n28a or n28b, containing all the
// call sign bits from a packed message.
int unpack28(uint32_t n28, uint8_t ip, uint8_t i3, char *result) {
2018-12-24 20:22:26 +08:00
// Check for special tokens DE, QRZ, CQ, CQ_nnn, CQ_aaaa
if (n28 < NTOKENS) {
if (n28 <= 2) {
if (n28 == 0) strcpy(result, "DE");
if (n28 == 1) strcpy(result, "QRZ");
if (n28 == 2) strcpy(result, "CQ");
return 0; // Success
}
if (n28 <= 1002) {
// CQ_nnn with 3 digits
strcpy(result, "CQ ");
int_to_dd(result + 3, n28 - 3, 3);
return 0; // Success
}
if (n28 <= 532443L) {
// CQ_aaaa with 4 alphanumeric symbols
uint32_t n = n28 - 1003;
char aaaa[5];
aaaa[4] = '\0';
for (int i = 3; /* */; --i) {
aaaa[i] = charn(n % 27, 4);
if (i == 0) break;
n /= 27;
2018-12-24 20:22:26 +08:00
}
strcpy(result, "CQ ");
strcat(result, trim_front(aaaa));
2018-12-24 20:22:26 +08:00
return 0; // Success
}
// ? TODO: unspecified in the WSJT-X code
return -1;
}
n28 = n28 - NTOKENS;
if (n28 < MAX22) {
// This is a 22-bit hash of a result
//n22=n28
//call hash22(n22,c13) !Retrieve result from hash table
// TODO: implement
return -2;
}
// Standard callsign
uint32_t n = n28 - MAX22;
char callsign[7];
callsign[6] = '\0';
callsign[5] = charn(n % 27, 4);
n /= 27;
callsign[4] = charn(n % 27, 4);
n /= 27;
callsign[3] = charn(n % 27, 4);
n /= 27;
callsign[2] = charn(n % 10, 3);
n /= 10;
callsign[1] = charn(n % 36, 2);
n /= 36;
callsign[0] = charn(n % 37, 1);
// Skip trailing and leading whitespace in case of a short callsign
strcpy(result, trim(callsign));
// Check if we should append /R or /P suffix
if (ip) {
if (i3 == 1) {
strcat(result, "/R");
}
else if (i3 == 2) {
strcat(result, "/P");
}
}
2018-12-24 20:22:26 +08:00
return 0; // Success
}
int unpack_type1(const uint8_t *a77, uint8_t i3, char *message) {
uint32_t n28a, n28b;
uint16_t igrid4;
uint8_t ir;
// Extract packed fields
// read(c77,1000) n28a,ipa,n28b,ipb,ir,igrid4,i3
// 1000 format(2(b28,b1),b1,b15,b3)
n28a = (a77[0] << 21);
n28a |= (a77[1] << 13);
n28a |= (a77[2] << 5);
n28a |= (a77[3] >> 3);
n28b = ((a77[3] & 0x07) << 26);
n28b |= (a77[4] << 18);
n28b |= (a77[5] << 10);
n28b |= (a77[6] << 2);
n28b |= (a77[7] >> 6);
ir = ((a77[7] & 0x20) >> 5);
igrid4 = ((a77[7] & 0x1F) << 10);
igrid4 |= (a77[8] << 2);
igrid4 |= (a77[9] >> 6);
// Unpack both callsigns
char field_1[14];
char field_2[14];
if (unpack28(n28a >> 1, n28a & 0x01, i3, field_1) < 0) {
2018-12-24 20:22:26 +08:00
return -1;
}
if (unpack28(n28b >> 1, n28b & 0x01, i3, field_2) < 0) {
2018-12-24 20:22:26 +08:00
return -2;
}
// Fix "CQ_" to "CQ " -> already done in unpack28()
// if (starts_with(field_1, "CQ_")) {
// field_1[2] = ' ';
// }
// Append first two fields to the result
strcpy(message, field_1);
strcat(message, " ");
strcat(message, field_2);
// TODO: add to recent calls
// if (field_1[0] != '<' && strlen(field_1) >= 4) {
// save_hash_call(field_1)
// }
// if (field_2[0] != '<' && strlen(field_2) >= 4) {
// save_hash_call(field_2)
// }
2018-12-24 20:22:26 +08:00
char field_3[5];
if (igrid4 <= MAXGRID4) {
// Extract 4 symbol grid locator
field_3[4] = '\0';
uint16_t n = igrid4;
field_3[3] = '0' + (n % 10);
n /= 10;
field_3[2] = '0' + (n % 10);
n /= 10;
field_3[1] = 'A' + (n % 18);
n /= 18;
field_3[0] = 'A' + (n % 18);
if (ir != 0) {
// In case of ir=1 add an " R " before grid
strcat(message, " R ");
}
}
else {
// Extract report
int irpt = igrid4 - MAXGRID4;
// Check special cases first
if (irpt == 1) field_3[0] = '\0';
else if (irpt == 2) strcpy(field_3, "RRR");
else if (irpt == 3) strcpy(field_3, "RR73");
else if (irpt == 4) strcpy(field_3, "73");
else if (irpt >= 5) {
// Extract signal report as a two digit number with a + or - sign
if (ir == 0) {
int_to_dd(field_3, irpt - 35, 2, true);
}
else {
field_3[0] = 'R';
int_to_dd(field_3 + 1, irpt - 35, 2, true);
}
}
}
// Append the last field to the result
if (strlen(field_3) > 0) {
strcat(message, " ");
strcat(message, field_3);
}
return 0; // Success
}
int unpack_text(const uint8_t *a71, char *text) {
// TODO: test
uint8_t b71[9];
uint8_t carry = 0;
2018-12-24 20:22:26 +08:00
for (int i = 0; i < 9; ++i) {
b71[i] = carry | (a71[i] >> 1);
carry = (a71[i] & 1) ? 0x80 : 0;
2018-12-24 20:22:26 +08:00
}
for (int idx = 12; idx >= 0; --idx) {
2018-12-24 20:22:26 +08:00
// Divide the long integer in b71 by 42
uint16_t rem = 0;
for (int i = 0; i < 9; ++i) {
2018-12-24 20:22:26 +08:00
rem = (rem << 8) | b71[i];
b71[i] = rem / 42;
rem = rem % 42;
}
text[idx] = charn(rem, 0);
}
text[13] = '\0';
return 0; // Success
}
int unpack_telemetry(const uint8_t *a71, char *telemetry) {
uint8_t b71[9];
// Shift bits in a71 right by 1
uint8_t carry = 0;
for (int i = 0; i < 9; ++i) {
b71[i] = (carry << 7) | (a71[i] >> 1);
carry = (a71[i] & 0x01);
}
// Convert b71 to hexadecimal string
for (int i = 0; i < 9; ++i) {
uint8_t nibble1 = (b71[i] >> 4);
uint8_t nibble2 = (b71[i] & 0x0F);
char c1 = (nibble1 > 9) ? (nibble1 - 10 + 'A') : nibble1 + '0';
char c2 = (nibble2 > 9) ? (nibble2 - 10 + 'A') : nibble2 + '0';
telemetry[i * 2] = c1;
telemetry[i * 2 + 1] = c2;
}
telemetry[18] = '\0';
return 0;
}
//none standard for wsjt-x 2.0
//by KD8CEC
int unpack_nonstandard(const uint8_t *a77, char *message)
{
/*
wsjt-x 2.1.0 rc5
read(c77,1050) n12,n58,iflip,nrpt,icq
1050 format(b12,b58,b1,b2,b1)
*/
uint32_t n12, iflip, nrpt, icq;
uint64_t n58;
n12 = (a77[0] << 4); //11 ~4 : 8
n12 |= (a77[1] >> 4); //3~0 : 12
n58 = ((uint64_t)(a77[1] & 0x0F) << 54); //57 ~ 54 : 4
n58 |= ((uint64_t)a77[2] << 46); //53 ~ 46 : 12
n58 |= ((uint64_t)a77[3] << 38); //45 ~ 38 : 12
n58 |= ((uint64_t)a77[4] << 30); //37 ~ 30 : 12
n58 |= ((uint64_t)a77[5] << 22); //29 ~ 22 : 12
n58 |= ((uint64_t)a77[6] << 14); //21 ~ 14 : 12
n58 |= ((uint64_t)a77[7] << 6); //13 ~ 6 : 12
n58 |= ((uint64_t)a77[8] >> 2); //5 ~ 0 : 765432 10
iflip = (a77[8] >> 1) & 0x01; //76543210
nrpt = ((a77[8] & 0x01) << 1);
nrpt |= (a77[9] >> 7); //76543210
icq = ((a77[9] >> 6) & 0x01);
char c11[12];
c11[11] = '\0';
for (int i = 10; /* no condition */ ; --i) {
c11[i] = charn(n58 % 38, 5);
if (i == 0) break;
n58 /= 38;
}
char call_3[15];
call_3[0] = '<';
call_3[1] = '>';
call_3[2] = '\0';
//hash12(n12, call_3);
char * call_1 = (iflip) ? c11 : call_3;
char * call_2 = (iflip) ? call_3 : c11;
//save_hash_call(c11_trimmed);
if (icq == 0) {
strcpy(message, trim(call_1));
strcat(message, " ");
strcat(message, trim(call_2));
if (nrpt == 1)
strcat(message, " RRR");
else if (nrpt == 2)
strcat(message, " RR73");
else if (nrpt == 3)
strcat(message, " 73");
} else {
strcpy(message, "CQ ");
strcat(message, trim(call_2));
}
return 0;
}
2018-12-24 20:22:26 +08:00
int unpack77(const uint8_t *a77, char *message) {
uint8_t n3, i3;
// Extract n3 (bits 71..73) and i3 (bits 74..76)
n3 = ((a77[8] << 2) & 0x04) | ((a77[9] >> 6) & 0x03);
2018-12-24 20:22:26 +08:00
i3 = (a77[9] >> 3) & 0x07;
if (i3 == 0 && n3 == 0) {
// 0.0 Free text
return unpack_text(a77, message);
}
// else if (i3 == 0 && n3 == 1) {
// // 0.1 K1ABC RR73; W9XYZ <KH1/KH7Z> -11 28 28 10 5 71 DXpedition Mode
// }
// else if (i3 == 0 && n3 == 2) {
// // 0.2 PA3XYZ/P R 590003 IO91NP 28 1 1 3 12 25 70 EU VHF contest
// }
// else if (i3 == 0 && (n3 == 3 || n3 == 4)) {
// // 0.3 WA9XYZ KA1ABC R 16A EMA 28 28 1 4 3 7 71 ARRL Field Day
// // 0.4 WA9XYZ KA1ABC R 32A EMA 28 28 1 4 3 7 71 ARRL Field Day
// }
2018-12-24 20:22:26 +08:00
else if (i3 == 0 && n3 == 5) {
// 0.5 0123456789abcdef01 71 71 Telemetry (18 hex)
return unpack_telemetry(a77, message);
}
else if (i3 == 1 || i3 == 2) {
// Type 1 (standard message) or Type 2 ("/P" form for EU VHF contest)
return unpack_type1(a77, i3, message);
}
// else if (i3 == 3) {
// // Type 3: ARRL RTTY Contest
// }
else if (i3 == 4) {
// // Type 4: Nonstandard calls, e.g. <WA9XYZ> PJ4/KA1ABC RR73
// // One hashed call or "CQ"; one compound or nonstandard call with up
// // to 11 characters; and (if not "CQ") an optional RRR, RR73, or 73.
return unpack_nonstandard(a77, message);
2018-12-24 20:22:26 +08:00
}
// else if (i3 == 5) {
// // Type 5: TU; W9XYZ K1ABC R-09 FN 1 28 28 1 7 9 74 WWROF contest
// }
2018-12-24 20:22:26 +08:00
// unknown type, should never get here
message[0] = '\0';
2018-12-24 20:22:26 +08:00
return 0;
}
} // namespace