Step 2.2: config registry and NVS store, GET/POST /api/config
- aes67_web/cfg.c: cfg_register(group, defaults, validate, apply), cfg_override_defaults() for project defaults, cfg_get(). Stored values are loaded on register and merged over defaults (known keys, matching types). NVS keeps only keys that differ from defaults, one JSON string per group, so new firmware defaults still reach untouched settings. - POST /api/config: type check against defaults, per-group validation, nothing stored unless every group passes; 400 "group.key: reason". Unknown keys/groups are ignored. Returns the new config. - Core groups registered by their components with the UI's ranges: ptp (aes67_ptp), aes67 (aes67_tx, incl. multicast range and mono_sum => 1 channel), net/inet (aes67_net), log (aes67_syslog). - Project: PROJECT.def overrides (hostname p4-aes67, name P4 AES67) and the "source" group in main/project_cfg.c. - Config is stored only; nothing is applied live yet. - Verified on board: defaults, valid save, 8 rejected values (nothing stored), persistence across reboot, restore to defaults. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
// Config registry and store (cJSON in NVS), part of aes67_web.
|
||||
// Each component registers its group; /api/config is built from the registry.
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
// Validate a candidate group object. On failure write "key: reason" to err and return false.
|
||||
typedef bool (*cfg_validate_cb_t)(const cJSON *group, char *err, size_t err_len);
|
||||
// Called with the new group values after a successful save (apply live where possible).
|
||||
typedef void (*cfg_apply_cb_t)(const cJSON *group);
|
||||
|
||||
// Project defaults merged over a group's core defaults. Call before that group is registered.
|
||||
esp_err_t cfg_override_defaults(const char *group, const char *json);
|
||||
// Register a group. Stored values (NVS) are loaded immediately, merged over the defaults.
|
||||
esp_err_t cfg_register(const char *group, const char *defaults_json,
|
||||
cfg_validate_cb_t validate, cfg_apply_cb_t apply);
|
||||
// Copy of a group's current values; caller frees with cJSON_Delete. NULL if not registered.
|
||||
cJSON *cfg_get(const char *group);
|
||||
|
||||
// Validation helpers for validate callbacks. Each returns false and fills err on failure.
|
||||
bool cfg_check_num(const cJSON *g, const char *key, double min, double max, char *err, size_t n);
|
||||
bool cfg_check_int(const cJSON *g, const char *key, double min, double max, char *err, size_t n);
|
||||
bool cfg_check_num_in(const cJSON *g, const char *key, const double *vals, int count, char *err, size_t n);
|
||||
bool cfg_check_enum(const cJSON *g, const char *key, const char *const *opts, char *err, size_t n);
|
||||
bool cfg_check_str(const cJSON *g, const char *key, size_t min_len, size_t max_len, char *err, size_t n);
|
||||
bool cfg_check_ipv4(const cJSON *g, const char *key, bool allow_empty, char *err, size_t n);
|
||||
@@ -13,7 +13,7 @@ typedef void (*status_cb_t)(cJSON *status);
|
||||
esp_err_t status_register(status_cb_t cb);
|
||||
esp_err_t web_register_uri(const httpd_uri_t *uri);
|
||||
|
||||
// Start httpd on port 80 with the core routes (/, /api/status, /api/reboot).
|
||||
// Start httpd on port 80 with the core routes (/, /api/status, /api/config, /api/reboot).
|
||||
esp_err_t aes67_web_start(void);
|
||||
|
||||
// Send a cJSON object as the response body (application/json).
|
||||
|
||||
Reference in New Issue
Block a user