Files
hardware/firmware/test/test_core.c
T
Claude 838afca480 Name the D-Modem Authors as the copyright holder
Copyright lines, licensing texts and metadata named Neo-Desktop; they
now name the D-Modem Authors, and the licensing contact points at the
D-Modem organisation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRUwD2JJ5d6fgHmpVTw
2026-10-29 11:14:57 +00:00

547 lines
22 KiB
C

/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (C) 2026 D-Modem Authors
*/
// Host tests for the serial core: role detection, line mapping, flow control,
// SERIAL_STATE, line coding, break, vendor requests, settings, ring buffer.
// Build and run: make -C firmware/test
#include <stdio.h>
#include <string.h>
#include "../src/core/core.h"
#include "fake_hal.h"
#include "wave.h"
static int failures, checks;
#define CHECK(cond) do { checks++; if (!(cond)) { failures++; \
printf(" FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); } } while (0)
#define TEST(name) static void name(void)
#define RUN(name) do { printf("%s\n", #name); fake_reset(); name(); } while (0)
static core_t c;
static void run_ms(uint32_t ms) { // advance time, polling every 5 ms
for (uint32_t t = 0; t < ms; t += 5) { hw.now += 5; core_poll(&c); }
}
// A fresh core in auto mode with nothing attached: no role, both chips off.
static void start_auto(void) {
core_init(&c);
CHECK(c.role == PS_ROLE_NONE);
CHECK(hw.xcvr == PS_ROLE_NONE);
}
// a computer attached, at the host's baud rate (received bytes go straight through)
static void start_dce_host_baud(void) {
start_auto();
CHECK(core_vendor_out(&c, PS_REQ_SET_BAUD_MODE, PS_BAUD_HOST));
hw.peer_dce = true; run_ms(500);
CHECK(c.role == PS_ROLE_DCE && c.ab_state == AB_OFF);
}
// A computer on the line makes the board a DCE, but only after the settle
// time and several agreeing samples, so one reading can't switch it.
TEST(detects_computer_as_dce) {
start_auto();
hw.peer_dce = true;
run_ms(CORE_SETTLE_MS + CORE_DETECT_PERIOD_MS); // one sample isn't enough
CHECK(c.role == PS_ROLE_NONE);
run_ms(CORE_DETECT_PERIOD_MS * CORE_DETECT_CONFIRM);
CHECK(c.role == PS_ROLE_DCE);
CHECK(hw.xcvr == PS_ROLE_DCE);
CHECK(hw.route == PS_ROLE_DCE);
CHECK(hw.format_calls >= 1);
}
// A modem on the line makes the board a DTE and powers chip B.
TEST(detects_modem_as_dte) {
start_auto();
hw.peer_dte = true;
run_ms(500);
CHECK(c.role == PS_ROLE_DTE);
CHECK(hw.xcvr == PS_ROLE_DTE);
}
// If both chips see a peer (e.g. a loopback or odd cable), the computer
// side wins: acting as a modem is the board's main job.
TEST(computer_wins_when_both_look_present) {
start_auto();
hw.peer_dce = hw.peer_dte = true;
run_ms(500);
CHECK(c.role == PS_ROLE_DCE);
}
// A single sample of a peer, then nothing, leaves the board undecided.
TEST(glitch_does_not_switch_on) {
start_auto();
run_ms(200);
hw.peer_dce = true; run_ms(CORE_DETECT_PERIOD_MS); // one sample
hw.peer_dce = false; run_ms(500);
CHECK(c.role == PS_ROLE_NONE);
}
// Unplugging is tolerated for just under CORE_LOST_MS, then the board drops
// back to detecting with both chips off, and can come back as the other role.
TEST(unplug_returns_to_detect) {
start_auto();
hw.peer_dce = true; run_ms(500);
CHECK(c.role == PS_ROLE_DCE);
hw.peer_dce = false;
run_ms(CORE_LOST_MS - 50);
CHECK(c.role == PS_ROLE_DCE); // brief loss tolerated
run_ms(100);
CHECK(c.role == PS_ROLE_NONE);
CHECK(hw.xcvr == PS_ROLE_NONE);
CHECK(hw.route == PS_ROLE_NONE);
hw.peer_dte = true; run_ms(500); // now a modem
CHECK(c.role == PS_ROLE_DTE);
}
// One good sample during a dropout restarts the loss timer, so a flaky
// cable doesn't keep the board switching roles.
TEST(short_loss_recovers) {
start_auto();
hw.peer_dce = true; run_ms(500);
hw.peer_dce = false; run_ms(400);
hw.peer_dce = true; run_ms(5);
hw.peer_dce = false; run_ms(800); // a fresh 800 ms: still under the limit
CHECK(c.role == PS_ROLE_DCE);
}
// A mode saved in flash (forced DTE) applies at start-up and detection
// can't override it, even with a computer on the line.
TEST(forced_mode_ignores_detection) {
core_settings_t s = {PS_MODE_DTE, PS_FLOW_AUTO, PS_BAUD_AUTO};
uint8_t blob[CORE_SETTINGS_BLOB];
core_settings_pack(&s, blob);
memcpy(hw.flash, blob, sizeof blob); hw.flash_valid = true;
core_init(&c);
CHECK(c.role == PS_ROLE_DTE);
hw.peer_dce = true; run_ms(3000);
CHECK(c.role == PS_ROLE_DTE);
}
// As a DCE, the host's DTR shows the computer DCD and DSR (like a modem
// that's on) and the host's RTS becomes CTS.
TEST(dce_line_mapping) {
start_auto();
hw.peer_dce = true; run_ms(500);
run_ms(5);
CHECK(hw.out == 0);
core_host_lines(&c, true, false); run_ms(5);
CHECK(hw.out == (PS_LINE_DCD | PS_LINE_DSR)); // host DTR = carrier
core_host_lines(&c, true, true); run_ms(5);
CHECK(hw.out == (PS_LINE_DCD | PS_LINE_DSR | PS_LINE_CTS));
core_host_lines(&c, false, true); run_ms(5);
CHECK(hw.out == PS_LINE_CTS);
}
// As a DTE, the host's DTR and RTS go straight out on DTR and RTS.
TEST(dte_line_mapping) {
start_auto();
hw.peer_dte = true; run_ms(500);
core_host_lines(&c, true, true); run_ms(5);
CHECK(hw.out == (PS_LINE_DTR | PS_LINE_RTS));
CHECK(hw.out_role == PS_ROLE_DTE);
}
// SET_OUTPUTS forces chosen lines on or off over the normal mapping (here
// RI on, DSR off); clearing it restores the mapping.
TEST(overrides) {
start_auto();
hw.peer_dce = true; run_ms(500);
core_host_lines(&c, true, true);
CHECK(core_vendor_out(&c, PS_REQ_SET_OUTPUTS, (uint16_t)((PS_LINE_RI | PS_LINE_DSR) << 8 | PS_LINE_RI)));
run_ms(5);
CHECK(hw.out == (PS_LINE_DCD | PS_LINE_RI | PS_LINE_CTS)); // RI on, DSR forced off
CHECK(core_vendor_out(&c, PS_REQ_SET_OUTPUTS, 0)); run_ms(5);
CHECK(hw.out == (PS_LINE_DCD | PS_LINE_DSR | PS_LINE_CTS));
}
// When the buffer to the host fills to 3/4, CTS to the computer drops; it
// comes back only once the buffer drains below half (hysteresis).
TEST(full_buffer_holds_the_far_end) {
start_dce_host_baud();
core_host_lines(&c, false, true); run_ms(5);
CHECK(hw.out & PS_LINE_CTS);
for (uint32_t i = 0; i < RING_SIZE * 3 / 4; i++) core_isr_rx(&c, 'x', 0);
run_ms(5);
CHECK(!(hw.out & PS_LINE_CTS));
uint8_t buf[256];
while (ring_count(&c.to_host) > RING_SIZE / 2 + 10) core_host_read(&c, buf, sizeof buf < 10 ? 1 : 10);
run_ms(5);
CHECK(!(hw.out & PS_LINE_CTS)); // hysteresis
core_host_read(&c, buf, 20); run_ms(5);
CHECK(hw.out & PS_LINE_CTS);
}
// Auto flow control: CTS is obeyed only once the far end has been seen
// using its RTS, so a 3-wire cable still works; it stays on until unplugged.
TEST(flow_auto_latches) {
start_auto();
hw.peer_dce = true; run_ms(500);
CHECK(!hw.hw_cts); // 3-wire cable: never obey an unused line
hw.in = PS_LINE_RTS; run_ms(5); // the computer uses RTS
CHECK(hw.hw_cts);
hw.in = 0; run_ms(5);
CHECK(hw.hw_cts); // ...and it stays obeyed
hw.peer_dce = false; run_ms(CORE_LOST_MS + 200);
CHECK(!hw.hw_cts); // forgotten when the peer goes
}
// The flow setting ON obeys CTS at once, OFF ignores it, and a bad value
// is refused.
TEST(flow_settings) {
start_auto();
hw.peer_dte = true; run_ms(500);
CHECK(core_vendor_out(&c, PS_REQ_SET_FLOW, PS_FLOW_ON)); run_ms(5);
CHECK(hw.hw_cts);
CHECK(core_vendor_out(&c, PS_REQ_SET_FLOW, PS_FLOW_OFF));
hw.in = PS_LINE_CTS; run_ms(5);
CHECK(!hw.hw_cts);
CHECK(!core_vendor_out(&c, PS_REQ_SET_FLOW, 7));
}
// SERIAL_STATE to the host as a DCE: sent only on change; carrier is on
// while a computer is attached, and the computer's DTR shows as DSR.
TEST(serial_state_dce) {
start_auto();
uint16_t s;
CHECK(core_serial_state(&c, &s) && s == 0);
CHECK(!core_serial_state(&c, &s)); // unchanged: nothing to send
hw.peer_dce = true; run_ms(500);
CHECK(core_serial_state(&c, &s) && s == SS_DCD);
hw.in = PS_LINE_DTR; run_ms(5);
CHECK(core_serial_state(&c, &s) && s == (SS_DCD | SS_DSR)); // computer's DTR shows as DSR
}
// As a DTE, the modem's DCD, DSR and RI reach the host; framing, overrun
// and break are reported once each and counted in the stats.
TEST(serial_state_dte_and_events) {
start_auto();
hw.peer_dte = true; run_ms(500);
hw.in = PS_LINE_DCD | PS_LINE_DSR | PS_LINE_RI; run_ms(5);
uint16_t s;
CHECK(core_serial_state(&c, &s) && s == (SS_DCD | SS_DSR | SS_RI));
core_isr_rx(&c, 0, RX_ERR_FRAMING | RX_ERR_OVERRUN);
CHECK(core_serial_state(&c, &s) && s == (SS_DCD | SS_DSR | SS_RI | SS_FRAMING | SS_OVERRUN));
CHECK(!core_serial_state(&c, &s)); // events are one-shot
core_isr_rx(&c, 0, RX_ERR_BREAK);
CHECK(core_serial_state(&c, &s) && (s & SS_BREAK));
CHECK(c.stats.breaks == 1 && c.stats.framing == 1 && c.stats.overrun == 1);
}
// The host's line coding reaches the UART; what the UART can't do (1.5 stop
// bits, mark parity) is adjusted and flagged, and rates above the chips'
// rating are flagged too.
TEST(line_coding) {
start_auto();
hw.peer_dce = true; run_ms(500);
core_host_format(&c, 9600, 0, 2, 7);
CHECK(hw.format.baud == 9600 && hw.format.parity == 2 && hw.format.data_bits == 7 && hw.format.stop_bits == 1);
CHECK(!(c.info_flags & PS_INFO_CODING_ADJUSTED));
core_host_format(&c, 2400, 1, 3, 8); // 1.5 stop, mark parity: the UART can't
CHECK(hw.format.stop_bits == 2 && hw.format.parity == 0);
CHECK(c.info_flags & PS_INFO_CODING_ADJUSTED);
core_host_format(&c, 460800, 0, 0, 8);
CHECK(c.info_flags & PS_INFO_BAUD_OVER_RATING);
CHECK(!(c.info_flags & PS_INFO_CODING_ADJUSTED));
}
// A timed break ends after its duration; 0xFFFF holds it until the host
// sends 0.
TEST(break_timing) {
start_auto();
hw.peer_dce = true; run_ms(500);
core_host_break(&c, 250);
CHECK(hw.brk);
run_ms(200); CHECK(hw.brk);
run_ms(60); CHECK(!hw.brk);
core_host_break(&c, 0xFFFF); run_ms(2000);
CHECK(hw.brk);
core_host_break(&c, 0);
CHECK(!hw.brk);
}
// Bytes from the host go out of the UART in order and received bytes reach
// the host, with both directions counted in the stats.
TEST(data_both_ways) {
start_dce_host_baud();
const uint8_t msg[] = "ATDT5551234\r";
CHECK(core_host_write(&c, msg, sizeof msg - 1) == sizeof msg - 1);
CHECK(hw.kicks > 0);
uint8_t b, out[32]; int n = 0;
while (core_isr_tx(&c, &b)) out[n++] = b;
CHECK(n == (int)sizeof msg - 1 && memcmp(out, msg, sizeof msg - 1) == 0);
core_isr_rx(&c, 'O', 0); core_isr_rx(&c, 'K', 0);
CHECK(core_host_read(&c, out, sizeof out) == 2 && out[0] == 'O' && out[1] == 'K');
CHECK(c.stats.to_rs232 == sizeof msg - 1 && c.stats.from_rs232 == 2);
}
// GET_INFO reports protocol, mode and role; unknown requests and bad modes
// are refused; SET_MODE switches at once and is saved once, after a delay.
TEST(vendor_info_and_mode) {
start_auto();
uint8_t buf[64];
int n = core_vendor_in(&c, PS_REQ_GET_INFO, 0, buf, sizeof buf);
CHECK(n == (int)sizeof(ps_info_t));
ps_info_t i; memcpy(&i, buf, sizeof i);
CHECK(i.protocol == PS_PROTOCOL_VERSION && i.mode == PS_MODE_AUTO && i.role == PS_ROLE_NONE && i.baud == 115200);
CHECK(core_vendor_in(&c, 0x7F, 0, buf, sizeof buf) == -1);
CHECK(!core_vendor_out(&c, PS_REQ_SET_MODE, 3));
CHECK(core_vendor_out(&c, PS_REQ_SET_MODE, PS_MODE_DCE));
CHECK(c.role == PS_ROLE_DCE && hw.xcvr == PS_ROLE_DCE); // switches at once
run_ms(CORE_SAVE_DELAY_MS + 20);
CHECK(hw.saves == 1);
core_settings_t s;
CHECK(core_settings_unpack(&s, hw.flash) && s.mode == PS_MODE_DCE);
run_ms(2000);
CHECK(hw.saves == 1); // not rewritten
CHECK(core_vendor_out(&c, PS_REQ_SET_MODE, PS_MODE_AUTO));
CHECK(c.role == PS_ROLE_NONE);
n = core_vendor_in(&c, PS_REQ_GET_INPUTS, 0, buf, sizeof buf);
CHECK(n == (int)sizeof(ps_inputs_t));
}
// Settings survive a pack/unpack round trip; a flipped bit or erased flash
// is rejected, and the core then starts with the defaults.
TEST(settings_blob) {
core_settings_t a = {PS_MODE_DTE, PS_FLOW_OFF, PS_BAUD_LEARN}, b = {0, 0, 0};
uint8_t blob[CORE_SETTINGS_BLOB];
core_settings_pack(&a, blob);
CHECK(core_settings_unpack(&b, blob) && b.mode == PS_MODE_DTE && b.flow == PS_FLOW_OFF && b.baud == PS_BAUD_LEARN);
blob[5] ^= 1;
CHECK(!core_settings_unpack(&b, blob)); // checksum catches it
memset(blob, 0xFF, sizeof blob); // erased flash
CHECK(!core_settings_unpack(&b, blob));
hw.flash_valid = true; memset(hw.flash, 0xFF, sizeof hw.flash);
core_init(&c);
CHECK(c.settings.mode == PS_MODE_AUTO && c.settings.flow == PS_FLOW_AUTO); // defaults
}
// The ring buffer holds exactly RING_SIZE bytes, refuses more, and gives
// them back in order across several wrap-arounds.
TEST(ring_wraps) {
static ring_t r;
ring_init(&r);
uint8_t b;
for (uint32_t round = 0; round < 3; round++) {
for (uint32_t i = 0; i < RING_SIZE; i++) CHECK(ring_put(&r, (uint8_t)i));
CHECK(!ring_put(&r, 0));
for (uint32_t i = 0; i < RING_SIZE; i++) CHECK(ring_get(&r, &b) && b == (uint8_t)i);
CHECK(!ring_get(&r, &b));
}
}
// LEDs: power on from the start; TR, CD and RD light for the host's DTR,
// the carrier and received data; RD goes out after its pulse.
TEST(leds) {
start_auto();
core_vendor_out(&c, PS_REQ_SET_BAUD_MODE, PS_BAUD_HOST);
run_ms(5);
CHECK(hw.leds == CORE_LED_PWR);
hw.peer_dce = true; run_ms(500);
hw.in = PS_LINE_DTR; core_host_lines(&c, true, false);
core_isr_rx(&c, 'a', 0);
run_ms(5);
CHECK(hw.leds == (CORE_LED_PWR | CORE_LED_TR | CORE_LED_CD | CORE_LED_RD));
run_ms(CORE_LED_PULSE_MS + 10);
CHECK(!(hw.leds & CORE_LED_RD));
}
// --- baud-rate learning ---
static wave_t wv;
static const uart_format_t F8N1 = {0, 8, 1, 0};
// Sends msg at baud (1% fast, edges jittered by 3% of a bit, one idle bit
// between characters) as level runs, the way the capture interrupt reports them.
static void send_runs(uint32_t baud, const char *msg, const uart_format_t *f) {
wave_make(&wv, FAKE_AB_HZ, baud, 0.01, f, (const uint8_t *)msg, strlen(msg), 1, 0.03);
for (uint32_t i = 0; i < wv.n; i++) core_isr_run(&c, wv.high[i], wv.ticks[i]);
}
// Reads what has reached the host (up to 128 bytes); true if it is exactly want.
static bool host_got(const char *want) {
uint8_t buf[128];
uint32_t n = core_host_read(&c, buf, sizeof buf);
return n == strlen(want) && memcmp(buf, want, n) == 0;
}
// The board's GET_INFO reply.
static ps_info_t info(void) {
ps_info_t i;
uint8_t buf[sizeof i];
CHECK(core_vendor_in(&c, PS_REQ_GET_INFO, 0, buf, sizeof buf) == (int)sizeof i);
memcpy(&i, buf, sizeof i);
return i;
}
// As a DCE in auto mode the board learns the computer's rate from "AT\r":
// the UART's garbage is dropped meanwhile, the characters it learned from
// still reach the host, and the host changing the format keeps that rate.
TEST(autobaud_learns_as_dce) {
start_auto();
hw.peer_dce = true; run_ms(500);
CHECK(c.ab_state == AB_LEARNING && hw.ab_role == PS_ROLE_DCE);
CHECK(info().flags & PS_INFO_BAUD_LEARNING);
CHECK(hw.format.baud == 115200); // the host's until learned
core_isr_rx(&c, 'x', RX_ERR_FRAMING); // the UART at the wrong rate: dropped
CHECK(ring_count(&c.to_host) == 0 && c.rx_errors == 0);
send_runs(9600, "AT\r", &F8N1);
run_ms(120);
CHECK(c.ab_state == AB_LOCKED && hw.ab_role == PS_ROLE_NONE);
CHECK(hw.format.baud == 9600 && hw.rx_flushes == 1);
CHECK(host_got("AT\r")); // the characters it learned from aren't lost
CHECK(c.stats.from_rs232 == 3);
ps_info_t i = info();
CHECK(i.baud == 9600 && (i.flags & PS_INFO_BAUD_LOCKED) && !(i.flags & PS_INFO_BAUD_LEARNING));
CHECK(i.baud_mode == PS_BAUD_AUTO);
core_isr_rx(&c, 'Z', 0); // now the UART's bytes go through
CHECK(host_got("Z"));
// the host changing the format keeps the learned rate
core_host_format(&c, 115200, 0, 2, 7);
CHECK(hw.format.baud == 9600 && hw.format.data_bits == 7 && hw.format.parity == 2);
CHECK(info().baud == 9600);
}
// After locking, a few scattered framing errors are ignored but a burst
// restarts learning, and the board locks onto the computer's new rate.
TEST(autobaud_relearns_on_framing_errors) {
start_auto();
hw.peer_dce = true; run_ms(500);
send_runs(2400, "ATZ\r", &F8N1); run_ms(120);
CHECK(c.ab_state == AB_LOCKED && hw.format.baud == 2400);
host_got("ATZ\r");
// scattered errors: not enough
core_isr_rx(&c, 0, RX_ERR_FRAMING); run_ms(1500);
core_isr_rx(&c, 0, RX_ERR_FRAMING); run_ms(1500);
core_isr_rx(&c, 0, RX_ERR_FRAMING); run_ms(1500);
CHECK(c.ab_state == AB_LOCKED);
// the computer switched to 115200: a burst of errors
for (int k = 0; k < 3; k++) { core_isr_rx(&c, 0, RX_ERR_FRAMING); run_ms(10); }
CHECK(c.ab_state == AB_LEARNING && hw.ab_role == PS_ROLE_DCE);
CHECK(hw.format.baud == 2400); // sends at the old rate until it knows
host_got("\0\0\0\0\0\0");
send_runs(115200, "AT&F\r", &F8N1); run_ms(120);
CHECK(c.ab_state == AB_LOCKED && hw.format.baud == 115200);
CHECK(host_got("AT&F\r"));
}
// AUTOBAUD_RESTART learns again; HOST mode goes back to the host's rate,
// stops learning and is saved; AUTO learns again.
TEST(autobaud_vendor_restart_and_modes) {
start_auto();
hw.peer_dce = true; run_ms(500);
send_runs(19200, "at\r", &F8N1); run_ms(120);
CHECK(hw.format.baud == 19200);
CHECK(core_vendor_out(&c, PS_REQ_AUTOBAUD_RESTART, 0));
CHECK(c.ab_state == AB_LEARNING);
send_runs(300, "AT\r", &F8N1); run_ms(120);
CHECK(hw.format.baud == 300);
// HOST: back to the host's rate, no learning
CHECK(core_vendor_out(&c, PS_REQ_SET_BAUD_MODE, PS_BAUD_HOST));
CHECK(c.ab_state == AB_OFF && hw.ab_role == PS_ROLE_NONE && hw.format.baud == 115200);
CHECK(core_vendor_out(&c, PS_REQ_AUTOBAUD_RESTART, 0) && c.ab_state == AB_OFF); // no effect
CHECK(!core_vendor_out(&c, PS_REQ_SET_BAUD_MODE, 3));
run_ms(CORE_SAVE_DELAY_MS + 20);
core_settings_t st;
CHECK(core_settings_unpack(&st, hw.flash) && st.baud == PS_BAUD_HOST);
CHECK(info().baud_mode == PS_BAUD_HOST);
// AUTO again: learns again
CHECK(core_vendor_out(&c, PS_REQ_SET_BAUD_MODE, PS_BAUD_AUTO));
CHECK(c.ab_state == AB_LEARNING);
}
// As a DTE, auto mode follows the host (a modem does); LEARN mode learns a
// device that just talks (here a GPS), and starts afresh after unplugging.
TEST(autobaud_dte) {
start_auto();
hw.peer_dte = true; run_ms(500);
CHECK(c.role == PS_ROLE_DTE && c.ab_state == AB_OFF && hw.ab_calls == 0); // AUTO: a modem follows the host
core_isr_rx(&c, 'K', 0);
CHECK(host_got("K"));
CHECK(core_vendor_out(&c, PS_REQ_SET_BAUD_MODE, PS_BAUD_LEARN));
CHECK(c.ab_state == AB_LEARNING && hw.ab_role == PS_ROLE_DTE);
send_runs(57600, "$GPGGA,", &F8N1); run_ms(120); // a device that just talks
CHECK(hw.format.baud == 57600 && host_got("$GPGGA,"));
// unplugged: stops, and starts afresh with the next device
hw.peer_dte = false; run_ms(CORE_LOST_MS + 200);
CHECK(c.role == PS_ROLE_NONE && c.ab_state == AB_OFF && hw.ab_role == PS_ROLE_NONE);
hw.peer_dte = true; run_ms(500);
CHECK(c.ab_state == AB_LEARNING && hw.format.baud == 115200);
}
// Learning copes with a glitch before typing, with lost timings (queue
// overflow), with pauses longer than the timer can count (wrapped or 0), and
// GET_INFO shows the host's rate again after unplugging.
TEST(autobaud_rough_starts) {
start_auto();
hw.peer_dce = true; run_ms(500);
// a glitch, then idle, then typing
core_isr_run(&c, false, 200); core_isr_run(&c, true, FAKE_AB_HZ / 10);
run_ms(120);
send_runs(4800, "ATDT5551212\r", &F8N1);
run_ms(200);
CHECK(c.ab_state == AB_LOCKED && hw.format.baud == 4800);
// timings lost (the interrupt's queue overflowed): starts over, still learns
CHECK(core_vendor_out(&c, PS_REQ_AUTOBAUD_RESTART, 0));
for (uint32_t k = 0; k < CORE_RUNS + 5; k++) core_isr_run(&c, k & 1, 1000);
CHECK(c.runs_lost);
run_ms(120);
CHECK(!c.runs_lost && c.ab_state == AB_LEARNING);
core_isr_run(&c, true, FAKE_AB_HZ / 10); // idle
send_runs(38400, "AT\r", &F8N1); run_ms(200);
CHECK(c.ab_state == AB_LOCKED && hw.format.baud == 38400);
// a pause longer than the timer can count, between characters
host_got("");
uint8_t junk[64]; while (core_host_read(&c, junk, sizeof junk)) {}
CHECK(core_vendor_out(&c, PS_REQ_AUTOBAUD_RESTART, 0));
send_runs(1200, "A", &F8N1); run_ms(120);
CHECK(c.ab_state == AB_LEARNING);
run_ms(CORE_AB_LONG_MS + 1000);
core_isr_run(&c, true, 5); // the count wrapped: looks short
send_runs(1200, "T\r", &F8N1); run_ms(120);
CHECK(c.ab_state == AB_LOCKED && hw.format.baud == 1200);
CHECK(host_got("AT\r"));
// a run the HAL couldn't count (0) is "long" too
CHECK(core_vendor_out(&c, PS_REQ_AUTOBAUD_RESTART, 0));
send_runs(600, "A", &F8N1); run_ms(30);
core_isr_run(&c, true, 0);
send_runs(600, "T\r", &F8N1); run_ms(200);
CHECK(c.ab_state == AB_LOCKED && hw.format.baud == 600);
CHECK(host_got("AT\r"));
// unplugged: GET_INFO is back to the host's speed
hw.peer_dce = false; run_ms(CORE_LOST_MS + 200);
CHECK(info().baud == 115200 && !(info().flags & (PS_INFO_BAUD_LOCKED | PS_INFO_BAUD_LEARNING)));
}
// Runs every test from a fresh fake board; exits 1 if any check failed.
int main(void) {
RUN(detects_computer_as_dce);
RUN(detects_modem_as_dte);
RUN(computer_wins_when_both_look_present);
RUN(glitch_does_not_switch_on);
RUN(unplug_returns_to_detect);
RUN(short_loss_recovers);
RUN(forced_mode_ignores_detection);
RUN(dce_line_mapping);
RUN(dte_line_mapping);
RUN(overrides);
RUN(full_buffer_holds_the_far_end);
RUN(flow_auto_latches);
RUN(flow_settings);
RUN(serial_state_dce);
RUN(serial_state_dte_and_events);
RUN(line_coding);
RUN(break_timing);
RUN(data_both_ways);
RUN(vendor_info_and_mode);
RUN(settings_blob);
RUN(ring_wraps);
RUN(leds);
RUN(autobaud_learns_as_dce);
RUN(autobaud_relearns_on_framing_errors);
RUN(autobaud_vendor_restart_and_modes);
RUN(autobaud_dte);
RUN(autobaud_rough_starts);
printf("%d checks, %d failed\n", checks, failures);
return failures ? 1 : 0;
}