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"
|
|
|
|
|
|
|
|
#include "c_string.h"
|
|
|
|
#include "c_stdlib.h"
|
|
|
|
|
|
|
|
#include "c_types.h"
|
|
|
|
#include "mem.h"
|
|
|
|
#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);
|
|
|
|
|
|
|
|
if (c_strcmp(key, "port") == 0) {
|
|
|
|
info.service_port = luaL_checknumber(L, -1);
|
|
|
|
} else if (c_strcmp(key, "service") == 0) {
|
|
|
|
info.service_name = luaL_checkstring(L, -1);
|
|
|
|
} else if (c_strcmp(key, "description") == 0) {
|
|
|
|
info.host_desc = luaL_checkstring(L, -1);
|
2016-01-28 03:57:58 +01:00
|
|
|
} else {
|
2016-04-03 20:10:52 +02:00
|
|
|
int len = c_strlen(key) + 1;
|
2016-01-28 03:57:58 +01:00
|
|
|
const char *value = luaL_checkstring(L, -1);
|
2016-04-03 20:10:52 +02:00
|
|
|
char *p = alloca(len + c_strlen(value) + 1);
|
|
|
|
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
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_BEGIN(mdns)
|
|
|
|
LROT_FUNCENTRY( register, mdns_register )
|
|
|
|
LROT_FUNCENTRY( close, mdns_close )
|
|
|
|
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);
|