Skip to content

Commit e56a295

Browse files
Add MDNS.addServiceTxt() to SimpleMDNS (#2679)
* Add MDNS.addServiceTxt() to SimpleMDNS Fixes #2678 A simple mapping allows for basic service text addition even when using SimpleMDNS and ArduinoOTA. * Protection against duplicate services addition
1 parent 5b4dff9 commit e56a295

File tree

2 files changed

+103
-8
lines changed

2 files changed

+103
-8
lines changed

libraries/SimpleMDNS/src/SimpleMDNS.cpp

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,36 @@ bool SimpleMDNS::begin(const char *hostname, unsigned int ttl) {
4242
}
4343

4444
void SimpleMDNS::enableArduino(unsigned int port, bool passwd) {
45-
if (!_running) {
45+
if (!_running || _arduinoAdded) {
4646
return;
4747
}
4848
struct netif *n = netif_list;
4949
while (n) {
5050
mdns_resp_add_service(n, _hostname, "_arduino", DNSSD_PROTO_TCP, port, _arduinoGetTxt, (void *)passwd);
5151
n = n->next;
5252
}
53+
_arduinoAdded = true;
5354
}
5455

55-
void SimpleMDNS::addService(const char *service, const char *proto, unsigned int port) {
56+
hMDNSService SimpleMDNS::addService(const char *service, const char *proto, unsigned int port) {
5657
if (!_running) {
57-
return;
58+
return nullptr;
59+
}
60+
if (_svcMap.find(service) != _svcMap.end()) {
61+
// Duplicates = error
62+
return nullptr;
5863
}
5964
char s[128];
6065
snprintf(s, sizeof(s), "_%s", service);
6166
s[sizeof(s) - 1] = 0;
67+
SimpleMDNSService *svc = new SimpleMDNSService();
68+
_svcMap.insert({strdup(service), svc});
6269
struct netif *n = netif_list;
6370
while (n) {
64-
mdns_resp_add_service(n, _hostname, s, !strcasecmp("tcp", proto) ? DNSSD_PROTO_TCP : DNSSD_PROTO_UDP, port, _nullGetTxt, nullptr);
71+
mdns_resp_add_service(n, _hostname, s, !strcasecmp("tcp", proto) ? DNSSD_PROTO_TCP : DNSSD_PROTO_UDP, port, SimpleMDNSService::callback, (void *)svc);
6572
n = n->next;
6673
}
74+
return (hMDNSService*) service;
6775
}
6876

6977
void SimpleMDNS::update() {
@@ -89,10 +97,65 @@ void SimpleMDNS::_arduinoGetTxt(struct mdns_service *service, void *txt_userdata
8997
_addServiceTxt(service, (bool)txt_userdata ? "auth_upload=yes" : "auth_upload=no");
9098
}
9199

92-
void SimpleMDNS::_nullGetTxt(struct mdns_service *service, void *txt_userdata) {
93-
/* nop */
100+
101+
SimpleMDNSService::SimpleMDNSService() {
102+
}
103+
104+
void SimpleMDNSService::callback(struct mdns_service *service, void *txt_userdata) {
105+
SimpleMDNSService *obj = (SimpleMDNSService *)txt_userdata;
106+
for (auto s : obj->txt) {
107+
mdns_resp_add_service_txtitem(service, s, strlen(s));
108+
}
94109
}
95110

111+
hMDNSTxt SimpleMDNSService::add(const char *key, const char *value) {
112+
char s[128];
113+
snprintf(s, sizeof(s), "%s=%s", key, value);
114+
s[sizeof(s) - 1] = 0;
115+
char *copy = strdup(s);
116+
txt.push_back(copy);
117+
return (void *)copy; // Do not use...
118+
};
119+
120+
// Add a (static) MDNS TXT item ('key' = 'value') to the service
121+
hMDNSTxt SimpleMDNS::addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, const char* p_pcValue) {
122+
const char *s = (const char *)p_hService;
123+
auto o = _svcMap.find(s);
124+
if (o != _svcMap.end()) {
125+
return o->second->add(p_pcKey, p_pcValue);
126+
}
127+
return nullptr;
128+
}
129+
130+
hMDNSTxt SimpleMDNS::addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, uint32_t p_u32Value) {
131+
char s[16];
132+
sprintf(s, "%lu", p_u32Value);
133+
return addServiceTxt(p_hService, p_pcKey, s);
134+
}
135+
136+
hMDNSTxt SimpleMDNS::addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, uint16_t p_u16Value) {
137+
return addServiceTxt(p_hService, p_pcKey, (uint32_t)p_u16Value);
138+
}
139+
140+
hMDNSTxt SimpleMDNS::addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, uint8_t p_u8Value) {
141+
return addServiceTxt(p_hService, p_pcKey, (uint32_t)p_u8Value);
142+
}
143+
144+
hMDNSTxt SimpleMDNS::addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, int32_t p_i32Value) {
145+
char s[16];
146+
sprintf(s, "%ld", p_i32Value);
147+
return addServiceTxt(p_hService, p_pcKey, s);
148+
}
149+
150+
hMDNSTxt SimpleMDNS::addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, int16_t p_i16Value) {
151+
return addServiceTxt(p_hService, p_pcKey, (int32_t)p_i16Value);
152+
}
153+
154+
hMDNSTxt SimpleMDNS::addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, int8_t p_i8Value) {
155+
return addServiceTxt(p_hService, p_pcKey, (int32_t)p_i8Value);
156+
}
157+
158+
96159
const char *SimpleMDNS::_hostname = nullptr;
97160

98161
SimpleMDNS MDNS;

libraries/SimpleMDNS/src/SimpleMDNS.h

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,44 @@
2121

2222
#pragma once
2323
#include <Arduino.h>
24+
#include <string>
25+
#include <map>
26+
#include <vector>
27+
28+
typedef void* hMDNSTxt; // Unusable in SimpleMDNS, for signature compatibility only
29+
30+
class SimpleMDNSService {
31+
public:
32+
SimpleMDNSService();
33+
static void callback(struct mdns_service *service, void *txt_userdata);
34+
hMDNSTxt add(const char *key, const char *val);
35+
private:
36+
std::vector<const char *> txt;
37+
};
38+
39+
// hMDNSService (opaque handle to access the service)
40+
typedef const void* hMDNSService;
2441

2542
class SimpleMDNS {
2643

2744
public:
2845
bool begin(const char *hostname, unsigned int ttl = 60);
2946
void enableArduino(unsigned int port, bool passwd = false);
30-
void addService(const char *service, const char *proto, unsigned int port);
47+
48+
hMDNSService addService(const char *service, const char *proto, unsigned int port);
49+
hMDNSService addService(const char *name, const char *service, const char *proto, unsigned int port) {
50+
(void) name; // Ignored
51+
return addService(service, proto, port);
52+
}
53+
54+
// Add a (static) MDNS TXT item ('key' = 'value') to the service
55+
hMDNSTxt addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, const char* p_pcValue);
56+
hMDNSTxt addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, uint32_t p_u32Value);
57+
hMDNSTxt addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, uint16_t p_u16Value);
58+
hMDNSTxt addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, uint8_t p_u8Value);
59+
hMDNSTxt addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, int32_t p_i32Value);
60+
hMDNSTxt addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, int16_t p_i16Value);
61+
hMDNSTxt addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, int8_t p_i8Value);
3162

3263
// No-ops here
3364
void end();
@@ -37,10 +68,11 @@ class SimpleMDNS {
3768
static void _statusCB(struct netif *netif);
3869
static void _addServiceTxt(struct mdns_service *service, const char *str);
3970
static void _arduinoGetTxt(struct mdns_service *service, void *txt_userdata);
40-
static void _nullGetTxt(struct mdns_service *service, void *txt_userdata);
4171

4272
bool _running = false;
4373
static const char *_hostname;
74+
std::map<std::string, SimpleMDNSService*> _svcMap;
75+
bool _arduinoAdded = false;
4476
};
4577

4678
extern SimpleMDNS MDNS;

0 commit comments

Comments
 (0)