Files
dali_cpp/src/gateway_provisioning.cpp
T

173 lines
5.6 KiB
C++

#include "gateway_provisioning.hpp"
#ifdef ESP_PLATFORM
extern "C" {
#include "esp_log.h"
#include "nvs.h"
#include "nvs_flash.h"
}
namespace {
constexpr const char* kTag = "gateway_provision";
constexpr const char* kKeyBrokerURI = "broker_uri";
constexpr const char* kKeyDeviceID = "device_id";
constexpr const char* kKeyUsername = "username";
constexpr const char* kKeyPassword = "password";
constexpr const char* kKeyTopicPrefix = "topic_prefix";
constexpr const char* kKeyCemiTransport = "cemi_transport";
constexpr const char* kKeyLteUartEnabled = "lte_uart_en";
constexpr const char* kKeyLteUartPort = "lte_uart_port";
constexpr const char* kKeyLteTxPin = "lte_tx_pin";
constexpr const char* kKeyLteRxPin = "lte_rx_pin";
constexpr const char* kKeyLteBaudrate = "lte_baudrate";
constexpr const char* kKeyLteRxBuffer = "lte_rx_buf";
constexpr const char* kKeyLteTxBuffer = "lte_tx_buf";
constexpr const char* kKeyQos = "qos";
esp_err_t writeString(nvs_handle_t handle, const char* key, const std::string& value) {
return nvs_set_str(handle, key, value.c_str());
}
esp_err_t readString(nvs_handle_t handle, const char* key, std::string* value) {
size_t required = 0;
esp_err_t err = nvs_get_str(handle, key, nullptr, &required);
if (err != ESP_OK) {
return err;
}
std::string buf(required, '\0');
err = nvs_get_str(handle, key, buf.data(), &required);
if (err != ESP_OK) {
return err;
}
if (!buf.empty() && buf.back() == '\0') {
buf.pop_back();
}
*value = buf;
return ESP_OK;
}
} // namespace
esp_err_t GatewayProvisioningStore::save(const GatewayCloudConfig& config) const {
nvs_handle_t handle;
esp_err_t err = nvs_open(nvsNamespace_.c_str(), NVS_READWRITE, &handle);
if (err != ESP_OK) {
ESP_LOGE(kTag, "nvs_open(save) failed: %s", esp_err_to_name(err));
return err;
}
err = writeString(handle, kKeyBrokerURI, config.brokerURI);
if (err == ESP_OK) err = writeString(handle, kKeyDeviceID, config.deviceID);
if (err == ESP_OK) err = writeString(handle, kKeyUsername, config.username);
if (err == ESP_OK) err = writeString(handle, kKeyPassword, config.password);
if (err == ESP_OK) err = writeString(handle, kKeyTopicPrefix, config.topicPrefix);
if (err == ESP_OK) err = writeString(handle, kKeyCemiTransport, config.cemiTransport);
if (err == ESP_OK) err = nvs_set_i32(handle, kKeyLteUartEnabled,
config.lteUartEnabled ? 1 : 0);
if (err == ESP_OK) err = nvs_set_i32(handle, kKeyLteUartPort, config.lteUartPort);
if (err == ESP_OK) err = nvs_set_i32(handle, kKeyLteTxPin, config.lteTxPin);
if (err == ESP_OK) err = nvs_set_i32(handle, kKeyLteRxPin, config.lteRxPin);
if (err == ESP_OK) err = nvs_set_i32(handle, kKeyLteBaudrate, config.lteBaudrate);
if (err == ESP_OK) err = nvs_set_i32(handle, kKeyLteRxBuffer, config.lteRxBufferSize);
if (err == ESP_OK) err = nvs_set_i32(handle, kKeyLteTxBuffer, config.lteTxBufferSize);
if (err == ESP_OK) err = nvs_set_i32(handle, kKeyQos, config.qos);
if (err == ESP_OK) err = nvs_commit(handle);
nvs_close(handle);
if (err != ESP_OK) {
ESP_LOGE(kTag, "save failed: %s", esp_err_to_name(err));
}
return err;
}
esp_err_t GatewayProvisioningStore::load(GatewayCloudConfig* config) const {
if (config == nullptr) {
return ESP_ERR_INVALID_ARG;
}
nvs_handle_t handle;
esp_err_t err = nvs_open(nvsNamespace_.c_str(), NVS_READONLY, &handle);
if (err != ESP_OK) {
return err;
}
err = readString(handle, kKeyBrokerURI, &config->brokerURI);
if (err == ESP_OK) err = readString(handle, kKeyDeviceID, &config->deviceID);
if (err == ESP_OK) err = readString(handle, kKeyUsername, &config->username);
if (err == ESP_OK) err = readString(handle, kKeyPassword, &config->password);
esp_err_t topicErr = readString(handle, kKeyTopicPrefix, &config->topicPrefix);
if (topicErr != ESP_OK) {
config->topicPrefix = "devices";
}
esp_err_t transportErr = readString(handle, kKeyCemiTransport, &config->cemiTransport);
if (transportErr != ESP_OK) {
config->cemiTransport = "mqtt";
}
int32_t intValue = 0;
if (nvs_get_i32(handle, kKeyLteUartEnabled, &intValue) == ESP_OK) {
config->lteUartEnabled = intValue != 0;
}
if (nvs_get_i32(handle, kKeyLteUartPort, &intValue) == ESP_OK) {
config->lteUartPort = intValue;
}
if (nvs_get_i32(handle, kKeyLteTxPin, &intValue) == ESP_OK) {
config->lteTxPin = intValue;
}
if (nvs_get_i32(handle, kKeyLteRxPin, &intValue) == ESP_OK) {
config->lteRxPin = intValue;
}
if (nvs_get_i32(handle, kKeyLteBaudrate, &intValue) == ESP_OK) {
config->lteBaudrate = intValue;
}
if (nvs_get_i32(handle, kKeyLteRxBuffer, &intValue) == ESP_OK) {
config->lteRxBufferSize = intValue;
}
if (nvs_get_i32(handle, kKeyLteTxBuffer, &intValue) == ESP_OK) {
config->lteTxBufferSize = intValue;
}
int32_t qos = 1;
esp_err_t qosErr = nvs_get_i32(handle, kKeyQos, &qos);
if (qosErr == ESP_OK) {
config->qos = qos;
} else {
config->qos = 1;
}
nvs_close(handle);
return err;
}
esp_err_t GatewayProvisioningStore::clear() const {
nvs_handle_t handle;
esp_err_t err = nvs_open(nvsNamespace_.c_str(), NVS_READWRITE, &handle);
if (err != ESP_OK) {
return err;
}
err = nvs_erase_all(handle);
if (err == ESP_OK) {
err = nvs_commit(handle);
}
nvs_close(handle);
return err;
}
#else
esp_err_t GatewayProvisioningStore::save(const GatewayCloudConfig& config) const {
(void)config;
return -1;
}
esp_err_t GatewayProvisioningStore::load(GatewayCloudConfig* config) const {
(void)config;
return -1;
}
esp_err_t GatewayProvisioningStore::clear() const { return -1; }
#endif