main.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "esp_mac.h"
  9. #include "nvs_flash.h"
  10. #include "esp_event.h"
  11. #include "esp_netif.h"
  12. #include "esp_log.h"
  13. #include "protocol_examples_common.h"
  14. #include "mdns.h"
  15. static const char *TAG = "mdns_test";
  16. void mdns_test(char *line);
  17. static void get_string(char *line, size_t size)
  18. {
  19. int count = 0;
  20. while (count < size) {
  21. int c = fgetc(stdin);
  22. if (c == '\n') {
  23. line[count] = '\0';
  24. break;
  25. } else if (c > 0 && c < 127) {
  26. line[count] = c;
  27. ++count;
  28. }
  29. vTaskDelay(20 / portTICK_PERIOD_MS);
  30. }
  31. }
  32. /** Generate host name based on sdkconfig, optionally adding a portion of MAC address to it.
  33. * @return host name string allocated from the heap
  34. */
  35. static char *generate_hostname(void)
  36. {
  37. #ifndef CONFIG_TEST_MDNS_ADD_MAC_TO_HOSTNAME
  38. return strdup(CONFIG_TEST_MDNS_HOSTNAME);
  39. #else
  40. uint8_t mac[6];
  41. char *hostname;
  42. esp_read_mac(mac, ESP_MAC_WIFI_STA);
  43. if (-1 == asprintf(&hostname, "%s-%02X%02X%02X", CONFIG_TEST_MDNS_HOSTNAME, mac[3], mac[4], mac[5])) {
  44. abort();
  45. }
  46. return hostname;
  47. #endif
  48. }
  49. static void initialise_mdns(void)
  50. {
  51. char *hostname = generate_hostname();
  52. //initialize mDNS
  53. ESP_ERROR_CHECK( mdns_init() );
  54. //set mDNS hostname (required if you want to advertise services)
  55. ESP_ERROR_CHECK( mdns_hostname_set(hostname) );
  56. ESP_LOGI(TAG, "mdns hostname set to: [%s]", hostname);
  57. //set default mDNS instance name
  58. ESP_ERROR_CHECK( mdns_instance_name_set(CONFIG_TEST_MDNS_INSTANCE) );
  59. //initialize service
  60. ESP_ERROR_CHECK( mdns_service_add("ESP32-WebServer", "_http", "_tcp", 80, NULL, 0) );
  61. #if CONFIG_TEST_MDNS_PUBLISH_DELEGATE_HOST
  62. char *delegated_hostname;
  63. if (-1 == asprintf(&delegated_hostname, "%s-delegated", hostname)) {
  64. abort();
  65. }
  66. mdns_ip_addr_t addr4, addr6;
  67. esp_netif_str_to_ip4("10.0.0.1", &addr4.addr.u_addr.ip4);
  68. addr4.addr.type = ESP_IPADDR_TYPE_V4;
  69. esp_netif_str_to_ip6("fd11:22::1", &addr6.addr.u_addr.ip6);
  70. addr6.addr.type = ESP_IPADDR_TYPE_V6;
  71. addr4.next = &addr6;
  72. addr6.next = NULL;
  73. ESP_ERROR_CHECK( mdns_delegate_hostname_add(delegated_hostname, &addr4) );
  74. ESP_ERROR_CHECK( mdns_service_add_for_host("test0", "_http", "_tcp", delegated_hostname, 1234, NULL, 0) );
  75. free(delegated_hostname);
  76. #endif // CONFIG_TEST_MDNS_PUBLISH_DELEGATE_HOST
  77. ESP_ERROR_CHECK( mdns_service_subtype_add_for_host("ESP32-WebServer", "_http", "_tcp", NULL, "_server") );
  78. free(hostname);
  79. }
  80. void app_main(void)
  81. {
  82. ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size());
  83. ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
  84. ESP_ERROR_CHECK(nvs_flash_init());
  85. ESP_ERROR_CHECK(esp_netif_init());
  86. ESP_ERROR_CHECK(esp_event_loop_create_default());
  87. initialise_mdns();
  88. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  89. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  90. * examples/protocols/README.md for more information about this function.
  91. */
  92. ESP_ERROR_CHECK(example_connect());
  93. while (1) {
  94. char line[256];
  95. get_string(line, sizeof(line));
  96. mdns_test(line);
  97. continue;
  98. }
  99. }