150 lines
4.4 KiB
C
150 lines
4.4 KiB
C
/* SPDX-License-Identifier: GPL-3.0-only */
|
|
/* Administration console for the station mDNS hostname. */
|
|
|
|
#include "mdns_console.h"
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "esp_console.h"
|
|
#include "mdns_config.h"
|
|
#include "mdns_service.h"
|
|
#include "wifi_manager.h"
|
|
|
|
static void print_usage(void)
|
|
{
|
|
printf("Usage: mdns status|suffix <lowercase-suffix>|save|load|defaults|reset\n");
|
|
}
|
|
|
|
static int request_reannounce(void)
|
|
{
|
|
esp_err_t error = wifi_manager_mdns_reannounce();
|
|
if (error != ESP_OK) {
|
|
printf("mDNS configuration updated; it will be used when Wi-Fi STA receives an IP (%s).\n",
|
|
esp_err_to_name(error));
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int show_status(void)
|
|
{
|
|
mdns_service_snapshot_t snapshot;
|
|
esp_err_t error = mdns_service_get_snapshot(&snapshot);
|
|
if (error != ESP_OK) {
|
|
printf("mDNS unavailable: %s\n", esp_err_to_name(error));
|
|
return 1;
|
|
}
|
|
printf("mDNS: initialized=%s announced=%s hostname=%s.local suffix=%s last-error=%s\n",
|
|
snapshot.initialized ? "yes" : "no", snapshot.announced ? "yes" : "no",
|
|
snapshot.hostname, snapshot.suffix, esp_err_to_name(snapshot.last_error));
|
|
return 0;
|
|
}
|
|
|
|
static int set_suffix(const char *suffix)
|
|
{
|
|
size_t length = strlen(suffix);
|
|
mdns_config_t config;
|
|
esp_err_t error = mdns_service_get_config(&config);
|
|
if (error == ESP_OK) {
|
|
memset(config.suffix, 0, sizeof(config.suffix));
|
|
if (length <= MDNS_CONFIG_SUFFIX_MAX_LEN) {
|
|
memcpy(config.suffix, suffix, length);
|
|
config.suffix_len = (uint8_t)length;
|
|
error = mdns_service_set_config(&config);
|
|
} else {
|
|
error = ESP_ERR_INVALID_ARG;
|
|
}
|
|
}
|
|
if (error != ESP_OK) {
|
|
printf("Suffix must be 1..%u lowercase letters, digits, or hyphens, and cannot begin or end with a hyphen.\n",
|
|
MDNS_CONFIG_SUFFIX_MAX_LEN);
|
|
return 1;
|
|
}
|
|
printf("mDNS suffix updated in RAM; hostname is sak-%s.local; use 'mdns save' to persist it.\n",
|
|
suffix);
|
|
return request_reannounce();
|
|
}
|
|
|
|
static int save_config(void)
|
|
{
|
|
mdns_config_t config;
|
|
esp_err_t error = mdns_service_get_config(&config);
|
|
if (error == ESP_OK) {
|
|
error = mdns_config_save(&config);
|
|
}
|
|
if (error != ESP_OK) {
|
|
printf("Could not save mDNS configuration: %s\n", esp_err_to_name(error));
|
|
return 1;
|
|
}
|
|
printf("mDNS configuration saved to NVS.\n");
|
|
return 0;
|
|
}
|
|
|
|
static int load_config(void)
|
|
{
|
|
mdns_config_t config;
|
|
bool stored = false;
|
|
esp_err_t error = mdns_config_load(&config, &stored);
|
|
if (error == ESP_OK) {
|
|
error = mdns_service_set_config(&config);
|
|
}
|
|
if (error != ESP_OK) {
|
|
printf("Could not load mDNS configuration: %s\n", esp_err_to_name(error));
|
|
return 1;
|
|
}
|
|
printf("Loaded %s mDNS configuration into RAM.\n", stored ? "stored" : "default");
|
|
return request_reannounce();
|
|
}
|
|
|
|
static int apply_defaults(bool persist)
|
|
{
|
|
mdns_config_t config;
|
|
mdns_config_defaults(&config);
|
|
esp_err_t error = mdns_service_set_config(&config);
|
|
if (error == ESP_OK && persist) {
|
|
error = mdns_config_save(&config);
|
|
}
|
|
if (error != ESP_OK) {
|
|
printf("Could not apply mDNS defaults: %s\n", esp_err_to_name(error));
|
|
return 1;
|
|
}
|
|
printf("MAC-derived mDNS defaults applied%s.\n", persist ? " and saved" : " in RAM");
|
|
return request_reannounce();
|
|
}
|
|
|
|
static int command_mdns(int argc, char **argv)
|
|
{
|
|
if (argc == 1 || (argc == 2 && strcmp(argv[1], "status") == 0)) {
|
|
return show_status();
|
|
}
|
|
if (argc == 3 && strcmp(argv[1], "suffix") == 0) {
|
|
return set_suffix(argv[2]);
|
|
}
|
|
if (argc == 2 && strcmp(argv[1], "save") == 0) {
|
|
return save_config();
|
|
}
|
|
if (argc == 2 && strcmp(argv[1], "load") == 0) {
|
|
return load_config();
|
|
}
|
|
if (argc == 2 && strcmp(argv[1], "defaults") == 0) {
|
|
return apply_defaults(false);
|
|
}
|
|
if (argc == 2 && strcmp(argv[1], "reset") == 0) {
|
|
return apply_defaults(true);
|
|
}
|
|
print_usage();
|
|
return 1;
|
|
}
|
|
|
|
esp_err_t mdns_console_register_commands(void)
|
|
{
|
|
const esp_console_cmd_t command = {
|
|
.command = "mdns",
|
|
.help = "Configure the STA mDNS hostname; use 'mdns' for status",
|
|
.hint = NULL,
|
|
.func = &command_mdns,
|
|
.argtable = NULL,
|
|
};
|
|
return esp_console_cmd_register(&command);
|
|
}
|