Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MDNS.addServiceTxt() to SimpleMDNS #2679

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 69 additions & 6 deletions libraries/SimpleMDNS/src/SimpleMDNS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,36 @@ bool SimpleMDNS::begin(const char *hostname, unsigned int ttl) {
}

void SimpleMDNS::enableArduino(unsigned int port, bool passwd) {
if (!_running) {
if (!_running || _arduinoAdded) {
return;
}
struct netif *n = netif_list;
while (n) {
mdns_resp_add_service(n, _hostname, "_arduino", DNSSD_PROTO_TCP, port, _arduinoGetTxt, (void *)passwd);
n = n->next;
}
_arduinoAdded = true;
}

void SimpleMDNS::addService(const char *service, const char *proto, unsigned int port) {
hMDNSService SimpleMDNS::addService(const char *service, const char *proto, unsigned int port) {
if (!_running) {
return;
return nullptr;
}
if (_svcMap.find(service) != _svcMap.end()) {
// Duplicates = error
return nullptr;
}
char s[128];
snprintf(s, sizeof(s), "_%s", service);
s[sizeof(s) - 1] = 0;
SimpleMDNSService *svc = new SimpleMDNSService();
_svcMap.insert({strdup(service), svc});
struct netif *n = netif_list;
while (n) {
mdns_resp_add_service(n, _hostname, s, !strcasecmp("tcp", proto) ? DNSSD_PROTO_TCP : DNSSD_PROTO_UDP, port, _nullGetTxt, nullptr);
mdns_resp_add_service(n, _hostname, s, !strcasecmp("tcp", proto) ? DNSSD_PROTO_TCP : DNSSD_PROTO_UDP, port, SimpleMDNSService::callback, (void *)svc);
n = n->next;
}
return (hMDNSService*) service;
}

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

void SimpleMDNS::_nullGetTxt(struct mdns_service *service, void *txt_userdata) {
/* nop */

SimpleMDNSService::SimpleMDNSService() {
}

void SimpleMDNSService::callback(struct mdns_service *service, void *txt_userdata) {
SimpleMDNSService *obj = (SimpleMDNSService *)txt_userdata;
for (auto s : obj->txt) {
mdns_resp_add_service_txtitem(service, s, strlen(s));
}
}

hMDNSTxt SimpleMDNSService::add(const char *key, const char *value) {
char s[128];
snprintf(s, sizeof(s), "%s=%s", key, value);
s[sizeof(s) - 1] = 0;
char *copy = strdup(s);
txt.push_back(copy);
return (void *)copy; // Do not use...
};

// Add a (static) MDNS TXT item ('key' = 'value') to the service
hMDNSTxt SimpleMDNS::addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, const char* p_pcValue) {
const char *s = (const char *)p_hService;
auto o = _svcMap.find(s);
if (o != _svcMap.end()) {
return o->second->add(p_pcKey, p_pcValue);
}
return nullptr;
}

hMDNSTxt SimpleMDNS::addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, uint32_t p_u32Value) {
char s[16];
sprintf(s, "%lu", p_u32Value);
return addServiceTxt(p_hService, p_pcKey, s);
}

hMDNSTxt SimpleMDNS::addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, uint16_t p_u16Value) {
return addServiceTxt(p_hService, p_pcKey, (uint32_t)p_u16Value);
}

hMDNSTxt SimpleMDNS::addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, uint8_t p_u8Value) {
return addServiceTxt(p_hService, p_pcKey, (uint32_t)p_u8Value);
}

hMDNSTxt SimpleMDNS::addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, int32_t p_i32Value) {
char s[16];
sprintf(s, "%ld", p_i32Value);
return addServiceTxt(p_hService, p_pcKey, s);
}

hMDNSTxt SimpleMDNS::addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, int16_t p_i16Value) {
return addServiceTxt(p_hService, p_pcKey, (int32_t)p_i16Value);
}

hMDNSTxt SimpleMDNS::addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, int8_t p_i8Value) {
return addServiceTxt(p_hService, p_pcKey, (int32_t)p_i8Value);
}


const char *SimpleMDNS::_hostname = nullptr;

SimpleMDNS MDNS;
36 changes: 34 additions & 2 deletions libraries/SimpleMDNS/src/SimpleMDNS.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,44 @@

#pragma once
#include <Arduino.h>
#include <string>
#include <map>
#include <vector>

typedef void* hMDNSTxt; // Unusable in SimpleMDNS, for signature compatibility only

class SimpleMDNSService {
public:
SimpleMDNSService();
static void callback(struct mdns_service *service, void *txt_userdata);
hMDNSTxt add(const char *key, const char *val);
private:
std::vector<const char *> txt;
};

// hMDNSService (opaque handle to access the service)
typedef const void* hMDNSService;

class SimpleMDNS {

public:
bool begin(const char *hostname, unsigned int ttl = 60);
void enableArduino(unsigned int port, bool passwd = false);
void addService(const char *service, const char *proto, unsigned int port);

hMDNSService addService(const char *service, const char *proto, unsigned int port);
hMDNSService addService(const char *name, const char *service, const char *proto, unsigned int port) {
(void) name; // Ignored
return addService(service, proto, port);
}

// Add a (static) MDNS TXT item ('key' = 'value') to the service
hMDNSTxt addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, const char* p_pcValue);
hMDNSTxt addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, uint32_t p_u32Value);
hMDNSTxt addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, uint16_t p_u16Value);
hMDNSTxt addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, uint8_t p_u8Value);
hMDNSTxt addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, int32_t p_i32Value);
hMDNSTxt addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, int16_t p_i16Value);
hMDNSTxt addServiceTxt(const hMDNSService p_hService, const char* p_pcKey, int8_t p_i8Value);

// No-ops here
void end();
Expand All @@ -37,10 +68,11 @@ class SimpleMDNS {
static void _statusCB(struct netif *netif);
static void _addServiceTxt(struct mdns_service *service, const char *str);
static void _arduinoGetTxt(struct mdns_service *service, void *txt_userdata);
static void _nullGetTxt(struct mdns_service *service, void *txt_userdata);

bool _running = false;
static const char *_hostname;
std::map<std::string, SimpleMDNSService*> _svcMap;
bool _arduinoAdded = false;
};

extern SimpleMDNS MDNS;
Expand Down
Loading