Merge pull request #878 from dnc40085/dev_wifi_sta_Xethostname
Add functions: wifi.sta.sethostname and wifi.sta.gethostname
This commit is contained in:
commit
7e8c54890f
|
@ -76,6 +76,17 @@
|
||||||
|
|
||||||
#define ENDUSER_SETUP_AP_SSID "SetupGadget"
|
#define ENDUSER_SETUP_AP_SSID "SetupGadget"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* A valid hostname only contains alphanumeric and hyphen(-) characters, with no hyphens at first or last char
|
||||||
|
* if WIFI_STA_HOSTNAME not defined: hostname will default to NODE-xxxxxx (xxxxxx being last 3 octets of MAC address)
|
||||||
|
* if WIFI_STA_HOSTNAME defined: hostname must only contain alphanumeric characters
|
||||||
|
* if WIFI_STA_HOSTNAME_APPEND_MAC not defined: Hostname MUST be 32 chars or less
|
||||||
|
* if WIFI_STA_HOSTNAME_APPEND_MAC defined: Hostname MUST be 26 chars or less, since last 3 octets of MAC address will be appended
|
||||||
|
* if defined hostname is invalid: hostname will default to NODE-xxxxxx (xxxxxx being last 3 octets of MAC address)
|
||||||
|
*/
|
||||||
|
//#define WIFI_STA_HOSTNAME "NodeMCU"
|
||||||
|
//#define WIFI_STA_HOSTNAME_APPEND_MAC
|
||||||
|
|
||||||
#define STRBUF_DEFAULT_INCREMENT 32
|
#define STRBUF_DEFAULT_INCREMENT 32
|
||||||
|
|
||||||
#endif /* __USER_CONFIG_H__ */
|
#endif /* __USER_CONFIG_H__ */
|
||||||
|
|
|
@ -919,6 +919,61 @@ static int wifi_station_listap( lua_State* L )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Lua: wifi.sta.gethostname()
|
||||||
|
static int wifi_sta_gethostname( lua_State* L )
|
||||||
|
{
|
||||||
|
char* hostname = wifi_station_get_hostname();
|
||||||
|
lua_pushstring(L, hostname);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8 wifi_sta_sethostname(const char *hostname, size_t len)
|
||||||
|
{
|
||||||
|
uint8 status;
|
||||||
|
if(hostname[0]!='-' && hostname[len-1]!='-' && (len >= 1 && len <= 32))
|
||||||
|
{
|
||||||
|
uint8 i=0;
|
||||||
|
while(hostname[i])
|
||||||
|
{
|
||||||
|
if(!(isalnum(hostname[i])||hostname[i]=='-'))
|
||||||
|
{
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
status=wifi_station_set_hostname((char*)hostname);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lua: wifi.sta.sethostname()
|
||||||
|
static int wifi_sta_sethostname_lua( lua_State* L )
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
const char *hostname = luaL_checklstring(L, 1, &len);
|
||||||
|
uint8 retval = wifi_sta_sethostname(hostname, len);
|
||||||
|
NODE_DBG("\n\tstring is: \"%s\"\n\tlength is: %u\t wifi_sta_sethostname returned: %u\n", hostname, len, retval);
|
||||||
|
if(retval==0)
|
||||||
|
{
|
||||||
|
lua_pushboolean(L, 0);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else if (retval==1)
|
||||||
|
{
|
||||||
|
lua_pushboolean(L, 1);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else if (retval==2)
|
||||||
|
{
|
||||||
|
return luaL_error(L, "Invalid hostname!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Lua: wifi.sta.status()
|
// Lua: wifi.sta.status()
|
||||||
static int wifi_station_status( lua_State* L )
|
static int wifi_station_status( lua_State* L )
|
||||||
{
|
{
|
||||||
|
@ -1348,6 +1403,8 @@ static const LUA_REG_TYPE wifi_station_map[] = {
|
||||||
{ LSTRKEY( "getmac" ), LFUNCVAL( wifi_station_getmac ) },
|
{ LSTRKEY( "getmac" ), LFUNCVAL( wifi_station_getmac ) },
|
||||||
{ LSTRKEY( "setmac" ), LFUNCVAL( wifi_station_setmac ) },
|
{ LSTRKEY( "setmac" ), LFUNCVAL( wifi_station_setmac ) },
|
||||||
{ LSTRKEY( "getap" ), LFUNCVAL( wifi_station_listap ) },
|
{ LSTRKEY( "getap" ), LFUNCVAL( wifi_station_listap ) },
|
||||||
|
{ LSTRKEY( "sethostname" ), LFUNCVAL( wifi_sta_sethostname_lua ) },
|
||||||
|
{ LSTRKEY( "gethostname" ), LFUNCVAL( wifi_sta_gethostname ) },
|
||||||
{ LSTRKEY( "status" ), LFUNCVAL( wifi_station_status ) },
|
{ LSTRKEY( "status" ), LFUNCVAL( wifi_station_status ) },
|
||||||
{ LSTRKEY( "eventMonReg" ), LFUNCVAL( wifi_station_event_mon_reg ) },
|
{ LSTRKEY( "eventMonReg" ), LFUNCVAL( wifi_station_event_mon_reg ) },
|
||||||
{ LSTRKEY( "eventMonStart" ), LFUNCVAL( wifi_station_event_mon_start ) },
|
{ LSTRKEY( "eventMonStart" ), LFUNCVAL( wifi_station_event_mon_start ) },
|
||||||
|
@ -1420,4 +1477,40 @@ static const LUA_REG_TYPE wifi_map[] = {
|
||||||
{ LNILKEY, LNILVAL }
|
{ LNILKEY, LNILVAL }
|
||||||
};
|
};
|
||||||
|
|
||||||
NODEMCU_MODULE(WIFI, "wifi", wifi_map, NULL);
|
int luaopen_wifi( lua_State *L )
|
||||||
|
{
|
||||||
|
#ifndef WIFI_STA_HOSTNAME
|
||||||
|
char temp[32];
|
||||||
|
uint8_t mac[6];
|
||||||
|
wifi_get_macaddr(STATION_IF, mac);
|
||||||
|
c_sprintf(temp, "NODE-%X%X%X", (mac)[3], (mac)[4], (mac)[5]);
|
||||||
|
wifi_sta_sethostname((const char*)temp, strlen(temp));
|
||||||
|
|
||||||
|
#elif defined(WIFI_STA_HOSTNAME) && !defined(WIFI_STA_HOSTNAME_APPEND_MAC)
|
||||||
|
const char* hostname=WIFI_STA_HOSTNAME;
|
||||||
|
uint8 retval=wifi_sta_sethostname(hostname, strlen(hostname));
|
||||||
|
if(retval!=1)
|
||||||
|
{
|
||||||
|
char temp[32];
|
||||||
|
uint8_t mac[6];
|
||||||
|
wifi_get_macaddr(STATION_IF, mac);
|
||||||
|
c_sprintf(temp, "NODE-%X%X%X", (mac)[3], (mac)[4], (mac)[5]);
|
||||||
|
wifi_sta_sethostname((const char*)temp, strlen(temp));
|
||||||
|
}
|
||||||
|
|
||||||
|
#elif defined(WIFI_STA_HOSTNAME) && defined(WIFI_STA_HOSTNAME_APPEND_MAC)
|
||||||
|
char temp[32];
|
||||||
|
uint8_t mac[6];
|
||||||
|
wifi_get_macaddr(STATION_IF, mac);
|
||||||
|
c_sprintf(temp, "%s%X%X%X", WIFI_STA_HOSTNAME, (mac)[3], (mac)[4], (mac)[5]);
|
||||||
|
uint8 retval=wifi_sta_sethostname(temp, strlen(temp));
|
||||||
|
if(retval!=1)
|
||||||
|
{
|
||||||
|
c_sprintf(temp, "NODE-%X%X%X", (mac)[3], (mac)[4], (mac)[5]);
|
||||||
|
wifi_sta_sethostname((const char*)temp, strlen(temp));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
NODEMCU_MODULE(WIFI, "wifi", wifi_map, luaopen_wifi);
|
||||||
|
|
Loading…
Reference in New Issue