diff --git a/app/modules/wifi.c b/app/modules/wifi.c index 3748cd3e..3b34b2e0 100644 --- a/app/modules/wifi.c +++ b/app/modules/wifi.c @@ -159,6 +159,18 @@ static int ICACHE_FLASH_ATTR wifi_getmac( lua_State* L, uint8_t mode ) return 1; } +// Lua: mac = wifi.xx.setmac() +static int ICACHE_FLASH_ATTR wifi_setmac( lua_State* L, uint8_t mode ) +{ + unsigned len = 0; + const char *mac = luaL_checklstring( L, 1, &len ); + if(len!=6) + return luaL_error( L, "wrong arg type" ); + + lua_pushboolean(L,wifi_set_macaddr(mode, (uint8 *)mac)); + return 1; +} + // Lua: ip = wifi.xx.getip() static int ICACHE_FLASH_ATTR wifi_getip( lua_State* L, uint8_t mode ) { @@ -167,10 +179,71 @@ static int ICACHE_FLASH_ATTR wifi_getip( lua_State* L, uint8_t mode ) wifi_get_ip_info(mode, &pTempIp); if(pTempIp.ip.addr==0){ lua_pushnil(L); + return 1; } else { c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&pTempIp.ip) ); lua_pushstring( L, temp ); + // c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&pTempIp.netmask) ); + // lua_pushstring( L, temp ); + // c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&pTempIp.gw) ); + // lua_pushstring( L, temp ); + // return 3; + return 1; } +} + +static uint32_t parse_key(lua_State* L, const char * key){ + lua_getfield(L, 1, key); + if( lua_isstring(L, -1) ) // deal with the ssid string + { + const char *ip = luaL_checkstring( L, -1 ); + return ipaddr_addr(ip); + } + lua_pop(L, 1); + return 0; +} + +// Lua: ip = wifi.xx.setip() +static int ICACHE_FLASH_ATTR wifi_setip( lua_State* L, uint8_t mode ) +{ + struct ip_info pTempIp; + wifi_get_ip_info(mode, &pTempIp); + + if (!lua_istable(L, 1)) + return luaL_error( L, "wrong arg type" ); + uint32_t ip = parse_key(L, "ip"); + if(ip!=0) + pTempIp.ip.addr = ip; + + ip = parse_key(L, "netmask"); + if(ip!=0) + pTempIp.netmask.addr = ip; + + ip = parse_key(L, "gateway"); + if(ip!=0) + pTempIp.gw.addr = ip; + + lua_pushboolean(L,wifi_set_ip_info(mode, &pTempIp)); + return 1; +} + +// Lua: realtype = sleeptype(type) +static int ICACHE_FLASH_ATTR wifi_sleeptype( lua_State* L ) +{ + unsigned type; + + if ( lua_isnumber(L, 1) ){ + type = lua_tointeger(L, 1); + if ( type != NONE_SLEEP_T && type != LIGHT_SLEEP_T && type != MODEM_SLEEP_T ) + return luaL_error( L, "wrong arg type" ); + if(!wifi_set_sleep_type(type)){ + lua_pushnil(L); + return 1; + } + } + + type = wifi_get_sleep_type(); + lua_pushinteger( L, type ); return 1; } @@ -179,11 +252,21 @@ static int ICACHE_FLASH_ATTR wifi_station_getmac( lua_State* L ){ return wifi_getmac(L, STATION_IF); } +// Lua: wifi.sta.setmac() +static int ICACHE_FLASH_ATTR wifi_station_setmac( lua_State* L ){ + return wifi_setmac(L, STATION_IF); +} + // Lua: wifi.sta.getip() static int ICACHE_FLASH_ATTR wifi_station_getip( lua_State* L ){ return wifi_getip(L, STATION_IF); } +// Lua: wifi.sta.setip() +static int ICACHE_FLASH_ATTR wifi_station_setip( lua_State* L ){ + return wifi_setip(L, STATION_IF); +} + // Lua: wifi.sta.config(ssid, password) static int ICACHE_FLASH_ATTR wifi_station_config( lua_State* L ) { @@ -281,11 +364,21 @@ static int ICACHE_FLASH_ATTR wifi_ap_getmac( lua_State* L ){ return wifi_getmac(L, SOFTAP_IF); } +// Lua: wifi.ap.setmac() +static int ICACHE_FLASH_ATTR wifi_ap_setmac( lua_State* L ){ + return wifi_setmac(L, SOFTAP_IF); +} + // Lua: wifi.ap.getip() static int ICACHE_FLASH_ATTR wifi_ap_getip( lua_State* L ){ return wifi_getip(L, SOFTAP_IF); } +// Lua: wifi.ap.setip() +static int ICACHE_FLASH_ATTR wifi_ap_setip( lua_State* L ){ + return wifi_setip(L, SOFTAP_IF); +} + // Lua: wifi.ap.config(table) static int ICACHE_FLASH_ATTR wifi_ap_config( lua_State* L ) { @@ -352,7 +445,9 @@ static const LUA_REG_TYPE wifi_station_map[] = { LSTRKEY( "disconnect" ), LFUNCVAL ( wifi_station_disconnect4lua ) }, { LSTRKEY( "autoconnect" ), LFUNCVAL ( wifi_station_setauto ) }, { LSTRKEY( "getip" ), LFUNCVAL ( wifi_station_getip ) }, + { LSTRKEY( "setip" ), LFUNCVAL ( wifi_station_setip ) }, { LSTRKEY( "getmac" ), LFUNCVAL ( wifi_station_getmac ) }, + { LSTRKEY( "setmac" ), LFUNCVAL ( wifi_station_setmac ) }, { LSTRKEY( "getap" ), LFUNCVAL ( wifi_station_listap ) }, { LSTRKEY( "status" ), LFUNCVAL ( wifi_station_status ) }, { LNILKEY, LNILVAL } @@ -362,7 +457,9 @@ static const LUA_REG_TYPE wifi_ap_map[] = { { LSTRKEY( "config" ), LFUNCVAL( wifi_ap_config ) }, { LSTRKEY( "getip" ), LFUNCVAL ( wifi_ap_getip ) }, + { LSTRKEY( "setip" ), LFUNCVAL ( wifi_ap_setip ) }, { LSTRKEY( "getmac" ), LFUNCVAL ( wifi_ap_getmac ) }, + { LSTRKEY( "setmac" ), LFUNCVAL ( wifi_ap_setmac ) }, { LNILKEY, LNILVAL } }; @@ -372,6 +469,7 @@ const LUA_REG_TYPE wifi_map[] = { LSTRKEY( "getmode" ), LFUNCVAL( wifi_getmode ) }, { LSTRKEY( "startsmart" ), LFUNCVAL( wifi_start_smart ) }, { LSTRKEY( "stopsmart" ), LFUNCVAL( wifi_exit_smart ) }, + { LSTRKEY( "sleeptype" ), LFUNCVAL( wifi_sleeptype ) }, #if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "sta" ), LROVAL( wifi_station_map ) }, { LSTRKEY( "ap" ), LROVAL( wifi_ap_map ) }, @@ -381,6 +479,10 @@ const LUA_REG_TYPE wifi_map[] = { LSTRKEY( "SOFTAP" ), LNUMVAL( SOFTAP_MODE ) }, { LSTRKEY( "STATIONAP" ), LNUMVAL( STATIONAP_MODE ) }, + { LSTRKEY( "NONE_SLEEP" ), LNUMVAL( NONE_SLEEP_T ) }, + { LSTRKEY( "LIGHT_SLEEP" ), LNUMVAL( LIGHT_SLEEP_T ) }, + { LSTRKEY( "MODEM_SLEEP" ), LNUMVAL( MODEM_SLEEP_T ) }, + // { LSTRKEY( "STA_IDLE" ), LNUMVAL( STATION_IDLE ) }, // { LSTRKEY( "STA_CONNECTING" ), LNUMVAL( STATION_CONNECTING ) }, // { LSTRKEY( "STA_WRONGPWD" ), LNUMVAL( STATION_WRONG_PASSWORD ) }, @@ -410,6 +512,10 @@ LUALIB_API int ICACHE_FLASH_ATTR luaopen_wifi( lua_State *L ) MOD_REG_NUMBER( L, "SOFTAP", SOFTAP_MODE ); MOD_REG_NUMBER( L, "STATIONAP", STATIONAP_MODE ); + MOD_REG_NUMBER( L, "NONE_SLEEP", NONE_SLEEP_T ); + MOD_REG_NUMBER( L, "LIGHT_SLEEP", LIGHT_SLEEP_T ); + MOD_REG_NUMBER( L, "MODEM_SLEEP", MODEM_SLEEP_T ); + // MOD_REG_NUMBER( L, "STA_IDLE", STATION_IDLE ); // MOD_REG_NUMBER( L, "STA_CONNECTING", STATION_CONNECTING ); // MOD_REG_NUMBER( L, "STA_WRONGPWD", STATION_WRONG_PASSWORD ); diff --git a/examples/fragment.lua b/examples/fragment.lua index 79256373..4acadc8c 100644 --- a/examples/fragment.lua +++ b/examples/fragment.lua @@ -300,4 +300,6 @@ sk=net.createConnection(net.TCP, 0) sk:on("receive", function(sck, c) print(c) e sk:send("GET / HTTP/1.1\r\nHost: 115.239.210.27\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") sk=net.createConnection(net.TCP, 1) sk:on("receive", function(sck, c) print(c) end ) -sk:on("connection", function(sck) sck:send("GET / HTTPS/1.1\r\nHost: www.google.com.hk\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") end ) sk:connect(443,"173.194.72.199") \ No newline at end of file +sk:on("connection", function(sck) sck:send("GET / HTTPS/1.1\r\nHost: www.google.com.hk\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") end ) sk:connect(443,"173.194.72.199") + +wifi.sta.setip({ip="192.168.18.119",netmask="255.255.255.0",gateway="192.168.18.1"})