2.3 KiB
Refactor Notes: main.cpp Split
Architecture
The firmware remains a unified image. Role selection is still done at boot in src/main.cpp using the role pin. src/main.cpp is now a thin coordinator that:
- detects role and initializes shared platform subsystems,
- creates role module config,
- calls
begin()once, - delegates runtime via
loop().
Sender Logic
Sender-only runtime now lives in:
src/sender_state_machine.hsrc/sender_state_machine.cpp
Public API:
SenderStateMachineConfigSenderStatsSenderStateMachine::begin(...)SenderStateMachine::loop()SenderStateMachine::stats()
Sender state machine exposes explicit states (Syncing, Normal, Catchup, WaitAck) and keeps sender flow isolated from top-level orchestration.
Sender helpers:
handleMeterRead(...)maybeSendBatch(...)handleAckWindow(...)applyTimeFromAck(...)validateInvariants()
Sender invariants are checked and debug-traceable (DD3_DEBUG) for:
- single inflight batch at a time,
- ACK acceptance only for matching
batch_id, - retry bounded by
BATCH_MAX_RETRIES, - queue depth bounded by
BATCH_QUEUE_DEPTH.
Receiver Logic
Receiver-only runtime now lives in:
src/receiver_pipeline.hsrc/receiver_pipeline.cpp
Shared receiver arrays and fault/discovery state used by setup wiring and runtime are held in:
src/app_context.h(ReceiverSharedState)
Public API:
ReceiverPipelineConfigReceiverStatsReceiverPipeline::begin(...)ReceiverPipeline::loop()ReceiverPipeline::stats()
Receiver pipeline owns LoRa RX processing, reassembly/decode, ACK send path, MQTT/SD/display updates, and receiver fault/discovery publication flow.
HA Manufacturer Single Source
include/config.h defines the canonical manufacturer:
HA_MANUFACTURER = "AcidBurns"
Home Assistant discovery payload generation in src/mqtt_client.cpp uses this constant directly (device["manufacturer"] = HA_MANUFACTURER).
Drift guards:
- compile-time lock in
include/config.hviastatic_assert, test/check_ha_manufacturer.ps1validates discovery assignment and rejects stray hardcoded manufacturer literals insrc/andinclude/,- smoke test
test/test_refactor_smoke/test_refactor_smoke.cppasserts the constant value and module header compatibility.