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:
@@ -36,9 +36,62 @@ namespace {
|
||||
constexpr char kTag[] = "StaClient";
|
||||
constexpr int kConnectedBit = BIT0;
|
||||
constexpr int kFailedBit = BIT1;
|
||||
constexpr TickType_t kConnectTimeout = pdMS_TO_TICKS(30000);
|
||||
constexpr int kMaxConnectRetries = 10;
|
||||
constexpr TickType_t kConnectTimeout = pdMS_TO_TICKS(45000);
|
||||
constexpr TickType_t kRetryDelay = pdMS_TO_TICKS(800);
|
||||
|
||||
EventGroupHandle_t s_wifiEventGroup = nullptr;
|
||||
int s_connectRetries = 0;
|
||||
|
||||
/**
|
||||
* @brief disconnectReasonString — map ESP-IDF Wi-Fi disconnect reason codes.
|
||||
*
|
||||
* @dname disconnectReasonString
|
||||
* @param reason wifi_event_sta_disconnected_t::reason value.
|
||||
* @return Short English label for logs.
|
||||
* @pubstate none
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-08-05
|
||||
*/
|
||||
[[nodiscard]] const char* disconnectReasonString(uint8_t reason) noexcept
|
||||
{
|
||||
switch (reason) {
|
||||
case WIFI_REASON_AUTH_EXPIRE:
|
||||
return "auth expired (wrong password?)";
|
||||
case WIFI_REASON_NO_AP_FOUND:
|
||||
return "no AP found (check SSID / 2.4 GHz)";
|
||||
case WIFI_REASON_AUTH_FAIL:
|
||||
return "auth failed (check password)";
|
||||
case WIFI_REASON_ASSOC_FAIL:
|
||||
return "association failed";
|
||||
case WIFI_REASON_HANDSHAKE_TIMEOUT:
|
||||
return "handshake timeout";
|
||||
case WIFI_REASON_BEACON_TIMEOUT:
|
||||
return "beacon timeout (weak signal / PS)";
|
||||
case WIFI_REASON_CONNECTION_FAIL:
|
||||
return "connection failed";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief applyStaLinkTuning — stabilise STA link after connect.
|
||||
*
|
||||
* @dname applyStaLinkTuning
|
||||
* @pubstate Disables PS, forces 20 MHz, disables inactive disconnect.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-08-05
|
||||
*/
|
||||
void applyStaLinkTuning() noexcept
|
||||
{
|
||||
esp_wifi_set_ps(WIFI_PS_NONE);
|
||||
esp_wifi_set_bandwidth(WIFI_IF_STA, WIFI_BW_HT20);
|
||||
esp_wifi_set_inactive_time(WIFI_IF_STA, 0);
|
||||
ESP_LOGI(kTag, "STA link tuning applied (PS off, HT20, inactive off)");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief wifiEventHandler — signal connect success or failure.
|
||||
@@ -59,29 +112,72 @@ void wifiEventHandler(void* arg,
|
||||
void* eventData)
|
||||
{
|
||||
(void)arg;
|
||||
(void)eventData;
|
||||
if (eventBase == WIFI_EVENT && eventId == WIFI_EVENT_STA_START) {
|
||||
esp_wifi_connect();
|
||||
} else if (eventBase == WIFI_EVENT
|
||||
&& eventId == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
const auto* disc =
|
||||
static_cast<const wifi_event_sta_disconnected_t*>(eventData);
|
||||
const uint8_t reason = disc != nullptr ? disc->reason : 0U;
|
||||
ESP_LOGW(kTag, "STA disconnected (reason %u: %s)",
|
||||
static_cast<unsigned>(reason),
|
||||
disconnectReasonString(reason));
|
||||
|
||||
if (s_wifiEventGroup != nullptr) {
|
||||
if (s_connectRetries < kMaxConnectRetries) {
|
||||
++s_connectRetries;
|
||||
ESP_LOGI(kTag, "retrying STA connect (%d/%d)",
|
||||
s_connectRetries, kMaxConnectRetries);
|
||||
vTaskDelay(kRetryDelay);
|
||||
esp_wifi_connect();
|
||||
return;
|
||||
}
|
||||
xEventGroupSetBits(s_wifiEventGroup, kFailedBit);
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGW(kTag, "STA link lost — reconnecting");
|
||||
esp_wifi_connect();
|
||||
} else if (eventBase == IP_EVENT && eventId == IP_EVENT_STA_GOT_IP) {
|
||||
const auto* event =
|
||||
static_cast<const ip_event_got_ip_t*>(eventData);
|
||||
if (event != nullptr) {
|
||||
ESP_LOGI(kTag, "STA IP " IPSTR, IP2STR(&event->ip_info.ip));
|
||||
}
|
||||
applyStaLinkTuning();
|
||||
if (s_wifiEventGroup != nullptr) {
|
||||
xEventGroupSetBits(s_wifiEventGroup, kConnectedBit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void unregisterStaHandlers(esp_event_handler_instance_t wifiHandler,
|
||||
esp_event_handler_instance_t ipHandler) noexcept
|
||||
{
|
||||
if (wifiHandler != nullptr) {
|
||||
esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID,
|
||||
wifiHandler);
|
||||
}
|
||||
if (ipHandler != nullptr) {
|
||||
esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP,
|
||||
ipHandler);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
StaClient::StaClient()
|
||||
: connected_(false)
|
||||
, wifiHandler_(nullptr)
|
||||
, ipHandler_(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
StaClient::~StaClient()
|
||||
{
|
||||
unregisterStaHandlers(wifiHandler_, ipHandler_);
|
||||
wifiHandler_ = nullptr;
|
||||
ipHandler_ = nullptr;
|
||||
if (connected_) {
|
||||
esp_wifi_stop();
|
||||
connected_ = false;
|
||||
@@ -90,18 +186,27 @@ StaClient::~StaClient()
|
||||
|
||||
StaClient::StaClient(StaClient&& other) noexcept
|
||||
: connected_(other.connected_)
|
||||
, wifiHandler_(other.wifiHandler_)
|
||||
, ipHandler_(other.ipHandler_)
|
||||
{
|
||||
other.connected_ = false;
|
||||
other.wifiHandler_ = nullptr;
|
||||
other.ipHandler_ = nullptr;
|
||||
}
|
||||
|
||||
StaClient& StaClient::operator=(StaClient&& other) noexcept
|
||||
{
|
||||
if (this != &other) {
|
||||
unregisterStaHandlers(wifiHandler_, ipHandler_);
|
||||
if (connected_) {
|
||||
esp_wifi_stop();
|
||||
}
|
||||
connected_ = other.connected_;
|
||||
wifiHandler_ = other.wifiHandler_;
|
||||
ipHandler_ = other.ipHandler_;
|
||||
other.connected_ = false;
|
||||
other.wifiHandler_ = nullptr;
|
||||
other.ipHandler_ = nullptr;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@@ -122,19 +227,18 @@ StaClient::connect(const core::WifiCredentials& creds, std::string_view hostname
|
||||
if (s_wifiEventGroup == nullptr) {
|
||||
return std::unexpected(NetError::StaConnectFailed);
|
||||
}
|
||||
s_connectRetries = 0;
|
||||
|
||||
esp_event_handler_instance_t instanceAnyId = nullptr;
|
||||
esp_event_handler_instance_t instanceGotIp = nullptr;
|
||||
esp_event_handler_instance_register(WIFI_EVENT,
|
||||
ESP_EVENT_ANY_ID,
|
||||
&wifiEventHandler,
|
||||
nullptr,
|
||||
&instanceAnyId);
|
||||
&wifiHandler_);
|
||||
esp_event_handler_instance_register(IP_EVENT,
|
||||
IP_EVENT_STA_GOT_IP,
|
||||
&wifiEventHandler,
|
||||
nullptr,
|
||||
&instanceGotIp);
|
||||
&ipHandler_);
|
||||
|
||||
if (esp_wifi_set_mode(WIFI_MODE_STA) != ESP_OK) {
|
||||
return std::unexpected(NetError::WifiConfigFailed);
|
||||
@@ -150,15 +254,31 @@ StaClient::connect(const core::WifiCredentials& creds, std::string_view hostname
|
||||
wifi_config_t wifiCfg = {};
|
||||
const std::string_view ssid = creds.ssid().value();
|
||||
const std::size_t ssidCopy =
|
||||
std::min(ssid.size(), sizeof(wifiCfg.sta.ssid) - 1);
|
||||
std::min(ssid.size(), sizeof(wifiCfg.sta.ssid) - 1U);
|
||||
std::memcpy(wifiCfg.sta.ssid, ssid.data(), ssidCopy);
|
||||
wifiCfg.sta.ssid[ssidCopy] = '\0';
|
||||
|
||||
std::size_t pwdLen = 0U;
|
||||
creds.password().usePlaintext([&](std::string_view pwd) {
|
||||
pwdLen = pwd.size();
|
||||
const std::size_t pwdCopy =
|
||||
std::min(pwd.size(), sizeof(wifiCfg.sta.password) - 1);
|
||||
std::min(pwd.size(), sizeof(wifiCfg.sta.password) - 1U);
|
||||
std::memcpy(wifiCfg.sta.password, pwd.data(), pwdCopy);
|
||||
wifiCfg.sta.password[pwdCopy] = '\0';
|
||||
});
|
||||
|
||||
if (pwdLen == 0U) {
|
||||
wifiCfg.sta.threshold.authmode = WIFI_AUTH_OPEN;
|
||||
} else {
|
||||
wifiCfg.sta.threshold.authmode = WIFI_AUTH_WPA2_WPA3_PSK;
|
||||
}
|
||||
wifiCfg.sta.pmf_cfg.capable = true;
|
||||
wifiCfg.sta.pmf_cfg.required = false;
|
||||
wifiCfg.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
|
||||
wifiCfg.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL;
|
||||
wifiCfg.sta.failure_retry_cnt = 3;
|
||||
wifiCfg.sta.listen_interval = 1;
|
||||
|
||||
if (esp_wifi_set_config(WIFI_IF_STA, &wifiCfg) != ESP_OK) {
|
||||
return std::unexpected(NetError::WifiConfigFailed);
|
||||
}
|
||||
@@ -167,20 +287,22 @@ StaClient::connect(const core::WifiCredentials& creds, std::string_view hostname
|
||||
return std::unexpected(NetError::WifiStartFailed);
|
||||
}
|
||||
|
||||
esp_wifi_set_protocol(WIFI_IF_STA,
|
||||
WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G
|
||||
| WIFI_PROTOCOL_11N);
|
||||
esp_wifi_set_ps(WIFI_PS_NONE);
|
||||
|
||||
const EventBits_t bits = xEventGroupWaitBits(s_wifiEventGroup,
|
||||
kConnectedBit | kFailedBit,
|
||||
pdTRUE,
|
||||
pdFALSE,
|
||||
kConnectTimeout);
|
||||
|
||||
esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP,
|
||||
instanceGotIp);
|
||||
esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID,
|
||||
instanceAnyId);
|
||||
vEventGroupDelete(s_wifiEventGroup);
|
||||
s_wifiEventGroup = nullptr;
|
||||
|
||||
if ((bits & kConnectedBit) != 0) {
|
||||
applyStaLinkTuning();
|
||||
connected_ = true;
|
||||
if (!hostLabel.empty()) {
|
||||
if (mdns_init() == ESP_OK) {
|
||||
@@ -192,6 +314,9 @@ StaClient::connect(const core::WifiCredentials& creds, std::string_view hostname
|
||||
return {};
|
||||
}
|
||||
|
||||
unregisterStaHandlers(wifiHandler_, ipHandler_);
|
||||
wifiHandler_ = nullptr;
|
||||
ipHandler_ = nullptr;
|
||||
esp_wifi_stop();
|
||||
ESP_LOGW(kTag, "STA connect timed out or failed");
|
||||
if ((bits & kFailedBit) != 0) {
|
||||
|
||||
Reference in New Issue
Block a user