Redo ap settings to rest and proto
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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; \
|
||||
}
|
||||
@@ -34,7 +34,7 @@ def compile_nanopb():
|
||||
|
||||
output_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
proto_files = [proto_dir / "filesystem.proto", proto_dir / "message.proto"]
|
||||
proto_files = [proto_dir / "filesystem.proto", proto_dir / "message.proto", proto_dir / "api.proto"]
|
||||
|
||||
print(f"Compiling protobuf files with nanopb...")
|
||||
print(f" Proto dir: {proto_dir}")
|
||||
|
||||
@@ -5,6 +5,9 @@ static const char *TAG = "APService";
|
||||
|
||||
APService::APService()
|
||||
: endpoint(APSettings::read, APSettings::update, this),
|
||||
protoEndpoint(APSettings::readProto, APSettings::updateProto, this,
|
||||
API_REQUEST_EXTRACTOR(ap_settings, api_APSettings),
|
||||
API_RESPONSE_ASSIGNER(ap_settings, api_APSettings)),
|
||||
_persistence(APSettings::read, APSettings::update, this, AP_SETTINGS_FILE) {
|
||||
addUpdateHandler([&](const std::string &originId) { reconfigureAP(); }, false);
|
||||
}
|
||||
@@ -27,6 +30,23 @@ void APService::status(JsonObject &root) {
|
||||
root["station_num"] = WiFi.softAPgetStationNum();
|
||||
}
|
||||
|
||||
esp_err_t APService::getStatusProto(httpd_req_t *request) {
|
||||
api_Response res = api_Response_init_zero;
|
||||
res.status_code = 200;
|
||||
res.which_payload = api_Response_ap_status_tag;
|
||||
statusProto(res.payload.ap_status);
|
||||
return NativeServer::sendProto(request, 200, res, api_Response_fields);
|
||||
}
|
||||
|
||||
void APService::statusProto(api_APStatus &proto) {
|
||||
proto.status = static_cast<api_APNetworkStatus>(getAPNetworkStatus());
|
||||
proto.ip_address = static_cast<uint32_t>(WiFi.softAPIP());
|
||||
String mac = WiFi.softAPmacAddress();
|
||||
strncpy(proto.mac_address, mac.c_str(), sizeof(proto.mac_address) - 1);
|
||||
proto.mac_address[sizeof(proto.mac_address) - 1] = '\0';
|
||||
proto.station_num = WiFi.softAPgetStationNum();
|
||||
}
|
||||
|
||||
APNetworkStatus APService::getAPNetworkStatus() {
|
||||
WiFiMode_t currentWiFiMode = WiFi.getMode();
|
||||
bool apActive = currentWiFiMode == WIFI_AP || currentWiFiMode == WIFI_AP_STA;
|
||||
|
||||
@@ -72,6 +72,9 @@ esp_err_t WebServer::httpHandler(httpd_req_t* req) {
|
||||
if (route.getHandler) {
|
||||
return route.getHandler(req);
|
||||
}
|
||||
if (route.rawHandler) {
|
||||
return route.rawHandler(req);
|
||||
}
|
||||
if (route.postHandler) {
|
||||
char* content = nullptr;
|
||||
size_t contentLen = req->content_len;
|
||||
@@ -187,6 +190,7 @@ void WebServer::on(const char* uri, httpd_method_t method, HttpGetHandler handle
|
||||
route.method = method;
|
||||
route.getHandler = handler;
|
||||
route.postHandler = nullptr;
|
||||
route.rawHandler = nullptr;
|
||||
route.isWebsocket = false;
|
||||
routes_.push_back(route);
|
||||
|
||||
@@ -201,6 +205,22 @@ void WebServer::on(const char* uri, httpd_method_t method, HttpPostHandler handl
|
||||
route.method = method;
|
||||
route.getHandler = nullptr;
|
||||
route.postHandler = handler;
|
||||
route.rawHandler = nullptr;
|
||||
route.isWebsocket = false;
|
||||
routes_.push_back(route);
|
||||
|
||||
if (server_) {
|
||||
registerRoute(route);
|
||||
}
|
||||
}
|
||||
|
||||
void NativeServer::onRaw(const char* uri, httpd_method_t method, HttpRawHandler handler) {
|
||||
HttpRoute route;
|
||||
route.uri = uri;
|
||||
route.method = method;
|
||||
route.getHandler = nullptr;
|
||||
route.postHandler = nullptr;
|
||||
route.rawHandler = handler;
|
||||
route.isWebsocket = false;
|
||||
routes_.push_back(route);
|
||||
|
||||
@@ -226,6 +246,7 @@ void WebServer::registerWebsocket(const char* uri) {
|
||||
route.method = HTTP_GET;
|
||||
route.getHandler = nullptr;
|
||||
route.postHandler = nullptr;
|
||||
route.rawHandler = nullptr;
|
||||
route.isWebsocket = true;
|
||||
routes_.push_back(route);
|
||||
|
||||
@@ -300,3 +321,13 @@ esp_err_t WebServer::sendError(httpd_req_t* req, int status, const char* message
|
||||
}
|
||||
|
||||
esp_err_t WebServer::sendOk(httpd_req_t* req) { return sendJson(req, 200, "{\"status\":\"ok\"}"); }
|
||||
|
||||
esp_err_t WebServer::sendProto(httpd_req_t* req, int status, const uint8_t* data, size_t len) {
|
||||
httpd_resp_set_status(req, status == 200 ? "200 OK"
|
||||
: status == 400 ? "400 Bad Request"
|
||||
: status == 404 ? "404 Not Found"
|
||||
: status == 500 ? "500 Internal Server Error"
|
||||
: "200 OK");
|
||||
httpd_resp_set_type(req, "application/x-protobuf");
|
||||
return httpd_resp_send(req, (const char*)data, len);
|
||||
}
|
||||
|
||||
+6
-6
@@ -73,12 +73,12 @@ void setupServer() {
|
||||
server.on("/api/wifi/networks", HTTP_GET, [&](httpd_req_t *request) { return wifiService.getNetworks(request); });
|
||||
server.on("/api/wifi/sta/status", HTTP_GET,
|
||||
[&](httpd_req_t *request) { return wifiService.getNetworkStatus(request); });
|
||||
|
||||
server.on("/api/ap/status", HTTP_GET, [&](httpd_req_t *request) { return apService.getStatus(request); });
|
||||
server.on("/api/ap/settings", HTTP_GET, [&](httpd_req_t *request) { return apService.endpoint.getState(request); });
|
||||
server.on("/api/ap/settings", HTTP_POST, [&](httpd_req_t *request, JsonVariant &json) {
|
||||
return apService.endpoint.handleStateUpdate(request, json);
|
||||
});
|
||||
|
||||
server.on("/api/ap/status", HTTP_GET, [&](httpd_req_t *request) { return apService.getStatusProto(request); });
|
||||
server.on("/api/ap/settings", HTTP_GET,
|
||||
[&](httpd_req_t *request) { return apService.protoEndpoint.getState(request); });
|
||||
server.onRaw("/api/ap/settings", HTTP_POST,
|
||||
[&](httpd_req_t *request) { return apService.protoEndpoint.handleStateUpdate(request); });
|
||||
|
||||
server.on("/api/peripherals", HTTP_GET,
|
||||
[&](httpd_req_t *request) { return peripherals.endpoint.getState(request); });
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
api.APSettings.ssid max_size:33
|
||||
api.APSettings.password max_size:64
|
||||
|
||||
api.APStatus.mac_address max_size:18
|
||||
@@ -0,0 +1,70 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package api;
|
||||
|
||||
// =============================================================================
|
||||
// AP (Access Point) Settings - shared data types
|
||||
// =============================================================================
|
||||
|
||||
enum APProvisionMode {
|
||||
AP_MODE_ALWAYS = 0;
|
||||
AP_MODE_DISCONNECTED = 1;
|
||||
AP_MODE_NEVER = 2;
|
||||
}
|
||||
|
||||
enum APNetworkStatus {
|
||||
AP_ACTIVE = 0;
|
||||
AP_INACTIVE = 1;
|
||||
AP_LINGERING = 2;
|
||||
}
|
||||
|
||||
message APSettings {
|
||||
APProvisionMode provision_mode = 1;
|
||||
string ssid = 2;
|
||||
string password = 3;
|
||||
uint32 channel = 4;
|
||||
bool ssid_hidden = 5;
|
||||
uint32 max_clients = 6;
|
||||
uint32 local_ip = 7;
|
||||
uint32 gateway_ip = 8;
|
||||
uint32 subnet_mask = 9;
|
||||
}
|
||||
|
||||
message APStatus {
|
||||
APNetworkStatus status = 1;
|
||||
uint32 ip_address = 2;
|
||||
string mac_address = 3;
|
||||
uint32 station_num = 4;
|
||||
}
|
||||
|
||||
// Empty request types for GET-like operations
|
||||
message APSettingsRequest {}
|
||||
message APStatusRequest {}
|
||||
|
||||
// =============================================================================
|
||||
// REST API wrappers - used by HTTP endpoints
|
||||
// =============================================================================
|
||||
|
||||
// Request wrapper for REST endpoints
|
||||
message Request {
|
||||
oneof payload {
|
||||
APSettings ap_settings = 10;
|
||||
APSettingsRequest ap_settings_request = 11;
|
||||
APStatusRequest ap_status_request = 12;
|
||||
// Future types:
|
||||
// MDNSSettings mdns_settings = 20;
|
||||
}
|
||||
}
|
||||
|
||||
// Response wrapper for REST endpoints
|
||||
message Response {
|
||||
uint32 status_code = 1;
|
||||
|
||||
oneof payload {
|
||||
APSettings ap_settings = 10;
|
||||
APStatus ap_status = 11;
|
||||
// Future types:
|
||||
// MDNSSettings mdns_settings = 20;
|
||||
// MDNSStatus mdns_status = 21;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user