Skip to content

Commit 6e0822f

Browse files
Send partial client info if MAC is unavailable
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 1f12583 commit 6e0822f

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/main.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,27 @@ static void websocket_event_handler(void *handler_args, esp_event_base_t base,
5858
char mac_str[18];
5959
char client_info[256];
6060

61+
int len;
6162
if (wifi_get_mac(mac) == 0) {
6263
snprintf(mac_str, sizeof(mac_str), "%02x:%02x:%02x:%02x:%02x:%02x",
6364
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
65+
ESP_LOGI(TAG, "MAC address obtained: %s", mac_str);
6466

65-
snprintf(client_info, sizeof(client_info),
67+
len = snprintf(client_info, sizeof(client_info),
6668
"{\"client_info\":{\"firmware_version\":\"%s\",\"firmware_type\":\"ESP32\",\"protocol_version\":%d,\"mac\":\"%s\"}}",
6769
FIRMWARE_VERSION, WEBSOCKET_PROTOCOL_VERSION, mac_str);
70+
} else {
71+
ESP_LOGW(TAG, "Failed to get MAC address; sending client info without MAC.");
72+
len = snprintf(client_info, sizeof(client_info),
73+
"{\"client_info\":{\"firmware_version\":\"%s\",\"firmware_type\":\"ESP32\",\"protocol_version\":%d}}",
74+
FIRMWARE_VERSION, WEBSOCKET_PROTOCOL_VERSION);
75+
}
6876

77+
if (len > 0 && len < sizeof(client_info)) {
6978
ESP_LOGI(TAG, "Sending client info: %s", client_info);
70-
esp_websocket_client_send_text(ws_handle, client_info, strlen(client_info), portMAX_DELAY);
79+
esp_websocket_client_send_text(ws_handle, client_info, len, portMAX_DELAY);
80+
} else {
81+
ESP_LOGE(TAG, "Failed to create client info string or it was truncated. Length: %d, Buffer size: %zu", len, sizeof(client_info));
7182
}
7283
}
7384
break;

0 commit comments

Comments
 (0)