You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

383 lines
14 KiB

/* SPDX-License-Identifier: MIT
* Copyright (c) 2026 Dan Chokola, KD2HSG
*
* Tests of the public API only (chirpmodem.h), linked like an application:
* - versions, defaults, error codes and policy
* - TX -> channel (IF, carrier offset, noise) -> streaming RX, in each
* input format, pushed in random block sizes
* - unlicensed encrypted frames and destination callsigns
* - two receivers decoding at once on two threads
*/
#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "chirpmodem.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
static int fails;
#define CHECK(c) do { if (!(c)) { printf(" FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); fails++; } } while (0)
/* ------------------------------------------------------------- noise */
static uint64_t rng_s = 88172645463325252ULL;
static double uni(uint64_t *s)
{
*s ^= *s << 13; *s ^= *s >> 7; *s ^= *s << 17;
return ((*s >> 11) + 0.5) / 9007199254740992.0;
}
static double gauss(uint64_t *s)
{
return sqrt(-2 * log(uni(s))) * cos(2 * M_PI * uni(s));
}
/* ------------------------------------------------------------- a test signal */
typedef struct {
float *iq; /* interleaved */
long n;
double fs;
double t_frames[8];
int n_frames;
} signal_t;
static void append(signal_t *s, const float *x, long n)
{
s->iq = realloc(s->iq, sizeof(float) * 2 * (size_t)(s->n + n));
if (x)
memcpy(s->iq + 2 * s->n, x, sizeof(float) * 2 * (size_t)n);
else
memset(s->iq + 2 * s->n, 0, sizeof(float) * 2 * (size_t)n);
s->n += n;
}
/* frames from tx, separated by gaps; returns 0 ok */
static int build(signal_t *s, chirpmodem_tx *tx, const char *src, const char *dst,
const char *const *bodies, int nb, int encrypted, double gap_s, int cw_id)
{
memset(s, 0, sizeof *s);
s->fs = chirpmodem_tx_fs(tx);
append(s, NULL, (long)(0.3 * s->fs));
for (int i = 0; i < nb; i++) {
long n = chirpmodem_tx_frame(tx, src, dst, (const uint8_t *)bodies[i], (int)strlen(bodies[i]),
encrypted, NULL, 0);
if (n <= 0)
return -1;
float *f = malloc(sizeof(float) * 2 * (size_t)n);
if (chirpmodem_tx_frame(tx, src, dst, (const uint8_t *)bodies[i], (int)strlen(bodies[i]),
encrypted, f, n) != n)
return -1;
s->t_frames[s->n_frames++] = s->n / s->fs;
append(s, f, n);
free(f);
append(s, NULL, (long)(gap_s * s->fs));
}
if (cw_id) {
long n = chirpmodem_tx_cw_id(tx, src, 20, NULL, 0);
float *f = malloc(sizeof(float) * 2 * (size_t)n);
if (chirpmodem_tx_cw_id(tx, src, 20, f, n) != n)
return -1;
append(s, f, n);
free(f);
}
append(s, NULL, (long)(0.3 * s->fs));
return 0;
}
/* carrier offset and AWGN at snr_db in the signal bandwidth (signal power amp^2) */
static void channel(signal_t *s, double amp, double bw, double snr_db, double cfo_hz, uint64_t seed)
{
double sig2 = amp * amp, n0 = sig2 / pow(10, snr_db / 10) * s->fs / bw;
double sd = sqrt(n0 / 2);
for (long k = 0; k < s->n; k++) {
double ph = 2 * M_PI * fmod(cfo_hz * k / s->fs, 1.0), c = cos(ph), d = sin(ph);
double re = s->iq[2 * k], im = s->iq[2 * k + 1];
s->iq[2 * k] = (float)(re * c - im * d + sd * gauss(&seed));
s->iq[2 * k + 1] = (float)(re * d + im * c + sd * gauss(&seed));
}
}
/* ------------------------------------------------------------- receiving */
typedef struct {
int n, n_fail;
char body[8][64];
char src[8][10], dst[8][10];
double t[8], snr[8], off[8];
int encrypted[8], profile[8], policy_ok[8], coherent[8];
} got_t;
static void on_frame(void *user, const chirpmodem_frame *f)
{
got_t *g = user;
if (f->status != CHIRPMODEM_FRAME_OK) {
g->n_fail++;
return;
}
if (g->n >= 8)
return;
int i = g->n++;
int nb = f->body_len < 63 ? f->body_len : 63;
memcpy(g->body[i], f->body, (size_t)nb);
g->body[i][nb] = 0;
strcpy(g->src[i], f->src);
strcpy(g->dst[i], f->dst);
g->t[i] = f->t;
g->snr[i] = f->snr_db;
g->off[i] = f->offset_hz;
g->encrypted[i] = f->encrypted;
g->profile[i] = f->profile;
g->policy_ok[i] = f->policy_ok;
g->coherent[i] = f->coherent;
}
/* push s in format fmt, random block sizes; scale so the samples fit the format */
static void receive(const signal_t *s, chirpmodem_format fmt, const chirpmodem_rx_config *base,
got_t *g, uint64_t seed)
{
memset(g, 0, sizeof *g);
chirpmodem_rx_config cfg = *base;
cfg.on_frame = on_frame;
cfg.user = g;
int err;
chirpmodem_rx *rx = chirpmodem_rx_new(&cfg, &err);
CHECK(rx != NULL);
if (!rx)
return;
double peak = 0;
for (long k = 0; k < 2 * s->n; k++)
if (fabs(s->iq[k]) > peak)
peak = fabs(s->iq[k]);
double g8 = 0.9 / peak;
size_t bpe = fmt == CHIRPMODEM_CF32 ? 8 : fmt == CHIRPMODEM_CI16 ? 4 : 2;
uint8_t *buf = malloc(bpe * 70000);
for (long k = 0; k < s->n;) {
long n = 1 + (long)(uni(&seed) * 60000);
if (n > s->n - k)
n = s->n - k;
for (long i = 0; i < n; i++) {
for (int c = 0; c < 2; c++) {
double v = s->iq[2 * (k + i) + c];
switch (fmt) {
case CHIRPMODEM_CU8: buf[2 * i + c] = (uint8_t)lrint(127.5 + 127 * g8 * v); break;
case CHIRPMODEM_CI8: buf[2 * i + c] = (uint8_t)(int8_t)lrint(127 * g8 * v); break;
case CHIRPMODEM_CI16: {
int16_t q = (int16_t)lrint(32767 * g8 * v);
buf[4 * i + 2 * c] = (uint8_t)(q & 0xFF);
buf[4 * i + 2 * c + 1] = (uint8_t)((uint16_t)q >> 8);
break;
}
default: {
float f = (float)v;
memcpy(buf + 8 * i + 4 * c, &f, 4);
}
}
}
}
CHECK(chirpmodem_rx_push(rx, buf, n, fmt) == CHIRPMODEM_OK);
k += n;
}
CHECK(chirpmodem_rx_finish(rx) == CHIRPMODEM_OK);
chirpmodem_rx_stats st;
chirpmodem_rx_get_stats(rx, &st);
CHECK(st.frames_ok == g->n);
CHECK(fabs(st.seconds_in - s->n / s->fs) < 1e-9);
free(buf);
chirpmodem_rx_free(rx);
}
/* ------------------------------------------------------------- tests */
static void test_basics(void)
{
printf("basics\n");
char v[32];
snprintf(v, sizeof v, "%d.%d.%d", CHIRPMODEM_VERSION_MAJOR, CHIRPMODEM_VERSION_MINOR,
CHIRPMODEM_VERSION_PATCH);
CHECK(!strcmp(chirpmodem_version(), v));
CHECK(CHIRPMODEM_FORMAT_VERSION == 1);
CHECK(chirpmodem_max_body("KD2HSG", NULL) == 2040);
CHECK(chirpmodem_max_body(NULL, NULL) == 2046);
CHECK(chirpmodem_default_coherent(9, 125e3) == 1);
CHECK(chirpmodem_default_coherent(9, 62.5e3) == 0);
CHECK(chirpmodem_default_coherent(10, 125e3) == 0);
CHECK(fabs(chirpmodem_frame_seconds(9, 62.5e3, 27, 0) - 0.739) < 1e-3); /* tx_file.py: 20 B body + 7 */
for (int e = 0; e >= -6; e--)
CHECK(chirpmodem_strerror(e) && strcmp(chirpmodem_strerror(e), "unknown error"));
chirpmodem_tx_config tc;
chirpmodem_tx_config_init(&tc);
CHECK(tc.sf == 9 && tc.bw == 62.5e3 && tc.fs == 0 && tc.amp == 0.7);
int err = 0;
tc.sf = 6;
CHECK(chirpmodem_tx_new(&tc, &err) == NULL && err == CHIRPMODEM_EINVAL);
chirpmodem_tx_config_init(&tc);
tc.fs = 150e3; /* too low for 62.5 kHz at +100 kHz */
tc.if_offset = 100e3;
CHECK(chirpmodem_tx_new(&tc, &err) == NULL && err == CHIRPMODEM_EINVAL);
chirpmodem_tx_config_init(&tc);
chirpmodem_tx *tx = chirpmodem_tx_new(&tc, &err);
CHECK(tx && err == CHIRPMODEM_OK);
CHECK(chirpmodem_tx_fs(tx) == 125e3);
const uint8_t b[] = "hi";
CHECK(chirpmodem_tx_frame(tx, NULL, NULL, b, 2, 0, NULL, 0) == CHIRPMODEM_EPOLICY);
CHECK(chirpmodem_tx_frame(tx, "KD2HSG", NULL, b, 2, 1, NULL, 0) == CHIRPMODEM_EPOLICY);
CHECK(chirpmodem_tx_frame(tx, "KD2HSG!", NULL, b, 2, 0, NULL, 0) == CHIRPMODEM_ECALLSIGN);
CHECK(chirpmodem_tx_frame(tx, "ABCDEFGHIJ", NULL, b, 2, 0, NULL, 0) == CHIRPMODEM_ECALLSIGN);
static uint8_t big[2100];
CHECK(chirpmodem_tx_frame(tx, "KD2HSG", NULL, big, 2041, 0, NULL, 0) == CHIRPMODEM_ETOOLONG);
long n = chirpmodem_tx_frame(tx, "KD2HSG", NULL, big, 2040, 0, NULL, 0);
CHECK(n > 0);
float small[16];
CHECK(chirpmodem_tx_frame(tx, "KD2HSG", NULL, b, 2, 0, small, 8) == CHIRPMODEM_ESPACE);
CHECK(chirpmodem_tx_cw_id(tx, "KD2HSG", 25, NULL, 0) == CHIRPMODEM_EINVAL); /* > 20 WPM */
CHECK(chirpmodem_tx_cw_id(tx, "KD2HSG", 0, NULL, 0) > 0);
chirpmodem_tx_free(tx);
chirpmodem_tx_free(NULL);
chirpmodem_rx_config rc;
chirpmodem_rx_config_init(&rc);
CHECK(rc.sf == 9 && rc.fs == 1e6 && rc.if_offset == 250e3 && rc.effort == 2);
CHECK(chirpmodem_rx_new(&rc, &err) == NULL && err == CHIRPMODEM_EINVAL); /* no callback */
rc.on_frame = on_frame;
rc.if_offset = 480e3; /* outside 1 MS/s */
CHECK(chirpmodem_rx_new(&rc, &err) == NULL && err == CHIRPMODEM_EINVAL);
chirpmodem_rx_free(NULL);
CHECK(chirpmodem_rx_push(NULL, NULL, 0, CHIRPMODEM_CU8) == CHIRPMODEM_EINVAL);
}
static void test_roundtrip(void)
{
static const char *const bodies[] = {"alpha de KD2HSG", "bravo", "charlie 123"};
/* TX at 1 MS/s with the signal 100 kHz up, RX sees it 250 kHz up after a
* different "LO": the channel adds the extra +150 kHz plus a 2.5 kHz offset */
chirpmodem_tx_config tc;
chirpmodem_tx_config_init(&tc);
tc.fs = 1e6;
tc.if_offset = 250e3;
chirpmodem_tx *tx = chirpmodem_tx_new(&tc, NULL);
signal_t s;
CHECK(build(&s, tx, "KD2HSG", "W1AW", bodies, 3, 0, 0.2, 1) == 0);
channel(&s, tc.amp, tc.bw, -8.0, -2500.0, 7);
chirpmodem_rx_config rc;
chirpmodem_rx_config_init(&rc);
static const char *names[] = {"cu8", "ci8", "ci16", "cf32"};
for (int f = CHIRPMODEM_CU8; f <= CHIRPMODEM_CF32; f++) {
printf("round trip, SF9 62.5 kHz non-coherent, -8 dB, %s\n", names[f]);
got_t g;
receive(&s, (chirpmodem_format)f, &rc, &g, 1234 + f);
CHECK(g.n == 3);
CHECK(g.n_fail == 0);
for (int i = 0; i < g.n && i < 3; i++) {
CHECK(!strcmp(g.body[i], bodies[i]));
CHECK(!strcmp(g.src[i], "KD2HSG") && !strcmp(g.dst[i], "W1AW"));
CHECK(fabs(g.t[i] - s.t_frames[i]) < 2e-3);
CHECK(fabs(g.off[i] + 2500.0) < 5.0);
CHECK(fabs(g.snr[i] + 8.0) < 1.5);
CHECK(g.profile[i] == CHIRPMODEM_LICENSED && g.policy_ok[i] && !g.encrypted[i]);
CHECK(g.coherent[i] == 0);
}
}
free(s.iq);
chirpmodem_tx_free(tx);
}
static void test_unlicensed_coherent(void)
{
printf("unlicensed, encrypted flag, coherent SF8 at 250 kHz, modem-rate stream\n");
static const char *const bodies[] = {"\x8f\x1e\x2d\x3c ciphertext"};
chirpmodem_tx_config tc;
chirpmodem_tx_config_init(&tc);
tc.sf = 8;
tc.bw = 250e3;
tc.profile = CHIRPMODEM_UNLICENSED;
tc.coherent = CHIRPMODEM_MODE_COHERENT;
chirpmodem_tx *tx = chirpmodem_tx_new(&tc, NULL);
CHECK(tx && chirpmodem_tx_coherent(tx) == 1);
signal_t s;
CHECK(build(&s, tx, NULL, NULL, bodies, 1, 1, 0.1, 0) == 0);
channel(&s, tc.amp, tc.bw, 0.0, 300.0, 11);
chirpmodem_rx_config rc;
chirpmodem_rx_config_init(&rc);
rc.sf = 8;
rc.bw = 250e3;
rc.fs = 500e3; /* exactly the modem rate: no conversion stage */
rc.if_offset = 0;
got_t g;
receive(&s, CHIRPMODEM_CF32, &rc, &g, 99);
CHECK(g.n == 1);
if (g.n == 1) {
CHECK(!strcmp(g.body[0], bodies[0]));
CHECK(g.src[0][0] == 0 && g.dst[0][0] == 0);
CHECK(g.profile[0] == CHIRPMODEM_UNLICENSED && g.encrypted[0] && g.policy_ok[0]);
CHECK(g.coherent[0] == 1);
}
free(s.iq);
chirpmodem_tx_free(tx);
}
typedef struct {
const signal_t *s;
const chirpmodem_rx_config *rc;
got_t g;
uint64_t seed;
} job_t;
static void *job(void *a)
{
job_t *j = a;
receive(j->s, CHIRPMODEM_CF32, j->rc, &j->g, j->seed);
return NULL;
}
static void test_threads(void)
{
printf("two receivers on two threads (SF7 125 kHz and SF10 62.5 kHz)\n");
static const char *const b1[] = {"thread one", "again"}, *const b2[] = {"thread two"};
chirpmodem_tx_config t1, t2;
chirpmodem_tx_config_init(&t1);
t1.sf = 7;
t1.bw = 125e3;
t1.fs = 1e6;
t1.if_offset = 250e3;
chirpmodem_tx_config_init(&t2);
t2.sf = 10;
t2.fs = 1e6;
t2.if_offset = 250e3;
chirpmodem_tx *x1 = chirpmodem_tx_new(&t1, NULL), *x2 = chirpmodem_tx_new(&t2, NULL);
signal_t s1, s2;
CHECK(build(&s1, x1, "KD2HSG", NULL, b1, 2, 0, 0.1, 0) == 0);
CHECK(build(&s2, x2, "KD2HSG", NULL, b2, 1, 0, 0.1, 0) == 0);
channel(&s1, 0.7, 125e3, -3.0, 1000.0, 5);
channel(&s2, 0.7, 62.5e3, -10.0, -700.0, 6);
chirpmodem_rx_config r1, r2;
chirpmodem_rx_config_init(&r1);
r1.sf = 7;
r1.bw = 125e3;
chirpmodem_rx_config_init(&r2);
r2.sf = 10;
job_t j1 = {&s1, &r1, {0}, 1}, j2 = {&s2, &r2, {0}, 2};
pthread_t a, b;
pthread_create(&a, NULL, job, &j1);
pthread_create(&b, NULL, job, &j2);
pthread_join(a, NULL);
pthread_join(b, NULL);
CHECK(j1.g.n == 2 && !strcmp(j1.g.body[0], "thread one") && !strcmp(j1.g.body[1], "again"));
CHECK(j2.g.n == 1 && !strcmp(j2.g.body[0], "thread two"));
free(s1.iq);
free(s2.iq);
chirpmodem_tx_free(x1);
chirpmodem_tx_free(x2);
}
int main(void)
{
(void)rng_s;
printf("chirpmodem %s, format v%d\n", chirpmodem_version(), CHIRPMODEM_FORMAT_VERSION);
test_basics();
test_roundtrip();
test_unlicensed_coherent();
test_threads();
printf(fails ? "%d check(s) FAILED\n" : "all API tests passed\n", fails);
return fails ? 1 : 0;
}