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>
This commit is contained in:
2026-09-26 07:58:26 +10:00
parent ee03d9f9f1
commit 5f27262b27
7 changed files with 61 additions and 13 deletions
+18 -2
View File
@@ -1,7 +1,10 @@
#include "aes67_ptp.h"
#include <string.h>
#include "aes67_cfg.h"
#include "aes67_web.h"
#include "esp_log.h"
#include "ptp_clock.h"
#include "ptp_hw.h"
@@ -14,7 +17,7 @@ static const char PTP_DEFAULTS[] =
static bool ptp_validate(const cJSON *g, char *err, size_t n)
{
static const char *const modes[] = { "multicast", "hybrid", NULL };
static const char *const roles[] = { "slave", "auto", "master", NULL };
static const char *const roles[] = { "slave", "auto", NULL };
return cfg_check_enum(g, "mode", modes, err, n) &&
cfg_check_enum(g, "role", roles, err, n) &&
cfg_check_int(g, "domain", 0, 127, err, n) &&
@@ -34,7 +37,20 @@ static void ptp_apply(const cJSON *g)
esp_err_t aes67_ptp_init(void)
{
return cfg_register("ptp", PTP_DEFAULTS, ptp_validate, ptp_apply);
esp_err_t err = cfg_register("ptp", PTP_DEFAULTS, ptp_validate, ptp_apply);
if (err != ESP_OK) {
return err;
}
// Role "master" was dropped (it behaved like auto; a low priority1 does the same). Stored values
// aren't re-validated, so convert it here.
cJSON *c = cfg_get("ptp");
bool master = !strcmp(cJSON_GetObjectItemCaseSensitive(c, "role")->valuestring, "master");
cJSON_Delete(c);
if (master) {
ESP_LOGW("ptp", "stored role \"master\" is no longer offered: now \"auto\" (same behaviour)");
cfg_set_string("ptp", "role", "auto", false);
}
return ESP_OK;
}
esp_err_t aes67_ptp_start(esp_eth_handle_t eth)
+3 -3
View File
@@ -622,8 +622,8 @@ static void load_config(void)
s.cfg_log_sync = cJSON_GetObjectItem(c, "log_sync")->valueint;
s.cfg_log_announce = cJSON_GetObjectItem(c, "log_announce")->valueint;
s.cfg_log_dreq = cJSON_GetObjectItem(c, "log_delay_req")->valueint;
// Roles (docs): slave = clockClass 255, never transmits; auto/master = 248 with the configured
// priorities (auto 250/250 by default, master e.g. p1 100).
// Roles (docs): slave = clockClass 255, never transmits; auto = 248 with the configured priorities
// (250/250 by default: loses to any real GM; a lower priority1 prefers this device).
s.slave_only = strcmp(cJSON_GetObjectItem(c, "role")->valuestring, "slave") == 0;
s.hybrid = strcmp(cJSON_GetObjectItem(c, "mode")->valuestring, "hybrid") == 0;
s.own_class = s.slave_only ? 255 : 248;
@@ -711,7 +711,7 @@ static void ptp_task(void *arg)
int tos2 = s.dscp << 2;
setsockopt(s.gen, IPPROTO_IP, IP_TOS, &tos2, sizeof(tos2));
ESP_LOGI(TAG, "config applied: role %s, mode %s, p1 %u p2 %u, domain %u",
s.slave_only ? "slave" : "auto/master", s.hybrid ? "hybrid" : "multicast",
s.slave_only ? "slave" : "auto", s.hybrid ? "hybrid" : "multicast",
s.own.p1, s.own.p2, s.domain);
// Re-run the decision: a foreign GM worse than our new dataset is dropped; as
// TimeTransmitter with role slave we stop.
+31
View File
@@ -295,6 +295,37 @@ esp_err_t cfg_set_number(const char *group, const char *key, double value, bool
return err;
}
esp_err_t cfg_set_string(const char *group, const char *key, const char *value, bool apply)
{
if (!s_lock) {
return ESP_ERR_INVALID_STATE;
}
xSemaphoreTake(s_lock, portMAX_DELAY);
cfg_group_t *g = find(group);
cJSON *cur = g ? cJSON_GetObjectItemCaseSensitive(g->values, key) : NULL;
esp_err_t err = ESP_OK;
if (!cur || !cJSON_IsString(cur)) {
err = ESP_ERR_NOT_FOUND;
} else {
cJSON_ReplaceItemInObjectCaseSensitive(g->values, key, cJSON_CreateString(value));
nvs_handle_t h;
if (s_nvs_ok && (err = nvs_open(NVS_NAMESPACE, NVS_READWRITE, &h)) == ESP_OK) {
err = store(h, g);
if (err == ESP_OK) {
err = nvs_commit(h);
}
nvs_close(h);
}
}
cJSON *copy = err == ESP_OK && apply && g->apply ? cJSON_Duplicate(g->values, true) : NULL;
xSemaphoreGive(s_lock);
if (copy) {
g->apply(copy);
cJSON_Delete(copy);
}
return err;
}
/* ----- HTTP ----- */
static esp_err_t config_get(httpd_req_t *req)
+2
View File
@@ -26,6 +26,8 @@ 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);