Removed JSON from ap settings and service -> preparing to remake servo
This commit is contained in:
@@ -1,14 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#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>
|
||||
#include <WiFi.h>
|
||||
#include "esp_timer.h"
|
||||
#include <string>
|
||||
|
||||
class APService : public StatefulService<APSettings> {
|
||||
public:
|
||||
@@ -19,18 +16,13 @@ class APService : public StatefulService<APSettings> {
|
||||
void loop();
|
||||
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;
|
||||
|
||||
DNSServer *_dnsServer;
|
||||
|
||||
volatile unsigned long _lastManaged;
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <template/state_result.h>
|
||||
#include <platform_shared/api.pb.h>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
|
||||
#include <DNSServer.h>
|
||||
|
||||
#ifndef FACTORY_AP_PROVISION_MODE
|
||||
#define FACTORY_AP_PROVISION_MODE AP_MODE_DISCONNECTED
|
||||
#define FACTORY_AP_PROVISION_MODE api_APProvisionMode_AP_MODE_DISCONNECTED
|
||||
#endif
|
||||
|
||||
#ifndef FACTORY_AP_SSID
|
||||
@@ -45,14 +43,17 @@
|
||||
#define FACTORY_AP_MAX_CLIENTS 4
|
||||
#endif
|
||||
|
||||
#define AP_MODE_ALWAYS 0
|
||||
#define AP_MODE_DISCONNECTED 1
|
||||
#define AP_MODE_NEVER 2
|
||||
#define AP_MODE_ALWAYS api_APProvisionMode_AP_MODE_ALWAYS
|
||||
#define AP_MODE_DISCONNECTED api_APProvisionMode_AP_MODE_DISCONNECTED
|
||||
#define AP_MODE_NEVER api_APProvisionMode_AP_MODE_NEVER
|
||||
|
||||
#define MANAGE_NETWORK_DELAY 10000
|
||||
#define DNS_PORT 53
|
||||
|
||||
enum APNetworkStatus { ACTIVE = 0, INACTIVE, LINGERING };
|
||||
using APNetworkStatus = api_APNetworkStatus;
|
||||
#define ACTIVE api_APNetworkStatus_AP_ACTIVE
|
||||
#define INACTIVE api_APNetworkStatus_AP_INACTIVE
|
||||
#define LINGERING api_APNetworkStatus_AP_LINGERING
|
||||
|
||||
inline uint32_t parseIPv4(const char *str) {
|
||||
IPAddress ip;
|
||||
@@ -60,102 +61,14 @@ inline uint32_t parseIPv4(const char *str) {
|
||||
return (uint32_t)ip;
|
||||
}
|
||||
|
||||
class APSettings {
|
||||
public:
|
||||
uint8_t provisionMode;
|
||||
std::string ssid;
|
||||
std::string password;
|
||||
uint8_t channel;
|
||||
bool ssidHidden;
|
||||
uint8_t maxClients;
|
||||
using APSettings = api_APSettings;
|
||||
|
||||
IPAddress localIP;
|
||||
IPAddress gatewayIP;
|
||||
IPAddress subnetMask;
|
||||
|
||||
bool operator==(const APSettings &settings) const {
|
||||
return provisionMode == settings.provisionMode && ssid == settings.ssid && password == settings.password &&
|
||||
channel == settings.channel && ssidHidden == settings.ssidHidden && maxClients == settings.maxClients &&
|
||||
localIP == settings.localIP && gatewayIP == settings.gatewayIP && subnetMask == settings.subnetMask;
|
||||
}
|
||||
inline void APSettings_read(const APSettings &settings, APSettings &proto) {
|
||||
proto = settings;
|
||||
}
|
||||
|
||||
static void read(APSettings &settings, JsonVariant &root) {
|
||||
root["provision_mode"] = settings.provisionMode;
|
||||
root["ssid"] = settings.ssid.c_str();
|
||||
root["password"] = settings.password.c_str();
|
||||
root["channel"] = settings.channel;
|
||||
root["ssid_hidden"] = settings.ssidHidden;
|
||||
root["max_clients"] = settings.maxClients;
|
||||
root["local_ip"] = (uint32_t)(settings.localIP);
|
||||
root["gateway_ip"] = (uint32_t)(settings.gatewayIP);
|
||||
root["subnet_mask"] = (uint32_t)(settings.subnetMask);
|
||||
}
|
||||
|
||||
static StateUpdateResult update(JsonVariant &root, APSettings &settings) {
|
||||
APSettings newSettings = {};
|
||||
newSettings.provisionMode = root["provision_mode"] | FACTORY_AP_PROVISION_MODE;
|
||||
switch (settings.provisionMode) {
|
||||
case AP_MODE_ALWAYS:
|
||||
case AP_MODE_DISCONNECTED:
|
||||
case AP_MODE_NEVER: break;
|
||||
default: newSettings.provisionMode = AP_MODE_DISCONNECTED;
|
||||
}
|
||||
newSettings.ssid = root["ssid"] | FACTORY_AP_SSID;
|
||||
newSettings.password = root["password"] | FACTORY_AP_PASSWORD;
|
||||
newSettings.channel = root["channel"] | FACTORY_AP_CHANNEL;
|
||||
newSettings.ssidHidden = root["ssid_hidden"] | FACTORY_AP_SSID_HIDDEN;
|
||||
newSettings.maxClients = root["max_clients"] | FACTORY_AP_MAX_CLIENTS;
|
||||
|
||||
newSettings.localIP = IPAddress(root["local_ip"] | parseIPv4(FACTORY_AP_LOCAL_IP));
|
||||
newSettings.gatewayIP = IPAddress(root["gateway_ip"] | parseIPv4(FACTORY_AP_GATEWAY_IP));
|
||||
newSettings.subnetMask = IPAddress(root["subnet_mask"] | parseIPv4(FACTORY_AP_SUBNET_MASK));
|
||||
|
||||
if (newSettings == settings) {
|
||||
return StateUpdateResult::UNCHANGED;
|
||||
}
|
||||
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;
|
||||
}
|
||||
};
|
||||
inline StateUpdateResult APSettings_update(const APSettings &proto, APSettings &settings) {
|
||||
settings = proto;
|
||||
return StateUpdateResult::CHANGED;
|
||||
}
|
||||
+12
-29
@@ -4,31 +4,15 @@
|
||||
static const char *TAG = "APService";
|
||||
|
||||
APService::APService()
|
||||
: endpoint(APSettings::read, APSettings::update, this),
|
||||
protoEndpoint(APSettings::readProto, APSettings::updateProto, this,
|
||||
: protoEndpoint(APSettings_read, APSettings_update, this,
|
||||
API_REQUEST_EXTRACTOR(ap_settings, api_APSettings),
|
||||
API_RESPONSE_ASSIGNER(ap_settings, api_APSettings)),
|
||||
_persistence(APSettings::read, APSettings::update, this, AP_SETTINGS_FILE) {
|
||||
API_RESPONSE_ASSIGNER(ap_settings, api_APSettings)) {
|
||||
addUpdateHandler([&](const std::string &originId) { reconfigureAP(); }, false);
|
||||
}
|
||||
|
||||
APService::~APService() {}
|
||||
|
||||
void APService::begin() { _persistence.readFromFS(); }
|
||||
|
||||
esp_err_t APService::getStatus(httpd_req_t *request) {
|
||||
JsonDocument doc;
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
status(root);
|
||||
return WebServer::sendJson(request, 200, doc);
|
||||
}
|
||||
|
||||
void APService::status(JsonObject &root) {
|
||||
root["status"] = getAPNetworkStatus();
|
||||
root["ip_address"] = (uint32_t)(WiFi.softAPIP());
|
||||
root["mac_address"] = WiFi.softAPmacAddress();
|
||||
root["station_num"] = WiFi.softAPgetStationNum();
|
||||
}
|
||||
void APService::begin() {}
|
||||
|
||||
esp_err_t APService::getStatusProto(httpd_req_t *request) {
|
||||
api_Response res = api_Response_init_zero;
|
||||
@@ -39,7 +23,7 @@ esp_err_t APService::getStatusProto(httpd_req_t *request) {
|
||||
}
|
||||
|
||||
void APService::statusProto(api_APStatus &proto) {
|
||||
proto.status = static_cast<api_APNetworkStatus>(getAPNetworkStatus());
|
||||
proto.status = 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);
|
||||
@@ -50,10 +34,10 @@ void APService::statusProto(api_APStatus &proto) {
|
||||
APNetworkStatus APService::getAPNetworkStatus() {
|
||||
WiFiMode_t currentWiFiMode = WiFi.getMode();
|
||||
bool apActive = currentWiFiMode == WIFI_AP || currentWiFiMode == WIFI_AP_STA;
|
||||
if (apActive && state().provisionMode != AP_MODE_ALWAYS && WiFi.status() == WL_CONNECTED) {
|
||||
return APNetworkStatus::LINGERING;
|
||||
if (apActive && state().provision_mode != AP_MODE_ALWAYS && WiFi.status() == WL_CONNECTED) {
|
||||
return LINGERING;
|
||||
}
|
||||
return apActive ? APNetworkStatus::ACTIVE : APNetworkStatus::INACTIVE;
|
||||
return apActive ? ACTIVE : INACTIVE;
|
||||
}
|
||||
|
||||
void APService::reconfigureAP() {
|
||||
@@ -76,8 +60,8 @@ void APService::loop() {
|
||||
|
||||
void APService::manageAP() {
|
||||
WiFiMode_t currentWiFiMode = WiFi.getMode();
|
||||
if (state().provisionMode == AP_MODE_ALWAYS ||
|
||||
(state().provisionMode == AP_MODE_DISCONNECTED && WiFi.status() != WL_CONNECTED) || _recoveryMode) {
|
||||
if (state().provision_mode == AP_MODE_ALWAYS ||
|
||||
(state().provision_mode == AP_MODE_DISCONNECTED && WiFi.status() != WL_CONNECTED) || _recoveryMode) {
|
||||
if (_reconfigureAp || currentWiFiMode == WIFI_OFF || currentWiFiMode == WIFI_STA) {
|
||||
startAP();
|
||||
}
|
||||
@@ -89,10 +73,9 @@ void APService::manageAP() {
|
||||
}
|
||||
|
||||
void APService::startAP() {
|
||||
ESP_LOGI(TAG, "Starting software access point: %s", state().ssid.c_str());
|
||||
WiFi.softAPConfig(state().localIP, state().gatewayIP, state().subnetMask);
|
||||
WiFi.softAP(state().ssid.c_str(), state().password.c_str(), state().channel, state().ssidHidden,
|
||||
state().maxClients);
|
||||
ESP_LOGI(TAG, "Starting software access point: %s", state().ssid);
|
||||
WiFi.softAPConfig(IPAddress(state().local_ip), IPAddress(state().gateway_ip), IPAddress(state().subnet_mask));
|
||||
WiFi.softAP(state().ssid, state().password, state().channel, state().ssid_hidden, state().max_clients);
|
||||
#if CONFIG_IDF_TARGET_ESP32C3
|
||||
WiFi.setTxPower(WIFI_POWER_8_5dBm);
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user