ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
mdns_service.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file mdns_service.cpp
6 * @brief mDNS / DNS-SD advertisement implementation (PC_ENABLE_MDNS).
7 *
8 * Uses the ESP-IDF `mdns` component directly (not the Arduino ESPmDNS wrapper)
9 * so the only external dependency stays the base SDK + mbedTLS.
10 */
11
12#include "mdns_service.h"
13
14#if PC_ENABLE_MDNS && defined(ARDUINO)
15
16#include "mdns.h"
17
18bool pc_mdns_begin(const char *hostname, uint16_t http_port)
19{
20 if (!hostname || hostname[0] == '\0')
21 {
22 return false;
23 }
24 if (mdns_init() != ESP_OK)
25 {
26 return false;
27 }
28 if (mdns_hostname_set(hostname) != ESP_OK)
29 {
30 return false;
31 }
32 // Advertise an HTTP service so browsers / DNS-SD tools discover the device.
33 mdns_service_add(nullptr, "_http", "_tcp", http_port, nullptr, 0);
34 return true;
35}
36
37bool pc_mdns_txt(const char *key, const char *value)
38{
39 if (!key || !value)
40 {
41 return false;
42 }
43 // Attach a TXT key/value to the _http._tcp service (Bonjour browsers show it).
44 return mdns_service_txt_item_set("_http", "_tcp", key, value) == ESP_OK;
45}
46
47bool pc_mdns_add_service(const char *service_type, const char *proto, uint16_t port)
48{
49 if (!service_type || !proto)
50 {
51 return false;
52 }
53 // Advertise an additional service, e.g. ("_https", "_tcp", 443).
54 return mdns_service_add(nullptr, service_type, proto, port, nullptr, 0) == ESP_OK;
55}
56
57#else
58
59bool pc_mdns_begin(const char *hostname, uint16_t http_port)
60{
61 (void)hostname;
62 (void)http_port;
63 return false; // mDNS disabled at compile time (or non-Arduino build)
64}
65
66#endif // PC_ENABLE_MDNS && ARDUINO
bool pc_mdns_begin(const char *hostname, uint16_t http_port)
Start mDNS responder and advertise an HTTP service.
Optional mDNS / DNS-SD advertisement (PC_ENABLE_MDNS).
bool pc_mdns_add_service(const char *service_type, const char *proto, uint16_t port)
Advertise an additional service, e.g. ("_https", "_tcp", 443).
bool pc_mdns_txt(const char *key, const char *value)
Add a TXT key/value record to the advertised _http._tcp service.