feat(gateway): enhance GatewayNetworkService with HTTP and UDP support, add status LED control
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -4,14 +4,19 @@
|
||||
#include "gateway_runtime.hpp"
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_netif_ip_addr.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "lwip/inet.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace gateway {
|
||||
@@ -19,6 +24,7 @@ namespace gateway {
|
||||
namespace {
|
||||
|
||||
constexpr const char* kTag = "gateway_network";
|
||||
constexpr const char* kSetupApSsid = "LAMMIN_Gateway";
|
||||
constexpr size_t kUdpBufferSize = 256;
|
||||
|
||||
class LockGuard {
|
||||
@@ -73,6 +79,13 @@ std::vector<uint8_t> DecodeHex(std::string_view hex) {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
std::string MacToHex(const uint8_t mac[6]) {
|
||||
char out[13] = {0};
|
||||
std::snprintf(out, sizeof(out), "%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3],
|
||||
mac[4], mac[5]);
|
||||
return std::string(out);
|
||||
}
|
||||
|
||||
std::string PrintJson(cJSON* node) {
|
||||
if (node == nullptr) {
|
||||
return {};
|
||||
@@ -105,6 +118,16 @@ esp_err_t ReadRequestBody(httpd_req_t* req, std::string& body) {
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t RegisterUri(httpd_handle_t server, const char* uri, httpd_method_t method,
|
||||
esp_err_t (*handler)(httpd_req_t*), void* user_ctx) {
|
||||
httpd_uri_t route = {};
|
||||
route.uri = uri;
|
||||
route.method = method;
|
||||
route.handler = handler;
|
||||
route.user_ctx = user_ctx;
|
||||
return httpd_register_uri_handler(server, &route);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
GatewayNetworkService::GatewayNetworkService(GatewayController& controller,
|
||||
@@ -123,8 +146,21 @@ esp_err_t GatewayNetworkService::start() {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (config_.wifi_enabled) {
|
||||
err = startWifi();
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
err = configureStatusLed();
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
controller_.addNotificationSink(
|
||||
[this](const std::vector<uint8_t>& frame) { handleGatewayNotification(frame); });
|
||||
controller_.addWifiStateSink([this](uint8_t mode) { handleWifiControl(mode); });
|
||||
|
||||
if (config_.http_enabled) {
|
||||
err = startHttpServer();
|
||||
@@ -162,6 +198,162 @@ esp_err_t GatewayNetworkService::ensureNetworkStack() {
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t GatewayNetworkService::startWifi() {
|
||||
if (wifi_started_) {
|
||||
return ESP_OK;
|
||||
}
|
||||
setup_ap_started_ = false;
|
||||
|
||||
if (wifi_sta_netif_ == nullptr) {
|
||||
wifi_sta_netif_ = esp_netif_create_default_wifi_sta();
|
||||
if (wifi_sta_netif_ == nullptr) {
|
||||
ESP_LOGE(kTag, "failed to create default Wi-Fi STA netif");
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
}
|
||||
|
||||
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
|
||||
esp_err_t err = esp_wifi_init(&wifi_init_config);
|
||||
if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) {
|
||||
ESP_LOGE(kTag, "failed to init Wi-Fi: %s", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
err = esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID,
|
||||
&GatewayNetworkService::HandleWifiEvent, this);
|
||||
if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) {
|
||||
ESP_LOGE(kTag, "failed to register Wi-Fi event handler: %s", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
err = esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP,
|
||||
&GatewayNetworkService::HandleWifiEvent, this);
|
||||
if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) {
|
||||
ESP_LOGE(kTag, "failed to register IP event handler: %s", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
||||
err = esp_wifi_set_mode(WIFI_MODE_STA);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(kTag, "failed to set Wi-Fi mode STA: %s", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
const auto device_info = runtime_.deviceInfo();
|
||||
if (device_info.wlan.has_value() && !device_info.wlan->ssid.empty()) {
|
||||
wifi_config_t wifi_config = {};
|
||||
std::strncpy(reinterpret_cast<char*>(wifi_config.sta.ssid), device_info.wlan->ssid.c_str(),
|
||||
sizeof(wifi_config.sta.ssid) - 1);
|
||||
std::strncpy(reinterpret_cast<char*>(wifi_config.sta.password),
|
||||
device_info.wlan->password.c_str(), sizeof(wifi_config.sta.password) - 1);
|
||||
err = esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(kTag, "failed to set Wi-Fi credentials: %s", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
err = esp_wifi_start();
|
||||
if (err != ESP_OK && err != ESP_ERR_WIFI_CONN) {
|
||||
ESP_LOGE(kTag, "failed to start Wi-Fi: %s", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
uint8_t mac[6] = {0};
|
||||
if (device_info.wlan.has_value() && !device_info.wlan->ssid.empty() &&
|
||||
esp_wifi_get_mac(WIFI_IF_STA, mac) == ESP_OK) {
|
||||
WirelessInfo wireless = device_info.wlan.value_or(WirelessInfo{});
|
||||
wireless.mac = MacToHex(mac);
|
||||
runtime_.setWirelessInfo(std::move(wireless));
|
||||
}
|
||||
|
||||
wifi_started_ = true;
|
||||
ESP_LOGI(kTag, "Wi-Fi STA started has_credentials=%d",
|
||||
device_info.wlan.has_value() && !device_info.wlan->ssid.empty());
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t GatewayNetworkService::startSetupAp() {
|
||||
if (wifi_ap_netif_ == nullptr) {
|
||||
wifi_ap_netif_ = esp_netif_create_default_wifi_ap();
|
||||
if (wifi_ap_netif_ == nullptr) {
|
||||
ESP_LOGE(kTag, "failed to create default Wi-Fi AP netif");
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
esp_netif_ip_info_t ip_info = {};
|
||||
IP4_ADDR(&ip_info.ip, 192, 168, 3, 1);
|
||||
IP4_ADDR(&ip_info.gw, 192, 168, 3, 1);
|
||||
IP4_ADDR(&ip_info.netmask, 255, 255, 255, 0);
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_netif_dhcps_stop(wifi_ap_netif_));
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_netif_set_ip_info(wifi_ap_netif_, &ip_info));
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_netif_dhcps_start(wifi_ap_netif_));
|
||||
}
|
||||
|
||||
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
|
||||
esp_err_t err = esp_wifi_init(&wifi_init_config);
|
||||
if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) {
|
||||
ESP_LOGE(kTag, "failed to init Wi-Fi for setup AP: %s", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
if (wifi_started_) {
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_disconnect());
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_stop());
|
||||
}
|
||||
|
||||
wifi_config_t ap_config = {};
|
||||
std::strncpy(reinterpret_cast<char*>(ap_config.ap.ssid), kSetupApSsid,
|
||||
sizeof(ap_config.ap.ssid) - 1);
|
||||
ap_config.ap.ssid_len = std::strlen(kSetupApSsid);
|
||||
ap_config.ap.channel = 1;
|
||||
ap_config.ap.authmode = WIFI_AUTH_OPEN;
|
||||
ap_config.ap.max_connection = 4;
|
||||
|
||||
err = esp_wifi_set_mode(WIFI_MODE_AP);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(kTag, "failed to set Wi-Fi AP mode: %s", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
err = esp_wifi_set_config(WIFI_IF_AP, &ap_config);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(kTag, "failed to configure setup AP: %s", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
err = esp_wifi_start();
|
||||
if (err != ESP_OK && err != ESP_ERR_WIFI_CONN) {
|
||||
ESP_LOGE(kTag, "failed to start setup AP: %s", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
wifi_started_ = true;
|
||||
setup_ap_started_ = true;
|
||||
ESP_LOGI(kTag, "setup AP started ssid=%s ip=192.168.3.1", kSetupApSsid);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t GatewayNetworkService::configureStatusLed() {
|
||||
if (config_.status_led_gpio < 0) {
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
gpio_config_t io_config = {};
|
||||
io_config.pin_bit_mask = 1ULL << static_cast<uint32_t>(config_.status_led_gpio);
|
||||
io_config.mode = GPIO_MODE_OUTPUT;
|
||||
io_config.pull_up_en = GPIO_PULLUP_DISABLE;
|
||||
io_config.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
io_config.intr_type = GPIO_INTR_DISABLE;
|
||||
const esp_err_t err = gpio_config(&io_config);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(kTag, "failed to configure status LED GPIO%d: %s", config_.status_led_gpio,
|
||||
esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
setStatusLed(false);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t GatewayNetworkService::startHttpServer() {
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.server_port = config_.http_port;
|
||||
@@ -173,26 +365,27 @@ esp_err_t GatewayNetworkService::startHttpServer() {
|
||||
return err;
|
||||
}
|
||||
|
||||
httpd_uri_t info_uri = {};
|
||||
info_uri.uri = "/info";
|
||||
info_uri.method = HTTP_GET;
|
||||
info_uri.handler = &GatewayNetworkService::HandleInfoGet;
|
||||
info_uri.user_ctx = this;
|
||||
err = httpd_register_uri_handler(http_server_, &info_uri);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(kTag, "failed to register /info handler: %s", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
struct Route {
|
||||
const char* uri;
|
||||
httpd_method_t method;
|
||||
esp_err_t (*handler)(httpd_req_t*);
|
||||
};
|
||||
|
||||
httpd_uri_t command_uri = {};
|
||||
command_uri.uri = "/dali/cmd";
|
||||
command_uri.method = HTTP_POST;
|
||||
command_uri.handler = &GatewayNetworkService::HandleCommandPost;
|
||||
command_uri.user_ctx = this;
|
||||
err = httpd_register_uri_handler(http_server_, &command_uri);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(kTag, "failed to register /dali/cmd handler: %s", esp_err_to_name(err));
|
||||
return err;
|
||||
const Route routes[] = {
|
||||
{"/info", HTTP_GET, &GatewayNetworkService::HandleInfoGet},
|
||||
{"/dali/cmd", HTTP_GET, &GatewayNetworkService::HandleCommandGet},
|
||||
{"/dali/cmd", HTTP_POST, &GatewayNetworkService::HandleCommandPost},
|
||||
{"/led/1", HTTP_GET, &GatewayNetworkService::HandleLedOnGet},
|
||||
{"/led/0", HTTP_GET, &GatewayNetworkService::HandleLedOffGet},
|
||||
{"/jq.js", HTTP_GET, &GatewayNetworkService::HandleJqJsGet},
|
||||
};
|
||||
|
||||
for (const auto& route : routes) {
|
||||
err = RegisterUri(http_server_, route.uri, route.method, route.handler, this);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(kTag, "failed to register %s handler: %s", route.uri, esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
@@ -220,6 +413,52 @@ void GatewayNetworkService::UdpTaskEntry(void* arg) {
|
||||
static_cast<GatewayNetworkService*>(arg)->udpTaskLoop();
|
||||
}
|
||||
|
||||
void GatewayNetworkService::HandleWifiEvent(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data) {
|
||||
auto* service = static_cast<GatewayNetworkService*>(arg);
|
||||
if (service != nullptr) {
|
||||
service->handleWifiEvent(event_base, event_id, event_data);
|
||||
}
|
||||
}
|
||||
|
||||
void GatewayNetworkService::handleWifiEvent(esp_event_base_t event_base, int32_t event_id,
|
||||
void* event_data) {
|
||||
if (!config_.wifi_enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
||||
const auto info = runtime_.deviceInfo();
|
||||
if (info.wlan.has_value() && !info.wlan->ssid.empty()) {
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_connect());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
auto info = runtime_.deviceInfo();
|
||||
if (info.wlan.has_value()) {
|
||||
const bool has_credentials = !info.wlan->ssid.empty();
|
||||
info.wlan->ip.clear();
|
||||
runtime_.setWirelessInfo(std::move(*info.wlan));
|
||||
if (has_credentials) {
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_connect());
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP && event_data != nullptr) {
|
||||
auto* event = static_cast<ip_event_got_ip_t*>(event_data);
|
||||
char ip[16] = {0};
|
||||
esp_ip4addr_ntoa(&event->ip_info.ip, ip, sizeof(ip));
|
||||
WirelessInfo wireless = runtime_.deviceInfo().wlan.value_or(WirelessInfo{});
|
||||
wireless.ip = ip;
|
||||
runtime_.setWirelessInfo(std::move(wireless));
|
||||
ESP_LOGI(kTag, "Wi-Fi got IP %s", ip);
|
||||
}
|
||||
}
|
||||
|
||||
void GatewayNetworkService::udpTaskLoop() {
|
||||
udp_socket_ = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
||||
if (udp_socket_ < 0) {
|
||||
@@ -285,6 +524,39 @@ void GatewayNetworkService::handleGatewayNotification(const std::vector<uint8_t>
|
||||
reinterpret_cast<const sockaddr*>(&remote_addr), remote_addr_len);
|
||||
}
|
||||
|
||||
void GatewayNetworkService::handleWifiControl(uint8_t mode) {
|
||||
if (mode == 0) {
|
||||
config_.wifi_enabled = false;
|
||||
if (wifi_started_) {
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_disconnect());
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_stop());
|
||||
wifi_started_ = false;
|
||||
setup_ap_started_ = false;
|
||||
}
|
||||
auto info = runtime_.deviceInfo();
|
||||
if (info.wlan.has_value()) {
|
||||
info.wlan->ip.clear();
|
||||
runtime_.setWirelessInfo(std::move(*info.wlan));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (mode == 101) {
|
||||
config_.wifi_enabled = true;
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(startSetupAp());
|
||||
return;
|
||||
}
|
||||
if (mode == 1) {
|
||||
config_.wifi_enabled = true;
|
||||
if (setup_ap_started_) {
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_stop());
|
||||
wifi_started_ = false;
|
||||
setup_ap_started_ = false;
|
||||
}
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(startWifi());
|
||||
}
|
||||
}
|
||||
|
||||
std::string GatewayNetworkService::deviceInfoJson() const {
|
||||
const auto info = runtime_.deviceInfo();
|
||||
cJSON* root = cJSON_CreateObject();
|
||||
@@ -332,6 +604,61 @@ std::string GatewayNetworkService::deviceInfoDoubleEncodedJson() const {
|
||||
return rendered;
|
||||
}
|
||||
|
||||
std::string GatewayNetworkService::gatewaySnapshotJson() {
|
||||
const auto snapshot = controller_.snapshot();
|
||||
cJSON* root = cJSON_CreateObject();
|
||||
if (root == nullptr) {
|
||||
return {};
|
||||
}
|
||||
|
||||
cJSON_AddNumberToObject(root, "count", static_cast<double>(snapshot.channels.size()));
|
||||
|
||||
cJSON* gw_info = cJSON_CreateObject();
|
||||
if (gw_info != nullptr) {
|
||||
cJSON_AddBoolToObject(gw_info, "setupMode", snapshot.setup_mode);
|
||||
cJSON_AddBoolToObject(gw_info, "bleEnabled", snapshot.ble_enabled);
|
||||
cJSON_AddBoolToObject(gw_info, "wifiEnabled", snapshot.wifi_enabled);
|
||||
cJSON_AddBoolToObject(gw_info, "IPRouter", snapshot.ip_router_enabled);
|
||||
cJSON_AddBoolToObject(gw_info, "iSceneEnabled", snapshot.internal_scene_supported);
|
||||
cJSON_AddBoolToObject(gw_info, "iGroupEnabled", snapshot.internal_group_supported);
|
||||
cJSON_AddItemToObject(root, "gwInfo", gw_info);
|
||||
}
|
||||
|
||||
cJSON* channels = cJSON_CreateArray();
|
||||
if (channels != nullptr) {
|
||||
for (const auto& channel : snapshot.channels) {
|
||||
cJSON* item = cJSON_CreateObject();
|
||||
if (item == nullptr) {
|
||||
continue;
|
||||
}
|
||||
cJSON_AddNumberToObject(item, "channel", channel.channel_index + 1);
|
||||
cJSON_AddNumberToObject(item, "gw", channel.gateway_id);
|
||||
cJSON_AddStringToObject(item, "name", channel.name.c_str());
|
||||
cJSON_AddStringToObject(item, "phy", channel.phy.c_str());
|
||||
cJSON_AddNumberToObject(item, "sceneMaskLow", channel.scene_mask_low);
|
||||
cJSON_AddNumberToObject(item, "sceneMaskHigh", channel.scene_mask_high);
|
||||
cJSON_AddNumberToObject(item, "groupMaskLow", channel.group_mask_low);
|
||||
cJSON_AddNumberToObject(item, "groupMaskHigh", channel.group_mask_high);
|
||||
cJSON_AddBoolToObject(item, "isAllocAddr", channel.allocating);
|
||||
cJSON_AddNumberToObject(item, "lastAllocAddr", channel.last_alloc_addr);
|
||||
cJSON_AddItemToArray(channels, item);
|
||||
}
|
||||
cJSON_AddItemToObject(root, "channels", channels);
|
||||
}
|
||||
|
||||
const std::string rendered = PrintJson(root);
|
||||
cJSON_Delete(root);
|
||||
return rendered;
|
||||
}
|
||||
|
||||
void GatewayNetworkService::setStatusLed(bool on) {
|
||||
if (config_.status_led_gpio < 0) {
|
||||
return;
|
||||
}
|
||||
const bool level = config_.status_led_active_high ? on : !on;
|
||||
gpio_set_level(static_cast<gpio_num_t>(config_.status_led_gpio), level ? 1 : 0);
|
||||
}
|
||||
|
||||
esp_err_t GatewayNetworkService::HandleInfoGet(httpd_req_t* req) {
|
||||
auto* service = static_cast<GatewayNetworkService*>(req->user_ctx);
|
||||
if (service == nullptr) {
|
||||
@@ -343,6 +670,17 @@ esp_err_t GatewayNetworkService::HandleInfoGet(httpd_req_t* req) {
|
||||
return httpd_resp_send(req, payload.data(), payload.size());
|
||||
}
|
||||
|
||||
esp_err_t GatewayNetworkService::HandleCommandGet(httpd_req_t* req) {
|
||||
auto* service = static_cast<GatewayNetworkService*>(req->user_ctx);
|
||||
if (service == nullptr) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
const std::string payload = service->gatewaySnapshotJson();
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
return httpd_resp_send(req, payload.data(), payload.size());
|
||||
}
|
||||
|
||||
esp_err_t GatewayNetworkService::HandleCommandPost(httpd_req_t* req) {
|
||||
auto* service = static_cast<GatewayNetworkService*>(req->user_ctx);
|
||||
if (service == nullptr) {
|
||||
@@ -376,4 +714,46 @@ esp_err_t GatewayNetworkService::HandleCommandPost(httpd_req_t* req) {
|
||||
return httpd_resp_sendstr(req, "ok");
|
||||
}
|
||||
|
||||
esp_err_t GatewayNetworkService::HandleLedOnGet(httpd_req_t* req) {
|
||||
auto* service = static_cast<GatewayNetworkService*>(req->user_ctx);
|
||||
if (service == nullptr) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
service->setStatusLed(true);
|
||||
httpd_resp_set_type(req, "text/plain");
|
||||
return httpd_resp_sendstr(req, "ok");
|
||||
}
|
||||
|
||||
esp_err_t GatewayNetworkService::HandleLedOffGet(httpd_req_t* req) {
|
||||
auto* service = static_cast<GatewayNetworkService*>(req->user_ctx);
|
||||
if (service == nullptr) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
service->setStatusLed(false);
|
||||
httpd_resp_set_type(req, "text/plain");
|
||||
return httpd_resp_sendstr(req, "ok");
|
||||
}
|
||||
|
||||
esp_err_t GatewayNetworkService::HandleJqJsGet(httpd_req_t* req) {
|
||||
FILE* file = std::fopen("/jq.js", "rb");
|
||||
if (file == nullptr) {
|
||||
return httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "Not Found/jq.js");
|
||||
}
|
||||
|
||||
httpd_resp_set_type(req, "application/javascript");
|
||||
char buffer[512] = {0};
|
||||
while (true) {
|
||||
const size_t read_len = std::fread(buffer, 1, sizeof(buffer), file);
|
||||
if (read_len > 0 && httpd_resp_send_chunk(req, buffer, read_len) != ESP_OK) {
|
||||
std::fclose(file);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
if (read_len < sizeof(buffer)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::fclose(file);
|
||||
return httpd_resp_send_chunk(req, nullptr, 0);
|
||||
}
|
||||
|
||||
} // namespace gateway
|
||||
Reference in New Issue
Block a user