314 lines
9.1 KiB
C++
314 lines
9.1 KiB
C++
#include "friend/friend_store.h"
|
|
|
|
#include "core/psram_object.h"
|
|
|
|
#include <algorithm>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
|
|
#include <cJSON.h>
|
|
#include <esp_err.h>
|
|
#include <esp_log.h>
|
|
#include <nvs.h>
|
|
|
|
namespace borla {
|
|
namespace {
|
|
|
|
constexpr const char* kTag = "FriendStore2";
|
|
constexpr const char* kNamespace = "friend_v2";
|
|
constexpr const char* kSnapshotKey = "snapshot";
|
|
constexpr uint32_t kMagic = 0x32444E46;
|
|
constexpr uint16_t kVersion = 1;
|
|
constexpr const char* kLegacyDeviceNamespace = "device";
|
|
constexpr const char* kLegacyDeviceNameKey = "name";
|
|
constexpr const char* kLegacyFriendsNamespace = "friends";
|
|
constexpr const char* kLegacyFriendCountKey = "count";
|
|
|
|
struct PersistedFriendSnapshot {
|
|
uint32_t magic = kMagic;
|
|
uint16_t version = kVersion;
|
|
uint16_t friend_count = 0;
|
|
uint8_t has_pending_upload = 0;
|
|
uint8_t reserved[3]{};
|
|
char device_name[kFriendNameCapacity]{};
|
|
FriendPeer friends[kMaximumFriendCount]{};
|
|
FriendPeer pending_upload{};
|
|
uint32_t checksum = 0;
|
|
};
|
|
|
|
uint32_t Checksum(const void* data, size_t size)
|
|
{
|
|
const auto* bytes = static_cast<const uint8_t*>(data);
|
|
uint32_t value = 2166136261U;
|
|
for (size_t index = 0; index < size; ++index) {
|
|
value ^= bytes[index];
|
|
value *= 16777619U;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
bool PeerValid(const FriendPeer& peer)
|
|
{
|
|
return peer.mac[0] != '\0' &&
|
|
std::memchr(peer.mac.data(), '\0', peer.mac.size()) != nullptr &&
|
|
std::memchr(peer.name.data(), '\0', peer.name.size()) != nullptr;
|
|
}
|
|
|
|
template <size_t Capacity>
|
|
bool CopyString(
|
|
const char* value,
|
|
std::array<char, Capacity>* output)
|
|
{
|
|
if (value == nullptr || output == nullptr) {
|
|
return false;
|
|
}
|
|
const size_t length = std::strlen(value);
|
|
if (length == 0 || length >= Capacity) {
|
|
return false;
|
|
}
|
|
output->fill('\0');
|
|
std::memcpy(output->data(), value, length);
|
|
return true;
|
|
}
|
|
|
|
bool LoadLegacy(FriendStoreSnapshot* snapshot)
|
|
{
|
|
bool migrated = false;
|
|
nvs_handle_t device_handle = 0;
|
|
if (nvs_open(
|
|
kLegacyDeviceNamespace,
|
|
NVS_READONLY,
|
|
&device_handle) == ESP_OK) {
|
|
size_t size = snapshot->device_name.size();
|
|
if (nvs_get_str(
|
|
device_handle,
|
|
kLegacyDeviceNameKey,
|
|
snapshot->device_name.data(),
|
|
&size) == ESP_OK &&
|
|
snapshot->device_name[0] != '\0') {
|
|
migrated = true;
|
|
} else {
|
|
snapshot->device_name.fill('\0');
|
|
}
|
|
nvs_close(device_handle);
|
|
}
|
|
|
|
nvs_handle_t friends_handle = 0;
|
|
if (nvs_open(
|
|
kLegacyFriendsNamespace,
|
|
NVS_READONLY,
|
|
&friends_handle) != ESP_OK) {
|
|
return migrated;
|
|
}
|
|
int32_t count = 0;
|
|
if (nvs_get_i32(
|
|
friends_handle,
|
|
kLegacyFriendCountKey,
|
|
&count) != ESP_OK ||
|
|
count <= 0) {
|
|
nvs_close(friends_handle);
|
|
return migrated;
|
|
}
|
|
count = std::min<int32_t>(
|
|
count, static_cast<int32_t>(kMaximumFriendCount));
|
|
for (int32_t index = 0; index < count; ++index) {
|
|
char key[16]{};
|
|
std::snprintf(key, sizeof(key), "f_%u",
|
|
static_cast<unsigned>(index));
|
|
std::array<char, 256> json{};
|
|
size_t size = json.size();
|
|
if (nvs_get_str(
|
|
friends_handle,
|
|
key,
|
|
json.data(),
|
|
&size) != ESP_OK) {
|
|
continue;
|
|
}
|
|
cJSON* root = cJSON_Parse(json.data());
|
|
const cJSON* mac =
|
|
cJSON_GetObjectItemCaseSensitive(root, "mac");
|
|
const cJSON* name =
|
|
cJSON_GetObjectItemCaseSensitive(root, "name");
|
|
FriendPeer peer;
|
|
const bool valid =
|
|
cJSON_IsString(mac) &&
|
|
cJSON_IsString(name) &&
|
|
CopyString(mac->valuestring, &peer.mac) &&
|
|
CopyString(name->valuestring, &peer.name);
|
|
cJSON_Delete(root);
|
|
if (valid && AddOrUpdateFriend(snapshot, peer)) {
|
|
migrated = true;
|
|
}
|
|
}
|
|
nvs_close(friends_handle);
|
|
return migrated;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
bool NvsFriendStore::Load(FriendStoreSnapshot* snapshot) const
|
|
{
|
|
if (snapshot == nullptr) {
|
|
return false;
|
|
}
|
|
auto legacy = MakePsramObject<FriendStoreSnapshot>();
|
|
auto stored = MakePsramObject<PersistedFriendSnapshot>();
|
|
if (!legacy || !stored) {
|
|
ESP_LOGE(kTag, "PSRAM work-buffer allocation failed");
|
|
return false;
|
|
}
|
|
const bool has_legacy = LoadLegacy(legacy.get());
|
|
*snapshot = {};
|
|
nvs_handle_t handle = 0;
|
|
esp_err_t result =
|
|
nvs_open(kNamespace, NVS_READONLY, &handle);
|
|
if (result == ESP_ERR_NVS_NOT_FOUND) {
|
|
*snapshot = *legacy;
|
|
if (has_legacy) {
|
|
ESP_LOGI(kTag, "migrated legacy friends/device name");
|
|
Save(*snapshot);
|
|
}
|
|
return true;
|
|
}
|
|
if (result != ESP_OK) {
|
|
ESP_LOGE(kTag, "open failed: %s", esp_err_to_name(result));
|
|
return false;
|
|
}
|
|
size_t size = sizeof(*stored);
|
|
result = nvs_get_blob(
|
|
handle, kSnapshotKey, stored.get(), &size);
|
|
nvs_close(handle);
|
|
if (result == ESP_ERR_NVS_NOT_FOUND) {
|
|
*snapshot = *legacy;
|
|
if (has_legacy) {
|
|
ESP_LOGI(kTag, "migrated legacy friends/device name");
|
|
Save(*snapshot);
|
|
}
|
|
return true;
|
|
}
|
|
const uint32_t expected = Checksum(
|
|
stored.get(), offsetof(PersistedFriendSnapshot, checksum));
|
|
if (result != ESP_OK || size != sizeof(*stored) ||
|
|
stored->magic != kMagic || stored->version != kVersion ||
|
|
stored->friend_count > kMaximumFriendCount ||
|
|
stored->checksum != expected ||
|
|
std::memchr(
|
|
stored->device_name,
|
|
'\0',
|
|
sizeof(stored->device_name)) == nullptr) {
|
|
ESP_LOGE(kTag, "invalid persisted friend snapshot");
|
|
return false;
|
|
}
|
|
snapshot->friend_count = stored->friend_count;
|
|
std::memcpy(
|
|
snapshot->device_name.data(),
|
|
stored->device_name,
|
|
snapshot->device_name.size());
|
|
for (size_t index = 0;
|
|
index < snapshot->friend_count;
|
|
++index) {
|
|
if (!PeerValid(stored->friends[index])) {
|
|
return false;
|
|
}
|
|
snapshot->friends[index] = stored->friends[index];
|
|
}
|
|
snapshot->has_pending_upload =
|
|
stored->has_pending_upload != 0;
|
|
if (snapshot->has_pending_upload) {
|
|
if (!PeerValid(stored->pending_upload)) {
|
|
return false;
|
|
}
|
|
snapshot->pending_upload = stored->pending_upload;
|
|
}
|
|
if (snapshot->device_name[0] == '\0' &&
|
|
legacy->device_name[0] != '\0') {
|
|
snapshot->device_name = legacy->device_name;
|
|
Save(*snapshot);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool NvsFriendStore::Save(const FriendStoreSnapshot& snapshot) const
|
|
{
|
|
if (snapshot.friend_count > kMaximumFriendCount ||
|
|
std::memchr(
|
|
snapshot.device_name.data(),
|
|
'\0',
|
|
snapshot.device_name.size()) == nullptr ||
|
|
(snapshot.has_pending_upload &&
|
|
!PeerValid(snapshot.pending_upload))) {
|
|
return false;
|
|
}
|
|
auto stored = MakePsramObject<PersistedFriendSnapshot>();
|
|
if (!stored) {
|
|
ESP_LOGE(kTag, "PSRAM work-buffer allocation failed");
|
|
return false;
|
|
}
|
|
stored->friend_count =
|
|
static_cast<uint16_t>(snapshot.friend_count);
|
|
stored->has_pending_upload =
|
|
snapshot.has_pending_upload ? 1 : 0;
|
|
std::memcpy(
|
|
stored->device_name,
|
|
snapshot.device_name.data(),
|
|
snapshot.device_name.size());
|
|
for (size_t index = 0;
|
|
index < snapshot.friend_count;
|
|
++index) {
|
|
if (!PeerValid(snapshot.friends[index])) {
|
|
return false;
|
|
}
|
|
stored->friends[index] = snapshot.friends[index];
|
|
}
|
|
stored->pending_upload = snapshot.pending_upload;
|
|
stored->checksum = Checksum(
|
|
stored.get(), offsetof(PersistedFriendSnapshot, checksum));
|
|
|
|
nvs_handle_t handle = 0;
|
|
esp_err_t result =
|
|
nvs_open(kNamespace, NVS_READWRITE, &handle);
|
|
if (result == ESP_OK) {
|
|
result = nvs_set_blob(
|
|
handle, kSnapshotKey, stored.get(), sizeof(*stored));
|
|
}
|
|
if (result == ESP_OK) {
|
|
result = nvs_commit(handle);
|
|
}
|
|
if (handle != 0) {
|
|
nvs_close(handle);
|
|
}
|
|
if (result != ESP_OK) {
|
|
ESP_LOGE(kTag, "save failed: %s", esp_err_to_name(result));
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool AddOrUpdateFriend(
|
|
FriendStoreSnapshot* snapshot,
|
|
const FriendPeer& peer)
|
|
{
|
|
if (snapshot == nullptr || !PeerValid(peer)) {
|
|
return false;
|
|
}
|
|
for (size_t index = 0;
|
|
index < snapshot->friend_count;
|
|
++index) {
|
|
if (std::strcmp(
|
|
snapshot->friends[index].mac.data(),
|
|
peer.mac.data()) == 0) {
|
|
snapshot->friends[index] = peer;
|
|
return true;
|
|
}
|
|
}
|
|
if (snapshot->friend_count >= snapshot->friends.size()) {
|
|
return false;
|
|
}
|
|
snapshot->friends[snapshot->friend_count++] = peer;
|
|
return true;
|
|
}
|
|
|
|
} // namespace borla
|