Every packet now leads with its class — icon + UPPERCASE label — as the first row of the viewer, whatever the kind: a raw frame shows its parsed payload class, everything else its own kind. The new PAYLOAD_ICONS table gives each raw payload class a concept glyph (📻 channel text, 🎯 trace, …, matching the tools that own those concepts) and joins the CLAUDE.md icon lexicon; the dialog title drops its emoji, per the title rule the icon was violating. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
661 lines
29 KiB
Python
661 lines
29 KiB
Python
"""The packet viewer: one floating popup that can open any packet a list shows.
|
|
|
|
Everywhere MeshTerm lists packets (the dashboard feed today; any future packet list)
|
|
the rows share this module's per-kind chrome — a colour and a two-cell icon per packet
|
|
class — and every row can open into the same viewer: a centered dialog over the list
|
|
that lays the packet out in full, flavoured by kind. An advert shows the node's
|
|
identity, type, and location; telemetry shows the node and its reported values; an
|
|
RX-logged packet shows its parsed class and route plus the relay path it rode in on —
|
|
as a ``via`` chain and, when it actually crossed a relay, as THE route graph
|
|
(:mod:`~meshterm.ui.pathgraph`): origin → relays → us, the same fan-lane picture the
|
|
Message paths dialog and the Trophy case draw — and, for an overheard channel-text frame
|
|
naming a channel we hold the key for, the decrypted text too (see
|
|
:func:`~meshterm.core.channels.decrypt_channel_text`), even though the radio itself never
|
|
decoded it for us; a message shows the sender, the
|
|
conversation, and the text; an ack shows its code. Common to all: the class headline
|
|
(icon + UPPERCASE class, first row of the card), the timestamp, the node (name coloured
|
|
by the app-wide palette, key in the key widget), reception quality on the shared SNR
|
|
bar, and any leftover raw field the flavoured layout doesn't already show.
|
|
|
|
When opened over a list the viewer pages through it in place — ``↑``/``↓`` step to
|
|
the newer/older packet, Home/End jump to the newest/oldest, mirroring the keys the
|
|
opening list itself walks rows with — so a burst can be read packet by packet
|
|
without bouncing back out to the list. PgUp/PgDn scroll a tall packet inside the
|
|
dialog, matching what they do on every other screen; Esc closes it.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from datetime import datetime
|
|
from typing import Callable, Optional, Sequence
|
|
|
|
from rich.console import RenderableType
|
|
from rich.table import Table
|
|
from rich.text import Text
|
|
|
|
from ..core.channels import decrypt_channel_text
|
|
from ..core.models import NODE_TYPE_LABELS, Observation
|
|
from ..services.trace_runner import NodeResolver
|
|
from .pathgraph import PathLayer, render_path_graph
|
|
from .theme import name_style, snr_style
|
|
from .trace_screen import snr_bar
|
|
from .tui.render import render_lines, render_to_ansi
|
|
from .tui.screen import Screen
|
|
from .widgets import (
|
|
TypeOf,
|
|
_age_seconds,
|
|
format_ago,
|
|
highlighted_hash,
|
|
node_type_legend,
|
|
path_text,
|
|
route_graph_style,
|
|
)
|
|
|
|
#: Friendly gloss for a raw packet's parsed payload class (mirrors the meshcore
|
|
#: library's own ``PAYLOAD_TYPENAMES``), shown on a ``packet`` entry's "class" row.
|
|
_PAYLOAD_GLOSS = {
|
|
"REQ": "request",
|
|
"RESPONSE": "response",
|
|
"TEXT_MSG": "direct message",
|
|
"ACK": "ack",
|
|
"ADVERT": "advert",
|
|
"GRP_TXT": "channel text",
|
|
"GRP_DATA": "channel data",
|
|
"ANON_REQ": "anonymous request",
|
|
"PATH": "path",
|
|
"TRACE": "trace",
|
|
"MULTIPART": "multipart",
|
|
"CONTROL": "control",
|
|
}
|
|
|
|
#: Raw-payload fields already folded into a flavoured row elsewhere (frame plumbing
|
|
#: parsed into "class"/"route", the channel crypto handled by
|
|
#: :meth:`PacketViewer._decrypt_rows`, or an advert field duplicating ``node``/
|
|
#: ``lat``/``lon``/etc.) — skipped so the generic dump doesn't repeat them.
|
|
_RAW_ROW_SKIP = frozenset({
|
|
"node", "name", "kind", "snr", "rssi", "lat", "lon", "path", "node_type",
|
|
"observed_at", "text",
|
|
"header", "payload_ver", "transport_code", "path_len", "path_hash_size",
|
|
"payload_type", "payload_typename", "route_type", "route_typename",
|
|
"pkt_payload", "pkt_hash", "raw_hex", "payload", "payload_length", "recv_time",
|
|
"chan_hash", "cipher_mac", "crypted", "message", "msg_hash", "sender_timestamp",
|
|
"attempt", "txt_type",
|
|
"adv_key", "adv_name", "adv_type", "adv_lat", "adv_lon",
|
|
})
|
|
|
|
#: Display style per packet class, shared by every packet list and the viewer.
|
|
KIND_STYLES = {
|
|
"advert": "accent",
|
|
"telemetry": "brand",
|
|
"packet": "muted",
|
|
"message": "ok",
|
|
"ack": "faint",
|
|
}
|
|
|
|
#: The two-cell icon per packet class — every icon is emoji-wide, so a list can swap
|
|
#: the textual kind label for the icon alone on a narrow terminal without the lanes
|
|
#: shifting. The fallback marks a class this table has never heard of.
|
|
KIND_ICONS = {
|
|
"advert": "📢",
|
|
"telemetry": "📊",
|
|
"packet": "📦",
|
|
"message": "💬",
|
|
"ack": "✅",
|
|
}
|
|
DEFAULT_ICON = "❔"
|
|
|
|
#: The two-cell icon per parsed payload class of a raw ``packet`` frame, shared by the
|
|
#: viewer's leading class row and the dashboard's traffic meters. One glyph per concept:
|
|
#: 📻 rhymes with the Channels tool, 🎯 with Trace, and a raw ADVERT/ACK frame reuses
|
|
#: its kind's icon; :data:`DEFAULT_ICON` marks a typename this table has never heard of.
|
|
PAYLOAD_ICONS = {
|
|
"REQ": "📥",
|
|
"RESPONSE": "📮",
|
|
"TEXT_MSG": "📩",
|
|
"ACK": "✅",
|
|
"ADVERT": "📢",
|
|
"GRP_TXT": "📻",
|
|
"GRP_DATA": "💽",
|
|
"ANON_REQ": "🎭",
|
|
"PATH": "🧭",
|
|
"TRACE": "🎯",
|
|
"MULTIPART": "🧩",
|
|
"CONTROL": "🧰",
|
|
}
|
|
|
|
#: The colour a relayed packet's route draws in on THE route graph. It is the only path
|
|
#: on the canvas, so white just reads as "the route the packet rode" (the Trophy case's
|
|
#: single-walk convention).
|
|
_ROUTE_EDGE = (255, 255, 255)
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class PacketEntry:
|
|
"""One listed packet, in the shape every packet list stores and the viewer reads.
|
|
|
|
A deliberately flat union of what the feed's three event families carry — the
|
|
monitor's observations, chat messages, and delivery acks — so one list type can
|
|
hold them all and the viewer can flavour by :attr:`kind`.
|
|
|
|
Attributes:
|
|
when: When the packet was heard.
|
|
kind: Packet class (``advert``/``telemetry``/``packet``/``message``/``ack``).
|
|
node: The transmitting node's stored hash, when identified.
|
|
name: The name the packet itself carried (an advert's node name, a message's
|
|
sender), if any; display falls back to resolving :attr:`node`.
|
|
snr: Reception SNR in dB, if measured (for ``packet`` rows: of the *last relay*).
|
|
rssi: Reception strength in dBm, if measured.
|
|
lat: Advertised latitude, when the packet shared a location.
|
|
lon: Advertised longitude, when the packet shared one.
|
|
node_type: The advertised node type (see ``NODE_TYPE_*``), if carried.
|
|
path: A ``packet`` row's relay path: comma-separated hop hashes in propagation
|
|
order (empty = arrived direct; ``None`` = the class carries no path).
|
|
where: A message's conversation (``ch 3`` / ``direct``) or an ack's code.
|
|
text: A message's body, when it is known.
|
|
raw: The raw event payload, for the values a class-specific layout can't name.
|
|
"""
|
|
|
|
when: datetime
|
|
kind: str
|
|
node: Optional[str] = None
|
|
name: Optional[str] = None
|
|
snr: Optional[float] = None
|
|
rssi: Optional[float] = None
|
|
lat: Optional[float] = None
|
|
lon: Optional[float] = None
|
|
node_type: Optional[int] = None
|
|
path: Optional[str] = None
|
|
where: Optional[str] = None
|
|
text: Optional[str] = None
|
|
raw: Optional[dict] = None
|
|
|
|
@classmethod
|
|
def from_observation(cls, obs: Observation) -> "PacketEntry":
|
|
"""Capture an overheard observation (the monitor's event family) as an entry."""
|
|
return cls(
|
|
when=obs.observed_at,
|
|
kind=obs.kind,
|
|
node=obs.node,
|
|
name=obs.name,
|
|
snr=obs.snr,
|
|
rssi=obs.rssi,
|
|
lat=obs.lat,
|
|
lon=obs.lon,
|
|
node_type=obs.node_type,
|
|
path=obs.path,
|
|
raw=obs.raw,
|
|
)
|
|
|
|
|
|
def kind_icon(kind: str) -> str:
|
|
"""The two-cell icon for a packet class (a fallback for unknown classes)."""
|
|
return KIND_ICONS.get(kind, DEFAULT_ICON)
|
|
|
|
|
|
def payload_class(raw: Optional[dict]) -> Optional[str]:
|
|
"""The friendly payload-class label for a raw ``packet`` frame, or ``None`` if unknown.
|
|
|
|
Maps the frame's ``payload_typename`` (``GRP_TXT``, ``TRACE``, …) through
|
|
:data:`_PAYLOAD_GLOSS` — the one identifying thing a relayed flood carries when it
|
|
names no origin node, so a packet list can read "channel text" / "trace" instead of a
|
|
bare ``?``. Shared by the viewer's "class" row and the dashboard feed's node lane.
|
|
"""
|
|
if not isinstance(raw, dict):
|
|
return None
|
|
typename = raw.get("payload_typename")
|
|
if not typename:
|
|
return None
|
|
return _PAYLOAD_GLOSS.get(typename, typename.lower())
|
|
|
|
|
|
def class_chrome(entry: PacketEntry) -> tuple[str, str]:
|
|
"""The class headline every packet leads with: a two-cell icon and an UPPERCASE label.
|
|
|
|
A raw ``packet`` frame's class is its parsed payload class — ``CHANNEL TEXT``,
|
|
``TRACE``, … — under its :data:`PAYLOAD_ICONS` glyph; a typename this table has
|
|
never heard of keeps the fallback mark and shouts the raw typename; a class-less
|
|
frame stays a plain ``PACKET``. Every other kind is its own class (``ADVERT``,
|
|
``MESSAGE``, …) under the shared :data:`KIND_ICONS` glyph.
|
|
"""
|
|
if entry.kind == "packet":
|
|
raw = entry.raw if isinstance(entry.raw, dict) else {}
|
|
typename = raw.get("payload_typename")
|
|
if typename:
|
|
icon = PAYLOAD_ICONS.get(typename, DEFAULT_ICON)
|
|
label = _PAYLOAD_GLOSS.get(typename, typename.lower()).upper()
|
|
return icon, label
|
|
return KIND_ICONS["packet"], "PACKET"
|
|
return kind_icon(entry.kind), entry.kind.upper()
|
|
|
|
|
|
def node_label(
|
|
entry: PacketEntry, resolve: NodeResolver, self_name: Optional[str] = None
|
|
) -> tuple[str, str]:
|
|
"""The display name and style for an entry's node — the app-wide naming rule.
|
|
|
|
What ``resolve`` knows about the hash wins (the contact list is the canonical
|
|
identity), then the name the packet itself carried; a genuinely nameless node
|
|
shows its hash. Names take their stable palette hue
|
|
(:func:`~meshterm.ui.theme.name_style`) — except our own node, which is always
|
|
the pure-white ``you`` — and a bare hash stays muted, because colour is the
|
|
"this is a name" signal.
|
|
|
|
Args:
|
|
entry: The packet whose node to label.
|
|
resolve: Maps a node hash to a friendly name when known.
|
|
self_name: Our own node's name, to spot "us" (``None`` = never matches).
|
|
|
|
Returns:
|
|
``(label, style)``, ready to append to a row.
|
|
"""
|
|
name = None
|
|
if entry.node:
|
|
resolved = resolve(entry.node)
|
|
if resolved and resolved != entry.node:
|
|
name = resolved
|
|
if not name:
|
|
name = entry.name
|
|
if not name:
|
|
return (entry.node or "?", "muted")
|
|
if self_name and name == self_name:
|
|
return (name, "you")
|
|
return (name, name_style(name, entry.node))
|
|
|
|
|
|
class PacketViewer(Screen):
|
|
"""The floating packet dialog: one entry laid out in full, pageable over its list.
|
|
|
|
A read-only popup. The opener hands it the list *as rendered* (newest first) and
|
|
the index of the row the user opened; ``↑``/``↓`` then walk toward newer/older
|
|
packets — the same keys the opening list itself uses — Home/End jump to the
|
|
ends, PgUp/PgDn scroll a tall body, Esc closes. Opened over a single packet (a
|
|
one-entry list) the paging keys simply do nothing.
|
|
|
|
The dialog only ever grows (see
|
|
:attr:`~meshterm.ui.tui.screen.Screen.grow_only`): paging from a short packet to a
|
|
tall one enlarges the box, but paging back keeps it at that size — blank-padded
|
|
below the shorter body — rather than re-centering smaller, so the header the reader
|
|
is looking at never hops around as they page.
|
|
"""
|
|
|
|
grow_only = True
|
|
|
|
def __init__(
|
|
self,
|
|
entries: list[PacketEntry],
|
|
index: int,
|
|
*,
|
|
resolve: NodeResolver,
|
|
prefix_bytes: int = 0,
|
|
self_name: Optional[str] = None,
|
|
on_navigate: Optional[Callable[[PacketEntry], None]] = None,
|
|
channels: Sequence[tuple[str, bytes]] = (),
|
|
source: Optional[Callable[[], Sequence[PacketEntry]]] = None,
|
|
type_of: Optional[TypeOf] = None,
|
|
) -> None:
|
|
"""Open the viewer over a packet list.
|
|
|
|
Args:
|
|
entries: The list's packets, newest first — the opening snapshot.
|
|
index: Which entry to open on.
|
|
resolve: Maps a node hash to a friendly name when known.
|
|
prefix_bytes: Path-hash width to light in displayed hashes (0 = none).
|
|
self_name: Our own node's name, drawn white wherever it appears.
|
|
on_navigate: Called with the newly shown entry whenever paging moves the
|
|
view, so the opening list can walk its own highlight in step.
|
|
channels: The device's configured channels, as ``(name, secret)`` pairs —
|
|
tried against an overheard ``packet`` entry's channel-text frame (see
|
|
:func:`~meshterm.core.channels.decrypt_channel_text`), so a raw frame
|
|
the radio never decoded for us can still be read when we hold the key.
|
|
source: Optional zero-arg callable returning the list's *current* packets
|
|
(newest first). When given, the viewer re-reads it every repaint and
|
|
keypress and re-locates the packet being viewed by identity, so packets
|
|
that arrive while the dialog is open become reachable (``↑`` walks up
|
|
into them) instead of the view being frozen at its opening snapshot.
|
|
type_of: Maps a relay hash to its node type, so a relayed packet's route
|
|
graph marks a repeater ``▲`` (etc.) instead of a generic dot; ``None``
|
|
keeps the plain named-dot / unknown-ring fallback.
|
|
"""
|
|
super().__init__()
|
|
self._entries = list(entries)
|
|
self._index = max(0, min(index, len(self._entries) - 1))
|
|
self._resolve = resolve
|
|
self._prefix_bytes = prefix_bytes
|
|
self._self_name = self_name
|
|
self._on_navigate = on_navigate
|
|
self._channels = channels
|
|
self._source = source
|
|
self._type_of = type_of
|
|
#: The packet currently shown, tracked by identity so a live prepend to the
|
|
#: source (which shifts every index) never slides the view onto another packet.
|
|
self._current: Optional[PacketEntry] = (
|
|
self._entries[self._index] if self._entries else None
|
|
)
|
|
self._update_footer()
|
|
self._set_title()
|
|
|
|
def _update_footer(self) -> None:
|
|
"""Set the footer hint to match whether the list currently has more than one entry."""
|
|
if len(self._entries) > 1:
|
|
self.footer_hint = "↑↓ newer/older · PgUp/PgDn scroll · Home/End ends · Esc close"
|
|
else:
|
|
self.footer_hint = "Esc close"
|
|
|
|
def _sync(self) -> None:
|
|
"""Refresh the entries from the live source, re-finding the viewed packet by identity.
|
|
|
|
A no-op without a ``source``. Otherwise the list is re-read (picking up any
|
|
packets that arrived since the last paint) and the view stays on the *same*
|
|
packet object — its index simply moves as newer packets prepend ahead of it —
|
|
so paging keys measure against the current list and newly arrived packets sit
|
|
reachable above the one being read. A packet that has since aged out of the
|
|
list drops the view to the nearest surviving index.
|
|
"""
|
|
if self._source is None:
|
|
return
|
|
entries = list(self._source())
|
|
if not entries:
|
|
return
|
|
self._entries = entries
|
|
if self._current is not None:
|
|
for i, candidate in enumerate(entries):
|
|
if candidate is self._current:
|
|
self._index = i
|
|
break
|
|
else:
|
|
self._index = min(self._index, len(entries) - 1)
|
|
self._current = entries[self._index]
|
|
else:
|
|
self._index = min(self._index, len(entries) - 1)
|
|
self._current = entries[self._index]
|
|
self._update_footer()
|
|
self._set_title()
|
|
|
|
def _set_title(self) -> None:
|
|
# No emoji in a dialog title (the standards' rule); the class row carries the icon.
|
|
entry = self._entries[self._index]
|
|
title = entry.kind
|
|
if len(self._entries) > 1:
|
|
title += f" · {self._index + 1}/{len(self._entries)}"
|
|
self.title = title
|
|
|
|
# --- input ---------------------------------------------------------------
|
|
|
|
def handle(self, action: str, data: str = "") -> None:
|
|
"""Page through the list, scroll a tall entry, or dismiss."""
|
|
if action in ("up", "down", "home", "ctrl_home", "end", "ctrl_end"):
|
|
# Re-read the live list first so newly arrived packets are in range before we
|
|
# step — otherwise the first ``↑`` off the newest could never reach them.
|
|
self._sync()
|
|
if action == "up":
|
|
self._jump(self._index - 1)
|
|
elif action == "down":
|
|
self._jump(self._index + 1)
|
|
elif action in ("home", "ctrl_home"):
|
|
self._jump(0)
|
|
elif action in ("end", "ctrl_end"):
|
|
self._jump(len(self._entries) - 1)
|
|
elif action == "pageup":
|
|
self.scroll_pages(-1)
|
|
elif action in ("pagedown", "space"):
|
|
self.scroll_pages(1)
|
|
elif action in ("escape", "enter"):
|
|
self.resolve(None)
|
|
|
|
def _jump(self, index: int) -> None:
|
|
"""Move to another entry (clamped), resetting the scroll for the new body."""
|
|
index = max(0, min(index, len(self._entries) - 1))
|
|
if index != self._index:
|
|
self._index = index
|
|
self._current = self._entries[index]
|
|
self.scroll = 0
|
|
self._set_title()
|
|
if self._on_navigate is not None:
|
|
self._on_navigate(self._current)
|
|
|
|
# --- rendering -------------------------------------------------------------
|
|
|
|
def render_body(self, width: int) -> list[str]:
|
|
"""Lay the current entry out as labelled rows, flavoured by its kind.
|
|
|
|
A relayed ``packet`` slots THE route graph (:mod:`~meshterm.ui.pathgraph`) between
|
|
its parsed detail (heard/from/class/via) and its extras (location/text/raw): the
|
|
via row names each hop precisely, the graph shows the same relay chain as a shape —
|
|
origin at the left, us at the right. The two label/value grids around it share one
|
|
label-lane width so their rows still line up across the picture.
|
|
"""
|
|
# Fold in any packets that arrived since the last paint, keeping the view on the
|
|
# same packet, so the title's ``n/total`` and the reachable range stay current.
|
|
self._sync()
|
|
entry = self._entries[self._index]
|
|
head, tail = self._head_rows(entry), self._tail_rows(entry)
|
|
label_w = self._label_width(head + tail, width)
|
|
|
|
lines = self._grid_lines(head, width, label_w)
|
|
graph = self._graph_lines(entry, width)
|
|
if graph:
|
|
lines.append("")
|
|
lines.extend(graph)
|
|
caption = Text("origin → you · labels = hash byte", style="faint")
|
|
lines.append(render_to_ansi(caption, width, no_wrap=True))
|
|
lines.append(render_to_ansi(node_type_legend(), width, no_wrap=True))
|
|
if tail:
|
|
lines.append("")
|
|
lines.extend(self._grid_lines(tail, width, label_w))
|
|
self._scroll_total = max(1, len(lines))
|
|
return lines
|
|
|
|
@staticmethod
|
|
def _label_width(rows: list[tuple[str, RenderableType]], width: int) -> int:
|
|
"""The shared label-lane width so wrapped values hang under their own block.
|
|
|
|
One fixed width for every grid on the card (the app-wide hanging-indent rule), never
|
|
column zero — sized to the widest label present (a raw payload field's full name
|
|
included, so none is clipped) plus a one-cell gutter, but never so wide it leaves the
|
|
value column under 20 cells (a pathological key then ellipsises instead of crushing it).
|
|
"""
|
|
widest = max((len(label) for label, _ in rows), default=0)
|
|
return max(10, min(widest + 1, width - 20))
|
|
|
|
@staticmethod
|
|
def _grid_lines(
|
|
rows: list[tuple[str, RenderableType]], width: int, label_w: int
|
|
) -> list[str]:
|
|
"""Render one label/value grid to ANSI lines (empty rows → no lines)."""
|
|
if not rows:
|
|
return []
|
|
grid = Table(
|
|
box=None, show_header=False, show_edge=False, pad_edge=False,
|
|
padding=(0, 0), expand=False,
|
|
)
|
|
grid.add_column(width=label_w, no_wrap=True)
|
|
grid.add_column(overflow="fold", max_width=max(20, width - label_w))
|
|
for label, value in rows:
|
|
grid.add_row(Text(label, style="muted"), value)
|
|
return render_lines(grid, width)
|
|
|
|
def _head_rows(self, entry: PacketEntry) -> list[tuple[str, RenderableType]]:
|
|
"""The rows above the route graph: the class headline, the common core, then a
|
|
packet's parsed frame."""
|
|
rows: list[tuple[str, RenderableType]] = []
|
|
|
|
icon, class_label = class_chrome(entry)
|
|
rows.append(("class", Text(f"{icon} {class_label}")))
|
|
|
|
secs = _age_seconds(entry.when)
|
|
heard = Text(entry.when.astimezone().strftime("%b %d %H:%M:%S"))
|
|
heard.append(f" ({format_ago(secs)})", style="muted")
|
|
rows.append(("heard", heard))
|
|
|
|
if entry.node or entry.name:
|
|
who = Text()
|
|
label, style = node_label(entry, self._resolve, self._self_name)
|
|
who.append(label, style=style)
|
|
if entry.node and label != entry.node:
|
|
who.append(" ")
|
|
who.append_text(highlighted_hash(entry.node, self._prefix_bytes))
|
|
rows.append(("from", who))
|
|
if entry.node_type is not None:
|
|
label = NODE_TYPE_LABELS.get(entry.node_type, f"type {entry.node_type}")
|
|
rows.append(("type", Text(label)))
|
|
|
|
if entry.snr is not None or entry.rssi is not None:
|
|
rows.append(("snr", self._reception(entry)))
|
|
if entry.kind == "packet":
|
|
rows.extend(self._packet_rows(entry))
|
|
return rows
|
|
|
|
def _tail_rows(self, entry: PacketEntry) -> list[tuple[str, RenderableType]]:
|
|
"""The rows below the route graph: location, the message/ack fields, the raw dump."""
|
|
rows: list[tuple[str, RenderableType]] = []
|
|
if entry.lat is not None and entry.lon is not None:
|
|
rows.append(("location", Text(f"{entry.lat:.5f}, {entry.lon:.5f}")))
|
|
|
|
if entry.where:
|
|
label = "code" if entry.kind == "ack" else "where"
|
|
rows.append((label, Text(entry.where)))
|
|
if entry.text:
|
|
rows.append(("text", Text(entry.text)))
|
|
|
|
rows.extend(self._raw_rows(entry))
|
|
return rows
|
|
|
|
def _graph_lines(self, entry: PacketEntry, width: int) -> list[str]:
|
|
"""Draw a relayed packet's origin → relays → us route on THE route graph.
|
|
|
|
Only a ``packet`` frame that actually crossed a relay draws one — a direct
|
|
arrival's ``via`` row (``direct — no relays``) already says all there is to say, so
|
|
a two-node graph would waste the height. The relay chain is the packet's path
|
|
(originator-nearest first), the left endpoint the origin when the frame named one
|
|
(an advert's ``adv_key``; other classes arrive origin-less, drawn ``?``), the right
|
|
endpoint always us — the same fan-lane widget the Message paths dialog and the
|
|
Trophy case draw, in a single white path.
|
|
"""
|
|
if entry.kind != "packet":
|
|
return []
|
|
hops = tuple(hop for hop in (entry.path or "").split(",") if hop)
|
|
if not hops:
|
|
return []
|
|
glyph_of, label_of, label_rgb_of = route_graph_style(
|
|
resolve=self._resolve, self_name=self._self_name,
|
|
source=self._graph_source(entry), type_of=self._type_of,
|
|
)
|
|
return render_path_graph(
|
|
[PathLayer(hops=hops, color=_ROUTE_EDGE, priority=3)],
|
|
width, glyph_of=glyph_of, label_of=label_of, label_rgb_of=label_rgb_of,
|
|
)
|
|
|
|
def _graph_source(self, entry: PacketEntry) -> Optional[str]:
|
|
"""The origin's display name for the graph's left endpoint, or ``None`` if unknown.
|
|
|
|
The naming rule the ``from`` row follows (a resolved contact wins, then the name the
|
|
frame carried), but a nameless origin returns ``None`` so the graph draws a plain
|
|
``?`` endpoint rather than passing a bare hash off as a named node.
|
|
"""
|
|
if entry.node:
|
|
resolved = self._resolve(entry.node)
|
|
if resolved and resolved != entry.node:
|
|
return resolved
|
|
return entry.name or None
|
|
|
|
def _reception(self, entry: PacketEntry) -> Text:
|
|
"""SNR (with the shared quality bar) and RSSI on one line."""
|
|
line = Text()
|
|
if entry.snr is not None:
|
|
line.append(f"{entry.snr:+.1f} dB ", style=snr_style(entry.snr))
|
|
line.append_text(snr_bar(entry.snr))
|
|
if entry.rssi is not None:
|
|
if entry.snr is not None:
|
|
line.append(" · ", style="muted")
|
|
line.append(f"{entry.rssi:.0f} dBm rssi", style="muted")
|
|
return line
|
|
|
|
def _path_text(self, entry: PacketEntry) -> Text:
|
|
"""A ``packet`` entry's relay path, rendered by the shared compact path widget."""
|
|
return path_text(
|
|
(entry.path or "").split(","),
|
|
self._resolve,
|
|
prefix_bytes=self._prefix_bytes,
|
|
self_name=self._self_name,
|
|
empty="direct — no relays",
|
|
)
|
|
|
|
def _packet_rows(self, entry: PacketEntry) -> list[tuple[str, RenderableType]]:
|
|
"""A raw ``packet`` entry's parsed class/route, relay path, and — for a
|
|
channel-text frame naming a channel we hold the key for — its plaintext.
|
|
"""
|
|
raw = entry.raw if isinstance(entry.raw, dict) else {}
|
|
rows: list[tuple[str, RenderableType]] = []
|
|
typename = raw.get("payload_typename") # class itself leads the card (class_chrome)
|
|
route = raw.get("route_typename")
|
|
if route:
|
|
rows.append(("route", Text(route.replace("_", " ").lower())))
|
|
rows.append(("via", self._path_text(entry)))
|
|
rows.append((
|
|
"", Text("reception describes the last relay, not the origin",
|
|
style="faint"),
|
|
))
|
|
if typename == "GRP_TXT":
|
|
rows.extend(self._decrypt_rows(raw))
|
|
return rows
|
|
|
|
def _decrypt_rows(self, raw: dict) -> list[tuple[str, RenderableType]]:
|
|
"""Try every known channel's key against an overheard channel-text frame.
|
|
|
|
The frame names its channel only by a one-byte hash fingerprint (several
|
|
channels can collide on it), so :func:`~meshterm.core.channels.
|
|
decrypt_channel_text` confirms the match by MAC before trusting a key to
|
|
decrypt anything — a channel we don't hold the key for, or a fingerprint we
|
|
can't confirm, is reported as such rather than left silently missing.
|
|
"""
|
|
chan_hash = raw.get("chan_hash")
|
|
cipher_mac = raw.get("cipher_mac")
|
|
crypted = raw.get("crypted")
|
|
if not (chan_hash and cipher_mac and crypted):
|
|
return []
|
|
decrypted = decrypt_channel_text(chan_hash, cipher_mac, crypted, self._channels)
|
|
if decrypted is None:
|
|
return [(
|
|
"channel",
|
|
Text(f"unknown (hash {chan_hash}) — can't decrypt", style="muted"),
|
|
)]
|
|
rows: list[tuple[str, RenderableType]] = [
|
|
("channel", Text(decrypted.channel_name, style="brand")),
|
|
]
|
|
body = Text(decrypted.text)
|
|
if decrypted.sent_at is not None:
|
|
body.append(
|
|
f" · sent {decrypted.sent_at.astimezone().strftime('%H:%M:%S')}",
|
|
style="muted",
|
|
)
|
|
if decrypted.attempt:
|
|
body.append(f" (resend #{decrypted.attempt})", style="muted")
|
|
rows.append(("text", body))
|
|
return rows
|
|
|
|
def _raw_rows(self, entry: PacketEntry) -> list[tuple[str, RenderableType]]:
|
|
"""The raw payload's leftover fields, one labelled row each (telemetry's meat).
|
|
|
|
Fields the flavoured layout already presents are skipped; the rest render as
|
|
flat ``key value`` rows so a telemetry frame's channels (or a new firmware
|
|
field the layout doesn't know yet) are still visible without a debugger.
|
|
"""
|
|
raw = entry.raw if isinstance(entry.raw, dict) else None
|
|
if not raw:
|
|
return []
|
|
shown = {k: v for k, v in raw.items() if k not in _RAW_ROW_SKIP and v is not None}
|
|
if not shown:
|
|
return []
|
|
rows: list[tuple[str, RenderableType]] = [("", Text())]
|
|
rows.append(("payload", Text(f"{len(shown)} raw fields", style="faint")))
|
|
for key, value in list(shown.items())[:12]:
|
|
body = str(value)
|
|
if len(body) > 200:
|
|
body = body[:199] + "…"
|
|
# The label lane widens to fit the full key (see :meth:`render_body`), so the
|
|
# field name is shown whole rather than clipped; the two-space indent nests it
|
|
# visually under the "payload" heading above.
|
|
rows.append((f" {key}", Text(body, style="muted")))
|
|
return rows
|