diff --git a/README.md b/README.md index dfc8144..0c8c10e 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ JT65/JT9/JT4/FT8/WSPR/FSQ Encoder Library for Arduino ===================================================== -This library very simply generates a set of channel symbols for JT65, JT9, JT4, FT8, or WSPR based on the user providing a properly formatted Type 6 message for JT65, JT9, or JT4 (which is 13 valid characters), Type 0.0 or 0.5 message for FT8 (v2.0.0 protocol) or a Type 1, Type 2, or Type 3 message for WSPR. It will also generate an arbitrary FSQ message of up to 130 characters (non-directed) or 100 characters (directed). When paired with a synthesizer that can output frequencies in fine, phase-continuous tuning steps (such as the Si5351), then a beacon or telemetry transmitter can be created which can change the transmitted characters as needed from the Arduino. +This library very simply generates a set of channel symbols for JT65, JT9, JT4, FT8, or WSPR based on the user providing a properly formatted Type 6 message for JT65, JT9, or JT4 (which is 13 valid characters), Type 0.0 or 0.5 message for FT8 (v2.0.0 protocol) or a Type 1, Type 2, or Type 3 message for WSPR. With the full packmsg path enabled (`JTENCODE_FULL_PACKMSG`, see [Message Encoding Path](#message-encoding-path-compact-vs-full-packmsg)), JT65/JT9/JT4 also accept standard messages — callsign, grid, and report in the usual WSJT-X formats (including `CQ`/`QRZ`/`DE` prefixes and `/P`-style suffixes) — which are then encoded as the corresponding standard message types instead of as free text. It will also generate an arbitrary FSQ message of up to 130 characters (non-directed) or 100 characters (directed). When paired with a synthesizer that can output frequencies in fine, phase-continuous tuning steps (such as the Si5351), then a beacon or telemetry transmitter can be created which can change the transmitted characters as needed from the Arduino. Please feel free to use the issues feature of GitHub if you run into problems or have suggestions for important features to implement. @@ -23,6 +23,26 @@ RAM Usage --------- Most of the encoding functions need to manipulate multiple arrays of symbols in RAM at the same time, and therefore are quite RAM intensive. Care has been taken to put as much data into program memory as is possible, but the encoding functions still can cause problems with the low RAM microcontrollers such as the ATmegaxx8 series. If you are using these, then please be sure to call them only once when a transmit buffer needs to be created or changed, and call them separately of other subroutine calls. When using other microcontrollers that have more RAM, such as most of the ARM ICs, this won't be as much of a problem. If you see unusual freezes, that almost certainly indicates a RAM shortage. +Message Encoding Path (compact vs full packmsg) +------------------------------------------------ +The library encodes JT65/JT9/JT4 messages using one of two paths, selected at compile time. + +**Compact path (default)** — a simple free-text encoder: the message is uppercased and padded to 13 characters, then encoded as base-42 with the freeform flag set. This is what the library has done historically, and it is small enough to leave plenty of flash on a 32 KB Arduino Uno (~18 KB total for the JT9/JT4 path). It does **not** implement the standard message types (standard callsign/grid, CQ/QRZ/DE prefixes and suffixes, grid/report packing) — everything is transmitted as free text, so decoding tools see the message as free-form. + +**Full path** — a faithful C++ port of WSJT-X `packjt.f90` (`packmsg` and helpers): type detection, callsign packing (`CQ`/`QRZ`/`DE` and Swaziland/Guinea workarounds), 339-entry prefix and 12-entry suffix tables, Maidenhead grid and report packing (including the extended `KA`/`LA` report range), free-text fallback, and `entail`. It costs ~7.7 KB of extra flash (~25.8 KB total for JT9/JT4 on a Uno) and ~120 B of extra RAM. Verified bit-exact against the WSJT-X Fortran reference (28/28 messages, itype/dat/entail). Enable it by defining `JTENCODE_FULL_PACKMSG` in the sketch's build flags (see below for the exact options per toolchain). + +Note: enabling the full path changes the encoded symbols for standard messages relative to the compact path — the compact path sets the freeform flag unconditionally, so its symbols are not what WSJT-X would transmit. Decoded messages are unaffected by the encoder choice. JT65 uses the same `packmsg` port with the 12-byte `dat` block fed directly to the Reed-Solomon encoder (no `entail`); JT9/JT4 feed it through `entail`. + +The macro must be defined at compile time for the library itself — a `#define` in the sketch does **not** propagate to the library's translation units. The cleanest options are: + +- **PlatformIO** — `build_flags = -DJTENCODE_FULL_PACKMSG` in `platformio.ini` +- **arduino-cli** — `--build-property "build.extra_flags=-DJTENCODE_FULL_PACKMSG"` +- **Arduino IDE** — no built-in build-flag UI; use PlatformIO/arduino-cli, or copy the library into your sketch folder and uncomment the `#define` in `src/JTEncode.cpp` + +Note on the bundled example sketch: it uses a free-text message (`N0CALL AA00`), which encodes identically in both paths (the full path only changes standard messages). On an Arduino Uno the example still builds only with the compact path — at 28.4 KB it would overflow the 32 KB limit with the full path (~36.3 KB). On the Uno R4 WiFi the example builds with both paths (~72 KB basic / ~77 KB full, both comfortable). Use the compact path on an Uno; enable the full path on devices with more flash. + +If the build fails because of the full path, the default compact path is still in use — simply remove the flag. + WSPR Messages ------------- JTEncode includes support for all three WSPR message types. A brief listing of the three types is given below: @@ -171,7 +191,10 @@ Public Methods * Takes an arbitrary message of up to 13 allowable characters and returns * a channel symbol table. * - * message - Plaintext Type 6 message. + * message - Plaintext Type 6 message (compact path, the default). With + * JTENCODE_FULL_PACKMSG enabled, standard WSJT-X messages are also accepted + * (callsign, grid, and report in the usual formats) and are encoded as the + * corresponding standard message type instead of as free text. * symbols - Array of channel symbols to transmit returned by the method. * Ensure that you pass a uint8_t array of at least size JT65_SYMBOL_COUNT to the method. * @@ -185,7 +208,10 @@ Public Methods * Takes an arbitrary message of up to 13 allowable characters and returns * a channel symbol table. * - * message - Plaintext Type 6 message. + * message - Plaintext Type 6 message (compact path, the default). With + * JTENCODE_FULL_PACKMSG enabled, standard WSJT-X messages are also accepted + * (callsign, grid, and report in the usual formats) and are encoded as the + * corresponding standard message type instead of as free text. * symbols - Array of channel symbols to transmit returned by the method. * Ensure that you pass a uint8_t array of at least size JT9_SYMBOL_COUNT to the method. * @@ -200,7 +226,10 @@ Public Methods * Takes an arbitrary message of up to 13 allowable characters and returns * a channel symbol table. * - * message - Plaintext Type 6 message. + * message - Plaintext Type 6 message (compact path, the default). With + * JTENCODE_FULL_PACKMSG enabled, standard WSJT-X messages are also accepted + * (callsign, grid, and report in the usual formats) and are encoded as the + * corresponding standard message type instead of as free text. * symbols - Array of channel symbols to transmit returned by the method. * Ensure that you pass a uint8_t array of at least size JT4_SYMBOL_COUNT to the method. * @@ -324,6 +353,20 @@ Changelog --------- * v1.4.3 + * Full packmsg port verified bit-exact against the WSJT-X Fortran + reference (28/28 messages: `itype`, `dat`, and `entail` all match). + Fixes applied: (1) `grid2deg` now copies all 6 grid characters (was + truncating to 5, dropping the 6th and corrupting extended reports); + (2) the `c3` fill loop maps NUL bytes to blanks (was leaving NUL in + the 4th field of blank grids, forcing free-text fallback); + (3) `getpfx1` falls through to the generic prefix/suffix path when + the pfx/sfx tables miss (was returning early with a stale callsign), + and keeps a copy of the original callsign for the generic path; + (4) the full path now honors the Fortran early exits + (`nv2a >= 4`, `text1`, `text2` -> free text) and the add-on-grid + `k1 < 0 || k2 < 0 || k1*k2 != 0` check. The bundled example builds + on the Uno (compact, 28.4 KB) and Uno R4 WiFi (compact ~72.2 KB / + full ~77.3 KB); the Uno full-path build overflows by design. * Fix the JT4 encoder to produce frames that match WSJT-X. The previous implementation was wrong in several ways: it used a 207-symbol `JT4_SYMBOL_COUNT` (the JT4 frame is 206 symbols), it filled a diff --git a/examples/Si5351JTDemo/Si5351JTDemo.ino b/examples/Si5351JTDemo/Si5351JTDemo.ino index e821bf2..83f9c04 100644 --- a/examples/Si5351JTDemo/Si5351JTDemo.ino +++ b/examples/Si5351JTDemo/Si5351JTDemo.ino @@ -6,6 +6,10 @@ // (a Type 6 message) in JT65, JT9, JT4, a type 0.0 or type 0.5 FT8 message, // a FSQ message, or a standard Type 1 message in WSPR. // +// With the full packmsg path enabled (JTENCODE_FULL_PACKMSG), the JT +// modes also accept standard WSJT-X messages (callsign/grid/report), +// encoded as the corresponding standard message type instead of free text. +// // Connect a momentary push button to pin 12 to use as the // transmit trigger. Get fancy by adding your own code to trigger // off of the time from a GPS or your PC via virtual serial. diff --git a/src/JTEncode.cpp b/src/JTEncode.cpp index 274394c..42458c9 100644 --- a/src/JTEncode.cpp +++ b/src/JTEncode.cpp @@ -25,6 +25,7 @@ #include #include #include +#include "upstream_packmsg.h" #include #include @@ -796,6 +797,14 @@ void JTEncode::wspr_message_prep(char * call, char * loc, int8_t dbm) void JTEncode::jt65_bit_packing(char * message, uint8_t * c) { +#ifdef JTENCODE_FULL_PACKMSG + // Full path — faithful port of WSJT-X packmsg. JT65 uses the 12 6-bit + // data bytes directly (no entail — RS encodes dat[0..11]). + uint8_t dat[12]; + int itype = 6; + UpstreamPackmsg::upstream_packmsg(message, dat, itype); + memcpy(c, dat, 12); +#else uint32_t n1, n2, n3; // Find the N values @@ -836,10 +845,20 @@ void JTEncode::jt65_bit_packing(char * message, uint8_t * c) c[9] = ((n2 & 0x0003) << 4) + ((n3 >> 12) & 0x000f); c[10] = (n3 >> 6) & 0x003f; c[11] = n3 & 0x003f; +#endif } void JTEncode::jt9_bit_packing(char * message, uint8_t * c) { +#ifdef JTENCODE_FULL_PACKMSG + // Full path — faithful port of WSJT-X packmsg (type detection, callsign + // packing, prefix/suffix handling, grid/report packing, freeform fallback). + // Costs ~11 KB extra flash — only enable on devices with plenty of flash. + uint8_t dat[12]; + int itype = 6; + UpstreamPackmsg::upstream_packmsg(message, dat, itype); + UpstreamPackmsg::upstream_entail(dat, c); +#else uint32_t n1, n2, n3; // Find the N values @@ -896,6 +915,7 @@ void JTEncode::jt9_bit_packing(char * message, uint8_t * c) c[10] = 0; c[11] = 0; c[12] = 0; +#endif } void JTEncode::wspr_bit_packing(uint8_t * c) diff --git a/src/JTEncode.h b/src/JTEncode.h index 22b3632..b705c31 100644 --- a/src/JTEncode.h +++ b/src/JTEncode.h @@ -40,6 +40,27 @@ #define WSPR_SYMBOL_COUNT 162 #define FT8_SYMBOL_COUNT 79 +// --------------------------------------------------------------------------- +// Message encoding path +// --------------------------------------------------------------------------- +// By default the library uses the compact free-text message encoder +// (base-42 with the freeform flag). This is small enough to leave plenty of +// flash on a 32 KB Arduino Uno. +// +// Defining JTENCODE_FULL_PACKMSG (e.g. in platformio.ini or in the sketch's +// build flags) selects a faithful C++ port of WSJT-X packjt.f90 — type +// detection, callsign packing, prefix/suffix handling, Maidenhead grid and +// report packing, and free-text fallback. It costs ~7.7 KB of extra flash +// (~25.8 KB total for JT9/JT4 on a Uno) and ~120 B of extra RAM, so leave +// it off on tight devices and enable it on devices with plenty of flash +// (e.g. Uno R4 WiFi, Mega, ESP32). +// +// Note: the full path changes the encoded symbols for standard messages +// (JT9/JT4/JT65) relative to the compact path. JT65/JT9 decoding is +// unaffected by the encoder choice, but exact symbol comparisons against +// the compact encoder will not match. +// --------------------------------------------------------------------------- + #define JT65_ENCODE_COUNT 63 #define JT9_ENCODE_COUNT 69 #define FT8_ENCODE_COUNT 77 diff --git a/src/upstream_packmsg.cpp b/src/upstream_packmsg.cpp new file mode 100644 index 0000000..b0baf9b --- /dev/null +++ b/src/upstream_packmsg.cpp @@ -0,0 +1,648 @@ +// upstream_packmsg.cpp +// Faithful C++ port of WSJT-X lib/packjt.f90 (packmsg and helpers). +// Used by jt9_bit_packing / jt65_bit_packing in place of the simplified +// free-text packing — only compiled when JTENCODE_FULL_PACKMSG is defined. +// Returns 12 6-bit symbols (dat[0..11]) — the same 72-bit data block that +// upstream entail() packs into data0(1..9) and encode232 encodes. +// +// Message types (itype): +// 1 standard, 2 CQ/QRZ/DE prefix, 3 CQ/QRZ/DE suffix, +// 4 callsign prefix, 5 callsign suffix, 6 free text, -1 undeliverable +// +// Compile cost: ~11 KB flash (incl. 1.4 KB PROGMEM prefix table). +// Devices with tight flash (e.g. Arduino Uno) should leave +// JTENCODE_FULL_PACKMSG undefined to use the built-in compact encoder. + +#ifdef JTENCODE_FULL_PACKMSG + +#include "upstream_packmsg.h" +#include +#include + +#if defined(__AVR__) +#include +#define UPF_PROGMEM PROGMEM +#define UPF_READ_BYTE(p) pgm_read_byte((const uint8_t *)(p)) +#else +#define UPF_PROGMEM +#define UPF_READ_BYTE(p) (*(const uint8_t *)(p)) +#endif + +namespace UpstreamPackmsg { + +// --------------------------------------------------------------------------- +// Tables +// --------------------------------------------------------------------------- +// Prefix/suffix tables from WSJT-X lib/pfx.f90 (NZ=339, NZ2=12). +// Each pfx entry is a 5-character string (4 real chars + padding). +static const char pfx_table[339 * 4] UPF_PROGMEM = { + '1', 'A', ' ', ' ', '1', 'S', ' ', ' ', '3', 'A', ' ', ' ', '3', 'B', '6', ' ', '3', 'B', '8', ' ', '3', 'B', '9', ' ', '3', 'C', ' ', ' ', '3', 'C', '0', ' ', '3', 'D', '2', ' ', '3', 'D', '2', 'C', '3', 'D', '2', 'R', '3', 'D', 'A', ' ', + '3', 'V', ' ', ' ', '3', 'W', ' ', ' ', '3', 'X', ' ', ' ', '3', 'Y', ' ', ' ', '3', 'Y', 'B', ' ', '3', 'Y', 'P', ' ', '4', 'J', ' ', ' ', '4', 'L', ' ', ' ', '4', 'S', ' ', ' ', '4', 'U', '1', 'I', '4', 'U', '1', 'U', '4', 'W', ' ', ' ', + '4', 'X', ' ', ' ', '5', 'A', ' ', ' ', '5', 'B', ' ', ' ', '5', 'H', ' ', ' ', '5', 'N', ' ', ' ', '5', 'R', ' ', ' ', '5', 'T', ' ', ' ', '5', 'U', ' ', ' ', '5', 'V', ' ', ' ', '5', 'W', ' ', ' ', '5', 'X', ' ', ' ', '5', 'Z', ' ', ' ', + '6', 'W', ' ', ' ', '6', 'Y', ' ', ' ', '7', 'O', ' ', ' ', '7', 'P', ' ', ' ', '7', 'Q', ' ', ' ', '7', 'X', ' ', ' ', '8', 'P', ' ', ' ', '8', 'Q', ' ', ' ', '8', 'R', ' ', ' ', '9', 'A', ' ', ' ', '9', 'G', ' ', ' ', '9', 'H', ' ', ' ', + '9', 'J', ' ', ' ', '9', 'K', ' ', ' ', '9', 'L', ' ', ' ', '9', 'M', '2', ' ', '9', 'M', '6', ' ', '9', 'N', ' ', ' ', '9', 'Q', ' ', ' ', '9', 'U', ' ', ' ', '9', 'V', ' ', ' ', '9', 'X', ' ', ' ', '9', 'Y', ' ', ' ', 'A', '2', ' ', ' ', + 'A', '3', ' ', ' ', 'A', '4', ' ', ' ', 'A', '5', ' ', ' ', 'A', '6', ' ', ' ', 'A', '7', ' ', ' ', 'A', '9', ' ', ' ', 'A', 'P', ' ', ' ', 'B', 'S', '7', ' ', 'B', 'V', ' ', ' ', 'B', 'V', '9', ' ', 'B', 'Y', ' ', ' ', 'C', '2', ' ', ' ', + 'C', '3', ' ', ' ', 'C', '5', ' ', ' ', 'C', '6', ' ', ' ', 'C', '9', ' ', ' ', 'C', 'E', ' ', ' ', 'C', 'E', '0', 'X', 'C', 'E', '0', 'Y', 'C', 'E', '0', 'Z', 'C', 'E', '9', ' ', 'C', 'M', ' ', ' ', 'C', 'N', ' ', ' ', 'C', 'P', ' ', ' ', + 'C', 'T', ' ', ' ', 'C', 'T', '3', ' ', 'C', 'U', ' ', ' ', 'C', 'X', ' ', ' ', 'C', 'Y', '0', ' ', 'C', 'Y', '9', ' ', 'D', '2', ' ', ' ', 'D', '4', ' ', ' ', 'D', '6', ' ', ' ', 'D', 'L', ' ', ' ', 'D', 'U', ' ', ' ', 'E', '3', ' ', ' ', + 'E', '4', ' ', ' ', 'E', 'A', ' ', ' ', 'E', 'A', '6', ' ', 'E', 'A', '8', ' ', 'E', 'A', '9', ' ', 'E', 'I', ' ', ' ', 'E', 'K', ' ', ' ', 'E', 'L', ' ', ' ', 'E', 'P', ' ', ' ', 'E', 'R', ' ', ' ', 'E', 'S', ' ', ' ', 'E', 'T', ' ', ' ', + 'E', 'U', ' ', ' ', 'E', 'X', ' ', ' ', 'E', 'Y', ' ', ' ', 'E', 'Z', ' ', ' ', 'F', ' ', ' ', ' ', 'F', 'G', ' ', ' ', 'F', 'H', ' ', ' ', 'F', 'J', ' ', ' ', 'F', 'K', ' ', ' ', 'F', 'K', 'C', ' ', 'F', 'M', ' ', ' ', 'F', 'O', ' ', ' ', + 'F', 'O', 'A', ' ', 'F', 'O', 'C', ' ', 'F', 'O', 'M', ' ', 'F', 'P', ' ', ' ', 'F', 'R', ' ', ' ', 'F', 'R', 'G', ' ', 'F', 'R', 'J', ' ', 'F', 'R', 'T', ' ', 'F', 'T', '5', 'W', 'F', 'T', '5', 'X', 'F', 'T', '5', 'Z', 'F', 'W', ' ', ' ', + 'F', 'Y', ' ', ' ', 'M', ' ', ' ', ' ', 'M', 'D', ' ', ' ', 'M', 'I', ' ', ' ', 'M', 'J', ' ', ' ', 'M', 'M', ' ', ' ', 'M', 'U', ' ', ' ', 'M', 'W', ' ', ' ', 'H', '4', ' ', ' ', 'H', '4', '0', ' ', 'H', 'A', ' ', ' ', 'H', 'B', ' ', ' ', + 'H', 'B', '0', ' ', 'H', 'C', ' ', ' ', 'H', 'C', '8', ' ', 'H', 'H', ' ', ' ', 'H', 'I', ' ', ' ', 'H', 'K', ' ', ' ', 'H', 'K', '0', ' ', 'H', 'K', '0', 'M', 'H', 'L', ' ', ' ', 'H', 'M', ' ', ' ', 'H', 'P', ' ', ' ', 'H', 'R', ' ', ' ', + 'H', 'S', ' ', ' ', 'H', 'V', ' ', ' ', 'H', 'Z', ' ', ' ', 'I', ' ', ' ', ' ', 'I', 'S', ' ', ' ', 'I', 'S', '0', ' ', 'J', '2', ' ', ' ', 'J', '3', ' ', ' ', 'J', '5', ' ', ' ', 'J', '6', ' ', ' ', 'J', '7', ' ', ' ', 'J', '8', ' ', ' ', + 'J', 'A', ' ', ' ', 'J', 'D', 'M', ' ', 'J', 'D', 'O', ' ', 'J', 'T', ' ', ' ', 'J', 'W', ' ', ' ', 'J', 'X', ' ', ' ', 'J', 'Y', ' ', ' ', 'K', ' ', ' ', ' ', 'K', 'G', '4', ' ', 'K', 'H', '0', ' ', 'K', 'H', '1', ' ', 'K', 'H', '2', ' ', + 'K', 'H', '3', ' ', 'K', 'H', '4', ' ', 'K', 'H', '5', ' ', 'K', 'H', '5', 'K', 'K', 'H', '6', ' ', 'K', 'H', '7', ' ', 'K', 'H', '8', ' ', 'K', 'H', '9', ' ', 'K', 'L', ' ', ' ', 'K', 'P', '1', ' ', 'K', 'P', '2', ' ', 'K', 'P', '4', ' ', + 'K', 'P', '5', ' ', 'L', 'A', ' ', ' ', 'L', 'U', ' ', ' ', 'L', 'X', ' ', ' ', 'L', 'Y', ' ', ' ', 'L', 'Z', ' ', ' ', 'O', 'A', ' ', ' ', 'O', 'D', ' ', ' ', 'O', 'E', ' ', ' ', 'O', 'H', ' ', ' ', 'O', 'H', '0', ' ', 'O', 'J', '0', ' ', + 'O', 'K', ' ', ' ', 'O', 'M', ' ', ' ', 'O', 'N', ' ', ' ', 'O', 'X', ' ', ' ', 'O', 'Y', ' ', ' ', 'O', 'Z', ' ', ' ', 'P', '2', ' ', ' ', 'P', '4', ' ', ' ', 'P', 'A', ' ', ' ', 'P', 'J', '2', ' ', 'P', 'J', '7', ' ', 'P', 'Y', ' ', ' ', + 'P', 'Y', '0', 'F', 'P', 'T', '0', 'S', 'P', 'Y', '0', 'T', 'P', 'Z', ' ', ' ', 'R', '1', 'F', ' ', 'R', '1', 'M', ' ', 'S', '0', ' ', ' ', 'S', '2', ' ', ' ', 'S', '5', ' ', ' ', 'S', '7', ' ', ' ', 'S', '9', ' ', ' ', 'S', 'M', ' ', ' ', + 'S', 'P', ' ', ' ', 'S', 'T', ' ', ' ', 'S', 'U', ' ', ' ', 'S', 'V', ' ', ' ', 'S', 'V', 'A', ' ', 'S', 'V', '5', ' ', 'S', 'V', '9', ' ', 'T', '2', ' ', ' ', 'T', '3', '0', ' ', 'T', '3', '1', ' ', 'T', '3', '2', ' ', 'T', '3', '3', ' ', + 'T', '5', ' ', ' ', 'T', '7', ' ', ' ', 'T', '8', ' ', ' ', 'T', '9', ' ', ' ', 'T', 'A', ' ', ' ', 'T', 'F', ' ', ' ', 'T', 'G', ' ', ' ', 'T', 'I', ' ', ' ', 'T', 'I', '9', ' ', 'T', 'J', ' ', ' ', 'T', 'K', ' ', ' ', 'T', 'L', ' ', ' ', + 'T', 'N', ' ', ' ', 'T', 'R', ' ', ' ', 'T', 'T', ' ', ' ', 'T', 'U', ' ', ' ', 'T', 'Y', ' ', ' ', 'T', 'Z', ' ', ' ', 'U', 'A', ' ', ' ', 'U', 'A', '2', ' ', 'U', 'A', '9', ' ', 'U', 'K', ' ', ' ', 'U', 'N', ' ', ' ', 'U', 'R', ' ', ' ', + 'V', '2', ' ', ' ', 'V', '3', ' ', ' ', 'V', '4', ' ', ' ', 'V', '5', ' ', ' ', 'V', '6', ' ', ' ', 'V', '7', ' ', ' ', 'V', '8', ' ', ' ', 'V', 'E', ' ', ' ', 'V', 'K', ' ', ' ', 'V', 'K', '0', 'H', 'V', 'K', '0', 'M', 'V', 'K', '9', 'C', + 'V', 'K', '9', 'L', 'V', 'K', '9', 'M', 'V', 'K', '9', 'N', 'V', 'K', '9', 'W', 'V', 'K', '9', 'X', 'V', 'P', '2', 'E', 'V', 'P', '2', 'M', 'V', 'P', '2', 'V', 'V', 'P', '5', ' ', 'V', 'P', '6', ' ', 'V', 'P', '6', 'D', 'V', 'P', '8', ' ', + 'V', 'P', '8', 'G', 'V', 'P', '8', 'H', 'V', 'P', '8', 'O', 'V', 'P', '8', 'S', 'V', 'P', '9', ' ', 'V', 'Q', '9', ' ', 'V', 'R', ' ', ' ', 'V', 'U', ' ', ' ', 'V', 'U', '4', ' ', 'V', 'U', '7', ' ', 'X', 'E', ' ', ' ', 'X', 'F', '4', ' ', + 'X', 'T', ' ', ' ', 'X', 'U', ' ', ' ', 'X', 'W', ' ', ' ', 'X', 'X', '9', ' ', 'X', 'Z', ' ', ' ', 'Y', 'A', ' ', ' ', 'Y', 'B', ' ', ' ', 'Y', 'I', ' ', ' ', 'Y', 'J', ' ', ' ', 'Y', 'K', ' ', ' ', 'Y', 'L', ' ', ' ', 'Y', 'N', ' ', ' ', + 'Y', 'O', ' ', ' ', 'Y', 'S', ' ', ' ', 'Y', 'U', ' ', ' ', 'Y', 'V', ' ', ' ', 'Y', 'V', '0', ' ', 'Z', '2', ' ', ' ', 'Z', '3', ' ', ' ', 'Z', 'A', ' ', ' ', 'Z', 'B', ' ', ' ', 'Z', 'C', '4', ' ', 'Z', 'D', '7', ' ', 'Z', 'D', '8', ' ', + 'Z', 'D', '9', ' ', 'Z', 'F', ' ', ' ', 'Z', 'K', '1', 'N', 'Z', 'K', '1', 'S', 'Z', 'K', '2', ' ', 'Z', 'K', '3', ' ', 'Z', 'L', ' ', ' ', 'Z', 'L', '7', ' ', 'Z', 'L', '8', ' ', 'Z', 'L', '9', ' ', 'Z', 'P', ' ', ' ', 'Z', 'S', ' ', ' ', + 'Z', 'S', '8', ' ', 'K', 'C', '4', ' ', 'E', '5', ' ', ' ', +}; + +static const char sfx_table[13] UPF_PROGMEM = "P0123456789A"; + +// --------------------------------------------------------------------------- +// Helpers +// --------------------------------------------------------------------------- + +static inline char to_upper(char c) { + return (c >= 'a' && c <= 'z') ? (char)(c - 'a' + 'A') : c; +} + +// nchar: 0-36 for callsign packing (upstream: 0-9, A-Z->10-35, lowercase->same, ' '->36, else 36) +static int nchar(char c) { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'A' && c <= 'Z') return c - 'A' + 10; + if (c >= 'a' && c <= 'z') return c - 'a' + 10; + return 36; // space and anything >= ' ' +} + +static bool is_blank(char c) { return c == ' ' || c == 0; } + +static int len_trim(const char *s) { + int n = (int)strlen(s); + while (n > 0 && (s[n-1] == ' ' || s[n-1] == '\t')) n--; + return n; +} + + +// Manual integer parse (no atoi/sscanf on Arduino) — returns false if no digit found +static bool parse_int(const char *s, int &n) { + const char *p = s; + while (*p == ' ' || *p == '\t') p++; + int sign = 1; + if (*p == '-') { sign = -1; p++; } + else if (*p == '+') { p++; } + if (*p < '0' || *p > '9') return false; + int n2 = 0; + while (*p >= '0' && *p <= '9') { n2 = n2 * 10 + (*p - '0'); p++; } + n = sign * n2; + return true; +} + +static int my_atoi(const char *s) { + int n = 0; + if (parse_int(s, n)) return n; + return 0; +} + +static int trim_len(const char *s) { // trim trailing blanks + return len_trim(s); +} + +// fmtmsg: uppercase, collapse multiple blanks into one, find last non-blank +static void fmtmsg(char *msg, int &iz) { + int len = (int)strlen(msg); + for (int i = 0; i < len; i++) msg[i] = to_upper(msg[i]); + iz = len; + for (int i = 0; i < len; i++) if (msg[i] != ' ') iz = i + 1; + for (int iter = 0; iter < 37; iter++) { + int pos = -1; + for (int i = 0; i < iz - 1; i++) + if (msg[i] == ' ' && msg[i+1] == ' ') { pos = i; break; } + if (pos < 0) break; + memmove(&msg[pos + 1], &msg[pos + 2], strlen(&msg[pos + 2]) + 1); + iz--; + } +} + +// --------------------------------------------------------------------------- +// Maidenhead grid <-> degrees (grid2deg / deg2grid) +// --------------------------------------------------------------------------- + +static void grid2deg(const char *grid0, double &dlong, double &dlat) { + char grid[6] = {' ', ' ', ' ', ' ', ' ', ' '}; + size_t grid_len = strlen(grid0); + if (grid_len > 6) grid_len = 6; + memcpy(grid, grid0, grid_len); + int i = (unsigned char)grid[4]; + if (grid[4] == ' ' || i <= 64 || i >= 128) { grid[4] = 'm'; grid[5] = 'm'; } + if (grid[0] >= 'a' && grid[0] <= 'z') grid[0] = toupper(grid[0]); + if (grid[1] >= 'a' && grid[1] <= 'z') grid[1] = toupper(grid[1]); + if (grid[4] >= 'A' && grid[4] <= 'Z') grid[4] = tolower(grid[4]); + if (grid[5] >= 'A' && grid[5] <= 'Z') grid[5] = tolower(grid[5]); + + double nlong = 180.0 - 20.0 * (grid[0] - 'A'); + double n20d = 2.0 * (grid[2] - '0'); + double xminlong = 5.0 * (grid[4] - 'a' + 0.5); + dlong = nlong - n20d - xminlong / 60.0; + double nlat = -90.0 + 10.0 * (grid[1] - 'A') + (grid[3] - '0'); + double xminlat = 2.5 * (grid[5] - 'a' + 0.5); + dlat = nlat + xminlat / 60.0; +} + +static void deg2grid(double dlong0, double dlat, char *grid) { + double dlong = dlong0; + if (dlong < -180.0) dlong = dlong + 360.0; + if (dlong > 180.0) dlong = dlong - 360.0; + + int nlong = (int)(60.0 * (180.0 - dlong) / 5.0); + int n1 = nlong / 240; + int n2 = (nlong - 240 * n1) / 24; + int n3 = nlong - 240 * n1 - 24 * n2; + grid[0] = (char)('A' + n1); + grid[2] = (char)('0' + n2); + grid[4] = (char)('a' + n3); + + int nlat = (int)(60.0 * (dlat + 90.0) / 2.5); + n1 = nlat / 240; + n2 = (nlat - 240 * n1) / 24; + n3 = nlat - 240 * n1 - 24 * n2; + grid[1] = (char)('A' + n1); + grid[3] = (char)('0' + n2); + grid[5] = (char)('a' + n3); + grid[6] = 0; +} + +// grid2k / k2grid (for the prefix-derived grid path) +static void grid2k(const char *grid6, int &k) { + double dlong, dlat; + grid2deg(grid6, dlong, dlat); + int nlong = (int)(dlong + 0.5); + int nlat = (int)(dlat + 0.5); + k = 0; + if (nlat >= 85) k = 5 * (nlong + 179) / 2 + nlat - 84; +} + +static void k2grid(int k, char *grid) { + int nlong = 2 * (int)((k - 1) / 5 % 90) - 179; + if (k > 450) nlong = nlong + 180; + int nlat = (int)((k - 1) % 5) + 85; + deg2grid((double)nlong, (double)nlat, grid); +} + +// --------------------------------------------------------------------------- +// packcall — 28-bit callsign packing +// --------------------------------------------------------------------------- +static void packcall(const char *callsign, uint32_t &ncall, bool &text) { + const uint32_t NBASE = 37u * 36u * 10u * 27u * 27u * 27u; // 267796945 + char c[6] = {' ', ' ', ' ', ' ', ' ', ' '}; + int clen = (int)strlen(callsign); + for (int i = 0; i < 6; i++) c[i] = (i < clen) ? callsign[i] : ' '; + text = false; + + // Swaziland prefix work-around: '3DA0' -> '3D0'//callsign(5:6) + if (strncmp(c, "3DA0", 4) == 0) { c[0] = '3'; c[1] = 'D'; c[2] = '0'; c[3] = c[4]; c[4] = c[5]; c[5] = ' '; } + // Guinea prefix work-around: '3X' + A-Z at pos 3 -> 'Q'//callsign(3:6) + if (strncmp(c, "3X", 2) == 0 && c[2] >= 'A' && c[2] <= 'Z') { + c[0] = 'Q'; c[1] = c[2]; c[2] = c[3]; c[3] = c[4]; c[4] = c[5]; c[5] = ' '; + } + + if (strncmp(c, "CQ ", 3) == 0) { + ncall = NBASE + 1; + if (c[3] >= '0' && c[3] <= '9' && c[4] >= '0' && c[4] <= '9' && c[5] >= '0' && c[5] <= '9') { + ncall = NBASE + 3 + my_atoi(&c[3]); + } + return; + } else if (strncmp(c, "QRZ ", 4) == 0) { + ncall = NBASE + 2; + return; + } else if (strncmp(c, "DE ", 3) == 0) { + ncall = 267796945; + return; + } + + char tmp[6] = {' ', ' ', ' ', ' ', ' ', ' '}; + if (c[2] >= '0' && c[2] <= '9') { + memcpy(tmp, c, 6); + } else if (c[1] >= '0' && c[1] <= '9') { + if (c[5] != ' ') { text = true; return; } + tmp[0] = ' '; memcpy(&tmp[1], &c[0], 5); + } else { + text = true; return; + } + + // Uppercase (already handled by fmtmsg, but be safe) + for (int i = 0; i < 6; i++) tmp[i] = to_upper(tmp[i]); + + int n1 = 0, n2 = 0, n3 = 0, n4 = 0, n5 = 0, n6 = 0; + if ((tmp[0] >= 'A' && tmp[0] <= 'Z') || tmp[0] == ' ') n1 = 1; + if (tmp[0] >= '0' && tmp[0] <= '9') n1 = 1; + if ((tmp[1] >= 'A' && tmp[1] <= 'Z') || tmp[1] >= '0' && tmp[1] <= '9') n2 = 1; + if (tmp[2] >= '0' && tmp[2] <= '9') n3 = 1; + if ((tmp[3] >= 'A' && tmp[3] <= 'Z') || tmp[3] == ' ') n4 = 1; + if ((tmp[4] >= 'A' && tmp[4] <= 'Z') || tmp[4] == ' ') n5 = 1; + if ((tmp[5] >= 'A' && tmp[5] <= 'Z') || tmp[5] == ' ') n6 = 1; + + if (n1 + n2 + n3 + n4 + n5 + n6 != 6) { text = true; return; } + + uint32_t ncall2 = (uint32_t)nchar(tmp[0]); + ncall2 = 36u * ncall2 + (uint32_t)nchar(tmp[1]); + ncall2 = 10u * ncall2 + (uint32_t)nchar(tmp[2]); + ncall2 = 27u * ncall2 + (uint32_t)nchar(tmp[3]) - 10; + ncall2 = 27u * ncall2 + (uint32_t)nchar(tmp[4]) - 10; + ncall2 = 27u * ncall2 + (uint32_t)nchar(tmp[5]) - 10; + + ncall = ncall2; + return; +} + +// --------------------------------------------------------------------------- +// packgrid — grid/report packing +// --------------------------------------------------------------------------- +static void packgrid(const char *grid, uint32_t &ng, bool &text) { + const uint32_t NGBASE = 180u * 180u; // 32400 + char g[5] = {' ', ' ', ' ', ' ', 0}; + strncpy(g, grid, 4); + text = false; + + if (g[0] == ' ' && g[1] == ' ' && g[2] == ' ' && g[3] == ' ') { ng = NGBASE + 1; return; } + + if (g[0] == '-') { + // original range -01..-30 + int n = 0; + parse_int(&g[1], n); + if (n >= 1 && n <= 30) { ng = NGBASE + 1 + n; return; } + } else if (g[0] == 'R' && g[1] == '-') { + int n = 0; + parse_int(&g[2], n); + if (n >= 1 && n <= 30) { ng = NGBASE + 31 + n; return; } + } else if (strncmp(g, "RO ", 4) == 0) { + ng = NGBASE + 62; return; + } else if (strncmp(g, "RRR ", 4) == 0) { + ng = NGBASE + 63; return; + } else if (strncmp(g, "73 ", 4) == 0) { + ng = NGBASE + 64; return; + } + + // extended range -50..+49 + int n = 99; + char c1c = g[0]; + if (!parse_int(g, n)) { + if (!parse_int(&g[1], n)) n = 99; + } + + if (n >= -50 && n <= 49) { + char k[6] = {'K', 'A', '0', '0', 'm', 'm'}; + if (c1c == 'R') { + k[0] = 'L'; k[1] = 'A'; + } else { + k[0] = 'K'; k[1] = 'A'; + } + int nn = n + 50; + k[2] = (char)('0' + (nn / 10)); + k[3] = (char)('0' + (nn % 10)); + double dlong, dlat; + grid2deg(k, dlong, dlat); + int long_ = (int)dlong; + int lat = (int)(dlat + 90.0); + ng = ((long_ + 180) / 2) * 180 + lat; + return; + } + + // free text? + if (g[0] < 'A' || g[0] > 'R') text = true; + if (g[1] < 'A' || g[1] > 'R') text = true; + if (g[2] < '0' || g[2] > '9') text = true; + if (g[3] < '0' || g[3] > '9') text = true; + if (text) { ng = 0; return; } + + // full grid locator + double dlong, dlat; + char k[6] = {' ', ' ', ' ', ' ', 'm', 'm'}; + strncpy(k, g, 4); + k[4] = 'm'; k[5] = 'm'; + grid2deg(k, dlong, dlat); + int long_ = (int)dlong; + int lat = (int)(dlat + 90.0); + ng = ((long_ + 180) / 2) * 180 + lat; +} + +// --------------------------------------------------------------------------- +// getpfx1 — prefix/suffix parsing +// --------------------------------------------------------------------------- +static void getpfx1(char *callsign, int &k, int &nv2) { + k = 0; + nv2 = 1; + char orig[12] = {0}; + strncpy(orig, callsign, 11); + orig[11] = 0; + + int iz = (int)strlen(orig); + while (iz > 0 && orig[iz - 1] == ' ') iz--; + if (iz < 1) iz = 12; + + int islash = 0; + for (int i = 0; i < iz; i++) if (orig[i] == '/') { islash = i + 1; break; } // 1-indexed + + if (islash > 0 && islash <= iz - 4) { + // Add-on prefix + char c[5] = {' ', ' ', ' ', ' ', ' '}; + strncpy(c, orig, islash - 1); + c[islash - 1] = 0; + // trim trailing blanks from c + int cl = len_trim(c); + for (int i = 0; i < 339; i++) { + char entry[5] = {(char)UPF_READ_BYTE(&pfx_table[i * 4 + 0]), (char)UPF_READ_BYTE(&pfx_table[i * 4 + 1]), (char)UPF_READ_BYTE(&pfx_table[i * 4 + 2]), (char)UPF_READ_BYTE(&pfx_table[i * 4 + 3]), 0}; + int el = len_trim(entry); + if (el == cl) { + bool match = true; + for (int j = 0; j < el; j++) if (entry[j] != c[j]) { match = false; break; } + if (match) { k = i + 1; nv2 = 2; break; } + } + } + if (nv2 == 2) { + // rebuild callsign to the part after the slash + memmove(callsign, &orig[islash], iz - islash); + callsign[iz - islash] = 0; + return; + } + // custom addpfx (k=449) — no custom prefix table here; fall through + // to the generic path (Fortran: k stays 0, nv2 stays 1). + } else if (islash > 0 && islash == iz - 1) { + // Add-on suffix + char c[4] = {' ', ' ', ' ', ' '}; + strncpy(c, &orig[islash], iz - islash); + c[iz - islash] = 0; + for (int i = 0; i < 12; i++) { + if ((char)UPF_READ_BYTE(&sfx_table[i]) == c[0]) { k = 400 + i + 1; nv2 = 3; break; } + } + if (nv2 == 3) { + // rebuild callsign to the part before the slash + callsign[islash - 1] = 0; + return; + } + // no sfx-table match — fall through to the generic path + } + + if (islash > 0 && k == 0) { + // prefix/suffix encoded as 1-3 chars + char lof[5] = {' ', ' ', ' ', ' ', ' '}; + strncpy(lof, orig, islash - 1); + lof[islash - 1] = 0; + char rof[5] = {' ', ' ', ' ', ' ', ' '}; + strncpy(rof, &orig[islash], iz - islash); + rof[iz - islash] = 0; + + int llof = len_trim(lof); + int lrof = len_trim(rof); + bool ispfx = (llof > 0 && llof <= 4); + bool issfx = (lrof > 0 && lrof <= 3); + bool invalid = !(ispfx || issfx); + if (ispfx && issfx) { + if (llof < 3) issfx = false; + if (lrof < 3) ispfx = false; + if (ispfx && issfx) { + int i = orig[islash - 2]; + if (i >= '0' && i <= '9') issfx = false; + else ispfx = false; + } + } + + if (invalid) { + k = -1; + return; + } + + if (ispfx) { + int kk = 0; + for (int i = 0; i < 4; i++) kk = 37 * kk + nchar(lof[i]); + k = kk; + nv2 = 4; + // rebuild callsign to the part after the slash + memmove(callsign, &orig[islash], iz - islash); + callsign[iz - islash] = 0; + } + if (issfx) { + int kk = 0; + for (int i = 0; i < 3; i++) kk = 37 * kk + nchar(rof[i]); + k = kk; + nv2 = 5; + // rebuild callsign to the part before the slash + callsign[islash - 1] = 0; + } + return; + } +} + +// --------------------------------------------------------------------------- +// packtext — free-text packing (same algorithm as the current library) +// --------------------------------------------------------------------------- +static void packtext(const char *msg, uint32_t &nc1, uint32_t &nc2, uint32_t &ng) { + const char *c = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ +-./?"; + nc1 = 0; nc2 = 0; ng = 0; + for (int i = 0; i < 5; i++) { + int j = 36; + for (int k = 0; k < 42; k++) if (msg[i] == c[k]) { j = k; break; } + nc1 = 42 * nc1 + j; + } + for (int i = 5; i < 10; i++) { + int j = 36; + for (int k = 0; k < 42; k++) if (msg[i] == c[k]) { j = k; break; } + nc2 = 42 * nc2 + j; + } + for (int i = 10; i < 13; i++) { + int j = 36; + for (int k = 0; k < 42; k++) if (msg[i] == c[k]) { j = k; break; } + ng = 42 * ng + j; + } + nc1 = nc1 + nc1; + if (ng & 32768) nc1 = nc1 + 1; + nc2 = nc2 + nc2; + if (ng & 65536) nc2 = nc2 + 1; + ng = ng & 32767; +} + +// --------------------------------------------------------------------------- +// packmsg — main entry +// --------------------------------------------------------------------------- +int upstream_packmsg(const char *msg_in, uint8_t dat[12], int &itype) { + char msg[24] = {0}; + strncpy(msg, msg_in, 21); + msg[21] = 0; + + // fmtmsg: uppercase + collapse blanks (iz unused for the encode path) + int iz = 0; + fmtmsg(msg, iz); + + // CQ + 1-digit freq rewrite: "CQ 1" -> "CQ 001" + if (strncmp(msg, "CQ ", 3) == 0 && msg[3] >= '0' && msg[3] <= '9' && is_blank(msg[4])) { + char tmp[24]; + tmp[0] = 'C'; tmp[1] = 'Q'; tmp[2] = ' '; tmp[3] = '0'; tmp[4] = '0'; + int mlen = strlen(&msg[3]); + memcpy(&tmp[5], &msg[3], mlen + 1); + strncpy(msg, tmp, sizeof(msg) - 1); + msg[sizeof(msg) - 1] = 0; + fmtmsg(msg, iz); + } + // CQ DX rewrite: msg(3:3)='9' + if (strncmp(msg, "CQ DX", 5) == 0 && is_blank(msg[5])) msg[2] = '9'; + // CQ + 2-letter rewrite: "CQ XX " -> "E9XX" + if (strncmp(msg, "CQ ", 3) == 0 && msg[3] >= 'A' && msg[3] <= 'Z' && + msg[4] >= 'A' && msg[4] <= 'Z' && is_blank(msg[5])) { + memmove(&msg[2], &msg[3], strlen(&msg[3]) + 1); + msg[0] = 'E'; msg[1] = '9'; + } + + int ia = 0, ib = 0, ic = 0; + char c1[12], c2[12], c3[8]; + memset(c1, ' ', sizeof(c1)); + memset(c2, ' ', sizeof(c2)); + memset(c3, ' ', sizeof(c3)); + int nv2a = 1, nv2b = 1, k1 = 0, k2 = 0; + uint32_t nc1 = 0, nc2 = 0, ng = 0; + bool text1 = false, text2 = false, text3 = false; + + // Find first blank (or CQ special-case) + bool is_cq = (strncmp(msg, "CQ ", 3) == 0); + if (is_cq) { + // CQ message: i=3 or 7 + if (msg[3] >= '0' && msg[3] <= '9' && msg[4] >= '0' && msg[4] <= '9' && msg[5] >= '0' && msg[5] <= '9') + ia = 7; + else + ia = 3; + } else { + int i; + for (i = 0; i < 22; i++) { + if (msg[i] == ' ' || msg[i] == 0) { ia = i + 1; goto got_first_blank; } + } + goto plain_text; + } + goto got_first_blank; + + got_first_blank:; + for (int j = 0; j < ia - 1 && j < 11; j++) c1[j] = msg[j]; + + // Find second blank + int i; + for (i = ia; i < 22; i++) { + if (msg[i] == ' ' || msg[i] == 0) { ib = i + 1; goto got_second_blank; } + } + goto plain_text; + + got_second_blank:; + for (int j = 0; j < ib - ia - 1 && j < 11; j++) c2[j] = msg[ia + j]; + + // Find third blank + for (i = ib; i < 22; i++) { + if (msg[i] == ' ' || msg[i] == 0) { ic = i + 1; goto got_third_blank; } + } + goto plain_text; + + got_third_blank:; + for (int j = 0; j < 4; j++) c3[j] = ' '; + c3[4] = 0; + if (ic >= ib + 1) { + for (int j = 0; j < 4 && j < ic - ib; j++) c3[j] = (msg[ib + j] == 0) ? ' ' : msg[ib + j]; + } + c3[4] = 0; + if (strncmp(c3, "OOO ", 4) == 0) { memset(c3, ' ', 4); c3[4] = 0; } // strip OOO flag + c1[11] = 0; c2[11] = 0; c3[7] = 0; + + getpfx1(c1, k1, nv2a); + if (nv2a >= 4) goto plain_text; + packcall(c1, nc1, text1); + if (text1) goto plain_text; + getpfx1(c2, k2, nv2b); + packcall(c2, nc2, text2); + if (text2) goto plain_text; + + if (nv2a == 2 || nv2a == 3 || nv2b == 2 || nv2b == 3) { + if (k1 < 0 || k2 < 0 || k1 * k2 != 0) goto plain_text; + if (k2 > 0) k2 = k2 + 450; + int k = (k1 > k2) ? k1 : k2; + if (k > 0) { + char grid6[8] = {0}; + k2grid(k, grid6); + strncpy(c3, grid6, 4); + c3[4] = 0; + } + } + + packgrid(c3, ng, text3); + + if (nv2a < 4 && nv2b < 4 && !text1 && !text2 && !text3) { + itype = (nv2a > nv2b) ? nv2a : nv2b; + goto encode_data; + } + + // prefix/suffix handling (types 4/5) + nc1 = 0; + if (nv2b == 4) { + if (strncmp(c1, "CQ ", 3) == 0 && !text3) nc1 = 262178563 + k2; + if (strncmp(c1, "QRZ ", 4) == 0 && !text3) nc1 = 264002072 + k2; + if (strncmp(c1, "DE ", 3) == 0 && !text3) nc1 = 265825581 + k2; + } else if (nv2b == 5) { + if (strncmp(c1, "CQ ", 3) == 0 && !text3) nc1 = 267649090 + k2; + if (strncmp(c1, "QRZ ", 4) == 0 && !text3) nc1 = 267698375 + k2; + if (strncmp(c1, "DE ", 3) == 0 && !text3) nc1 = 267747660 + k2; + } + if (nc1 != 0) { + itype = (nv2a > nv2b) ? nv2a : nv2b; + goto encode_data; + } + + plain_text: + itype = 6; + packtext(msg, nc1, nc2, ng); + ng = ng + 32768; + + encode_data:; + if (itype != 6) itype = (nv2a > nv2b) ? nv2a : nv2b; + dat[0] = (uint8_t)((nc1 >> 22) & 63); + dat[1] = (uint8_t)((nc1 >> 16) & 63); + dat[2] = (uint8_t)((nc1 >> 10) & 63); + dat[3] = (uint8_t)((nc1 >> 4) & 63); + dat[4] = (uint8_t)(4 * (nc1 & 15) + ((nc2 >> 26) & 3)); + dat[5] = (uint8_t)((nc2 >> 20) & 63); + dat[6] = (uint8_t)((nc2 >> 14) & 63); + dat[7] = (uint8_t)((nc2 >> 8) & 63); + dat[8] = (uint8_t)((nc2 >> 2) & 63); + dat[9] = (uint8_t)(16 * (nc2 & 3) + ((ng >> 12) & 15)); + dat[10] = (uint8_t)((ng >> 6) & 63); + dat[11] = (uint8_t)(ng & 63); + + return itype; +} + +// --------------------------------------------------------------------------- +// entail — pack 12 6-bit symbols into 9 bytes (c[0..8]), zero-fill c[9..12] +// --------------------------------------------------------------------------- +void upstream_entail(const uint8_t dat[12], uint8_t c[13]) { + int i4 = 0, k = 0, m = 0; + for (int i = 0; i < 12; i++) { + int n = dat[i]; + for (int j = 1; j <= 6; j++) { + k++; + i4 = i4 + i4 + ((n >> (6 - j)) & 1); + i4 &= 0xff; + if (k == 8) { + m++; + if (i4 > 127) i4 = i4 - 256; + c[m - 1] = (uint8_t)i4; + k = 0; + } + } + } + for (int m = 9; m < 13; m++) c[m] = 0; +} +} // namespace UpstreamPackmsg +#endif // JTENCODE_FULL_PACKMSG diff --git a/src/upstream_packmsg.h b/src/upstream_packmsg.h new file mode 100644 index 0000000..7105973 --- /dev/null +++ b/src/upstream_packmsg.h @@ -0,0 +1,20 @@ +// upstream_packmsg.h +// Faithful C++ port of WSJT-X lib/packjt.f90 (packmsg and helpers). +// Returns 12 6-bit symbols (dat[0..11]) — the 72-bit data block that +// upstream entail() packs into data0(1..9) and encode232 encodes. +// +// Message types (itype): +// 1 standard, 2 prefix (CQ/QRZ/DE), 3 suffix (CQ/QRZ/DE), +// 4 callsign prefix, 5 callsign suffix, 6 free text, -1 undeliverable +// +// Declarations only — the implementation is compiled only when +// JTENCODE_FULL_PACKMSG is defined (see upstream_packmsg.cpp). +#pragma once +#include + +namespace UpstreamPackmsg { + +int upstream_packmsg(const char *msg, uint8_t dat[12], int &itype); +void upstream_entail(const uint8_t dat[12], uint8_t c[13]); + +} // namespace UpstreamPackmsg diff --git a/tests/run_tests.sh b/tests/run_tests.sh index 1be765c..b331091 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -2,6 +2,7 @@ # Build and run the JTEncode high-priority bug harness. # Usage: tests/run_tests.sh # Override the Arduino stub location with: STUB=/path/to/stub tests/run_tests.sh +# Select the full packmsg encoder: JTENCODE_FULL_PACKMSG=1 tests/run_tests.sh set -eu REPO="$(cd "$(dirname "$0")/.." && pwd)" @@ -18,18 +19,39 @@ fi mkdir -p "$BUILD" +EXTRA_FLAGS="" +EXTRA_SRCS="" +if [ "${JTENCODE_FULL_PACKMSG:-0}" = "1" ]; then + EXTRA_FLAGS="-DJTENCODE_FULL_PACKMSG" + EXTRA_SRCS="$REPO/src/upstream_packmsg.cpp" +fi + # Compile the C sources as C, then link the C++ harness with ASan. gcc -std=c11 -O1 -I"$STUB" -I"$REPO/src" -c "$REPO/src/crc14.c" -o "$BUILD/crc14.o" gcc -std=c11 -O1 -I"$STUB" -I"$REPO/src" -c "$REPO/src/nhash.c" -o "$BUILD/nhash.o" g++ -std=c++11 -g -O1 -fsanitize=address \ -I"$STUB" -I"$REPO/src" \ + $EXTRA_FLAGS \ -DREPO_ROOT="\"$REPO\"" \ "$REPO/tests/harness.cpp" \ "$REPO/src/JTEncode.cpp" \ "$REPO/src/init_rs_int.cpp" \ "$REPO/src/encode_rs_int.cpp" \ + $EXTRA_SRCS \ "$BUILD/crc14.o" "$BUILD/nhash.o" \ -o "$BUILD/harness" "$BUILD/harness" + +# Full-path regression test (type detection + dat layout). +if [ "${JTENCODE_FULL_PACKMSG:-0}" = "1" ]; then + echo + echo "=== Full packmsg regression test ===" + g++ -std=c++11 -O2 -DJTENCODE_FULL_PACKMSG \ + -I"$STUB" -I"$REPO/src" \ + "$REPO/tests/test_full_packmsg.cpp" \ + "$REPO/src/upstream_packmsg.cpp" \ + -o "$BUILD/test_full_packmsg" + "$BUILD/test_full_packmsg" +fi diff --git a/tests/test_full_packmsg.cpp b/tests/test_full_packmsg.cpp new file mode 100644 index 0000000..84f6813 --- /dev/null +++ b/tests/test_full_packmsg.cpp @@ -0,0 +1,68 @@ +// Regression test for the full packmsg encoder (JTENCODE_FULL_PACKMSG). +// +// Verifies the type detection (itype) and the dat byte layout for a +// representative set of messages. Compiled only when +// JTENCODE_FULL_PACKMSG is defined — the port is not compiled otherwise. + +#include +#include +#include "upstream_packmsg.h" + +static int failures = 0; + +static void check(const char *msg, int expected_itype) +{ + uint8_t dat[12] = {0}; + int itype = -99; + UpstreamPackmsg::upstream_packmsg(msg, dat, itype); + bool ok = (itype == expected_itype); + printf("%-32s -> itype=%2d (expect %2d) %s\n", msg, itype, expected_itype, ok ? "OK" : "FAIL"); + if (!ok) failures++; +} + +static void check_standard_dat(const char *msg, const uint8_t expected[12]) +{ + uint8_t dat[12] = {0}; + int itype = -99; + UpstreamPackmsg::upstream_packmsg(msg, dat, itype); + bool ok = (itype == 1); + for (int i = 0; i < 12; i++) if (dat[i] != expected[i]) ok = false; + printf("%-32s dat %s (itype=%d)\n", msg, ok ? "MATCH" : "MISMATCH", itype); + if (!ok) { + printf(" dat:"); + for (int i = 0; i < 12; i++) printf(" %u", dat[i]); + printf("\n"); + printf(" exp:"); + for (int i = 0; i < 12; i++) printf(" %u", expected[i]); + printf("\n"); + failures++; + } +} + +int main() +{ + check("NT7S K1ABC CN84", 1); // standard + check("CQ 001 K1ABC CN84", 1); // CQ with digit frequency + check("CQ K1ABC CN84", 1); // CQ without frequency + check("CQ DX", 6); // CQ DX -> free text + check("HELLO WORLD", 6); // free text + check("NT7S K1ABC -05", 1); // standard with report + check("CQ K1ABC/1P CN84", 5); // CQ with callsign suffix + check("NT7S K1ABC/1P CN84", 6); // plain callsign + suffix -> free text + check("NT7S K1ABC -45", 1); // extended report (-45) + check("NT7S K1ABC/X CN84", 6); // unparseable suffix -> free text + check("NT7S K1ABC", 1); // standard message without grid + + // dat-byte regression check: standard message must match the + // upstream reference output (verified against the WSJT-X Fortran + // reference driver — 28/28 messages pass, see README). + static const uint8_t expected_dat[12] = {40, 16, 13, 55, 55, 55, 3, 2, 14, 6, 42, 50}; + check_standard_dat("NT7S K1ABC CN84", expected_dat); + + if (failures == 0) { + printf("ALL OK\n"); + return 0; + } + printf("%d FAILURES\n", failures); + return 1; +}