|
|
@@ -21,6 +21,7 @@ local wifi_ping_ip
|
|
|
local eth_ping_ip
|
|
|
local local_network_mode
|
|
|
local need_ping = true
|
|
|
+local single_network_mode = false
|
|
|
|
|
|
local ping_time = 10000
|
|
|
-- 连接状态
|
|
|
@@ -57,6 +58,103 @@ local function type_to_string(net_type)
|
|
|
return type_map[net_type] or "Unknown"
|
|
|
end
|
|
|
|
|
|
+
|
|
|
+--httpdns域名解析测试
|
|
|
+local function http_dnstest(adaptertest)
|
|
|
+ local ip = httpdns.ali("baidu.com", { adapter = adaptertest, timeout = 3000 })
|
|
|
+ if ip ~= nil then
|
|
|
+ available[adaptertest] = connection_states.CONNECTED
|
|
|
+ log.info(type_to_string(adaptertest) .. "网卡httpdns域名解析成功")
|
|
|
+ else
|
|
|
+ log.info(type_to_string(adaptertest) .. "网卡httpdns域名解析失败")
|
|
|
+ end
|
|
|
+ log.info("httpdns", "baidu.com", ip)
|
|
|
+end
|
|
|
+-- ping操作
|
|
|
+local function ping_request(adaptertest)
|
|
|
+ log.info("dns_request",type_to_string(adaptertest),need_ping)
|
|
|
+ if need_ping then
|
|
|
+ if adaptertest == socket.LWIP_ETH or adaptertest == socket.LWIP_USER1 then
|
|
|
+ if eth_ping_ip == nil then
|
|
|
+ http_dnstest(adaptertest)
|
|
|
+ else
|
|
|
+ icmp.setup(adaptertest)
|
|
|
+ icmp.ping(adaptertest, eth_ping_ip)
|
|
|
+ end
|
|
|
+ end
|
|
|
+ if adaptertest == socket.LWIP_STA then
|
|
|
+ if wifi_ping_ip == nil then
|
|
|
+ http_dnstest(adaptertest)
|
|
|
+ else
|
|
|
+ icmp.setup(adaptertest)
|
|
|
+ icmp.ping(adaptertest, wifi_ping_ip)
|
|
|
+ end
|
|
|
+ end
|
|
|
+ if adaptertest == socket.LWIP_GP then
|
|
|
+ if eth_ping_ip ~= nil then
|
|
|
+ icmp.setup(adaptertest)
|
|
|
+ icmp.ping(adaptertest, eth_ping_ip)
|
|
|
+ elseif wifi_ping_ip ~= nil then
|
|
|
+ icmp.setup(adaptertest)
|
|
|
+ icmp.ping(adaptertest, wifi_ping_ip)
|
|
|
+ else
|
|
|
+ http_dnstest(adaptertest)
|
|
|
+ end
|
|
|
+ end
|
|
|
+ else
|
|
|
+ log.info(type_to_string(adaptertest) .. "配置了不需要ping,直接切换为CONNECTED状态")
|
|
|
+ available[adaptertest] = connection_states.CONNECTED
|
|
|
+ end
|
|
|
+ apply_priority()
|
|
|
+end
|
|
|
+-- 网卡上线回调函数
|
|
|
+local function ip_ready_handle(ip, adapter)
|
|
|
+ local _, _, gw = socket.localIP(adapter)
|
|
|
+ log.info("ip_ready_handle", ip, type_to_string(adapter), "state", available[adapter], "gw", gw)
|
|
|
+ if local_network_mode then
|
|
|
+ if adapter == socket.LWIP_ETH or adapter == socket.LWIP_USER1 then
|
|
|
+ eth_ping_ip = gw
|
|
|
+ elseif adapter == socket.LWIP_STA then
|
|
|
+ wifi_ping_ip = gw
|
|
|
+ end
|
|
|
+ end
|
|
|
+ log.info("eth_ping_ip", eth_ping_ip, "wifi_ping_ip", wifi_ping_ip)
|
|
|
+ -- 需要ping操作,ping通后认为网络可用
|
|
|
+ if available[adapter] == connection_states.OPENED then
|
|
|
+ available[adapter] = connection_states.CONNECTING
|
|
|
+ end
|
|
|
+ -- ping_request(adapter)
|
|
|
+end
|
|
|
+-- 网卡下线回调函数
|
|
|
+local function ip_lose_handle(adapter)
|
|
|
+ log.info("ip_lose_handle", type_to_string(adapter))
|
|
|
+ if available[adapter] == connection_states.CONNECTING or available[adapter] == connection_states.CONNECTED then
|
|
|
+ available[adapter] = connection_states.OPENED
|
|
|
+ end
|
|
|
+ if current_active == adapter then
|
|
|
+ log.info(type_to_string(adapter) .. " 失效,切换到其他网络")
|
|
|
+ apply_priority()
|
|
|
+ end
|
|
|
+end
|
|
|
+
|
|
|
+local interval_time = nil
|
|
|
+
|
|
|
+--[[
|
|
|
+对正常状态的网卡进行ping测试
|
|
|
+@api exnetif.check_network_status(interval),
|
|
|
+@int 检测间隔时间ms(选填),不填时只检测一次,填写后将根据间隔时间循环检测,会提高模块功耗
|
|
|
+]]
|
|
|
+function exnetif.check_network_status(interval)
|
|
|
+ if interval ~= nil then
|
|
|
+ interval_time = interval
|
|
|
+ end
|
|
|
+ for _, net_type in ipairs(current_priority) do
|
|
|
+ if available[net_type] == connection_states.CONNECTED then
|
|
|
+ available[net_type] = connection_states.CONNECTING
|
|
|
+ end
|
|
|
+ end
|
|
|
+end
|
|
|
+
|
|
|
-- 状态更改后重新设置默认网卡
|
|
|
local function apply_priority()
|
|
|
local usable = false
|
|
|
@@ -97,7 +195,7 @@ local function setup_eth(config)
|
|
|
ping_time = config.ping_time
|
|
|
end
|
|
|
log.info("初始化以太网")
|
|
|
- if not config.single_network then
|
|
|
+ if not single_network_mode then
|
|
|
available[socket.LWIP_ETH] = connection_states.OPENED
|
|
|
else
|
|
|
available[socket.LWIP_ETH] = connection_states.SINGLE_NETWORK
|
|
|
@@ -106,7 +204,7 @@ local function setup_eth(config)
|
|
|
if config.pwrpin then
|
|
|
gpio.setup(config.pwrpin, 1, gpio.PULLUP)
|
|
|
end
|
|
|
- sys.wait(100)
|
|
|
+ sys.wait(100) -- 等待以太网模块上电稳定
|
|
|
if config.tp == nil then
|
|
|
log.info("8101以太网")
|
|
|
if netdrv.setup(socket.LWIP_ETH) == false then
|
|
|
@@ -163,7 +261,7 @@ local function setup_eth_user1(config)
|
|
|
ping_time = config.ping_time
|
|
|
end
|
|
|
log.info("初始化以太网")
|
|
|
- if not config.single_network then
|
|
|
+ if not single_network_mode then
|
|
|
available[socket.LWIP_USER1] = connection_states.OPENED
|
|
|
else
|
|
|
available[socket.LWIP_USER1] = connection_states.SINGLE_NETWORK
|
|
|
@@ -172,7 +270,7 @@ local function setup_eth_user1(config)
|
|
|
if config.pwrpin then
|
|
|
gpio.setup(config.pwrpin, 1, gpio.PULLUP)
|
|
|
end
|
|
|
- sys.wait(100)
|
|
|
+ sys.wait(100)-- 等待以太网模块上电稳定
|
|
|
log.info("config.opts.spi", config.opts.spi, ",config.type", config.tp)
|
|
|
-- 配置SPI和初始化网络驱动
|
|
|
local result = spi.setup(config.opts.spi, -- spi id
|
|
|
@@ -222,7 +320,7 @@ local function set_wifi_info(config)
|
|
|
log.info("密码 :", config.password)
|
|
|
log.info("ping_ip :", config.ping_ip)
|
|
|
wlan.init()
|
|
|
- if not config.single_network then
|
|
|
+ if not single_network_mode then
|
|
|
available[socket.LWIP_STA] = connection_states.OPENED
|
|
|
else
|
|
|
available[socket.LWIP_STA] = connection_states.SINGLE_NETWORK
|
|
|
@@ -299,32 +397,82 @@ exnetif.set_priority_order({
|
|
|
WIFI = { -- WiFi配置
|
|
|
ssid = "test", -- WiFi名称(string)
|
|
|
password = "HZ88888888", -- WiFi密码(string)
|
|
|
- single_network = true, -- 是否单网络模式, 默认false, 单网络模式下只使用WIFI网络
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
--- 单网络模式下只使用以太网网络
|
|
|
+-- 单网络模式下只使用SPI以太网网络
|
|
|
exnetif.set_priority_order({
|
|
|
{
|
|
|
ETHERNET = { -- 以太网配置
|
|
|
pwrpin = 140, -- 供电使能引脚(number)
|
|
|
tp = netdrv.CH390, -- 网卡芯片型号(选填参数),仅spi方式外挂以太网时需要填写。
|
|
|
opts = {spi = 1, cs = 12}, -- 外挂方式,需要额外的参数(选填参数),仅spi方式外挂以太网时需要填写。
|
|
|
- single_network = true -- 是否单网络模式, 默认false, 单网络模式下只使用以太网
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
+-- 单网络模式下只使用RMII以太网网络
|
|
|
+ exnetif.set_priority_order({
|
|
|
+ {
|
|
|
+ ETHERNET = { -- 以太网配置
|
|
|
+ pwrpin = 13, -- 供电使能引脚(number)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+-- 单网络4G时不需要调用接口,直接运行业务代码即可
|
|
|
]]
|
|
|
function exnetif.set_priority_order(networkConfigs)
|
|
|
- --判断表中数据个数
|
|
|
- if #networkConfigs <1 then
|
|
|
+ -- 判断表中数据个数
|
|
|
+ if #networkConfigs < 1 then
|
|
|
log.error("网络配置为空")
|
|
|
return false
|
|
|
end
|
|
|
+ if #networkConfigs == 1 then
|
|
|
+ single_network_mode = true
|
|
|
+ end
|
|
|
+
|
|
|
+ if not single_network_mode then
|
|
|
+ -- CONNECTING的网卡需要定时ping
|
|
|
+ sys.taskInit(function()
|
|
|
+ while true do
|
|
|
+ for _, net_type in ipairs(current_priority) do
|
|
|
+ -- log.info("网卡顺序",type_to_string(net_type),available[net_type])
|
|
|
+ if available[net_type] == connection_states.CONNECTING then
|
|
|
+ log.info(type_to_string(net_type) .. "网卡开始PING")
|
|
|
+ ping_request(net_type)
|
|
|
+ sys.wait(ping_time)
|
|
|
+ end
|
|
|
+ end
|
|
|
+ sys.wait(1000) -- 避免死循环
|
|
|
+ end
|
|
|
+ end)
|
|
|
+
|
|
|
+ -- 循环ping检测任务,默认不启用
|
|
|
+ sys.taskInit(function()
|
|
|
+ while true do
|
|
|
+ if interval_time ~= nil then
|
|
|
+ sys.wait(interval_time)
|
|
|
+ exnetif.check_network_status()
|
|
|
+ end
|
|
|
+ sys.wait(1000) -- 避免死循环
|
|
|
+ end
|
|
|
+ end)
|
|
|
+
|
|
|
+ sys.subscribe("PING_RESULT", function(id, time, dst)
|
|
|
+ log.info("ping", id, time, dst)
|
|
|
+ log.info(type_to_string(id) .. "网卡PING测试成功")
|
|
|
+ available[id] = connection_states.CONNECTED
|
|
|
+ apply_priority()
|
|
|
+ end)
|
|
|
+ -- 订阅网络状态变化的消息
|
|
|
+ sys.subscribe("IP_READY", ip_ready_handle)
|
|
|
+ sys.subscribe("IP_LOSE", ip_lose_handle)
|
|
|
+
|
|
|
+ end
|
|
|
+
|
|
|
local new_priority = {}
|
|
|
for _, config in ipairs(networkConfigs) do
|
|
|
if type(config.WIFI) == "table" then
|
|
|
- --开启wifi
|
|
|
+ -- 开启wifi
|
|
|
local res = set_wifi_info(config.WIFI)
|
|
|
if res == false then
|
|
|
log.error("wifi连接失败")
|
|
|
@@ -333,7 +481,7 @@ function exnetif.set_priority_order(networkConfigs)
|
|
|
table.insert(new_priority, socket.LWIP_STA)
|
|
|
end
|
|
|
if type(config.ETHUSER1) == "table" then
|
|
|
- --开启以太网
|
|
|
+ -- 开启以太网
|
|
|
local res = setup_eth_user1(config.ETHUSER1)
|
|
|
if res == false then
|
|
|
log.error("以太网打开失败")
|
|
|
@@ -342,7 +490,7 @@ function exnetif.set_priority_order(networkConfigs)
|
|
|
table.insert(new_priority, socket.LWIP_USER1)
|
|
|
end
|
|
|
if type(config.ETHERNET) == "table" then
|
|
|
- --开启以太网
|
|
|
+ -- 开启以太网
|
|
|
local res = setup_eth(config.ETHERNET)
|
|
|
if res == false then
|
|
|
log.error("以太网打开失败")
|
|
|
@@ -351,7 +499,7 @@ function exnetif.set_priority_order(networkConfigs)
|
|
|
table.insert(new_priority, socket.LWIP_ETH)
|
|
|
end
|
|
|
if config.LWIP_GP then
|
|
|
- --开启4G
|
|
|
+ -- 开启4G
|
|
|
table.insert(new_priority, socket.LWIP_GP)
|
|
|
available[socket.LWIP_GP] = connection_states.CONNECTING
|
|
|
end
|
|
|
@@ -371,6 +519,7 @@ function exnetif.set_priority_order(networkConfigs)
|
|
|
return true
|
|
|
end
|
|
|
|
|
|
+
|
|
|
--[[
|
|
|
设置网络状态变化回调函数。触发条件:网卡切换或者所有网卡都断网。回调函数的输入参数: 1. 当有可用网络的时候,返回当前使用网卡、网卡id;2. 当没有可用网络的时候,返回 nil、-1 。
|
|
|
@api exnetif.notify_status(cb_fnc)
|
|
|
@@ -396,8 +545,32 @@ end
|
|
|
@adapter 提供网络的网卡,例如socket.LWIP_GP
|
|
|
@table 其他设置参数(选填参数),
|
|
|
@usage
|
|
|
- --典型应用:
|
|
|
- -- 4G作为出口供WiFi和以太网设备上网
|
|
|
+ 典型应用:
|
|
|
+ -- 以太网WAN提供网络其他设备连接以太网LAN口上网
|
|
|
+ exnetif.setproxy(socket.LWIP_ETH, socket.LWIP_USER1, {
|
|
|
+ ethpower_en = 20,-- 以太网模块的pwrpin引脚(gpio编号)
|
|
|
+ tp = netdrv.CH390, -- 网卡芯片型号(选填参数),仅spi方式外挂以太网时需要填写。
|
|
|
+ opts = {spi = 0, cs = 8}, -- 外挂方式,需要额外的参数(选填参数),仅spi方式外挂以太网时需要填写。
|
|
|
+ main_adapter = {
|
|
|
+ ethpower_en = 21,-- 以太网模块的pwrpin引脚(gpio编号)
|
|
|
+ tp = netdrv.CH390, -- 网卡芯片型号(选填参数),仅spi方式外挂以太网时需要填写。
|
|
|
+ opts = {spi = 1, cs = 12}
|
|
|
+ }
|
|
|
+ }) then
|
|
|
+ -- wifi_sta提供网络开启wifi_ap热点供设备上网
|
|
|
+ exnetif.setproxy(socket.LWIP_AP, socket.LWIP_STA, {
|
|
|
+ ssid = "test2", -- AP热点名称(string),网卡包含wifi时填写
|
|
|
+ password = "HZ88888888", -- AP热点密码(string),网卡包含wifi时填写
|
|
|
+ ap_opts = { -- AP模式下配置项(选填参数)
|
|
|
+ hidden = false, -- 是否隐藏SSID, 默认false,不隐藏
|
|
|
+ max_conn = 4 }, -- 最大客户端数量, 默认4
|
|
|
+ channel = 6, -- AP建立的通道, 默认6
|
|
|
+ main_adapter = {
|
|
|
+ ssid = "test", -- 提供网络的网卡开启参数
|
|
|
+ password = "HZ88888888"
|
|
|
+ }
|
|
|
+ })
|
|
|
+ -- 4G提供网络开启wifi_ap热点供设备上网,其他设备连接以太网LAN口上网
|
|
|
exnetif.setproxy(socket.LWIP_AP, socket.LWIP_GP, {
|
|
|
ssid = "Hotspot", -- WiFi名称(string),网卡包含wifi时填写
|
|
|
password = "password123", -- WiFi密码(string),网卡包含wifi时填写
|
|
|
@@ -415,22 +588,164 @@ end
|
|
|
adapter_addr = "192.168.5.1", -- adapter网卡的ip地址(选填),需要自定义ip和网关ip时填写
|
|
|
adapter_gw= { 192, 168, 5, 1 }, -- adapter网卡的网关地址(选填),需要自定义ip和网关ip时填写
|
|
|
})
|
|
|
- -- 以太网作为出口供WiFi设备上网
|
|
|
+ -- 以太网提供网络供WiFi设备上网
|
|
|
exnetif.setproxy(socket.LWIP_AP, socket.LWIP_ETH, {
|
|
|
ssid = "Hotspot", -- WiFi名称(string),网卡包含wifi时填写
|
|
|
password = "password123", -- WiFi密码(string),网卡包含wifi时填写
|
|
|
- tp = netdrv.CH390, -- 网卡芯片型号(选填参数),仅spi方式外挂以太网时需要填写。
|
|
|
- opts = { spi = 1, cs = 12}, -- 外挂方式,需要额外的参数(选填参数),仅spi方式外挂以太网时需要填写。
|
|
|
- ethpower_en = 140, -- 以太网模块的pwrpin引脚(gpio编号)
|
|
|
+ main_adapter={
|
|
|
+ tp = netdrv.CH390, -- 网卡芯片型号(选填参数),仅spi方式外挂以太网时需要填写。
|
|
|
+ opts = { spi = 1, cs = 12}, -- 外挂方式,需要额外的参数(选填参数),仅spi方式外挂以太网时需要填写。
|
|
|
+ ethpower_en = 140, -- 以太网模块的pwrpin引脚(gpio编号)
|
|
|
+ }
|
|
|
})
|
|
|
- -- 4G作为出口供以太网设备上网
|
|
|
- exnetif.setproxy(socket.LWIP_ETH, socket.LWIP_GP, {
|
|
|
+ -- WIFI提供网络供以太网设备上网
|
|
|
+ exnetif.setproxy(socket.LWIP_ETH, socket.LWIP_STA, {
|
|
|
tp = netdrv.CH390, -- 网卡芯片型号(选填参数),仅spi方式外挂以太网时需要填写。
|
|
|
opts = { spi = 1, cs = 12}, -- 外挂方式,需要额外的参数(选填参数),仅spi方式外挂以太网时需要填写。
|
|
|
ethpower_en = 140, -- 以太网模块的pwrpin引脚(gpio编号)
|
|
|
+ main_adapter = {
|
|
|
+ ssid = "test", -- 提供网络的网卡开启参数
|
|
|
+ password = "HZ88888888"
|
|
|
+ }
|
|
|
})
|
|
|
]]
|
|
|
function exnetif.setproxy(adapter, main_adapter, other_configs)
|
|
|
+ if main_adapter == socket.LWIP_ETH and available[socket.LWIP_ETH] == connection_states.DISCONNECTED then
|
|
|
+ -- 打开WAN功能
|
|
|
+ log.info("ch390", "打开LDO供电", other_configs.main_adapter.ethpower_en)
|
|
|
+ available[socket.LWIP_ETH] = connection_states.OPENED
|
|
|
+ -- 打开CH390供电
|
|
|
+ if other_configs.main_adapter.ethpower_en then
|
|
|
+ gpio.setup(other_configs.main_adapter.ethpower_en, 1, gpio.PULLUP)
|
|
|
+ end
|
|
|
+ sys.wait(100) -- 等待以太网模块上电稳定
|
|
|
+ if other_configs.main_adapter.tp == nil then
|
|
|
+ log.info("8101以太网")
|
|
|
+ if netdrv.setup(socket.LWIP_ETH) == false then
|
|
|
+ log.error("以太网初始化失败")
|
|
|
+ if other_configs.main_adapter.ethpower_en then
|
|
|
+ gpio.close(other_configs.main_adapter.ethpower_en)
|
|
|
+ end
|
|
|
+ return false
|
|
|
+ end
|
|
|
+ else
|
|
|
+ log.info("config.opts.spi", other_configs.main_adapter.opts.spi, ",config.type", other_configs.main_adapter.tp)
|
|
|
+ -- 配置SPI和初始化网络驱动
|
|
|
+ local result = spi.setup(other_configs.main_adapter.opts.spi, -- spi id
|
|
|
+ nil, 0, -- CPHA
|
|
|
+ 0, -- CPOL
|
|
|
+ 8, -- 数据宽度
|
|
|
+ 51200000 -- ,--波特率
|
|
|
+ )
|
|
|
+ log.info("main", "open spi", result)
|
|
|
+ if result ~= 0 then -- 返回值为0,表示打开成功
|
|
|
+ log.info("main", "spi open error", result)
|
|
|
+ if other_configs.main_adapter.ethpower_en then
|
|
|
+ gpio.close(other_configs.main_adapter.ethpower_en)
|
|
|
+ end
|
|
|
+ return false
|
|
|
+ end
|
|
|
+ -- 初始化指定netdrv设备,
|
|
|
+ local success = netdrv.setup(socket.LWIP_ETH, other_configs.main_adapter.tp, other_configs.main_adapter.opts)
|
|
|
+ if not success then
|
|
|
+ log.error("以太网初始化失败")
|
|
|
+ if other_configs.main_adapter.ethpower_en then
|
|
|
+ gpio.close(other_configs.main_adapter.ethpower_en)
|
|
|
+ end
|
|
|
+ return false
|
|
|
+ end
|
|
|
+ end
|
|
|
+ netdrv.dhcp(socket.LWIP_ETH, true)
|
|
|
+ local count = 1
|
|
|
+ while 1 do
|
|
|
+ local ip = netdrv.ipv4(socket.LWIP_ETH)
|
|
|
+ if ip and ip ~= "0.0.0.0" then break end
|
|
|
+ if count > 600 then
|
|
|
+ log.error("以太网连接超时,请检查配置")
|
|
|
+ if other_configs.main_adapter.ethpower_en then
|
|
|
+ gpio.close(other_configs.main_adapter.ethpower_en)
|
|
|
+ end
|
|
|
+ return false
|
|
|
+ end
|
|
|
+ sys.wait(100)
|
|
|
+ count = count + 1
|
|
|
+ end
|
|
|
+ elseif main_adapter == socket.LWIP_USER1 and available[socket.LWIP_USER1] == connection_states.DISCONNECTED then
|
|
|
+ log.info("初始化以太网")
|
|
|
+ -- 打开CH390供电
|
|
|
+ if other_configs.main_adapter.ethpower_en then
|
|
|
+ gpio.setup(other_configs.main_adapter.ethpower_en, 1, gpio.PULLUP)
|
|
|
+ end
|
|
|
+ sys.wait(100) -- 等待以太网模块上电稳定
|
|
|
+ log.info("config.opts.spi", other_configs.main_adapter.opts.spi, ",config.type", other_configs.main_adapter.tp)
|
|
|
+ available[socket.LWIP_USER1] = connection_states.OPENED
|
|
|
+ -- 配置SPI和初始化网络驱动
|
|
|
+ local result = spi.setup(other_configs.main_adapter.opts.spi, -- spi id
|
|
|
+ nil, 0, -- CPHA
|
|
|
+ 0, -- CPOL
|
|
|
+ 8, -- 数据宽度
|
|
|
+ 51200000 -- ,--波特率
|
|
|
+ )
|
|
|
+ log.info("main", "open spi", result)
|
|
|
+ if result ~= 0 then -- 返回值为0,表示打开成功
|
|
|
+ log.info("main", "spi open error", result)
|
|
|
+ if other_configs.main_adapter.ethpower_en then
|
|
|
+ gpio.close(other_configs.main_adapter.ethpower_en)
|
|
|
+ end
|
|
|
+ return false
|
|
|
+ end
|
|
|
+ -- 初始化指定netdrv设备,
|
|
|
+ -- socket.LWIP_ETH 网络适配器编号
|
|
|
+ -- netdrv.CH390外挂CH390
|
|
|
+ -- SPI ID 1, 片选 GPIO12
|
|
|
+ if netdrv.setup(socket.LWIP_USER1, other_configs.main_adapter.tp, other_configs.main_adapter.opts) == false then
|
|
|
+ log.error("以太网初始化失败")
|
|
|
+ if other_configs.main_adapter.ethpower_en then
|
|
|
+ gpio.close(other_configs.main_adapter.ethpower_en)
|
|
|
+ end
|
|
|
+ return false
|
|
|
+ end
|
|
|
+ netdrv.dhcp(socket.LWIP_USER1, true)
|
|
|
+ log.info("以太网初始化完成")
|
|
|
+ local count = 1
|
|
|
+ while 1 do
|
|
|
+ local ip = netdrv.ipv4(socket.LWIP_USER1)
|
|
|
+ if ip and ip ~= "0.0.0.0" then break end
|
|
|
+ if count > 600 then
|
|
|
+ log.error("以太网连接超时,请检查配置")
|
|
|
+ if other_configs.main_adapter.ethpower_en then
|
|
|
+ gpio.close(other_configs.main_adapter.ethpower_en)
|
|
|
+ end
|
|
|
+ return false
|
|
|
+ end
|
|
|
+ sys.wait(100)
|
|
|
+ count = count + 1
|
|
|
+ end
|
|
|
+ elseif main_adapter == socket.LWIP_STA and available[socket.LWIP_STA] == connection_states.DISCONNECTED then
|
|
|
+ -- 打开STA功能,设置混合模式
|
|
|
+ wlan.init()
|
|
|
+ wlan.setMode(wlan.APSTA)
|
|
|
+ available[socket.LWIP_STA] = connection_states.OPENED
|
|
|
+ -- 尝试连接Wi-Fi,并处理可能出现的错误
|
|
|
+ wlan.connect(other_configs.main_adapter.ssid, other_configs.main_adapter.password)
|
|
|
+ -- 等待获取IP地址
|
|
|
+ local count = 1
|
|
|
+ while 1 do
|
|
|
+ local ip = netdrv.ipv4(socket.LWIP_STA)
|
|
|
+ if ip and ip ~= "0.0.0.0" then
|
|
|
+ log.info("WiFi STA已连接,IP:", ip)
|
|
|
+ break
|
|
|
+ end
|
|
|
+ if count > 600 then
|
|
|
+ log.error("WiFi STA连接超时,请检查配置")
|
|
|
+ return false
|
|
|
+ end
|
|
|
+ sys.wait(100)
|
|
|
+ count = count + 1
|
|
|
+ end
|
|
|
+ log.info("WiFi STA初始化完成")
|
|
|
+ end
|
|
|
+
|
|
|
if adapter == socket.LWIP_ETH then
|
|
|
log.info("ch390", "打开LDO供电", other_configs.ethpower_en)
|
|
|
if other_configs.ethpower_en then
|
|
|
@@ -465,8 +780,7 @@ function exnetif.setproxy(adapter, main_adapter, other_configs)
|
|
|
end
|
|
|
return false
|
|
|
end
|
|
|
- log.info("netdrv", "等待以太网就绪")
|
|
|
- sys.wait(1000)
|
|
|
+ sys.wait(1000) -- 等待以太网模块初始化完成,去掉会导致以太网初始化失败
|
|
|
-- 设置以太网的 IP 地址、子网掩码、网关地址
|
|
|
log.info("netdrv", "自定义以太网IP地址", other_configs.adapter_addr, "网关地址", other_configs.adapter_gw)
|
|
|
netdrv.ipv4(socket.LWIP_ETH, other_configs.adapter_addr or "192.168.5.1", "255.255.255.0", "0.0.0.0")
|
|
|
@@ -554,8 +868,7 @@ function exnetif.setproxy(adapter, main_adapter, other_configs)
|
|
|
end
|
|
|
return false
|
|
|
end
|
|
|
- log.info("netdrv", "等待以太网就绪")
|
|
|
- sys.wait(1000)
|
|
|
+ sys.wait(1000) -- 等待以太网模块初始化完成,去掉会导致以太网初始化失败
|
|
|
-- 设置以太网的 IP 地址、子网掩码、网关地址
|
|
|
log.info("netdrv", "自定义以太网IP地址", other_configs.adapter_addr, "网关地址", other_configs.adapter_gw)
|
|
|
netdrv.ipv4(socket.LWIP_USER1, other_configs.adapter_addr or "192.168.5.1", "255.255.255.0", "0.0.0.0")
|
|
|
@@ -585,276 +898,8 @@ function exnetif.setproxy(adapter, main_adapter, other_configs)
|
|
|
log.info("netdrv", "创建dns代理服务, 供以太网使用")
|
|
|
end
|
|
|
|
|
|
-
|
|
|
- if main_adapter == socket.LWIP_ETH and available[socket.LWIP_ETH] == connection_states.DISCONNECTED then
|
|
|
- -- 打开WAN功能
|
|
|
- log.info("ch390", "打开LDO供电", other_configs.ethpower_en)
|
|
|
- available[socket.LWIP_ETH] = connection_states.OPENED
|
|
|
- -- 打开CH390供电
|
|
|
- if other_configs.ethpower_en then
|
|
|
- gpio.setup(other_configs.ethpower_en, 1, gpio.PULLUP)
|
|
|
- end
|
|
|
- sys.wait(100)
|
|
|
- if other_configs.tp == nil then
|
|
|
- log.info("8101以太网")
|
|
|
- if netdrv.setup(socket.LWIP_ETH) == false then
|
|
|
- log.error("以太网初始化失败")
|
|
|
- if other_configs.ethpower_en then
|
|
|
- gpio.close(other_configs.ethpower_en)
|
|
|
- end
|
|
|
- return false
|
|
|
- end
|
|
|
- else
|
|
|
- log.info("config.opts.spi", other_configs.opts.spi, ",config.type", other_configs.tp)
|
|
|
- -- 配置SPI和初始化网络驱动
|
|
|
- local result = spi.setup(other_configs.opts.spi, -- spi id
|
|
|
- nil, 0, -- CPHA
|
|
|
- 0, -- CPOL
|
|
|
- 8, -- 数据宽度
|
|
|
- 51200000 -- ,--波特率
|
|
|
- )
|
|
|
- log.info("main", "open spi", result)
|
|
|
- if result ~= 0 then -- 返回值为0,表示打开成功
|
|
|
- log.info("main", "spi open error", result)
|
|
|
- if other_configs.ethpower_en then
|
|
|
- gpio.close(other_configs.ethpower_en)
|
|
|
- end
|
|
|
- return false
|
|
|
- end
|
|
|
- -- 初始化指定netdrv设备,
|
|
|
- local success = netdrv.setup(socket.LWIP_ETH, other_configs.tp, other_configs.opts)
|
|
|
- if not success then
|
|
|
- log.error("以太网初始化失败")
|
|
|
- if other_configs.ethpower_en then
|
|
|
- gpio.close(other_configs.ethpower_en)
|
|
|
- end
|
|
|
- return false
|
|
|
- end
|
|
|
- end
|
|
|
- netdrv.dhcp(socket.LWIP_ETH, true)
|
|
|
- local count = 1
|
|
|
- while 1 do
|
|
|
- local ip = netdrv.ipv4(socket.LWIP_ETH)
|
|
|
- if ip and ip ~= "0.0.0.0" then break end
|
|
|
- if count > 600 then
|
|
|
- log.error("以太网连接超时,请检查配置")
|
|
|
- if other_configs.ethpower_en then
|
|
|
- gpio.close(other_configs.ethpower_en)
|
|
|
- end
|
|
|
- return false
|
|
|
- end
|
|
|
- sys.wait(100)
|
|
|
- count = count + 1
|
|
|
- end
|
|
|
- elseif main_adapter == socket.LWIP_USER1 and available[socket.LWIP_USER1] == connection_states.DISCONNECTED then
|
|
|
- log.info("初始化以太网")
|
|
|
- -- 打开CH390供电
|
|
|
- if other_configs.ethpower_en then
|
|
|
- gpio.setup(other_configs.ethpower_en, 1, gpio.PULLUP)
|
|
|
- end
|
|
|
- sys.wait(100)
|
|
|
- log.info("config.opts.spi", other_configs.opts.spi, ",config.type", other_configs.tp)
|
|
|
- available[socket.LWIP_USER1] = connection_states.OPENED
|
|
|
- -- 配置SPI和初始化网络驱动
|
|
|
- local result = spi.setup(other_configs.opts.spi, -- spi id
|
|
|
- nil, 0, -- CPHA
|
|
|
- 0, -- CPOL
|
|
|
- 8, -- 数据宽度
|
|
|
- 51200000 -- ,--波特率
|
|
|
- )
|
|
|
- log.info("main", "open spi", result)
|
|
|
- if result ~= 0 then -- 返回值为0,表示打开成功
|
|
|
- log.info("main", "spi open error", result)
|
|
|
- if other_configs.ethpower_en then
|
|
|
- gpio.close(other_configs.ethpower_en)
|
|
|
- end
|
|
|
- return false
|
|
|
- end
|
|
|
- -- 初始化指定netdrv设备,
|
|
|
- -- socket.LWIP_ETH 网络适配器编号
|
|
|
- -- netdrv.CH390外挂CH390
|
|
|
- -- SPI ID 1, 片选 GPIO12
|
|
|
- if netdrv.setup(socket.LWIP_USER1, other_configs.tp, other_configs.opts) == false then
|
|
|
- log.error("以太网初始化失败")
|
|
|
- if other_configs.ethpower_en then
|
|
|
- gpio.close(other_configs.ethpower_en)
|
|
|
- end
|
|
|
- return false
|
|
|
- end
|
|
|
- netdrv.dhcp(socket.LWIP_USER1, true)
|
|
|
- log.info("以太网初始化完成")
|
|
|
- local count = 1
|
|
|
- while 1 do
|
|
|
- local ip = netdrv.ipv4(socket.LWIP_USER1)
|
|
|
- if ip and ip ~= "0.0.0.0" then break end
|
|
|
- if count > 600 then
|
|
|
- log.error("以太网连接超时,请检查配置")
|
|
|
- if other_configs.ethpower_en then
|
|
|
- gpio.close(other_configs.ethpower_en)
|
|
|
- end
|
|
|
- return false
|
|
|
- end
|
|
|
- sys.wait(100)
|
|
|
- count = count + 1
|
|
|
- end
|
|
|
- elseif main_adapter == socket.LWIP_STA and available[socket.LWIP_STA] == connection_states.DISCONNECTED then
|
|
|
- -- 打开STA功能,设置混合模式
|
|
|
- wlan.init()
|
|
|
- wlan.setMode(wlan.APSTA)
|
|
|
- available[socket.LWIP_STA] = connection_states.OPENED
|
|
|
- -- 尝试连接Wi-Fi,并处理可能出现的错误
|
|
|
- wlan.connect(other_configs.ssid, other_configs.password)
|
|
|
- -- 等待获取IP地址
|
|
|
- local count = 1
|
|
|
- while 1 do
|
|
|
- local ip = netdrv.ipv4(socket.LWIP_STA)
|
|
|
- if ip and ip ~= "0.0.0.0" then
|
|
|
- log.info("WiFi STA已连接,IP:", ip)
|
|
|
- break
|
|
|
- end
|
|
|
- if count > 600 then
|
|
|
- log.error("WiFi STA连接超时,请检查配置")
|
|
|
- return false
|
|
|
- end
|
|
|
- sys.wait(100)
|
|
|
- count = count + 1
|
|
|
- end
|
|
|
- log.info("WiFi STA初始化完成")
|
|
|
- end
|
|
|
dnsproxy.setup(adapter, main_adapter)
|
|
|
netdrv.napt(main_adapter)
|
|
|
return true
|
|
|
end
|
|
|
---httpdns域名解析测试
|
|
|
-local function http_dnstest(adaptertest)
|
|
|
- local ip = httpdns.ali("baidu.com", { adapter = adaptertest, timeout = 3000 })
|
|
|
- if ip ~= nil then
|
|
|
- available[adaptertest] = connection_states.CONNECTED
|
|
|
- log.info(type_to_string(adaptertest) .. "网卡httpdns域名解析成功")
|
|
|
- else
|
|
|
- log.info(type_to_string(adaptertest) .. "网卡httpdns域名解析失败")
|
|
|
- end
|
|
|
- log.info("httpdns", "baidu.com", ip)
|
|
|
-end
|
|
|
--- ping操作
|
|
|
-local function ping_request(adaptertest)
|
|
|
- log.info("dns_request",type_to_string(adaptertest),need_ping)
|
|
|
- if need_ping then
|
|
|
- if adaptertest == socket.LWIP_ETH or adaptertest == socket.LWIP_USER1 then
|
|
|
- if eth_ping_ip == nil then
|
|
|
- http_dnstest(adaptertest)
|
|
|
- else
|
|
|
- icmp.setup(adaptertest)
|
|
|
- icmp.ping(adaptertest, eth_ping_ip)
|
|
|
- end
|
|
|
- end
|
|
|
- if adaptertest == socket.LWIP_STA then
|
|
|
- if wifi_ping_ip == nil then
|
|
|
- http_dnstest(adaptertest)
|
|
|
- else
|
|
|
- icmp.setup(adaptertest)
|
|
|
- icmp.ping(adaptertest, wifi_ping_ip)
|
|
|
- end
|
|
|
- end
|
|
|
- if adaptertest == socket.LWIP_GP then
|
|
|
- if eth_ping_ip ~= nil then
|
|
|
- icmp.setup(adaptertest)
|
|
|
- icmp.ping(adaptertest, eth_ping_ip)
|
|
|
- elseif wifi_ping_ip ~= nil then
|
|
|
- icmp.setup(adaptertest)
|
|
|
- icmp.ping(adaptertest, wifi_ping_ip)
|
|
|
- else
|
|
|
- http_dnstest(adaptertest)
|
|
|
- end
|
|
|
- end
|
|
|
- else
|
|
|
- log.info(type_to_string(adaptertest) .. "配置了不需要ping,直接切换为CONNECTED状态")
|
|
|
- available[adaptertest] = connection_states.CONNECTED
|
|
|
- end
|
|
|
- apply_priority()
|
|
|
-end
|
|
|
--- 网卡上线回调函数
|
|
|
-local function ip_ready_handle(ip, adapter)
|
|
|
- local _, _, gw = socket.localIP(adapter)
|
|
|
- log.info("ip_ready_handle", ip, type_to_string(adapter),"state",available[adapter],"gw", gw)
|
|
|
- if local_network_mode then
|
|
|
- if adapter == socket.LWIP_ETH or adapter == socket.LWIP_USER1 then
|
|
|
- eth_ping_ip = gw
|
|
|
- elseif adapter == socket.LWIP_STA then
|
|
|
- wifi_ping_ip = gw
|
|
|
- end
|
|
|
- end
|
|
|
- log.info("eth_ping_ip",eth_ping_ip,"wifi_ping_ip",wifi_ping_ip)
|
|
|
- -- 需要ping操作,ping通后认为网络可用
|
|
|
- if available[adapter] == connection_states.OPENED then
|
|
|
- available[adapter] = connection_states.CONNECTING
|
|
|
- end
|
|
|
- -- ping_request(adapter)
|
|
|
-end
|
|
|
--- 网卡下线回调函数
|
|
|
-local function ip_lose_handle(adapter)
|
|
|
- log.info("ip_lose_handle", type_to_string(adapter))
|
|
|
- if available[adapter] ~= connection_states.DISCONNECTED then
|
|
|
- available[adapter] = connection_states.OPENED
|
|
|
- end
|
|
|
- if current_active == adapter then
|
|
|
- log.info(type_to_string(adapter) .. " 失效,切换到其他网络")
|
|
|
- apply_priority()
|
|
|
- end
|
|
|
-end
|
|
|
-
|
|
|
---CONNECTING的网卡需要定时ping
|
|
|
-sys.taskInit(function()
|
|
|
- while true do
|
|
|
- for _, net_type in ipairs(current_priority) do
|
|
|
- -- log.info("网卡顺序",type_to_string(net_type),available[net_type])
|
|
|
- if available[net_type] == connection_states.CONNECTING then
|
|
|
- log.info(type_to_string(net_type) .. "网卡开始PING")
|
|
|
- ping_request(net_type)
|
|
|
- sys.wait(ping_time)
|
|
|
- end
|
|
|
- end
|
|
|
- sys.wait(1000)
|
|
|
- end
|
|
|
-end)
|
|
|
-
|
|
|
-local interval_time = nil
|
|
|
-
|
|
|
---[[
|
|
|
-对正常状态的网卡进行ping测试
|
|
|
-@api exnetif.check_network_status(interval),
|
|
|
-@int 检测间隔时间ms(选填),不填时只检测一次,填写后将根据间隔时间循环检测,会提高模块功耗
|
|
|
-]]
|
|
|
-function exnetif.check_network_status(interval)
|
|
|
- if interval ~= nil then
|
|
|
- interval_time = interval
|
|
|
- end
|
|
|
- for _, net_type in ipairs(current_priority) do
|
|
|
- if available[net_type] == connection_states.CONNECTED then
|
|
|
- available[net_type] = connection_states.CONNECTING
|
|
|
- end
|
|
|
- end
|
|
|
-end
|
|
|
-
|
|
|
-
|
|
|
---循环ping检测任务,默认不启用
|
|
|
-sys.taskInit(function()
|
|
|
- while true do
|
|
|
- if interval_time ~= nil then
|
|
|
- sys.wait(interval_time)
|
|
|
- exnetif.check_network_status()
|
|
|
- end
|
|
|
- sys.wait(1000)
|
|
|
- end
|
|
|
-end)
|
|
|
-
|
|
|
-sys.subscribe("PING_RESULT", function(id, time, dst)
|
|
|
- log.info("ping",id, time, dst)
|
|
|
- log.info(type_to_string(id) .. "网卡PING测试成功")
|
|
|
- available[id] = connection_states.CONNECTED
|
|
|
- apply_priority()
|
|
|
-end)
|
|
|
--- 订阅网络状态变化的消息
|
|
|
-sys.subscribe("IP_READY", ip_ready_handle)
|
|
|
-sys.subscribe("IP_LOSE", ip_lose_handle)
|
|
|
return exnetif
|