Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ Public Methods
*
* call - Callsign (12 characters maximum).
* loc - Maidenhead grid locator (6 characters maximum).
* dbm - Output power in dBm. Valid range is -30 to 60 dBm; values are rounded
* down to the nearest valid power level.
* dbm - Output power in dBm. Values are clamped to the WSPR range 0 to 60
* dBm, then normalized to a sendable power using WSJT-X's `nu` table.
* symbols - Array of channel symbols to transmit returned by the method.
* Ensure that you pass a uint8_t array of at least size WSPR_SYMBOL_COUNT to the method.
*
Expand Down Expand Up @@ -377,6 +377,23 @@ Also, a big thank you to Murray Greenman, ZL1BPU for working allowing me to pick

Changelog
---------
* v1.5.1

* WSPR power normalization now follows the WSJT-X `ndbm + nu(mod(ndbm, 10))` table
after clamping to 0..60, instead of rounding invalid powers down through the old
`valid_dbm` table.
* Full packmsg prefix parsing is bounds-checked (`getpfx1()`), avoiding a one-byte
stack overflow for slash-prefix inputs.
* Free-text packing maps invalid characters to code 0 in both compact and full paths,
matching Fortran `packtext77`.
* Example beacon sketches limit pre-transmission WiFi reconnect time so reconnect
cannot overrun the NTP-aligned transmit slot.
* Fixed the suffix-parse precedence warning, made bounded `snprintf` precision
explicit, removed unused `trim_len`/`grid2k`, and made the RS symbol-size sign
compare explicit.
* Added regression tests for invalid free-text characters and overlong prefix cases;
the harness runs 36 checks.

* v1.5.0

* Add FT4 support (issue #37): 105-symbol FT4 encoder (4 tones at 20.833 Hz spacing,
Expand Down
20 changes: 15 additions & 5 deletions examples/FT4_Beacon_UnoR4WiFi/FT4_Beacon_UnoR4WiFi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ static const char *MSG_STRINGS[MSG_COUNT] = {
// Re-sync NTP shortly before each transmission (if the last sync is older
// than this) so the board's millis() drift since the last sync stays small.
#define PRE_TX_SYNC_MS 15000UL
#define NTP_SYNC_MAX_MS 5000UL
#define RECONNECT_SAFETY_MS 1000UL

// ---- Objects ------------------------------------------------------------------
Si5351 si5351;
Expand Down Expand Up @@ -280,7 +282,7 @@ unsigned long now()
}

// ---- WiFi ----------------------------------------------------------------------
bool connectWiFi()
bool connectWiFi(uint64_t max_ms)
{
Serial.print("Connecting to WiFi: ");
Serial.println(SECRET_SSID);
Expand All @@ -294,7 +296,7 @@ bool connectWiFi()
WiFi.begin(SECRET_SSID, SECRET_PASS);

unsigned long start = millis();
while (WiFi.status() != WL_CONNECTED && (millis() - start) < 60000UL)
while (WiFi.status() != WL_CONNECTED && (uint64_t)(millis() - start) < max_ms)
{
delay(500);
}
Expand All @@ -313,6 +315,11 @@ bool connectWiFi()
return false;
}

bool connectWiFi()
{
return connectWiFi(60000ULL);
}

// ---- Time formatting ------------------------------------------------------------
// Prints "HH:MM:SS" (UTC) for an epoch time.
void printTime(unsigned long epoch)
Expand Down Expand Up @@ -519,9 +526,12 @@ void loop()
{
if (WiFi.status() != WL_CONNECTED)
{
// WiFi dropped: reconnect and re-sync; the slot target is unchanged
// (the local clock keeps running from millis() in the meantime).
connectWiFi();
// WiFi dropped: reconnect within a budget that leaves room for the
// following NTP resync before the transmit slot.
uint64_t budget = remaining > (NTP_SYNC_MAX_MS + RECONNECT_SAFETY_MS)
? remaining - NTP_SYNC_MAX_MS - RECONNECT_SAFETY_MS
: 0;
connectWiFi(budget);
syncTime();
continue;
}
Expand Down
20 changes: 15 additions & 5 deletions examples/FT8_Beacon_UnoR4WiFi/FT8_Beacon_UnoR4WiFi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ static const char *MSG_STRINGS[MSG_COUNT] = {
// Re-sync NTP shortly before each transmission (if the last sync is older
// than this) so the board's millis() drift since the last sync stays small.
#define PRE_TX_SYNC_MS 15000UL
#define NTP_SYNC_MAX_MS 5000UL
#define RECONNECT_SAFETY_MS 1000UL

// ---- Objects ------------------------------------------------------------------
Si5351 si5351;
Expand Down Expand Up @@ -279,7 +281,7 @@ unsigned long now()
}

// ---- WiFi ----------------------------------------------------------------------
bool connectWiFi()
bool connectWiFi(uint64_t max_ms)
{
Serial.print("Connecting to WiFi: ");
Serial.println(SECRET_SSID);
Expand All @@ -293,7 +295,7 @@ bool connectWiFi()
WiFi.begin(SECRET_SSID, SECRET_PASS);

unsigned long start = millis();
while (WiFi.status() != WL_CONNECTED && (millis() - start) < 60000UL)
while (WiFi.status() != WL_CONNECTED && (uint64_t)(millis() - start) < max_ms)
{
delay(500);
}
Expand All @@ -312,6 +314,11 @@ bool connectWiFi()
return false;
}

bool connectWiFi()
{
return connectWiFi(60000ULL);
}

// ---- Time formatting ------------------------------------------------------------
// Prints "HH:MM:SS" (UTC) for an epoch time.
void printTime(unsigned long epoch)
Expand Down Expand Up @@ -518,9 +525,12 @@ void loop()
{
if (WiFi.status() != WL_CONNECTED)
{
// WiFi dropped: reconnect and re-sync; the slot target is unchanged
// (the local clock keeps running from millis() in the meantime).
connectWiFi();
// WiFi dropped: reconnect within a budget that leaves room for the
// following NTP resync before the transmit slot.
uint64_t budget = remaining > (NTP_SYNC_MAX_MS + RECONNECT_SAFETY_MS)
? remaining - NTP_SYNC_MAX_MS - RECONNECT_SAFETY_MS
: 0;
connectWiFi(budget);
syncTime();
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Etherkit JTEncode
version=1.5.0
version=1.5.1
author=Jason Milldrum <milldrum@gmail.com>
maintainer=Jason Milldrum <milldrum@gmail.com>
sentence=Generate JT65, JT9, JT4, FT8, FT4, WSPR, and FSQ symbols on your Arduino.
Expand Down
39 changes: 11 additions & 28 deletions src/JTEncode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ void JTEncode::ft_message_prep(char * message)
uint8_t i;
char temp_msg[14];

snprintf(temp_msg, 14, "%13s", message);
snprintf(temp_msg, sizeof(temp_msg), "%13.13s", message);

// Convert all chars to uppercase
for(i = 0; i < 13; i++)
Expand Down Expand Up @@ -812,36 +812,18 @@ void JTEncode::wspr_message_prep(char * call, char * loc, int8_t dbm)
memset(locator, 0, sizeof(locator));
strncpy(locator, loc, sizeof(locator) - 1);

// Power level validation
// Only certain increments are allowed
if(dbm > 60)
// Power level normalization
// WSJT-X clamps to 0..60 and maps invalid powers with the `nu` table.
if(dbm < 0)
{
dbm = 60;
dbm = 0;
}
if(dbm < -30)
if(dbm > 60)
{
dbm = -30;
dbm = 60;
}
const uint8_t VALID_DBM_SIZE = 28;
const int8_t valid_dbm[VALID_DBM_SIZE] =
{-30, -27, -23, -20, -17, -13, -10, -7, -3,
0, 3, 7, 10, 13, 17, 20, 23, 27, 30, 33, 37, 40,
43, 47, 50, 53, 57, 60};
for(i = 0; i < VALID_DBM_SIZE; i++)
{
if(dbm == valid_dbm[i])
{
power = dbm;
}
}
// If we got this far, we have an invalid power level, so we'll round down
for(i = 1; i < VALID_DBM_SIZE; i++)
{
if(dbm < valid_dbm[i] && dbm >= valid_dbm[i - 1])
{
power = valid_dbm[i - 1];
}
}
static const int8_t nu[10] = {0, -1, 1, 0, -1, 2, 1, 0, -1, 1};
power = dbm + nu[dbm % 10];
}

void JTEncode::jt65_bit_packing(char * message, uint8_t * c)
Expand Down Expand Up @@ -1250,7 +1232,7 @@ void JTEncode::ft8_bit_packing(char* message, uint8_t* codeword)

memset(c18, 0, 19);
memmove(c18, message, i0 + 1);
snprintf(temp_msg, 19, "%18s", c18);
snprintf(temp_msg, sizeof(temp_msg), "%18.18s", c18);

// Convert all chars to uppercase
for(i = 0; i < strlen(temp_msg); i++)
Expand Down Expand Up @@ -1313,6 +1295,7 @@ void JTEncode::ft8_bit_packing(char* message, uint8_t* codeword)
for(i = 0; i < 13; ++i)
{
x = ft_code(message[i]);
if (x == 255) x = 0; // match packtext77: unknown character -> blank

// mult
ireg = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/init_rs_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void * JTEncode::init_rs_int(int symsize, int gfpoly, int fcr, int prim,

rs = ((struct rs *)0);
/* Check parameter ranges */
if(symsize < 0 || symsize > 8*sizeof(data_t)){
if(symsize < 0 || symsize > static_cast<int>(8 * sizeof(data_t))){
goto done;
}

Expand Down
6 changes: 3 additions & 3 deletions src/upstream_pack77.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,6 @@ static int pack77_1(int nwords, int nw[19], char w[19][13], int &i3, int &n3, ui
if (nwords != 2) {
const char *wlast = w[nwords - 1];
char c1 = wlast[0];
char c2 = (len_trim(wlast, 13) >= 2) ? wlast[1] : ' ';
bool is_grid4_last = is_grid4(wlast);
bool is_report_plus = (c1 == '+' || c1 == '-');
bool is_report_rplus = (strncmp(wlast, "R+", 2) == 0 || strncmp(wlast, "R-", 2) == 0);
Expand Down Expand Up @@ -532,7 +531,7 @@ static int hex2int_impl(char ch) {

static void ft_message_prep_impl(char *message) {
char temp_msg[14];
snprintf(temp_msg, 14, "%13s", message);
snprintf(temp_msg, sizeof(temp_msg), "%13.13s", message);
for (int i = 0; i < 13; i++) {
if (temp_msg[i] >= 'a' && temp_msg[i] <= 'z') temp_msg[i] = to_upper(temp_msg[i]);
}
Expand Down Expand Up @@ -575,7 +574,7 @@ static int telemetry_pack(char *message, uint8_t c[77], int &i3, int &n3) {
if (ntel1 >= (1 << 23)) return -1;

char temp_msg[19];
snprintf(temp_msg, 19, "%18s", c18);
snprintf(temp_msg, sizeof(temp_msg), "%18.18s", c18);
for (i = 0; i < 18; i++) temp_msg[i] = to_upper(temp_msg[i]);
strncpy(message, temp_msg, 18);
message[18] = 0;
Expand Down Expand Up @@ -623,6 +622,7 @@ static void free_text_pack(char *message, uint8_t c[77], int &i3, int &n3) {

for (i = 0; i < 13; ++i) {
x = ft_code_impl(message[i]);
if (x == 255) x = 0; // match packtext77: unknown character -> blank
// mult: ireg = 42 * qa + carry
ireg = 0;
for (j = 0; j < 9; ++j) {
Expand Down
64 changes: 27 additions & 37 deletions src/upstream_packmsg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ static int my_atoi(const char *s) {
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) {
Expand Down Expand Up @@ -182,15 +179,6 @@ static void deg2grid(double dlong0, double dlat, char *grid) {
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;
Expand Down Expand Up @@ -246,7 +234,7 @@ static void packcall(const char *callsign, uint32_t &ncall, bool &text) {
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[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;
Expand Down Expand Up @@ -353,15 +341,20 @@ static void getpfx1(char *callsign, int &k, int &nv2) {
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
int prefix_len = islash > 0 ? islash - 1 : 0;
int suffix_len = islash > 0 ? iz - islash : 0;

if (islash > 0 && prefix_len <= 4 && islash <= iz - 4) {
// Add-on prefix. pfx_table entries are four real characters; do not
// write a terminating NUL outside the four-character entry area.
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 < prefix_len; i++) c[i] = orig[i];
int cl = prefix_len;
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};
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;
Expand All @@ -371,17 +364,16 @@ static void getpfx1(char *callsign, int &k, int &nv2) {
}
if (nv2 == 2) {
// rebuild callsign to the part after the slash
memmove(callsign, &orig[islash], iz - islash);
callsign[iz - islash] = 0;
memmove(callsign, &orig[islash], suffix_len);
callsign[suffix_len] = 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) {
} else if (islash > 0 && suffix_len == 1) {
// Add-on suffix
char c[4] = {' ', ' ', ' ', ' '};
strncpy(c, &orig[islash], iz - islash);
c[iz - islash] = 0;
c[0] = orig[islash];
for (int i = 0; i < 12; i++) {
if ((char)UPF_READ_BYTE(&sfx_table[i]) == c[0]) { k = 400 + i + 1; nv2 = 3; break; }
}
Expand All @@ -394,16 +386,9 @@ static void getpfx1(char *callsign, int &k, int &nv2) {
}

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);
// prefix/suffix encoded as 1-4 prefix chars or 1-3 suffix chars
int llof = prefix_len;
int lrof = suffix_len;
bool ispfx = (llof > 0 && llof <= 4);
bool issfx = (lrof > 0 && lrof <= 3);
bool invalid = !(ispfx || issfx);
Expand All @@ -422,14 +407,19 @@ static void getpfx1(char *callsign, int &k, int &nv2) {
return;
}

char lof[5] = {' ', ' ', ' ', ' ', ' '};
char rof[5] = {' ', ' ', ' ', ' ', ' '};
for (int i = 0; i < llof && i < 4; i++) lof[i] = orig[i];
for (int i = 0; i < lrof && i < 3; i++) rof[i] = orig[islash + i];

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;
memmove(callsign, &orig[islash], suffix_len);
callsign[suffix_len] = 0;
}
if (issfx) {
int kk = 0;
Expand Down
Loading
Loading