feat: mDNS-Responder für _ninlight._tcp
This commit is contained in:
+79
-4
@@ -1,5 +1,80 @@
|
|||||||
#include "net/nl_mdns.h"
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
/* Placeholder until the mDNS responder lands (plan 2, task 7). */
|
#include "wm_include.h"
|
||||||
void nl_mdns_on_ip_up(void) {}
|
#include "lwip/apps/mdns.h"
|
||||||
void nl_mdns_set_name(const char *name) { (void)name; }
|
#include "lwip/tcpip.h"
|
||||||
|
|
||||||
|
#include "config/nl_settings.h"
|
||||||
|
#include "net/nl_http_server.h"
|
||||||
|
#include "net/nl_mdns.h"
|
||||||
|
#include "net/nl_names.h"
|
||||||
|
#include "net/nl_wifi.h"
|
||||||
|
|
||||||
|
#define NL_MDNS_TTL 120
|
||||||
|
|
||||||
|
static bool s_started;
|
||||||
|
static char s_name[NL_NAME_LEN];
|
||||||
|
static char s_mac[18];
|
||||||
|
|
||||||
|
static void add_txt(struct mdns_service *service, const char *key, const char *value)
|
||||||
|
{
|
||||||
|
char item[64];
|
||||||
|
int n = snprintf(item, sizeof item, "%s=%s", key, value);
|
||||||
|
if (n > 0)
|
||||||
|
mdns_resp_add_service_txtitem(service, item, (u8_t)(n < (int)sizeof item ? n : (int)sizeof item - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* tcpip thread */
|
||||||
|
static void txt_cb(struct mdns_service *service, void *data)
|
||||||
|
{
|
||||||
|
char name[NL_NAME_LEN];
|
||||||
|
u32 sr;
|
||||||
|
|
||||||
|
(void)data;
|
||||||
|
sr = tls_os_set_critical();
|
||||||
|
memcpy(name, s_name, sizeof name);
|
||||||
|
tls_os_release_critical(sr);
|
||||||
|
add_txt(service, "type", NINLIGHT_DEVICE_ID);
|
||||||
|
add_txt(service, "fw", NINLIGHT_FW_VERSION);
|
||||||
|
add_txt(service, "mac", s_mac);
|
||||||
|
add_txt(service, "name", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* tcpip thread */
|
||||||
|
static void announce(void *arg)
|
||||||
|
{
|
||||||
|
struct netif *nif = tls_get_netif();
|
||||||
|
|
||||||
|
(void)arg;
|
||||||
|
if (!nif)
|
||||||
|
return;
|
||||||
|
if (!s_started) {
|
||||||
|
mdns_resp_init();
|
||||||
|
if (mdns_resp_add_netif(nif, nl_wifi_hostname(), NL_MDNS_TTL) == ERR_OK &&
|
||||||
|
mdns_resp_add_service(nif, nl_wifi_hostname(), "_ninlight", DNSSD_PROTO_TCP, NL_HTTP_PORT, NL_MDNS_TTL,
|
||||||
|
txt_cb, NULL) == ERR_OK)
|
||||||
|
s_started = true;
|
||||||
|
printf("mdns: %s.local _ninlight._tcp %s\r\n", nl_wifi_hostname(), s_started ? "up" : "failed");
|
||||||
|
} else {
|
||||||
|
mdns_resp_netif_settings_changed(nif); /* re-announce: new IP or TXT */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void nl_mdns_on_ip_up(void)
|
||||||
|
{
|
||||||
|
uint8_t mac[6];
|
||||||
|
nl_wifi_mac(mac);
|
||||||
|
nl_names_mac_str(mac, s_mac);
|
||||||
|
tcpip_callback(announce, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void nl_mdns_set_name(const char *name)
|
||||||
|
{
|
||||||
|
u32 sr = tls_os_set_critical();
|
||||||
|
strncpy(s_name, name, sizeof s_name - 1);
|
||||||
|
s_name[sizeof s_name - 1] = '\0';
|
||||||
|
tls_os_release_critical(sr);
|
||||||
|
if (s_started)
|
||||||
|
tcpip_callback(announce, NULL);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user