rbtest: timed small messages, one 50 kB message, and MT checks
`rbtest` in the shell sample sends test messages with a 16-byte header and a counting ramp, so every byte can be checked at the other end. `rbtest small` sends a few 32-byte messages and logs how long each took, `rbtest large` sends one of up to 50000 bytes and only with `confirm`, and `rbtest mt` shows the last MT and checks it. `rb9704 pattern make` and `check` in python/ make and check the same messages. rb9704_send_fill writes a message straight into the module's buffer, so a large generated one is not held twice. overlay-large.conf raises the limit to 50000 bytes; on the M5Stamp-C3 that links on the USB console and not beside WiFi, which overflows by 11 kB. Run against the fake modem on native_sim only; not yet on a real modem.
This commit is contained in:
1 parent
9f68104ac6
commit
144efe683e
15 files changed
+798
-6
No files matched your search
@@ -44,6 +44,8 @@ macOS, Windows). Without the `modem` extra, the fake modem, `jspr`,
|
||||
| `cloud msg ID` | | one message record and its Lingo form |
|
||||
| `cloud mt --text/--hex/--file [--thing ID]` | airtime | queue an MT on `IMT_TOPIC_RAW` |
|
||||
| `cloud status ID` | | an MT's delivery status |
|
||||
| `pattern make [--size N] [--out F]` | | an `rbtest` test message, as hex or a file, to queue with `cloud mt` |
|
||||
| `pattern check --hex/--b64/--file` | | check every byte of one, as Cloudloop shows it |
|
||||
|
||||
The verbs that cost airtime print the size and ask; `--yes` skips the
|
||||
question and is the only way to send from a script.
|
||||
|
||||
@@ -12,6 +12,8 @@ rb9704 - drive a RockBLOCK 9704 from the host over its USB-C port.
|
||||
rb9704 [-p PORT] listen [--out DIR] [--for S]
|
||||
rb9704 [-p PORT] session (--text T | --hex H | --file F) [--quiet S] [--yes]
|
||||
rb9704 cloud ping | things | mo | msg ID | mt ... | status ID
|
||||
rb9704 pattern make [--size N] [--seq N] [--count N] [--out F]
|
||||
rb9704 pattern check (--hex H | --b64 B | --file F)
|
||||
|
||||
The modem side uses Ground Control's rockblock9704 package, a binding of
|
||||
the same C library this repository builds into Zephyr, so what works here
|
||||
@@ -23,6 +25,10 @@ the provisioning when a message is sent, and asking that way is free.
|
||||
question. Everything else on the modem side is local serial traffic and
|
||||
costs nothing. `cloud mt` costs airtime too.
|
||||
|
||||
`pattern` makes and checks the test message the shell sample's `rbtest`
|
||||
sends (rb9704tools/pattern.py): `check` takes the bytes Cloudloop shows,
|
||||
in hex or Base64, and `make` gives a message to queue with `cloud mt`.
|
||||
|
||||
The port is -p, or $RB9704_PORT, or the one FTDI FT234XD attached (the
|
||||
9704's USB-C bridge, 0403:6015). --log FILE appends every event as a JSON
|
||||
line, for the bench log.
|
||||
@@ -42,6 +48,8 @@ import urllib.error
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
from rb9704tools import pattern
|
||||
|
||||
FTDI_VID, FT234X_PID = 0x0403, 0x6015
|
||||
BAUD = 230400
|
||||
RAW_TOPIC = 244
|
||||
@@ -570,6 +578,38 @@ def cmd_cloud(a):
|
||||
ev("cloud_mt", bytes=len(data), topic=a.topic, result=json.dumps(r))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- pattern
|
||||
|
||||
def cmd_pattern(a):
|
||||
if a.verb == "make":
|
||||
data = pattern.make(a.size, seq=a.seq, count=a.count)
|
||||
if a.out:
|
||||
with open(a.out, "wb") as f:
|
||||
f.write(data)
|
||||
else:
|
||||
print(data.hex())
|
||||
return
|
||||
if a.hex is not None:
|
||||
data = bytes.fromhex(a.hex)
|
||||
elif a.b64 is not None:
|
||||
# Cloudloop leaves the padding off
|
||||
data = base64.b64decode(a.b64 + "=" * (-len(a.b64) % 4))
|
||||
else:
|
||||
with open(a.file, "rb") as f:
|
||||
data = f.read()
|
||||
r = pattern.check(data)
|
||||
if r is None:
|
||||
die(f"{len(data)} bytes, not a test message: {data[:16].hex()}")
|
||||
print(f"{r['kind']} {r['seq']}/{r['count']}, {r['got']} bytes, made at uptime "
|
||||
f"{r['uptime_ms'] / 1000:.3f} s")
|
||||
if r["ok"]:
|
||||
print("every byte as expected")
|
||||
elif r["bad_at"] == 8:
|
||||
die(f"the header says {r['length']} bytes, {r['got']} arrived")
|
||||
else:
|
||||
die(f"wrong from byte {r['bad_at']}")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- main
|
||||
|
||||
def add_payload(p, required=True):
|
||||
@@ -660,6 +700,20 @@ def main():
|
||||
add_payload(q)
|
||||
p.set_defaults(fn=cmd_cloud)
|
||||
|
||||
p = sub.add_parser("pattern", help="make or check an `rbtest` test message (free)")
|
||||
ps = p.add_subparsers(dest="verb", required=True)
|
||||
q = ps.add_parser("make", help="one test message, as hex or to a file")
|
||||
q.add_argument("--size", type=int, default=32)
|
||||
q.add_argument("--seq", type=int, default=1)
|
||||
q.add_argument("--count", type=int, default=1)
|
||||
q.add_argument("--out", help="write the bytes here; default hex on stdout")
|
||||
q = ps.add_parser("check", help="every byte of one, as Cloudloop shows it")
|
||||
g = q.add_mutually_exclusive_group(required=True)
|
||||
g.add_argument("--hex")
|
||||
g.add_argument("--b64")
|
||||
g.add_argument("--file")
|
||||
p.set_defaults(fn=cmd_pattern)
|
||||
|
||||
a = ap.parse_args()
|
||||
LOG = a.log
|
||||
a.fn(a)
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
# SPDX-License-Identifier: MIT
|
||||
# Copyright (c) 2026 Scott Penrose <scottp@dd.com.au>
|
||||
"""
|
||||
The test message `rbtest` in the shell sample sends, and checks when one
|
||||
arrives as an MT (samples/shell/src/rbtest.c has the same description).
|
||||
|
||||
A 16-byte header:
|
||||
|
||||
0..2 b"RBT"
|
||||
3 version, 1
|
||||
4 kind: b"S" small, b"L" large, anything else for one made here
|
||||
5 seq, from 1
|
||||
6 count in the run
|
||||
7 0
|
||||
8..11 the whole message's length, big-endian
|
||||
12..15 uptime in ms when it was made, big-endian
|
||||
|
||||
then from byte 16 to the end, byte k is k % 510 counted up 0..255 and back
|
||||
down 254..1.
|
||||
"""
|
||||
import struct
|
||||
|
||||
HEADER = 16
|
||||
MAGIC = b"RBT\x01"
|
||||
|
||||
|
||||
def ramp(k):
|
||||
v = k % 510
|
||||
return v if v < 256 else 510 - v
|
||||
|
||||
|
||||
def make(length, kind="M", seq=1, count=1, uptime_ms=0):
|
||||
if length <= HEADER:
|
||||
raise ValueError(f"a test message is more than {HEADER} bytes")
|
||||
head = MAGIC + struct.pack(">cBBxII", kind.encode()[:1], seq, count, length, uptime_ms)
|
||||
return head + bytes(ramp(k) for k in range(HEADER, length))
|
||||
|
||||
|
||||
def check(data):
|
||||
"""A dict describing data, with "ok" True when every byte is as made,
|
||||
or None when it is not a test message at all."""
|
||||
if len(data) < HEADER or not data.startswith(MAGIC):
|
||||
return None
|
||||
kind, seq, count, length, uptime_ms = struct.unpack(">cBBxII", data[4:HEADER])
|
||||
r = {"kind": kind.decode("ascii", "replace"), "seq": seq, "count": count,
|
||||
"length": length, "got": len(data), "uptime_ms": uptime_ms, "bad_at": None}
|
||||
if length != len(data):
|
||||
r["bad_at"] = 8
|
||||
else:
|
||||
r["bad_at"] = next((k for k in range(HEADER, len(data)) if data[k] != ramp(k)), None)
|
||||
r["ok"] = r["bad_at"] is None
|
||||
return r
|
||||
@@ -2,11 +2,14 @@
|
||||
# Copyright (c) 2026 Scott Penrose <scottp@dd.com.au>
|
||||
"""The rb9704 command line against the fake modem."""
|
||||
|
||||
import base64
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
from rb9704tools import pattern
|
||||
|
||||
|
||||
def run(*args, timeout=30):
|
||||
return subprocess.run([sys.executable, "-m", "rb9704tools.cli", *args], capture_output=True,
|
||||
@@ -46,3 +49,29 @@ def test_send_with_yes(fake):
|
||||
assert r.returncode == 0, r.stdout + r.stderr
|
||||
m = fake.out.expect(r"^MO 244 \d+ (\w+)")
|
||||
assert bytes.fromhex(m.group(1)) == b"cli-hello"
|
||||
|
||||
|
||||
def test_pattern_make_and_check():
|
||||
r = run("pattern", "make", "--size", "1000", "--seq", "2", "--count", "3")
|
||||
assert r.returncode == 0, r.stderr
|
||||
hexed = r.stdout.strip()
|
||||
data = bytes.fromhex(hexed)
|
||||
assert len(data) == 1000 and data[:4] == b"RBT\x01"
|
||||
# the ramp turns at 255 and comes back down
|
||||
assert data[255] == 255 and data[256] == 254 and data[510] == 0
|
||||
r = run("pattern", "check", "--hex", hexed)
|
||||
assert r.returncode == 0, r.stdout + r.stderr
|
||||
assert "M 2/3, 1000 bytes" in r.stdout and "every byte as expected" in r.stdout
|
||||
b64 = base64.b64encode(data).decode().rstrip("=")
|
||||
assert run("pattern", "check", "--b64", b64).returncode == 0
|
||||
|
||||
|
||||
def test_pattern_check_finds_the_damage():
|
||||
data = bytearray(pattern.make(100))
|
||||
data[40] ^= 1
|
||||
r = run("pattern", "check", "--hex", data.hex())
|
||||
assert r.returncode != 0 and "byte 40" in r.stdout + r.stderr
|
||||
r = run("pattern", "check", "--hex", pattern.make(100)[:90].hex())
|
||||
assert r.returncode != 0 and "says 100 bytes, 90 arrived" in r.stdout + r.stderr
|
||||
r = run("pattern", "check", "--hex", b"hello".hex())
|
||||
assert r.returncode != 0 and "not a test message" in r.stdout + r.stderr
|
||||
@@ -10,6 +10,8 @@ import zlib
|
||||
|
||||
import pytest
|
||||
|
||||
from rb9704tools import pattern
|
||||
|
||||
|
||||
def begin(z):
|
||||
c, f = z
|
||||
@@ -181,3 +183,61 @@ def test_end(zephyr):
|
||||
c.out.expect(r"end: ok")
|
||||
c.sh("rb9704 send 244 after-end")
|
||||
c.out.expect(r"send: -\d+ ENOTCONN")
|
||||
|
||||
|
||||
def test_rbtest_small_sends_checkable_messages(zephyr):
|
||||
"""`rbtest small`, two messages with no gap: each reaches the network
|
||||
whole, numbered, and its time is logged."""
|
||||
c, f = zephyr
|
||||
begin(zephyr)
|
||||
c.sh("rbtest small 244 2 0")
|
||||
c.out.expect(r"2 messages of 32 bytes on topic 244")
|
||||
for seq in (1, 2):
|
||||
m = f.out.expect(r"^MO 244 \d+ (\w+)")
|
||||
r = pattern.check(bytes.fromhex(m.group(1)))
|
||||
assert r["ok"] and (r["kind"], r["seq"], r["count"], r["got"]) == ("S", seq, 2, 32)
|
||||
c.out.expect(r"rbtest S %d/2: sent in \d+\.\d{3} s" % seq)
|
||||
c.out.expect(r"rbtest S: 2 of 2 sent$")
|
||||
c.sh("rbtest status")
|
||||
c.out.expect(r"last run S: 2 of 2 done, 32 bytes each on topic 244")
|
||||
c.out.expect(r"2: sent in \d+\.\d{3} s")
|
||||
|
||||
|
||||
def test_rbtest_small_logs_a_failure(zephyr):
|
||||
c, f = zephyr
|
||||
begin(zephyr)
|
||||
f.cmd("mo-fail network_error")
|
||||
c.sh("rbtest small 244 1 0")
|
||||
c.out.expect(r"rbtest S 1/1: failed after \d+ ms: -\d+")
|
||||
c.out.expect(r"rbtest S: 0 of 1 sent$")
|
||||
|
||||
|
||||
def test_rbtest_large_asks_first(zephyr):
|
||||
"""Without confirm nothing goes; with it, one message of the size given
|
||||
(the 50000 default needs CONFIG_RB9704_MESSAGE_MAX raised)."""
|
||||
c, f = zephyr
|
||||
begin(zephyr)
|
||||
c.sh("rbtest large 244 2000")
|
||||
c.out.expect(r"Nothing sent. To send it: rbtest large 244 2000 confirm")
|
||||
with pytest.raises(AssertionError):
|
||||
f.out.expect(r"^MO ", timeout=2)
|
||||
c.sh("rbtest large 244 confirm")
|
||||
c.out.expect(r"50000 bytes: from 17 to \d+")
|
||||
c.sh("rbtest large 244 2000 confirm")
|
||||
m = f.out.expect(r"^MO 244 \d+ (\w+)")
|
||||
r = pattern.check(bytes.fromhex(m.group(1)))
|
||||
assert r["ok"] and (r["kind"], r["got"]) == ("L", 2000)
|
||||
c.out.expect(r"rbtest L 1/1: sent in")
|
||||
|
||||
|
||||
def test_rbtest_mt_checks_a_test_message(zephyr):
|
||||
c, f = zephyr
|
||||
begin(zephyr)
|
||||
c.sh("rbtest mt")
|
||||
c.out.expect(r"no MT since boot|mt\s+\d+ since boot")
|
||||
f.cmd("mthex 244 " + pattern.make(1200, seq=4, count=5).hex())
|
||||
c.out.expect(r"rbtest: MT is a test message, M 4/5, all 1200 bytes as expected")
|
||||
c.sh("rbtest mt")
|
||||
c.out.expect(r"topic 244 id \d+, 1200 bytes, crc %08x" % zlib.crc32(pattern.make(1200, seq=4, count=5)))
|
||||
c.out.expect(r"52 42 54 01 4d 04 05 00 00 00 04 b0")
|
||||
c.out.expect(r"test message M 4/5, all 1200 bytes as expected")
|
||||
Reference in new issue
Block a user