mdns_example_main.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* MDNS-SD Query and advertise Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <string.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/event_groups.h"
  11. #include "esp_system.h"
  12. #include "esp_wifi.h"
  13. #include "esp_event.h"
  14. #include "esp_log.h"
  15. #include "nvs_flash.h"
  16. #include "tcpip_adapter.h"
  17. #include "protocol_examples_common.h"
  18. #include "mdns.h"
  19. #include "driver/gpio.h"
  20. #include <sys/socket.h>
  21. #include <netdb.h>
  22. #define EXAMPLE_MDNS_INSTANCE CONFIG_MDNS_INSTANCE
  23. #define EXAMPLE_BUTTON_GPIO 0
  24. static const char *TAG = "mdns-test";
  25. static char* generate_hostname(void);
  26. static void initialise_mdns(void)
  27. {
  28. char* hostname = generate_hostname();
  29. //initialize mDNS
  30. ESP_ERROR_CHECK( mdns_init() );
  31. //set mDNS hostname (required if you want to advertise services)
  32. ESP_ERROR_CHECK( mdns_hostname_set(hostname) );
  33. ESP_LOGI(TAG, "mdns hostname set to: [%s]", hostname);
  34. //set default mDNS instance name
  35. ESP_ERROR_CHECK( mdns_instance_name_set(EXAMPLE_MDNS_INSTANCE) );
  36. //structure with TXT records
  37. mdns_txt_item_t serviceTxtData[3] = {
  38. {"board","esp32"},
  39. {"u","user"},
  40. {"p","password"}
  41. };
  42. //initialize service
  43. ESP_ERROR_CHECK( mdns_service_add("ESP32-WebServer", "_http", "_tcp", 80, serviceTxtData, 3) );
  44. //add another TXT item
  45. ESP_ERROR_CHECK( mdns_service_txt_item_set("_http", "_tcp", "path", "/foobar") );
  46. //change TXT item value
  47. ESP_ERROR_CHECK( mdns_service_txt_item_set("_http", "_tcp", "u", "admin") );
  48. free(hostname);
  49. }
  50. /* these strings match tcpip_adapter_if_t enumeration */
  51. static const char * if_str[] = {"STA", "AP", "ETH", "MAX"};
  52. /* these strings match mdns_ip_protocol_t enumeration */
  53. static const char * ip_protocol_str[] = {"V4", "V6", "MAX"};
  54. static void mdns_print_results(mdns_result_t * results){
  55. mdns_result_t * r = results;
  56. mdns_ip_addr_t * a = NULL;
  57. int i = 1, t;
  58. while(r){
  59. printf("%d: Interface: %s, Type: %s\n", i++, if_str[r->tcpip_if], ip_protocol_str[r->ip_protocol]);
  60. if(r->instance_name){
  61. printf(" PTR : %s\n", r->instance_name);
  62. }
  63. if(r->hostname){
  64. printf(" SRV : %s.local:%u\n", r->hostname, r->port);
  65. }
  66. if(r->txt_count){
  67. printf(" TXT : [%u] ", r->txt_count);
  68. for(t=0; t<r->txt_count; t++){
  69. printf("%s=%s; ", r->txt[t].key, r->txt[t].value?r->txt[t].value:"NULL");
  70. }
  71. printf("\n");
  72. }
  73. a = r->addr;
  74. while(a){
  75. if(a->addr.type == IPADDR_TYPE_V6){
  76. printf(" AAAA: " IPV6STR "\n", IPV62STR(a->addr.u_addr.ip6));
  77. } else {
  78. printf(" A : " IPSTR "\n", IP2STR(&(a->addr.u_addr.ip4)));
  79. }
  80. a = a->next;
  81. }
  82. r = r->next;
  83. }
  84. }
  85. static void query_mdns_service(const char * service_name, const char * proto)
  86. {
  87. ESP_LOGI(TAG, "Query PTR: %s.%s.local", service_name, proto);
  88. mdns_result_t * results = NULL;
  89. esp_err_t err = mdns_query_ptr(service_name, proto, 3000, 20, &results);
  90. if(err){
  91. ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err));
  92. return;
  93. }
  94. if(!results){
  95. ESP_LOGW(TAG, "No results found!");
  96. return;
  97. }
  98. mdns_print_results(results);
  99. mdns_query_results_free(results);
  100. }
  101. static void query_mdns_host(const char * host_name)
  102. {
  103. ESP_LOGI(TAG, "Query A: %s.local", host_name);
  104. struct ip4_addr addr;
  105. addr.addr = 0;
  106. esp_err_t err = mdns_query_a(host_name, 2000, &addr);
  107. if(err){
  108. if(err == ESP_ERR_NOT_FOUND){
  109. ESP_LOGW(TAG, "%s: Host was not found!", esp_err_to_name(err));
  110. return;
  111. }
  112. ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err));
  113. return;
  114. }
  115. ESP_LOGI(TAG, "Query A: %s.local resolved to: " IPSTR, host_name, IP2STR(&addr));
  116. }
  117. static void initialise_button(void)
  118. {
  119. gpio_config_t io_conf = {0};
  120. io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
  121. io_conf.pin_bit_mask = BIT64(EXAMPLE_BUTTON_GPIO);
  122. io_conf.mode = GPIO_MODE_INPUT;
  123. io_conf.pull_up_en = 1;
  124. io_conf.pull_down_en = 0;
  125. gpio_config(&io_conf);
  126. }
  127. static void check_button(void)
  128. {
  129. static bool old_level = true;
  130. bool new_level = gpio_get_level(EXAMPLE_BUTTON_GPIO);
  131. if (!new_level && old_level) {
  132. query_mdns_host("esp32");
  133. query_mdns_service("_arduino", "_tcp");
  134. query_mdns_service("_http", "_tcp");
  135. query_mdns_service("_printer", "_tcp");
  136. query_mdns_service("_ipp", "_tcp");
  137. query_mdns_service("_afpovertcp", "_tcp");
  138. query_mdns_service("_smb", "_tcp");
  139. query_mdns_service("_ftp", "_tcp");
  140. query_mdns_service("_nfs", "_tcp");
  141. }
  142. old_level = new_level;
  143. }
  144. static void mdns_example_task(void *pvParameters)
  145. {
  146. #if CONFIG_MDNS_RESOLVE_TEST_SERVICES == 1
  147. /* Send initial queries that are started by CI tester */
  148. query_mdns_host("tinytester");
  149. #endif
  150. while(1) {
  151. check_button();
  152. vTaskDelay(50 / portTICK_PERIOD_MS);
  153. }
  154. }
  155. void app_main(void)
  156. {
  157. ESP_ERROR_CHECK(nvs_flash_init());
  158. tcpip_adapter_init();
  159. ESP_ERROR_CHECK(esp_event_loop_create_default());
  160. initialise_mdns();
  161. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  162. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  163. * examples/protocols/README.md for more information about this function.
  164. */
  165. ESP_ERROR_CHECK(example_connect());
  166. initialise_button();
  167. xTaskCreate(&mdns_example_task, "mdns_example_task", 2048, NULL, 5, NULL);
  168. }
  169. /** Generate host name based on sdkconfig, optionally adding a portion of MAC address to it.
  170. * @return host name string allocated from the heap
  171. */
  172. static char* generate_hostname(void)
  173. {
  174. #ifndef CONFIG_MDNS_ADD_MAC_TO_HOSTNAME
  175. return strdup(CONFIG_MDNS_HOSTNAME);
  176. #else
  177. uint8_t mac[6];
  178. char *hostname;
  179. esp_read_mac(mac, ESP_MAC_WIFI_STA);
  180. if (-1 == asprintf(&hostname, "%s-%02X%02X%02X", CONFIG_MDNS_HOSTNAME, mac[3], mac[4], mac[5])) {
  181. abort();
  182. }
  183. return hostname;
  184. #endif
  185. }