Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
a331438c
Commit
a331438c
authored
Dec 29, 2014
by
funshine
Browse files
add setip setmac api
parent
ee8daad3
Changes
2
Hide whitespace changes
Inline
Side-by-side
app/modules/wifi.c
View file @
a331438c
...
@@ -159,6 +159,18 @@ static int ICACHE_FLASH_ATTR wifi_getmac( lua_State* L, uint8_t mode )
...
@@ -159,6 +159,18 @@ static int ICACHE_FLASH_ATTR wifi_getmac( lua_State* L, uint8_t mode )
return
1
;
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()
// Lua: ip = wifi.xx.getip()
static
int
ICACHE_FLASH_ATTR
wifi_getip
(
lua_State
*
L
,
uint8_t
mode
)
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 )
...
@@ -167,10 +179,71 @@ static int ICACHE_FLASH_ATTR wifi_getip( lua_State* L, uint8_t mode )
wifi_get_ip_info
(
mode
,
&
pTempIp
);
wifi_get_ip_info
(
mode
,
&
pTempIp
);
if
(
pTempIp
.
ip
.
addr
==
0
){
if
(
pTempIp
.
ip
.
addr
==
0
){
lua_pushnil
(
L
);
lua_pushnil
(
L
);
return
1
;
}
else
{
}
else
{
c_sprintf
(
temp
,
"%d.%d.%d.%d"
,
IP2STR
(
&
pTempIp
.
ip
)
);
c_sprintf
(
temp
,
"%d.%d.%d.%d"
,
IP2STR
(
&
pTempIp
.
ip
)
);
lua_pushstring
(
L
,
temp
);
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
;
return
1
;
}
}
...
@@ -179,11 +252,21 @@ static int ICACHE_FLASH_ATTR wifi_station_getmac( lua_State* L ){
...
@@ -179,11 +252,21 @@ static int ICACHE_FLASH_ATTR wifi_station_getmac( lua_State* L ){
return
wifi_getmac
(
L
,
STATION_IF
);
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()
// Lua: wifi.sta.getip()
static
int
ICACHE_FLASH_ATTR
wifi_station_getip
(
lua_State
*
L
){
static
int
ICACHE_FLASH_ATTR
wifi_station_getip
(
lua_State
*
L
){
return
wifi_getip
(
L
,
STATION_IF
);
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)
// Lua: wifi.sta.config(ssid, password)
static
int
ICACHE_FLASH_ATTR
wifi_station_config
(
lua_State
*
L
)
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 ){
...
@@ -281,11 +364,21 @@ static int ICACHE_FLASH_ATTR wifi_ap_getmac( lua_State* L ){
return
wifi_getmac
(
L
,
SOFTAP_IF
);
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()
// Lua: wifi.ap.getip()
static
int
ICACHE_FLASH_ATTR
wifi_ap_getip
(
lua_State
*
L
){
static
int
ICACHE_FLASH_ATTR
wifi_ap_getip
(
lua_State
*
L
){
return
wifi_getip
(
L
,
SOFTAP_IF
);
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)
// Lua: wifi.ap.config(table)
static
int
ICACHE_FLASH_ATTR
wifi_ap_config
(
lua_State
*
L
)
static
int
ICACHE_FLASH_ATTR
wifi_ap_config
(
lua_State
*
L
)
{
{
...
@@ -352,7 +445,9 @@ static const LUA_REG_TYPE wifi_station_map[] =
...
@@ -352,7 +445,9 @@ static const LUA_REG_TYPE wifi_station_map[] =
{
LSTRKEY
(
"disconnect"
),
LFUNCVAL
(
wifi_station_disconnect4lua
)
},
{
LSTRKEY
(
"disconnect"
),
LFUNCVAL
(
wifi_station_disconnect4lua
)
},
{
LSTRKEY
(
"autoconnect"
),
LFUNCVAL
(
wifi_station_setauto
)
},
{
LSTRKEY
(
"autoconnect"
),
LFUNCVAL
(
wifi_station_setauto
)
},
{
LSTRKEY
(
"getip"
),
LFUNCVAL
(
wifi_station_getip
)
},
{
LSTRKEY
(
"getip"
),
LFUNCVAL
(
wifi_station_getip
)
},
{
LSTRKEY
(
"setip"
),
LFUNCVAL
(
wifi_station_setip
)
},
{
LSTRKEY
(
"getmac"
),
LFUNCVAL
(
wifi_station_getmac
)
},
{
LSTRKEY
(
"getmac"
),
LFUNCVAL
(
wifi_station_getmac
)
},
{
LSTRKEY
(
"setmac"
),
LFUNCVAL
(
wifi_station_setmac
)
},
{
LSTRKEY
(
"getap"
),
LFUNCVAL
(
wifi_station_listap
)
},
{
LSTRKEY
(
"getap"
),
LFUNCVAL
(
wifi_station_listap
)
},
{
LSTRKEY
(
"status"
),
LFUNCVAL
(
wifi_station_status
)
},
{
LSTRKEY
(
"status"
),
LFUNCVAL
(
wifi_station_status
)
},
{
LNILKEY
,
LNILVAL
}
{
LNILKEY
,
LNILVAL
}
...
@@ -362,7 +457,9 @@ static const LUA_REG_TYPE wifi_ap_map[] =
...
@@ -362,7 +457,9 @@ static const LUA_REG_TYPE wifi_ap_map[] =
{
{
{
LSTRKEY
(
"config"
),
LFUNCVAL
(
wifi_ap_config
)
},
{
LSTRKEY
(
"config"
),
LFUNCVAL
(
wifi_ap_config
)
},
{
LSTRKEY
(
"getip"
),
LFUNCVAL
(
wifi_ap_getip
)
},
{
LSTRKEY
(
"getip"
),
LFUNCVAL
(
wifi_ap_getip
)
},
{
LSTRKEY
(
"setip"
),
LFUNCVAL
(
wifi_ap_setip
)
},
{
LSTRKEY
(
"getmac"
),
LFUNCVAL
(
wifi_ap_getmac
)
},
{
LSTRKEY
(
"getmac"
),
LFUNCVAL
(
wifi_ap_getmac
)
},
{
LSTRKEY
(
"setmac"
),
LFUNCVAL
(
wifi_ap_setmac
)
},
{
LNILKEY
,
LNILVAL
}
{
LNILKEY
,
LNILVAL
}
};
};
...
@@ -372,6 +469,7 @@ const LUA_REG_TYPE wifi_map[] =
...
@@ -372,6 +469,7 @@ const LUA_REG_TYPE wifi_map[] =
{
LSTRKEY
(
"getmode"
),
LFUNCVAL
(
wifi_getmode
)
},
{
LSTRKEY
(
"getmode"
),
LFUNCVAL
(
wifi_getmode
)
},
{
LSTRKEY
(
"startsmart"
),
LFUNCVAL
(
wifi_start_smart
)
},
{
LSTRKEY
(
"startsmart"
),
LFUNCVAL
(
wifi_start_smart
)
},
{
LSTRKEY
(
"stopsmart"
),
LFUNCVAL
(
wifi_exit_smart
)
},
{
LSTRKEY
(
"stopsmart"
),
LFUNCVAL
(
wifi_exit_smart
)
},
{
LSTRKEY
(
"sleeptype"
),
LFUNCVAL
(
wifi_sleeptype
)
},
#if LUA_OPTIMIZE_MEMORY > 0
#if LUA_OPTIMIZE_MEMORY > 0
{
LSTRKEY
(
"sta"
),
LROVAL
(
wifi_station_map
)
},
{
LSTRKEY
(
"sta"
),
LROVAL
(
wifi_station_map
)
},
{
LSTRKEY
(
"ap"
),
LROVAL
(
wifi_ap_map
)
},
{
LSTRKEY
(
"ap"
),
LROVAL
(
wifi_ap_map
)
},
...
@@ -381,6 +479,10 @@ const LUA_REG_TYPE wifi_map[] =
...
@@ -381,6 +479,10 @@ const LUA_REG_TYPE wifi_map[] =
{
LSTRKEY
(
"SOFTAP"
),
LNUMVAL
(
SOFTAP_MODE
)
},
{
LSTRKEY
(
"SOFTAP"
),
LNUMVAL
(
SOFTAP_MODE
)
},
{
LSTRKEY
(
"STATIONAP"
),
LNUMVAL
(
STATIONAP_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_IDLE" ), LNUMVAL( STATION_IDLE ) },
// { LSTRKEY( "STA_CONNECTING" ), LNUMVAL( STATION_CONNECTING ) },
// { LSTRKEY( "STA_CONNECTING" ), LNUMVAL( STATION_CONNECTING ) },
// { LSTRKEY( "STA_WRONGPWD" ), LNUMVAL( STATION_WRONG_PASSWORD ) },
// { LSTRKEY( "STA_WRONGPWD" ), LNUMVAL( STATION_WRONG_PASSWORD ) },
...
@@ -410,6 +512,10 @@ LUALIB_API int ICACHE_FLASH_ATTR luaopen_wifi( lua_State *L )
...
@@ -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
,
"SOFTAP"
,
SOFTAP_MODE
);
MOD_REG_NUMBER
(
L
,
"STATIONAP"
,
STATIONAP_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_IDLE", STATION_IDLE );
// MOD_REG_NUMBER( L, "STA_CONNECTING", STATION_CONNECTING );
// MOD_REG_NUMBER( L, "STA_CONNECTING", STATION_CONNECTING );
// MOD_REG_NUMBER( L, "STA_WRONGPWD", STATION_WRONG_PASSWORD );
// MOD_REG_NUMBER( L, "STA_WRONGPWD", STATION_WRONG_PASSWORD );
...
...
examples/fragment.lua
View file @
a331438c
...
@@ -300,4 +300,6 @@ sk=net.createConnection(net.TCP, 0) sk:on("receive", function(sck, c) print(c) e
...
@@ -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
:
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
=
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"
)
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
wifi
.
sta
.
setip
({
ip
=
"192.168.18.119"
,
netmask
=
"255.255.255.0"
,
gateway
=
"192.168.18.1"
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment