MDNSService.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "MDNSService.h"
  2. #include <arpa/inet.h>
  3. #include <vector>
  4. #include "mdns.h"
  5. using namespace bell;
  6. /**
  7. * ESP32 implementation of MDNSService
  8. * @see https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/protocols/mdns.html
  9. **/
  10. void* MDNSService::registerService(
  11. const std::string& serviceName,
  12. const std::string& serviceType,
  13. const std::string& serviceProto,
  14. const std::string& serviceHost,
  15. int servicePort,
  16. const std::map<std::string, std::string> txtData
  17. ) {
  18. std::vector<mdns_txt_item_t> txtItems;
  19. txtItems.reserve(txtData.size());
  20. for (auto& data : txtData) {
  21. mdns_txt_item_t item;
  22. item.key = data.first.c_str();
  23. item.value = data.second.c_str();
  24. txtItems.push_back(item);
  25. }
  26. mdns_service_add(
  27. serviceName.c_str(), /* instance_name */
  28. serviceType.c_str(), /* service_type */
  29. serviceProto.c_str(), /* proto */
  30. servicePort, /* port */
  31. txtItems.data(), /* txt */
  32. txtItems.size() /* num_items */
  33. );
  34. return NULL;
  35. }