Add Typed Admin Network Settings
This commit is contained in:
+51
-6
@@ -12,6 +12,7 @@
|
||||
|
||||
static SemaphoreHandle_t s_mutex;
|
||||
static mdns_config_t s_config;
|
||||
static uint32_t s_config_generation;
|
||||
static bool s_component_initialized;
|
||||
static bool s_initialization_failed;
|
||||
static bool s_announced;
|
||||
@@ -45,6 +46,7 @@ esp_err_t mdns_service_init(const mdns_config_t *config)
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
s_config = *config;
|
||||
s_config_generation = 1;
|
||||
s_last_error = ESP_OK;
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -66,27 +68,68 @@ esp_err_t mdns_service_set_config(const mdns_config_t *config)
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
lock_service();
|
||||
if (s_config_generation == UINT32_MAX) { unlock_service(); return ESP_ERR_INVALID_STATE; }
|
||||
s_config = *config;
|
||||
++s_config_generation;
|
||||
unlock_service();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t mdns_service_get_snapshot(mdns_service_snapshot_t *snapshot)
|
||||
static void snapshot_locked(mdns_service_snapshot_t *snapshot)
|
||||
{
|
||||
if (snapshot == NULL || s_mutex == NULL) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
lock_service();
|
||||
memset(snapshot, 0, sizeof(*snapshot));
|
||||
snapshot->config_generation = s_config_generation;
|
||||
snapshot->initialized = true;
|
||||
snapshot->announced = s_announced;
|
||||
memcpy(snapshot->suffix, s_config.suffix, s_config.suffix_len);
|
||||
make_hostname(&s_config, snapshot->hostname, sizeof(snapshot->hostname));
|
||||
snapshot->last_error = s_last_error;
|
||||
}
|
||||
|
||||
esp_err_t mdns_service_get_snapshot(mdns_service_snapshot_t *snapshot)
|
||||
{
|
||||
if (!snapshot || !s_mutex) return ESP_ERR_INVALID_STATE;
|
||||
lock_service();
|
||||
snapshot_locked(snapshot);
|
||||
unlock_service();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t mdns_service_get_settings(mdns_service_snapshot_t *snapshot)
|
||||
{
|
||||
if (!snapshot) return ESP_ERR_INVALID_ARG;
|
||||
memset(snapshot, 0, sizeof(*snapshot));
|
||||
if (!s_mutex) return ESP_ERR_INVALID_STATE;
|
||||
if (xSemaphoreTake(s_mutex, 0) != pdTRUE) return ESP_ERR_TIMEOUT;
|
||||
snapshot_locked(snapshot);
|
||||
unlock_service();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t mdns_service_update_current(uint32_t generation, mdns_settings_action_t action,
|
||||
const mdns_config_t *config, bool *stored)
|
||||
{
|
||||
if (!stored || action > MDNS_SETTINGS_DEFAULTS || action < MDNS_SETTINGS_SET ||
|
||||
(action == MDNS_SETTINGS_SET && mdns_config_validate(config) != ESP_OK)) return ESP_ERR_INVALID_ARG;
|
||||
*stored = true;
|
||||
if (!s_mutex) return ESP_ERR_INVALID_STATE;
|
||||
lock_service();
|
||||
if (!generation || generation != s_config_generation) { unlock_service(); return ESP_ERR_NOT_FOUND; }
|
||||
esp_err_t error = ESP_OK;
|
||||
mdns_config_t candidate = s_config;
|
||||
if (action == MDNS_SETTINGS_SAVE) error = mdns_config_save(&s_config);
|
||||
else if (s_config_generation == UINT32_MAX) error = ESP_ERR_INVALID_STATE;
|
||||
else {
|
||||
if (action == MDNS_SETTINGS_SET) candidate = *config;
|
||||
else if (action == MDNS_SETTINGS_LOAD) error = mdns_config_load(&candidate, stored);
|
||||
else mdns_config_defaults(&candidate);
|
||||
if (error == ESP_OK) error = mdns_config_validate(&candidate);
|
||||
if (error == ESP_OK) { s_config = candidate; ++s_config_generation; }
|
||||
}
|
||||
unlock_service();
|
||||
return error;
|
||||
}
|
||||
|
||||
esp_err_t mdns_service_start(void)
|
||||
{
|
||||
if (s_mutex == NULL) {
|
||||
@@ -96,7 +139,9 @@ esp_err_t mdns_service_start(void)
|
||||
if (s_component_initialized) {
|
||||
s_announced = true;
|
||||
unlock_service();
|
||||
return ESP_OK;
|
||||
/* A suffix staged while offline must reach the already-created responder
|
||||
* when the next STA IP arrives, even if its reannounce command ran offline. */
|
||||
return mdns_service_reannounce();
|
||||
}
|
||||
if (s_initialization_failed) {
|
||||
esp_err_t error = s_last_error;
|
||||
|
||||
Reference in New Issue
Block a user