Metadata-Version: 2.4
Name: meshcore-desktop
Version: 0.9.16
Summary: MeshCore companion desktop client for Linux
License: MIT
Requires-Python: >=3.11
Description-Content-Type: text/markdown
Requires-Dist: pyserial>=3.5
Requires-Dist: cryptography>=41
Provides-Extra: gui
Requires-Dist: PySide6>=6.6; extra == "gui"
Provides-Extra: flash
Requires-Dist: esptool>=4.7; extra == "flash"
Provides-Extra: dev
Requires-Dist: pytest>=7.4; extra == "dev"
Requires-Dist: pytest-xdist>=3.5; extra == "dev"

# MeshCore desktop companion (Linux)

A desktop client for a USB-connected [MeshCore](https://meshcore.co.uk)
companion radio. Classical chat-client layout — conversations on the left, the
exchange in the middle, a map of discovered nodes alongside — plus tools for
looking after the repeaters you run.

Any MeshCore companion radio presenting a USB serial port works. Devices are
identified by the **protocol handshake**, never by a USB VID/PID whitelist;
known IDs only affect the order ports are probed in. Developed against a
Heltec WiFi LoRa 32 V3, but nothing is specific to it.

---

## What it does

**Chat**
- Contacts and channels in one list, ordered by recent activity
- Favourites pinned in a collapsible section, for people and channels alike
- Message bubbles with delivery state, per-message SNR and hop count, and
  **the route each message arrived over** — a *Show path* link that draws it
  on the app's own map
- Delivery that keeps its promise: an unacknowledged direct message is
  retried, and a stale route dropped so the retry floods
- Select and copy message text, with `Ctrl+C` and a context menu
- `@mentions` with completion, including people who have only spoken in a
  channel; a mention of you is highlighted and raises the notification.
  Written on the air as `@[name]`, the form every other MeshCore client
  uses, and shown without the brackets
- Emoji picker, and a character counter against the **real** limit (160 bytes
  direct, less on a channel — the radio prepends your node name)
- Clickable `http`/`https` links, and nothing else, deliberately
- Desktop notifications, with a choice of always / unfocused / never
- Outgoing messages labelled with the identity that sent them (once you have
  more than one) and the region they went out on

**Nodes and map**
- Repeaters and sensors on their own page, kept out of the chat list
- Every advert ever heard, including nodes never added as contacts
- Map with pins, position-uncertainty circles, historical tracks and
  **offline tile caching**
- Routing paths resolved to node names and drawn on the map

**Managing your repeaters**
- Mark nodes as yours; they get a **Manage repeaters** page that flags
  anything not heard for six hours
- Log in over the air and run the firmware's own CLI
- A configuration form for every setting the firmware exposes, readable
  section by section
- Region (flood scope) management
- Battery and temperature over the air, with a guest password — no admin needed

**Your data**
- SQLite archive with built-in backup and restore, shared schema with the
  Android client (a `.mcbackup` moves between them)
- Radio profiles: save a whole radio configuration — optionally including its
  identity — and put it back on this radio or another one
- Generate a new node identity in the app, without a factory reset
- Firmware library and flasher: import images with type, version and
  supported hardware derived from the filename, kept in your data directory
  and included in backups; flash ESP32 boards over serial and nRF52/RP2040
  boards over UF2, with no internet needed
- Start with the computer, tray icon, light/dark themes

---

## There is a second client

`android/` is a Qt/QML client for Android, built on the same protocol core
and the **same database schema** — a backup written on the desktop restores
on the phone and the other way round. It has the chat, the map, the nodes
and repeater lists, repeater administration, profiles, the firmware library
and backup/restore.

Three pieces are shared rather than reimplemented, and each is generated
from this repository so the two cannot drift:

| Shared | Generated by | Used for |
|---|---|---|
| `schema/schema.sql`, `schema/migrations/` | `tools/make_schema.py` | one database definition, and forward migrations any client can apply |
| `qt/firmware_catalog.h` | `tools/make_catalog.py` | 114 boards, 14 roles, 32 manufacturer prefixes |
| `qt/repeater_config.h` | `tools/make_repeater_config.py` | the repeater configuration form |
| `conformance/` | `tools/make_conformance.py` | 86 vectors the C++ core is held to |

`qt/` holds the Qt-but-not-Android code both would otherwise duplicate: the
store, the firmware library. A test fails if any generated file goes stale.

## Requirements

- Linux, recent Debian/Ubuntu family, x86_64
- Python 3.10+ (3.12 recommended)
- A MeshCore companion radio flashed with a **USB serial** companion build

---

## Install

### Debian / Ubuntu package

```bash
sudo apt install ./build/meshcore-desktop_*_amd64.deb
```

Self-contained: it vendors its own PySide6 into `/opt/meshcore-desktop`
rather than depending on distribution Qt packages, because QtLocation for
PySide6 is not packaged on most releases and the map needs it. PySide6's
wheels are `cp310-abi3`, so one build covers Python 3.10 to 3.13 — Ubuntu
22.04 through Debian 13 — and no virtualenv is baked in, so a system Python
upgrade cannot break it.

Build it yourself with `./packaging/build-deb.sh` (needs `dpkg-deb`, no root).
The `.deb` is a **snapshot**: rebuild it after changing the source, and bump
the version in `pyproject.toml` or `apt` will treat it as already installed.

### From source

```bash
python3 -m venv .venv
.venv/bin/pip install -e ".[gui]"
./packaging/install-desktop-entry.sh    # menu entry and icons, no root
```

### Serial port permissions

On Debian/Ubuntu-family systems `/dev/ttyUSB*` belongs to the `dialout` group,
so the port exists and refuses to open unless your user is in it.

**You should not have to do anything about this.** The `.deb` installs
`/usr/lib/udev/rules.d/70-meshcore-radio.rules`, which tags USB serial devices
`uaccess` so systemd-logind puts an ACL for whoever is logged in at the
computer onto the device node — no group, and no logging out. If you run from
a checkout instead, the app notices at startup and offers to install the same
rule for you, asking for an administrator password through the desktop's own
prompt.

The rule is scoped by subsystem (a tty with a USB device in its parent chain),
never by USB vendor/product: this project identifies a radio by the protocol
handshake, not by a device whitelist.

If neither applies — no polkit, or a session with no local seat, such as over
SSH — the old route still works:

```bash
sudo usermod -aG dialout $USER
```

Then **log out and back in**; group membership is only picked up at login, and
`newgrp dialout` works for one shell as a stopgap. Whichever way it fails, the
app says so in words rather than printing a stack trace.

---

## Usage

### The app

```bash
.venv/bin/meshcore                    # probe, connect, open the window
.venv/bin/meshcore --port /dev/ttyUSB0
.venv/bin/meshcore --offline          # browse the archive with no radio
.venv/bin/meshcore --theme dark       # or light; defaults to the desktop's
.venv/bin/meshcore --minimized        # start in the tray
```

A navigation rail runs down the left: **Chat**, **Repeaters**, **Nodes**,
**Map**, then **Manage repeaters** (appears once you mark one) and
**Settings** at the foot. There is no menu bar; everything lives where it
applies.

### Command line

```bash
meshcore-cli probe                       # find the radio, print what it is
meshcore-cli monitor --raw --decode      # watch decoded frames
meshcore-cli contacts                    # sync and list contacts
meshcore-cli send "OZ1LLA Mobil" "hello"
meshcore-cli send --channel 0 "hello mesh"
meshcore-cli advert --flood              # send an advert, and prove it transmitted
meshcore-cli backup ~/backups/           # dated file in a directory
meshcore-cli restore ~/backups/meshcore-20260830.db
```

`monitor` does **not** drain the radio's message queue unless you pass
`--sync`, because reading a message deletes it from the device.

`advert` is also the diagnostic to reach for when nobody can hear you: it
reports the radio's own packet counters and TX airtime before and after, so
"the command was accepted" and "a packet reached the air" are told apart.

---

## Your data

Everything lives in `~/.local/share/meshcore-desktop/`:

```
store.db        the archive — contacts, messages, channels, adverts,
                positions, routing paths, settings
store.db-wal    recent writes, not yet folded into store.db
tiles/          cached OpenStreetMap tiles — NOT regenerable offline
profiles/       saved radio configurations, one JSON file each
firmware/       imported firmware images, plus index.json
```

**Do not back it up with `cp`.** The database runs in WAL mode, so recent
writes live in `store.db-wal` until a checkpoint; a plain copy silently
produces an archive missing whatever arrived since, and on a young database
can contain no tables at all. Use *Settings → Data → Back up*, or:

```bash
meshcore-cli backup ~/backups/
```

This matters more than usual here. **The radio's message queue is drained
destructively** — reading a message removes it from the device, and the queue
holds only 16 entries — so for anything received, this archive is the only
copy that exists.

A `.mcbackup` is a ZIP holding the database, your profiles, the cached map
tiles and the firmware library — everything that cannot be fetched again
without internet. Copy it to another machine and restore it there; restoring
saves your current data alongside first, and merges tiles rather than
replacing them. Backups written by earlier versions were bare SQLite files
and still restore.

### Radio profiles

A backup holds *your* data. It does not hold the radio's own configuration,
because that lives on the radio rather than on this machine. *Settings →
Profiles → Save this radio* reads it over the wire into
`profiles/<name>.json`: node name, position, frequency, bandwidth,
spreading factor, coding rate, TX power, tuning, auto-add, default region
and channels. Apply it later to put it back, on this radio or another one.

**New identity…** mints a fresh keypair locally and stores it in a profile
alongside the radio's current settings. Nothing is written to the radio until
you apply it, so you can keep several identities and switch between them —
the firmware itself has no way to do this short of a factory reset, which
formats its filesystem. If the radio's current identity is not yet in a
profile, the dialog saves it first.

Ticking **Include the identity** also saves the radio's private key — the
64 bytes that make it *that node* on the mesh, rather than a radio with the
same settings. Worth doing if you want your name and public key to survive a
dead device; the file is written readable only by you, and anyone who holds
it can be your node. Some firmware builds refuse to export it, in which case
the app says so and saves everything else.

---

## Documentation

| Document | What it covers |
|---|---|
| [`docs/README.md`](docs/README.md) | **Start here.** Which of these to read, and when. |
| [`docs/AGENTS.md`](docs/AGENTS.md) | The standing brief for anyone working on this — what is not yours to change, how to iterate without wasting an hour, and what will brick the test phone. |
| [`docs/PROTOCOL.md`](docs/PROTOCOL.md) | The companion protocol, derived from firmware source, written so a C++ client can be built from it alone. Records the firmware commit it came from, and where the official documentation is wrong. |
| [`docs/ARCHITECTURE.md`](docs/ARCHITECTURE.md) | Layering, threading, event flow, the invariants, and a table of things that look like bugs and are not. |
| [`conformance/`](conformance/README.md) | Language-neutral test vectors for reimplementing the protocol, including 17 frames captured from real hardware. Generated by `tools/make_conformance.py`. |
| [`docs/UI.md`](docs/UI.md) | UX decisions and the reasoning behind them, including what was verified by testing rather than assumed. |
| [`cpp/README.md`](cpp/README.md) | The C++17 protocol core: what is ported, what is not, and the one rule to know before using `Session`. Shared by the Android client and intended for the handheld. |
| [`android/README.md`](android/README.md) | The Android client: what it needs from a radio, the traps that cost a day each, and what is still missing. |
| [`docs/SYNC.md`](docs/SYNC.md) | Multi-profile scoping and device sync. **All six phases are built**: one archive holding several radios, merge rules, sync over the LAN with discovery, tiles and firmware, and composing on one device to transmit from another. Records why each rule is what it is. |
| [`docs/FEATURES.md`](docs/FEATURES.md) | Every capability in both clients side by side, marked yes/no/partial and verified against the source. Says of each difference whether it is a platform limit, a deliberate choice, or a gap. |
| [`docs/BETA.md`](docs/BETA.md) | The plan for the first beta, and **the task list** — every outstanding item has an ID (B build, F fix, V verify on hardware, D decide). Partly built; the planning is not finished. |
| [`docs/SUGGESTIONS.md`](docs/SUGGESTIONS.md) | Things found but deliberately not started, sized and grouped by what they need. A queue to pick from, not a plan — this is where findings go instead of quietly becoming work. |
| [`docs/ARTIFACTS.md`](docs/ARTIFACTS.md) | Which documents are also published as web pages, and the rule that stops the two disagreeing. |

---

## Development

```bash
.venv/bin/pip install -e ".[dev,gui]"
.venv/bin/python -m pytest -q -n auto
```

**1161 tests, no hardware needed.** Use `-n auto`: the suite takes 257 s
serially and 61 s across eight cores, and what makes it slow is Qt — a
handful of offscreen widget tests cost 7–13 s each, and `test_qt_sync.py`
compiles and runs the phone's own C++ against the desktop's Python over a
real socket. The C++ suite (`cd cpp && make test`, 1108 checks) takes seven
seconds.

`tests/fixtures/session_heltec_v3.bin` is a
byte-exact capture of a real session, replayed as a regression corpus;
refresh it against your own radio with:

```bash
.venv/bin/python -m tests.record_fixture --port /dev/ttyUSB0
```

That capture contains your node's public key and your contacts' — all
broadcast over the air in adverts anyway, but worth knowing before publishing
a repository.

The UI is smoke-tested by rendering under `QT_QPA_PLATFORM=offscreen` and by
**pressing the buttons**. That second part exists because of a real bug:
`_backup` was rewritten to open a dialog, `QDialog` was never imported, and
the suite stayed green because the test asserted `callable(window._backup)`
— which a method with a `NameError` in its body satisfies perfectly. Daniel
found it by clicking. The tests now click too.

### Test devices

Two machines are wired up for this, and `docs/AGENTS.md` holds the rules for
using them safely — read it before touching either.

**The Zorin laptop** runs the packaged `.deb` and has Bluetooth hardware,
which the development machine does not:

```bash
ssh daniel@192.168.240.151          # passwordless, key-based
```

Patch `/opt/meshcore-desktop/lib/…` in place to test a change. Never rebuild
and copy the package — that is ten minutes and 80 MB per iteration.

**The Galaxy S25** runs the Android client:

```bash
~/Android/sdk/platform-tools/adb connect 192.168.240.38:5555
```

Set up once from USB with `adb tcpip 5555`; that port is fixed and survives
the screen locking, where Samsung's *Wireless debugging* toggle does not. It
resets on reboot and has to be redone from a cable.

**There is no USB cable at the desk.** Anything that severs the network —
airplane mode, Wi-Fi off, a reboot — makes the phone unreachable until
somebody walks over to it. And never uninstall: the archive on that phone is
the only copy of its messages.

### Layout

```
meshcore/
  protocol/   pure stdlib: framing, codes, payloads, session, telemetry
  transport/  pyserial, reader thread, discovery by handshake
  store/      SQLite schema, migrations, models
  app/        controller, mention and path parsing
  ui/         Qt widgets, QML map, theme
  cli.py      probe / monitor / contacts / send / advert / backup / restore
```

Lower layers never import from higher ones, and `protocol/` is pure standard
library — both enforced by `tests/test_layering.py`. That package plus
`PROTOCOL.md` is what gets hand-translated to C++17 for a future handheld
client, so keeping it free of pyserial and Qt is the point.

---

## Troubleshooting

**Nothing hears me, but I hear everyone.** Receiving is gated only by the
channel key; flood scopes affect relaying. Check *Manage repeaters →
Diagnostics*, or `meshcore-cli advert --flood`, which reports whether a
packet actually reached the air. If TX airtime is zero since boot, check the
antenna before transmitting further.

**I see nothing on a channel others are using.** A channel key mismatch. The
key is `sha256(name)` of the name *exactly as typed*, so `horsens` and
`#horsens` are different channels. There is no error for a wrong key: the
packet arrives, the MAC check fails, and it is dropped.

**Messages arrive truncated on a channel.** The radio prepends
`"<node_name>: "` before encrypting and then silently truncates to 160 bytes.
The composer's counter already accounts for it.

**Timestamps are wrong.** The radio's clock is set only by whichever client
connected last and resets on power loss; on first connect here it was 2.3
years behind. The app sets it, but cannot move a clock backwards — the
firmware refuses. Both the device and host timestamps are stored, and host
time is what you see.

**No notifications.** They need a freedesktop notification daemon and
`gdbus`. Without them the window flashes in the taskbar instead; *Settings →
General* says so.

---

## Non-goals

Windows and macOS, BLE or WiFi transports, voice or file transfer, and any
reimplementation of MeshCore's routing or crypto — the radio does all of
that. This is a terminal for it.

Repeater administration *was* a non-goal and is now implemented, at the
user's request.
