bolar_2 backup
This commit is contained in:
129
main/protocol/realtime_config.cc
Normal file
129
main/protocol/realtime_config.cc
Normal file
@@ -0,0 +1,129 @@
|
||||
#include "protocol/realtime_config.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
#include <esp_err.h>
|
||||
#include <esp_log.h>
|
||||
#include <nvs.h>
|
||||
|
||||
namespace borla {
|
||||
namespace {
|
||||
|
||||
constexpr const char* kTag = "RealtimeConfig2";
|
||||
constexpr const char* kNamespace = "websocket";
|
||||
constexpr const char* kFallbackUrl = kDefaultRealtimeUrl;
|
||||
|
||||
template <size_t N>
|
||||
void CopyLiteral(std::array<char, N>* output, const char* value)
|
||||
{
|
||||
output->fill('\0');
|
||||
if (value != nullptr) {
|
||||
std::strncpy(output->data(), value, output->size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
bool ReadString(
|
||||
nvs_handle_t handle,
|
||||
const char* key,
|
||||
std::array<char, N>* output)
|
||||
{
|
||||
size_t size = output->size();
|
||||
output->fill('\0');
|
||||
return nvs_get_str(handle, key, output->data(), &size) == ESP_OK;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool NvsRealtimeConfigStore::SaveEndpoint(
|
||||
const char* url,
|
||||
const char* token)
|
||||
{
|
||||
if ((url == nullptr || url[0] == '\0') &&
|
||||
(token == nullptr || token[0] == '\0')) {
|
||||
return true;
|
||||
}
|
||||
nvs_handle_t handle = 0;
|
||||
esp_err_t result =
|
||||
nvs_open(kNamespace, NVS_READWRITE, &handle);
|
||||
if (result != ESP_OK) {
|
||||
ESP_LOGE(kTag, "open writable namespace failed: %s",
|
||||
esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
if (url != nullptr && url[0] != '\0') {
|
||||
result = nvs_set_str(handle, "url", url);
|
||||
}
|
||||
if (result == ESP_OK && token != nullptr && token[0] != '\0') {
|
||||
result = nvs_set_str(handle, "token", token);
|
||||
}
|
||||
if (result == ESP_OK) {
|
||||
result = nvs_set_i32(handle, "version", 3);
|
||||
}
|
||||
if (result == ESP_OK) {
|
||||
result = nvs_commit(handle);
|
||||
}
|
||||
nvs_close(handle);
|
||||
if (result != ESP_OK) {
|
||||
ESP_LOGE(kTag, "endpoint save failed: %s",
|
||||
esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
ESP_LOGI(kTag, "endpoint persisted url=%s token=%s",
|
||||
url != nullptr && url[0] != '\0' ? url : "(unchanged)",
|
||||
token != nullptr && token[0] != '\0'
|
||||
? "present"
|
||||
: "(unchanged)");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NvsRealtimeConfigStore::Load(RealtimeConfig* output) const
|
||||
{
|
||||
if (output == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RealtimeConfig config;
|
||||
CopyLiteral(&config.transport.url, kFallbackUrl);
|
||||
|
||||
nvs_handle_t handle = 0;
|
||||
const esp_err_t open_result =
|
||||
nvs_open(kNamespace, NVS_READONLY, &handle);
|
||||
if (open_result == ESP_OK) {
|
||||
std::array<char, kWebsocketUrlCapacity> saved_url{};
|
||||
if (ReadString(handle, "url", &saved_url) &&
|
||||
saved_url[0] != '\0') {
|
||||
config.transport.url = saved_url;
|
||||
}
|
||||
ReadString(handle, "token", &config.transport.token);
|
||||
|
||||
int32_t version = 0;
|
||||
if (nvs_get_i32(handle, "version", &version) == ESP_OK &&
|
||||
version > 0 && version <= UINT8_MAX) {
|
||||
config.transport.protocol_version =
|
||||
static_cast<uint8_t>(version);
|
||||
}
|
||||
nvs_close(handle);
|
||||
} else if (open_result != ESP_ERR_NVS_NOT_FOUND) {
|
||||
ESP_LOGE(kTag, "open namespace failed: %s",
|
||||
esp_err_to_name(open_result));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (config.transport.protocol_version != 3) {
|
||||
ESP_LOGW(kTag, "forcing protocol version 3 instead of %u",
|
||||
static_cast<unsigned>(
|
||||
config.transport.protocol_version));
|
||||
config.transport.protocol_version = 3;
|
||||
}
|
||||
*output = config;
|
||||
ESP_LOGI(kTag, "loaded endpoint url=%s protocol=%u token=%s",
|
||||
output->transport.url.data(),
|
||||
static_cast<unsigned>(
|
||||
output->transport.protocol_version),
|
||||
output->transport.token[0] == '\0' ? "absent" : "present");
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace borla
|
||||
Reference in New Issue
Block a user