Add Slice 4 tuner stack with AGENTS-compliant core types and HTTP API.
Introduces Si4684/ADAU1701 drivers, TunerService, FrequencyKHz, SeekDirection, /api/tuner routes without file-scope globals, host tests, and firmware blob tooling (binaries remain gitignored). Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -22,7 +22,10 @@
|
||||
#include "core/HealthStatus.hpp"
|
||||
#include "core/HealthStatusJson.hpp"
|
||||
#include "core/ParseError.hpp"
|
||||
#include "core/SeekDirection.hpp"
|
||||
#include "core/TunerJson.hpp"
|
||||
#include "core/WifiProvisionJson.hpp"
|
||||
#include "tuner/TunerService.hpp"
|
||||
|
||||
#include "esp_http_server.h"
|
||||
#include "esp_log.h"
|
||||
@@ -37,17 +40,30 @@ namespace net {
|
||||
|
||||
namespace {
|
||||
constexpr char kTag[] = "SetupWebServer";
|
||||
constexpr char kFirmwareVersion[] = "0.2.0";
|
||||
constexpr char kFirmwareVersion[] = "0.4.0";
|
||||
constexpr unsigned kRebootDelaySec = 3;
|
||||
|
||||
SetupWebServer* gActiveServer = nullptr;
|
||||
core::ISecureStore* gStore = nullptr;
|
||||
|
||||
extern const uint8_t www_index_html_gz_start[] asm(
|
||||
"_binary_www_index_html_gz_start");
|
||||
extern const uint8_t www_index_html_gz_end[] asm(
|
||||
"_binary_www_index_html_gz_end");
|
||||
|
||||
/**
|
||||
* @brief routeContextFrom — read handler dependencies from user_ctx.
|
||||
*
|
||||
* @dname routeContextFrom
|
||||
* @param req HTTP request handle from esp_http_server.
|
||||
* @return Route context pointer, or nullptr when unset.
|
||||
* @pubstate none
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
[[nodiscard]] HttpRouteContext* routeContextFrom(httpd_req_t* req) noexcept
|
||||
{
|
||||
return static_cast<HttpRouteContext*>(httpd_req_get_user_ctx(req));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief rebootTask — restart after provisioning so STA mode can run.
|
||||
*
|
||||
@@ -91,6 +107,40 @@ void rebootTask(void* arg)
|
||||
return "parse_error";
|
||||
}
|
||||
|
||||
[[nodiscard]] const char* tunerErrorToken(core::TunerError error) noexcept
|
||||
{
|
||||
switch (error) {
|
||||
case core::TunerError::NotBooted:
|
||||
return "not_booted";
|
||||
case core::TunerError::WrongBand:
|
||||
return "wrong_band";
|
||||
case core::TunerError::TuneFailed:
|
||||
return "tune_failed";
|
||||
case core::TunerError::ServiceListEmpty:
|
||||
return "service_list_empty";
|
||||
case core::TunerError::InvalidInput:
|
||||
return "invalid_input";
|
||||
case core::TunerError::HardwareFailed:
|
||||
return "hardware_failed";
|
||||
}
|
||||
return "tuner_error";
|
||||
}
|
||||
|
||||
[[nodiscard]] bool readRequestBody(httpd_req_t* req, std::array<char, 512>& body)
|
||||
{
|
||||
int received = 0;
|
||||
while (received < static_cast<int>(body.size()) - 1) {
|
||||
const int chunk = httpd_req_recv(req, body.data() + received,
|
||||
body.size() - 1 - received);
|
||||
if (chunk <= 0) {
|
||||
break;
|
||||
}
|
||||
received += chunk;
|
||||
}
|
||||
body[static_cast<std::size_t>(received)] = '\0';
|
||||
return received > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief healthGetHandler — serve GET /api/health as JSON.
|
||||
*
|
||||
@@ -111,6 +161,208 @@ esp_err_t healthGetHandler(httpd_req_t* req)
|
||||
return httpd_resp_send(req, json.c_str(), json.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief tunerStatusGetHandler — serve GET /api/tuner/status as JSON.
|
||||
*
|
||||
* @dname tunerStatusGetHandler
|
||||
* @param req HTTP request handle from esp_http_server.
|
||||
* @return ESP_OK on success, or an esp_err_t error code.
|
||||
* @pubstate uses route context tuner service.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
esp_err_t tunerStatusGetHandler(httpd_req_t* req)
|
||||
{
|
||||
auto* ctx = routeContextFrom(req);
|
||||
if (ctx == nullptr || ctx->tuner == nullptr) {
|
||||
httpd_resp_set_status(req, "503 Service Unavailable");
|
||||
return httpd_resp_send(req, nullptr, 0);
|
||||
}
|
||||
auto status = ctx->tuner->refreshStatus();
|
||||
if (!status) {
|
||||
const std::string json =
|
||||
core::serializeTunerErrorJson(tunerErrorToken(status.error()));
|
||||
httpd_resp_set_status(req, "500 Internal Server Error");
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
return httpd_resp_send(req, json.c_str(), json.size());
|
||||
}
|
||||
const std::string json = core::serializeTunerStatusJson(*status);
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
return httpd_resp_send(req, json.c_str(), json.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief tunerServicesGetHandler — serve GET /api/tuner/services as JSON.
|
||||
*
|
||||
* @dname tunerServicesGetHandler
|
||||
* @param req HTTP request handle from esp_http_server.
|
||||
* @return ESP_OK on success, or an esp_err_t error code.
|
||||
* @pubstate uses route context tuner service.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
esp_err_t tunerServicesGetHandler(httpd_req_t* req)
|
||||
{
|
||||
auto* ctx = routeContextFrom(req);
|
||||
if (ctx == nullptr || ctx->tuner == nullptr) {
|
||||
httpd_resp_set_status(req, "503 Service Unavailable");
|
||||
return httpd_resp_send(req, nullptr, 0);
|
||||
}
|
||||
auto services = ctx->tuner->listDabServices();
|
||||
if (!services) {
|
||||
const std::string json =
|
||||
core::serializeTunerErrorJson(tunerErrorToken(services.error()));
|
||||
httpd_resp_set_status(req, "409 Conflict");
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
return httpd_resp_send(req, json.c_str(), json.size());
|
||||
}
|
||||
const std::string json = core::serializeTunerServicesJson(*services);
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
return httpd_resp_send(req, json.c_str(), json.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief tunerTunePostHandler — accept POST /api/tuner/tune JSON.
|
||||
*
|
||||
* @dname tunerTunePostHandler
|
||||
* @param req HTTP request handle from esp_http_server.
|
||||
* @return ESP_OK on success, or an esp_err_t error code.
|
||||
* @pubstate uses route context tuner service.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
esp_err_t tunerTunePostHandler(httpd_req_t* req)
|
||||
{
|
||||
auto* ctx = routeContextFrom(req);
|
||||
if (ctx == nullptr || ctx->tuner == nullptr) {
|
||||
httpd_resp_set_status(req, "503 Service Unavailable");
|
||||
return httpd_resp_send(req, nullptr, 0);
|
||||
}
|
||||
|
||||
std::array<char, 512> body{};
|
||||
if (!readRequestBody(req, body)) {
|
||||
httpd_resp_set_status(req, "400 Bad Request");
|
||||
return httpd_resp_send(req, nullptr, 0);
|
||||
}
|
||||
|
||||
const auto parsed =
|
||||
core::parseTunerTuneJson(std::string_view(body.data()));
|
||||
if (!parsed) {
|
||||
const std::string json = core::serializeTunerErrorJson("invalid_json");
|
||||
httpd_resp_set_status(req, "400 Bad Request");
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
return httpd_resp_send(req, json.c_str(), json.size());
|
||||
}
|
||||
|
||||
std::expected<void, core::TunerError> result = std::unexpected(
|
||||
core::TunerError::InvalidInput);
|
||||
if (parsed->band == core::TunerBand::Dab) {
|
||||
result = ctx->tuner->tuneDab(parsed->dabFreqIndex);
|
||||
} else if (parsed->fmFrequency) {
|
||||
result = ctx->tuner->tuneFm(*parsed->fmFrequency);
|
||||
}
|
||||
|
||||
if (!result) {
|
||||
const std::string json =
|
||||
core::serializeTunerErrorJson(tunerErrorToken(result.error()));
|
||||
httpd_resp_set_status(req, "409 Conflict");
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
return httpd_resp_send(req, json.c_str(), json.size());
|
||||
}
|
||||
|
||||
auto status = ctx->tuner->refreshStatus();
|
||||
const std::string json = status
|
||||
? core::serializeTunerStatusJson(*status)
|
||||
: std::string("{\"status\":\"ok\"}");
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
return httpd_resp_send(req, json.c_str(), json.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief tunerPlayPostHandler — accept POST /api/tuner/play JSON.
|
||||
*
|
||||
* @dname tunerPlayPostHandler
|
||||
* @param req HTTP request handle from esp_http_server.
|
||||
* @return ESP_OK on success, or an esp_err_t error code.
|
||||
* @pubstate uses route context tuner service.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
esp_err_t tunerPlayPostHandler(httpd_req_t* req)
|
||||
{
|
||||
auto* ctx = routeContextFrom(req);
|
||||
if (ctx == nullptr || ctx->tuner == nullptr) {
|
||||
httpd_resp_set_status(req, "503 Service Unavailable");
|
||||
return httpd_resp_send(req, nullptr, 0);
|
||||
}
|
||||
|
||||
std::array<char, 512> body{};
|
||||
if (!readRequestBody(req, body)) {
|
||||
httpd_resp_set_status(req, "400 Bad Request");
|
||||
return httpd_resp_send(req, nullptr, 0);
|
||||
}
|
||||
|
||||
const auto parsed =
|
||||
core::parseTunerPlayJson(std::string_view(body.data()));
|
||||
if (!parsed) {
|
||||
const std::string json = core::serializeTunerErrorJson("invalid_json");
|
||||
httpd_resp_set_status(req, "400 Bad Request");
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
return httpd_resp_send(req, json.c_str(), json.size());
|
||||
}
|
||||
|
||||
if (auto play = ctx->tuner->playDabService(parsed->serviceId,
|
||||
parsed->componentId);
|
||||
!play) {
|
||||
const std::string json =
|
||||
core::serializeTunerErrorJson(tunerErrorToken(play.error()));
|
||||
httpd_resp_set_status(req, "409 Conflict");
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
return httpd_resp_send(req, json.c_str(), json.size());
|
||||
}
|
||||
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
return httpd_resp_send(req, "{\"status\":\"playing\"}", 20);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief tunerSeekPostHandler — accept POST /api/tuner/seek (FM up).
|
||||
*
|
||||
* @dname tunerSeekPostHandler
|
||||
* @param req HTTP request handle from esp_http_server.
|
||||
* @return ESP_OK on success, or an esp_err_t error code.
|
||||
* @pubstate uses route context tuner service with SeekDirection::Up.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
esp_err_t tunerSeekPostHandler(httpd_req_t* req)
|
||||
{
|
||||
auto* ctx = routeContextFrom(req);
|
||||
if (ctx == nullptr || ctx->tuner == nullptr) {
|
||||
httpd_resp_set_status(req, "503 Service Unavailable");
|
||||
return httpd_resp_send(req, nullptr, 0);
|
||||
}
|
||||
|
||||
auto freq = ctx->tuner->seekFm(core::SeekDirection::Up);
|
||||
if (freq) {
|
||||
const std::string json = std::string("{\"frequency_khz\":")
|
||||
+ std::to_string(freq->value()) + "}";
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
return httpd_resp_send(req, json.c_str(), json.size());
|
||||
}
|
||||
|
||||
const std::string json =
|
||||
core::serializeTunerErrorJson(tunerErrorToken(freq.error()));
|
||||
httpd_resp_set_status(req, "409 Conflict");
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
return httpd_resp_send(req, json.c_str(), json.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief indexGetHandler — serve gzipped setup page from flash.
|
||||
*
|
||||
@@ -139,14 +391,15 @@ esp_err_t indexGetHandler(httpd_req_t* req)
|
||||
* @dname wifiPostHandler
|
||||
* @param req HTTP request handle from esp_http_server.
|
||||
* @return ESP_OK on success, or an esp_err_t error code.
|
||||
* @pubstate uses gActiveServer->store_ for persistence.
|
||||
* @pubstate uses route context secure store for persistence.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
esp_err_t wifiPostHandler(httpd_req_t* req)
|
||||
{
|
||||
if (gStore == nullptr) {
|
||||
auto* ctx = routeContextFrom(req);
|
||||
if (ctx == nullptr || ctx->store == nullptr) {
|
||||
httpd_resp_set_status(req, "500 Internal Server Error");
|
||||
return httpd_resp_send(req, nullptr, 0);
|
||||
}
|
||||
@@ -173,7 +426,7 @@ esp_err_t wifiPostHandler(httpd_req_t* req)
|
||||
return httpd_resp_send(req, json.c_str(), json.size());
|
||||
}
|
||||
|
||||
if (!gStore->saveWifiCredentials(parsed.value())) {
|
||||
if (!ctx->store->saveWifiCredentials(parsed.value())) {
|
||||
const std::string json =
|
||||
core::serializeWifiProvisionErrorJson("store_failed");
|
||||
httpd_resp_set_status(req, "500 Internal Server Error");
|
||||
@@ -196,6 +449,8 @@ SetupWebServer::SetupWebServer()
|
||||
: server_(nullptr)
|
||||
, store_(nullptr)
|
||||
, netState_(NetState::Uninitialized)
|
||||
, tuner_(nullptr)
|
||||
, routeContext_{nullptr, nullptr}
|
||||
{
|
||||
}
|
||||
|
||||
@@ -203,11 +458,14 @@ SetupWebServer::SetupWebServer(SetupWebServer&& other) noexcept
|
||||
: server_(other.server_)
|
||||
, store_(other.store_)
|
||||
, netState_(other.netState_)
|
||||
, tuner_(other.tuner_)
|
||||
, routeContext_(other.routeContext_)
|
||||
{
|
||||
other.server_ = nullptr;
|
||||
other.store_ = nullptr;
|
||||
other.netState_ = NetState::Uninitialized;
|
||||
gActiveServer = this;
|
||||
other.tuner_ = nullptr;
|
||||
other.routeContext_ = {nullptr, nullptr};
|
||||
}
|
||||
|
||||
SetupWebServer& SetupWebServer::operator=(SetupWebServer&& other) noexcept
|
||||
@@ -219,30 +477,30 @@ SetupWebServer& SetupWebServer::operator=(SetupWebServer&& other) noexcept
|
||||
server_ = other.server_;
|
||||
store_ = other.store_;
|
||||
netState_ = other.netState_;
|
||||
tuner_ = other.tuner_;
|
||||
routeContext_ = other.routeContext_;
|
||||
other.server_ = nullptr;
|
||||
other.store_ = nullptr;
|
||||
other.netState_ = NetState::Uninitialized;
|
||||
gActiveServer = this;
|
||||
other.tuner_ = nullptr;
|
||||
other.routeContext_ = {nullptr, nullptr};
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
SetupWebServer::~SetupWebServer()
|
||||
{
|
||||
if (gActiveServer == this) {
|
||||
gActiveServer = nullptr;
|
||||
}
|
||||
if (gStore == store_) {
|
||||
gStore = nullptr;
|
||||
}
|
||||
if (server_ != nullptr) {
|
||||
httpd_stop(server_);
|
||||
server_ = nullptr;
|
||||
}
|
||||
routeContext_ = {nullptr, nullptr};
|
||||
}
|
||||
|
||||
std::expected<void, NetError> SetupWebServer::start(core::ISecureStore& store,
|
||||
NetState netState)
|
||||
std::expected<void, NetError> SetupWebServer::start(
|
||||
core::ISecureStore& store,
|
||||
NetState netState,
|
||||
tuner::TunerService& tuner)
|
||||
{
|
||||
if (server_ != nullptr) {
|
||||
return {};
|
||||
@@ -250,8 +508,9 @@ std::expected<void, NetError> SetupWebServer::start(core::ISecureStore& store,
|
||||
|
||||
store_ = &store;
|
||||
netState_ = netState;
|
||||
gActiveServer = this;
|
||||
gStore = &store;
|
||||
tuner_ = &tuner;
|
||||
routeContext_.store = &store;
|
||||
routeContext_.tuner = &tuner;
|
||||
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.server_port = 80;
|
||||
@@ -259,16 +518,17 @@ std::expected<void, NetError> SetupWebServer::start(core::ISecureStore& store,
|
||||
|
||||
if (httpd_start(&server_, &config) != ESP_OK) {
|
||||
ESP_LOGE(kTag, "httpd_start failed");
|
||||
gActiveServer = nullptr;
|
||||
gStore = nullptr;
|
||||
routeContext_ = {nullptr, nullptr};
|
||||
return std::unexpected(NetError::HttpServerStartFailed);
|
||||
}
|
||||
|
||||
void* routeCtx = &routeContext_;
|
||||
|
||||
const httpd_uri_t healthUri = {
|
||||
.uri = "/api/health",
|
||||
.method = HTTP_GET,
|
||||
.handler = healthGetHandler,
|
||||
.user_ctx = nullptr,
|
||||
.user_ctx = routeCtx,
|
||||
};
|
||||
httpd_register_uri_handler(server_, &healthUri);
|
||||
|
||||
@@ -276,7 +536,7 @@ std::expected<void, NetError> SetupWebServer::start(core::ISecureStore& store,
|
||||
.uri = "/",
|
||||
.method = HTTP_GET,
|
||||
.handler = indexGetHandler,
|
||||
.user_ctx = nullptr,
|
||||
.user_ctx = routeCtx,
|
||||
};
|
||||
httpd_register_uri_handler(server_, &indexUri);
|
||||
|
||||
@@ -284,10 +544,50 @@ std::expected<void, NetError> SetupWebServer::start(core::ISecureStore& store,
|
||||
.uri = "/api/wifi",
|
||||
.method = HTTP_POST,
|
||||
.handler = wifiPostHandler,
|
||||
.user_ctx = nullptr,
|
||||
.user_ctx = routeCtx,
|
||||
};
|
||||
httpd_register_uri_handler(server_, &wifiUri);
|
||||
|
||||
const httpd_uri_t tunerStatusUri = {
|
||||
.uri = "/api/tuner/status",
|
||||
.method = HTTP_GET,
|
||||
.handler = tunerStatusGetHandler,
|
||||
.user_ctx = routeCtx,
|
||||
};
|
||||
httpd_register_uri_handler(server_, &tunerStatusUri);
|
||||
|
||||
const httpd_uri_t tunerServicesUri = {
|
||||
.uri = "/api/tuner/services",
|
||||
.method = HTTP_GET,
|
||||
.handler = tunerServicesGetHandler,
|
||||
.user_ctx = routeCtx,
|
||||
};
|
||||
httpd_register_uri_handler(server_, &tunerServicesUri);
|
||||
|
||||
const httpd_uri_t tunerTuneUri = {
|
||||
.uri = "/api/tuner/tune",
|
||||
.method = HTTP_POST,
|
||||
.handler = tunerTunePostHandler,
|
||||
.user_ctx = routeCtx,
|
||||
};
|
||||
httpd_register_uri_handler(server_, &tunerTuneUri);
|
||||
|
||||
const httpd_uri_t tunerPlayUri = {
|
||||
.uri = "/api/tuner/play",
|
||||
.method = HTTP_POST,
|
||||
.handler = tunerPlayPostHandler,
|
||||
.user_ctx = routeCtx,
|
||||
};
|
||||
httpd_register_uri_handler(server_, &tunerPlayUri);
|
||||
|
||||
const httpd_uri_t tunerSeekUri = {
|
||||
.uri = "/api/tuner/seek",
|
||||
.method = HTTP_POST,
|
||||
.handler = tunerSeekPostHandler,
|
||||
.user_ctx = routeCtx,
|
||||
};
|
||||
httpd_register_uri_handler(server_, &tunerSeekUri);
|
||||
|
||||
ESP_LOGI(kTag, "HTTP server listening on port 80");
|
||||
return {};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user