Files
DigiRadio/Software/components/net/include/net/NetBootstrap.hpp
T
micheleandClaude Sonnet 5 f610121fd3 Add FM ANTCAP antenna calibration: HTTP sweep control + EEPROM persistence
Confirmed live tonight that the Si4684's automatic front-end tuning
(FE_VARM/VARB properties) is measurably suboptimal on this board: a
129-value ANTCAP sweep (0-128, AN851 Appendix A) at four different FM
frequencies found antcap=102 beats auto-tune by +6 to +11 dB RSSI and
+3 to +11 dB SNR everywhere tested, consistent with the front-end
network component mismatch already logged in
docs/si4684-rf-investigation-report.md — the board's actual matching
network differs from the AN851 reference network those auto-tune
constants were derived from, so a fixed empirical override
compensates for a gap the chip's own algorithm can't see.

Wiring, bottom to top:
- Si4684Driver::tuneFm() already took an antCap byte; threaded it
  through core::ITuner::tuneFm() and si4684::Si4684Tuner::tuneFm() as
  a new parameter (default 0 = auto, unchanged behaviour for every
  existing caller).
- TunerService::tuneFm() takes an optional override instead: omitted,
  it falls back to a new defaultFmAntCap_ member so every ordinary FM
  tune (seek, scan, station recall, live UI) benefits automatically
  once calibrated, not just calls that pass antcap explicitly.
- POST /api/tuner/tune gained an optional "antcap" field for sweeping
  live without touching the saved calibration.
- POST /api/tuner/calibrate-antenna commits a sweep result: writes it
  to the 24AA025E48 EEPROM's user-writable region (word address 0x00,
  separate from the factory-locked EUI-48 at 0xFA-0xFF) via a new
  Eeprom24aa::writeFmAntCap()/readFmAntCap() pair, and immediately
  updates the live TunerService default — no reboot needed to take
  effect, though HardwareBootstrap::boot() also loads it at every
  boot so it survives power cycles. Same net::AntennaCalibration
  function-pointer bridge pattern as PhoneStreamSink/BleProvisioning,
  so components/net stays free of eeprom24aa headers.

Verified end to end on hardware: swept and found 102, saved it via
the new endpoint, confirmed the live default changed immediately,
then power-cycled and confirmed the boot log reports "FM ANTCAP
calibration loaded: 102" and a subsequent default tune reflects it.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0178rASQ6ZETPMUamvpoR2KR
2026-08-19 02:01:09 +02:00

189 lines
5.9 KiB
C++

/**
* @file NetBootstrap.hpp
* @brief Owns network resources for setup or STA mode for app lifetime.
*
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
*
* Copyright 2026 Michele Bigi
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* @author Michele Bigi
* @date 2026-07-06
*/
#pragma once
#include "core/CompanionChipStatus.hpp"
#include "core/DeviceIdentity.hpp"
#include "core/ISecureStore.hpp"
#include "net/NetError.hpp"
#include "net/NetState.hpp"
#include "net/SetupWebServer.hpp"
#include "net/SigmaStudioTcpServer.hpp"
#include "net/SoftApHost.hpp"
#include "net/StaClient.hpp"
#include <expected>
#include <optional>
namespace audio {
class AudioService;
} // namespace audio
namespace bluetooth {
class BluetoothService;
} // namespace bluetooth
namespace station {
class StationService;
} // namespace station
namespace integration {
class IntegrationService;
} // namespace integration
namespace ota {
class OtaService;
} // namespace ota
namespace tuner {
class TunerService;
} // namespace tuner
namespace webradio {
class WebRadioService;
} // namespace webradio
namespace net {
/**
* @brief NetBootstrap — brings up SoftAP or STA plus the HTTP server.
*
* @dname NetBootstrap
* @return n/a (type)
* @pubstate Owns optional softAp_, optional sta_, webServer_, and
* sigmaStudio_. Must outlive app_main; keep one instance alive
* for process lifetime.
*
* @author Michele Bigi
* @date 2026-07-06
*/
class NetBootstrap {
public:
/**
* @brief start — init platform and join stored Wi-Fi or open SoftAP.
*
* @dname start
* @param store Secure store consulted for saved STA credentials.
* @param tuner Tuner service exposed by the HTTP API.
* @param audio Audio service exposed by the HTTP API.
* @param bluetooth Bluetooth pairing service for REST routes.
* @param stations Station preset service for REST routes.
* @param integration Application orchestration for preset recall.
* @param ota Firmware OTA service for POST /api/system/ota.
* @param webRadio Streaming config for GET/POST /api/streaming.
* @param phoneStream I2S write-through for PUT /api/stream/phone.
* @param antennaCalibration EEPROM write-through for
* POST /api/tuner/calibrate-antenna.
* @param companionChips Boot flags exposed on GET /api/health.
* @param deviceIdentity EEPROM-derived SSID, hostname, and serial.
* @return NetBootstrap on success, or a NetError.
* @pubstate none
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[nodiscard]] static std::expected<NetBootstrap, NetError>
start(core::ISecureStore& store, tuner::TunerService& tuner,
audio::AudioService& audio, bluetooth::BluetoothService& bluetooth,
station::StationService& stations,
integration::IntegrationService& integration,
ota::OtaService& ota,
webradio::WebRadioService& webRadio,
PhoneStreamSink& phoneStream,
AntennaCalibration& antennaCalibration,
core::CompanionChipStatus companionChips,
const core::DeviceIdentity& deviceIdentity);
NetBootstrap(const NetBootstrap&) = delete;
NetBootstrap& operator=(const NetBootstrap&) = delete;
/**
* @brief NetBootstrap — construct a started bootstrap.
*
* @dname NetBootstrap
* @param softAp Optional SoftAP mode host.
* @param sta Optional STA client instance.
* @param webServer HTTP server instance.
* @param sigmaStudio SigmaStudio TCP:8086 bridge instance.
* @param state Initial network state.
* @pubstate Transfers ownership of optional network resources.
*/
NetBootstrap(std::optional<SoftApHost> softAp,
std::optional<StaClient> sta,
SetupWebServer webServer,
SigmaStudioTcpServer sigmaStudio,
NetState state);
/**
* @brief NetBootstrap — move-construct from a started bootstrap.
*
* @dname NetBootstrap
* @param other Source instance; left empty after the move.
* @pubstate transfers softAp_, sta_, webServer_, and sigmaStudio_ from other.
*
* @author Michele Bigi
* @date 2026-07-06
*/
NetBootstrap(NetBootstrap&& other) noexcept = default;
/**
* @brief operator= — move-assign from a started bootstrap.
*
* @dname operator=
* @param other Source instance; left empty after the move.
* @return Reference to this instance.
* @pubstate transfers softAp_, sta_, webServer_, and sigmaStudio_ from other.
*
* @author Michele Bigi
* @date 2026-07-06
*/
NetBootstrap& operator=(NetBootstrap&& other) noexcept = default;
/**
* @brief ~NetBootstrap — tear down network subsystems.
*
* @dname ~NetBootstrap
* @pubstate destroys softAp_, sta_, webServer_, and sigmaStudio_.
*
* @author Michele Bigi
* @date 2026-07-06
*/
~NetBootstrap() = default;
/**
* @brief state — read the active network phase.
*
* @dname state
* @return Current NetState.
* @pubstate reads state_.
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[nodiscard]] NetState state() const noexcept;
private:
std::optional<SoftApHost> softAp_;
std::optional<StaClient> sta_;
SetupWebServer webServer_;
SigmaStudioTcpServer sigmaStudio_;
NetState state_;
};
} // namespace net