优先使用int替换为uint,方便运算,能够准确识别产生的负数。
GCC/VC编译通过,MDK编译Port/I2C失败
This commit is contained in:
parent
226f837205
commit
1d6498c866
|
@ -17,7 +17,9 @@
|
|||
#include "SString.h"
|
||||
|
||||
char* utohex(uint value, byte size, char* string, bool upper);
|
||||
#ifndef _MSC_VER
|
||||
extern char* itoa(int value, char* string, int radix);
|
||||
#endif
|
||||
extern char* ltoa(Int64 value, char* string, int radix);
|
||||
extern char* utoa(uint value, char* string, int radix);
|
||||
extern char* ultoa(UInt64 value, char* string, int radix);
|
||||
|
@ -189,7 +191,7 @@ bool String::CheckCapacity(int size)
|
|||
|
||||
void* String::Alloc(int len)
|
||||
{
|
||||
if (len <= sizeof(Arr))
|
||||
if (len <= (int)sizeof(Arr))
|
||||
{
|
||||
_needFree = false;
|
||||
return Arr;
|
||||
|
|
|
@ -4,13 +4,13 @@
|
|||
|
||||
Proxy::Proxy()
|
||||
{
|
||||
Cache = nullptr;
|
||||
CacheSize = 10;
|
||||
BufferSize = 256;
|
||||
TimeStamp = 0;
|
||||
EnableStamp = false;
|
||||
UploadTaskId= 0;
|
||||
AutoStart = false;
|
||||
Cache = nullptr;
|
||||
CacheSize = 10;
|
||||
BufferSize = 256;
|
||||
TimeStamp = 0;
|
||||
EnableStamp = false;
|
||||
UploadTaskId = 0;
|
||||
AutoStart = false;
|
||||
}
|
||||
|
||||
bool Proxy::Open()
|
||||
|
@ -50,7 +50,7 @@ bool Proxy::SetConfig(Dictionary<cstring, int>& config, String& str)
|
|||
value = 0;
|
||||
if (config.TryGetValue(ByteParam2[i], value))
|
||||
{
|
||||
*(ParamP2[i]) = value;
|
||||
*(ParamP2[i]) = value > 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -72,7 +72,7 @@ bool Proxy::GetConfig(Dictionary<cstring, int>& config)
|
|||
// debug_printf("基础配置条数%d\r\n",config.Count());
|
||||
OnGetConfig(config);
|
||||
|
||||
debug_printf("一共%d跳配置",config.Count());
|
||||
debug_printf("一共%d跳配置", config.Count());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -98,7 +98,7 @@ bool Proxy::Upload(Buffer& data)
|
|||
bp.Set("Data", data);
|
||||
// CacheSize 为0时立马发送 || Cache 满立马发送
|
||||
if ((UploadTaskId && !CacheSize)
|
||||
||(CacheSize && Cache->Position()>CacheSize))Sys.SetTask(UploadTaskId, true, 0);
|
||||
|| (CacheSize && Cache->Position() > CacheSize))Sys.SetTask(UploadTaskId, true, 0);
|
||||
/*
|
||||
不知道怎么在同一个包里存放多个数据,暂时没写缓存问题的东西
|
||||
*/
|
||||
|
@ -117,8 +117,8 @@ bool Proxy::LoadConfig()
|
|||
|
||||
CacheSize = cfg.CacheSize;
|
||||
BufferSize = cfg.BufferSize;
|
||||
EnableStamp = cfg.EnableStamp;
|
||||
AutoStart = cfg.AutoStart;
|
||||
EnableStamp = cfg.EnableStamp > 0;
|
||||
AutoStart = cfg.AutoStart > 0;
|
||||
|
||||
Stream st(cfg.PortCfg, sizeof(cfg.PortCfg));
|
||||
OnGetConfig(st);
|
||||
|
@ -130,10 +130,10 @@ void Proxy::SaveConfig()
|
|||
{
|
||||
ProxyConfig cfg(Name);
|
||||
|
||||
cfg.CacheSize = CacheSize;
|
||||
cfg.EnableStamp = EnableStamp;
|
||||
cfg.AutoStart = AutoStart;
|
||||
cfg.BufferSize = BufferSize;
|
||||
cfg.CacheSize = CacheSize;
|
||||
cfg.EnableStamp = EnableStamp;
|
||||
cfg.AutoStart = AutoStart;
|
||||
cfg.BufferSize = BufferSize;
|
||||
|
||||
Stream st(cfg.PortCfg, sizeof(cfg.PortCfg));
|
||||
OnSetConfig(st);
|
||||
|
@ -203,7 +203,7 @@ bool ComProxy::OnSetConfig(Dictionary<cstring, int>& config, String& str)
|
|||
}
|
||||
|
||||
cstring const ByteParam[] = { "parity","dataBits","stopBits" };
|
||||
ushort* ParamP[] = {&parity, &dataBits, &stopBits};
|
||||
byte* ParamP[] = { &parity, &dataBits, &stopBits };
|
||||
bool haveChang = false;
|
||||
for (int i = 0; i < ArrayLength(ByteParam); i++)
|
||||
{
|
||||
|
@ -214,7 +214,7 @@ bool ComProxy::OnSetConfig(Dictionary<cstring, int>& config, String& str)
|
|||
haveChang = true;
|
||||
}
|
||||
}
|
||||
if (haveChang)port.Set(parity, dataBits, stopBits);
|
||||
if (haveChang)port.Set(parity, dataBits, stopBits);
|
||||
// SaveConfig();
|
||||
return true;
|
||||
}
|
||||
|
@ -249,7 +249,7 @@ uint ComProxy::Dispatch(ITransport* port, Buffer& bs, void* param, void* param2)
|
|||
// 串口不关心 param2
|
||||
auto& pro = *(ComProxy*)param;
|
||||
pro.Upload(bs);
|
||||
|
||||
|
||||
// auto fac = ProxyFactory::Current; // 直接扔给上级
|
||||
// if (!fac)return 0;
|
||||
// fac->Upload(pro, bs);
|
||||
|
@ -258,9 +258,9 @@ uint ComProxy::Dispatch(ITransport* port, Buffer& bs, void* param, void* param2)
|
|||
|
||||
bool ComProxy::OnGetConfig(Stream& cfg)
|
||||
{
|
||||
parity = cfg.ReadUInt16();
|
||||
dataBits = cfg.ReadUInt16();
|
||||
stopBits = cfg.ReadUInt16();
|
||||
parity = (byte)cfg.ReadByte();
|
||||
dataBits = (byte)cfg.ReadByte();
|
||||
stopBits = (byte)cfg.ReadByte();
|
||||
baudRate = cfg.ReadUInt32();
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -53,9 +53,9 @@ public:
|
|||
|
||||
SerialPort port;
|
||||
|
||||
ushort parity;
|
||||
ushort dataBits;
|
||||
ushort stopBits;
|
||||
byte parity;
|
||||
byte dataBits;
|
||||
byte stopBits;
|
||||
int baudRate;
|
||||
|
||||
virtual bool OnSetConfig(Dictionary<cstring, int>& config, String& str) override;
|
||||
|
|
|
@ -132,7 +132,7 @@ bool SerialPort::OnWrite(const Buffer& bs)
|
|||
}
|
||||
|
||||
// 刷出某个端口中的数据
|
||||
bool SerialPort::Flush(uint times)
|
||||
bool SerialPort::Flush(int times)
|
||||
{
|
||||
// 打开串口发送
|
||||
Set485(true);
|
||||
|
|
|
@ -14,15 +14,15 @@ class SerialPort : public ITransport, public Power
|
|||
private:
|
||||
friend class ComProxy;
|
||||
int _baudRate;
|
||||
ushort _dataBits;
|
||||
ushort _parity;
|
||||
ushort _stopBits;
|
||||
byte _dataBits;
|
||||
byte _parity;
|
||||
byte _stopBits;
|
||||
|
||||
void Init();
|
||||
|
||||
public:
|
||||
char Name[5]; // 名称。COMxx,后面1字节\0表示结束
|
||||
byte Remap; // 重映射组
|
||||
byte Remap; // 重映射组
|
||||
OutputPort* RS485; // RS485使能引脚
|
||||
int Error; // 错误计数
|
||||
int ByteTime; // 字节间隔,最小1ms
|
||||
|
@ -30,28 +30,28 @@ public:
|
|||
Port* Ports[2]; // Tx/Rx
|
||||
COM Index;
|
||||
|
||||
void* State;
|
||||
void* State;
|
||||
|
||||
// 收发缓冲区
|
||||
Queue Tx;
|
||||
Queue Rx;
|
||||
|
||||
SerialPort();
|
||||
SerialPort(COM index, int baudRate = SERIAL_BAUDRATE);
|
||||
SerialPort(COM index, int baudRate = SERIAL_BAUDRATE);
|
||||
|
||||
// 析构时自动关闭
|
||||
virtual ~SerialPort();
|
||||
virtual ~SerialPort();
|
||||
|
||||
void Set(COM index, int baudRate = SERIAL_BAUDRATE);
|
||||
void Set(byte dataBits, byte parity, byte stopBits);
|
||||
void Set(COM index, int baudRate = SERIAL_BAUDRATE);
|
||||
void Set(byte dataBits, byte parity, byte stopBits);
|
||||
|
||||
uint SendData(byte data, uint times = 3000);
|
||||
int SendData(byte data, int times = 3000);
|
||||
|
||||
bool Flush(uint times = 3000);
|
||||
bool Flush(int times = 3000);
|
||||
|
||||
void SetBaudRate(int baudRate = SERIAL_BAUDRATE);
|
||||
|
||||
virtual void Register(TransportHandler handler, void* param = nullptr);
|
||||
virtual void Register(TransportHandler handler, void* param = nullptr);
|
||||
|
||||
// 电源等级变更(如进入低功耗模式)时调用
|
||||
virtual void ChangePower(int level);
|
||||
|
@ -69,9 +69,9 @@ public:
|
|||
|
||||
protected:
|
||||
virtual bool OnOpen();
|
||||
virtual void OnClose();
|
||||
virtual void OnClose();
|
||||
|
||||
virtual bool OnWrite(const Buffer& bs);
|
||||
virtual bool OnWrite(const Buffer& bs);
|
||||
virtual uint OnRead(Buffer& bs);
|
||||
|
||||
private:
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -11,44 +11,44 @@
|
|||
class Enc28j60 : public ITransport
|
||||
{
|
||||
private:
|
||||
Spi* _spi;
|
||||
OutputPort _ce;
|
||||
Spi* _spi;
|
||||
OutputPort _ce;
|
||||
OutputPort _reset;
|
||||
|
||||
uint NextPacketPtr;
|
||||
uint NextPacketPtr;
|
||||
|
||||
UInt64 LastTime; // 记录最后一次收到数据的时间,超时重启
|
||||
uint ResetPeriod; // 重启间隔,默认6000微秒
|
||||
int ResetPeriod; // 重启间隔,默认6000微秒
|
||||
uint _ResetTask; // 重启任务
|
||||
public:
|
||||
//byte Mac[6];
|
||||
MacAddress Mac;
|
||||
byte Bank;
|
||||
byte Bank;
|
||||
|
||||
Enc28j60();
|
||||
virtual ~Enc28j60();
|
||||
virtual ~Enc28j60();
|
||||
void Init(Spi* spi, Pin ce = P0, Pin reset = P0);
|
||||
|
||||
byte ReadOp(byte op, byte addr);
|
||||
void WriteOp(byte op, byte addr, byte data);
|
||||
void ReadBuffer(byte* buf, uint len);
|
||||
void WriteBuffer(const byte* buf, uint len);
|
||||
byte ReadOp(byte op, byte addr);
|
||||
void WriteOp(byte op, byte addr, byte data);
|
||||
void ReadBuffer(byte* buf, int len);
|
||||
void WriteBuffer(const byte* buf, int len);
|
||||
// 设定寄存器地址区域
|
||||
void SetBank(byte addr);
|
||||
void SetBank(byte addr);
|
||||
// 读取寄存器值 发送读寄存器命令和地址
|
||||
byte ReadReg(byte addr);
|
||||
byte ReadReg(byte addr);
|
||||
// 写寄存器值 发送写寄存器命令和地址
|
||||
void WriteReg(byte addr, byte data);
|
||||
ushort PhyRead(byte addr);
|
||||
bool PhyWrite(byte addr, ushort data);
|
||||
void ClockOut(byte clock);
|
||||
void WriteReg(byte addr, byte data);
|
||||
ushort PhyRead(byte addr);
|
||||
bool PhyWrite(byte addr, ushort data);
|
||||
void ClockOut(byte clock);
|
||||
bool Linked();
|
||||
|
||||
// 电源等级变更(如进入低功耗模式)时调用
|
||||
virtual void ChangePower(int level);
|
||||
|
||||
//void Init(byte mac[6]);
|
||||
byte GetRevision();
|
||||
//void Init(byte mac[6]);
|
||||
byte GetRevision();
|
||||
|
||||
bool Broadcast;
|
||||
// 设置是否接收广播数据包
|
||||
|
@ -63,9 +63,9 @@ public:
|
|||
|
||||
protected:
|
||||
virtual bool OnOpen();
|
||||
virtual void OnClose() { }
|
||||
virtual void OnClose() { }
|
||||
|
||||
virtual bool OnWrite(const Buffer& bs);
|
||||
virtual bool OnWrite(const Buffer& bs);
|
||||
virtual uint OnRead(Buffer& bs);
|
||||
};
|
||||
|
||||
|
|
|
@ -50,7 +50,7 @@ bool JTW8953::WriteKey(ushort index, byte data)
|
|||
index = index + ADDRESS_T;
|
||||
|
||||
ByteArray buf(3);
|
||||
buf[0] = index;
|
||||
buf[0] = index & 0xFF;
|
||||
buf[1] = 0x10;
|
||||
buf[2] = data;
|
||||
|
||||
|
|
|
@ -69,8 +69,8 @@ public:
|
|||
|
||||
ShunCom::ShunCom()
|
||||
{
|
||||
Led = nullptr;
|
||||
ExternalCfg = nullptr;
|
||||
Led = nullptr;
|
||||
ExternalCfg = nullptr;
|
||||
}
|
||||
|
||||
void ShunCom::Init(ITransport* port, Pin rst)
|
||||
|
@ -79,43 +79,43 @@ void ShunCom::Init(ITransport* port, Pin rst)
|
|||
|
||||
Set(port);
|
||||
//MaxSize = 82;
|
||||
MaxSize = 64;
|
||||
AddrLength = 0;
|
||||
MaxSize = 64;
|
||||
AddrLength = 0;
|
||||
|
||||
if(rst != P0) Reset.Init(rst, true);
|
||||
if (rst != P0) Reset.Init(rst, true);
|
||||
}
|
||||
|
||||
bool ShunCom::OnOpen()
|
||||
{
|
||||
if(ExternalCfg)ExternalCfg(this);
|
||||
if (ExternalCfg)ExternalCfg(this);
|
||||
|
||||
debug_printf("\r\nShunCom::Open \r\n");
|
||||
|
||||
debug_printf(" Sleep : ");
|
||||
debug_printf(" Sleep : ");
|
||||
Sleep.Open();
|
||||
debug_printf(" Config: ");
|
||||
debug_printf(" Config: ");
|
||||
Config.Open();
|
||||
debug_printf(" Reset : ");
|
||||
debug_printf(" Reset : ");
|
||||
Reset.Open();
|
||||
debug_printf(" Power : ");
|
||||
debug_printf(" Power : ");
|
||||
Power.Open();
|
||||
|
||||
Power = true;
|
||||
Sleep = false;
|
||||
Config = false;
|
||||
Reset = false;
|
||||
Power = true;
|
||||
Sleep = false;
|
||||
Config = false;
|
||||
Reset = false;
|
||||
|
||||
//debug_printf("Power=%d Sleep=%d Config=%d Reset=%d MinSize=%d\r\n", Power.Read(), Sleep.Read(), Config.Read(), Reset.Read(), MinSize);
|
||||
|
||||
Port->MinSize = MinSize;
|
||||
Port->MinSize = MinSize;
|
||||
|
||||
return PackPort::OnOpen();
|
||||
}
|
||||
|
||||
void ShunCom::OnClose()
|
||||
{
|
||||
Power = false;
|
||||
Reset = true;
|
||||
Power = false;
|
||||
Reset = true;
|
||||
|
||||
Power.Close();
|
||||
Sleep.Close();
|
||||
|
@ -131,10 +131,10 @@ void ShunCom::ChangePower(int level)
|
|||
{
|
||||
//Power = false;
|
||||
|
||||
Sleep = true;
|
||||
Config = false;
|
||||
Sleep = true;
|
||||
Config = false;
|
||||
|
||||
Reset = true;
|
||||
Reset = true;
|
||||
|
||||
//Power* pwr = dynamic_cast<Power*>(Port);
|
||||
//if(pwr) pwr->ChangePower(level);
|
||||
|
@ -144,16 +144,16 @@ void ShunCom::ChangePower(int level)
|
|||
// 引发数据到达事件
|
||||
uint ShunCom::OnReceive(Buffer& bs, void* param)
|
||||
{
|
||||
if(Led) Led->Write(1000);
|
||||
if (Led) Led->Write(1000);
|
||||
|
||||
//debug_printf("zigbee接收\r\n");
|
||||
//bs.Show(true);
|
||||
|
||||
if(!AddrLength)
|
||||
return ITransport::OnReceive(bs, param);
|
||||
if (!AddrLength)
|
||||
return ITransport::OnReceive(bs, param);
|
||||
|
||||
// 取出地址
|
||||
byte* addr = bs.GetBuffer();
|
||||
byte* addr = bs.GetBuffer();
|
||||
Buffer bs2(addr + AddrLength, bs.Length() - AddrLength);
|
||||
//debug_printf("zigbee接收\r\n");
|
||||
//bs2.Show(true);
|
||||
|
@ -166,7 +166,7 @@ bool ShunCom::OnWriteEx(const Buffer& bs, const void* opt)
|
|||
//debug_printf("zigbee发送\r\n");
|
||||
//bs.Show(true);
|
||||
|
||||
if(!AddrLength || !opt) return OnWrite(bs);
|
||||
if (!AddrLength || !opt) return OnWrite(bs);
|
||||
// 加入地址
|
||||
ByteArray bs2;
|
||||
bs2.Copy(0, opt, AddrLength);
|
||||
|
@ -181,22 +181,22 @@ bool ShunCom::OnWriteEx(const Buffer& bs, const void* opt)
|
|||
// 进入配置模式
|
||||
bool ShunCom::EnterConfig()
|
||||
{
|
||||
if(!Open()) return false;
|
||||
if (!Open()) return false;
|
||||
|
||||
Sys.Sleep(2000);
|
||||
|
||||
Config = true;
|
||||
Config = true;
|
||||
Sys.Sleep(1000);
|
||||
Config = false;
|
||||
Config = false;
|
||||
|
||||
ByteArray rs1;
|
||||
|
||||
// 清空串口缓冲区
|
||||
while(true)
|
||||
while (true)
|
||||
{
|
||||
ByteArray rs1;
|
||||
Read(rs1);
|
||||
if(rs1.Length() == 0) break;
|
||||
if (rs1.Length() == 0) break;
|
||||
}
|
||||
|
||||
Sys.Sleep(1000);
|
||||
|
@ -206,59 +206,59 @@ bool ShunCom::EnterConfig()
|
|||
// 退出配置模式
|
||||
void ShunCom::ExitConfig()
|
||||
{
|
||||
if(!Open()) return;
|
||||
if (!Open()) return;
|
||||
|
||||
ShunComMessage msg(0x0041);
|
||||
msg.Length = 1;
|
||||
msg.Data[0] = 0x00;
|
||||
msg.Length = 1;
|
||||
msg.Data[0] = 0x00;
|
||||
|
||||
MemoryStream ms;
|
||||
auto buf = msg.ToArray(ms);
|
||||
//debug_printf("ShunComs重启生效\r\n");
|
||||
//buf.Show();
|
||||
Write(buf);
|
||||
// debug_printf("\r\n");
|
||||
// debug_printf("\r\n");
|
||||
|
||||
}
|
||||
void ShunCom::PrintSrc(bool flag)
|
||||
{
|
||||
ShunComMessage msg(0x0921);
|
||||
if(flag)
|
||||
if (flag)
|
||||
{
|
||||
msg.Set(0x040E,(byte)2);
|
||||
msg.Set(0x040E, (byte)2);
|
||||
}
|
||||
else
|
||||
{
|
||||
msg.Set(0x040E,(byte)1);
|
||||
msg.Set(0x040E, (byte)1);
|
||||
}
|
||||
MemoryStream ms;
|
||||
auto buf = msg.ToArray(ms);
|
||||
//debug_printf("ShunCom设置源地址\r\n");
|
||||
//buf.Show();
|
||||
Write(buf);
|
||||
// debug_printf("\r\n");
|
||||
// debug_printf("\r\n");
|
||||
|
||||
}
|
||||
|
||||
void ShunCom::EraConfig()
|
||||
{
|
||||
if(!Open()) return;
|
||||
if (!Open()) return;
|
||||
|
||||
ShunComMessage msg(0x0921);
|
||||
msg.Set(0x0003,(byte)02);
|
||||
msg.Set(0x0003, (byte)02);
|
||||
|
||||
MemoryStream ms;
|
||||
auto buf = msg.ToArray(ms);
|
||||
//debug_printf("ShunCom擦除组网信息\r\n");
|
||||
buf.Show();
|
||||
//Write(buf);
|
||||
//debug_printf("\r\n");
|
||||
//debug_printf("\r\n");
|
||||
}
|
||||
|
||||
// 读取配置信息
|
||||
void ShunCom::ShowConfig()
|
||||
{
|
||||
if(!Open()) return;
|
||||
if (!Open()) return;
|
||||
|
||||
ShunComMessage msg(0x1521);
|
||||
|
||||
|
@ -271,7 +271,7 @@ void ShunCom::ShowConfig()
|
|||
|
||||
ByteArray bs;
|
||||
Read(bs);
|
||||
debug_printf("ShunCom配置信息\r\n");
|
||||
debug_printf("ShunCom配置信息\r\n");
|
||||
bs.Show(true);
|
||||
}
|
||||
|
||||
|
@ -285,7 +285,7 @@ void ShunCom::SetDevice(byte kind)
|
|||
//debug_printf("ShunCom配置设备类型\r\n");
|
||||
//buf.Show();
|
||||
Write(buf);
|
||||
// debug_printf("\r\n");
|
||||
// debug_printf("\r\n");
|
||||
}
|
||||
|
||||
// 设置无线频点,注意大小端,ShunCom是小端存储
|
||||
|
@ -296,11 +296,11 @@ void ShunCom::SetChannel(byte chn)
|
|||
msg.Set(0x0084, (uint)(0x01 << chn));
|
||||
|
||||
MemoryStream ms;
|
||||
auto buf=msg.ToArray(ms);
|
||||
auto buf = msg.ToArray(ms);
|
||||
//debug_printf("ShunCom配置无线频点\r\n");
|
||||
//buf.Show();
|
||||
Write(buf);
|
||||
// debug_printf("\r\n");
|
||||
// debug_printf("\r\n");
|
||||
}
|
||||
|
||||
// 进入配置PanID,同一网络PanID必须相同
|
||||
|
@ -314,7 +314,7 @@ void ShunCom::SetPanID(ushort id)
|
|||
//debug_printf("ShunCom配置PanID\r\n");
|
||||
//buf.Show();
|
||||
Write(buf);
|
||||
// debug_printf("\r\n");
|
||||
// debug_printf("\r\n");
|
||||
}
|
||||
|
||||
// 设置发送模式00为广播、01为主从模式、02为点对点模式
|
||||
|
@ -328,7 +328,7 @@ void ShunCom::SetSend(byte mode)
|
|||
//debug_printf("ShunCom配置设备主从模式\r\n");
|
||||
//buf.Show();
|
||||
Write(buf);
|
||||
// debug_printf("\r\n");
|
||||
// debug_printf("\r\n");
|
||||
}
|
||||
//还原zigBee默认配置
|
||||
void ShunCom::ShunComReset()
|
||||
|
@ -345,57 +345,57 @@ void ShunCom::ShunComReset()
|
|||
}
|
||||
ShunComMessage::ShunComMessage(ushort code)
|
||||
{
|
||||
Frame = 0xFE;
|
||||
Code = code;
|
||||
Length = 0;
|
||||
Frame = 0xFE;
|
||||
Code = code;
|
||||
Length = 0;
|
||||
}
|
||||
|
||||
bool ShunComMessage::Read(Stream& ms)
|
||||
{
|
||||
byte magic = ms.ReadByte();
|
||||
if(magic != 0xFE) return false;
|
||||
byte magic = ms.ReadByte();
|
||||
if (magic != 0xFE) return false;
|
||||
|
||||
Frame = magic;
|
||||
Length = ms.ReadByte();
|
||||
Code = ms.ReadUInt16();
|
||||
|
||||
Frame = magic;
|
||||
Length = ms.ReadByte();
|
||||
Code = ms.ReadUInt16();
|
||||
|
||||
Buffer bs(Data, sizeof(Data));
|
||||
if(Length > 4)
|
||||
if (Length > 4)
|
||||
{
|
||||
Kind = ms.ReadUInt16();
|
||||
Size = _REV16(ms.ReadUInt16());
|
||||
Kind = ms.ReadUInt16();
|
||||
Size = _REV16(ms.ReadUInt16());
|
||||
assert(2 + 2 + Size == Length, "ShunComMessage::Read");
|
||||
//ms.Read(Data, 0, Size);
|
||||
bs.SetLength(Size);
|
||||
}
|
||||
else if(Length > 0)
|
||||
else if (Length > 0)
|
||||
{
|
||||
//ms.Read(Data, 0, Length);
|
||||
bs.SetLength(Length);
|
||||
}
|
||||
ms.Read(bs);
|
||||
// 不做校验检查,不是很重要
|
||||
Checksum = ms.ReadByte();
|
||||
Checksum = ms.ReadByte();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ShunComMessage::Write(Stream& ms) const
|
||||
{
|
||||
byte* p = ms.Current();
|
||||
byte* p = ms.Current();
|
||||
ms.Write(Frame);
|
||||
ms.Write(Length);
|
||||
ms.Write(Code);
|
||||
|
||||
Buffer bs((void*)Data, sizeof(Data));
|
||||
if(Length > 4)
|
||||
if (Length > 4)
|
||||
{
|
||||
ms.Write(Kind);
|
||||
ms.Write((ushort)_REV16(Size));
|
||||
//ms.Write(Data, 0, Size);
|
||||
bs.SetLength(Size);
|
||||
}
|
||||
else if(Length > 0)
|
||||
else if (Length > 0)
|
||||
{
|
||||
//ms.Write(Data, 0, Length);
|
||||
bs.SetLength(Length);
|
||||
|
@ -403,8 +403,8 @@ void ShunComMessage::Write(Stream& ms) const
|
|||
ms.Write(bs);
|
||||
//ms.Write(Checksum);
|
||||
// 计算校验
|
||||
byte sum = 0;
|
||||
while(p++ < ms.Current()-1) sum^= *p;
|
||||
byte sum = 0;
|
||||
while (p++ < ms.Current() - 1) sum ^= *p;
|
||||
ms.Write(sum);
|
||||
|
||||
}
|
||||
|
@ -425,41 +425,41 @@ ByteArray ShunComMessage::ToArray() const
|
|||
ByteArray ShunComMessage::ToArray(Stream& ms)
|
||||
{
|
||||
Write(ms);
|
||||
ByteArray bs(ms.GetBuffer(), ms.Position());
|
||||
ByteArray bs(ms.GetBuffer(), ms.Position());
|
||||
return bs;
|
||||
}
|
||||
void ShunComMessage::Set(ushort kind, const Buffer& bs)
|
||||
{
|
||||
Kind = kind;
|
||||
Kind = kind;
|
||||
bs.CopyTo(0, Data, -1);
|
||||
|
||||
Length = 2 + 2 + bs.Length();
|
||||
Length = 2 + 2 + bs.Length();
|
||||
}
|
||||
|
||||
void ShunComMessage::Set(ushort kind, byte dat)
|
||||
{
|
||||
Kind = kind;
|
||||
Data[0] = dat;
|
||||
Size = 1;
|
||||
Length = 2 + 2 + Size;
|
||||
Kind = kind;
|
||||
Data[0] = dat;
|
||||
Size = 1;
|
||||
Length = 2 + 2 + Size;
|
||||
}
|
||||
|
||||
void ShunComMessage::Set(ushort kind, ushort dat)
|
||||
{
|
||||
Kind = kind;
|
||||
Data[0] = dat;
|
||||
Data[1] = dat >> 8;
|
||||
Size = 2;
|
||||
Length = 2 + 2 + Size;
|
||||
Kind = kind;
|
||||
Data[0] = dat & 0xFF;
|
||||
Data[1] = dat >> 8;
|
||||
Size = 2;
|
||||
Length = 2 + 2 + Size;
|
||||
}
|
||||
|
||||
void ShunComMessage::Set(ushort kind, uint dat)
|
||||
{
|
||||
Kind = kind;
|
||||
Data[0] = dat;
|
||||
Data[1] = dat >> 8;
|
||||
Data[2] = dat >> 16;
|
||||
Data[3] = dat >> 24;
|
||||
Size = 4;
|
||||
Length = 2 + 2 + Size;
|
||||
Kind = kind;
|
||||
Data[0] = dat;
|
||||
Data[1] = dat >> 8;
|
||||
Data[2] = dat >> 16;
|
||||
Data[3] = dat >> 24;
|
||||
Size = 4;
|
||||
Length = 2 + 2 + Size;
|
||||
}
|
||||
|
|
|
@ -189,7 +189,7 @@ INROOT void TSys::Delay(int us) const
|
|||
if(Sys.Started && us != 0 && us >= 1000)
|
||||
{
|
||||
bool cancel = false;
|
||||
auto ct = Task::Scheduler()->ExecuteForWait(us / 1000, cancel);
|
||||
int ct = Task::Scheduler()->ExecuteForWait(us / 1000, cancel);
|
||||
ct *= 1000;
|
||||
|
||||
if(ct >= us) return;
|
||||
|
@ -253,6 +253,7 @@ extern "C"
|
|||
return rs;
|
||||
}
|
||||
|
||||
#ifndef _MSC_VER
|
||||
/* 重载fputc可以让用户程序使用printf函数 */
|
||||
int fputc(int ch, FILE *f)
|
||||
{
|
||||
|
@ -267,6 +268,7 @@ extern "C"
|
|||
#endif
|
||||
return ch;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/****************系统跟踪****************/
|
||||
|
|
|
@ -36,9 +36,9 @@ public:
|
|||
// 字典名值对操作
|
||||
IDictionary GetAll() const;
|
||||
bool Set(const IDictionary& dic);
|
||||
|
||||
|
||||
private:
|
||||
uint _p; // 写入时的位置
|
||||
int _p; // 写入时的位置
|
||||
Stream* _s;
|
||||
bool _canWrite;
|
||||
};
|
||||
|
|
|
@ -70,16 +70,16 @@ bool UTPacket::AndPort(byte id, UTPort* port)
|
|||
|
||||
bool UTPacket::PressTMsg(const Pair& args, Stream& result)
|
||||
{
|
||||
Buffer buff = args.Get("Data"); // 引用源数据区,后面使用要小心,不能修改任何值。
|
||||
if (buff.Length() < sizeof(PacketHead))return false;
|
||||
auto buff = args.Get("Data"); // 引用源数据区,后面使用要小心,不能修改任何值。
|
||||
if (buff.Length() < (int)sizeof(PacketHead))return false;
|
||||
|
||||
PacketHead * head = (PacketHead*)buff.GetBuffer();
|
||||
auto head = (PacketHead*)buff.GetBuffer();
|
||||
void * tail = head + buff.Length(); // 结尾位置
|
||||
UTPort * port = nullptr;
|
||||
UTPort* port = nullptr;
|
||||
|
||||
while (head < tail)
|
||||
{
|
||||
if ((uint)head->Next() >(uint)tail)break; // 要么越界,要么数据包错
|
||||
if ((uint)head->Next() > (uint)tail)break; // 要么越界,要么数据包错
|
||||
|
||||
MemoryStream resms;
|
||||
if (!Ports.TryGetValue((uint)head->PortID, port) || !port) // 获取端口
|
||||
|
|
|
@ -14,7 +14,7 @@ public:
|
|||
ArpSession(const IPAddress& ip) : IP(ip) { }
|
||||
};
|
||||
|
||||
ArpSession* _ArpSession = nullptr;
|
||||
ArpSession* _ArpSession = nullptr;
|
||||
|
||||
ArpSocket::ArpSocket(TinyIP* tip) : TinySocket(tip, IP_NONE)
|
||||
{
|
||||
|
@ -28,7 +28,7 @@ ArpSocket::ArpSocket(TinyIP* tip) : TinySocket(tip, IP_NONE)
|
|||
|
||||
ArpSocket::~ArpSocket()
|
||||
{
|
||||
if(_Arps) delete _Arps;
|
||||
if (_Arps) delete _Arps;
|
||||
_Arps = nullptr;
|
||||
}
|
||||
|
||||
|
@ -38,8 +38,8 @@ bool ArpSocket::Process(IP_HEADER& ip, Stream& ms)
|
|||
//uint size = ms->Position + sizeof(ARP_HEADER);
|
||||
//if(ms->Length < size) ms->Length = size;
|
||||
|
||||
auto arp = ms.Retrieve<ARP_HEADER>();
|
||||
if(!arp) return false;
|
||||
auto arp = ms.Retrieve<ARP_HEADER>();
|
||||
if (!arp) return false;
|
||||
|
||||
/*
|
||||
当封装的ARP报文在以太网上传输时,硬件类型字段赋值为0x0100,标识硬件为以太网硬件;
|
||||
|
@ -49,7 +49,7 @@ bool ArpSocket::Process(IP_HEADER& ip, Stream& ms)
|
|||
*/
|
||||
|
||||
// 如果是Arp响应包,自动加入缓存
|
||||
if(arp->Option == 0x0200)
|
||||
if (arp->Option == 0x0200)
|
||||
{
|
||||
IPAddress addr = arp->SrcIP;
|
||||
MacAddress mac = arp->SrcMac.Value();
|
||||
|
@ -57,7 +57,7 @@ bool ArpSocket::Process(IP_HEADER& ip, Stream& ms)
|
|||
Add(addr, mac);
|
||||
}
|
||||
// 别人的响应包这里收不到呀,还是把请求包也算上吧
|
||||
if(arp->Option == 0x0100)
|
||||
if (arp->Option == 0x0100)
|
||||
{
|
||||
IPAddress addr = arp->SrcIP;
|
||||
MacAddress mac = arp->SrcMac.Value();
|
||||
|
@ -66,14 +66,14 @@ bool ArpSocket::Process(IP_HEADER& ip, Stream& ms)
|
|||
}
|
||||
|
||||
// 是否发给本机。
|
||||
if(arp->DestIP != Tip->IP.Value) return true;
|
||||
if (arp->DestIP != Tip->IP.Value) return true;
|
||||
|
||||
if(arp->Option == 0x0200 && _ArpSession && _ArpSession->IP.Value == arp->SrcIP)
|
||||
if (arp->Option == 0x0200 && _ArpSession && _ArpSession->IP.Value == arp->SrcIP)
|
||||
{
|
||||
_ArpSession->Mac = arp->SrcMac.Value();
|
||||
_ArpSession->Handle.Set();
|
||||
_ArpSession = nullptr;
|
||||
|
||||
_ArpSession = nullptr;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -85,9 +85,9 @@ bool ArpSocket::Process(IP_HEADER& ip, Stream& ms)
|
|||
assert_param(arp->ProtocolLength == 4);
|
||||
//assert_param(arp->Option == 0x0100);
|
||||
|
||||
if(arp->Option == 0x0100)
|
||||
if (arp->Option == 0x0100)
|
||||
debug_printf("ARP::Request For ");
|
||||
else if(arp->Option == 0x0200)
|
||||
else if (arp->Option == 0x0200)
|
||||
debug_printf("ARP::Response For ");
|
||||
else
|
||||
debug_printf("ARP::Unkown %d For ", arp->Option);
|
||||
|
@ -101,7 +101,7 @@ bool ArpSocket::Process(IP_HEADER& ip, Stream& ms)
|
|||
#endif
|
||||
|
||||
// 仅处理ARP请求
|
||||
if(arp->Option != 0x0100) return true;
|
||||
if (arp->Option != 0x0100) return true;
|
||||
|
||||
// 目标Mac地址
|
||||
MacAddress mac = arp->SrcMac.Value();
|
||||
|
@ -110,9 +110,9 @@ bool ArpSocket::Process(IP_HEADER& ip, Stream& ms)
|
|||
arp->Option = 0x0200;
|
||||
// 来源IP和Mac作为目的地址
|
||||
arp->DestMac = arp->SrcMac;
|
||||
arp->SrcMac = Tip->Mac.Value;
|
||||
arp->DestIP = arp->SrcIP;
|
||||
arp->SrcIP = Tip->IP.Value;
|
||||
arp->SrcMac = Tip->Mac.Value;
|
||||
arp->DestIP = arp->SrcIP;
|
||||
arp->SrcIP = Tip->IP.Value;
|
||||
|
||||
#if NET_DEBUG
|
||||
debug_printf("ARP::Response To ");
|
||||
|
@ -141,13 +141,13 @@ bool ArpSocket::Request(const IPAddress& ip, MacAddress& mac, int timeout)
|
|||
// 构造请求包
|
||||
arp->Option = 0x0100;
|
||||
arp->DestMac = 0;
|
||||
arp->SrcMac = Tip->Mac;
|
||||
arp->DestIP = ip.Value;
|
||||
arp->SrcIP = Tip->IP.Value;
|
||||
arp->SrcMac = Tip->Mac;
|
||||
arp->DestIP = ip.Value;
|
||||
arp->SrcIP = Tip->IP.Value;
|
||||
|
||||
#if NET_DEBUG
|
||||
debug_printf("ARP::Request To ");
|
||||
if(timeout <= 0) debug_printf("异步 ");
|
||||
if (timeout <= 0) debug_printf("异步 ");
|
||||
IPAddress(arp->DestIP).Show();
|
||||
debug_printf(" size=%d\r\n", sizeof(ARP_HEADER));
|
||||
#endif
|
||||
|
@ -155,13 +155,13 @@ bool ArpSocket::Request(const IPAddress& ip, MacAddress& mac, int timeout)
|
|||
Tip->SendEthernet(ETH_ARP, MacAddress::Empty(), (byte*)arp, sizeof(ARP_HEADER));
|
||||
|
||||
// 如果没有超时时间,表示异步请求,不用等待结果
|
||||
if(timeout <= 0) return false;
|
||||
if (timeout <= 0) return false;
|
||||
|
||||
// 等待响应
|
||||
ArpSession ss(ip);
|
||||
_ArpSession = &ss;
|
||||
|
||||
if(!ss.Handle.WaitOne(timeout)) return false;
|
||||
if (!ss.Handle.WaitOne(timeout)) return false;
|
||||
|
||||
mac = ss.Mac;
|
||||
return true;
|
||||
|
@ -170,28 +170,28 @@ bool ArpSocket::Request(const IPAddress& ip, MacAddress& mac, int timeout)
|
|||
bool ArpSocket::Resolve(const IPAddress& ip, MacAddress& mac)
|
||||
{
|
||||
mac = MacAddress::Empty();
|
||||
if(ip.IsAny()) return true;
|
||||
if (ip.IsAny()) return true;
|
||||
|
||||
mac = MacAddress::Full();
|
||||
if(ip.IsAny() || Tip->IsBroadcast(ip)) return true;
|
||||
if (ip.IsAny() || Tip->IsBroadcast(ip)) return true;
|
||||
|
||||
IPAddress dest = ip;
|
||||
auto dest = ip;
|
||||
// 如果不在本子网,那么应该找网关的Mac
|
||||
//if((ip & Tip->Mask) != (Tip->IP & Tip->Mask)) ip = Tip->Gateway;
|
||||
if(dest.GetSubNet(Tip->Mask) != Tip->IP.GetSubNet(Tip->Mask)) dest = Tip->Gateway;
|
||||
if (dest.GetSubNet(Tip->Mask) != Tip->IP.GetSubNet(Tip->Mask)) dest = Tip->Gateway;
|
||||
|
||||
ARP_ITEM* item = nullptr; // 匹配项
|
||||
if(_Arps)
|
||||
if (_Arps)
|
||||
{
|
||||
uint sNow = Sys.Ms() >> 10; // 当前时间,秒
|
||||
int sNow = (int)(Sys.Ms() >> 10); // 当前时间,秒
|
||||
// 在表中查找
|
||||
for(int i=0; i<Count; i++)
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
ARP_ITEM* arp = &_Arps[i];
|
||||
if(arp->IP == dest.Value)
|
||||
if (arp->IP == dest.Value)
|
||||
{
|
||||
// 如果未过期,则直接使用。否则重新请求
|
||||
if(arp->Time > sNow)
|
||||
if (arp->Time > sNow)
|
||||
{
|
||||
mac = arp->Mac.Value();
|
||||
return true;
|
||||
|
@ -205,9 +205,9 @@ bool ArpSocket::Resolve(const IPAddress& ip, MacAddress& mac)
|
|||
|
||||
// 找不到则发送Arp请求。如果有旧值,则使用异步请求即可
|
||||
bool rs = Request(dest, mac, item ? 0 : 1);
|
||||
if(!rs)
|
||||
if (!rs)
|
||||
{
|
||||
if(!item) return false;
|
||||
if (!item) return false;
|
||||
|
||||
mac = item->Mac.Value();
|
||||
#if NET_DEBUG
|
||||
|
@ -225,9 +225,9 @@ bool ArpSocket::Resolve(const IPAddress& ip, MacAddress& mac)
|
|||
|
||||
void ArpSocket::Add(const IPAddress& ip, const MacAddress& mac)
|
||||
{
|
||||
if(ip.IsAny() || ip.IsBroadcast()) return;
|
||||
if (ip.IsAny() || ip.IsBroadcast()) return;
|
||||
|
||||
if(!_Arps)
|
||||
if (!_Arps)
|
||||
{
|
||||
_Arps = new ARP_ITEM[Count];
|
||||
Buffer(_Arps, sizeof(ARP_ITEM) * Count).Clear();
|
||||
|
@ -236,15 +236,15 @@ void ArpSocket::Add(const IPAddress& ip, const MacAddress& mac)
|
|||
ARP_ITEM* item = nullptr;
|
||||
ARP_ITEM* empty = nullptr;
|
||||
// 在表中查找项
|
||||
for(int i=0; i<Count; i++)
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
ARP_ITEM* arp = &_Arps[i];
|
||||
if(arp->IP == ip.Value)
|
||||
if (arp->IP == ip.Value)
|
||||
{
|
||||
item = arp;
|
||||
break;
|
||||
}
|
||||
if(!empty && arp->IP == 0)
|
||||
if (!empty && arp->IP == 0)
|
||||
{
|
||||
empty = arp;
|
||||
break;
|
||||
|
@ -252,7 +252,7 @@ void ArpSocket::Add(const IPAddress& ip, const MacAddress& mac)
|
|||
}
|
||||
|
||||
#if NET_DEBUG
|
||||
if(!item)
|
||||
if (!item)
|
||||
{
|
||||
debug_printf("Arp::Add(");
|
||||
ip.Show();
|
||||
|
@ -263,17 +263,17 @@ void ArpSocket::Add(const IPAddress& ip, const MacAddress& mac)
|
|||
#endif
|
||||
|
||||
// 如果没有匹配项,则使用空项
|
||||
if(!item) item = empty;
|
||||
if (!item) item = empty;
|
||||
// 如果也没有空项,表示满了,那就替换最老的一个
|
||||
if(!item)
|
||||
if (!item)
|
||||
{
|
||||
uint oldTime = 0xFFFFFFFF;
|
||||
int oldTime = 0x7FFFFFFF;
|
||||
// 在表中查找最老项用于替换
|
||||
for(int i=0; i<Count; i++)
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
ARP_ITEM* arp = &_Arps[i];
|
||||
auto arp = &_Arps[i];
|
||||
// 找最老的一个,待会如果需要覆盖,就先覆盖它。避开网关
|
||||
if(arp->Time < oldTime && arp->IP != Tip->Gateway.Value)
|
||||
if (arp->Time < oldTime && arp->IP != Tip->Gateway.Value)
|
||||
{
|
||||
oldTime = arp->Time;
|
||||
item = arp;
|
||||
|
@ -286,9 +286,9 @@ void ArpSocket::Add(const IPAddress& ip, const MacAddress& mac)
|
|||
#endif
|
||||
}
|
||||
|
||||
uint sNow = Sys.Ms() >> 10; // 当前时间,秒
|
||||
int sNow = (int)(Sys.Ms() >> 10); // 当前时间,秒
|
||||
// 保存
|
||||
item->IP = ip.Value;
|
||||
item->Mac = mac;
|
||||
item->Time = sNow + 60; // 默认过期时间1分钟
|
||||
item->IP = ip.Value;
|
||||
item->Mac = mac;
|
||||
item->Time = sNow + 60; // 默认过期时间1分钟
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ private:
|
|||
{
|
||||
IPAddr IP;
|
||||
MacAddr Mac;
|
||||
uint Time; // 生存时间,秒
|
||||
int Time; // 生存时间,秒
|
||||
}ARP_ITEM;
|
||||
|
||||
ARP_ITEM* _Arps; // Arp表,动态分配
|
||||
|
|
|
@ -16,16 +16,16 @@ public:
|
|||
|
||||
PingSession(IPAddress& ip, ushort id, ushort seq)
|
||||
{
|
||||
Address = ip;
|
||||
Identifier = id;
|
||||
Sequence = seq;
|
||||
Address = ip;
|
||||
Identifier = id;
|
||||
Sequence = seq;
|
||||
}
|
||||
|
||||
bool Check(IPAddress& remote, ICMP_HEADER* icmp)
|
||||
{
|
||||
if(remote != Address) return false;
|
||||
if(Identifier != icmp->Identifier) return false;
|
||||
if(Sequence != icmp->Sequence) return false;
|
||||
if (remote != Address) return false;
|
||||
if (Identifier != icmp->Identifier) return false;
|
||||
if (Sequence != icmp->Sequence) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -43,26 +43,26 @@ IcmpSocket::IcmpSocket(TinyIP* tip) : TinySocket(tip, IP_ICMP)
|
|||
|
||||
bool IcmpSocket::Process(IP_HEADER& ip, Stream& ms)
|
||||
{
|
||||
auto icmp = ms.Retrieve<ICMP_HEADER>();
|
||||
if(!icmp) return false;
|
||||
auto icmp = ms.Retrieve<ICMP_HEADER>();
|
||||
if (!icmp) return false;
|
||||
|
||||
IPAddress remote = ip.SrcIP;
|
||||
IPAddress remote = ip.SrcIP;
|
||||
|
||||
// 检查有没有会话等待
|
||||
if(icmp->Type == 0 && _IcmpSession != nullptr && _IcmpSession->Check(remote, icmp))
|
||||
if (icmp->Type == 0 && _IcmpSession != nullptr && _IcmpSession->Check(remote, icmp))
|
||||
{
|
||||
_IcmpSession->Handle.Set();
|
||||
_IcmpSession = nullptr;
|
||||
_IcmpSession = nullptr;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint len = ms.Remain();
|
||||
if(OnPing)
|
||||
if (OnPing)
|
||||
{
|
||||
// 返回值指示是否向对方发送数据包
|
||||
bool rs = OnPing(*this, *icmp, icmp->Next(), len);
|
||||
if(!rs) return true;
|
||||
if (!rs) return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -71,20 +71,20 @@ bool IcmpSocket::Process(IP_HEADER& ip, Stream& ms)
|
|||
debug_printf("Ping From "); // 打印发方的ip
|
||||
else
|
||||
debug_printf("Ping Reply "); // 打印发方的ip*/
|
||||
switch(icmp->Type)
|
||||
switch (icmp->Type)
|
||||
{
|
||||
case 0:
|
||||
debug_printf("Ping Reply ");
|
||||
break;
|
||||
case 3:
|
||||
debug_printf("ICMP::应用端口不可达 ");
|
||||
break;
|
||||
case 8:
|
||||
debug_printf("Ping From ");
|
||||
break;
|
||||
default:
|
||||
debug_printf("ICMP%d ", icmp->Type);
|
||||
break;
|
||||
case 0:
|
||||
debug_printf("Ping Reply ");
|
||||
break;
|
||||
case 3:
|
||||
debug_printf("ICMP::应用端口不可达 ");
|
||||
break;
|
||||
case 8:
|
||||
debug_printf("Ping From ");
|
||||
break;
|
||||
default:
|
||||
debug_printf("ICMP%d ", icmp->Type);
|
||||
break;
|
||||
}
|
||||
remote.Show();
|
||||
debug_printf(" Payload=%d ", len);
|
||||
|
@ -95,41 +95,41 @@ bool IcmpSocket::Process(IP_HEADER& ip, Stream& ms)
|
|||
}
|
||||
|
||||
// 只处理ECHO请求
|
||||
if(icmp->Type != 8) return true;
|
||||
if (icmp->Type != 8) return true;
|
||||
|
||||
icmp->Type = 0; // 响应
|
||||
// 因为仅仅改变类型,因此我们能够提前修正校验码
|
||||
icmp->Checksum += 0x08;
|
||||
|
||||
// 这里不能直接用sizeof(ICMP_HEADER),而必须用len,因为ICMP包后面一般有附加数据
|
||||
Tip->SendIP(IP_ICMP, remote, (byte*)icmp, icmp->Size() + len);
|
||||
Tip->SendIP(IP_ICMP, remote, (byte*)icmp, icmp->Size() + len);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Ping目的地址,附带a~z重复的负载数据
|
||||
bool IcmpSocket::Ping(IPAddress& ip, uint payloadLength)
|
||||
bool IcmpSocket::Ping(IPAddress& ip, int payloadLength)
|
||||
{
|
||||
byte buf[sizeof(ETH_HEADER) + sizeof(IP_HEADER) + sizeof(ICMP_HEADER) + 64];
|
||||
// 注意,此时指针位于0,而内容长度为缓冲区长度
|
||||
Stream ms(buf, ArrayLength(buf));
|
||||
ms.Seek(sizeof(ETH_HEADER) + sizeof(IP_HEADER));
|
||||
|
||||
auto icmp = ms.Retrieve<ICMP_HEADER>();
|
||||
auto icmp = ms.Retrieve<ICMP_HEADER>();
|
||||
icmp->Init(true);
|
||||
|
||||
icmp->Type = 8;
|
||||
icmp->Code = 0;
|
||||
|
||||
// 限定最大长度64
|
||||
if(payloadLength > 64) payloadLength = 64;
|
||||
for(int i=0, k=0; i<payloadLength; i++, k++)
|
||||
if (payloadLength > 64) payloadLength = 64;
|
||||
for (int i = 0, k = 0; i < payloadLength; i++, k++)
|
||||
{
|
||||
if(k >= 23) k-=23;
|
||||
if (k >= 23) k -= 23;
|
||||
ms.Write((byte)('a' + k));
|
||||
}
|
||||
|
||||
ushort now = Sys.Ms();
|
||||
ushort now = (ushort)Sys.Ms();
|
||||
ushort id = _REV16(Sys.ID[0]);
|
||||
ushort seq = _REV16(now);
|
||||
icmp->Identifier = id;
|
||||
|
@ -151,11 +151,11 @@ bool IcmpSocket::Ping(IPAddress& ip, uint payloadLength)
|
|||
_IcmpSession = &ps;
|
||||
|
||||
// 等待响应
|
||||
bool rs = ps.Handle.WaitOne(1000);
|
||||
bool rs = ps.Handle.WaitOne(1000);
|
||||
|
||||
#if NET_DEBUG
|
||||
uint cost = ct.Elapsed() / 1000;
|
||||
if(rs)
|
||||
if (rs)
|
||||
debug_printf(" 成功 %dms\r\n", cost);
|
||||
else
|
||||
debug_printf(" 失败 %dms\r\n", cost);
|
||||
|
|
|
@ -14,11 +14,11 @@ public:
|
|||
virtual bool Process(IP_HEADER& ip, Stream& ms);
|
||||
|
||||
// 收到Ping请求时触发,传递结构体和负载数据长度。返回值指示是否向对方发送数据包
|
||||
typedef bool (*PingHandler)(IcmpSocket& socket, ICMP_HEADER& icmp, byte* buf, uint len);
|
||||
typedef bool(*PingHandler)(IcmpSocket& socket, ICMP_HEADER& icmp, byte* buf, int len);
|
||||
PingHandler OnPing;
|
||||
|
||||
// Ping目的地址,附带a~z重复的负载数据
|
||||
bool Ping(IPAddress& ip, uint payloadLength = 32);
|
||||
bool Ping(IPAddress& ip, int payloadLength = 32);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -56,7 +56,7 @@ uint TinyIP::Fetch(Stream& ms)
|
|||
Buffer bs(ms.Current(), ms.Remain());
|
||||
int len = _port->Read(bs);
|
||||
// 如果缓冲器里面没有数据则转入下一次循环
|
||||
if(len < sizeof(ETH_HEADER)) return 0;
|
||||
if(len < (int)sizeof(ETH_HEADER)) return 0;
|
||||
|
||||
// 位置指针后移
|
||||
//ms.Seek(-len);
|
||||
|
@ -223,7 +223,7 @@ bool TinyIP::OnOpen()
|
|||
task->MaxDeepth = 2; // 以太网允许重入,因为有时候在接收里面等待下一次接收
|
||||
|
||||
#if NET_DEBUG
|
||||
uint us = Sys.Ms() - _StartTime;
|
||||
int us = (int)(Sys.Ms() - _StartTime);
|
||||
debug_printf("TinyIP Ready! Cost:%dms\r\n\r\n", us / 1000);
|
||||
#endif
|
||||
|
||||
|
|
|
@ -75,7 +75,7 @@ void UdpSocket::OnProcess(IP_HEADER& ip, UDP_HEADER& udp, Stream& ms)
|
|||
byte* data = ms.Current();
|
||||
//uint len = ms.Remain();
|
||||
// 计算标称的数据长度
|
||||
uint len = _REV16(udp.Length) - sizeof(UDP_HEADER);
|
||||
int len = _REV16(udp.Length) - sizeof(UDP_HEADER);
|
||||
assert(len <= ms.Remain(), "UDP数据包不完整");
|
||||
|
||||
// 触发ITransport接口事件
|
||||
|
|
|
@ -9,8 +9,8 @@
|
|||
class DataMessage
|
||||
{
|
||||
public:
|
||||
uint Offset;
|
||||
uint Length;
|
||||
int Offset;
|
||||
int Length;
|
||||
|
||||
DataMessage(const Message& msg, Stream* dest, bool isTokenMsg = false);
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#include "PingMessage.h"
|
||||
#include "DataMessage.h"
|
||||
|
||||
TinyClient* TinyClient::Current = nullptr;
|
||||
TinyClient* TinyClient::Current = nullptr;
|
||||
|
||||
static void TinyClientTask(void* param);
|
||||
//static void TinyClientReset();
|
||||
|
@ -17,64 +17,64 @@ static void TinyClientTask(void* param);
|
|||
|
||||
TinyClient::TinyClient(TinyController* control)
|
||||
{
|
||||
Control = control;
|
||||
Control->GetKey = Delegate2<byte, Buffer&>(&TinyClient::GetDeviceKey, this);
|
||||
Control = control;
|
||||
Control->GetKey = Delegate2<byte, Buffer&>(&TinyClient::GetDeviceKey, this);
|
||||
|
||||
Opened = false;
|
||||
Joining = false;
|
||||
Server = 0;
|
||||
Type = Sys.Code;
|
||||
Opened = false;
|
||||
Joining = false;
|
||||
Server = 0;
|
||||
Type = Sys.Code;
|
||||
|
||||
LastSend = 0;
|
||||
LastActive = 0;
|
||||
LastSend = 0;
|
||||
LastActive = 0;
|
||||
|
||||
Received = nullptr;
|
||||
Param = nullptr;
|
||||
Received = nullptr;
|
||||
Param = nullptr;
|
||||
|
||||
Cfg = nullptr;
|
||||
Cfg = nullptr;
|
||||
|
||||
_TaskID = 0;
|
||||
_TaskID = 0;
|
||||
|
||||
NextReport = 0;
|
||||
Encryption = false;
|
||||
NextReport = 0;
|
||||
Encryption = false;
|
||||
}
|
||||
|
||||
void TinyClient::Open()
|
||||
{
|
||||
if(Opened) return;
|
||||
if (Opened) return;
|
||||
|
||||
// 使用另一个强类型参数的委托,事件函数里面不再需要做类型
|
||||
Control->Received = Delegate2<TinyMessage&, TinyController&>(&TinyClient::OnReceive, this);
|
||||
Control->Received = Delegate2<TinyMessage&, TinyController&>(&TinyClient::OnReceive, this);
|
||||
//Control->Param = this;
|
||||
|
||||
TranID = (int)Sys.Ms();
|
||||
TranID = (int)Sys.Ms();
|
||||
|
||||
if(Cfg->Address > 0 && Cfg->Server > 0)
|
||||
if (Cfg->Address > 0 && Cfg->Server > 0)
|
||||
{
|
||||
Control->Address = Cfg->Address;
|
||||
Server = Cfg->Server;
|
||||
|
||||
//Password.Load(Cfg->Password, ArrayLength(Cfg->Password));
|
||||
Password = Cfg->Pass;
|
||||
Password = Cfg->Pass;
|
||||
}
|
||||
|
||||
HardCrc = Crc::Hash16(Buffer(Sys.ID, 16));
|
||||
if(Sys.Ver > 1) Encryption = true;
|
||||
HardCrc = Crc::Hash16(Buffer(Sys.ID, 16));
|
||||
if (Sys.Ver > 1) Encryption = true;
|
||||
|
||||
Control->Mode = 0; // 客户端只接收自己的消息
|
||||
Control->Open();
|
||||
|
||||
int t = 5000;
|
||||
if(Server) t = Cfg->PingTime * 1000;
|
||||
if(t < 1000) t = 1000;
|
||||
int t = 5000;
|
||||
if (Server) t = Cfg->PingTime * 1000;
|
||||
if (t < 1000) t = 1000;
|
||||
_TaskID = Sys.AddTask(TinyClientTask, this, 0, t, "微网客户端");
|
||||
|
||||
Opened = true;
|
||||
Opened = true;
|
||||
}
|
||||
|
||||
void TinyClient::Close()
|
||||
{
|
||||
if(!Opened) return;
|
||||
if (!Opened) return;
|
||||
|
||||
Sys.RemoveTask(_TaskID);
|
||||
|
||||
|
@ -83,7 +83,7 @@ void TinyClient::Close()
|
|||
|
||||
Control->Close();
|
||||
|
||||
Opened = false;
|
||||
Opened = false;
|
||||
}
|
||||
|
||||
/******************************** 收发中心 ********************************/
|
||||
|
@ -93,10 +93,10 @@ bool TinyClient::Send(TinyMessage& msg)
|
|||
assert(Control, "令牌控制器未初始化");
|
||||
|
||||
// 未组网时,禁止发其它消息。组网消息通过广播发出,不经过这里
|
||||
if(!Server) return false;
|
||||
if (!Server) return false;
|
||||
|
||||
// 设置网关地址
|
||||
if(!msg.Dest) msg.Dest = Server;
|
||||
if (!msg.Dest) msg.Dest = Server;
|
||||
|
||||
return Control->Send(msg);
|
||||
}
|
||||
|
@ -107,9 +107,9 @@ bool TinyClient::Reply(TinyMessage& msg)
|
|||
assert(Control, "令牌控制器未初始化");
|
||||
|
||||
// 未组网时,禁止发其它消息。组网消息通过广播发出,不经过这里
|
||||
if(!Server) return false;
|
||||
if (!Server) return false;
|
||||
|
||||
if(!msg.Dest) msg.Dest = Server;
|
||||
if (!msg.Dest) msg.Dest = Server;
|
||||
|
||||
return Control->Reply(msg);
|
||||
}
|
||||
|
@ -117,33 +117,33 @@ bool TinyClient::Reply(TinyMessage& msg)
|
|||
void TinyClient::OnReceive(TinyMessage& msg, TinyController& ctrl)
|
||||
{
|
||||
// 不是组网消息。不是被组网网关消息,不受其它消息设备控制.
|
||||
if(msg.Code != 0x01 && Server != msg.Src) return;
|
||||
if (msg.Code != 0x01 && Server != msg.Src) return;
|
||||
|
||||
if(msg.Src == Server) LastActive = Sys.Ms();
|
||||
if (msg.Src == Server) LastActive = Sys.Ms();
|
||||
|
||||
switch(msg.Code)
|
||||
switch (msg.Code)
|
||||
{
|
||||
case 0x01:
|
||||
OnJoin(msg);
|
||||
break;
|
||||
case 0x02:
|
||||
OnDisjoin(msg);
|
||||
break;
|
||||
case 0x03:
|
||||
OnPing(msg);
|
||||
break;
|
||||
case 0x05:
|
||||
case 0x15:
|
||||
OnRead(msg);
|
||||
break;
|
||||
case 0x06:
|
||||
case 0x16:
|
||||
OnWrite(msg);
|
||||
break;
|
||||
case 0x01:
|
||||
OnJoin(msg);
|
||||
break;
|
||||
case 0x02:
|
||||
OnDisjoin(msg);
|
||||
break;
|
||||
case 0x03:
|
||||
OnPing(msg);
|
||||
break;
|
||||
case 0x05:
|
||||
case 0x15:
|
||||
OnRead(msg);
|
||||
break;
|
||||
case 0x06:
|
||||
case 0x16:
|
||||
OnWrite(msg);
|
||||
break;
|
||||
}
|
||||
|
||||
// 消息转发
|
||||
if(Received) Received(this, msg, Param);
|
||||
if (Received) Received(this, msg, Param);
|
||||
}
|
||||
|
||||
/******************************** 数据区 ********************************/
|
||||
|
@ -154,26 +154,26 @@ void TinyClient::OnReceive(TinyMessage& msg, TinyController& ctrl)
|
|||
*/
|
||||
void TinyClient::OnRead(const TinyMessage& msg)
|
||||
{
|
||||
if(msg.Reply) return;
|
||||
if(msg.Length < 2) return;
|
||||
if (msg.Reply) return;
|
||||
if (msg.Length < 2) return;
|
||||
|
||||
auto rs = msg.CreateReply();
|
||||
auto ms = rs.ToStream();
|
||||
auto rs = msg.CreateReply();
|
||||
auto ms = rs.ToStream();
|
||||
|
||||
DataMessage dm(msg, &ms);
|
||||
|
||||
bool rt = true;
|
||||
if(dm.Offset < 64)
|
||||
rt = dm.ReadData(Store);
|
||||
else if(dm.Offset < 128)
|
||||
bool rt = true;
|
||||
if (dm.Offset < 64)
|
||||
rt = dm.ReadData(Store);
|
||||
else if (dm.Offset < 128)
|
||||
{
|
||||
dm.Offset -= 64;
|
||||
dm.Offset -= 64;
|
||||
Buffer bs(Cfg, Cfg->Length);
|
||||
rt = dm.ReadData(bs);
|
||||
rt = dm.ReadData(bs);
|
||||
}
|
||||
|
||||
rs.Error = !rt;
|
||||
rs.Length = ms.Position();
|
||||
rs.Error = !rt;
|
||||
rs.Length = ms.Position();
|
||||
|
||||
Reply(rs);
|
||||
}
|
||||
|
@ -185,34 +185,34 @@ void TinyClient::OnRead(const TinyMessage& msg)
|
|||
*/
|
||||
void TinyClient::OnWrite(const TinyMessage& msg)
|
||||
{
|
||||
if(msg.Reply) return;
|
||||
if(msg.Length < 2) return;
|
||||
if (msg.Reply) return;
|
||||
if (msg.Length < 2) return;
|
||||
|
||||
auto rs = msg.CreateReply();
|
||||
auto ms = rs.ToStream();
|
||||
auto rs = msg.CreateReply();
|
||||
auto ms = rs.ToStream();
|
||||
|
||||
DataMessage dm(msg, &ms);
|
||||
|
||||
bool rt = true;
|
||||
if(dm.Offset < 64)
|
||||
bool rt = true;
|
||||
if (dm.Offset < 64)
|
||||
{
|
||||
rt = dm.WriteData(Store, true);
|
||||
rt = dm.WriteData(Store, true);
|
||||
}
|
||||
else if(dm.Offset < 128)
|
||||
else if (dm.Offset < 128)
|
||||
{
|
||||
dm.Offset -= 64;
|
||||
dm.Offset -= 64;
|
||||
Buffer bs(Cfg, Cfg->Length);
|
||||
rt = dm.WriteData(bs, true);
|
||||
rt = dm.WriteData(bs, true);
|
||||
|
||||
Cfg->Save();
|
||||
}
|
||||
|
||||
rs.Error = !rt;
|
||||
rs.Length = ms.Position();
|
||||
rs.Error = !rt;
|
||||
rs.Length = ms.Position();
|
||||
|
||||
Reply(rs);
|
||||
|
||||
if(dm.Offset >= 64 && dm.Offset < 128)
|
||||
if (dm.Offset >= 64 && dm.Offset < 128)
|
||||
{
|
||||
debug_printf("\r\n 配置区被修改,200ms后重启\r\n");
|
||||
Sys.Sleep(200);
|
||||
|
@ -223,36 +223,36 @@ void TinyClient::OnWrite(const TinyMessage& msg)
|
|||
Sys.SetTask(_TaskID, true, 500);
|
||||
}
|
||||
|
||||
bool TinyClient::Report(uint offset, byte dat)
|
||||
bool TinyClient::Report(int offset, byte dat)
|
||||
{
|
||||
TinyMessage msg;
|
||||
msg.Code = 0x06;
|
||||
msg.Code = 0x06;
|
||||
|
||||
auto ms = msg.ToStream();
|
||||
ms.WriteEncodeInt(offset);
|
||||
ms.Write(dat);
|
||||
msg.Length = ms.Position();
|
||||
msg.Length = ms.Position();
|
||||
|
||||
return Send(msg);
|
||||
}
|
||||
|
||||
bool TinyClient::Report(uint offset, const Buffer& bs)
|
||||
bool TinyClient::Report(int offset, const Buffer& bs)
|
||||
{
|
||||
TinyMessage msg;
|
||||
msg.Code = 0x06;
|
||||
msg.Code = 0x06;
|
||||
|
||||
auto ms = msg.ToStream();
|
||||
ms.WriteEncodeInt(offset);
|
||||
ms.Write(bs);
|
||||
msg.Length = ms.Position();
|
||||
msg.Length = ms.Position();
|
||||
|
||||
return Send(msg);
|
||||
}
|
||||
|
||||
void TinyClient::ReportAsync(uint offset,uint length)
|
||||
void TinyClient::ReportAsync(int offset, int length)
|
||||
{
|
||||
//if(this == nullptr) return;
|
||||
if(offset + length >= Store.Data.Length()) return;
|
||||
if (offset + length >= Store.Data.Length()) return;
|
||||
|
||||
NextReport = offset;
|
||||
ReportLength = length;
|
||||
|
@ -265,17 +265,17 @@ void TinyClient::ReportAsync(uint offset,uint length)
|
|||
void TinyClientTask(void* param)
|
||||
{
|
||||
auto client = (TinyClient*)param;
|
||||
uint offset = client->NextReport;
|
||||
uint len = client->ReportLength;
|
||||
int offset = client->NextReport;
|
||||
int len = client->ReportLength;
|
||||
assert(offset == 0 || offset < 0x10, "自动上报偏移量异常!");
|
||||
|
||||
if(offset)
|
||||
if (offset)
|
||||
{
|
||||
// 检查索引,否则数组越界
|
||||
auto& bs = client->Store.Data;
|
||||
if(bs.Length() > offset + len)
|
||||
if (bs.Length() > offset + len)
|
||||
{
|
||||
if(len == 1)
|
||||
if (len == 1)
|
||||
client->Report(offset, bs[offset]);
|
||||
else
|
||||
{
|
||||
|
@ -286,8 +286,8 @@ void TinyClientTask(void* param)
|
|||
client->NextReport = 0;
|
||||
return;
|
||||
}
|
||||
if(client->Server == 0 || client->Joining) client->Join();
|
||||
if(client->Server != 0) client->Ping();
|
||||
if (client->Server == 0 || client->Joining) client->Join();
|
||||
if (client->Server != 0) client->Ping();
|
||||
}
|
||||
|
||||
void TinyClient::GetDeviceKey(byte id, Buffer& key)
|
||||
|
@ -313,10 +313,10 @@ void TinyClient::Join()
|
|||
|
||||
// 组网版本不是系统版本,而是为了做新旧版本组网消息兼容的版本号
|
||||
//dm.Version = Sys.Version;
|
||||
dm.Kind = Type;
|
||||
dm.Kind = Type;
|
||||
//dm.HardID.Copy(Sys.ID, 16);
|
||||
dm.HardID = Sys.ID;
|
||||
dm.TranID = TranID;
|
||||
dm.HardID = Sys.ID;
|
||||
dm.TranID = TranID;
|
||||
dm.WriteMessage(msg);
|
||||
//dm.Show(true);
|
||||
|
||||
|
@ -327,7 +327,7 @@ void TinyClient::Join()
|
|||
bool TinyClient::OnJoin(const TinyMessage& msg)
|
||||
{
|
||||
// 客户端只处理Discover响应
|
||||
if(!msg.Reply || msg.Error) return true;
|
||||
if (!msg.Reply || msg.Error) return true;
|
||||
|
||||
TS("TinyClient::OnJoin");
|
||||
|
||||
|
@ -337,34 +337,34 @@ bool TinyClient::OnJoin(const TinyMessage& msg)
|
|||
dm.Show(true);
|
||||
|
||||
// 校验不对
|
||||
if(TranID != dm.TranID)
|
||||
if (TranID != dm.TranID)
|
||||
{
|
||||
debug_printf("组网响应序列号 0x%04X 不等于内部序列号 0x%04X \r\n", dm.TranID, TranID);
|
||||
return true;
|
||||
}
|
||||
|
||||
Joining = false;
|
||||
Joining = false;
|
||||
|
||||
Cfg->SoftVer = dm.Version;
|
||||
Cfg->SoftVer = dm.Version;
|
||||
// 小于2的版本不加密
|
||||
if(dm.Version < 2) Encryption = false;
|
||||
if (dm.Version < 2) Encryption = false;
|
||||
|
||||
Cfg->Address = dm.Address;
|
||||
Control->Address = dm.Address;
|
||||
Password = dm.Password;
|
||||
Cfg->Pass = dm.Password;
|
||||
Cfg->Address = dm.Address;
|
||||
Control->Address = dm.Address;
|
||||
Password = dm.Password;
|
||||
Cfg->Pass = dm.Password;
|
||||
//Password.Copy(0, dm.Password, 0, -1);
|
||||
//Password.Save(Cfg->Password, ArrayLength(Cfg->Password));
|
||||
|
||||
// 记住服务端地址
|
||||
Server = dm.Server;
|
||||
Cfg->Server = dm.Server;
|
||||
if(Cfg->Channel != dm.Channel)
|
||||
Cfg->Server = dm.Server;
|
||||
if (Cfg->Channel != dm.Channel)
|
||||
{
|
||||
//todo 设置zigbee 通道
|
||||
//todo 设置zigbee 通道
|
||||
}
|
||||
Cfg->Channel = dm.Channel;
|
||||
Cfg->Speed = dm.Speed * 10;
|
||||
Cfg->Channel = dm.Channel;
|
||||
Cfg->Speed = dm.Speed * 10;
|
||||
|
||||
// 服务端组网密码,退网使用
|
||||
//Cfg->Mac[0] = dm.HardID.Length();
|
||||
|
@ -378,10 +378,10 @@ bool TinyClient::OnJoin(const TinyMessage& msg)
|
|||
#endif
|
||||
|
||||
// 取消Join任务,启动Ping任务
|
||||
ushort time = Cfg->PingTime;
|
||||
if(time < 5) time = 5;
|
||||
if(time > 60) time = 60;
|
||||
Cfg->PingTime = time;
|
||||
ushort time = Cfg->PingTime;
|
||||
if (time < 5) time = 5;
|
||||
if (time > 60) time = 60;
|
||||
Cfg->PingTime = (byte)time;
|
||||
Sys.SetTaskPeriod(_TaskID, time * 1000);
|
||||
|
||||
// 组网成功更新一次最后活跃时间
|
||||
|
@ -401,11 +401,11 @@ void TinyClient::DisJoin()
|
|||
TS("TinyClient::DisJoin");
|
||||
|
||||
TinyMessage msg;
|
||||
msg.Code = 2;
|
||||
msg.Code = 2;
|
||||
|
||||
auto ms = msg.ToStream();
|
||||
auto ms = msg.ToStream();
|
||||
ms.Write(HardCrc);
|
||||
msg.Length = ms.Position();
|
||||
msg.Length = ms.Position();
|
||||
|
||||
Send(msg);
|
||||
}
|
||||
|
@ -413,14 +413,14 @@ void TinyClient::DisJoin()
|
|||
// 离网
|
||||
bool TinyClient::OnDisjoin(const TinyMessage& msg)
|
||||
{
|
||||
if(msg.Length < 2) return false;
|
||||
if (msg.Length < 2) return false;
|
||||
|
||||
TS("TinyClient::OnDisJoin");
|
||||
|
||||
auto ms = msg.ToStream();
|
||||
ushort crc = ms.ReadUInt16();
|
||||
auto ms = msg.ToStream();
|
||||
ushort crc = ms.ReadUInt16();
|
||||
|
||||
if(crc != HardCrc)
|
||||
if (crc != HardCrc)
|
||||
{
|
||||
debug_printf("退网密码 0x%04X 不等于本地密码 0x%04X \r\n", crc, HardCrc);
|
||||
return false;
|
||||
|
@ -428,8 +428,8 @@ bool TinyClient::OnDisjoin(const TinyMessage& msg)
|
|||
|
||||
Cfg->Clear();
|
||||
|
||||
Sys.Sleep(3000);
|
||||
Sys.Reboot();
|
||||
Sys.Sleep(3000);
|
||||
Sys.Reboot();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -441,7 +441,7 @@ void TinyClient::Ping()
|
|||
|
||||
/*ushort off = (Cfg->OfflineTime)*5;
|
||||
//debug_printf(" TinyClient::Ping Cfg->OfflineTime:%d\r\n", Cfg->OfflineTime);
|
||||
ushort now = Sys.Seconds();
|
||||
ushort now = Sys.Seconds();
|
||||
|
||||
if(off < 10) off = 30;
|
||||
|
||||
|
@ -470,19 +470,19 @@ void TinyClient::Ping()
|
|||
|
||||
debug_printf("TinyClient::Ping");
|
||||
// 没有服务端时不要上报
|
||||
if(!Server) return;
|
||||
if (!Server) return;
|
||||
|
||||
// 30秒内发过数据,不再发送心跳
|
||||
if(LastSend > 0 && LastSend + 30000 > Sys.Ms()) return;
|
||||
if (LastSend > 0 && LastSend + 30000 > Sys.Ms()) return;
|
||||
|
||||
TinyMessage msg;
|
||||
msg.Code = 3;
|
||||
|
||||
auto ms = msg.ToStream();
|
||||
PingMessage pm;
|
||||
pm.MaxSize = ms.Capacity();
|
||||
pm.MaxSize = ms.Capacity();
|
||||
uint len = Control->Port->MaxSize - TinyMessage::MinSize;
|
||||
if(pm.MaxSize > len) pm.MaxSize = len;
|
||||
if (pm.MaxSize > len) pm.MaxSize = len;
|
||||
|
||||
pm.WriteData(ms, 0x01, Store.Data);
|
||||
pm.WriteHardCrc(ms, HardCrc);
|
||||
|
@ -496,35 +496,35 @@ void TinyClient::Ping()
|
|||
bool TinyClient::OnPing(const TinyMessage& msg)
|
||||
{
|
||||
// 仅处理来自网关的消息
|
||||
if(Server == 0 || Server != msg.Src) return true;
|
||||
if (Server == 0 || Server != msg.Src) return true;
|
||||
|
||||
TS("TinyClient::OnPing");
|
||||
|
||||
// 忽略响应消息
|
||||
if(!msg.Reply)return true;
|
||||
if (!msg.Reply)return true;
|
||||
|
||||
if(msg.Src != Server) return true;
|
||||
if (msg.Src != Server) return true;
|
||||
|
||||
// 处理消息
|
||||
auto ms = msg.ToStream();
|
||||
auto ms = msg.ToStream();
|
||||
PingMessage pm;
|
||||
pm.MaxSize = Control->Port->MaxSize - TinyMessage::MinSize;
|
||||
pm.MaxSize = Control->Port->MaxSize - TinyMessage::MinSize;
|
||||
// 子操作码
|
||||
while(ms.Remain())
|
||||
while (ms.Remain())
|
||||
{
|
||||
switch(ms.ReadByte())
|
||||
switch (ms.ReadByte())
|
||||
{
|
||||
case 0x04:
|
||||
case 0x04:
|
||||
{
|
||||
uint seconds = 0;
|
||||
if (pm.ReadTime(ms, seconds))
|
||||
{
|
||||
uint seconds = 0;
|
||||
if(pm.ReadTime(ms, seconds))
|
||||
{
|
||||
((TTime&)Time).SetTime(seconds);
|
||||
}
|
||||
break;
|
||||
((TTime&)Time).SetTime(seconds);
|
||||
}
|
||||
default:
|
||||
break;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -42,16 +42,16 @@ public:
|
|||
|
||||
static TinyClient* Current;
|
||||
|
||||
// 数据区
|
||||
// 数据区
|
||||
public:
|
||||
DataStore Store; // 数据存储区
|
||||
|
||||
bool Report(uint offset, byte dat);
|
||||
bool Report(uint offset, const Buffer& bs);
|
||||
bool Report(int offset, byte dat);
|
||||
bool Report(int offset, const Buffer& bs);
|
||||
|
||||
uint NextReport; // 下次上报偏移,0不动
|
||||
uint ReportLength; // 下次上报数据长度
|
||||
void ReportAsync(uint offset,uint length = 1);
|
||||
int NextReport; // 下次上报偏移,0不动
|
||||
int ReportLength; // 下次上报数据长度
|
||||
void ReportAsync(int offset, int length = 1);
|
||||
|
||||
private:
|
||||
uint _TaskID;
|
||||
|
@ -62,7 +62,7 @@ private:
|
|||
|
||||
void GetDeviceKey(byte id, Buffer& key);
|
||||
|
||||
// 常用系统级消息
|
||||
// 常用系统级消息
|
||||
public:
|
||||
// 组网
|
||||
ushort TranID; // 组网会话
|
||||
|
|
|
@ -17,7 +17,7 @@ DeviceBody::DeviceBody(BodyManagement* mgmt)
|
|||
|
||||
bool DeviceBody::SetSecDaStor(uint addr, byte * buf, int len)
|
||||
{
|
||||
if (addr < Store.Data.Length() || addr > 0x8000000)
|
||||
if (addr < (uint)Store.Data.Length() || addr > 0x8000000)
|
||||
{
|
||||
debug_printf("DeviceBody::SetSecDaStor 地址设置不合法\r\n");
|
||||
return false;
|
||||
|
@ -31,7 +31,7 @@ bool DeviceBody::SetSecDaStor(uint addr, byte * buf, int len)
|
|||
|
||||
bool DeviceBody::SetSecDaStor(DataStore * store)
|
||||
{
|
||||
if (store->VirAddrBase + store->Data.Length() > 0x8000000 || store->VirAddrBase < Store.Data.Length())
|
||||
if (store->VirAddrBase + store->Data.Length() > 0x8000000 || store->VirAddrBase < (uint)Store.Data.Length())
|
||||
{
|
||||
debug_printf(" DeviceBody::SetSecDaStor 虚拟地址不正确\r\n");
|
||||
}
|
||||
|
@ -121,7 +121,7 @@ void DeviceBody::OnWrite(const TokenMessage & msg)
|
|||
dm.Offset -= 64;
|
||||
//rt = dm.WriteData(dv.GetConfig(), false);
|
||||
}
|
||||
else if(Store2 && dm.Offset > Store2->VirAddrBase)
|
||||
else if(Store2 && (uint)dm.Offset > Store2->VirAddrBase)
|
||||
{
|
||||
rt = dm.WriteData(*Store2, false);
|
||||
}
|
||||
|
@ -156,7 +156,7 @@ void DeviceBody::OnRead(const TokenMessage & msg)
|
|||
//Buffer bs(Cfg, Cfg->Length);
|
||||
//rt = dm.ReadData(bs);
|
||||
}
|
||||
else if (Store2 && dm.Offset > Store2->VirAddrBase)
|
||||
else if (Store2 && (uint)dm.Offset > Store2->VirAddrBase)
|
||||
{
|
||||
rt = dm.ReadData(*Store2);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue