2017-01-04 11:54:07 -05:00
|
|
|
/* MDNS-SD Query and advertise Example
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "esp_system.h"
|
2018-11-21 00:52:08 -05:00
|
|
|
#include "esp_event.h"
|
2017-01-04 11:54:07 -05:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "nvs_flash.h"
|
2019-08-31 13:06:54 -04:00
|
|
|
#include "esp_netif.h"
|
2018-11-21 00:52:08 -05:00
|
|
|
#include "protocol_examples_common.h"
|
2017-01-04 11:54:07 -05:00
|
|
|
#include "mdns.h"
|
2017-12-07 08:21:40 -05:00
|
|
|
#include "driver/gpio.h"
|
2019-08-31 13:06:54 -04:00
|
|
|
#include "netdb.h"
|
2017-01-04 11:54:07 -05:00
|
|
|
|
|
|
|
|
|
|
|
#define EXAMPLE_MDNS_INSTANCE CONFIG_MDNS_INSTANCE
|
2018-11-21 00:52:08 -05:00
|
|
|
#define EXAMPLE_BUTTON_GPIO 0
|
2017-01-04 11:54:07 -05:00
|
|
|
|
|
|
|
static const char *TAG = "mdns-test";
|
2019-07-16 05:33:30 -04:00
|
|
|
static char* generate_hostname(void);
|
2017-01-04 11:54:07 -05:00
|
|
|
|
2019-06-19 07:20:58 -04:00
|
|
|
#if CONFIG_MDNS_RESOLVE_TEST_SERVICES == 1
|
|
|
|
static void query_mdns_host_with_gethostbyname(char * host);
|
|
|
|
static void query_mdns_host_with_getaddrinfo(char * host);
|
|
|
|
#endif
|
|
|
|
|
2017-12-07 08:21:40 -05:00
|
|
|
static void initialise_mdns(void)
|
2017-01-04 11:54:07 -05:00
|
|
|
{
|
2019-01-25 14:24:30 -05:00
|
|
|
char* hostname = generate_hostname();
|
2017-12-07 08:21:40 -05:00
|
|
|
//initialize mDNS
|
|
|
|
ESP_ERROR_CHECK( mdns_init() );
|
|
|
|
//set mDNS hostname (required if you want to advertise services)
|
2018-10-12 10:45:52 -04:00
|
|
|
ESP_ERROR_CHECK( mdns_hostname_set(hostname) );
|
|
|
|
ESP_LOGI(TAG, "mdns hostname set to: [%s]", hostname);
|
2017-12-07 08:21:40 -05:00
|
|
|
//set default mDNS instance name
|
|
|
|
ESP_ERROR_CHECK( mdns_instance_name_set(EXAMPLE_MDNS_INSTANCE) );
|
|
|
|
|
|
|
|
//structure with TXT records
|
|
|
|
mdns_txt_item_t serviceTxtData[3] = {
|
|
|
|
{"board","esp32"},
|
|
|
|
{"u","user"},
|
|
|
|
{"p","password"}
|
|
|
|
};
|
|
|
|
|
|
|
|
//initialize service
|
|
|
|
ESP_ERROR_CHECK( mdns_service_add("ESP32-WebServer", "_http", "_tcp", 80, serviceTxtData, 3) );
|
|
|
|
//add another TXT item
|
|
|
|
ESP_ERROR_CHECK( mdns_service_txt_item_set("_http", "_tcp", "path", "/foobar") );
|
|
|
|
//change TXT item value
|
|
|
|
ESP_ERROR_CHECK( mdns_service_txt_item_set("_http", "_tcp", "u", "admin") );
|
2019-01-25 14:24:30 -05:00
|
|
|
free(hostname);
|
2017-12-07 08:21:40 -05:00
|
|
|
}
|
|
|
|
|
2018-11-21 00:52:08 -05:00
|
|
|
/* these strings match tcpip_adapter_if_t enumeration */
|
2017-12-07 08:21:40 -05:00
|
|
|
static const char * if_str[] = {"STA", "AP", "ETH", "MAX"};
|
2018-11-21 00:52:08 -05:00
|
|
|
|
|
|
|
/* these strings match mdns_ip_protocol_t enumeration */
|
2017-12-07 08:21:40 -05:00
|
|
|
static const char * ip_protocol_str[] = {"V4", "V6", "MAX"};
|
|
|
|
|
|
|
|
static void mdns_print_results(mdns_result_t * results){
|
|
|
|
mdns_result_t * r = results;
|
|
|
|
mdns_ip_addr_t * a = NULL;
|
|
|
|
int i = 1, t;
|
|
|
|
while(r){
|
|
|
|
printf("%d: Interface: %s, Type: %s\n", i++, if_str[r->tcpip_if], ip_protocol_str[r->ip_protocol]);
|
|
|
|
if(r->instance_name){
|
|
|
|
printf(" PTR : %s\n", r->instance_name);
|
|
|
|
}
|
|
|
|
if(r->hostname){
|
|
|
|
printf(" SRV : %s.local:%u\n", r->hostname, r->port);
|
|
|
|
}
|
|
|
|
if(r->txt_count){
|
|
|
|
printf(" TXT : [%u] ", r->txt_count);
|
|
|
|
for(t=0; t<r->txt_count; t++){
|
2018-04-30 03:03:35 -04:00
|
|
|
printf("%s=%s; ", r->txt[t].key, r->txt[t].value?r->txt[t].value:"NULL");
|
2017-01-04 11:54:07 -05:00
|
|
|
}
|
2017-12-07 08:21:40 -05:00
|
|
|
printf("\n");
|
2017-01-04 11:54:07 -05:00
|
|
|
}
|
2017-12-07 08:21:40 -05:00
|
|
|
a = r->addr;
|
|
|
|
while(a){
|
2019-08-31 13:06:54 -04:00
|
|
|
if(a->addr.type == ESP_IPADDR_TYPE_V6){
|
2017-12-07 08:21:40 -05:00
|
|
|
printf(" AAAA: " IPV6STR "\n", IPV62STR(a->addr.u_addr.ip6));
|
|
|
|
} else {
|
|
|
|
printf(" A : " IPSTR "\n", IP2STR(&(a->addr.u_addr.ip4)));
|
2017-01-04 11:54:07 -05:00
|
|
|
}
|
2017-12-07 08:21:40 -05:00
|
|
|
a = a->next;
|
2017-01-04 11:54:07 -05:00
|
|
|
}
|
2017-12-07 08:21:40 -05:00
|
|
|
r = r->next;
|
2017-01-04 11:54:07 -05:00
|
|
|
}
|
2017-12-07 08:21:40 -05:00
|
|
|
|
2017-01-04 11:54:07 -05:00
|
|
|
}
|
|
|
|
|
2017-12-07 08:21:40 -05:00
|
|
|
static void query_mdns_service(const char * service_name, const char * proto)
|
2017-01-04 11:54:07 -05:00
|
|
|
{
|
2017-12-07 08:21:40 -05:00
|
|
|
ESP_LOGI(TAG, "Query PTR: %s.%s.local", service_name, proto);
|
|
|
|
|
|
|
|
mdns_result_t * results = NULL;
|
|
|
|
esp_err_t err = mdns_query_ptr(service_name, proto, 3000, 20, &results);
|
|
|
|
if(err){
|
2018-02-22 08:48:53 -05:00
|
|
|
ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err));
|
2017-12-07 08:21:40 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!results){
|
|
|
|
ESP_LOGW(TAG, "No results found!");
|
|
|
|
return;
|
|
|
|
}
|
2017-01-04 11:54:07 -05:00
|
|
|
|
2017-12-07 08:21:40 -05:00
|
|
|
mdns_print_results(results);
|
|
|
|
mdns_query_results_free(results);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void query_mdns_host(const char * host_name)
|
|
|
|
{
|
|
|
|
ESP_LOGI(TAG, "Query A: %s.local", host_name);
|
|
|
|
|
2019-08-31 13:06:54 -04:00
|
|
|
struct esp_ip4_addr addr;
|
2017-12-07 08:21:40 -05:00
|
|
|
addr.addr = 0;
|
|
|
|
|
|
|
|
esp_err_t err = mdns_query_a(host_name, 2000, &addr);
|
|
|
|
if(err){
|
|
|
|
if(err == ESP_ERR_NOT_FOUND){
|
2018-02-22 08:48:53 -05:00
|
|
|
ESP_LOGW(TAG, "%s: Host was not found!", esp_err_to_name(err));
|
2017-12-07 08:21:40 -05:00
|
|
|
return;
|
2017-01-04 11:54:07 -05:00
|
|
|
}
|
2018-02-22 08:48:53 -05:00
|
|
|
ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err));
|
2017-12-07 08:21:40 -05:00
|
|
|
return;
|
|
|
|
}
|
2017-01-04 11:54:07 -05:00
|
|
|
|
2018-10-12 10:45:52 -04:00
|
|
|
ESP_LOGI(TAG, "Query A: %s.local resolved to: " IPSTR, host_name, IP2STR(&addr));
|
2017-12-07 08:21:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void initialise_button(void)
|
|
|
|
{
|
2018-11-21 00:52:08 -05:00
|
|
|
gpio_config_t io_conf = {0};
|
2017-12-07 08:21:40 -05:00
|
|
|
io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
|
2018-11-21 00:52:08 -05:00
|
|
|
io_conf.pin_bit_mask = BIT64(EXAMPLE_BUTTON_GPIO);
|
2017-12-07 08:21:40 -05:00
|
|
|
io_conf.mode = GPIO_MODE_INPUT;
|
|
|
|
io_conf.pull_up_en = 1;
|
|
|
|
io_conf.pull_down_en = 0;
|
|
|
|
gpio_config(&io_conf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void check_button(void)
|
|
|
|
{
|
|
|
|
static bool old_level = true;
|
2018-11-21 00:52:08 -05:00
|
|
|
bool new_level = gpio_get_level(EXAMPLE_BUTTON_GPIO);
|
2017-12-07 08:21:40 -05:00
|
|
|
if (!new_level && old_level) {
|
|
|
|
query_mdns_host("esp32");
|
|
|
|
query_mdns_service("_arduino", "_tcp");
|
|
|
|
query_mdns_service("_http", "_tcp");
|
|
|
|
query_mdns_service("_printer", "_tcp");
|
|
|
|
query_mdns_service("_ipp", "_tcp");
|
|
|
|
query_mdns_service("_afpovertcp", "_tcp");
|
|
|
|
query_mdns_service("_smb", "_tcp");
|
|
|
|
query_mdns_service("_ftp", "_tcp");
|
|
|
|
query_mdns_service("_nfs", "_tcp");
|
|
|
|
}
|
|
|
|
old_level = new_level;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mdns_example_task(void *pvParameters)
|
|
|
|
{
|
2019-05-09 10:43:06 -04:00
|
|
|
#if CONFIG_MDNS_RESOLVE_TEST_SERVICES == 1
|
2018-10-12 10:45:52 -04:00
|
|
|
/* Send initial queries that are started by CI tester */
|
|
|
|
query_mdns_host("tinytester");
|
2019-06-19 07:20:58 -04:00
|
|
|
query_mdns_host_with_gethostbyname("tinytester-lwip.local");
|
|
|
|
query_mdns_host_with_getaddrinfo("tinytester-lwip.local");
|
2018-10-12 10:45:52 -04:00
|
|
|
#endif
|
|
|
|
|
2017-12-07 08:21:40 -05:00
|
|
|
while(1) {
|
|
|
|
check_button();
|
|
|
|
vTaskDelay(50 / portTICK_PERIOD_MS);
|
2017-01-04 11:54:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2017-01-04 11:54:07 -05:00
|
|
|
{
|
2018-11-21 00:52:08 -05:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
2019-08-31 10:19:21 -04:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2018-11-21 00:52:08 -05:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
|
2018-11-21 00:46:45 -05:00
|
|
|
initialise_mdns();
|
2018-11-21 00:52:08 -05:00
|
|
|
|
|
|
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
|
|
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
|
|
|
* examples/protocols/README.md for more information about this function.
|
|
|
|
*/
|
|
|
|
ESP_ERROR_CHECK(example_connect());
|
|
|
|
|
2017-12-07 08:21:40 -05:00
|
|
|
initialise_button();
|
2017-03-22 00:36:11 -04:00
|
|
|
xTaskCreate(&mdns_example_task, "mdns_example_task", 2048, NULL, 5, NULL);
|
2017-01-04 11:54:07 -05:00
|
|
|
}
|
2019-01-25 14:24:30 -05:00
|
|
|
|
|
|
|
/** Generate host name based on sdkconfig, optionally adding a portion of MAC address to it.
|
|
|
|
* @return host name string allocated from the heap
|
|
|
|
*/
|
2019-07-16 05:33:30 -04:00
|
|
|
static char* generate_hostname(void)
|
2019-01-25 14:24:30 -05:00
|
|
|
{
|
|
|
|
#ifndef CONFIG_MDNS_ADD_MAC_TO_HOSTNAME
|
|
|
|
return strdup(CONFIG_MDNS_HOSTNAME);
|
|
|
|
#else
|
|
|
|
uint8_t mac[6];
|
|
|
|
char *hostname;
|
|
|
|
esp_read_mac(mac, ESP_MAC_WIFI_STA);
|
|
|
|
if (-1 == asprintf(&hostname, "%s-%02X%02X%02X", CONFIG_MDNS_HOSTNAME, mac[3], mac[4], mac[5])) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return hostname;
|
|
|
|
#endif
|
|
|
|
}
|
2019-06-19 07:20:58 -04:00
|
|
|
|
|
|
|
#if CONFIG_MDNS_RESOLVE_TEST_SERVICES == 1
|
|
|
|
/**
|
|
|
|
* @brief Executes gethostbyname and displays list of resolved addresses.
|
|
|
|
* Note: This function is used only to test advertised mdns hostnames resolution
|
|
|
|
*/
|
|
|
|
static void query_mdns_host_with_gethostbyname(char * host)
|
|
|
|
{
|
|
|
|
struct hostent *res = gethostbyname(host);
|
|
|
|
if (res) {
|
|
|
|
unsigned int i = 0;
|
|
|
|
while (res->h_addr_list[i] != NULL) {
|
|
|
|
ESP_LOGI(TAG, "gethostbyname: %s resolved to: %s", host, inet_ntoa(*(struct in_addr *) (res->h_addr_list[i])));
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Executes getaddrinfo and displays list of resolved addresses.
|
|
|
|
* Note: This function is used only to test advertised mdns hostnames resolution
|
|
|
|
*/
|
|
|
|
static void query_mdns_host_with_getaddrinfo(char * host)
|
|
|
|
{
|
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo * res;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
if (!getaddrinfo(host, NULL, &hints, &res)) {
|
|
|
|
while (res) {
|
|
|
|
ESP_LOGI(TAG, "getaddrinfo: %s resolved to: %s", host,
|
|
|
|
res->ai_family == AF_INET?
|
|
|
|
inet_ntoa(((struct sockaddr_in *) res->ai_addr)->sin_addr):
|
|
|
|
inet_ntoa(((struct sockaddr_in6 *) res->ai_addr)->sin6_addr));
|
|
|
|
res = res->ai_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|