Redo ap settings to rest and proto

This commit is contained in:
Niklas Jensen
2026-01-23 14:23:19 +01:00
committed by nikguin04
parent 02aaee0878
commit e1e656478d
10 changed files with 360 additions and 7 deletions
+4
View File
@@ -2,6 +2,7 @@
#include <template/stateful_service.h>
#include <template/stateful_endpoint.h>
#include <template/stateful_proto_endpoint.h>
#include <template/stateful_persistence.h>
#include <settings/ap_settings.h>
#include <utils/timing.h>
@@ -19,10 +20,13 @@ class APService : public StatefulService<APSettings> {
void recoveryMode();
esp_err_t getStatus(httpd_req_t *request);
esp_err_t getStatusProto(httpd_req_t *request);
void status(JsonObject &root);
void statusProto(api_APStatus &proto);
APNetworkStatus getAPNetworkStatus();
StatefulHttpEndpoint<APSettings> endpoint;
StatefulProtoEndpoint<APSettings, api_APSettings> protoEndpoint;
private:
FSPersistence<APSettings> _persistence;
+43
View File
@@ -6,9 +6,12 @@
#include <string>
#include <map>
#include <ArduinoJson.h>
#include <pb_encode.h>
#include <pb_decode.h>
using HttpGetHandler = std::function<esp_err_t(httpd_req_t*)>;
using HttpPostHandler = std::function<esp_err_t(httpd_req_t*, JsonVariant&)>;
using HttpRawHandler = std::function<esp_err_t(httpd_req_t*)>;
using WsFrameHandler = std::function<esp_err_t(httpd_req_t*, httpd_ws_frame_t*)>;
using WsOpenHandler = std::function<void(httpd_req_t*)>;
using WsCloseHandler = std::function<void(int)>;
@@ -18,6 +21,7 @@ struct HttpRoute {
httpd_method_t method;
HttpGetHandler getHandler;
HttpPostHandler postHandler;
HttpRawHandler rawHandler; // For proto handlers that don't need JSON parsing
bool isWebsocket;
};
@@ -32,6 +36,7 @@ class WebServer {
void on(const char* uri, httpd_method_t method, HttpGetHandler handler);
void on(const char* uri, httpd_method_t method, HttpPostHandler handler);
void onRaw(const char* uri, httpd_method_t method, HttpRawHandler handler);
void onWsFrame(WsFrameHandler handler);
void onWsOpen(WsOpenHandler handler);
@@ -52,6 +57,44 @@ class WebServer {
static esp_err_t sendJson(httpd_req_t* req, int status, JsonDocument& doc);
static esp_err_t sendError(httpd_req_t* req, int status, const char* message);
static esp_err_t sendOk(httpd_req_t* req);
static esp_err_t sendProto(httpd_req_t* req, int status, const uint8_t* data, size_t len);
template <typename T>
static esp_err_t sendProto(httpd_req_t* req, int status, const T& msg, const pb_msgdesc_t* fields) {
uint8_t buffer[1024];
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
if (!pb_encode(&stream, fields, &msg)) {
return sendError(req, 500, "Failed to encode proto");
}
return sendProto(req, status, buffer, stream.bytes_written);
}
template <typename T>
static bool receiveProto(httpd_req_t* req, T& msg, const pb_msgdesc_t* fields) {
size_t contentLen = req->content_len;
if (contentLen == 0 || contentLen > 4096) {
return false;
}
uint8_t* buffer = (uint8_t*)malloc(contentLen);
if (!buffer) {
return false;
}
int received = 0;
int remaining = contentLen;
while (remaining > 0) {
int ret = httpd_req_recv(req, (char*)buffer + received, remaining);
if (ret <= 0) {
free(buffer);
return false;
}
received += ret;
remaining -= ret;
}
pb_istream_t stream = pb_istream_from_buffer(buffer, contentLen);
bool success = pb_decode(&stream, fields, &msg);
free(buffer);
return success;
}
private:
httpd_handle_t server_ = nullptr;
+44
View File
@@ -3,7 +3,9 @@
#include <WiFi.h>
#include <ArduinoJson.h>
#include <template/state_result.h>
#include <platform_shared/api.pb.h>
#include <string>
#include <cstring>
#include <DNSServer.h>
@@ -114,4 +116,46 @@ class APSettings {
settings = newSettings;
return StateUpdateResult::CHANGED;
}
/** Converts internal state to protobuf message */
static void readProto(const APSettings &settings, api_APSettings &proto) {
proto.provision_mode = static_cast<api_APProvisionMode>(settings.provisionMode);
strncpy(proto.ssid, settings.ssid.c_str(), sizeof(proto.ssid) - 1);
proto.ssid[sizeof(proto.ssid) - 1] = '\0';
strncpy(proto.password, settings.password.c_str(), sizeof(proto.password) - 1);
proto.password[sizeof(proto.password) - 1] = '\0';
proto.channel = settings.channel;
proto.ssid_hidden = settings.ssidHidden;
proto.max_clients = settings.maxClients;
proto.local_ip = static_cast<uint32_t>(settings.localIP);
proto.gateway_ip = static_cast<uint32_t>(settings.gatewayIP);
proto.subnet_mask = static_cast<uint32_t>(settings.subnetMask);
}
/** Converts incoming protobuf message to internal state */
static StateUpdateResult updateProto(const api_APSettings &proto, APSettings &settings) {
APSettings newSettings = {};
newSettings.provisionMode = static_cast<uint8_t>(proto.provision_mode);
switch (newSettings.provisionMode) {
case AP_MODE_ALWAYS:
case AP_MODE_DISCONNECTED:
case AP_MODE_NEVER: break;
default: newSettings.provisionMode = AP_MODE_DISCONNECTED;
}
newSettings.ssid = proto.ssid[0] ? proto.ssid : FACTORY_AP_SSID;
newSettings.password = proto.password[0] ? proto.password : FACTORY_AP_PASSWORD;
newSettings.channel = proto.channel ? proto.channel : FACTORY_AP_CHANNEL;
newSettings.ssidHidden = proto.ssid_hidden;
newSettings.maxClients = proto.max_clients ? proto.max_clients : FACTORY_AP_MAX_CLIENTS;
newSettings.localIP = proto.local_ip ? IPAddress(proto.local_ip) : IPAddress(parseIPv4(FACTORY_AP_LOCAL_IP));
newSettings.gatewayIP = proto.gateway_ip ? IPAddress(proto.gateway_ip) : IPAddress(parseIPv4(FACTORY_AP_GATEWAY_IP));
newSettings.subnetMask = proto.subnet_mask ? IPAddress(proto.subnet_mask) : IPAddress(parseIPv4(FACTORY_AP_SUBNET_MASK));
if (newSettings == settings) {
return StateUpdateResult::UNCHANGED;
}
settings = newSettings;
return StateUpdateResult::CHANGED;
}
};
@@ -0,0 +1,137 @@
#pragma once
#include <esp_http_server.h>
#include <template/stateful_service.h>
#include <communication/native_server.h>
#include <platform_shared/api.pb.h>
#include <pb_encode.h>
#include <pb_decode.h>
#include <functional>
#define PROTO_ENDPOINT_ORIGIN_ID "proto"
/**
* A stateful HTTP endpoint that uses protobuf encoding with api::Request/Response wrappers.
*
* @tparam T The internal state type (e.g., APSettings C++ class)
* @tparam ProtoT The protobuf message type within the oneof (e.g., api_APSettings)
*
* The endpoint receives api::Request, extracts the specific payload from the oneof,
* and returns api::Response with the updated state.
*/
template <class T, class ProtoT>
class StatefulProtoEndpoint {
public:
/** Converts internal state to protobuf message for responses */
using ProtoStateReader = std::function<void(const T&, ProtoT&)>;
/** Converts incoming protobuf message to internal state */
using ProtoStateUpdater = std::function<StateUpdateResult(const ProtoT&, T&)>;
/** Extracts the specific proto type from Request oneof */
using RequestExtractor = std::function<bool(const api_Request&, ProtoT&)>;
/** Assigns the specific proto type to Response oneof */
using ResponseAssigner = std::function<void(api_Response&, const ProtoT&)>;
protected:
ProtoStateReader _stateReader;
ProtoStateUpdater _stateUpdater;
StatefulService<T>* _statefulService;
RequestExtractor _requestExtractor;
ResponseAssigner _responseAssigner;
public:
/**
* Constructor for wrapped proto endpoint
* @param stateReader Converts internal state to proto
* @param stateUpdater Converts proto to internal state
* @param statefulService The stateful service to manage
* @param requestExtractor Extracts specific type from Request oneof
* @param responseAssigner Assigns specific type to Response oneof
*/
StatefulProtoEndpoint(ProtoStateReader stateReader, ProtoStateUpdater stateUpdater,
StatefulService<T>* statefulService,
RequestExtractor requestExtractor, ResponseAssigner responseAssigner)
: _stateReader(stateReader),
_stateUpdater(stateUpdater),
_statefulService(statefulService),
_requestExtractor(requestExtractor),
_responseAssigner(responseAssigner) {}
/**
* Handles POST requests: decodes Request, updates state, returns Response
*/
esp_err_t handleStateUpdate(httpd_req_t* request) {
api_Request req = api_Request_init_zero;
if (!NativeServer::receiveProto(request, req, api_Request_fields)) {
return sendErrorResponse(request, 400, "Failed to decode request");
}
ProtoT protoMsg = {};
if (!_requestExtractor(req, protoMsg)) {
return sendErrorResponse(request, 400, "Invalid request type");
}
StateUpdateResult outcome = _statefulService->update(
[this, &protoMsg](T& settings) { return _stateUpdater(protoMsg, settings); }, PROTO_ENDPOINT_ORIGIN_ID);
if (outcome == StateUpdateResult::ERROR) {
return sendErrorResponse(request, 400, "Invalid state");
}
return sendStateResponse(request, 200);
}
/**
* Handles GET requests: reads current state and returns it as Response
*/
esp_err_t getState(httpd_req_t* request) {
return sendStateResponse(request, 200);
}
private:
/** Sends current state wrapped in Response */
esp_err_t sendStateResponse(httpd_req_t* request, uint32_t statusCode) {
api_Response res = api_Response_init_zero;
res.status_code = statusCode;
ProtoT protoState = {};
_statefulService->read([this, &protoState](const T& settings) { _stateReader(settings, protoState); });
_responseAssigner(res, protoState);
return NativeServer::sendProto(request, 200, res, api_Response_fields);
}
/** Sends error wrapped in Response */
esp_err_t sendErrorResponse(httpd_req_t* request, uint32_t statusCode, const char* message) {
api_Response res = api_Response_init_zero;
res.status_code = statusCode;
return NativeServer::sendProto(request, statusCode == 200 ? 200 : 400, res, api_Response_fields);
}
};
// =============================================================================
// Helper macros for defining request extractors and response assigners
// =============================================================================
/**
* Creates a request extractor lambda for a specific payload type
* Usage: API_REQUEST_EXTRACTOR(ap_settings, api_APSettings)
*/
#define API_REQUEST_EXTRACTOR(field_name, proto_type) \
[](const api_Request& req, proto_type& out) -> bool { \
if (req.which_payload == api_Request_##field_name##_tag) { \
out = req.payload.field_name; \
return true; \
} \
return false; \
}
/**
* Creates a response assigner lambda for a specific payload type
* Usage: API_RESPONSE_ASSIGNER(ap_settings, api_APSettings)
*/
#define API_RESPONSE_ASSIGNER(field_name, proto_type) \
[](api_Response& res, const proto_type& data) { \
res.which_payload = api_Response_##field_name##_tag; \
res.payload.field_name = data; \
}