espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.4k stars 7.37k forks source link

Crash on mdns_query_a(). #1281

Closed CelliesProjects closed 6 years ago

CelliesProjects commented 6 years ago

Hardware:

Board: MH ET LIVE d1 mini ESP32 Core Installation/update date: 30 march 2018 3a4ec66d41615cbb1c3e830cb6e761cdc4cca9d3 IDE name: Arduino IDE Flash Frequency: 80Mhz Upload Speed: 115200

Description:

Afaik it is impossible to start the mDNS service without assigning a name at the same time. Something like MDNS.begin("ESP32_Browser") will assign a name directly.

But I would like to query if a avahi hostname is already on my network before I assign it to a device.

This mDNS test code (adapted from https://github.com/espressif/esp-idf/blob/master/docs/en/api-reference/protocols/mdns.rst ) will cause an error and reboot the esp32. The crash is triggered by the esp_err_t err = mdns_query_a(host_name, 2000, &addr) statement. According to the exception decoder, an error is thrown from within the mDNS component.

So scanning for existing hostname ( without assigning one ) is impossible in Arduino IDE?

Sketch:

#include <ESPmDNS.h>               
#include <WiFi.h>               

char hostName[30] = "example";

void setup() {
  // put your setup code here, to run once:
  WiFi.begin();
  while ( !WiFi.isConnected() )
  {
    delay(100);
  }
  ESP_LOGI( TAG, "Wifi connected" );

  //initialize mDNS service
  esp_err_t err = mdns_init();
  if (err)
  {
    ESP_LOGI( TAG, "MDNS Init failed: %d\n", err );
  }

  resolve_mdns_host( hostName );
}

void loop() {
  // put your main code here, to run repeatedly:

}

void resolve_mdns_host(const char * host_name)
{
    ESP_LOGI( TAG, "Query A: %s.local", host_name);

    struct ip4_addr addr;
    addr.addr = 0;

    esp_err_t err = mdns_query_a(host_name, 2000,  &addr);
    if(err)
    {
      if(err == ESP_ERR_NOT_FOUND)
      {
        ESP_LOGI( TAG, "Host was not found!");
        return;
      }
      ESP_LOGI( TAG, "Query Failed");
      return;
    }
    ESP_LOGI( TAG, IPSTR, IP2STR(&addr));
}

Debug Messages:

entry 0x40078a58
[D][WiFiGeneric.cpp:293] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:293] _eventCallback(): Event: 4 - STA_CONNECTED
[D][WiFiGeneric.cpp:293] _eventCallback(): Event: 7 - STA_GOT_IP
[I][testMDNS.ino:13] setup(): Wifi connected
[I][testMDNS.ino:32] resolve_mdns_host(): Query A: example.local
Guru Meditation Error: Core  0 panic'ed (LoadProhibited)
. Exception was unhandled.
Register dump:
PC      : 0x400011f1  PS      : 0x00060230  A0      : 0x800d47e1  A1      : 0x3ffdb3b0  
A2      : 0x00000031  A3      : 0x00000000  A4      : 0x00000040  A5      : 0x3ffc3df8  
A6      : 0x00000000  A7      : 0x00000000  A8      : 0x00000000  A9      : 0x00000000  
A10     : 0x00000051  A11     : 0x3ff96354  A12     : 0x3ffc3cf4  A13     : 0x00000020  
A14     : 0x00000000  A15     : 0x2d383631  SAR     : 0x00000010  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000000  LBEG    : 0x4000c349  LEND    : 0x4000c36b  LCOUNT  : 0x00000000  

Backtrace: 0x400011f1:0x3ffdb3b0 0x400d47de:0x3ffdb3d0 0x400d7e1f:0x3ffdb3f0 0x400d8bde:0x3ffdb470 0x400d8c36:0x3ffdb4a0

Exception decoded:

0x400d47ba: _mdns_name_is_ours at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/mdns/./mdns.c line 4614
0x400d7dfb: mdns_parse_packet at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/mdns/./mdns.c line 4614
0x400d8bba: _mdns_execute_action at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/mdns/./mdns.c line 4614
0x400d8c12: _mdns_service_task at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/mdns/./mdns.c line 4614
CelliesProjects commented 6 years ago

Fixed my issue with CelliesProjects/aquacontrol32@25d6f2097966ee96787db8ec0374e43f610e3a6d The statement

esp_err_t err = mdns_init();

Was the actual problem, it should include arguments referring to the WiFi interface. But the following code did the trick:

  if ( !MDNS.begin( "" ) )
  {
    ESP_LOGE( TAG, "Error setting up mDNS." );
  }
  else if ( !setupMDNS( hostName ) )
  {
    ESP_LOGE( TAG, "Error setting up %s as hostname. ", hostName );
  }

tl;dr: Just start MDNS with an empty hostname.