diff --git a/firmware/README.md b/firmware/README.md index 0467f0b..721eda3 100644 --- a/firmware/README.md +++ b/firmware/README.md @@ -30,3 +30,20 @@ Sends a companion frame and prints the reply frames. Uses only the Python standa tools/companion_query.py /dev/cu.usbmodemXXXX # CMD_DEVICE_QUERY over the XIAO's USB tools/companion_query.py /dev/cu.usbserial-XXXX 16 03 # over a USB-TTL adapter on D6/D7 ``` + +## tools/fake_radio.py + +A fake companion radio on a pseudo-terminal, for testing clients without transmitting. It answers the protocol with: +- device and self info, and the clock +- channels (Public, `#test`; unused slots carry stale bytes like the real firmware) +- one contact of each type, plus one with a UTF-8 name +- queued messages announced with `PUSH_MSG_WAITING` +- DM sends: `SENT`, then `PUSH_SEND_CONFIRMED` after about 2 s (never if the text contains "noack"), then an echo reply +- a channel message from "Bob" every `--chat` seconds + +```sh +tools/fake_radio.py # prints its pty, e.g. /dev/ttys004 +../palm-client/tools/visor_bridge.py /dev/ttys004 # bridge the Visor to it +``` + +Type `c ` or `d ` at its prompt to inject a channel message or a DM. Note that the Visor stores what it receives from the fake radio in its real message database. diff --git a/firmware/tools/fake_radio.py b/firmware/tools/fake_radio.py new file mode 100755 index 0000000..688eb7d --- /dev/null +++ b/firmware/tools/fake_radio.py @@ -0,0 +1,202 @@ +#!/usr/bin/env python3 +"""A fake MeshCore companion radio on a pseudo-terminal, for testing clients +without transmitting anything. + + fake_radio.py # prints the pty path, e.g. /dev/ttys012 + visor_bridge.py /dev/ttys012 # then bridge the Visor to it + +It answers the companion protocol (layouts per MeshCore's MyMesh.cpp): +device/self info, clock, channels (Public, #test; unused slots carry stale +bytes like the real firmware), contacts of every type, queued messages behind +PUSH_MSG_WAITING, and sends: a DM gets SENT then PUSH_SEND_CONFIRMED after +~2 s (never, if its text contains "noack"), plus an echo reply. A channel +message from "Bob" arrives every --chat seconds. + +Type at its prompt to inject messages: + c channel message on Public from "Alice" + d DM from the first contact +""" +import argparse +import os +import random +import select +import struct +import sys +import threading +import time + +NAME = 'FakeRadio' +CHANNELS = {0: 'Public', 1: '#test'} +MAX_CHANNELS = 8 +CONTACTS = [ # (key prefix byte, type, name, path_len) + (0xA1, 1, 'Alice', 0x00), + (0xA2, 1, 'Bob ☃', 0x41), # snowman: a UTF-8 name + (0xA3, 2, 'Hilltop Repeater', 0x42), + (0xA4, 3, 'Rittenhouse Room', 0xFF), + (0xA5, 4, 'Weather Sensor', 0x01), +] + + +def key_of(b): + return bytes([b] * 32) + + +class Radio: + def __init__(self, fd, chat_every): + self.fd = fd + self.lock = threading.Lock() + self.queue = [] # pending SYNC_NEXT_MESSAGE frames (payloads) + self.clock_offset = 0 + self.out_buf = bytearray() # the firmware reuses one reply buffer: unused channel replies show its leftovers + self.chat_every = chat_every + + def now(self): + return int(time.time()) + self.clock_offset + + def send(self, payload): + self.out_buf[:len(payload)] = payload + with self.lock: + os.write(self.fd, b'>' + struct.pack('= self.now(): + self.clock_offset += t - self.now() + self.ok() + else: + self.send(b'\x01\x06') + elif code == 31: # GET_CHANNEL + idx = p[1] + if idx >= MAX_CHANNELS: + self.send(b'\x01\x02') + return + name = CHANNELS.get(idx, '') + reply = bytearray(self.out_buf[:50].ljust(50, b'\0')) # stale bytes, as the firmware does + reply[0], reply[1] = 18, idx + enc = name.encode() + reply[2:2 + len(enc) + 1] = enc + b'\0' + if name: + reply[2 + 32:50] = bytes(range(16)) + self.send(bytes(reply)) + elif code == 4: # GET_CONTACTS + self.send(b'\x02' + struct.pack(' 0: + threading.Thread(target=radio.chatter, daemon=True).start() + try: + for line in sys.stdin: + line = line.strip() + if line.startswith('c '): + radio.queue_channel(0, 'Alice', line[2:]) + elif line.startswith('d '): + radio.queue_dm(CONTACTS[0][0], line[2:]) + except KeyboardInterrupt: + pass + + +if __name__ == '__main__': + main()