Files
gateway/components/gateway_runtime/include/gateway_runtime.hpp
T

135 lines
4.0 KiB
C++

#pragma once
#include <cstddef>
#include <cstdint>
#include <deque>
#include <functional>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "esp_err.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "gateway_core.hpp"
#include "nvs.h"
namespace gateway {
esp_err_t InitializeRuntimeNvs();
std::string ReadRuntimeSerialId();
class DaliDomainService;
struct WirelessInfo {
std::string ssid;
std::string password;
std::string mac;
std::string ip;
};
struct GatewayRuntimeConfig {
std::string_view project_name;
std::string_view version;
std::string serial_id;
size_t command_queue_capacity{16};
};
struct GatewayDeviceInfo {
std::string serial_id;
std::string type;
std::string project;
std::string version;
size_t dali_gateway_count{0};
bool ble_enabled{false};
std::optional<WirelessInfo> wlan;
};
class GatewaySettingsStore {
public:
GatewaySettingsStore();
~GatewaySettingsStore();
esp_err_t open();
void close();
bool getBleEnabled(bool default_value = false) const;
bool setBleEnabled(bool enabled);
std::optional<std::string> getWifiSsid() const;
std::optional<std::string> getWifiPassword() const;
bool setWifiCredentials(std::string_view ssid, std::string_view password);
std::string getGatewayName(uint8_t gateway_id, std::string_view fallback) const;
bool setGatewayName(uint8_t gateway_id, std::string_view name);
private:
std::optional<std::string> readString(std::string_view key) const;
bool writeString(std::string_view key, std::string_view value);
std::string makeGatewayNameKey(uint8_t gateway_id) const;
mutable nvs_handle_t handle_{0};
};
class GatewayRuntime {
public:
enum class CommandDropReason {
kNone,
kDuplicate,
kQueueFull,
};
GatewayRuntime(BootProfile profile, GatewayRuntimeConfig config,
DaliDomainService* dali_domain);
~GatewayRuntime();
esp_err_t start();
static std::vector<uint8_t> checksum(std::vector<uint8_t> frame);
static bool hasValidChecksum(const std::vector<uint8_t>& frame);
static bool isGatewayCommandFrame(const std::vector<uint8_t>& frame);
static std::vector<uint8_t> buildNotificationFrame(const std::vector<uint8_t>& payload);
bool enqueueCommand(std::vector<uint8_t> command);
std::optional<std::vector<uint8_t>> popNextCommand();
void completeCurrentCommand();
bool hasPendingQueryCommand(const std::vector<uint8_t>& command) const;
CommandDropReason lastEnqueueDropReason() const;
void setGatewayCount(size_t gateway_count);
void setWirelessInfo(WirelessInfo info);
void setCommandAddressResolver(std::function<uint8_t(uint8_t gw, uint8_t raw_addr)> resolver);
GatewayDeviceInfo deviceInfo() const;
bool bleEnabled() const;
bool setBleEnabled(bool enabled);
std::string gatewayName(uint8_t gateway_id) const;
bool setGatewayName(uint8_t gateway_id, std::string_view name);
std::string gatewaySerialHex(uint8_t gateway_id) const;
std::string bleMacHex() const;
std::string bleGatewayName(uint8_t gateway_id, std::string_view gateway_name) const;
std::string defaultBleGatewayName() const;
private:
bool isQueryCommand(const std::vector<uint8_t>& command) const;
std::optional<std::string> queryCommandKey(const std::vector<uint8_t>& command) const;
std::string defaultGatewayName(uint8_t gateway_id) const;
std::vector<uint8_t> serialBytes() const;
static std::string toHex(const std::vector<uint8_t>& bytes);
BootProfile profile_;
GatewayRuntimeConfig config_;
DaliDomainService* dali_domain_;
GatewaySettingsStore settings_;
std::optional<std::vector<uint8_t>> current_command_;
std::deque<std::vector<uint8_t>> pending_commands_;
size_t gateway_count_{0};
bool ble_enabled_{false};
CommandDropReason last_enqueue_drop_reason_{CommandDropReason::kNone};
std::function<uint8_t(uint8_t gw, uint8_t raw_addr)> command_address_resolver_;
std::optional<WirelessInfo> wireless_info_;
SemaphoreHandle_t command_lock_{nullptr};
};
} // namespace gateway