Skip to content

refactor: extract shared utilities from duplicated MeshCore interface code - #4

Open
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1783039049-refactor-shared-utilities
Open

devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1783039049-refactor-shared-utilities

Conversation

@devin-ai-integration

Copy link
Copy Markdown

Summary

MeshCore_Channel_Interface and MeshCore_Dynamic_Interface contained substantial duplicated logic. This PR extracts 10 shared utilities into a new Interface/_meshcore_shared.py module and rewires both interfaces to use them.

Extracted utilities:

Function / Class What it replaces
load_meshcore(interface_name) Identical _load_meshcore_or_panic import+log pattern in both files
start_asyncio_loop_thread(name, iname) Identical _run_loop + threading.Thread boilerplate in both __init__
create_meshcore_connection(mc, transport, ...) Duplicated serial/ble/tcp if/elif/else creation blocks (~30 lines each)
configure_radio(mc, freq, bw, sf, cr, ...) Near-identical radio-parameter-override blocks
configure_channel(mc, idx, name, secret, ...) Near-identical channel setup blocks
process_incoming(interface, data) Identical 4-line processIncoming body (rxb += len; owner.inbound)
decode_tunnel_frame(text, prefix, ...) Duplicated RNS: prefix-strip + base64url decode in _process_tunnel_text
reassemble_fragment(asm, meta, lock, key, ...) Core fragment-reassembly logic (~25 lines each, same dict/lock pattern)
cleanup_stale_fragments(asm, meta, lock, ...) Identical stale-assembly eviction in _cleanup_loop
PacketIdCounter(bits) Replaces _pkt_id + _pkt_id_lock + manual & 0xFF / & 0xFFFFFFFF in both files

What stays in each interface:

  • _PacketHandler classes (different wire formats: 9-byte vs 6-byte headers)
  • Deduplication strategies (OrderedDict LRU in Channel vs sliding-window timestamps in Dynamic)
  • Peer discovery / RNSBIND protocol (Dynamic only)
  • RemoteTerm WebSocket support (Channel only)
  • Rate limiting and RNS header parsing (Dynamic only)

Import mechanism for Reticulum's exec()-based loading:

try:
    _iface_dir = os.path.dirname(os.path.abspath(__file__))
except NameError:
    _iface_dir = os.path.expanduser("~/.reticulum/interfaces")
if _iface_dir not in sys.path:
    sys.path.insert(0, _iface_dir)

MeshCore_Interface.py is untouched — its older architecture (custom _safe_log, different fragmentation format, PacketHandler with Bb struct) is different enough that forcing shared code would reduce clarity.

Net diff: Channel −141 lines, Dynamic −37 lines, shared module +286 lines. The value is in eliminating duplicated patterns that would otherwise drift independently.

Link to Devin session: https://app.devin.ai/sessions/788f2435d2354a17b1474fcb1a261495
Requested by: @comms-engineer

… code

Create _meshcore_shared.py containing common logic that was duplicated
between MeshCore_Channel_Interface and MeshCore_Dynamic_Interface:

- load_meshcore(): meshcore library import with error handling
- start_asyncio_loop_thread(): event loop thread creation
- create_meshcore_connection(): serial/ble/tcp transport init
- configure_radio() / configure_channel(): radio+channel setup
- process_incoming(): standard RNS inbound delivery
- decode_tunnel_frame(): sender-prefix stripping + base64 decode
- reassemble_fragment(): thread-safe fragment reassembly
- cleanup_stale_fragments(): timed fragment eviction
- PacketIdCounter: thread-safe rolling packet ID counter

MeshCore_Interface.py is left unchanged (architecturally different,
older fragmentation format, different threading model).

Import mechanism uses sys.path with __file__ fallback to handle
Reticulum's exec()-based interface file loading.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@comms-engineer comms-engineer self-assigned this Jul 3, 2026
@devin-ai-integration

Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant