2016-04-03 20:10:52 +02:00
|
|
|
// Module for access to the nodemcu_mdns functions
|
2016-01-28 03:57:58 +01:00
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "lauxlib.h"
|
|
|
|
|
2019-07-21 23:58:21 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <alloca.h>
|
2016-01-28 03:57:58 +01:00
|
|
|
|
2019-07-23 06:22:38 +02:00
|
|
|
#include <stdint.h>
|
2016-01-28 03:57:58 +01:00
|
|
|
#include "lwip/ip_addr.h"
|
2016-04-03 20:10:52 +02:00
|
|
|
#include "nodemcu_mdns.h"
|
2016-01-28 03:57:58 +01:00
|
|
|
#include "user_interface.h"
|
|
|
|
|
|
|
|
//
|
|
|
|
// mdns.close()
|
2019-02-17 19:26:29 +01:00
|
|
|
//
|
2016-01-28 03:57:58 +01:00
|
|
|
static int mdns_close(lua_State *L)
|
|
|
|
{
|
2016-04-03 20:10:52 +02:00
|
|
|
nodemcu_mdns_close();
|
2016-01-28 03:57:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2016-04-03 20:10:52 +02:00
|
|
|
// mdns.register(hostname [, { attributes} ])
|
|
|
|
//
|
|
|
|
static int mdns_register(lua_State *L)
|
2016-01-28 03:57:58 +01:00
|
|
|
{
|
2016-04-03 20:10:52 +02:00
|
|
|
struct nodemcu_mdns_info info;
|
2016-01-28 03:57:58 +01:00
|
|
|
|
2016-04-03 20:10:52 +02:00
|
|
|
memset(&info, 0, sizeof(info));
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2016-04-03 20:10:52 +02:00
|
|
|
info.host_name = luaL_checkstring(L, 1);
|
|
|
|
info.service_name = "http";
|
|
|
|
info.service_port = 80;
|
|
|
|
info.host_desc = info.host_name;
|
2016-01-28 03:57:58 +01:00
|
|
|
|
2016-04-03 20:10:52 +02:00
|
|
|
if (lua_gettop(L) >= 2) {
|
|
|
|
luaL_checktype(L, 2, LUA_TTABLE);
|
2016-01-28 03:57:58 +01:00
|
|
|
lua_pushnil(L); // first key
|
|
|
|
int slot = 0;
|
2016-04-03 20:10:52 +02:00
|
|
|
while (lua_next(L, 2) != 0 && slot < sizeof(info.txt_data) / sizeof(info.txt_data[0])) {
|
|
|
|
luaL_checktype(L, -2, LUA_TSTRING);
|
|
|
|
const char *key = luaL_checkstring(L, -2);
|
|
|
|
|
2019-07-21 23:58:21 +02:00
|
|
|
if (strcmp(key, "port") == 0) {
|
2020-08-29 18:48:24 +02:00
|
|
|
info.service_port = luaL_checkinteger(L, -1);
|
2019-07-21 23:58:21 +02:00
|
|
|
} else if (strcmp(key, "service") == 0) {
|
2016-04-03 20:10:52 +02:00
|
|
|
info.service_name = luaL_checkstring(L, -1);
|
2019-07-21 23:58:21 +02:00
|
|
|
} else if (strcmp(key, "description") == 0) {
|
2016-04-03 20:10:52 +02:00
|
|
|
info.host_desc = luaL_checkstring(L, -1);
|
2016-01-28 03:57:58 +01:00
|
|
|
} else {
|
2019-07-21 23:58:21 +02:00
|
|
|
int len = strlen(key) + 1;
|
2016-01-28 03:57:58 +01:00
|
|
|
const char *value = luaL_checkstring(L, -1);
|
2019-07-21 23:58:21 +02:00
|
|
|
char *p = alloca(len + strlen(value) + 1);
|
2016-04-03 20:10:52 +02:00
|
|
|
strcpy(p, key);
|
|
|
|
strcat(p, "=");
|
|
|
|
strcat(p, value);
|
|
|
|
info.txt_data[slot++] = p;
|
2016-01-28 03:57:58 +01:00
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct ip_info ipconfig;
|
|
|
|
|
|
|
|
uint8_t mode = wifi_get_opmode();
|
|
|
|
|
|
|
|
if (!wifi_get_ip_info((mode == 2) ? SOFTAP_IF : STATION_IF, &ipconfig) || !ipconfig.ip.addr) {
|
|
|
|
return luaL_error(L, "No network connection");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close up the old session (if any). This cannot fail
|
|
|
|
// so no chance of losing the memory in 'result'
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2016-01-28 03:57:58 +01:00
|
|
|
mdns_close(L);
|
|
|
|
|
2016-04-03 20:10:52 +02:00
|
|
|
// Save the result as it appears that nodemcu_mdns_init needs
|
2016-01-28 03:57:58 +01:00
|
|
|
// to have the data valid while it is running.
|
|
|
|
|
2016-04-03 20:10:52 +02:00
|
|
|
if (!nodemcu_mdns_init(&info)) {
|
|
|
|
mdns_close(L);
|
|
|
|
return luaL_error(L, "Unable to start mDns daemon");
|
|
|
|
}
|
2016-01-28 03:57:58 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Module function map
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_BEGIN(mdns, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_FUNCENTRY( register, mdns_register )
|
|
|
|
LROT_FUNCENTRY( close, mdns_close )
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_END(mdns, NULL, 0)
|
2016-01-28 03:57:58 +01:00
|
|
|
|
2019-05-08 13:08:20 +02:00
|
|
|
|
|
|
|
NODEMCU_MODULE(MDNS, "mdns", mdns, NULL);
|