fix: mdns lookup failing due to early free
This commit is contained in:
+26
-28
@@ -8,7 +8,6 @@
|
||||
#include "esp_event.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_bt.h"
|
||||
#include "mdns.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
@@ -48,15 +47,8 @@ static void on_ble_scan_result(const char *tag_id, int8_t rssi)
|
||||
|
||||
static void wifi_init_sta(void)
|
||||
{
|
||||
esp_netif_create_default_wifi_sta();
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, NULL);
|
||||
esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler, NULL);
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
ESP_ERROR_CHECK(esp_wifi_connect());
|
||||
}
|
||||
|
||||
@@ -75,14 +67,24 @@ static bool resolve_mqtt_broker_mdns(char *host_out, size_t host_len, uint16_t *
|
||||
|
||||
mdns_result_t *r = results;
|
||||
while (r) {
|
||||
if (r->hostname && r->port) {
|
||||
strncpy(host_out, r->hostname, host_len - 1);
|
||||
*port_out = r->port;
|
||||
|
||||
ESP_LOGI(TAG, "mDNS found broker: %s:%u", host_out, *port_out);
|
||||
|
||||
found = true;
|
||||
goto free_results;
|
||||
if (r->port) {
|
||||
if (r->addr) {
|
||||
snprintf(host_out, host_len, IPSTR, IP2STR(&r->addr->addr.u_addr.ip4));
|
||||
*port_out = r->port;
|
||||
ESP_LOGI(TAG, "mDNS found broker: %s:%u", host_out, *port_out);
|
||||
found = true;
|
||||
goto free_results;
|
||||
} else if (r->hostname) {
|
||||
esp_ip4_addr_t ip4;
|
||||
if (mdns_query_a(r->hostname, MDNS_QUERY_TIMEOUT_MS, &ip4) == ESP_OK) {
|
||||
snprintf(host_out, host_len, IPSTR, IP2STR(&ip4));
|
||||
*port_out = r->port;
|
||||
ESP_LOGI(TAG, "mDNS found broker: %s:%u", host_out, *port_out);
|
||||
found = true;
|
||||
goto free_results;
|
||||
}
|
||||
ESP_LOGW(TAG, "mDNS A query for %s failed", r->hostname);
|
||||
}
|
||||
}
|
||||
r = r->next;
|
||||
}
|
||||
@@ -106,21 +108,17 @@ void app_main(void)
|
||||
|
||||
s_evt = xEventGroupCreate();
|
||||
|
||||
// Provisioning
|
||||
/* WiFi hardware must be up before the provisioning manager runs */
|
||||
esp_netif_create_default_wifi_sta();
|
||||
wifi_init_config_t wcfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&wcfg));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
if (!config_store_is_provisioned()) {
|
||||
ESP_LOGI(TAG, "Not provisioned - starting BLE provisioning");
|
||||
|
||||
/* WiFi must be started (STA mode) before provisioning manager */
|
||||
esp_netif_create_default_wifi_sta();
|
||||
wifi_init_config_t wcfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&wcfg));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
provisioning_run(); /* blocks until complete */
|
||||
|
||||
/* Release BT memory now that provisioning used it; we reinit below for scanning */
|
||||
esp_bt_mem_release(ESP_BT_MODE_BTDM);
|
||||
/* BT memory released by NETWORK_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM */
|
||||
}
|
||||
|
||||
// connect to wifi
|
||||
|
||||
Reference in New Issue
Block a user