Phase 6: SoftAP MTG-Companion web server, /api/last_uid + /api/bind UID pairing, embedded pairing SPA
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include <sys/unistd.h>
|
||||
#include "esp_log.h"
|
||||
#include "esp_littlefs.h"
|
||||
#include "cJSON.h"
|
||||
#include "storage_manager.h"
|
||||
|
||||
static const char *TAG = "storage";
|
||||
@@ -109,6 +110,64 @@ esp_err_t storage_write_text(const char *path, const char *text)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t storage_manager_bind_uid(const char *uid, const char *card_name)
|
||||
{
|
||||
cJSON *root = NULL;
|
||||
char *existing = NULL;
|
||||
size_t existing_len = 0;
|
||||
|
||||
if (storage_read_text(STORAGE_MAPPINGS, &existing, &existing_len) == ESP_OK && existing != NULL) {
|
||||
root = cJSON_Parse(existing);
|
||||
}
|
||||
free(existing);
|
||||
|
||||
if (root == NULL || !cJSON_IsObject(root)) {
|
||||
if (root) cJSON_Delete(root);
|
||||
root = cJSON_CreateObject();
|
||||
}
|
||||
if (root == NULL) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
cJSON_DeleteItemFromObject(root, uid);
|
||||
cJSON_AddItemToObject(root, uid, cJSON_CreateString(card_name));
|
||||
|
||||
char *out = cJSON_Print(root);
|
||||
esp_err_t ret = out ? storage_write_text(STORAGE_MAPPINGS, out) : ESP_ERR_NO_MEM;
|
||||
free(out);
|
||||
cJSON_Delete(root);
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t storage_manager_lookup_name(const char *uid, char **out)
|
||||
{
|
||||
*out = NULL;
|
||||
char *buf = NULL;
|
||||
size_t len = 0;
|
||||
if (storage_read_text(STORAGE_MAPPINGS, &buf, &len) != ESP_OK || buf == NULL) {
|
||||
free(buf);
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
cJSON *root = cJSON_Parse(buf);
|
||||
free(buf);
|
||||
if (root == NULL || !cJSON_IsObject(root)) {
|
||||
if (root) cJSON_Delete(root);
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
cJSON *item = cJSON_GetObjectItem(root, uid);
|
||||
esp_err_t ret = (item && cJSON_IsString(item) && item->valuestring) ? ESP_OK : ESP_ERR_NOT_FOUND;
|
||||
if (ret == ESP_OK) {
|
||||
*out = strdup(item->valuestring);
|
||||
if (*out == NULL) {
|
||||
ret = ESP_ERR_NO_MEM;
|
||||
}
|
||||
}
|
||||
cJSON_Delete(root);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static esp_err_t seed_defaults(void)
|
||||
{
|
||||
/* Create /spiffs/cards directory */
|
||||
|
||||
Reference in New Issue
Block a user