板级包bsp应该只带有硬件相关的成员,不该涉及协议。拆分W5500/Esp8266/Gsm三个硬件模组,借助C++的多继承来重构bsp

This commit is contained in:
大石头 2017-09-04 01:18:41 +08:00
parent afa1d03243
commit aea6c2c1f1
29 changed files with 282 additions and 557 deletions

View File

@ -1,13 +1,13 @@
#include "AP0801.h"
#include "Drivers\NRF24L01.h"
//#include "Drivers\NRF24L01.h"
#include "Drivers\W5500.h"
#include "Drivers\Esp8266\Esp8266.h"
#include "Message\ProxyFactory.h"
//#include "Message\ProxyFactory.h"
AP0801* AP0801::Current = nullptr;
static ProxyFactory* ProxyFac = nullptr; // 透传管理器
//static ProxyFactory* ProxyFac = nullptr; // 透传管理器
AP0801::AP0801()
{
@ -18,88 +18,13 @@ AP0801::AP0801()
LedInvert = false;
ProxyFac = nullptr;
AlarmObj = nullptr;
Net.Spi = Spi2;
Net.Irq = PE1;
Net.Reset = PD13;
Esp.Com = COM4;
Esp.Baudrate = 115200;
Esp.Power = PE2;
Esp.Reset = PD3;
Esp.LowPower = P0;
//ProxyFac = nullptr;
//AlarmObj = nullptr;
Current = this;
}
NetworkInterface* AP0801::Create5500()
{
debug_printf("\r\nW5500::Create \r\n");
auto net = new W5500(Net.Spi, Net.Irq, Net.Reset);
if (!net->Open())
{
delete net;
return nullptr;
}
net->SetLed(*Leds[0]);
net->EnableDNS();
net->EnableDHCP();
return net;
}
NetworkInterface* AP0801::Create8266()
{
debug_printf("\r\nEsp8266::Create \r\n");
auto esp = new Esp8266();
esp->Init(Esp.Com, Esp.Baudrate);
esp->Set(Esp.Power, Esp.Reset, Esp.LowPower);
// 初次需要指定模式 否则为 Wire
bool join = esp->SSID && *esp->SSID;
if (!join)
{
*esp->SSID = "WSWL";
*esp->Pass = "12345678";
esp->Mode = NetworkType::STA_AP;
esp->WorkMode = NetworkType::STA_AP;
}
if (!esp->Open())
{
delete esp;
return nullptr;
}
esp->SetLed(*Leds[1]);
Client->Register("SetWiFi", &Esp8266::SetWiFi, esp);
Client->Register("GetWiFi", &Esp8266::GetWiFi, esp);
return esp;
}
static void OnInitNet(void* param)
{
auto& bsp = *(AP0801*)param;
bsp.Create5500();
bsp.Create8266();
bsp.Client->Open();
}
void AP0801::InitNet()
{
Sys.AddTask(OnInitNet, this, 0, -1, "InitNet");
}
void AP0801::InitProxy()
/*void AP0801::InitProxy()
{
if (ProxyFac)return;
if (!Client)
@ -142,7 +67,7 @@ void AP0801::InitAlarm()
AlarmObj->OnAlarm = OnAlarm;
AlarmObj->Start();
}
}*/
/******************************** 2401 ********************************/

View File

@ -1,39 +1,30 @@
#ifndef _AP0801_H_
#define _AP0801_H_
#include "TokenBoard.h"
#include "BaseBoard.h"
#include "W5500Module.h"
#include "Esp8266Module.h"
#include "Net\Socket.h"
#include "Device\Spi.h"
#include "Device\SerialPort.h"
#include "App\Alarm.h"
//#include "App\Alarm.h"
// 阿波罗0801
class AP0801 : public TokenBoard
class AP0801 : public BaseBoard, public W5500Module, public Esp8266Module
{
public:
List<OutputPort*> Outputs;
List<InputPort*> Inputs;
Alarm* AlarmObj;
SpiConfig Net;
SerialConfig Esp;
//Alarm* AlarmObj;
AP0801();
// 打开以太网W5500
NetworkInterface* Create5500();
// 打开Esp8266作为主控或者纯AP
NetworkInterface* Create8266();
// ITransport* Create2401();
void InitNet();
void InitProxy();
void InitAlarm();
//void InitProxy();
//void InitAlarm();
static AP0801* Current;
};

View File

@ -1,12 +1,9 @@
#include "AP0803.h"
#include "Drivers\A67.h"
#include "Drivers\Sim900A.h"
#include "Message\ProxyFactory.h"
//#include "Message\ProxyFactory.h"
AP0803* AP0803::Current = nullptr;
static ProxyFactory* ProxyFac = nullptr; // 透传管理器
//static ProxyFactory* ProxyFac = nullptr; // 透传管理器
AP0803::AP0803()
{
@ -16,9 +13,9 @@ AP0803::AP0803()
ButtonPins.Add(PE9);
ButtonPins.Add(PE14);
Client = nullptr;
/*Client = nullptr;
ProxyFac = nullptr;
AlarmObj = nullptr;
AlarmObj = nullptr;*/
Gsm.Com = COM4;
Gsm.Baudrate = 115200;
@ -29,55 +26,7 @@ AP0803::AP0803()
Current = this;
}
static NetworkInterface* CreateGPRS(GSM07* net, const SerialConfig& gsm, OutputPort* led)
{
net->Init(gsm.Com, gsm.Baudrate);
net->Set(gsm.Power, gsm.Reset, gsm.LowPower);
if (led) net->SetLed(*led);
if (!net->Open())
{
delete net;
return nullptr;
}
return net;
}
NetworkInterface* AP0803::CreateA67()
{
debug_printf("\r\nCreateA67::Create \r\n");
auto net = new A67();
return CreateGPRS(net, Gsm, Leds[0]);
}
NetworkInterface* AP0803::CreateSIM900A()
{
debug_printf("\r\nCreateSIM900A::Create \r\n");
auto net = new Sim900A();
return CreateGPRS(net, Gsm, Leds[0]);
}
static void OnInitNet(void* param)
{
auto& bsp = *(AP0803*)param;
//bsp.CreateGPRS();
bsp.CreateSIM900A();
bsp.Client->Open();
}
void AP0803::InitNet()
{
Sys.AddTask(OnInitNet, this, 0, -1, "InitNet");
}
void AP0803::InitProxy()
/*void AP0803::InitProxy()
{
if (ProxyFac)return;
if (!Client)
@ -120,7 +69,7 @@ void AP0803::InitAlarm()
AlarmObj->OnAlarm = OnAlarm;
AlarmObj->Start();
}
}*/
/*

View File

@ -1,33 +1,24 @@
#ifndef _AP0803_H_
#define _AP0803_H_
#include "TokenBoard.h"
#include "BaseBoard.h"
#include "GsmModule.h"
#include "Net\Socket.h"
#include "Device\SerialPort.h"
#include "App\Alarm.h"
//#include "App\Alarm.h"
// 阿波罗0803 GPRS通信
class AP0803 : public TokenBoard
class AP0803 : public BaseBoard, public GsmModule
{
public:
List<OutputPort*> Outputs;
List<InputPort*> Inputs;
Alarm* AlarmObj;
SerialConfig Gsm;
//Alarm* AlarmObj;
AP0803();
// 打开GPRS
NetworkInterface* CreateA67();
NetworkInterface* CreateSIM900A();
void InitNet();
void InitProxy();
void InitAlarm();
//void InitProxy();
//void InitAlarm();
static AP0803* Current;
};

View File

@ -1,151 +0,0 @@
#include "B8266.h"
#include "Drivers\Esp8266\Esp8266.h"
B8266* B8266::Current = nullptr;
B8266::B8266()
{
LedPins.Add(PA4);
LedPins.Add(PA5);
Esp.Com = COM2;
Esp.Baudrate = 115200;
Esp.Power = PB2;
Esp.Reset = PA1;
Esp.LowPower = P0;
SSID = "WSWL";
Pass = "12345678";
Host = nullptr; // 网络主机
Current = this;
}
void B8266::InitWiFi(cstring ssid, cstring pass)
{
SSID = ssid;
Pass = pass;
}
NetworkInterface* B8266::Create8266()
{
auto esp = new Esp8266();
esp->Init(Esp.Com, Esp.Baudrate);
esp->Set(Esp.Power, Esp.Reset, Esp.LowPower);
// 初次需要指定模式 否则为 Wire
bool join = esp->SSID && *esp->SSID;
//if (!join) esp->Mode = NetworkType::AP;
if (!join)
{
*esp->SSID = SSID;
*esp->Pass = Pass;
esp->Mode = NetworkType::STA_AP;
}
if (!esp->Open())
{
delete esp;
return nullptr;
}
//esp->SetLed(*Leds[0]);
Client->Register("SetWiFi", &Esp8266::SetWiFi, esp);
Client->Register("GetWiFi", &Esp8266::GetWiFi, esp);
Client->Register("GetAPs", &Esp8266::GetAPs, esp);
return esp;
}
void B8266::InitNet()
{
Host = Create8266();
Client->Open();
}
static void OnAlarm(AlarmItem& item)
{
// 1长度n + 1类型 + 1偏移 + (n-2)数据
auto bs = item.GetData();
debug_printf("OnAlarm ");
bs.Show(true);
if (bs[1] == 0x06)
{
auto client = B8266::Current->Client;
client->Store.Write(bs[2], bs.Sub(3, bs[0] - 2));
// 主动上报状态
client->ReportAsync(bs[2], bs[0] - 2);
}
}
void B8266::InitAlarm()
{
if (!Client)return;
if (!AlarmObj) AlarmObj = new Alarm();
Client->Register("Policy/AlarmSet", &Alarm::Set, AlarmObj);
Client->Register("Policy/AlarmGet", &Alarm::Get, AlarmObj);
AlarmObj->OnAlarm = OnAlarm;
AlarmObj->Start();
}
static void RestPress(InputPort& port, bool down)
{
if (down) return;
auto client = B8266::Current;
client->Restore();
}
void B8266::SetRestore(Pin pin)
{
if (pin == P0) return;
auto port = new InputPort(pin);
port->Open();
port->UsePress();
port->Press = RestPress;
RestPort = port;
}
/*
NRF24L01+ (SPI3) | W5500 (SPI2) | TOUCH (SPI3)
NSS | NSS | PD6 NSS
CLK | SCK | SCK
MISO | MISO | MISO
MOSI | MOSI | MOSI
PE3 IRQ | PE1 INT(IRQ) | PD11 INT(IRQ)
PD12 CE | PD13 NET_NRST | NET_NRST
PE6 POWER | POWER | POWER
ESP8266 (COM4)
TX
RX
PD3 RST
PE2 POWER
TFT
FSMC_D 0..15 TFT_D 0..15
NOE RD
NWE RW
NE1 RS
PE4 CE
PC7 LIGHT
PE5 RST
PE13 KEY1
PE14 KEY2
PE15 LED2
PD8 LED1
USB
PA11 PA12
*/

View File

@ -1,41 +0,0 @@
#ifndef _B8266_H_
#define _B8266_H_
#include "TokenBoard.h"
#include "Net\Socket.h"
#include "Device\Spi.h"
#include "Device\SerialPort.h"
#include "App\Alarm.h"
// Esp8266核心板
class B8266 : public TokenBoard
{
public:
Alarm* AlarmObj;
SerialConfig Esp;
NetworkInterface* Host; // 网络主机
cstring SSID;
cstring Pass;
B8266();
void InitWiFi(cstring ssid, cstring pass);
void SetRestore(Pin pin = PB4); // 设置重置引脚
NetworkInterface* Create8266();
void InitNet();
void InitAlarm();
static B8266* Current;
private:
InputPort* RestPort;
};
#endif

54
Board/Esp8266Module.cpp Normal file
View File

@ -0,0 +1,54 @@
#include "Esp8266Module.h"
#include "Drivers\Esp8266\Esp8266.h"
Esp8266Module::Esp8266Module()
{
SSID = "WSWL";
Pass = "12345678";
Esp.Com = COM4;
Esp.Baudrate = 115200;
Esp.Power = PE2;
Esp.Reset = PD3;
Esp.LowPower = P0;
}
void Esp8266Module::InitWiFi(cstring ssid, cstring pass)
{
SSID = ssid;
Pass = pass;
}
NetworkInterface* Esp8266Module::Create8266(OutputPort* led)
{
debug_printf("\r\nEsp8266::Create \r\n");
auto esp = new Esp8266();
esp->Init(Esp.Com, Esp.Baudrate);
esp->Set(Esp.Power, Esp.Reset, Esp.LowPower);
// 初次需要指定模式 否则为 Wire
bool join = esp->SSID && *esp->SSID;
if (!join)
{
*esp->SSID = SSID;
*esp->Pass = Pass;
esp->Mode = NetworkType::STA_AP;
esp->WorkMode = NetworkType::STA_AP;
}
if (!esp->Open())
{
delete esp;
return nullptr;
}
if (led)esp->SetLed(*led);
//Client->Register("SetWiFi", &Esp8266::SetWiFi, esp);
//Client->Register("GetWiFi", &Esp8266::GetWiFi, esp);
//Client->Register("GetAPs", &Esp8266::GetAPs, esp);
return esp;
}

24
Board/Esp8266Module.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef _Esp8266Module_H_
#define _Esp8266Module_H_
#include "Net\Socket.h"
#include "Device\SerialPort.h"
// Esp8266模块
class Esp8266Module
{
public:
SerialConfig Esp;
cstring SSID;
cstring Pass;
Esp8266Module();
void InitWiFi(cstring ssid, cstring pass);
// 打开Esp8266作为主控或者纯AP
NetworkInterface* Create8266(OutputPort* led = nullptr);
};
#endif

46
Board/GsmModule.cpp Normal file
View File

@ -0,0 +1,46 @@
#include "GsmModule.h"
#include "Drivers\A67.h"
#include "Drivers\Sim900A.h"
GsmModule::GsmModule()
{
Gsm.Com = COM4;
Gsm.Baudrate = 115200;
Gsm.Power = PE0; // 0A04170509板上电源为PA4
Gsm.Reset = PD3;
Gsm.LowPower = P0;
}
static NetworkInterface* CreateGPRS(GSM07* net, const SerialConfig& gsm, OutputPort* led)
{
net->Init(gsm.Com, gsm.Baudrate);
net->Set(gsm.Power, gsm.Reset, gsm.LowPower);
if (led) net->SetLed(*led);
if (!net->Open())
{
delete net;
return nullptr;
}
return net;
}
NetworkInterface* GsmModule::CreateA67(OutputPort* led)
{
debug_printf("\r\nCreateA67::Create \r\n");
auto net = new A67();
return CreateGPRS(net, Gsm, led);
}
NetworkInterface* GsmModule::CreateSIM900A(OutputPort* led)
{
debug_printf("\r\nCreateSIM900A::Create \r\n");
auto net = new Sim900A();
return CreateGPRS(net, Gsm, led);
}

20
Board/GsmModule.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef _GsmModule_H_
#define _GsmModule_H_
#include "Net\Socket.h"
#include "Device\SerialPort.h"
// GSM模块
class GsmModule
{
public:
SerialConfig Gsm;
GsmModule();
// 打开GPRS
NetworkInterface* CreateA67(OutputPort* led = nullptr);
NetworkInterface* CreateSIM900A(OutputPort* led = nullptr);
};
#endif

View File

@ -25,10 +25,14 @@ IOK026X::IOK026X()
Data = nullptr;
Size = 0;
Current = this;
SSID = "WSWL";
Pass = "12345678";
Esp.Com = COM2;
Esp.Baudrate = 115200;
Esp.Power = PB2;
Esp.Reset = PA1;
Esp.LowPower = P0;
Current = this;
}
void* IOK026X::InitData(void* data, int size)
@ -56,12 +60,6 @@ void* IOK026X::InitData(void* data, int size)
return data;
}
void IOK026X::InitWiFi(cstring ssid, cstring pass)
{
SSID = ssid;
Pass = pass;
}
void IOK026X::InitLeds()
{
for (int i = 0; i < LedPins.Count(); i++)
@ -74,38 +72,6 @@ void IOK026X::InitLeds()
}
}
NetworkInterface* IOK026X::Create8266()
{
auto esp = new Esp8266();
esp->Init(COM2);
esp->Set(PB2, PA1);
// 初次需要指定模式 否则为 Wire
bool join = esp->SSID && *esp->SSID;
//if (!join) esp->Mode = NetworkType::AP;
if (!join)
{
*esp->SSID = SSID;
*esp->Pass = Pass;
esp->Mode = NetworkType::STA_AP;
}
if(!esp->Open())
{
delete esp;
return nullptr;
}
esp->SetLed(*Leds[0]);
Client->Register("SetWiFi", &Esp8266::SetWiFi, esp);
Client->Register("GetWiFi", &Esp8266::GetWiFi, esp);
Client->Register("GetAPs", &Esp8266::GetAPs, esp);
return esp;
}
/******************************** Token ********************************/
void IOK026X::InitClient()

View File

@ -1,7 +1,9 @@
#ifndef _IOK026X_H_
#define _IOK026X_H_
#include "Kernel\Sys.h"
#include "BaseBoard.h"
#include "Esp8266Module.h"
#include "Net\ITransport.h"
#include "Net\Socket.h"
@ -10,7 +12,7 @@
#include "Device\RTC.h"
// WIFI触摸开关 123位
class IOK026X
class IOK026X : public BaseBoard, public Esp8266Module
{
public:
List<Pin> LedPins;
@ -23,13 +25,9 @@ public:
Alarm* AlarmObj;
uint LedsTaskId;
cstring SSID;
cstring Pass;
IOK026X();
void* InitData(void* data, int size);
void InitWiFi(cstring ssid, cstring pass);
void Register(int index, IDataPort& dp);
void InitLeds();
@ -37,8 +35,6 @@ public:
byte LedStat(byte showmode);
NetworkInterface* Create8266();
void InitClient();
void InitNet();
void InitAlarm();

View File

@ -1,8 +1,9 @@
#include "IOK027X.h"
IOK027X* IOK027X::Current = nullptr;
#include "Drivers\Esp8266\Esp8266.h"
IOK027X* IOK027X::Current = nullptr;
IOK027X::IOK027X()
{
LedPins.Clear();
@ -83,7 +84,7 @@ void IOK027X::InitLeds()
}
void IOK027X::FlushLed()
/*void IOK027X::FlushLed()
{
if (LedsShow == 0) // 启动时候20秒
{
@ -150,7 +151,7 @@ byte IOK027X::LedStat(byte showmode)
}
return LedsShow;
}
}*/
/*
NRF24L01+ (SPI3) | W5500 (SPI2) | TOUCH (SPI3)

View File

@ -1,16 +1,20 @@
#ifndef _IOK027X_H_
#define _IOK027X_H_
#include "B8266.h"
#include "BaseBoard.h"
#include "Esp8266Module.h"
#include "TokenNet\TokenClient.h"
#include "APP\Button_GrayLevel.h"
// WIFI触摸开关
class IOK027X : public B8266
class IOK027X : public BaseBoard, public Esp8266Module
{
public:
byte LedsShow; // LED 显示状态开关 0 刚启动时候的20秒 1 使能 2 失能
uint LedsTaskId;
TokenClient* Client;
IOK027X();

View File

@ -34,7 +34,7 @@ void IOK0612::InitLeds()
}
void IOK0612::FlushLed()
/*void IOK0612::FlushLed()
{
if (LedsShow == 0) // 启动时候20秒
{
@ -101,7 +101,8 @@ byte IOK0612::LedStat(byte showmode)
}
return LedsShow;
}
}*/
/*
NRF24L01+ (SPI3)
NSS |

View File

@ -1,10 +1,11 @@
#ifndef _IOK0612_H_
#define _IOK0612_H_
#include "B8266.h"
#include "BaseBoard.h"
#include "Esp8266Module.h"
// WIFI WRGB调色
class IOK0612 : public B8266
class IOK0612 : public BaseBoard, public Esp8266Module
{
public:
byte LedsShow; // LED 显示状态开关 0 刚启动时候的20秒 1 使能 2 失能

View File

@ -52,16 +52,8 @@ void LinkBoard::InitClient()
{
if (Client) return;
// 初始化配置区
InitConfig();
// 初始化令牌网
auto tk = LinkConfig::Create("tcp://192.168.0.3:2233");
// 创建客户端
auto tc = LinkClient::CreateFast(Buffer(Data, Size));
tc->Cfg = tk;
tc->MaxNotActive = 8 * 60 * 1000;
auto tc = LinkClient::Create("tcp://192.168.0.3:2233", Buffer(Data, Size));
Client = tc;
}
@ -102,53 +94,3 @@ void LinkBoard::OnLongPress(InputPort* port, bool down)
Sys.Reboot(1000);
}
}
NetworkInterface* LinkBoard::Create5500()
{
debug_printf("\r\nW5500::Create \r\n");
auto net = new W5500(Net.Spi, Net.Irq, Net.Reset);
if (!net->Open())
{
delete net;
return nullptr;
}
net->SetLed(*Leds[0]);
net->EnableDNS();
net->EnableDHCP();
return net;
}
NetworkInterface* LinkBoard::Create8266()
{
debug_printf("\r\nEsp8266::Create \r\n");
auto esp = new Esp8266();
esp->Init(Esp.Com, Esp.Baudrate);
esp->Set(Esp.Power, Esp.Reset, Esp.LowPower);
// 初次需要指定模式 否则为 Wire
bool join = esp->SSID && *esp->SSID;
if (!join)
{
*esp->SSID = "WSWL";
*esp->Pass = "12345678";
esp->Mode = NetworkType::STA_AP;
esp->WorkMode = NetworkType::STA_AP;
}
if (!esp->Open())
{
delete esp;
return nullptr;
}
esp->SetLed(*Leds[1]);
//Client->Register("SetWiFi", &Esp8266::SetWiFi, esp);
//Client->Register("GetWiFi", &Esp8266::GetWiFi, esp);
return esp;
}

View File

@ -1,17 +1,10 @@
#ifndef _LinkBoard_H_
#define _LinkBoard_H_
#include "Net\Socket.h"
#include "Device\Spi.h"
#include "Device\SerialPort.h"
#include "BaseBoard.h"
#include "Link\LinkClient.h"
// 物联协议板级包基类
class LinkBoard : public BaseBoard
class LinkBoard
{
public:
LinkClient* Client; // 物联客户端
@ -32,14 +25,6 @@ public:
void InitClient();
SpiConfig Net;
SerialConfig Esp;
// 打开以太网W5500
NetworkInterface* Create5500();
// 打开Esp8266作为主控或者纯AP
NetworkInterface* Create8266();
private:
void* Data;
int Size;

View File

@ -85,39 +85,6 @@ void NH3_0317::InitButtons(const Delegate2<InputPort&, bool>& press)
}
}
NetworkInterface* NH3_0317::Create8266()
{
// auto esp = new Esp8266(COM2, PB2, PA1); // 触摸开关的
//auto esp = new Esp8266(COM3, P0, PA5);
auto esp = new Esp8266();
esp->Init(COM3);
esp->Set(P0, PA5);
// 初次需要指定模式 否则为 Wire
bool join = esp->SSID && *esp->SSID;
//if (!join) esp->Mode = NetworkType::AP;
if (!join)
{
*esp->SSID = "WSWL";
*esp->Pass = "12345678";
esp->Mode = NetworkType::STA_AP;
}
if(!esp->Open())
{
delete esp;
return nullptr;
}
esp->SetLed(*Leds[0]);
Client->Register("SetWiFi", &Esp8266::SetWiFi, esp);
Client->Register("GetWiFi", &Esp8266::GetWiFi, esp);
return esp;
}
/******************************** Token ********************************/
void NH3_0317::InitClient()

View File

@ -1,16 +1,15 @@
#ifndef _NH3_0317_H_
#define _NH3_0317_H_
#include "Kernel\Sys.h"
#include "Net\ITransport.h"
#include "Net\Socket.h"
#include "BaseBoard.h"
#include "Esp8266Module.h"
#include "TokenNet\TokenClient.h"
#include "App\Alarm.h"
#include "Device\RTC.h"
// 氨气
class NH3_0317
class NH3_0317 : public BaseBoard, public Esp8266Module
{
public:
List<Pin> LedPins;
@ -21,7 +20,7 @@ public:
bool LedsShow; // LED 显示状态开关
NetworkInterface* Host; // 网络主机
TokenClient* Client; //
TokenClient* Client;
Alarm* AlarmObj;
uint LedsTaskId;
@ -36,8 +35,6 @@ public:
bool LedStat(bool enable);
NetworkInterface* Create8266();
void InitClient();
void InitNet();
void InitAlarm();

View File

@ -49,9 +49,6 @@ void TokenBoard::InitClient(bool useLocal)
{
if (Client) return;
// 初始化配置区
InitConfig();
// 创建客户端
auto tc = TokenClient::Create("udp://smart.wslink.cn:33333", Buffer(Data, Size));
if (useLocal) tc->UseLocal();

View File

@ -1,12 +1,10 @@
#ifndef _TokenBoard_H_
#define _TokenBoard_H_
#include "BaseBoard.h"
#include "TokenNet\TokenClient.h"
// 令牌协议板级包基类
class TokenBoard : public BaseBoard
class TokenBoard
{
public:
TokenClient* Client; // 令牌客户端

28
Board/W5500Module.cpp Normal file
View File

@ -0,0 +1,28 @@
#include "W5500Module.h"
#include "Drivers\W5500.h"
W5500Module::W5500Module()
{
Net.Spi = Spi2;
Net.Irq = PE1;
Net.Reset = PD13;
}
NetworkInterface* W5500Module::Create5500(OutputPort* led)
{
debug_printf("\r\nW5500::Create \r\n");
auto net = new W5500(Net.Spi, Net.Irq, Net.Reset);
if (!net->Open())
{
delete net;
return nullptr;
}
if (led) net->SetLed(*led);
net->EnableDNS();
net->EnableDHCP();
return net;
}

19
Board/W5500Module.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef _W5500Module_H_
#define _W5500Module_H_
#include "Net\Socket.h"
#include "Device\Spi.h"
// W5500模块
class W5500Module
{
public:
SpiConfig Net;
W5500Module();
// 打开以太网W5500
NetworkInterface* Create5500(OutputPort* led = nullptr);
};
#endif

View File

@ -5,6 +5,8 @@
#include "Net\Socket.h"
#include "Device\Spi.h"
class IDataPort;
// W5500以太网驱动
class W5500 : public NetworkInterface
{

View File

@ -29,7 +29,7 @@ LinkClient::LinkClient()
MaxNotActive = 0;
_task = 0;
ReportStart = 0;
ReportStart = -1;
ReportLength = 0;
assert(!Current, "只能有一个物联客户端实例");
@ -377,6 +377,7 @@ void LinkClient::Ping()
// 原始密码对盐值进行加密,得到登录密码
auto ms = (int)Sys.Ms();
json.Add("Time", ms);
json.Add("Data", Store.Data.ToHex());
Invoke("Device/Ping", json);
}
@ -479,7 +480,7 @@ void LinkClient::Write(int start, const Buffer& bs)
js.Add("start", start);
js.Add("data", bs.ToHex());
Invoke("Write", js);
Invoke("Device/Write", js);
}
void LinkClient::Write(int start, byte dat)
@ -510,11 +511,11 @@ bool LinkClient::CheckReport()
auto offset = ReportStart;
int len = ReportLength;
if (offset < 0) return false;
if (offset < 0 || len <= 0) return false;
// 检查索引,否则数组越界
auto& bs = Store.Data;
if (bs.Length() >= offset + len) Write(offset, Buffer(&bs[offset], len));
if (bs.Length() >= offset + len) Write(offset, bs.Sub(offset, len));
ReportStart = -1;
@ -522,9 +523,17 @@ bool LinkClient::CheckReport()
}
// 快速建立客户端注册默认Api
LinkClient* LinkClient::CreateFast(const Buffer& store)
LinkClient* LinkClient::Create(cstring server, const Buffer& store)
{
// Flash最后一块作为配置区
if (Config::Current == nullptr) Config::Current = &Config::CreateFlash();
// 初始化令牌网
auto tk = LinkConfig::Create(server);
auto tc = new LinkClient();
tc->Cfg = tk;
tc->MaxNotActive = 8 * 60 * 1000;
/*// 重启
tc->Register("Gateway/Restart", &LinkClient::InvokeRestart, tc);

View File

@ -66,7 +66,7 @@ public:
void Reboot(const String& reason);
// 快速建立客户端注册默认Api
static LinkClient* CreateFast(const Buffer& store);
static LinkClient* Create(cstring server, const Buffer& store);
static LinkClient* Current;

View File

@ -33,8 +33,9 @@
<ClCompile Include="..\Board\AP0801.cpp" />
<ClCompile Include="..\Board\AP0802.cpp" />
<ClCompile Include="..\Board\AP0803.cpp" />
<ClCompile Include="..\Board\B8266.cpp" />
<ClCompile Include="..\Board\BaseBoard.cpp" />
<ClCompile Include="..\Board\Esp8266Module.cpp" />
<ClCompile Include="..\Board\GsmModule.cpp" />
<ClCompile Include="..\Board\IOK026X.cpp" />
<ClCompile Include="..\Board\IOK027X.cpp" />
<ClCompile Include="..\Board\IOK0612.cpp" />
@ -42,6 +43,7 @@
<ClCompile Include="..\Board\NH3_0317.cpp" />
<ClCompile Include="..\Board\Pandora.cpp" />
<ClCompile Include="..\Board\TokenBoard.cpp" />
<ClCompile Include="..\Board\W5500Module.cpp" />
<ClCompile Include="..\BootConfig.cpp" />
<ClCompile Include="..\Config.cpp" />
<ClCompile Include="..\Core\Array.cpp" />
@ -184,7 +186,6 @@
<ClCompile Include="..\TokenNet\HelloMessage.cpp" />
<ClCompile Include="..\TokenNet\LoginMessage.cpp" />
<ClCompile Include="..\TokenNet\RegisterMessage.cpp" />
<ClCompile Include="..\TokenNet\Token.cpp" />
<ClCompile Include="..\TokenNet\TokenClient.cpp" />
<ClCompile Include="..\TokenNet\TokenConfig.cpp" />
<ClCompile Include="..\TokenNet\TokenController.cpp" />

View File

@ -456,9 +456,6 @@
<ClCompile Include="..\TokenNet\RegisterMessage.cpp">
<Filter>TokenNet</Filter>
</ClCompile>
<ClCompile Include="..\TokenNet\Token.cpp">
<Filter>TokenNet</Filter>
</ClCompile>
<ClCompile Include="..\TokenNet\TokenClient.cpp">
<Filter>TokenNet</Filter>
</ClCompile>
@ -575,9 +572,6 @@
<ClCompile Include="..\Drivers\A67.cpp">
<Filter>Drivers</Filter>
</ClCompile>
<ClCompile Include="..\Board\B8266.cpp">
<Filter>Board</Filter>
</ClCompile>
<ClCompile Include="..\Board\TokenBoard.cpp">
<Filter>Board</Filter>
</ClCompile>
@ -596,5 +590,14 @@
<ClCompile Include="..\Board\LinkBoard.cpp">
<Filter>Board</Filter>
</ClCompile>
<ClCompile Include="..\Board\Esp8266Module.cpp">
<Filter>Board</Filter>
</ClCompile>
<ClCompile Include="..\Board\GsmModule.cpp">
<Filter>Board</Filter>
</ClCompile>
<ClCompile Include="..\Board\W5500Module.cpp">
<Filter>Board</Filter>
</ClCompile>
</ItemGroup>
</Project>