Add eth.set_ip() (#3437)

Sets static IP address, netmask, gateway, dns address of the ethernet.
The code is directly copied from the wifi.sta module.
This commit is contained in:
Lian Yang 2021-09-07 02:48:06 +08:00 committed by GitHub
parent 8e0e0cb31c
commit fa6fd1a41b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 0 deletions

View File

@ -193,6 +193,58 @@ static int leth_get_mac( lua_State *L )
return 1; return 1;
} }
static int leth_set_ip(lua_State *L )
{
tcpip_adapter_ip_info_t ipInfo;
tcpip_adapter_dns_info_t dnsinfo;
size_t len;
const char *str;
ip_addr_t ipAddr;
ipAddr.type = IPADDR_TYPE_V4;
luaL_checkanytable (L, 1);
//memset(&ipInfo, 0, sizeof(tcpip_adapter_ip_info_t));
lua_getfield (L, 1, "ip");
str = luaL_checklstring (L, -1, &len);
if(!ipaddr_aton(str, &ipAddr))
{
return luaL_error(L, "Could not parse IP address, aborting");
}
ipInfo.ip = ipAddr.u_addr.ip4;
lua_getfield (L, 1, "netmask");
str = luaL_checklstring (L, -1, &len);
if(!ipaddr_aton(str, &ipAddr))
{
return luaL_error(L, "Could not parse Netmask, aborting");
}
ipInfo.netmask = ipAddr.u_addr.ip4;
lua_getfield (L, 1, "gateway");
str = luaL_checklstring (L, -1, &len);
if(!ipaddr_aton(str, &ipAddr))
{
return luaL_error(L, "Could not parse Gateway address, aborting");
}
ipInfo.gw = ipAddr.u_addr.ip4;
lua_getfield (L, 1, "dns");
str = luaL_optlstring(L, -1, str, &len);
if(!ipaddr_aton(str, &dnsinfo.ip))
{
return luaL_error(L, "Could not parse DNS address, aborting");
}
ESP_ERROR_CHECK(tcpip_adapter_dhcpc_stop(ESP_IF_ETH));
tcpip_adapter_set_ip_info(ESP_IF_ETH, &ipInfo);
tcpip_adapter_set_dns_info(ESP_IF_ETH, TCPIP_ADAPTER_DNS_MAIN, &dnsinfo);
return 0;
}
static int leth_get_speed( lua_State *L ) static int leth_get_speed( lua_State *L )
{ {
eth_speed_mode_t speed = esp_eth_get_speed(); eth_speed_mode_t speed = esp_eth_get_speed();
@ -293,6 +345,7 @@ LROT_BEGIN(eth)
LROT_FUNCENTRY( get_speed, leth_get_speed ) LROT_FUNCENTRY( get_speed, leth_get_speed )
LROT_FUNCENTRY( get_mac, leth_get_mac ) LROT_FUNCENTRY( get_mac, leth_get_mac )
LROT_FUNCENTRY( set_mac, leth_set_mac ) LROT_FUNCENTRY( set_mac, leth_set_mac )
LROT_FUNCENTRY( set_ip, leth_set_ip )
LROT_NUMENTRY( PHY_LAN8720, ETH_PHY_LAN8720 ) LROT_NUMENTRY( PHY_LAN8720, ETH_PHY_LAN8720 )
LROT_NUMENTRY( PHY_TLK110, ETH_PHY_TLK110 ) LROT_NUMENTRY( PHY_TLK110, ETH_PHY_TLK110 )

View File

@ -154,3 +154,33 @@ eth.set_mac(mac)
`nil` `nil`
An error is thrown in case of invalid parameters or if the ethernet driver failed. An error is thrown in case of invalid parameters or if the ethernet driver failed.
## eth.set_ip()
Sets IP address, netmask, gateway, dns address of the ethernet.
Options set by this function are not saved to flash.
#### Syntax
`eth.set_ip(cfg)`
#### Parameters
- `cfg` table to hold configuration:
- `ip` device ip address.
- `netmask` network netmask.
- `gateway` gateway address.
- `dns` name server address.
#### Returns
`nil`
#### Example
```lua
cfg={}
cfg.ip=192.168.0.10
cfg.netmask=255.255.255.0
cfg.gateway=192.168.0.1
cfg.dns=8.8.8.8
eth.set_ip(cfg)
```