@@ -42,28 +42,36 @@ bool SimpleMDNS::begin(const char *hostname, unsigned int ttl) {
42
42
}
43
43
44
44
void SimpleMDNS::enableArduino (unsigned int port, bool passwd) {
45
- if (!_running) {
45
+ if (!_running || _arduinoAdded ) {
46
46
return ;
47
47
}
48
48
struct netif *n = netif_list;
49
49
while (n) {
50
50
mdns_resp_add_service (n, _hostname, " _arduino" , DNSSD_PROTO_TCP, port, _arduinoGetTxt, (void *)passwd);
51
51
n = n->next ;
52
52
}
53
+ _arduinoAdded = true ;
53
54
}
54
55
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) {
56
57
if (!_running) {
57
- return ;
58
+ return nullptr ;
59
+ }
60
+ if (_svcMap.find (service) != _svcMap.end ()) {
61
+ // Duplicates = error
62
+ return nullptr ;
58
63
}
59
64
char s[128 ];
60
65
snprintf (s, sizeof (s), " _%s" , service);
61
66
s[sizeof (s) - 1 ] = 0 ;
67
+ SimpleMDNSService *svc = new SimpleMDNSService ();
68
+ _svcMap.insert ({strdup (service), svc});
62
69
struct netif *n = netif_list;
63
70
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 );
65
72
n = n->next ;
66
73
}
74
+ return (hMDNSService*) service;
67
75
}
68
76
69
77
void SimpleMDNS::update () {
@@ -89,10 +97,65 @@ void SimpleMDNS::_arduinoGetTxt(struct mdns_service *service, void *txt_userdata
89
97
_addServiceTxt (service, (bool )txt_userdata ? " auth_upload=yes" : " auth_upload=no" );
90
98
}
91
99
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
+ }
94
109
}
95
110
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
+
96
159
const char *SimpleMDNS::_hostname = nullptr ;
97
160
98
161
SimpleMDNS MDNS;
0 commit comments