拆分数据操作
This commit is contained in:
parent
7834ca5884
commit
bf804bdc78
|
@ -0,0 +1,53 @@
|
||||||
|
#include "DataMessage.h"
|
||||||
|
|
||||||
|
// 读取数据
|
||||||
|
bool ReadData(Stream& ms, const Array& bs, uint offset, uint len)
|
||||||
|
{
|
||||||
|
TS("DataMessage::ReadData");
|
||||||
|
|
||||||
|
int remain = bs.Length() - offset;
|
||||||
|
if(remain < 0)
|
||||||
|
{
|
||||||
|
ms.Write((byte)2);
|
||||||
|
ms.WriteEncodeInt(offset);
|
||||||
|
ms.WriteEncodeInt(len);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ms.WriteEncodeInt(offset);
|
||||||
|
if(len > remain) len = remain;
|
||||||
|
if(len > 0) ms.Write(bs.GetBuffer(), offset, len);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入数据
|
||||||
|
bool WriteData(Stream& ms, Array& bs, uint offset, Stream& ds)
|
||||||
|
{
|
||||||
|
TS("DataMessage::WriteData");
|
||||||
|
|
||||||
|
// 剩余可写字节数
|
||||||
|
uint len = ds.Remain();
|
||||||
|
int remain = bs.Length() - offset;
|
||||||
|
if(remain < 0)
|
||||||
|
{
|
||||||
|
ms.Write((byte)2);
|
||||||
|
ms.WriteEncodeInt(offset);
|
||||||
|
ms.WriteEncodeInt(len);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ms.WriteEncodeInt(offset);
|
||||||
|
|
||||||
|
if(len > remain) len = remain;
|
||||||
|
bs.Copy(ds.Current(), len);
|
||||||
|
ms.WriteEncodeInt(len);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
#ifndef __DataMessage_H__
|
||||||
|
#define __DataMessage_H__
|
||||||
|
|
||||||
|
#include "Stream.h"
|
||||||
|
|
||||||
|
// 数据消息
|
||||||
|
class DataMessage
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static bool ReadData(Stream& ms, const Array& bs, uint offset, uint len);
|
||||||
|
static bool WriteData(Stream& ms, Array& bs, uint offset, Stream& ds);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
#include "JoinMessage.h"
|
#include "JoinMessage.h"
|
||||||
#include "PingMessage.h"
|
#include "PingMessage.h"
|
||||||
|
#include "DataMessage.h"
|
||||||
|
|
||||||
TinyClient* TinyClient::Current = NULL;
|
TinyClient* TinyClient::Current = NULL;
|
||||||
|
|
||||||
|
@ -12,9 +13,6 @@ static void TinyClientTask(void* param);
|
||||||
//static void TinyClientReset();
|
//static void TinyClientReset();
|
||||||
static void GetDeviceKey(byte id, Array& key, void* param);
|
static void GetDeviceKey(byte id, Array& key, void* param);
|
||||||
|
|
||||||
static bool ReadData(Stream& ms, const Array& bs, uint offset, uint len);
|
|
||||||
static bool WriteData(Stream& ms, Array& bs, uint offset, Stream& ds);
|
|
||||||
|
|
||||||
/******************************** 初始化和开关 ********************************/
|
/******************************** 初始化和开关 ********************************/
|
||||||
|
|
||||||
TinyClient::TinyClient(TinyController* control)
|
TinyClient::TinyClient(TinyController* control)
|
||||||
|
@ -168,9 +166,9 @@ void TinyClient::OnRead(const TinyMessage& msg)
|
||||||
|
|
||||||
bool rt = true;
|
bool rt = true;
|
||||||
if(offset < 64)
|
if(offset < 64)
|
||||||
rt = ReadData(ms2, Store.Data, offset, len);
|
rt = DataMessage::ReadData(ms2, Store.Data, offset, len);
|
||||||
else if(offset < 128)
|
else if(offset < 128)
|
||||||
rt = ReadData(ms2, Array(Cfg, Cfg->Length), offset - 64, len);
|
rt = DataMessage::ReadData(ms2, Array(Cfg, Cfg->Length), offset - 64, len);
|
||||||
|
|
||||||
rs.Error = !rt;
|
rs.Error = !rt;
|
||||||
rs.Length = ms2.Position();
|
rs.Length = ms2.Position();
|
||||||
|
@ -198,11 +196,11 @@ void TinyClient::OnWrite(const TinyMessage& msg)
|
||||||
|
|
||||||
bool rt = true;
|
bool rt = true;
|
||||||
if(offset < 64)
|
if(offset < 64)
|
||||||
rt = WriteData(ms2, Store.Data, offset, ms);
|
rt = DataMessage::WriteData(ms2, Store.Data, offset, ms);
|
||||||
else if(offset < 128)
|
else if(offset < 128)
|
||||||
{
|
{
|
||||||
Array cs(Cfg, Cfg->Length);
|
Array cs(Cfg, Cfg->Length);
|
||||||
rt = WriteData(ms2, cs, offset - 64, ms);
|
rt = DataMessage::WriteData(ms2, cs, offset - 64, ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
rs.Error = !rt;
|
rs.Error = !rt;
|
||||||
|
@ -540,55 +538,3 @@ bool TinyClient::OnPing(const TinyMessage& msg)
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取数据
|
|
||||||
bool ReadData(Stream& ms, const Array& bs, uint offset, uint len)
|
|
||||||
{
|
|
||||||
TS("DataMessage::ReadData");
|
|
||||||
|
|
||||||
int remain = bs.Length() - offset;
|
|
||||||
if(remain < 0)
|
|
||||||
{
|
|
||||||
ms.Write((byte)2);
|
|
||||||
ms.WriteEncodeInt(offset);
|
|
||||||
ms.WriteEncodeInt(len);
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ms.WriteEncodeInt(offset);
|
|
||||||
if(len > remain) len = remain;
|
|
||||||
if(len > 0) ms.Write(bs.GetBuffer(), offset, len);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 写入数据
|
|
||||||
bool WriteData(Stream& ms, Array& bs, uint offset, Stream& ds)
|
|
||||||
{
|
|
||||||
TS("DataMessage::WriteData");
|
|
||||||
|
|
||||||
// 剩余可写字节数
|
|
||||||
uint len = ds.Remain();
|
|
||||||
int remain = bs.Length() - offset;
|
|
||||||
if(remain < 0)
|
|
||||||
{
|
|
||||||
ms.Write((byte)2);
|
|
||||||
ms.WriteEncodeInt(offset);
|
|
||||||
ms.WriteEncodeInt(len);
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ms.WriteEncodeInt(offset);
|
|
||||||
|
|
||||||
if(len > remain) len = remain;
|
|
||||||
bs.Copy(ds.Current(), len);
|
|
||||||
ms.WriteEncodeInt(len);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#include "JoinMessage.h"
|
#include "JoinMessage.h"
|
||||||
#include "PingMessage.h"
|
#include "PingMessage.h"
|
||||||
|
#include "DataMessage.h"
|
||||||
|
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#include "Drivers\ShunCom.h"
|
#include "Drivers\ShunCom.h"
|
||||||
|
@ -489,7 +490,7 @@ bool TinyServer::OnRead(TinyMessage& msg, Device& dv)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取响应,服务端趁机缓存一份。定时上报也是采用该指令。
|
// 读取响应,服务端趁机缓存一份。
|
||||||
bool TinyServer::OnReadReply(const TinyMessage& msg, Device& dv)
|
bool TinyServer::OnReadReply(const TinyMessage& msg, Device& dv)
|
||||||
{
|
{
|
||||||
if(!msg.Reply || msg.Error) return false;
|
if(!msg.Reply || msg.Error) return false;
|
||||||
|
@ -497,14 +498,11 @@ bool TinyServer::OnReadReply(const TinyMessage& msg, Device& dv)
|
||||||
|
|
||||||
TS("TinyServer::OnReadReply");
|
TS("TinyServer::OnReadReply");
|
||||||
|
|
||||||
//debug_printf("响应读取写入数据 \r\n") ;
|
auto ms = msg.ToStream();
|
||||||
// 起始地址为7位压缩编码整数
|
|
||||||
Stream ms = msg.ToStream();
|
|
||||||
uint offset = ms.ReadEncodeInt();
|
uint offset = ms.ReadEncodeInt();
|
||||||
|
|
||||||
auto bs = dv.GetStore();
|
auto bs = dv.GetStore();
|
||||||
int remain = bs.Capacity() - offset;
|
int remain = bs.Capacity() - offset;
|
||||||
|
|
||||||
if(remain < 0) return false;
|
if(remain < 0) return false;
|
||||||
|
|
||||||
uint len = ms.Remain();
|
uint len = ms.Remain();
|
||||||
|
@ -574,6 +572,7 @@ bool TinyServer::OnWrite(TinyMessage& msg, Device& dv)
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//设置zigbee的通道,2401无效
|
//设置zigbee的通道,2401无效
|
||||||
void TinyServer::SetChannel(byte channel)
|
void TinyServer::SetChannel(byte channel)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue