Add internet radio streaming with runtime API, modernize web UI, remove auto-tune/beep at boot

Streaming (main feature this session):
- New WebRadioConfig/WebRadioJson core types, ISecureStore-backed persistence
- New webradio::WebRadioService (thread-safe live config) + GET/POST /api/streaming
- web_radio_stream task now runtime-toggleable (no reboot), no hardcoded URL
- Content-Type diagnostic: warns clearly when a URL is a webpage, not an audio stream

Boot cleanup:
- Removed boot-time auto FM/DAB tune, auto-beep, and the (now-concluded) Si4684
  crystal IBIAS/CTUN empirical sweep from main.cpp — tuning/beep are on-demand
  via the existing REST API only

Web UI:
- Modernized styling (cards, gradients, toggle switches, light/dark theme)
- New Stream tab wired to /api/streaming

Fixes found via real idf.py build (not just clangd):
- Restored wrongly-removed si4684/Si4684Tuner.hpp include in main.cpp
- Fixed MP3Decode() argument types in web_radio_stream.cpp (unsigned char**/int*)

Quality-gate fixes:
- Host-test stub headers (esp_log.h, freertos/*) so TunerService.cpp's
  scanForStation logging/pacing compiles for station_service_test /
  integration_service_test instead of running stale binaries
- Added WifiScanner and WebRadioService manual sections; filled in missing
  Doxygen docs on BluetoothService, i2s_sdata_probe, test_firmware, Bt1035At
- Ignore clangd's .cache/ index directory

Also includes prior uncommitted work carried in the tree: Wi-Fi/Bluetooth
device scan REST API and UI (WifiScanner, BT scan), SigmaStudio TCP bridge,
and the current ADAU1701 SigmaStudio DSP program export.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-08 21:15:22 +02:00
co-authored by Claude Sonnet 5
parent 8a8523515d
commit 6f7b6dd12c
131 changed files with 20309 additions and 1702 deletions
@@ -2,7 +2,7 @@ idf_component_register(
SRCS
"src/BluetoothService.cpp"
INCLUDE_DIRS "include"
REQUIRES core bt1035
REQUIRES core bt1035 freertos
)
target_compile_features(${COMPONENT_LIB} PUBLIC cxx_std_23)
@@ -15,6 +15,9 @@
#include "bt1035/Bt1035Driver.hpp"
#include "bt1035/Bt1035Error.hpp"
#include "core/BluetoothJson.hpp"
#include "core/BtSpeakerTarget.hpp"
#include "core/ISecureStore.hpp"
#include "core/StoreError.hpp"
#include <expected>
#include <vector>
@@ -22,12 +25,11 @@
namespace bluetooth {
/**
* @brief BluetoothService — pairing and A2DP status for the web API.
* @brief BluetoothService — pairing, scan, and saved-speaker reconnect.
*
* @dname BluetoothService
* @return n/a (type)
* @pubstate Borrows bt1035::Bt1035Driver for the process lifetime. Tracks
* whether discoverable mode was requested via startPairing().
* @pubstate Borrows bt1035::Bt1035Driver and core::ISecureStore for life.
*
* @author Michele Bigi
* @date 2026-07-06
@@ -35,96 +37,173 @@ namespace bluetooth {
class BluetoothService {
public:
/**
* @brief BluetoothService — bind to the BT1035 driver.
* @brief BluetoothService — bind driver and store for the process life.
*
* @dname BluetoothService
* @param driver Booted BT1035 driver (must outlive this service).
* @pubstate stores driver reference; pairing inactive initially.
*
* @author Michele Bigi
* @date 2026-07-06
* @param driver BT1035 driver (must outlive this service).
* @param store Secure store for the saved default speaker.
* @pubstate initialises pairingActive_ to false.
*/
explicit BluetoothService(bt1035::Bt1035Driver& driver);
explicit BluetoothService(bt1035::Bt1035Driver& driver,
core::ISecureStore& store);
/**
* @brief refreshStatus — read module boot, pairing, and A2DP state.
* @brief refreshStatus — read boot/pairing/name/reconnect/A2DP state.
*
* @dname refreshStatus
* @return BluetoothStatus on success, or Bt1035Error from the driver.
* @pubstate queries driver for A2DP state when booted.
*
* @author Michele Bigi
* @date 2026-07-06
* @return BluetoothStatus (booted=false if the module never booted),
* or a Bt1035Error from the first failing query.
* @pubstate queries driver_; reads pairingActive_.
*/
[[nodiscard]] std::expected<core::BluetoothStatus, bt1035::Bt1035Error>
refreshStatus();
/**
* @brief startPairing — enter discoverable mode (AT+PAIR=1).
* @brief startPairing — enter BT1035 discoverable mode.
*
* @dname startPairing
* @return Ok on success, or Bt1035Error.
* @pubstate sets pairingActive_ on success.
*
* @author Michele Bigi
* @date 2026-07-06
* @return Ok on success, or a Bt1035Error.
* @pubstate sets pairingActive_ true on success.
*/
[[nodiscard]] std::expected<void, bt1035::Bt1035Error> startPairing();
/**
* @brief stopPairing — leave discoverable mode (AT+PAIR=0).
* @brief stopPairing — leave BT1035 discoverable mode.
*
* @dname stopPairing
* @return Ok on success, or Bt1035Error.
* @pubstate clears pairingActive_ on success.
*
* @author Michele Bigi
* @date 2026-07-06
* @return Ok on success, or a Bt1035Error.
* @pubstate sets pairingActive_ false on success.
*/
[[nodiscard]] std::expected<void, bt1035::Bt1035Error> stopPairing();
/**
* @brief disconnect — release the active A2DP link (AT+A2DPDISC).
* @brief disconnect — tear down the current A2DP link.
*
* @dname disconnect
* @return Ok on success, or Bt1035Error.
* @pubstate delegates to driver.
*
* @author Michele Bigi
* @date 2026-07-06
* @return Ok on success, or a Bt1035Error.
* @pubstate none
*/
[[nodiscard]] std::expected<void, bt1035::Bt1035Error> disconnect();
/**
* @brief listPaired — read paired remotes from the module.
* @brief listPaired — read the BT1035's paired-device list.
*
* @dname listPaired
* @return Device list on success, or Bt1035Error.
* @pubstate queries AT+PLIST via the driver.
*
* @author Michele Bigi
* @date 2026-07-07
* @return Paired devices, or a Bt1035Error.
* @pubstate none
*/
[[nodiscard]] std::expected<std::vector<core::Bt1035PairedDevice>,
bt1035::Bt1035Error>
listPaired();
/**
* @brief setAutoReconnect — configure power-on reconnect attempts.
* @brief setAutoReconnect — set the module's auto-reconnect attempts.
*
* @dname setAutoReconnect
* @param times 0 off, 115 per Feasycom manual.
* @return Ok on success, or Bt1035Error.
* @pubstate writes AT+AUTOCONN via the driver.
*
* @author Michele Bigi
* @date 2026-07-07
* @param times Retry count passed to the BT1035 firmware.
* @return Ok on success, or a Bt1035Error.
* @pubstate none
*/
[[nodiscard]] std::expected<void, bt1035::Bt1035Error> setAutoReconnect(
std::uint8_t times);
/**
* @brief scanNearby — classic BT/EDR discovery, cancelling pairing.
*
* @dname scanNearby
* @param scanSeconds Scan duration in seconds.
* @return Discovered devices, or a Bt1035Error.
* @pubstate leaves pairing mode and stops any active scan first; sets
* pairingActive_ false.
*/
[[nodiscard]] std::expected<std::vector<core::Bt1035ScannedDevice>,
bt1035::Bt1035Error>
scanNearby(std::uint8_t scanSeconds = 20U);
/**
* @brief connectTo — direct A2DPCONN to a MAC, wait for streaming.
*
* @dname connectTo
* @param mac Target BT1035 MAC (12 hex chars, no separators).
* @return Ok once A2DP reaches Streaming, or a Bt1035Error.
* @pubstate sets pairingActive_ false; blocks up to the connect and
* stream settle timeouts.
*/
[[nodiscard]] std::expected<void, bt1035::Bt1035Error> connectTo(
std::string_view mac);
/**
* @brief connectTo — connect by request, optionally saving the target.
*
* @dname connectTo
* @param request Validated MAC/name plus a save flag.
* @return Ok once A2DP reaches Streaming, or a Bt1035Error
* (UnexpectedResponse for an invalid MAC).
* @pubstate sets pairingActive_ false; persists via saveSpeaker() when
* request.save is true (a failed save does not fail connect).
*/
[[nodiscard]] std::expected<void, bt1035::Bt1035Error> connectTo(
const core::BluetoothConnectRequest& request);
/**
* @brief hasSavedSpeaker — check whether a default speaker is stored.
*
* @dname hasSavedSpeaker
* @return true when loadSavedSpeaker() would succeed.
* @pubstate reads store_.
*/
[[nodiscard]] bool hasSavedSpeaker() const;
/**
* @brief loadSavedSpeaker — read the stored default speaker.
*
* @dname loadSavedSpeaker
* @return BtSpeakerTarget on success, or a StoreError.
* @pubstate reads store_.
*/
[[nodiscard]] std::expected<core::BtSpeakerTarget, core::StoreError>
loadSavedSpeaker() const;
/**
* @brief saveSpeaker — persist the default A2DP speaker target.
*
* @dname saveSpeaker
* @param target MAC and optional display name.
* @return Ok on success, or a StoreError.
* @pubstate writes store_.
*/
[[nodiscard]] std::expected<void, core::StoreError> saveSpeaker(
const core::BtSpeakerTarget& target);
/**
* @brief clearSavedSpeaker — erase the stored default speaker.
*
* @dname clearSavedSpeaker
* @return Ok on success, or a StoreError.
* @pubstate writes store_.
*/
[[nodiscard]] std::expected<void, core::StoreError> clearSavedSpeaker();
/** @brief Launch background task: A2DPCONN saved MAC, scan fallback. */
void startupReconnect();
/**
* @brief reconnectSavedSpeaker — direct MAC connect, optional scan retry.
*
* @dname reconnectSavedSpeaker
* @param scanFallback Run AT+SCAN when direct connect fails.
* @return Ok when A2DP reaches Connected, or Bt1035Error.
* @pubstate may block tens of seconds when scanFallback is true.
*/
[[nodiscard]] std::expected<void, bt1035::Bt1035Error> reconnectSavedSpeaker(
bool scanFallback);
private:
[[nodiscard]] std::expected<void, bt1035::Bt1035Error> connectDirect(
std::string_view mac);
bt1035::Bt1035Driver& driver_;
core::ISecureStore& store_;
bool pairingActive_;
};
@@ -13,10 +13,67 @@
#include "bluetooth/BluetoothService.hpp"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
namespace bluetooth {
BluetoothService::BluetoothService(bt1035::Bt1035Driver& driver)
namespace {
constexpr char kTag[] = "BluetoothSvc";
constexpr int kConnectSettleMs = 30000;
constexpr int kStreamSettleMs = 20000;
constexpr int kAutoReconnectWaitMs = 45000;
[[nodiscard]] bool isA2dpLinked(core::Bt1035A2dpState state) noexcept
{
using core::Bt1035A2dpState;
return state == Bt1035A2dpState::Connected
|| state == Bt1035A2dpState::Streaming
|| state == Bt1035A2dpState::Paused;
}
[[nodiscard]] bool isA2dpLinked(bt1035::Bt1035Driver& driver)
{
if (auto state = driver.queryA2dpState(); state) {
return isA2dpLinked(*state);
}
return false;
}
[[nodiscard]] bool ensureA2dpStreaming(bt1035::Bt1035Driver& driver)
{
if (auto state = driver.queryA2dpState(); state
&& *state == core::Bt1035A2dpState::Streaming) {
ESP_LOGI(kTag, "A2DP already streaming");
return true;
}
if (!driver.waitForA2dpStreaming(kStreamSettleMs)) {
ESP_LOGW(kTag, "A2DP not streaming within %d ms", kStreamSettleMs);
return false;
}
ESP_LOGI(kTag, "A2DP streaming OK");
return true;
}
void savedSpeakerReconnectTask(void* arg)
{
auto* service = static_cast<BluetoothService*>(arg);
ESP_LOGI(kTag, "boot reconnect task started");
if (auto result = service->reconnectSavedSpeaker(true); !result) {
ESP_LOGW(kTag, "boot reconnect failed (%d)",
static_cast<int>(result.error()));
} else {
ESP_LOGI(kTag, "boot reconnect OK");
}
vTaskDelete(nullptr);
}
} // namespace
BluetoothService::BluetoothService(bt1035::Bt1035Driver& driver,
core::ISecureStore& store)
: driver_(driver)
, store_(store)
, pairingActive_(false)
{
}
@@ -91,4 +148,152 @@ std::expected<void, bt1035::Bt1035Error> BluetoothService::setAutoReconnect(
return driver_.setAutoReconnect(times);
}
std::expected<std::vector<core::Bt1035ScannedDevice>, bt1035::Bt1035Error>
BluetoothService::scanNearby(std::uint8_t scanSeconds)
{
pairingActive_ = false;
(void)driver_.leavePairingMode();
(void)driver_.stopScan();
return driver_.scanNearbyBrEdr(scanSeconds);
}
std::expected<void, bt1035::Bt1035Error> BluetoothService::connectDirect(
std::string_view mac)
{
if (auto sent = driver_.connectA2dp(mac); !sent) {
return sent;
}
if (!driver_.waitForA2dpConnected(kConnectSettleMs)) {
ESP_LOGW(kTag, "A2DP connect sent but link not up in %d ms",
kConnectSettleMs);
return std::unexpected(bt1035::Bt1035Error::AtTimeout);
}
if (!ensureA2dpStreaming(driver_)) {
return std::unexpected(bt1035::Bt1035Error::AtTimeout);
}
return {};
}
std::expected<void, bt1035::Bt1035Error> BluetoothService::connectTo(
std::string_view mac)
{
pairingActive_ = false;
return connectDirect(mac);
}
std::expected<void, bt1035::Bt1035Error> BluetoothService::connectTo(
const core::BluetoothConnectRequest& request)
{
if (request.mac.empty() || !core::isValidBt1035Mac(request.mac)) {
return std::unexpected(bt1035::Bt1035Error::UnexpectedResponse);
}
pairingActive_ = false;
if (auto connected = connectDirect(request.mac); !connected) {
return connected;
}
if (request.save) {
core::BtSpeakerTarget target{
.mac = request.mac,
.name = request.name,
};
if (auto saved = saveSpeaker(target); !saved) {
ESP_LOGW(kTag, "connect OK but save speaker failed");
}
}
return {};
}
bool BluetoothService::hasSavedSpeaker() const
{
return store_.hasBtSpeakerTarget();
}
std::expected<core::BtSpeakerTarget, core::StoreError>
BluetoothService::loadSavedSpeaker() const
{
return store_.loadBtSpeakerTarget();
}
std::expected<void, core::StoreError> BluetoothService::saveSpeaker(
const core::BtSpeakerTarget& target)
{
return store_.saveBtSpeakerTarget(target);
}
std::expected<void, core::StoreError> BluetoothService::clearSavedSpeaker()
{
return store_.clearBtSpeakerTarget();
}
void BluetoothService::startupReconnect()
{
if (!hasSavedSpeaker()) {
ESP_LOGI(kTag, "no saved BT speaker — skip boot reconnect");
return;
}
if (xTaskCreate(savedSpeakerReconnectTask, "bt_reconn", 8192, this, 4,
nullptr)
!= pdPASS) {
ESP_LOGW(kTag, "boot reconnect task create failed");
}
}
std::expected<void, bt1035::Bt1035Error> BluetoothService::reconnectSavedSpeaker(
bool scanFallback)
{
const auto target = loadSavedSpeaker();
if (!target) {
return {};
}
ESP_LOGI(kTag, "reconnect saved speaker %s (%s)", target->mac.c_str(),
target->name.empty() ? "(no name)" : target->name.c_str());
if (isA2dpLinked(driver_)) {
ESP_LOGI(kTag, "A2DP already linked — ensure streaming");
if (!ensureA2dpStreaming(driver_)) {
return std::unexpected(bt1035::Bt1035Error::AtTimeout);
}
return {};
}
if (auto connected = connectDirect(target->mac); connected) {
return connected;
}
if (isA2dpLinked(driver_)) {
ESP_LOGI(kTag, "A2DP linked after connect attempt");
if (!ensureA2dpStreaming(driver_)) {
return std::unexpected(bt1035::Bt1035Error::AtTimeout);
}
return {};
}
ESP_LOGI(kTag, "waiting for module auto-reconnect (%d ms)",
kAutoReconnectWaitMs);
if (driver_.waitForA2dpConnected(kAutoReconnectWaitMs)) {
ESP_LOGI(kTag, "A2DP auto-reconnect linked");
if (!ensureA2dpStreaming(driver_)) {
return std::unexpected(bt1035::Bt1035Error::AtTimeout);
}
return {};
}
if (!scanFallback) {
return std::unexpected(bt1035::Bt1035Error::AtTimeout);
}
ESP_LOGW(kTag, "direct A2DPCONN failed — retry connect (no scan)");
if (auto retry = connectDirect(target->mac); retry) {
return retry;
}
if (isA2dpLinked(driver_)) {
if (!ensureA2dpStreaming(driver_)) {
return std::unexpected(bt1035::Bt1035Error::AtTimeout);
}
return {};
}
return std::unexpected(bt1035::Bt1035Error::AtTimeout);
}
} // namespace bluetooth