From a87151cc9c565ae08e956b8d887ec82c2f7a1db0 Mon Sep 17 00:00:00 2001 From: user Date: Wed, 4 Feb 2026 18:40:47 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20Add=20runtime=20HOSTNAME=20command=20?= =?UTF-8?q?=E2=80=94=20NVS=20persisted,=20mDNS=20updated?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace all CONFIG_CSI_HOSTNAME usage with runtime s_hostname variable. Load from NVS on boot (falls back to Kconfig default). HOSTNAME sets hostname, persists to NVS, updates mDNS live. HOSTNAME (no arg) queries current value. One firmware binary now works for all devices. --- get-started/csi_recv_router/main/app_main.c | 47 ++++++++++++++++----- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/get-started/csi_recv_router/main/app_main.c b/get-started/csi_recv_router/main/app_main.c index 69b528d..6d3d232 100644 --- a/get-started/csi_recv_router/main/app_main.c +++ b/get-started/csi_recv_router/main/app_main.c @@ -120,12 +120,15 @@ static struct sockaddr_in s_dest_addr; static char s_udp_buffer[2048]; static char s_target_ip[16]; /* runtime target IP (NVS or Kconfig default) */ static uint16_t s_target_port; /* runtime target port */ +static char s_hostname[32]; /* runtime hostname (NVS or Kconfig default) */ /* --- NVS helpers --- */ static void config_load_nvs(void) { - /* Start with Kconfig defaults for target */ + /* Start with Kconfig defaults */ + strncpy(s_hostname, CONFIG_CSI_HOSTNAME, sizeof(s_hostname) - 1); + s_hostname[sizeof(s_hostname) - 1] = '\0'; strncpy(s_target_ip, CONFIG_CSI_UDP_TARGET_IP, sizeof(s_target_ip) - 1); s_target_ip[sizeof(s_target_ip) - 1] = '\0'; s_target_port = CONFIG_CSI_UDP_TARGET_PORT; @@ -158,9 +161,11 @@ static void config_load_nvs(void) if (nvs_get_i32(h, "target_port", &port) == ESP_OK && port > 0 && port <= 65535) { s_target_port = (uint16_t)port; } + size_t hn_len = sizeof(s_hostname); + nvs_get_str(h, "hostname", s_hostname, &hn_len); nvs_close(h); - ESP_LOGI(TAG, "NVS loaded: rate=%d tx_power=%d adaptive=%d threshold=%.6f ble=%d target=%s:%d", - s_send_frequency, s_tx_power_dbm, s_adaptive, s_motion_threshold, s_ble_enabled, + ESP_LOGI(TAG, "NVS loaded: hostname=%s rate=%d tx_power=%d adaptive=%d threshold=%.6f ble=%d target=%s:%d", + s_hostname, s_send_frequency, s_tx_power_dbm, s_adaptive, s_motion_threshold, s_ble_enabled, s_target_ip, s_target_port); } else { ESP_LOGI(TAG, "NVS: no saved config, using defaults"); @@ -320,7 +325,7 @@ static void wifi_csi_rx_cb(void *ctx, wifi_csi_info_t *info) } pos = snprintf(s_udp_buffer, sizeof(s_udp_buffer), "CSI_DATA,%s,%d," MACSTR ",%d,%d,%d,%d,%d,%d,%d,%d,%d", - CONFIG_CSI_HOSTNAME, s_count, MAC2STR(info->mac), rx_ctrl->rssi, rx_ctrl->rate, + s_hostname, s_count, MAC2STR(info->mac), rx_ctrl->rssi, rx_ctrl->rate, rx_ctrl->noise_floor, fft_gain, agc_gain, rx_ctrl->channel, rx_ctrl->timestamp, rx_ctrl->sig_len, rx_ctrl->rx_state); #else @@ -329,7 +334,7 @@ static void wifi_csi_rx_cb(void *ctx, wifi_csi_info_t *info) } pos = snprintf(s_udp_buffer, sizeof(s_udp_buffer), "CSI_DATA,%s,%d," MACSTR ",%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", - CONFIG_CSI_HOSTNAME, s_count, MAC2STR(info->mac), rx_ctrl->rssi, rx_ctrl->rate, rx_ctrl->sig_mode, + s_hostname, s_count, MAC2STR(info->mac), rx_ctrl->rssi, rx_ctrl->rate, rx_ctrl->sig_mode, rx_ctrl->mcs, rx_ctrl->cwb, rx_ctrl->smoothing, rx_ctrl->not_sounding, rx_ctrl->aggregation, rx_ctrl->stbc, rx_ctrl->fec_coding, rx_ctrl->sgi, rx_ctrl->noise_floor, rx_ctrl->ampdu_cnt, rx_ctrl->channel, rx_ctrl->secondary_channel, @@ -521,7 +526,7 @@ static int ble_gap_event_cb(struct ble_gap_event *event, void *arg) char buf[160]; int len = snprintf(buf, sizeof(buf), "BLE_DATA,%s,%02x:%02x:%02x:%02x:%02x:%02x,%d,%s,%s\n", - CONFIG_CSI_HOSTNAME, + s_hostname, disc->addr.val[5], disc->addr.val[4], disc->addr.val[3], disc->addr.val[2], disc->addr.val[1], disc->addr.val[0], disc->rssi, @@ -637,7 +642,7 @@ static void adaptive_task(void *arg) char event[128]; int len = snprintf(event, sizeof(event), "EVENT,%s,motion=%d rate=%d wander=%.6f", - CONFIG_CSI_HOSTNAME, motion ? 1 : 0, target_rate, wander); + s_hostname, motion ? 1 : 0, target_rate, wander); if (s_udp_socket >= 0) { sendto(s_udp_socket, event, len, 0, (struct sockaddr *)&s_dest_addr, sizeof(s_dest_addr)); @@ -719,7 +724,7 @@ static void wifi_promiscuous_cb(void *buf, wifi_promiscuous_pkt_type_t type) "%02x:%02x:%02x:%02x:%02x:%02x," "%02x:%02x:%02x:%02x:%02x:%02x," "%d\n", - CONFIG_CSI_HOSTNAME, type_str, + s_hostname, type_str, hdr->addr2[0], hdr->addr2[1], hdr->addr2[2], hdr->addr2[3], hdr->addr2[4], hdr->addr2[5], hdr->addr1[0], hdr->addr1[1], hdr->addr1[2], @@ -811,7 +816,7 @@ static int cmd_handle(const char *cmd, char *reply, size_t reply_size) snprintf(reply, reply_size, "OK STATUS uptime=%s heap=%lu rssi=%d tx_power=%d rate=%d hostname=%s version=%s adaptive=%s motion=%d ble=%s target=%s:%d temp=%.1f", uptime_str, (unsigned long)heap, rssi, (int)s_tx_power_dbm, - s_send_frequency, CONFIG_CSI_HOSTNAME, app_desc->version, + s_send_frequency, s_hostname, app_desc->version, s_adaptive ? "on" : "off", s_motion_detected ? 1 : 0, s_ble_enabled ? "on" : "off", s_target_ip, s_target_port, chip_temp); @@ -883,6 +888,26 @@ static int cmd_handle(const char *cmd, char *reply, size_t reply_size) return strlen(reply); } + /* HOSTNAME */ + if (strncmp(cmd, "HOSTNAME ", 9) == 0) { + const char *name = cmd + 9; + size_t nlen = strlen(name); + if (nlen == 0 || nlen >= sizeof(s_hostname)) { + snprintf(reply, reply_size, "ERR HOSTNAME length 1-%d", (int)sizeof(s_hostname) - 1); + return strlen(reply); + } + strncpy(s_hostname, name, sizeof(s_hostname) - 1); + s_hostname[sizeof(s_hostname) - 1] = '\0'; + config_save_str("hostname", s_hostname); + mdns_hostname_set(s_hostname); + snprintf(reply, reply_size, "OK HOSTNAME %s (mDNS updated, reboot recommended)", s_hostname); + return strlen(reply); + } + if (strcmp(cmd, "HOSTNAME") == 0) { + snprintf(reply, reply_size, "OK HOSTNAME %s", s_hostname); + return strlen(reply); + } + /* BLE ON/OFF */ if (strncmp(cmd, "BLE ", 4) == 0) { const char *arg = cmd + 4; @@ -1102,9 +1127,9 @@ void app_main() /* mDNS: announce as .local */ ESP_ERROR_CHECK(mdns_init()); - mdns_hostname_set(CONFIG_CSI_HOSTNAME); + mdns_hostname_set(s_hostname); mdns_instance_name_set("ESP32 CSI Sensor"); - ESP_LOGI(TAG, "mDNS hostname: %s.local", CONFIG_CSI_HOSTNAME); + ESP_LOGI(TAG, "mDNS hostname: %s.local", s_hostname); /* Watchdog: 30s timeout, auto-reboot on hang */ esp_task_wdt_config_t wdt_cfg = {