Updated mdns to use protobufs (completely untested)
This commit is contained in:
@@ -91,35 +91,6 @@ export type ServoConfiguration = {
|
|||||||
servos: Servo[]
|
servos: Servo[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MDNSServiceQuery {
|
|
||||||
services: MDNSServiceItem[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface MDNSServiceItem {
|
|
||||||
ip: string
|
|
||||||
port: number
|
|
||||||
name: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface MDNSService {
|
|
||||||
service: string
|
|
||||||
protocol: string
|
|
||||||
port: number
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface MDNSTxtRecord {
|
|
||||||
key: string
|
|
||||||
value: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface MDNSStatus {
|
|
||||||
started: boolean
|
|
||||||
hostname: string
|
|
||||||
instance: string
|
|
||||||
services: MDNSService[]
|
|
||||||
global_txt_records: MDNSTxtRecord[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Result {
|
export interface Result {
|
||||||
success: boolean
|
success: boolean
|
||||||
error?: string
|
error?: string
|
||||||
|
|||||||
@@ -6,33 +6,46 @@
|
|||||||
import StatusItem from '$lib/components/StatusItem.svelte'
|
import StatusItem from '$lib/components/StatusItem.svelte'
|
||||||
import { cubicOut } from 'svelte/easing'
|
import { cubicOut } from 'svelte/easing'
|
||||||
import { slide } from 'svelte/transition'
|
import { slide } from 'svelte/transition'
|
||||||
import type { MDNSStatus, MDNSServiceItem, MDNSServiceQuery } from '$lib/types/models'
|
import {
|
||||||
|
type MDNSStatus,
|
||||||
|
type MDNSQueryResult,
|
||||||
|
Request,
|
||||||
|
type Response as ProtoResponse
|
||||||
|
} from '$lib/platform_shared/api'
|
||||||
import { compareIp } from '$lib/utilities'
|
import { compareIp } from '$lib/utilities'
|
||||||
|
|
||||||
let mdnsStatus: MDNSStatus | undefined = $state()
|
let mdnsStatus = $state<MDNSStatus | undefined>()
|
||||||
let services: MDNSServiceItem[] = $state([])
|
let services = $state<MDNSQueryResult[]>([])
|
||||||
let isLoading = $state(false)
|
let isLoading = $state(false)
|
||||||
|
|
||||||
const getMDNSStatus = async () => {
|
const getMDNSStatus = async () => {
|
||||||
const result = await api.get<MDNSStatus>('/api/mdns/status')
|
const result = await api.get<ProtoResponse>('/api/mdns/status')
|
||||||
if (result.isErr()) {
|
if (result.isErr()) {
|
||||||
console.error('Error:', result.inner)
|
console.error('Error:', result.inner)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mdnsStatus = result.inner
|
if (result.inner.mdnsStatus) {
|
||||||
|
mdnsStatus = result.inner.mdnsStatus
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const queryMDNSServices = async () => {
|
const queryMDNSServices = async () => {
|
||||||
isLoading = true
|
isLoading = true
|
||||||
const result = await api.post<MDNSServiceQuery>('/api/mdns/query', {
|
const request = Request.create({
|
||||||
|
mdnsQueryRequest: {
|
||||||
service: 'http',
|
service: 'http',
|
||||||
protocol: 'tcp'
|
protocol: 'tcp'
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
const result = await api.post_proto<ProtoResponse>('/api/mdns/query', request)
|
||||||
if (result.isErr()) {
|
if (result.isErr()) {
|
||||||
console.error('Error:', result.inner)
|
console.error('Error:', result.inner)
|
||||||
|
isLoading = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
services = result.inner.services.sort((a, b) => compareIp(a.ip, b.ip))
|
if (result.inner.mdnsQueryResponse) {
|
||||||
|
services = result.inner.mdnsQueryResponse.services.sort((a, b) => compareIp(a.ip, b.ip))
|
||||||
|
}
|
||||||
isLoading = false
|
isLoading = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,13 +8,8 @@
|
|||||||
|
|
||||||
#define ESP_FS LittleFS
|
#define ESP_FS LittleFS
|
||||||
|
|
||||||
#define AP_SETTINGS_FILE "/config/apSettings.json"
|
|
||||||
#define CAMERA_SETTINGS_FILE "/config/cameraSettings.json"
|
|
||||||
#define FS_CONFIG_DIRECTORY "/config"
|
#define FS_CONFIG_DIRECTORY "/config"
|
||||||
#define DEVICE_CONFIG_FILE "/config/peripheral.json"
|
#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 {
|
namespace FileSystem {
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <esp_http_server.h>
|
#include <esp_http_server.h>
|
||||||
#include <ArduinoJson.h>
|
|
||||||
#include <ESPmDNS.h>
|
#include <ESPmDNS.h>
|
||||||
#include <template/stateful_service.h>
|
#include <template/stateful_service.h>
|
||||||
#include <template/stateful_endpoint.h>
|
#include <template/stateful_proto_endpoint.h>
|
||||||
#include <template/stateful_persistence.h>
|
#include <template/stateful_persistence_pb.h>
|
||||||
#include <settings/mdns_settings.h>
|
#include <settings/mdns_settings.h>
|
||||||
#include <utils/timing.h>
|
#include <utils/timing.h>
|
||||||
#include <string>
|
|
||||||
|
#define MDNS_SETTINGS_FILE "/config/mdnsSettings.pb"
|
||||||
|
|
||||||
class MDNSService : public StatefulService<MDNSSettings> {
|
class MDNSService : public StatefulService<MDNSSettings> {
|
||||||
private:
|
private:
|
||||||
FSPersistence<MDNSSettings> _persistence;
|
FSPersistencePB<MDNSSettings> _persistence;
|
||||||
bool _started {false};
|
bool _started {false};
|
||||||
|
|
||||||
void reconfigureMDNS();
|
void reconfigureMDNS();
|
||||||
@@ -27,9 +27,7 @@ class MDNSService : public StatefulService<MDNSSettings> {
|
|||||||
void begin();
|
void begin();
|
||||||
|
|
||||||
esp_err_t getStatus(httpd_req_t *request);
|
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);
|
StatefulProtoEndpoint<MDNSSettings, api_MDNSSettings> protoEndpoint;
|
||||||
|
|
||||||
StatefulHttpEndpoint<MDNSSettings> endpoint;
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <ArduinoJson.h>
|
|
||||||
#include <template/state_result.h>
|
#include <template/state_result.h>
|
||||||
#include <filesystem.h>
|
#include <platform_shared/api.pb.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
#ifndef FACTORY_MDNS_HOSTNAME
|
#ifndef FACTORY_MDNS_HOSTNAME
|
||||||
#define FACTORY_MDNS_HOSTNAME "esp32"
|
#define FACTORY_MDNS_HOSTNAME "esp32"
|
||||||
@@ -13,126 +13,45 @@
|
|||||||
#define FACTORY_MDNS_INSTANCE "ESP32 Device"
|
#define FACTORY_MDNS_INSTANCE "ESP32 Device"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct {
|
// Use proto types directly
|
||||||
std::string key;
|
using MDNSTxtRecord = api_MDNSTxtRecord;
|
||||||
std::string value;
|
using MDNSServiceDef = api_MDNSServiceDef;
|
||||||
|
using MDNSSettings = api_MDNSSettings;
|
||||||
|
using MDNSStatus = api_MDNSStatus;
|
||||||
|
|
||||||
void serialize(JsonVariant &json) const {
|
// Default factory settings
|
||||||
json["key"] = key.c_str();
|
inline MDNSSettings MDNSSettings_defaults() {
|
||||||
json["value"] = value.c_str();
|
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);
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
return settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool deserialize(const JsonVariant &json) {
|
// Proto read/update are identity functions since type is the same
|
||||||
key = json["key"].as<std::string>();
|
inline void MDNSSettings_read(const MDNSSettings& settings, MDNSSettings& proto) {
|
||||||
value = json["value"].as<std::string>();
|
proto = settings;
|
||||||
|
|
||||||
return key.length() > 0;
|
|
||||||
}
|
|
||||||
} mdns_txt_record_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
std::string service;
|
|
||||||
std::string protocol;
|
|
||||||
uint16_t port;
|
|
||||||
std::vector<mdns_txt_record_t> txtRecords;
|
|
||||||
|
|
||||||
void serialize(JsonVariant &json) const {
|
|
||||||
json["service"] = service.c_str();
|
|
||||||
json["protocol"] = protocol.c_str();
|
|
||||||
json["port"] = port;
|
|
||||||
|
|
||||||
if (txtRecords.size() > 0) {
|
|
||||||
JsonArray txtArray = json["txt_records"].to<JsonArray>();
|
|
||||||
for (const auto &txt : txtRecords) {
|
|
||||||
JsonVariant txtObj = txtArray.add<JsonVariant>();
|
|
||||||
txt.serialize(txtObj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline StateUpdateResult MDNSSettings_update(const MDNSSettings& proto, MDNSSettings& settings) {
|
||||||
|
settings = proto;
|
||||||
return StateUpdateResult::CHANGED;
|
return StateUpdateResult::CHANGED;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
+9
-7
@@ -82,7 +82,7 @@ void setupServer() {
|
|||||||
return apService.protoEndpoint.handleStateUpdate(request, protoReq);
|
return apService.protoEndpoint.handleStateUpdate(request, protoReq);
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: REMAKE TO PROTO
|
// TODO: REMAKE TO PROTO - note: these are unused?
|
||||||
server.on("/api/peripherals", HTTP_GET,
|
server.on("/api/peripherals", HTTP_GET,
|
||||||
[&](httpd_req_t *request) { return peripherals.endpoint.getState(request); });
|
[&](httpd_req_t *request) { return peripherals.endpoint.getState(request); });
|
||||||
server.on("/api/peripherals", HTTP_POST, [&](httpd_req_t *request, JsonVariant &json) {
|
server.on("/api/peripherals", HTTP_POST, [&](httpd_req_t *request, JsonVariant &json) {
|
||||||
@@ -90,14 +90,16 @@ void setupServer() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
#if FT_ENABLED(USE_MDNS)
|
#if FT_ENABLED(USE_MDNS)
|
||||||
// TODO: REMAKE TO PROTO
|
server.on("/api/mdns/settings", HTTP_GET, [&](httpd_req_t *request) { return mdnsService.protoEndpoint.getState(request); });
|
||||||
server.on("/api/mdns", HTTP_GET, [&](httpd_req_t *request) { return mdnsService.endpoint.getState(request); });
|
server.onProto("/api/mdns/settings", HTTP_POST,
|
||||||
server.on("/api/mdns", HTTP_POST, [&](httpd_req_t *request, JsonVariant &json) {
|
[&](httpd_req_t *request, api_Request *protoReq) {
|
||||||
return mdnsService.endpoint.handleStateUpdate(request, json);
|
return mdnsService.protoEndpoint.handleStateUpdate(request, protoReq);
|
||||||
});
|
});
|
||||||
server.on("/api/mdns/status", HTTP_GET, [&](httpd_req_t *request) { return mdnsService.getStatus(request); });
|
server.on("/api/mdns/status", HTTP_GET, [&](httpd_req_t *request) { return mdnsService.getStatus(request); });
|
||||||
server.on("/api/mdns/query", HTTP_POST,
|
server.onProto("/api/mdns/query", HTTP_POST,
|
||||||
[&](httpd_req_t *request, JsonVariant &json) { return mdnsService.queryServices(request, json); });
|
[&](httpd_req_t *request, api_Request *protoReq) {
|
||||||
|
return mdnsService.queryServices(request, protoReq);
|
||||||
|
});
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
server.on("/api/config/*", HTTP_GET, [](httpd_req_t *request) { return FileSystem::getConfigFile(request); });
|
server.on("/api/config/*", HTTP_GET, [](httpd_req_t *request) { return FileSystem::getConfigFile(request); });
|
||||||
|
|||||||
+57
-34
@@ -4,8 +4,12 @@
|
|||||||
static const char *TAG = "MDNSService";
|
static const char *TAG = "MDNSService";
|
||||||
|
|
||||||
MDNSService::MDNSService()
|
MDNSService::MDNSService()
|
||||||
: _persistence(MDNSSettings::read, MDNSSettings::update, this, MDNS_SETTINGS_FILE),
|
: protoEndpoint(MDNSSettings_read, MDNSSettings_update, this,
|
||||||
endpoint(MDNSSettings::read, MDNSSettings::update, this) {
|
API_REQUEST_EXTRACTOR(mdns_settings, api_MDNSSettings),
|
||||||
|
API_RESPONSE_ASSIGNER(mdns_settings, api_MDNSSettings)),
|
||||||
|
_persistence(MDNSSettings_read, MDNSSettings_update, this,
|
||||||
|
MDNS_SETTINGS_FILE, api_MDNSSettings_fields, api_MDNSSettings_size,
|
||||||
|
MDNSSettings_defaults()) {
|
||||||
addUpdateHandler([&](const std::string &originId) { reconfigureMDNS(); }, false);
|
addUpdateHandler([&](const std::string &originId) { reconfigureMDNS(); }, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,15 +32,15 @@ void MDNSService::reconfigureMDNS() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MDNSService::startMDNS() {
|
void MDNSService::startMDNS() {
|
||||||
ESP_LOGV(TAG, "Starting MDNS with hostname: %s", state().hostname.c_str());
|
ESP_LOGV(TAG, "Starting MDNS with hostname: %s", state().hostname);
|
||||||
|
|
||||||
if (MDNS.begin(state().hostname.c_str())) {
|
if (MDNS.begin(state().hostname)) {
|
||||||
_started = true;
|
_started = true;
|
||||||
MDNS.setInstanceName(state().instance.c_str());
|
MDNS.setInstanceName(state().instance);
|
||||||
|
|
||||||
addServices();
|
addServices();
|
||||||
|
|
||||||
ESP_LOGI(TAG, "MDNS started successfully with hostname: %s", state().hostname.c_str());
|
ESP_LOGI(TAG, "MDNS started successfully with hostname: %s", state().hostname);
|
||||||
} else {
|
} else {
|
||||||
_started = false;
|
_started = false;
|
||||||
ESP_LOGE(TAG, "Failed to start MDNS");
|
ESP_LOGE(TAG, "Failed to start MDNS");
|
||||||
@@ -50,52 +54,71 @@ void MDNSService::stopMDNS() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MDNSService::addServices() {
|
void MDNSService::addServices() {
|
||||||
for (const auto &service : state().services) {
|
for (size_t i = 0; i < state().services_count; i++) {
|
||||||
MDNS.addService(service.service.c_str(), service.protocol.c_str(), service.port);
|
const auto &service = state().services[i];
|
||||||
|
MDNS.addService(service.service, service.protocol, service.port);
|
||||||
|
|
||||||
for (const auto &txt : service.txtRecords) {
|
for (size_t j = 0; j < service.txt_records_count; j++) {
|
||||||
MDNS.addServiceTxt(service.service.c_str(), service.protocol.c_str(), txt.key.c_str(), txt.value.c_str());
|
const auto &txt = service.txt_records[j];
|
||||||
|
MDNS.addServiceTxt(service.service, service.protocol, txt.key, txt.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto &txt : state().globalTxtRecords) {
|
for (size_t i = 0; i < state().global_txt_records_count; i++) {
|
||||||
for (const auto &service : state().services) {
|
const auto &txt = state().global_txt_records[i];
|
||||||
MDNS.addServiceTxt(service.service.c_str(), service.protocol.c_str(), txt.key.c_str(), txt.value.c_str());
|
for (size_t j = 0; j < state().services_count; j++) {
|
||||||
|
const auto &service = state().services[j];
|
||||||
|
MDNS.addServiceTxt(service.service, service.protocol, txt.key, txt.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t MDNSService::getStatus(httpd_req_t *request) {
|
esp_err_t MDNSService::getStatus(httpd_req_t *request) {
|
||||||
JsonDocument doc;
|
api_Response response = api_Response_init_zero;
|
||||||
JsonVariant root = doc.to<JsonVariant>();
|
response.which_payload = api_Response_mdns_status_tag;
|
||||||
getStatus(root);
|
|
||||||
return WebServer::sendJson(request, 200, doc);
|
MDNSStatus &status = response.payload.mdns_status;
|
||||||
|
status.started = _started;
|
||||||
|
strncpy(status.hostname, state().hostname, sizeof(status.hostname) - 1);
|
||||||
|
strncpy(status.instance, state().instance, sizeof(status.instance) - 1);
|
||||||
|
|
||||||
|
status.services_count = state().services_count;
|
||||||
|
for (size_t i = 0; i < state().services_count; i++) {
|
||||||
|
status.services[i] = state().services[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
void MDNSService::getStatus(JsonVariant &root) {
|
status.global_txt_records_count = state().global_txt_records_count;
|
||||||
state().read(state(), root);
|
for (size_t i = 0; i < state().global_txt_records_count; i++) {
|
||||||
root["started"] = _started;
|
status.global_txt_records[i] = state().global_txt_records[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t MDNSService::queryServices(httpd_req_t *request, JsonVariant &json) {
|
return WebServer::sendProto(request, 200, api_Response_fields, &response, api_Response_size);
|
||||||
std::string service = json["service"].as<std::string>();
|
}
|
||||||
std::string proto = json["protocol"].as<std::string>();
|
|
||||||
|
|
||||||
JsonDocument doc;
|
esp_err_t MDNSService::queryServices(httpd_req_t *request, api_Request *protoReq) {
|
||||||
JsonVariant root = doc.to<JsonVariant>();
|
if (protoReq->which_payload != api_Request_mdns_query_request_tag) {
|
||||||
|
return WebServer::sendError(request, 400, "Invalid request payload");
|
||||||
|
}
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Querying for service: %s, protocol: %s", service.c_str(), proto.c_str());
|
const api_MDNSQueryRequest &queryReq = protoReq->payload.mdns_query_request;
|
||||||
|
ESP_LOGI(TAG, "Querying for service: %s, protocol: %s", queryReq.service, queryReq.protocol);
|
||||||
|
|
||||||
int n = MDNS.queryService(service.c_str(), proto.c_str());
|
int n = MDNS.queryService(queryReq.service, queryReq.protocol);
|
||||||
ESP_LOGI(TAG, "Found %d services", n);
|
ESP_LOGI(TAG, "Found %d services", n);
|
||||||
|
|
||||||
JsonArray servicesArray = root["services"].to<JsonArray>();
|
api_Response response = api_Response_init_zero;
|
||||||
for (int i = 0; i < n; i++) {
|
response.which_payload = api_Response_mdns_query_response_tag;
|
||||||
JsonVariant serviceObj = servicesArray.add<JsonVariant>();
|
api_MDNSQueryResponse &queryResp = response.payload.mdns_query_response;
|
||||||
serviceObj["name"] = MDNS.hostname(i);
|
|
||||||
serviceObj["ip"] = MDNS.IP(i);
|
// Limit to max_count from options file (16)
|
||||||
serviceObj["port"] = MDNS.port(i);
|
size_t count = (n > 16) ? 16 : static_cast<size_t>(n);
|
||||||
|
queryResp.services_count = count;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < count; i++) {
|
||||||
|
strncpy(queryResp.services[i].name, MDNS.hostname(i).c_str(), sizeof(queryResp.services[i].name) - 1);
|
||||||
|
strncpy(queryResp.services[i].ip, MDNS.IP(i).toString().c_str(), sizeof(queryResp.services[i].ip) - 1);
|
||||||
|
queryResp.services[i].port = MDNS.port(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
return WebServer::sendJson(request, 200, doc);
|
return WebServer::sendProto(request, 200, api_Response_fields, &response, api_Response_size);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,3 +23,28 @@ api.WifiNetwork.ssid max_size:33
|
|||||||
api.WifiNetwork.password max_size:65
|
api.WifiNetwork.password max_size:65
|
||||||
api.WifiSettings.hostname max_size:33
|
api.WifiSettings.hostname max_size:33
|
||||||
api.WifiSettings.wifi_networks max_count:5
|
api.WifiSettings.wifi_networks max_count:5
|
||||||
|
|
||||||
|
api.MDNSTxtRecord.key max_size:32
|
||||||
|
api.MDNSTxtRecord.value max_size:64
|
||||||
|
|
||||||
|
api.MDNSServiceDef.service max_size:16
|
||||||
|
api.MDNSServiceDef.protocol max_size:8
|
||||||
|
api.MDNSServiceDef.txt_records max_count:4
|
||||||
|
|
||||||
|
api.MDNSSettings.hostname max_size:33
|
||||||
|
api.MDNSSettings.instance max_size:64
|
||||||
|
api.MDNSSettings.services max_count:4
|
||||||
|
api.MDNSSettings.global_txt_records max_count:4
|
||||||
|
|
||||||
|
api.MDNSStatus.hostname max_size:33
|
||||||
|
api.MDNSStatus.instance max_size:64
|
||||||
|
api.MDNSStatus.services max_count:4
|
||||||
|
api.MDNSStatus.global_txt_records max_count:4
|
||||||
|
|
||||||
|
api.MDNSQueryRequest.service max_size:16
|
||||||
|
api.MDNSQueryRequest.protocol max_size:8
|
||||||
|
|
||||||
|
api.MDNSQueryResult.name max_size:64
|
||||||
|
api.MDNSQueryResult.ip max_size:46
|
||||||
|
|
||||||
|
api.MDNSQueryResponse.services max_count:16
|
||||||
|
|||||||
@@ -125,6 +125,55 @@ message CameraSettings {
|
|||||||
|
|
||||||
message CameraSettingsRequest {}
|
message CameraSettingsRequest {}
|
||||||
|
|
||||||
|
// =============================================================================
|
||||||
|
// mDNS Settings - shared data types
|
||||||
|
// =============================================================================
|
||||||
|
|
||||||
|
message MDNSTxtRecord {
|
||||||
|
string key = 1;
|
||||||
|
string value = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message MDNSServiceDef {
|
||||||
|
string service = 1;
|
||||||
|
string protocol = 2;
|
||||||
|
uint32 port = 3;
|
||||||
|
repeated MDNSTxtRecord txt_records = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
message MDNSSettings {
|
||||||
|
string hostname = 1;
|
||||||
|
string instance = 2;
|
||||||
|
repeated MDNSServiceDef services = 3;
|
||||||
|
repeated MDNSTxtRecord global_txt_records = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
message MDNSStatus {
|
||||||
|
bool started = 1;
|
||||||
|
string hostname = 2;
|
||||||
|
string instance = 3;
|
||||||
|
repeated MDNSServiceDef services = 4;
|
||||||
|
repeated MDNSTxtRecord global_txt_records = 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
message MDNSSettingsRequest {}
|
||||||
|
message MDNSStatusRequest {}
|
||||||
|
|
||||||
|
message MDNSQueryRequest {
|
||||||
|
string service = 1;
|
||||||
|
string protocol = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message MDNSQueryResult {
|
||||||
|
string name = 1;
|
||||||
|
string ip = 2;
|
||||||
|
uint32 port = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
message MDNSQueryResponse {
|
||||||
|
repeated MDNSQueryResult services = 1;
|
||||||
|
}
|
||||||
|
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
// File System - shared data types
|
// File System - shared data types
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
@@ -175,6 +224,10 @@ message Request {
|
|||||||
WifiSettingsRequest wifi_settings_request = 41;
|
WifiSettingsRequest wifi_settings_request = 41;
|
||||||
CameraSettings camera_settings = 50;
|
CameraSettings camera_settings = 50;
|
||||||
CameraSettingsRequest camera_settings_request = 51;
|
CameraSettingsRequest camera_settings_request = 51;
|
||||||
|
MDNSSettings mdns_settings = 60;
|
||||||
|
MDNSSettingsRequest mdns_settings_request = 61;
|
||||||
|
MDNSStatusRequest mdns_status_request = 62;
|
||||||
|
MDNSQueryRequest mdns_query_request = 63;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,5 +244,8 @@ message Response {
|
|||||||
FileList file_list = 30;
|
FileList file_list = 30;
|
||||||
WifiSettings wifi_settings = 40;
|
WifiSettings wifi_settings = 40;
|
||||||
CameraSettings camera_settings = 50;
|
CameraSettings camera_settings = 50;
|
||||||
|
MDNSSettings mdns_settings = 60;
|
||||||
|
MDNSStatus mdns_status = 61;
|
||||||
|
MDNSQueryResponse mdns_query_response = 62;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user