Files
bsncubed 5f27262b27 PTP: drop the "Preferred TimeTransmitter" role
It behaved exactly like auto (clock class 248); the only difference was a
priority1 suggestion in the page. Roles are now slave|auto; to prefer this
device, use auto with a low priority1. A stored "master" is converted to
auto at boot (new cfg_set_string, like cfg_set_number).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 07:58:26 +10:00

39 lines
2.4 KiB
C

// 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);
// Write-only key (e.g. a password): GET /api/config returns "" for it; a POST with "" keeps the
// stored value, null clears it. cfg_get() still returns the real value. Call after cfg_register().
esp_err_t cfg_mark_secret(const char *group, const char *key);
// Copy of a group's current values; caller frees with cJSON_Delete. NULL if not registered.
cJSON *cfg_get(const char *group);
// Firmware-side change of one number (e.g. aes67.session_ver): stored like a POST, no validation.
// apply = false skips the group's apply callback (e.g. to avoid restarting the stream).
esp_err_t cfg_set_number(const char *group, const char *key, double value, bool apply);
// Same for a string value.
esp_err_t cfg_set_string(const char *group, const char *key, const char *value, bool apply);
// 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);