扩展网络接口配置,支持保存WiFi密码

This commit is contained in:
大石头X2 2017-02-08 20:45:45 +08:00
parent fda1e688f6
commit 02f1014fff
6 changed files with 82 additions and 34 deletions

View File

@ -130,7 +130,7 @@ NetworkInterface* AP0801::Create5500()
debug_printf("\r\nW5500::Create \r\n");
auto net = new W5500(Spi2, PE1, PD13);
// 软路由的DHCP要求很严格必须先把自己IP设为0
// 必须先把自己IP设为0否则本地IP段与路由器不一致时将得不到分配
//net->IP = IPAddress::Any();
if(!net->Open())

View File

@ -46,11 +46,6 @@ Esp8266::Esp8266(COM idx, Pin power, Pin rst)
srp->MaxSize = 512;
Init(srp, power, rst);
InitConfig();
LoadConfig();
// 配置模式作为工作模式
WorkMode = Mode;
}
Esp8266::~Esp8266()
@ -85,10 +80,13 @@ void Esp8266::Init(ITransport* port, Pin power, Pin rst)
Buffer(_sockets, 5 * 4).Clear();
Mode = NetworkType::STA_AP;
WorkMode = NetworkType::STA_AP;
WorkMode= NetworkType::STA_AP;
SSID = new String();
Pass = new String();
InitConfig();
LoadConfig();
// 配置模式作为工作模式
WorkMode = Mode;
}
void Esp8266::SetLed(Pin led)
@ -243,6 +241,9 @@ bool Esp8266::OnLink(uint retry)
if (join)
{
if (!JoinAP(*SSID, *Pass)) return false;
ShowConfig();
SaveConfig();
}
}

View File

@ -318,7 +318,6 @@ W5500::W5500(SPI spi, Pin irq, Pin rst)
auto spi_ = new Spi(spi, 36000000);
Init();
LoadConfig();
Init(spi_, irq, rst);
}
@ -347,8 +346,6 @@ void W5500::Init()
LowLevelTime= 0;
PingACK = true;
TaskID = 0;
InitConfig();
}
// 初始化
@ -376,6 +373,9 @@ void W5500::Init(Spi* spi, Pin irq, Pin rst)
}
_spi = spi;
InitConfig();
LoadConfig();
}
void W5500::SetLed(Pin led)

View File

@ -25,8 +25,9 @@ struct NetConfig
uint DNSServer2;
uint Gateway;
char SSID[32];
char Pass[32];
/*char SSID[32];
char Pass[32];*/
char Data[4 * (32 - 7)];
};
NetworkInterface::NetworkInterface()
@ -135,8 +136,7 @@ void NetworkInterface::InitConfig()
Mode = NetworkType::Wire;
//if(SSID) SSID->Clear();
//if(Pass) Pass->Clear();
OnInitConfig();
}
bool NetworkInterface::LoadConfig()
@ -145,7 +145,9 @@ bool NetworkInterface::LoadConfig()
NetConfig nc;
Buffer bs(&nc, sizeof(nc));
if(!Config::Current->Get("NET", bs)) return false;
if(!Config::Current->Get(Name, bs)) return false;
debug_printf("%s::Load %d 字节\r\n", Name, sizeof(nc));
IP = nc.IP;
Mask = nc.Mask;
@ -156,13 +158,12 @@ bool NetworkInterface::LoadConfig()
DNSServer2 = nc.DNSServer2;
Gateway = nc.Gateway;
//if(SSID) *SSID = nc.SSID;
//if(Pass) *Pass = nc.Pass;
OnLoadConfig(Buffer(nc.Data, sizeof(nc.Data)));
return true;
}
bool NetworkInterface::SaveConfig()
bool NetworkInterface::SaveConfig() const
{
if(!Config::Current) return false;
@ -176,18 +177,17 @@ bool NetworkInterface::SaveConfig()
nc.DNSServer2 = DNSServer2.Value;
nc.Gateway = Gateway.Value;
/*if(SSID) SSID->CopyTo(0, nc.SSID, ArrayLength(nc.SSID) - 1);
if (Pass)
{
Pass->CopyTo(0, nc.Pass, ArrayLength(nc.Pass) - 1);
if (Pass->Length() == 0)nc.Pass[0] = 0x00; // 如果密码为空 写一个字节 弥补String Copy的问题
}*/
Buffer dat(nc.Data, sizeof(nc.Data));
dat.Clear();
OnSaveConfig(dat);
debug_printf("%s::Save %d 字节\r\n", Name, sizeof(nc));
Buffer bs(&nc, sizeof(nc));
return Config::Current->Set("NET", bs);
return Config::Current->Set(Name, bs);
}
void NetworkInterface::ShowConfig()
void NetworkInterface::ShowConfig() const
{
#if NET_DEBUG
net_printf(" MAC:\t");
@ -207,6 +207,7 @@ void NetworkInterface::ShowConfig()
net_printf("\r\n DNS2:\t");
DNSServer2.Show();
}
net_printf("\r\n Speed:\t%dMps", Speed);
net_printf("\r\n Mode:\t");
switch(Mode)
{
@ -225,8 +226,7 @@ void NetworkInterface::ShowConfig()
}
//net_printf("\r\n");
//if(SSID) { net_printf("\r\n SSID:\t"); SSID->Show(false); }
//if(Pass) { net_printf("\r\n Pass:\t"); Pass->Show(false); }
OnShowConfig();
net_printf("\r\n");
#endif
@ -257,3 +257,36 @@ bool WiFiInterface::IsAP() const
{
return Mode == NetworkType::AP || Mode == NetworkType::STA_AP;
}
void WiFiInterface::OnInitConfig()
{
if(!SSID)
SSID = new String();
else
SSID->Clear();
if(!Pass)
Pass = new String();
else
Pass->Clear();
}
void WiFiInterface::OnLoadConfig(const Buffer& buf)
{
Stream ms(buf);
if(SSID) *SSID = ms.ReadString();
if(Pass) *Pass = ms.ReadString();
}
void WiFiInterface::OnSaveConfig(Buffer& buf) const
{
Stream ms(buf);
if(SSID) ms.WriteArray(*SSID);
if(Pass) ms.WriteArray(*Pass);
}
void WiFiInterface::OnShowConfig() const
{
if(SSID) { net_printf("\r\n SSID:\t"); SSID->Show(false); }
if(Pass) { net_printf("\r\n Pass:\t"); Pass->Show(false); }
}

View File

@ -61,11 +61,11 @@ public:
// 应用配置
virtual void Config() = 0;
// 保存和加载动态获取的网络配置到存储设备
// 保存和加载网络配置
void InitConfig();
bool LoadConfig();
bool SaveConfig();
void ShowConfig();
bool SaveConfig() const;
void ShowConfig() const;
virtual Socket* CreateSocket(NetType type) = 0;
@ -89,6 +89,12 @@ protected:
virtual bool OnLink(uint retry) { return true; }
virtual bool CheckLink() { return Linked; }
// 保存和加载网络配置
virtual void OnInitConfig() {}
virtual void OnLoadConfig(const Buffer& buf) {}
virtual void OnSaveConfig(Buffer& buf) const {}
virtual void OnShowConfig() const {}
public:
// 全局静态
static List<NetworkInterface*> All;
@ -108,6 +114,13 @@ public:
bool IsStation() const;
bool IsAP() const;
protected:
// 保存和加载网络配置
virtual void OnInitConfig();
virtual void OnLoadConfig(const Buffer& buf);
virtual void OnSaveConfig(Buffer& buf) const;
virtual void OnShowConfig() const;
};
#endif

View File

@ -46,6 +46,7 @@ void TinyIP::Init(ITransport* port)
_StartTime = Sys.Ms();
InitConfig();
LoadConfig();
}
// 循环调度的任务
@ -238,7 +239,7 @@ void TinyIP::OnClose()
void TinyIP::Config()
{
ShowConfig();
//ShowConfig();
}
bool TinyIP::SendEthernet(ETH_TYPE type, const MacAddress& remote, const byte* buf, uint len)