52aa2fc129
Co-authored-by: Copilot <copilot@github.com>
61 lines
1.5 KiB
C++
61 lines
1.5 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "esp_err.h"
|
|
#include "esp_http_server.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/semphr.h"
|
|
#include "freertos/task.h"
|
|
#include "lwip/sockets.h"
|
|
|
|
namespace gateway {
|
|
|
|
class GatewayController;
|
|
class GatewayRuntime;
|
|
|
|
struct GatewayNetworkServiceConfig {
|
|
bool http_enabled{true};
|
|
bool udp_enabled{true};
|
|
uint16_t http_port{80};
|
|
uint16_t udp_port{2020};
|
|
uint32_t udp_task_stack_size{4096};
|
|
UBaseType_t udp_task_priority{4};
|
|
};
|
|
|
|
class GatewayNetworkService {
|
|
public:
|
|
GatewayNetworkService(GatewayController& controller, GatewayRuntime& runtime,
|
|
GatewayNetworkServiceConfig config = {});
|
|
|
|
esp_err_t start();
|
|
|
|
private:
|
|
static void UdpTaskEntry(void* arg);
|
|
static esp_err_t HandleInfoGet(httpd_req_t* req);
|
|
static esp_err_t HandleCommandPost(httpd_req_t* req);
|
|
|
|
esp_err_t ensureNetworkStack();
|
|
esp_err_t startHttpServer();
|
|
esp_err_t startUdpTask();
|
|
void udpTaskLoop();
|
|
void handleGatewayNotification(const std::vector<uint8_t>& frame);
|
|
std::string deviceInfoJson() const;
|
|
std::string deviceInfoDoubleEncodedJson() const;
|
|
|
|
GatewayController& controller_;
|
|
GatewayRuntime& runtime_;
|
|
GatewayNetworkServiceConfig config_;
|
|
bool started_{false};
|
|
httpd_handle_t http_server_{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
|