4ce3513dd2
- Introduced DaliRawFrame structure to encapsulate raw frame data. - Enhanced DaliDomainService to manage raw frame sinks and processing. - Implemented raw frame task for asynchronous handling of incoming DALI frames. - Integrated raw frame handling in GatewayBleBridge and GatewayNetworkService. - Added GatewayUsbSetupBridge to facilitate USB Serial/JTAG communication with DALI. - Configured ESP-NOW for wireless communication and setup management. - Updated GatewayRuntime to support clearing wireless credentials on boot button long press. - Enhanced CMakeLists to include new components and dependencies. Co-authored-by: Copilot <copilot@github.com>
112 lines
3.7 KiB
C++
112 lines
3.7 KiB
C++
#pragma once
|
|
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "esp_err.h"
|
|
#include "esp_event.h"
|
|
#include "esp_http_server.h"
|
|
#include "esp_netif.h"
|
|
#include "esp_now.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/semphr.h"
|
|
#include "freertos/task.h"
|
|
#include "lwip/sockets.h"
|
|
|
|
namespace gateway {
|
|
|
|
class GatewayController;
|
|
class DaliDomainService;
|
|
struct DaliRawFrame;
|
|
class GatewayRuntime;
|
|
|
|
struct GatewayNetworkServiceConfig {
|
|
bool wifi_enabled{true};
|
|
bool espnow_setup_enabled{true};
|
|
bool espnow_setup_startup_enabled{false};
|
|
bool http_enabled{true};
|
|
bool udp_enabled{true};
|
|
uint16_t http_port{80};
|
|
uint16_t udp_port{2020};
|
|
int status_led_gpio{-1};
|
|
bool status_led_active_high{true};
|
|
int boot_button_gpio{-1};
|
|
bool boot_button_active_low{true};
|
|
uint32_t boot_button_long_press_ms{3000};
|
|
uint32_t boot_button_task_stack_size{2048};
|
|
UBaseType_t boot_button_task_priority{2};
|
|
uint32_t udp_task_stack_size{4096};
|
|
UBaseType_t udp_task_priority{4};
|
|
};
|
|
|
|
class GatewayNetworkService {
|
|
public:
|
|
GatewayNetworkService(GatewayController& controller, GatewayRuntime& runtime,
|
|
DaliDomainService& dali_domain, GatewayNetworkServiceConfig config = {});
|
|
|
|
esp_err_t start();
|
|
|
|
private:
|
|
static void UdpTaskEntry(void* arg);
|
|
static void BootButtonTaskEntry(void* arg);
|
|
static esp_err_t HandleInfoGet(httpd_req_t* req);
|
|
static esp_err_t HandleCommandGet(httpd_req_t* req);
|
|
static esp_err_t HandleCommandPost(httpd_req_t* req);
|
|
static esp_err_t HandleLedOnGet(httpd_req_t* req);
|
|
static esp_err_t HandleLedOffGet(httpd_req_t* req);
|
|
static esp_err_t HandleJqJsGet(httpd_req_t* req);
|
|
static void HandleWifiEvent(void* arg, esp_event_base_t event_base, int32_t event_id,
|
|
void* event_data);
|
|
static void HandleEspNowReceive(const esp_now_recv_info_t* info, const uint8_t* data,
|
|
int data_len);
|
|
|
|
esp_err_t ensureNetworkStack();
|
|
esp_err_t startWifi();
|
|
esp_err_t startSetupAp();
|
|
esp_err_t startEspNow();
|
|
void stopEspNow();
|
|
esp_err_t addEspNowPeer(const uint8_t* mac, bool broadcast = false);
|
|
esp_err_t sendEspNowJson(const uint8_t* mac, const std::string& payload);
|
|
esp_err_t configureStatusLed();
|
|
esp_err_t startHttpServer();
|
|
esp_err_t startUdpTask();
|
|
esp_err_t configureBootButton();
|
|
esp_err_t startBootButtonTask();
|
|
void udpTaskLoop();
|
|
void bootButtonTaskLoop();
|
|
void handleGatewayNotification(const std::vector<uint8_t>& frame);
|
|
void handleWifiControl(uint8_t mode);
|
|
void handleWifiEvent(esp_event_base_t event_base, int32_t event_id, void* event_data);
|
|
void handleEspNowReceive(const esp_now_recv_info_t* info, const uint8_t* data, int data_len);
|
|
void handleSetupUartFrame(int setup_id, const std::vector<uint8_t>& frame);
|
|
void handleDaliRawFrame(const DaliRawFrame& frame);
|
|
std::string deviceInfoJson() const;
|
|
std::string deviceInfoDoubleEncodedJson() const;
|
|
std::string gatewaySnapshotJson();
|
|
void setStatusLed(bool on);
|
|
|
|
GatewayController& controller_;
|
|
GatewayRuntime& runtime_;
|
|
DaliDomainService& dali_domain_;
|
|
GatewayNetworkServiceConfig config_;
|
|
bool started_{false};
|
|
httpd_handle_t http_server_{nullptr};
|
|
esp_netif_t* wifi_sta_netif_{nullptr};
|
|
esp_netif_t* wifi_ap_netif_{nullptr};
|
|
bool wifi_started_{false};
|
|
bool setup_ap_started_{false};
|
|
bool espnow_started_{false};
|
|
bool espnow_connected_{false};
|
|
std::array<uint8_t, 6> espnow_peer_{};
|
|
TaskHandle_t boot_button_task_handle_{nullptr};
|
|
TaskHandle_t udp_task_handle_{nullptr};
|
|
int udp_socket_{-1};
|
|
SemaphoreHandle_t udp_lock_{nullptr};
|
|
bool has_udp_remote_{false};
|
|
sockaddr_storage udp_remote_addr_{};
|
|
socklen_t udp_remote_addr_len_{0};
|
|
};
|
|
|
|
} // namespace gateway
|