Updated mdns to use protobufs (completely untested)
This commit is contained in:
@@ -8,13 +8,8 @@
|
||||
|
||||
#define ESP_FS LittleFS
|
||||
|
||||
#define AP_SETTINGS_FILE "/config/apSettings.json"
|
||||
#define CAMERA_SETTINGS_FILE "/config/cameraSettings.json"
|
||||
#define FS_CONFIG_DIRECTORY "/config"
|
||||
#define DEVICE_CONFIG_FILE "/config/peripheral.json"
|
||||
#define WIFI_SETTINGS_FILE "/config/wifiSettings.json"
|
||||
#define SERVO_SETTINGS_FILE "/config/servoSettings.json"
|
||||
#define MDNS_SETTINGS_FILE "/config/mdnsSettings.json"
|
||||
|
||||
namespace FileSystem {
|
||||
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <esp_http_server.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <ESPmDNS.h>
|
||||
#include <template/stateful_service.h>
|
||||
#include <template/stateful_endpoint.h>
|
||||
#include <template/stateful_persistence.h>
|
||||
#include <template/stateful_proto_endpoint.h>
|
||||
#include <template/stateful_persistence_pb.h>
|
||||
#include <settings/mdns_settings.h>
|
||||
#include <utils/timing.h>
|
||||
#include <string>
|
||||
|
||||
#define MDNS_SETTINGS_FILE "/config/mdnsSettings.pb"
|
||||
|
||||
class MDNSService : public StatefulService<MDNSSettings> {
|
||||
private:
|
||||
FSPersistence<MDNSSettings> _persistence;
|
||||
FSPersistencePB<MDNSSettings> _persistence;
|
||||
bool _started {false};
|
||||
|
||||
void reconfigureMDNS();
|
||||
@@ -27,9 +27,7 @@ class MDNSService : public StatefulService<MDNSSettings> {
|
||||
void begin();
|
||||
|
||||
esp_err_t getStatus(httpd_req_t *request);
|
||||
void getStatus(JsonVariant &root);
|
||||
esp_err_t queryServices(httpd_req_t *request, api_Request *protoReq);
|
||||
|
||||
static esp_err_t queryServices(httpd_req_t *request, JsonVariant &json);
|
||||
|
||||
StatefulHttpEndpoint<MDNSSettings> endpoint;
|
||||
StatefulProtoEndpoint<MDNSSettings, api_MDNSSettings> protoEndpoint;
|
||||
};
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <template/state_result.h>
|
||||
#include <filesystem.h>
|
||||
#include <platform_shared/api.pb.h>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
|
||||
#ifndef FACTORY_MDNS_HOSTNAME
|
||||
#define FACTORY_MDNS_HOSTNAME "esp32"
|
||||
@@ -13,126 +13,45 @@
|
||||
#define FACTORY_MDNS_INSTANCE "ESP32 Device"
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
std::string key;
|
||||
std::string value;
|
||||
// Use proto types directly
|
||||
using MDNSTxtRecord = api_MDNSTxtRecord;
|
||||
using MDNSServiceDef = api_MDNSServiceDef;
|
||||
using MDNSSettings = api_MDNSSettings;
|
||||
using MDNSStatus = api_MDNSStatus;
|
||||
|
||||
void serialize(JsonVariant &json) const {
|
||||
json["key"] = key.c_str();
|
||||
json["value"] = value.c_str();
|
||||
}
|
||||
// Default factory settings
|
||||
inline MDNSSettings MDNSSettings_defaults() {
|
||||
MDNSSettings settings = api_MDNSSettings_init_zero;
|
||||
strncpy(settings.hostname, FACTORY_MDNS_HOSTNAME, sizeof(settings.hostname) - 1);
|
||||
strncpy(settings.instance, FACTORY_MDNS_INSTANCE, sizeof(settings.instance) - 1);
|
||||
|
||||
bool deserialize(const JsonVariant &json) {
|
||||
key = json["key"].as<std::string>();
|
||||
value = json["value"].as<std::string>();
|
||||
// Default HTTP service
|
||||
settings.services_count = 2;
|
||||
strncpy(settings.services[0].service, "http", sizeof(settings.services[0].service) - 1);
|
||||
strncpy(settings.services[0].protocol, "tcp", sizeof(settings.services[0].protocol) - 1);
|
||||
settings.services[0].port = 80;
|
||||
settings.services[0].txt_records_count = 0;
|
||||
|
||||
return key.length() > 0;
|
||||
}
|
||||
} mdns_txt_record_t;
|
||||
// Default WS service
|
||||
strncpy(settings.services[1].service, "ws", sizeof(settings.services[1].service) - 1);
|
||||
strncpy(settings.services[1].protocol, "tcp", sizeof(settings.services[1].protocol) - 1);
|
||||
settings.services[1].port = 80;
|
||||
settings.services[1].txt_records_count = 0;
|
||||
|
||||
typedef struct {
|
||||
std::string service;
|
||||
std::string protocol;
|
||||
uint16_t port;
|
||||
std::vector<mdns_txt_record_t> txtRecords;
|
||||
// Default global txt record
|
||||
settings.global_txt_records_count = 1;
|
||||
strncpy(settings.global_txt_records[0].key, "Firmware Version", sizeof(settings.global_txt_records[0].key) - 1);
|
||||
strncpy(settings.global_txt_records[0].value, APP_VERSION, sizeof(settings.global_txt_records[0].value) - 1);
|
||||
|
||||
void serialize(JsonVariant &json) const {
|
||||
json["service"] = service.c_str();
|
||||
json["protocol"] = protocol.c_str();
|
||||
json["port"] = port;
|
||||
return settings;
|
||||
}
|
||||
|
||||
if (txtRecords.size() > 0) {
|
||||
JsonArray txtArray = json["txt_records"].to<JsonArray>();
|
||||
for (const auto &txt : txtRecords) {
|
||||
JsonVariant txtObj = txtArray.add<JsonVariant>();
|
||||
txt.serialize(txtObj);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Proto read/update are identity functions since type is the same
|
||||
inline void MDNSSettings_read(const MDNSSettings& settings, MDNSSettings& proto) {
|
||||
proto = settings;
|
||||
}
|
||||
|
||||
bool deserialize(const JsonVariant &json) {
|
||||
service = json["service"].as<std::string>();
|
||||
protocol = json["protocol"].as<std::string>();
|
||||
port = json["port"] | 0;
|
||||
|
||||
txtRecords.clear();
|
||||
if (json["txt_records"].is<JsonArray>()) {
|
||||
JsonArray txtArray = json["txt_records"];
|
||||
for (JsonVariant txtObj : txtArray) {
|
||||
mdns_txt_record_t txt;
|
||||
if (txt.deserialize(txtObj)) {
|
||||
txtRecords.push_back(txt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return service.length() > 0 && protocol.length() > 0 && port > 0;
|
||||
}
|
||||
} mdns_service_t;
|
||||
|
||||
class MDNSSettings {
|
||||
public:
|
||||
std::string hostname;
|
||||
std::string instance;
|
||||
std::vector<mdns_service_t> services;
|
||||
std::vector<mdns_txt_record_t> globalTxtRecords;
|
||||
|
||||
static void read(MDNSSettings &settings, JsonVariant &root) {
|
||||
root["hostname"] = settings.hostname.c_str();
|
||||
root["instance"] = settings.instance.c_str();
|
||||
|
||||
JsonArray servicesArray = root["services"].to<JsonArray>();
|
||||
for (const auto &service : settings.services) {
|
||||
JsonVariant serviceObj = servicesArray.add<JsonVariant>();
|
||||
service.serialize(serviceObj);
|
||||
}
|
||||
|
||||
JsonArray txtArray = root["global_txt_records"].to<JsonArray>();
|
||||
for (const auto &txt : settings.globalTxtRecords) {
|
||||
JsonVariant txtObj = txtArray.add<JsonVariant>();
|
||||
txt.serialize(txtObj);
|
||||
}
|
||||
}
|
||||
|
||||
static StateUpdateResult update(JsonVariant &root, MDNSSettings &settings) {
|
||||
settings.hostname = root["hostname"] | FACTORY_MDNS_HOSTNAME;
|
||||
settings.instance = root["instance"] | FACTORY_MDNS_INSTANCE;
|
||||
|
||||
settings.services.clear();
|
||||
if (root["services"].is<JsonArray>()) {
|
||||
JsonArray servicesArray = root["services"];
|
||||
for (JsonVariant serviceObj : servicesArray) {
|
||||
mdns_service_t service;
|
||||
if (service.deserialize(serviceObj)) {
|
||||
settings.services.push_back(service);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (settings.services.empty()) {
|
||||
mdns_service_t httpService = {.service = "http", .protocol = "tcp", .port = 80, .txtRecords = {}};
|
||||
settings.services.push_back(httpService);
|
||||
|
||||
mdns_service_t wsService = {.service = "ws", .protocol = "tcp", .port = 80, .txtRecords = {}};
|
||||
settings.services.push_back(wsService);
|
||||
}
|
||||
|
||||
settings.globalTxtRecords.clear();
|
||||
if (root["global_txt_records"].is<JsonArray>()) {
|
||||
JsonArray txtArray = root["global_txt_records"];
|
||||
for (JsonVariant txtObj : txtArray) {
|
||||
mdns_txt_record_t txt;
|
||||
if (txt.deserialize(txtObj)) {
|
||||
settings.globalTxtRecords.push_back(txt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (settings.globalTxtRecords.empty()) {
|
||||
mdns_txt_record_t firmwareVersion = {.key = "Firmware Version", .value = APP_VERSION};
|
||||
settings.globalTxtRecords.push_back(firmwareVersion);
|
||||
}
|
||||
|
||||
return StateUpdateResult::CHANGED;
|
||||
}
|
||||
};
|
||||
inline StateUpdateResult MDNSSettings_update(const MDNSSettings& proto, MDNSSettings& settings) {
|
||||
settings = proto;
|
||||
return StateUpdateResult::CHANGED;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user