每个网络接口增加“网络检测”任务,定制检测网络断开和连接等操作
This commit is contained in:
parent
dbc0555a25
commit
0bdc856f8a
117
Board/AP0103.cpp
117
Board/AP0103.cpp
|
@ -39,13 +39,6 @@ AP0103::AP0103()
|
|||
Data = nullptr;
|
||||
Size = 0;
|
||||
|
||||
// AlarmObj = nullptr;
|
||||
// 一众标识初始化
|
||||
|
||||
NetMaster = false;
|
||||
NetBra = false;
|
||||
EspMaster = false;
|
||||
EspBra = false;
|
||||
Current = this;
|
||||
}
|
||||
|
||||
|
@ -142,8 +135,6 @@ NetworkInterface* AP0103::Create5500()
|
|||
else
|
||||
host = new W5500(Spi1, PE8, PE7);
|
||||
|
||||
host->NetReady.Bind(&AP0103::OpenClient, this);
|
||||
|
||||
return host;
|
||||
}
|
||||
|
||||
|
@ -162,13 +153,10 @@ NetworkInterface* AP0103::Create8266(bool apOnly)
|
|||
host->Mode = NetworkType::STA_AP;
|
||||
}
|
||||
|
||||
// 绑定委托,避免5500没有连接时导致没有启动客户端
|
||||
host->NetReady.Bind(&AP0103::OpenClient, this);
|
||||
|
||||
Client->Register("SetWiFi", &Esp8266::SetWiFi, host);
|
||||
Client->Register("GetWiFi", &Esp8266::GetWiFi, host);
|
||||
|
||||
host->OpenAsync();
|
||||
host->Open();
|
||||
|
||||
return host;
|
||||
}
|
||||
|
@ -227,109 +215,6 @@ void AP0103::Register(int index, IDataPort& dp)
|
|||
ds.Register(index, dp);
|
||||
}
|
||||
|
||||
void AP0103::OpenClient(NetworkInterface& host)
|
||||
{
|
||||
assert(Client, "Client");
|
||||
|
||||
debug_printf("\r\n OpenClient \r\n");
|
||||
|
||||
// 网络就绪后,打开指示灯
|
||||
auto net = dynamic_cast<W5500*>(&host);
|
||||
if (net && !net->Led) net->SetLed(*Leds[0]);
|
||||
|
||||
auto esp = dynamic_cast<Esp8266*>(&host);
|
||||
if (esp && !esp->Led) esp->SetLed(*Leds[1]);
|
||||
|
||||
auto tk = TokenConfig::Current;
|
||||
NetUri uri(NetType::Udp, IPAddress::Broadcast(), 3355);
|
||||
|
||||
// 避免重复打开
|
||||
if (!Client->Opened && Host)
|
||||
{
|
||||
AddControl(*Host, tk->Uri(), 0);
|
||||
AddControl(*Host, uri, tk->Port);
|
||||
|
||||
Client->Open();
|
||||
if (_GateWay)_GateWay->Start();
|
||||
}
|
||||
|
||||
// 避免重复打开
|
||||
if (Host)
|
||||
{
|
||||
if (Host == esp) // 8266 作为Host的时候 使用 Master 和广播端口两个 HostAP 为空
|
||||
{
|
||||
if (esp->Joined && !EspMaster)
|
||||
{
|
||||
AddControl(*Host, tk->Uri(), 0); // 如果 Host 是 ESP8266 则要求 JoinAP 完成才能添加主控制器
|
||||
EspMaster = true;
|
||||
}
|
||||
if (!EspBra)
|
||||
{
|
||||
AddControl(*Host, uri, tk->Port);
|
||||
EspBra = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (Host == net) // w5500 作为Host的时候 使用Master和广播两个端口 HostAP 开启AP时非空 打开其内网端口
|
||||
{
|
||||
if (!NetMaster)
|
||||
{
|
||||
AddControl(*Host, tk->Uri(), 0); // 如果 Host 是 W5500 打开了就直接允许添加Master
|
||||
NetMaster = true;
|
||||
}
|
||||
if (!NetBra)
|
||||
{
|
||||
AddControl(*Host, uri, tk->Port);
|
||||
NetBra = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (HostAP && HostAP == esp) // 只使用esp的时候HostAp为空
|
||||
{
|
||||
if (!EspBra)
|
||||
{
|
||||
AddControl(*Host, uri, tk->Port);
|
||||
EspBra = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!Client->Opened)
|
||||
{
|
||||
Client->Open();
|
||||
if (_GateWay)_GateWay->Start();
|
||||
}
|
||||
else
|
||||
{
|
||||
Client->AttachControls();
|
||||
if (_GateWay)_GateWay->Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TokenController* AP0103::AddControl(NetworkInterface& host, const NetUri& uri, ushort localPort)
|
||||
{
|
||||
// 创建连接服务器的Socket
|
||||
auto socket = Socket::CreateRemote(uri);
|
||||
|
||||
// 创建连接服务器的控制器
|
||||
auto ctrl = new TokenController();
|
||||
//ctrl->Port = dynamic_cast<ITransport*>(socket);
|
||||
ctrl->Socket = socket;
|
||||
|
||||
// 创建客户端
|
||||
auto client = Client;
|
||||
if (localPort == 0)
|
||||
client->Master = ctrl;
|
||||
else
|
||||
{
|
||||
socket->Local.Port = localPort;
|
||||
ctrl->ShowRemote = true;
|
||||
client->Controls.Add(ctrl);
|
||||
}
|
||||
|
||||
return ctrl;
|
||||
}
|
||||
|
||||
/*
|
||||
网络使用流程:
|
||||
|
||||
|
|
|
@ -73,15 +73,6 @@ private:
|
|||
void* Data;
|
||||
int Size;
|
||||
|
||||
// Control 打开情况标识
|
||||
bool NetMaster;
|
||||
bool NetBra;
|
||||
bool EspMaster;
|
||||
bool EspBra;
|
||||
|
||||
void OpenClient(NetworkInterface& host);
|
||||
TokenController* AddControl(NetworkInterface& host, const NetUri& uri, ushort localPort);
|
||||
|
||||
static int Fix2401(const Buffer& bs);
|
||||
};
|
||||
|
||||
|
|
116
Board/AP0104.cpp
116
Board/AP0104.cpp
|
@ -39,13 +39,6 @@ AP0104::AP0104()
|
|||
Data = nullptr;
|
||||
Size = 0;
|
||||
|
||||
// AlarmObj = nullptr;
|
||||
// 一众标识初始化
|
||||
|
||||
NetMaster = false;
|
||||
NetBra = false;
|
||||
EspMaster = false;
|
||||
EspBra = false;
|
||||
Current = this;
|
||||
}
|
||||
|
||||
|
@ -136,7 +129,6 @@ NetworkInterface* AP0104::Create5500()
|
|||
debug_printf("\r\nW5500::Create \r\n");
|
||||
|
||||
auto host = new W5500(Spi1, PE7, PB2);
|
||||
host->NetReady.Bind(&AP0104::OpenClient, this);
|
||||
|
||||
return host;
|
||||
}
|
||||
|
@ -156,13 +148,10 @@ NetworkInterface* AP0104::Create8266(bool apOnly)
|
|||
host->Mode = NetworkType::STA_AP;
|
||||
}
|
||||
|
||||
// 绑定委托,避免5500没有连接时导致没有启动客户端
|
||||
host->NetReady.Bind(&AP0104::OpenClient, this);
|
||||
|
||||
Client->Register("SetWiFi", &Esp8266::SetWiFi, host);
|
||||
Client->Register("GetWiFi", &Esp8266::GetWiFi, host);
|
||||
|
||||
host->OpenAsync();
|
||||
host->Open();
|
||||
|
||||
return host;
|
||||
}
|
||||
|
@ -221,109 +210,6 @@ void AP0104::Register(int index, IDataPort& dp)
|
|||
ds.Register(index, dp);
|
||||
}
|
||||
|
||||
void AP0104::OpenClient(NetworkInterface& host)
|
||||
{
|
||||
assert(Client, "Client");
|
||||
|
||||
debug_printf("\r\n OpenClient \r\n");
|
||||
|
||||
// 网络就绪后,打开指示灯
|
||||
auto net = dynamic_cast<W5500*>(&host);
|
||||
if (net && !net->Led) net->SetLed(*Leds[0]);
|
||||
|
||||
auto esp = dynamic_cast<Esp8266*>(&host);
|
||||
if (esp && !esp->Led) esp->SetLed(*Leds[1]);
|
||||
|
||||
auto tk = TokenConfig::Current;
|
||||
NetUri uri(NetType::Udp, IPAddress::Broadcast(), 3355);
|
||||
|
||||
// 避免重复打开
|
||||
if (!Client->Opened && Host)
|
||||
{
|
||||
AddControl(*Host, tk->Uri(), 0);
|
||||
AddControl(*Host, uri, tk->Port);
|
||||
|
||||
Client->Open();
|
||||
if (_GateWay)_GateWay->Start();
|
||||
}
|
||||
|
||||
// 避免重复打开
|
||||
if (Host)
|
||||
{
|
||||
if (Host == esp) // 8266 作为Host的时候 使用 Master 和广播端口两个 HostAP 为空
|
||||
{
|
||||
if (esp->Joined && !EspMaster)
|
||||
{
|
||||
AddControl(*Host, tk->Uri(), 0); // 如果 Host 是 ESP8266 则要求 JoinAP 完成才能添加主控制器
|
||||
EspMaster = true;
|
||||
}
|
||||
if (!EspBra)
|
||||
{
|
||||
AddControl(*Host, uri, tk->Port);
|
||||
EspBra = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (Host == net) // w5500 作为Host的时候 使用Master和广播两个端口 HostAP 开启AP时非空 打开其内网端口
|
||||
{
|
||||
if (!NetMaster)
|
||||
{
|
||||
AddControl(*Host, tk->Uri(), 0); // 如果 Host 是 W5500 打开了就直接允许添加Master
|
||||
NetMaster = true;
|
||||
}
|
||||
if (!NetBra)
|
||||
{
|
||||
AddControl(*Host, uri, tk->Port);
|
||||
NetBra = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (HostAP && HostAP == esp) // 只使用esp的时候HostAp为空
|
||||
{
|
||||
if (!EspBra)
|
||||
{
|
||||
AddControl(*Host, uri, tk->Port);
|
||||
EspBra = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!Client->Opened)
|
||||
{
|
||||
Client->Open();
|
||||
if (_GateWay)_GateWay->Start();
|
||||
}
|
||||
else
|
||||
{
|
||||
Client->AttachControls();
|
||||
if (_GateWay)_GateWay->Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TokenController* AP0104::AddControl(NetworkInterface& host, const NetUri& uri, ushort localPort)
|
||||
{
|
||||
// 创建连接服务器的Socket
|
||||
auto socket = Socket::CreateRemote(uri);
|
||||
|
||||
// 创建连接服务器的控制器
|
||||
auto ctrl = new TokenController();
|
||||
//ctrl->Port = dynamic_cast<ITransport*>(socket);
|
||||
ctrl->Socket = socket;
|
||||
|
||||
// 创建客户端
|
||||
auto client = Client;
|
||||
if (localPort == 0)
|
||||
client->Master = ctrl;
|
||||
else
|
||||
{
|
||||
socket->Local.Port = localPort;
|
||||
ctrl->ShowRemote = true;
|
||||
client->Controls.Add(ctrl);
|
||||
}
|
||||
|
||||
return ctrl;
|
||||
}
|
||||
|
||||
/*
|
||||
网络使用流程:
|
||||
|
||||
|
|
|
@ -72,15 +72,6 @@ private:
|
|||
void* Data;
|
||||
int Size;
|
||||
|
||||
// Control 打开情况标识
|
||||
bool NetMaster;
|
||||
bool NetBra;
|
||||
bool EspMaster;
|
||||
bool EspBra;
|
||||
|
||||
void OpenClient(NetworkInterface& host);
|
||||
TokenController* AddControl(NetworkInterface& host, const NetUri& uri, ushort localPort);
|
||||
|
||||
static int Fix2401(const Buffer& bs);
|
||||
};
|
||||
|
||||
|
|
128
Board/AP0801.cpp
128
Board/AP0801.cpp
|
@ -36,11 +36,7 @@ AP0801::AP0801()
|
|||
|
||||
Data = nullptr;
|
||||
Size = 0;
|
||||
// 一众标识初始化
|
||||
NetMaster = false;
|
||||
NetBra = false;
|
||||
EspMaster = false;
|
||||
EspBra = false;
|
||||
|
||||
Current = this;
|
||||
}
|
||||
|
||||
|
@ -135,9 +131,13 @@ NetworkInterface* AP0801::Create5500()
|
|||
|
||||
auto net = new W5500(Spi2, PE1, PD13);
|
||||
// 软路由的DHCP要求很严格,必须先把自己IP设为0
|
||||
net->IP = IPAddress::Any();
|
||||
//net->IP = IPAddress::Any();
|
||||
|
||||
net->NetReady.Bind(&AP0801::OpenClient, this);
|
||||
if(!net->Open())
|
||||
{
|
||||
delete net;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
net->EnableDNS();
|
||||
net->EnableDHCP();
|
||||
|
@ -147,6 +147,8 @@ NetworkInterface* AP0801::Create5500()
|
|||
|
||||
NetworkInterface* AP0801::Create8266(bool apOnly)
|
||||
{
|
||||
debug_printf("\r\nEsp8266::Create \r\n");
|
||||
|
||||
auto esp = new Esp8266(COM4, PE2, PD3);
|
||||
|
||||
// 初次需要指定模式 否则为 Wire
|
||||
|
@ -160,12 +162,14 @@ NetworkInterface* AP0801::Create8266(bool apOnly)
|
|||
esp->WorkMode = NetworkType::STA_AP;
|
||||
}
|
||||
|
||||
// 绑定委托,避免5500没有连接时导致没有启动客户端
|
||||
esp->NetReady.Bind(&AP0801::OpenClient, this);
|
||||
if(!esp->Open())
|
||||
{
|
||||
delete esp;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Client->Register("SetWiFi", &Esp8266::SetWiFi, esp);
|
||||
Client->Register("GetWiFi", &Esp8266::GetWiFi, esp);
|
||||
esp->OpenAsync();
|
||||
|
||||
return esp;
|
||||
}
|
||||
|
@ -235,112 +239,12 @@ void AP0801::Register(uint offset, uint size, Handler hook)
|
|||
ds.Register(offset, size, hook);
|
||||
}
|
||||
|
||||
void AP0801::OpenClient(NetworkInterface& host)
|
||||
{
|
||||
assert(Client, "Client");
|
||||
|
||||
// 网络就绪后,打开指示灯
|
||||
auto net = dynamic_cast<W5500*>(&host);
|
||||
if (net && !net->Led) net->SetLed(*Leds[0]);
|
||||
|
||||
auto esp = dynamic_cast<Esp8266*>(&host);
|
||||
if (esp && !esp->Led) esp->SetLed(*Leds[1]);
|
||||
|
||||
auto tk = TokenConfig::Current;
|
||||
NetUri uri(NetType::Udp, IPAddress::Broadcast(), 3355);
|
||||
|
||||
// 避免重复打开
|
||||
if (Host)
|
||||
{
|
||||
if (Host == esp) // 8266 作为Host的时候 使用 Master 和广播端口两个 HostAP 为空
|
||||
{
|
||||
if (esp->Joined && !EspMaster)
|
||||
{
|
||||
AddControl(*Host, tk->Uri(), 0); // 如果 Host 是 ESP8266 则要求 JoinAP 完成才能添加主控制器
|
||||
EspMaster = true;
|
||||
}
|
||||
if (!EspBra)
|
||||
{
|
||||
AddControl(*Host, uri, tk->Port);
|
||||
EspBra = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (Host == net) // w5500 作为Host的时候 使用Master和广播两个端口 HostAP 开启AP时非空 打开其内网端口
|
||||
{
|
||||
if (!NetMaster)
|
||||
{
|
||||
AddControl(*Host, tk->Uri(), 0); // 如果 Host 是 W5500 打开了就直接允许添加Master
|
||||
NetMaster = true;
|
||||
}
|
||||
if (!NetBra)
|
||||
{
|
||||
AddControl(*Host, uri, tk->Port);
|
||||
NetBra = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (HostAP && HostAP == esp) // 只使用esp的时候HostAp为空
|
||||
{
|
||||
if (!EspBra)
|
||||
{
|
||||
AddControl(*Host, uri, tk->Port);
|
||||
EspBra = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!Client->Opened)
|
||||
{
|
||||
Client->Open();
|
||||
if (ProxyFac)ProxyFac->AutoStart();
|
||||
}
|
||||
else
|
||||
Client->AttachControls();
|
||||
}
|
||||
}
|
||||
|
||||
void AP0801::AddControl(NetworkInterface& host, const NetUri& uri, ushort localPort)
|
||||
{
|
||||
// 创建连接服务器的Socket
|
||||
auto socket = Socket::CreateRemote(uri);
|
||||
|
||||
// 创建连接服务器的控制器
|
||||
auto ctrl = new TokenController();
|
||||
ctrl->Socket = socket;
|
||||
|
||||
// 创建客户端
|
||||
auto tc = Client;
|
||||
if (localPort == 0)
|
||||
tc->Master = ctrl;
|
||||
else
|
||||
{
|
||||
socket->Local.Port = localPort;
|
||||
ctrl->ShowRemote = true;
|
||||
tc->Controls.Add(ctrl);
|
||||
}
|
||||
}
|
||||
|
||||
void OnInitNet(void* param)
|
||||
{
|
||||
auto& bsp = *(AP0801*)param;
|
||||
|
||||
// 检查是否连接网线
|
||||
auto host = (W5500*)bsp.Create5500();
|
||||
if (host->Open())
|
||||
bsp.Host = host;
|
||||
else
|
||||
delete host;
|
||||
|
||||
auto esp = (WiFiInterface*)bsp.Create8266(false);
|
||||
if (esp)
|
||||
{
|
||||
// 未组网时,主机留空,仅保留AP主机
|
||||
bool join = esp->SSID && *esp->SSID;
|
||||
if (join && esp->IsStation())
|
||||
bsp.Host = esp;
|
||||
else
|
||||
bsp.HostAP = esp;
|
||||
}
|
||||
bsp.Create5500();
|
||||
bsp.Create8266(false);
|
||||
}
|
||||
|
||||
void AP0801::InitNet()
|
||||
|
|
|
@ -21,12 +21,8 @@ public:
|
|||
List<OutputPort*> Outputs;
|
||||
List<InputPort*> Inputs;
|
||||
|
||||
// HardRTC* Rtc;
|
||||
|
||||
NetworkInterface* Host; // 网络主机
|
||||
NetworkInterface* HostAP; // 网络主机
|
||||
//TokenClient* Client; // 令牌客户端
|
||||
//ProxyFactory* ProxyFac; // 透传管理器
|
||||
Alarm* AlarmObj;
|
||||
|
||||
AP0801();
|
||||
|
@ -70,16 +66,6 @@ public:
|
|||
private:
|
||||
void* Data;
|
||||
int Size;
|
||||
// Control 打开情况标识
|
||||
bool NetMaster;
|
||||
bool NetBra;
|
||||
bool EspMaster;
|
||||
bool EspBra;
|
||||
|
||||
// uint Flag; // 内部使用 标识Controller创建情况
|
||||
|
||||
void OpenClient(NetworkInterface& host);
|
||||
void AddControl(NetworkInterface& host, const NetUri& uri, ushort localPort);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
134
Board/AP0802.cpp
134
Board/AP0802.cpp
|
@ -26,11 +26,7 @@ AP0802::AP0802()
|
|||
|
||||
Data = nullptr;
|
||||
Size = 0;
|
||||
// Control 打开情况标识
|
||||
NetMaster = false;
|
||||
NetBra = false;
|
||||
EspMaster = false;
|
||||
EspBra = false;
|
||||
|
||||
Current = this;
|
||||
|
||||
HardwareVer = HardwareVerLast;
|
||||
|
@ -167,7 +163,6 @@ NetworkInterface* AP0802::Create5500()
|
|||
debug_printf("\r\nW5500::Create \r\n");
|
||||
|
||||
auto host = new W5500(Spi2, PE1, PD13);
|
||||
host->NetReady.Bind(&AP0802::OpenClient, this);
|
||||
|
||||
return host;
|
||||
}
|
||||
|
@ -188,13 +183,10 @@ NetworkInterface* AP0802::Create8266(bool apOnly)
|
|||
host->WorkMode = NetworkType::STA_AP;
|
||||
}
|
||||
|
||||
// 绑定委托,避免5500没有连接时导致没有启动客户端
|
||||
host->NetReady.Bind(&AP0802::OpenClient, this);
|
||||
|
||||
Client->Register("SetWiFi", &Esp8266::SetWiFi, host);
|
||||
Client->Register("GetWiFi", &Esp8266::GetWiFi, host);
|
||||
|
||||
host->OpenAsync();
|
||||
host->Open();
|
||||
|
||||
return host;
|
||||
}
|
||||
|
@ -256,128 +248,6 @@ void AP0802::Register(int index, IDataPort& dp)
|
|||
ds.Register(index, dp);
|
||||
}
|
||||
|
||||
/*
|
||||
// Control 打开情况标识
|
||||
bool NetMaster;
|
||||
bool NetBra;
|
||||
bool EspMaster;
|
||||
bool EspBra;
|
||||
*/
|
||||
void AP0802::OpenClient(NetworkInterface& host)
|
||||
{
|
||||
/*
|
||||
Flag 四个字节 最高字节标识esp作为Master 第二字节标识esp广播Controller
|
||||
第三字节为 w5500 作为Master 第四字节为 w5500 广播端口
|
||||
*/
|
||||
|
||||
assert(Client, "Client");
|
||||
debug_printf("\r\n OpenClient \r\n");
|
||||
|
||||
// 网络就绪后,打开指示灯
|
||||
auto net = dynamic_cast<W5500*>(&host);
|
||||
if (net && !net->Led) net->SetLed(*Leds[0]);
|
||||
|
||||
auto esp = dynamic_cast<Esp8266*>(&host);
|
||||
if (esp && !esp->Led) esp->SetLed(*Leds[1]);
|
||||
|
||||
auto tk = TokenConfig::Current;
|
||||
NetUri uri(NetType::Udp, IPAddress::Broadcast(), 3355);
|
||||
|
||||
// 避免重复打开
|
||||
if (Host)
|
||||
{
|
||||
if (Host == esp) // 8266 作为Host的时候 使用 Master 和广播端口两个 HostAP 为空
|
||||
{
|
||||
if (esp->Joined && !EspMaster)
|
||||
{
|
||||
AddControl(*Host, tk->Uri(), 0); // 如果 Host 是 ESP8266 则要求 JoinAP 完成才能添加主控制器
|
||||
EspMaster = true;
|
||||
}
|
||||
if (!EspBra)
|
||||
{
|
||||
AddControl(*Host, uri, tk->Port);
|
||||
EspBra = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (Host == net) // w5500 作为Host的时候 使用Master和广播两个端口 HostAP 开启AP时非空 打开其内网端口
|
||||
{
|
||||
if (!NetMaster)
|
||||
{
|
||||
AddControl(*Host, tk->Uri(), 0); // 如果 Host 是 W5500 打开了就直接允许添加Master
|
||||
NetMaster = true;
|
||||
}
|
||||
if (!NetBra)
|
||||
{
|
||||
AddControl(*Host, uri, tk->Port);
|
||||
NetBra = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (HostAP && HostAP == esp) // 只使用esp的时候HostAp为空
|
||||
{
|
||||
if (!EspBra)
|
||||
{
|
||||
AddControl(*Host, uri, tk->Port);
|
||||
EspBra = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!Client->Opened)
|
||||
Client->Open();
|
||||
else
|
||||
Client->AttachControls();
|
||||
|
||||
// if(!esp && Host == esp && esp->Joined)AddControl(*Host, tk->Uri(), 0); // 如果 Host 是 ESP8266 则要求 JoinAP 完成才能添加主控制器
|
||||
// if(!net && Host == net)AddControl(*Host, tk->Uri(), 0);
|
||||
// AddControl(*Host, uri, tk->Port);
|
||||
//
|
||||
// Client->Open();
|
||||
}
|
||||
|
||||
// if(HostAP && esp)
|
||||
// {
|
||||
// auto ctrl = AddControl(*HostAP, uri, tk->Port);
|
||||
//
|
||||
// // 如果没有主机,这里打开令牌客户端,为组网做准备
|
||||
// if (!Host)
|
||||
// Client->Open();
|
||||
// else
|
||||
// Client->AttachControls();
|
||||
//
|
||||
// // 假如来迟了,客户端已经打开,那么自己挂载事件
|
||||
// if(Client->Opened && Client->Master)
|
||||
// {
|
||||
// ctrl->Received = Client->Master->Received;
|
||||
// ctrl->Open();
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
TokenController* AP0802::AddControl(NetworkInterface& host, const NetUri& uri, ushort localPort)
|
||||
{
|
||||
// 创建连接服务器的Socket
|
||||
auto socket = Socket::CreateRemote(uri);
|
||||
|
||||
// 创建连接服务器的控制器
|
||||
auto ctrl = new TokenController();
|
||||
//ctrl->Port = dynamic_cast<ITransport*>(socket);
|
||||
ctrl->Socket = socket;
|
||||
|
||||
// 创建客户端
|
||||
auto client = Client;
|
||||
if (localPort == 0)
|
||||
client->Master = ctrl;
|
||||
else
|
||||
{
|
||||
socket->Local.Port = localPort;
|
||||
ctrl->ShowRemote = true;
|
||||
client->Controls.Add(ctrl);
|
||||
}
|
||||
|
||||
return ctrl;
|
||||
}
|
||||
|
||||
/*
|
||||
网络使用流程:
|
||||
|
||||
|
|
|
@ -61,16 +61,8 @@ public:
|
|||
private:
|
||||
void* Data;
|
||||
int Size;
|
||||
// Control 打开情况标识
|
||||
bool NetMaster;
|
||||
bool NetBra;
|
||||
bool EspMaster;
|
||||
bool EspBra;
|
||||
Alarm* AlarmObj;
|
||||
// uint Flag; // 内部使用 标识Controller创建情况
|
||||
|
||||
void OpenClient(NetworkInterface& host);
|
||||
TokenController* AddControl(NetworkInterface& host, const NetUri& uri, ushort localPort);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -109,14 +109,12 @@ NetworkInterface* IOK026X::CreateNet()
|
|||
|
||||
host->Mode = NetworkType::STA_AP;
|
||||
}
|
||||
// 绑定委托,避免5500没有连接时导致没有启动客户端
|
||||
host->NetReady.Bind(&IOK026X::OpenClient, this);
|
||||
|
||||
Client->Register("SetWiFi", &Esp8266::SetWiFi, host);
|
||||
Client->Register("GetWiFi", &Esp8266::GetWiFi, host);
|
||||
Client->Register("GetAPs", &Esp8266::GetAPs, host);
|
||||
|
||||
host->OpenAsync();
|
||||
host->Open();
|
||||
|
||||
return host;
|
||||
}
|
||||
|
@ -174,59 +172,6 @@ void IOK026X::Register(int index, IDataPort& dp)
|
|||
ds.Register(index, dp);
|
||||
}
|
||||
|
||||
void IOK026X::OpenClient(NetworkInterface& host)
|
||||
{
|
||||
assert(Client, "Client");
|
||||
|
||||
//if(Client->Opened) return;
|
||||
|
||||
debug_printf("\r\n OpenClient \r\n");
|
||||
|
||||
auto esp = dynamic_cast<Esp8266*>(&host);
|
||||
if (esp && !esp->Led && LedsShow == 1) esp->SetLed(*Leds[0]);
|
||||
|
||||
auto tk = TokenConfig::Current;
|
||||
|
||||
// STA模式下,主连接服务器
|
||||
if (esp->IsStation() && esp->Joined && !Client->Master) AddControl(host, tk->Uri(), 0);
|
||||
|
||||
// STA或AP模式下,建立本地监听
|
||||
if (Client->Controls.Count() == 0)
|
||||
{
|
||||
NetUri uri(NetType::Udp, IPAddress::Broadcast(), 3355);
|
||||
AddControl(host, uri, tk->Port);
|
||||
}
|
||||
|
||||
if (!Client->Opened)
|
||||
Client->Open();
|
||||
else
|
||||
Client->AttachControls();
|
||||
}
|
||||
|
||||
TokenController* IOK026X::AddControl(NetworkInterface& host, const NetUri& uri, ushort localPort)
|
||||
{
|
||||
// 创建连接服务器的Socket
|
||||
auto socket = Socket::CreateRemote(uri);
|
||||
|
||||
// 创建连接服务器的控制器
|
||||
auto ctrl = new TokenController();
|
||||
//ctrl->Port = dynamic_cast<ITransport*>(socket);
|
||||
ctrl->Socket = socket;
|
||||
|
||||
// 创建客户端
|
||||
auto client = Client;
|
||||
if (localPort == 0)
|
||||
client->Master = ctrl;
|
||||
else
|
||||
{
|
||||
socket->Local.Port = localPort;
|
||||
ctrl->ShowRemote = true;
|
||||
client->Controls.Add(ctrl);
|
||||
}
|
||||
|
||||
return ctrl;
|
||||
}
|
||||
|
||||
void IOK026X::InitNet()
|
||||
{
|
||||
Host = CreateNet();
|
||||
|
@ -308,7 +253,7 @@ void IOK026X::FlushLed()
|
|||
if (LedsShow == 0) // 启动时候20秒
|
||||
{
|
||||
auto esp = dynamic_cast<Esp8266*>(Host);
|
||||
if (esp && esp->Joined) // 8266 初始化完成 且 连接完成
|
||||
if (esp && esp->Linked) // 8266 初始化完成 且 连接完成
|
||||
{
|
||||
Sys.SetTaskPeriod(LedsTaskId, 500); // 慢闪
|
||||
}
|
||||
|
|
|
@ -50,9 +50,6 @@ public:
|
|||
private:
|
||||
void* Data;
|
||||
int Size;
|
||||
|
||||
void OpenClient(NetworkInterface& host);
|
||||
TokenController* AddControl(NetworkInterface& host, const NetUri& uri, ushort localPort);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -107,14 +107,12 @@ NetworkInterface* IOK027X::Create8266(Pin power)
|
|||
|
||||
host->Mode = NetworkType::STA_AP;
|
||||
}
|
||||
// 绑定委托,避免5500没有连接时导致没有启动客户端
|
||||
host->NetReady.Bind(&IOK027X::OpenClient, this);
|
||||
|
||||
Client->Register("SetWiFi", &Esp8266::SetWiFi, host);
|
||||
Client->Register("GetWiFi", &Esp8266::GetWiFi, host);
|
||||
Client->Register("GetAPs", &Esp8266::GetAPs, host);
|
||||
|
||||
host->OpenAsync();
|
||||
host->Open();
|
||||
|
||||
return host;
|
||||
}
|
||||
|
@ -172,59 +170,6 @@ void IOK027X::Register(int index, IDataPort& dp)
|
|||
ds.Register(index, dp);
|
||||
}
|
||||
|
||||
void IOK027X::OpenClient(NetworkInterface& host)
|
||||
{
|
||||
assert(Client, "Client");
|
||||
|
||||
//if(Client->Opened) return;
|
||||
|
||||
debug_printf("\r\n OpenClient \r\n");
|
||||
|
||||
auto esp = dynamic_cast<Esp8266*>(&host);
|
||||
if (esp && !esp->Led && LedsShow == 1) esp->SetLed(*Leds[0]);
|
||||
|
||||
auto tk = TokenConfig::Current;
|
||||
|
||||
// STA模式下,主连接服务器
|
||||
if (esp->IsStation() && esp->Joined && !Client->Master) AddControl(host, tk->Uri(), 0);
|
||||
|
||||
// STA或AP模式下,建立本地监听
|
||||
if (Client->Controls.Count() == 0)
|
||||
{
|
||||
NetUri uri(NetType::Udp, IPAddress::Broadcast(), 3355);
|
||||
AddControl(host, uri, tk->Port);
|
||||
}
|
||||
|
||||
if (!Client->Opened)
|
||||
Client->Open();
|
||||
else
|
||||
Client->AttachControls();
|
||||
}
|
||||
|
||||
TokenController* IOK027X::AddControl(NetworkInterface& host, const NetUri& uri, ushort localPort)
|
||||
{
|
||||
// 创建连接服务器的Socket
|
||||
auto socket = Socket::CreateRemote(uri);
|
||||
|
||||
// 创建连接服务器的控制器
|
||||
auto ctrl = new TokenController();
|
||||
//ctrl->Port = dynamic_cast<ITransport*>(socket);
|
||||
ctrl->Socket = socket;
|
||||
|
||||
// 创建客户端
|
||||
auto client = Client;
|
||||
if (localPort == 0)
|
||||
client->Master = ctrl;
|
||||
else
|
||||
{
|
||||
socket->Local.Port = localPort;
|
||||
ctrl->ShowRemote = true;
|
||||
client->Controls.Add(ctrl);
|
||||
}
|
||||
|
||||
return ctrl;
|
||||
}
|
||||
|
||||
void IOK027X::InitNet(Pin power)
|
||||
{
|
||||
Host = Create8266(power);
|
||||
|
@ -325,7 +270,7 @@ void IOK027X::FlushLed()
|
|||
if (LedsShow == 0) // 启动时候20秒
|
||||
{
|
||||
auto esp = dynamic_cast<Esp8266*>(Host);
|
||||
if (esp && esp->Joined) // 8266 初始化完成 且 连接完成
|
||||
if (esp && esp->Linked) // 8266 初始化完成 且 连接完成
|
||||
{
|
||||
Sys.SetTaskPeriod(LedsTaskId, 500); // 慢闪
|
||||
}
|
||||
|
|
|
@ -52,9 +52,6 @@ private:
|
|||
void* Data;
|
||||
int Size;
|
||||
InputPort* RestPort;
|
||||
|
||||
void OpenClient(NetworkInterface& host);
|
||||
TokenController* AddControl(NetworkInterface& host, const NetUri& uri, ushort localPort);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -130,13 +130,11 @@ NetworkInterface* IOK0612::Create8266()
|
|||
|
||||
host->Mode = NetworkType::STA_AP;
|
||||
}
|
||||
// 绑定委托,避免5500没有连接时导致没有启动客户端
|
||||
host->NetReady.Bind(&IOK0612::OpenClient, this);
|
||||
|
||||
Client->Register("SetWiFi", &Esp8266::SetWiFi, host);
|
||||
Client->Register("GetWiFi", &Esp8266::GetWiFi, host);
|
||||
|
||||
host->OpenAsync();
|
||||
host->Open();
|
||||
|
||||
return host;
|
||||
}
|
||||
|
@ -200,59 +198,6 @@ void IOK0612::Register(int index, IDataPort& dp)
|
|||
ds.Register(index, dp);
|
||||
}
|
||||
|
||||
void IOK0612::OpenClient(NetworkInterface& host)
|
||||
{
|
||||
assert(Client, "Client");
|
||||
|
||||
//if(Client->Opened) return;
|
||||
|
||||
debug_printf("\r\n OpenClient \r\n");
|
||||
|
||||
auto esp = dynamic_cast<Esp8266*>(&host);
|
||||
if(esp && !esp->Led) esp->SetLed(*Leds[0]);
|
||||
|
||||
auto tk = TokenConfig::Current;
|
||||
|
||||
// STA模式下,主连接服务器
|
||||
if (esp->IsStation() && esp->Joined && !Client->Master) AddControl(host, tk->Uri(), 0);
|
||||
|
||||
// STA或AP模式下,建立本地监听
|
||||
if(Client->Controls.Count() == 0)
|
||||
{
|
||||
NetUri uri(NetType::Udp, IPAddress::Broadcast(), 3355);
|
||||
AddControl(host, uri, tk->Port);
|
||||
}
|
||||
|
||||
if (!Client->Opened)
|
||||
Client->Open();
|
||||
else
|
||||
Client->AttachControls();
|
||||
}
|
||||
|
||||
TokenController* IOK0612::AddControl(NetworkInterface& host, const NetUri& uri, ushort localPort)
|
||||
{
|
||||
// 创建连接服务器的Socket
|
||||
auto socket = Socket::CreateRemote(uri);
|
||||
|
||||
// 创建连接服务器的控制器
|
||||
auto ctrl = new TokenController();
|
||||
//ctrl->Port = dynamic_cast<ITransport*>(socket);
|
||||
ctrl->Socket = socket;
|
||||
|
||||
// 创建客户端
|
||||
auto client = Client;
|
||||
if(localPort == 0)
|
||||
client->Master = ctrl;
|
||||
else
|
||||
{
|
||||
socket->Local.Port = localPort;
|
||||
ctrl->ShowRemote = true;
|
||||
client->Controls.Add(ctrl);
|
||||
}
|
||||
|
||||
return ctrl;
|
||||
}
|
||||
|
||||
void IOK0612::InitNet()
|
||||
{
|
||||
Host = Create8266();
|
||||
|
|
|
@ -52,9 +52,6 @@ public:
|
|||
private:
|
||||
void* Data;
|
||||
int Size;
|
||||
|
||||
void OpenClient(NetworkInterface& host);
|
||||
TokenController* AddControl(NetworkInterface& host, const NetUri& uri, ushort localPort);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -130,13 +130,11 @@ NetworkInterface* NH3_0317::Create8266()
|
|||
|
||||
host->Mode = NetworkType::STA_AP;
|
||||
}
|
||||
// 绑定委托,避免5500没有连接时导致没有启动客户端
|
||||
host->NetReady.Bind(&NH3_0317::OpenClient, this);
|
||||
|
||||
Client->Register("SetWiFi", &Esp8266::SetWiFi, host);
|
||||
Client->Register("GetWiFi", &Esp8266::GetWiFi, host);
|
||||
|
||||
host->OpenAsync();
|
||||
host->Open();
|
||||
|
||||
return host;
|
||||
}
|
||||
|
@ -204,57 +202,6 @@ void NH3_0317::Register(int index, IDataPort& dp)
|
|||
ds.Register(index, dp);
|
||||
}
|
||||
|
||||
void NH3_0317::OpenClient(NetworkInterface& host)
|
||||
{
|
||||
assert(Client, "Client");
|
||||
|
||||
debug_printf("\r\n OpenClient \r\n");
|
||||
|
||||
auto esp = dynamic_cast<Esp8266*>(&host);
|
||||
if(esp && !esp->Led) esp->SetLed(*Leds[0]);
|
||||
|
||||
auto tk = TokenConfig::Current;
|
||||
|
||||
// STA模式下,主连接服务器
|
||||
if (esp->IsStation() && esp->Joined && !Client->Master) AddControl(host, tk->Uri(), 0);
|
||||
|
||||
// STA或AP模式下,建立本地监听
|
||||
if(Client->Controls.Count() == 0)
|
||||
{
|
||||
NetUri uri(NetType::Udp, IPAddress::Broadcast(), 3355);
|
||||
AddControl(host, uri, tk->Port);
|
||||
}
|
||||
|
||||
if (!Client->Opened)
|
||||
Client->Open();
|
||||
else
|
||||
Client->AttachControls();
|
||||
}
|
||||
|
||||
TokenController* NH3_0317::AddControl(NetworkInterface& host, const NetUri& uri, ushort localPort)
|
||||
{
|
||||
// 创建连接服务器的Socket
|
||||
auto socket = Socket::CreateRemote(uri);
|
||||
|
||||
// 创建连接服务器的控制器
|
||||
auto ctrl = new TokenController();
|
||||
//ctrl->Port = dynamic_cast<ITransport*>(socket);
|
||||
ctrl->Socket = socket;
|
||||
|
||||
// 创建客户端
|
||||
auto client = Client;
|
||||
if(localPort == 0)
|
||||
client->Master = ctrl;
|
||||
else
|
||||
{
|
||||
socket->Local.Port = localPort;
|
||||
ctrl->ShowRemote = true;
|
||||
client->Controls.Add(ctrl);
|
||||
}
|
||||
|
||||
return ctrl;
|
||||
}
|
||||
|
||||
void NH3_0317::InitNet()
|
||||
{
|
||||
Host = Create8266();
|
||||
|
|
|
@ -54,9 +54,6 @@ public:
|
|||
private:
|
||||
void* Data;
|
||||
int Size;
|
||||
|
||||
void OpenClient(NetworkInterface& host);
|
||||
TokenController* AddControl(NetworkInterface& host, const NetUri& uri, ushort localPort);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -91,7 +91,6 @@ NetworkInterface* PA0903::Create5500()
|
|||
debug_printf("\r\nW5500::Create \r\n");
|
||||
|
||||
auto host = new W5500(Spi1, PA8, PA0);
|
||||
host->NetReady.Bind(&PA0903::OpenClient, this);
|
||||
|
||||
return host;
|
||||
}
|
||||
|
@ -113,13 +112,10 @@ NetworkInterface* PA0903::Create8266(bool apOnly)
|
|||
host->WorkMode = NetworkType::STA_AP;
|
||||
}
|
||||
|
||||
// 绑定委托,避免5500没有连接时导致没有启动客户端
|
||||
host->NetReady.Bind(&PA0903::OpenClient, this);
|
||||
|
||||
//Sys.AddTask(SetWiFiTask, this, 0, -1, "SetWiFi");
|
||||
Client->Register("SetWiFi", &Esp8266::SetWiFi, host);
|
||||
Client->Register("GetWiFi", &Esp8266::GetWiFi, host);
|
||||
host->OpenAsync();
|
||||
host->Open();
|
||||
|
||||
return host;
|
||||
}
|
||||
|
@ -172,53 +168,6 @@ void PA0903::Register(int index, IDataPort& dp)
|
|||
ds.Register(index, dp);
|
||||
}
|
||||
|
||||
void PA0903::OpenClient(NetworkInterface& host)
|
||||
{
|
||||
assert(Client, "Client");
|
||||
|
||||
debug_printf("\r\n OpenClient \r\n");
|
||||
|
||||
// 网络就绪后,打开指示灯
|
||||
auto net = dynamic_cast<W5500*>(&host);
|
||||
if (net && !net->Led) net->SetLed(*Leds[0]);
|
||||
|
||||
auto tk = TokenConfig::Current;
|
||||
NetUri uri(NetType::Udp, IPAddress::Broadcast(), 3355);
|
||||
|
||||
// 避免重复打开 不判断也行 这里只有W5500
|
||||
if (!Client->Opened)
|
||||
{
|
||||
AddControl(*Host, tk->Uri(), 0);
|
||||
AddControl(*Host, uri, tk->Port);
|
||||
Client->Open();
|
||||
if (ProxyFac)ProxyFac->AutoStart();
|
||||
}
|
||||
}
|
||||
|
||||
TokenController* PA0903::AddControl(NetworkInterface& host, const NetUri& uri, ushort localPort)
|
||||
{
|
||||
// 创建连接服务器的Socket
|
||||
auto socket = Socket::CreateRemote(uri);
|
||||
|
||||
// 创建连接服务器的控制器
|
||||
auto ctrl = new TokenController();
|
||||
//ctrl->Port = dynamic_cast<ITransport*>(socket);
|
||||
ctrl->Socket = socket;
|
||||
|
||||
// 创建客户端
|
||||
auto client = Client;
|
||||
if (localPort == 0)
|
||||
client->Master = ctrl;
|
||||
else
|
||||
{
|
||||
socket->Local.Port = localPort;
|
||||
ctrl->ShowRemote = true;
|
||||
client->Controls.Add(ctrl);
|
||||
}
|
||||
|
||||
return ctrl;
|
||||
}
|
||||
|
||||
void OnInitNet(void* param)
|
||||
{
|
||||
auto& bsp = *(PA0903*)param;
|
||||
|
|
|
@ -62,9 +62,6 @@ public:
|
|||
private:
|
||||
void* Data;
|
||||
int Size;
|
||||
|
||||
void OpenClient(NetworkInterface& host);
|
||||
TokenController* AddControl(NetworkInterface& host, const NetUri& uri, ushort localPort);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -61,11 +61,14 @@ Esp8266::~Esp8266()
|
|||
|
||||
void Esp8266::Init(ITransport* port, Pin power, Pin rst)
|
||||
{
|
||||
Name = "Esp8266";
|
||||
Speed = 54;
|
||||
|
||||
Port = port;
|
||||
if(Port)
|
||||
{
|
||||
//MinSize = Port->MinSize;
|
||||
MaxSize = Port->MaxSize;
|
||||
if(MinSize) MinSize = Port->MinSize;
|
||||
if(MaxSize) MaxSize = Port->MaxSize;
|
||||
|
||||
Port->Register(OnPortReceive, this);
|
||||
}
|
||||
|
@ -74,10 +77,6 @@ void Esp8266::Init(ITransport* port, Pin power, Pin rst)
|
|||
if (rst != P0) _rst.Init(rst, true);
|
||||
|
||||
_task = 0;
|
||||
_task2 = 0;
|
||||
|
||||
AutoConn = false;
|
||||
Joined = false;
|
||||
|
||||
Led = nullptr;
|
||||
|
||||
|
@ -115,37 +114,7 @@ void Esp8266::RemoveLed()
|
|||
Led = nullptr;
|
||||
}
|
||||
|
||||
void LoopOpenTask(void* param)
|
||||
{
|
||||
auto& esp = *(Esp8266*)param;
|
||||
// 如果8266没有被打开,并且没有处于正在打开的状态,那么再次打开8266
|
||||
if (!esp.Opened && !esp.Opening)
|
||||
esp.Open();
|
||||
}
|
||||
|
||||
void LoopJoinTask(void* param)
|
||||
{
|
||||
auto& esp = *(Esp8266*)param;
|
||||
if (esp.Opened && !esp.Joined)
|
||||
{
|
||||
// 如果已打开,则尝试连WiFi
|
||||
esp.TryJoinAP();
|
||||
}
|
||||
}
|
||||
|
||||
void Esp8266::OpenAsync()
|
||||
{
|
||||
if (Opened || Opening) return;
|
||||
|
||||
// 异步打开任务,一般执行时间6~10秒,分离出来避免拉高8266数据处理任务的平均值
|
||||
Sys.AddTask(LoopOpenTask, this, 0, -1, "Open8266");
|
||||
//if(!_task) _task = Sys.AddTask(LoopTask, this, 0, -1, "Open8266");
|
||||
|
||||
// 马上调度一次
|
||||
//Sys.SetTask(_task, true, 0);
|
||||
}
|
||||
|
||||
bool Esp8266::Open()
|
||||
bool Esp8266::OnOpen()
|
||||
{
|
||||
if (!Port->Open()) return false;
|
||||
|
||||
|
@ -186,21 +155,8 @@ bool Esp8266::Open()
|
|||
|
||||
SetDHCP(mode, true);
|
||||
|
||||
bool join = SSID && *SSID;
|
||||
// 等待WiFi自动连接
|
||||
if (!AutoConn || !WaitForCmd("WIFI CONNECTED", 3000))
|
||||
{
|
||||
if (!join || mode == NetworkType::STA_AP) OpenAP();
|
||||
if (join)
|
||||
{
|
||||
if (!_task2) _task2 = Sys.AddTask(LoopJoinTask, this, 0, 30 * 1000, "JoinAP");
|
||||
}
|
||||
}
|
||||
|
||||
if (!_task) _task = Sys.AddTask(&Esp8266::Process, this, -1, -1, "Esp8266");
|
||||
|
||||
NetReady(*this);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -264,38 +220,9 @@ void Esp8266::OpenAP()
|
|||
#endif
|
||||
}
|
||||
|
||||
void Esp8266::TryJoinAP()
|
||||
{
|
||||
if (JoinAP(*SSID, *Pass))
|
||||
{
|
||||
// 拿到IP,网络就绪
|
||||
IP = GetIP(true);
|
||||
|
||||
SaveConfig();
|
||||
ShowConfig();
|
||||
|
||||
Joined = true;
|
||||
|
||||
NetReady(*this);
|
||||
// 停止尝试
|
||||
Sys.RemoveTask(_task2);
|
||||
}
|
||||
else if (Mode != NetworkType::STA_AP)
|
||||
{
|
||||
// 尝试若干次以后还是连接失败,则重启模块
|
||||
auto tsk = Task::Get(_task2);
|
||||
if (tsk->Times > 0 && tsk->Times % 4 == 0)
|
||||
{
|
||||
Close();
|
||||
OpenAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Esp8266::Close()
|
||||
void Esp8266::OnClose()
|
||||
{
|
||||
if (_task) Sys.RemoveTask(_task);
|
||||
if (_task2) Sys.RemoveTask(_task2);
|
||||
|
||||
_power.Close();
|
||||
_rst.Close();
|
||||
|
@ -303,6 +230,30 @@ void Esp8266::Close()
|
|||
Port->Close();
|
||||
}
|
||||
|
||||
bool Esp8266::OnLink()
|
||||
{
|
||||
bool join = SSID && *SSID;
|
||||
// 等待WiFi自动连接
|
||||
if (!WaitForCmd("WIFI CONNECTED", 3000))
|
||||
{
|
||||
auto mode = WorkMode;
|
||||
// 默认Both
|
||||
if (mode == NetworkType::Wire) mode = NetworkType::STA_AP;
|
||||
if (!join || mode == NetworkType::STA_AP) OpenAP();
|
||||
if (join)
|
||||
{
|
||||
if (!JoinAP(*SSID, *Pass)) return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Esp8266::CheckLink()
|
||||
{
|
||||
return Linked;
|
||||
}
|
||||
|
||||
// 配置网络参数
|
||||
void Esp8266::Config()
|
||||
{
|
||||
|
@ -317,7 +268,7 @@ void Esp8266::Config()
|
|||
mac[5]++;
|
||||
SetMAC(false, mac);
|
||||
|
||||
SetAutoConn(AutoConn);
|
||||
//SetAutoConn(AutoConn);
|
||||
}
|
||||
|
||||
Socket* Esp8266::CreateSocket(NetType type)
|
||||
|
@ -1085,8 +1036,6 @@ bool Esp8266::SetWiFi(const Pair& args, Stream& result)
|
|||
SaveConfig();
|
||||
}
|
||||
|
||||
// 马上要准备重启了 不需要再JoinAp了 避免堵塞SetWifi的消息回复。
|
||||
if (_task2)Sys.SetTask(_task2, false);
|
||||
// Sleep跳出去处理其他的
|
||||
// 让JoinAp相关的东西数据先处理掉 然后再去回复 较少处理数据冲突的情况。
|
||||
Sys.Sleep(150);
|
||||
|
@ -1094,7 +1043,7 @@ bool Esp8266::SetWiFi(const Pair& args, Stream& result)
|
|||
// 返回结果
|
||||
result.Write((byte)true);
|
||||
// 延迟重启
|
||||
if (haveChang)Sys.Reboot(5000);
|
||||
if (haveChang) Sys.Reboot(5000);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -19,16 +19,11 @@ private:
|
|||
void* _param;
|
||||
|
||||
public:
|
||||
bool Opening; // 是否正在打开
|
||||
bool Opened; // 是否打开
|
||||
|
||||
ushort MinSize; // 数据包最小大小
|
||||
ushort MaxSize; // 数据包最大大小
|
||||
|
||||
ITransport* Port; // 传输口
|
||||
|
||||
bool AutoConn; // 是否自动连接WiFi,默认false
|
||||
bool Joined; // 是否已连接热点
|
||||
NetworkType WorkMode; // 工作模式
|
||||
|
||||
IDataPort* Led; // 指示灯
|
||||
|
@ -39,10 +34,6 @@ public:
|
|||
|
||||
void Init(ITransport* port, Pin power = P0, Pin rst = P0);
|
||||
|
||||
void OpenAsync();
|
||||
void TryJoinAP();
|
||||
virtual bool Open();
|
||||
virtual void Close();
|
||||
virtual void Config();
|
||||
void SetLed(Pin led);
|
||||
void SetLed(OutputPort& led);
|
||||
|
@ -123,10 +114,16 @@ private:
|
|||
OutputPort _rst;
|
||||
|
||||
uint _task; // 调度任务
|
||||
uint _task2; // 连接热点任务
|
||||
ByteArray _Buffer; // 待处理数据包
|
||||
IPEndPoint _Remote; // 当前数据包远程地址
|
||||
|
||||
// 打开与关闭
|
||||
virtual bool OnOpen();
|
||||
virtual void OnClose();
|
||||
// 检测连接
|
||||
virtual bool OnLink();
|
||||
virtual bool CheckLink();
|
||||
|
||||
bool CheckReady();
|
||||
void OpenAP();
|
||||
|
||||
|
|
|
@ -326,14 +326,15 @@ W5500::~W5500()
|
|||
{
|
||||
Close();
|
||||
|
||||
Sys.RemoveTask(TaskID);
|
||||
|
||||
delete _spi;
|
||||
}
|
||||
|
||||
// 初始化
|
||||
void W5500::Init()
|
||||
{
|
||||
Name = "W5500";
|
||||
Speed = 100;
|
||||
|
||||
_spi = nullptr;
|
||||
Led = nullptr;
|
||||
_Dns = nullptr;
|
||||
|
@ -345,7 +346,6 @@ void W5500::Init()
|
|||
RetryCount = 8;
|
||||
LowLevelTime= 0;
|
||||
PingACK = true;
|
||||
Opened = false;
|
||||
TaskID = 0;
|
||||
|
||||
InitConfig();
|
||||
|
@ -356,7 +356,7 @@ void W5500::Init(Spi* spi, Pin irq, Pin rst)
|
|||
{
|
||||
assert(spi, "spi");
|
||||
|
||||
debug_printf("\r\n");
|
||||
//debug_printf("\r\n");
|
||||
|
||||
if(rst != P0) Rst.Init(rst, true);
|
||||
if(irq != P0)
|
||||
|
@ -395,15 +395,14 @@ void W5500::SetLed(OutputPort& led)
|
|||
Led = fp;
|
||||
}
|
||||
|
||||
bool W5500::Open()
|
||||
bool W5500::OnOpen()
|
||||
{
|
||||
if(Opened) return true;
|
||||
|
||||
net_printf("\r\nW5500::Open\r\n");
|
||||
net_printf("W5500::Open\r\n");
|
||||
//ShowInfo();
|
||||
|
||||
net_printf("\tRst: ");
|
||||
if(!Rst.Open()) return false;
|
||||
|
||||
//net_printf("硬件复位 \r\n");
|
||||
Rst = true; // 低电平有效
|
||||
Sys.Delay(600); // 最少500us
|
||||
|
@ -432,12 +431,34 @@ bool W5500::Open()
|
|||
}
|
||||
net_printf("硬件版本: %02X\r\n", ver);
|
||||
|
||||
#if NET_DEBUG
|
||||
//StateShow();
|
||||
//PhyStateShow();
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void W5500::OnClose()
|
||||
{
|
||||
net_printf("W5500::Close \r\n");
|
||||
|
||||
Sys.RemoveTask(TaskID);
|
||||
|
||||
_spi->Close();
|
||||
|
||||
Irq.Close();
|
||||
Rst.Close();
|
||||
}
|
||||
|
||||
bool W5500::OnLink()
|
||||
{
|
||||
debug_printf("等待PHY连接 ");
|
||||
|
||||
T_PHYCFGR phy;
|
||||
phy.Init(0x00); // 先清空
|
||||
|
||||
tw.Reset(5000);
|
||||
TimeWheel tw(5000);
|
||||
int temp = 0;
|
||||
while(!tw.Expired() && !phy.LNK)
|
||||
{
|
||||
|
@ -449,7 +470,6 @@ bool W5500::Open()
|
|||
debug_printf(".");
|
||||
}
|
||||
}
|
||||
//debug_printf("\r\n");
|
||||
|
||||
if(phy.LNK)
|
||||
{
|
||||
|
@ -466,8 +486,8 @@ bool W5500::Open()
|
|||
else
|
||||
{
|
||||
net_printf("PHY连接异常\r\n");
|
||||
OnClose();
|
||||
debug_printf("W5500::Open 打开失败!请检查网线!\r\n");
|
||||
//OnClose();
|
||||
debug_printf("W5500::Link 连接失败!请检查网线!\r\n");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -481,32 +501,18 @@ bool W5500::Open()
|
|||
WriteByte(offsetof(TSocket, RXBUF_SIZE), 0x02, i+1); //Socket Tx mempry size=2k
|
||||
}
|
||||
|
||||
//if(!TaskID) TaskID = Sys.AddTask(IRQTask, this, -1, -1, "W5500中断");
|
||||
// 为解决芯片有时候无法接收数据的问题,需要守护任务辅助
|
||||
if(!TaskID)
|
||||
{
|
||||
int time = Irq.Empty() || !Irq.HardEvent ? 10 : 500;
|
||||
TaskID = Sys.AddTask(IRQTask, this, time, time, "W5500中断");
|
||||
int time = (Irq.Empty() || !Irq.HardEvent) ? 10 : 500;
|
||||
TaskID = Sys.AddTask([](void* p){ ((W5500*)p)->OnIRQ(); }, this, time, time, "W5500中断");
|
||||
auto task = Task::Get(TaskID);
|
||||
task->MaxDeepth = 2; // 以太网允许重入,因为有时候在接收里面等待下一次接收
|
||||
}
|
||||
|
||||
Opened = true;
|
||||
|
||||
#if NET_DEBUG
|
||||
//StateShow();
|
||||
//PhyStateShow();
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void W5500::IRQTask(void* param)
|
||||
{
|
||||
W5500* net = (W5500*)param;
|
||||
net->OnIRQ();
|
||||
}
|
||||
|
||||
void W5500::Config()
|
||||
{
|
||||
ShowConfig();
|
||||
|
@ -550,30 +556,12 @@ void W5500::Config()
|
|||
WriteFrame(0, bs);
|
||||
}
|
||||
|
||||
void W5500::Close()
|
||||
{
|
||||
if(!Opened) return;
|
||||
|
||||
net_printf("W5500::Close \r\n");
|
||||
OnClose();
|
||||
|
||||
Opened = false;
|
||||
}
|
||||
|
||||
void W5500::ChangePower(int level)
|
||||
{
|
||||
// 进入低功耗时,直接关闭
|
||||
if(level) Close();
|
||||
}
|
||||
|
||||
void W5500::OnClose()
|
||||
{
|
||||
_spi->Close();
|
||||
|
||||
Irq.Close();
|
||||
Rst.Close();
|
||||
}
|
||||
|
||||
// 复位
|
||||
void W5500::Reset()
|
||||
{
|
||||
|
@ -783,10 +771,6 @@ void W5500::OnIRQ(InputPort& port, bool down)
|
|||
{
|
||||
if(!down) return; // 低电平中断
|
||||
|
||||
//auto net = (W5500*)param;
|
||||
//net->OnIRQ();
|
||||
//net_printf("OnIRQ \r\n");
|
||||
//Sys.SetTask(net->TaskID, true, 0);
|
||||
Sys.SetTask(TaskID, true, 0);
|
||||
}
|
||||
|
||||
|
@ -888,14 +872,14 @@ bool W5500::EnableDNS()
|
|||
return true;
|
||||
}
|
||||
|
||||
static void OnDhcpStop(W5500& net, Dhcp& dhcp)
|
||||
/*static void OnDhcpStop(W5500& net, Dhcp& dhcp)
|
||||
{
|
||||
// DHCP成功,或者失败且超过最大错误次数,都要启动网关,让它以上一次配置工作
|
||||
if(dhcp.Result || dhcp.Times >= dhcp.MaxTimes)
|
||||
{
|
||||
net.NetReady(net);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
// 启用DHCP
|
||||
bool W5500::EnableDHCP()
|
||||
|
@ -904,7 +888,7 @@ bool W5500::EnableDHCP()
|
|||
|
||||
// 打开DHCP
|
||||
auto dhcp = new Dhcp(*this);
|
||||
dhcp->OnStop.Bind(OnDhcpStop, this);
|
||||
//dhcp->OnStop.Bind(OnDhcpStop, this);
|
||||
dhcp->Start();
|
||||
|
||||
_Dhcp = dhcp;
|
||||
|
|
|
@ -13,7 +13,6 @@ public:
|
|||
byte RetryCount;
|
||||
bool PingACK;
|
||||
|
||||
bool Opened; // 是否已经打开
|
||||
uint TaskID;
|
||||
|
||||
IDataPort* Led; // 指示灯
|
||||
|
@ -30,8 +29,6 @@ public:
|
|||
void SetLed(Pin led);
|
||||
void SetLed(OutputPort& led);
|
||||
|
||||
virtual bool Open();
|
||||
virtual void Close();
|
||||
virtual void Config();
|
||||
|
||||
// 读写帧,帧本身由外部构造 (包括帧数据内部的读写标志)
|
||||
|
@ -47,7 +44,7 @@ public:
|
|||
// 网卡状态输出
|
||||
void StateShow();
|
||||
// 测试PHY状态 返回是否连接网线
|
||||
bool CheckLink();
|
||||
virtual bool CheckLink();
|
||||
// 输出物理链路层状态
|
||||
void PhyStateShow();
|
||||
|
||||
|
@ -87,11 +84,15 @@ private:
|
|||
ushort ReadByte2(ushort addr, byte socket = 0 ,byte block = 0);
|
||||
|
||||
void SetAddress(ushort addr, byte rw, byte socket = 0 ,byte block = 0);
|
||||
void OnClose();
|
||||
|
||||
// 打开与关闭
|
||||
virtual bool OnOpen();
|
||||
virtual void OnClose();
|
||||
// 检测连接
|
||||
virtual bool OnLink();
|
||||
|
||||
// 中断脚回调
|
||||
void OnIRQ(InputPort& port, bool down);
|
||||
static void IRQTask(void* param);
|
||||
void OnIRQ();
|
||||
};
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#include "NetworkInterface.h"
|
||||
#include "NetworkInterface.h"
|
||||
#include "Config.h"
|
||||
|
||||
#define NET_DEBUG DEBUG
|
||||
|
@ -31,17 +31,79 @@ struct NetConfig
|
|||
|
||||
NetworkInterface::NetworkInterface()
|
||||
{
|
||||
Mode = NetworkType::Wire;
|
||||
Status = NetworkStatus::Unknown;
|
||||
Name = "Network";
|
||||
Speed = 0;
|
||||
Opened = false;
|
||||
Linked = false;
|
||||
|
||||
Mode = NetworkType::Wire;
|
||||
Status = NetworkStatus::None;
|
||||
|
||||
_taskLink = 0;
|
||||
|
||||
debug_printf("Network::Add 0x%p\r\n", this);
|
||||
All.Add(this);
|
||||
}
|
||||
|
||||
NetworkInterface::~NetworkInterface()
|
||||
{
|
||||
debug_printf("Network::Remove 0x%p\r\n", this);
|
||||
All.Remove(this);
|
||||
|
||||
if(!Opened) Close();
|
||||
}
|
||||
|
||||
bool NetworkInterface::Open()
|
||||
{
|
||||
if(Opened) return true;
|
||||
|
||||
// 打开接口
|
||||
Opened = OnOpen();
|
||||
|
||||
// 建立连接任务
|
||||
_taskLink = Sys.AddTask([](void* s){ ((NetworkInterface*)s)->OnLoop(); }, this, 0, 1000, "网络检测");
|
||||
|
||||
return Opened;
|
||||
}
|
||||
|
||||
void NetworkInterface::Close()
|
||||
{
|
||||
if(!Opened) return;
|
||||
|
||||
Sys.RemoveTask(_taskLink);
|
||||
|
||||
OnClose();
|
||||
|
||||
Opened = false;
|
||||
}
|
||||
|
||||
void NetworkInterface::OnLoop()
|
||||
{
|
||||
// 检测并通知状态改变
|
||||
bool link = CheckLink();
|
||||
if(link ^ Linked)
|
||||
{
|
||||
debug_printf("%s::Change %s => %s \r\n", Name, Linked ? "连接" : "断开", link ? "连接" : "断开");
|
||||
Linked = link;
|
||||
|
||||
Changed(*this);
|
||||
link = Linked;
|
||||
}
|
||||
|
||||
// 未连接时执行连接
|
||||
if(!link)
|
||||
{
|
||||
link = OnLink();
|
||||
if(link ^ Linked)
|
||||
{
|
||||
debug_printf("%s::Change %s => %s \r\n", Name, Linked ? "连接" : "断开", link ? "连接" : "断开");
|
||||
Linked = link;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//bool NetworkInterface::OnLink() { return true; }
|
||||
|
||||
void NetworkInterface::InitConfig()
|
||||
{
|
||||
IPAddress defip(192, 168, 1, 1);
|
||||
|
|
|
@ -24,7 +24,7 @@ enum class NetworkType
|
|||
// 网络状态
|
||||
enum class NetworkStatus
|
||||
{
|
||||
Unknown = 0, // 未知
|
||||
None = 0, // 未知
|
||||
Up = 1, // 启用
|
||||
Down = 2, // 禁用
|
||||
};
|
||||
|
@ -33,31 +33,30 @@ enum class NetworkStatus
|
|||
class NetworkInterface
|
||||
{
|
||||
public:
|
||||
byte Index; // 索引
|
||||
bool Active; // 可用
|
||||
ushort Speed; // 速度Mbps。决定优先级
|
||||
cstring Name; // 名称
|
||||
ushort Speed; // 速度Mbps。决定优先级
|
||||
bool Opened; // 接口已上电打开,检测到网卡存在
|
||||
bool Linked; // 接口网络就绪,可以通信
|
||||
NetworkType Mode; // 无线模式。0不是无线,1是STA,2是AP,3是混合
|
||||
NetworkStatus Status; // 网络状态
|
||||
|
||||
IPAddress IP; // 本地IP地址
|
||||
IPAddress Mask; // 子网掩码
|
||||
MacAddress Mac; // 本地Mac地址
|
||||
NetworkType Mode; // 无线模式。0不是无线,1是STA,2是AP,3是混合
|
||||
NetworkStatus Status; // 网络状态
|
||||
|
||||
IPAddress Gateway;
|
||||
IPAddress DNSServer;
|
||||
IPAddress DNSServer2;
|
||||
|
||||
Delegate<NetworkInterface&> Changed; // 网络改变
|
||||
Delegate<NetworkInterface&> NetReady; // 网络准备就绪。带this参数
|
||||
|
||||
NetworkInterface();
|
||||
// 加上虚析构函数,因为应用层可能要释放该接口
|
||||
virtual ~NetworkInterface();
|
||||
|
||||
// 打开与关闭
|
||||
virtual bool Open() = 0;
|
||||
virtual void Close()= 0;
|
||||
bool Open();
|
||||
void Close();
|
||||
|
||||
// 应用配置
|
||||
virtual void Config() = 0;
|
||||
|
@ -77,6 +76,19 @@ public:
|
|||
// 启用DHCP
|
||||
virtual bool EnableDHCP() { return false; }
|
||||
|
||||
protected:
|
||||
uint _taskLink;
|
||||
|
||||
// 打开与关闭
|
||||
virtual bool OnOpen() = 0;
|
||||
virtual void OnClose() = 0;
|
||||
|
||||
// 循环检测连接
|
||||
virtual void OnLoop();
|
||||
virtual bool OnLink() { return true; }
|
||||
virtual bool CheckLink() { return false; }
|
||||
|
||||
public:
|
||||
// 全局静态
|
||||
static List<NetworkInterface*> All;
|
||||
};
|
||||
|
@ -95,7 +107,6 @@ public:
|
|||
|
||||
bool IsStation() const;
|
||||
bool IsAP() const;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#include "Socket.h"
|
||||
#include "Socket.h"
|
||||
#include "Config.h"
|
||||
|
||||
#include "NetworkInterface.h"
|
||||
|
@ -17,7 +17,7 @@ Socket* Socket::CreateClient(const NetUri& uri)
|
|||
for(int i=0; i < list.Count(); i++)
|
||||
{
|
||||
auto ni = list[i];
|
||||
if(ni->Active)
|
||||
if(ni->Linked)
|
||||
{
|
||||
auto socket = ni->CreateSocket(uri.Type);
|
||||
if(socket)
|
||||
|
@ -39,7 +39,7 @@ Socket* Socket::CreateRemote(const NetUri& uri)
|
|||
for(int i=0; i < list.Count(); i++)
|
||||
{
|
||||
auto ni = list[i];
|
||||
if(ni && ni->Active)
|
||||
if(ni && ni->Linked)
|
||||
{
|
||||
auto socket = ni->CreateSocket(uri.Type);
|
||||
if(socket)
|
||||
|
|
|
@ -199,7 +199,7 @@ void TinyIP::Work(void* param)
|
|||
}
|
||||
}
|
||||
|
||||
bool TinyIP::Open()
|
||||
bool TinyIP::OnOpen()
|
||||
{
|
||||
debug_printf("\r\nTinyIP::Open...\r\n");
|
||||
|
||||
|
@ -229,6 +229,13 @@ bool TinyIP::Open()
|
|||
return true;
|
||||
}
|
||||
|
||||
void TinyIP::OnClose()
|
||||
{
|
||||
delete Arp;
|
||||
|
||||
_port->Close();
|
||||
}
|
||||
|
||||
void TinyIP::Config()
|
||||
{
|
||||
ShowConfig();
|
||||
|
|
|
@ -59,8 +59,6 @@ public:
|
|||
virtual ~TinyIP();
|
||||
void Init(ITransport* port);
|
||||
|
||||
virtual bool Open();
|
||||
virtual void Close();
|
||||
virtual void Config();
|
||||
ushort CheckSum(IPAddress* remote, const byte* buf, uint len, byte type);
|
||||
|
||||
|
@ -69,6 +67,11 @@ public:
|
|||
bool IsBroadcast(const IPAddress& ip); // 是否广播地址
|
||||
|
||||
virtual Socket* CreateSocket(NetType type);
|
||||
|
||||
private:
|
||||
// 打开与关闭
|
||||
virtual bool OnOpen();
|
||||
virtual void OnClose();
|
||||
};
|
||||
|
||||
/*
|
||||
|
|
Loading…
Reference in New Issue