123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630 |
- #pragma once
- // 下列 ifdef 块是创建使从 DLL 导出更简单的
- // 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 DIOSPACKET_EXPORTS
- // 符号编译的。在使用此 DLL 的
- // 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将
- // DIOSPACKET_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的
- // 符号视为是被导出的。
- #ifndef DIOSPACKET_EXPORTS
- #ifdef _WIN64
- #ifdef _DEBUG
- #pragma comment(lib, "DiosPacketX64D.lib")
- #else
- #pragma comment(lib, "DiosPacketX64.lib")
- #endif
- #else
- #ifdef _DEBUG
- #pragma comment(lib, "DiosPacketD.lib")
- #else
- #pragma comment(lib, "DiosPacket.lib")
- #endif
- #endif
- #endif
- #ifdef DIOSPACKET_EXPORTS
- #define DIOSPACKET_API __declspec(dllexport)
- #define DIOSPACKET_C_API extern "C" __declspec(dllexport)
- #else
- #define DIOSPACKET_API __declspec(dllimport)
- #define DIOSPACKET_C_API extern "C" __declspec(dllimport)
- #endif
- #include "SCF.h"
- #include "DiosPackComDefs.h"
- //Raw packet object------------
- class DIOSPACKET_API RawPacketObjectExption
- {
- std::string *m_ExpContext;
- public:
- RawPacketObjectExption(const RawPacketObjectExption &tValue);
- RawPacketObjectExption(const char *pExp);
- ~RawPacketObjectExption(void);
- RawPacketObjectExption& operator = (const RawPacketObjectExption &tValue);
- const char *what();
- };
- class DIOSPACKET_API RawPacketObject
- {
- //有主key,由SetKey自行设定
- //如果无主key,add操作是并列填充到末尾,不能赋值&读取常量,只能读取子对象和添加子对象
- //either way,更新PacketSize!!
- //Parent存在的话,HasKey必须是对象
- //Parent不存在情况,比较复杂,必须SetKey命令显示调用则有KEY,其他情况假设无KEY.
- bool m_HasKey;
- bool m_InternalMalloc;
- vector<unsigned char> *m_pInternalBuff;
- RawPacketObject *m_pParent;
- vector<RawPacketObject*> *m_vec;
- unsigned char *m_pValue;
-
- //below three is main vals
- unsigned char *m_pPacketBuff;
- size_t m_PacketSize;
- size_t m_BuffSize;
- void AddPacketSize(size_t addsize);//called by subobject,update this,and call upper layer
- void NotifySubPacketIsObject();//called by subobject,update this,and call upper layer
- public:
- RawPacketObject(RawPacketObject *pParent);//malloc internal
- RawPacketObject(RawPacketObject *pParent,unsigned char *p, size_t packetsize, size_t buffsize);//using outside memory
- ~RawPacketObject();
- //construct
- RawPacketObject(const RawPacketObject &tValue);
- //init procedure---------------------------------------------------------
- BOOL HasKey(); //函数需要
- DIOSKEYTYPE GetKey();//需要实现
- size_t sizeOfRaw();//raw数据的真实长度
- size_t sizeOfObject();//object的个数
- size_t sizeOfBuff();
- void Clear();
- void delet();
- bool ReloadObject();
- void Reload(RawPacketObject *pParent, unsigned char *p, size_t packetsize, size_t buffsize);//重新构造对象树
- //add procedure----------------------------------------------------------
- //val is empty,reserve the space with zero
- void SetKey(DIOSKEYTYPE key);
- //normal add,静态或可变长值,但不是对象!!!
- template <class T>
- bool add_Template(DIOSKEYTYPE Key, T tValue)
- {
-
- unsigned char *pTempBuff;
- size_t addSize = 0;
- bool Flexible;
- size_t LengthSize = 0;
- size_t ContextSize = 0;
- if(m_PacketSize + sizeof(Key)+ sizeof(tValue) > sizeOfBuff())
- {
-
- if(m_InternalMalloc)
- {
- ReloadInternalMalloc(sizeof(Key)+sizeof(tValue));
- }
- else
- {
- throw RawPacketObjectExption("add size overflow ");
- return false;
- }
- }
-
- if(m_HasKey)
- {
- GetKeyFlexble(m_pPacketBuff,Flexible,LengthSize,ContextSize);
- if(!Flexible)
- {
- throw RawPacketObjectExption("fixed length can not use add function!");
- return false;
- }
- }
-
- pTempBuff = &(m_pPacketBuff[m_PacketSize]);
- memcpy(pTempBuff,&Key,sizeof(Key));
-
- GetKeyFlexble(pTempBuff,Flexible,LengthSize,ContextSize);
- if(Flexible)
- {
- throw RawPacketObjectExption("Wring key to satatic values ");
- return false;
- }
- else
- {
- addSize = sizeof(Key) + ContextSize;
-
- pTempBuff += sizeof(Key);
- memcpy(pTempBuff,&tValue,ContextSize);
- }
- if(!SetKeyLengthValue(addSize))
- {
- throw RawPacketObjectExption("update Length zore value error!");
- return false;
- }
-
- RawPacketObject * subRPO = new RawPacketObject(this, (unsigned char *)&(m_pPacketBuff[m_PacketSize]), addSize, addSize);
- (*m_vec).push_back(subRPO);
- AddPacketSize(addSize);
-
- return true;
- }
-
- bool add(DIOSKEYTYPE Key, bool tValue);
- bool add(DIOSKEYTYPE Key, char tValue);
- bool add(DIOSKEYTYPE Key, unsigned char tValue);
- bool add(DIOSKEYTYPE Key, short tValue);
- bool add(DIOSKEYTYPE Key, unsigned short tValue);
- bool add(DIOSKEYTYPE Key, int tValue);
- bool add(DIOSKEYTYPE Key, unsigned int tValue);
- bool add(DIOSKEYTYPE Key, long tValue);
- bool add(DIOSKEYTYPE Key, unsigned long tValue);
- bool add(DIOSKEYTYPE Key, long long tValue);
- bool add(DIOSKEYTYPE Key, unsigned long long tValue);
- bool add(DIOSKEYTYPE Key, float tValue);
- bool add(DIOSKEYTYPE Key, double tValue);
- bool add(DIOSKEYTYPE Key, const char* pValue,size_t size);
-
- void ReloadInternalMalloc(size_t sizeofadd);
- //normal add,静态或可变长值,是对象!!!
- bool add(RawPacketObject &dataobj);
-
- bool SetKeyLengthValue(size_t addSize);
- //赋值 procedure----------------------------------------------------------
- RawPacketObject& operator = (const RawPacketObject &tValue);
- template <class T1>
- bool SignedAssignmentProcess(T1 temp)
- {
- bool Flexible;
- size_t LengthSize = 0;
- size_t ContextSize = 0;
- GetKeyFlexble(m_pPacketBuff,Flexible,LengthSize,ContextSize);
- if(Flexible)
- {
- throw RawPacketObjectExption("Flexible length can not use = function!");
- return false;
- }
- else
- {
- switch(ContextSize)
- {
- case 8:
- {
- //64bit
- long long Local = (long long)temp;
- unsigned long long LocalLen = (unsigned long long)Local;
- memcpy(m_pValue,&LocalLen,sizeof(unsigned long long));
- break;
- }
- case 4:
- {
- //32bit
- int Local = (int)temp;
- unsigned int LocalLen = (unsigned int)Local;
- memcpy(m_pValue,&LocalLen,sizeof(unsigned int));
- break;
- }
- case 2:
- {
- //16bit
- short Local = (short)temp;
- unsigned short LocalLen = (unsigned short)Local;
- memcpy(m_pValue,&LocalLen,sizeof(unsigned short));
- break;
- }
- case 1:
- {
- //8bit
- char Local = (char)temp;
- unsigned char LocalLen = (unsigned char)Local;
- memcpy(m_pValue,&LocalLen,sizeof(unsigned char));
- break;
- }
- default:
- throw RawPacketObjectExption("ContextSize error!");
- break;
- }
- AddPacketSize(ContextSize);
- }
- return true;
- }
- template <class T2>
- bool UnSignedAssignmentProcess(T2 temp)
- {
- bool Flexible;
- size_t LengthSize = 0;
- size_t ContextSize = 0;
- GetKeyFlexble(m_pPacketBuff,Flexible,LengthSize,ContextSize);
- if(Flexible)
- {
- throw RawPacketObjectExption("Flexible length can not use = function!");
- return false;
- }
- else
- {
- switch(ContextSize)
- {
- case 8:
- {
- //64bit
- unsigned long long LocalLen = (unsigned long long)temp;
- memcpy(m_pValue,&LocalLen,sizeof(unsigned long long));
-
- break;
- }
- case 4:
- {
- //32bit
- unsigned int LocalLen = (unsigned int)temp;
- memcpy(m_pValue,&LocalLen,sizeof(unsigned int));
-
- break;
- }
- case 2:
- {
- //16bit
- unsigned short LocalLen = (unsigned short)temp;
- memcpy(m_pValue,&LocalLen,sizeof(unsigned short));
-
- break;
- }
- case 1:
- {
- //8bit
- unsigned char LocalLen = (unsigned char)temp;
- memcpy(m_pValue,&LocalLen,sizeof(unsigned char));
-
- break;
- }
- default:
- throw RawPacketObjectExption("ContextSize error!");
- break;
- }
- AddPacketSize(ContextSize);
-
- return true;
- }
- }
- RawPacketObject& operator = (const bool tValue);
- RawPacketObject& operator = (const char tValue);
- RawPacketObject& operator = (const unsigned char tValue);
- RawPacketObject& operator = (const short tValue);
- RawPacketObject& operator = (const unsigned short tValue);
- RawPacketObject& operator = (const int tValue);
- RawPacketObject& operator = (const unsigned int tValue);
- RawPacketObject& operator = (const long tValue);
- RawPacketObject& operator = (const unsigned long tValue);
- RawPacketObject& operator = (const long long tValue);
- RawPacketObject& operator = (const unsigned long long tValue);
- RawPacketObject& operator = (const float tValue);
- RawPacketObject& operator = (const double tValue);
- bool update(const char* pValue, size_t size);
- //读取 procedure----------------------------------------------------------
- RawPacketObject &operator [](size_t idx);
- RawPacketObject &operator [](int idx);
- RawPacketObject &operator [](DIOSKEYTYPE Key);
- template <class T3>
- bool ReadSignedValueByType(T3 & value)
- {
- if(m_HasKey)
- {
- bool Flexible;
- size_t LengthSize = 0;
- size_t ContextSize = 0;
- GetKeyFlexble(m_pPacketBuff,Flexible,LengthSize,ContextSize);
- if(Flexible)
- {
- throw RawPacketObjectExption("Flexible length can not use = function!");
- return false;
- }
- else
- {
- switch(ContextSize)
- {
- case 8:
- {
- //64bit
- unsigned long long LocalLen;
- memcpy(&LocalLen,m_pValue,sizeof(unsigned long long));
- long long Local = (long long)LocalLen;
- value = (T3)Local;
-
- break;
- }
- case 4:
- {
- //32bit
- unsigned int LocalLen;
- memcpy(&LocalLen,m_pValue,sizeof(unsigned int));
- int Local = (int)LocalLen;
- value = (T3)Local;
-
- break;
- }
- case 2:
- {
- //16bit
- unsigned short LocalLen;
- memcpy(&LocalLen,m_pValue,sizeof(unsigned short));
- short Local = (short)LocalLen;
- value = (T3)Local;
-
- break;
- }
- case 1:
- {
- //8bit
- unsigned char LocalLen;
- memcpy(&LocalLen,m_pValue,sizeof(unsigned char));
- char Local = (char)LocalLen;
- value = (T3)Local;
-
- break;
- }
- default:
- throw RawPacketObjectExption("ContextSize error!");
- break;
- }
- return true;
- }
- }
- else
- {
- throw RawPacketObjectExption("Do not have key, can not get value");
- return false;
- }
- }
-
- template <class T4>
- bool ReadUnSignedValueByType(T4 & value)
- {
- if(m_HasKey)
- {
- bool Flexible;
- size_t LengthSize = 0;
- size_t ContextSize = 0;
- GetKeyFlexble(m_pPacketBuff,Flexible,LengthSize,ContextSize);
- if(Flexible)
- {
- throw RawPacketObjectExption("Flexible length can not use = function!");
- return false;
- }
- else
- {
- switch(ContextSize)
- {
- case 8:
- {
- //64bit
- unsigned long long LocalLen;
- memcpy(&LocalLen,m_pValue,sizeof(unsigned long long));
- value = (T4)LocalLen;
-
- break;
- }
- case 4:
- {
- //32bit
- unsigned int LocalLen;
- memcpy(&LocalLen,m_pValue,sizeof(unsigned int));
- value = (T4)LocalLen;
-
- break;
- }
- case 2:
- {
- //16bit
- unsigned short LocalLen;
- memcpy(&LocalLen,m_pValue,sizeof(unsigned short));
- value = (T4)LocalLen;
-
- break;
- }
- case 1:
- {
- //8bit
- unsigned char LocalLen;
- memcpy(&LocalLen,m_pValue,sizeof(unsigned char));
- value = (T4)LocalLen;
-
- break;
- }
- default:
- throw RawPacketObjectExption("ContextSize error!");
- break;
- }
- return true;
- }
- }
- else
- {
- throw RawPacketObjectExption("Do not have key, can not get value");
- return false;
- }
- }
-
- operator bool();
- operator char();
- operator unsigned char();
- operator short();
- operator unsigned short();
- operator int();
- operator unsigned int();
- operator long();
- operator unsigned long();
- operator long long();
- operator unsigned long long();
- operator float();
- operator double();
- operator const char*();
- bool IsObject();
- //比较 procedure----------------------------------------------------------
- bool operator== (const RawPacketObject& ar) const;//未实现
- };
- //Dios短指令协议包
- class DIOSPACKET_API DiosPacket : public SCFPacket
- {
- protected:
- RawPacketObject *m_pDataObj;
- bool m_bReady;//Packet的有效性
- unsigned int m_LenzoneSize;//in byte
- bool m_CrcEffective;
- unsigned int m_Direction;
- unsigned int m_FreezeLenzone;//01:freeze
- bool UpdateLastEnv();
- public:
- DiosPacket();
- DiosPacket(char* pData, size_t Length);
- virtual ~DiosPacket();
- //normal access---------------------------
- //Head(1Byte:0xFF)
- //bool GetHeadInfo(char &HeadInfo);
- //bool SetHeadInfo(char &PhyDevIdx);
- //Ver(1Byte)
- //bool GetVerInfo(char &VerInfo);
- //bool SetVerInfo(char &VerInfo);
- //PhyIdx(1Byte)
- bool GetPhyDevIdx(PHYIDXTYPE &PhyDevIdx);
- bool SetPhyDevIdx(PHYIDXTYPE PhyDevIdx);
- //LogicIdx(1Byte)
- bool GetLogicDevIdx(LOGICIDXTYPE &LogicDevIdx);
- bool SetLogicDevIdx(LOGICIDXTYPE LogicDevIdx);
- //8bit
- //crcEffective Flag:1bit (After DataArea we have crc16,the Flag indicates the crc16 whether effective or not)
- bool GetDataCrcEffectiveFlag(bool &Effective);
- bool SetDataCrcEffectiveFlag(bool Effective);
- //targetdirection:2bit (00:REQ,01:RES,10:NOTIFY)
- bool GetTargetDirection(unsigned int &Direction);
- bool SetTargetDirection(unsigned int Direction);
- //dataLenArea Flag:2bit (01 means dataLen Area is not flexible)
- bool GetFreezeLenZoneFlag(unsigned int &FreezeFlag);
- bool SetFreezeLenZoneFlag(unsigned int FreezeFlag);
- //dataLenAreaSize:3bit (DataLenArea's size depends on this value. )
- bool GetLenZoneSize(unsigned int &LenzoneSize);//it might useless...
- bool SetLenZoneSize(unsigned int LenzoneSize);//it might useless...
- //command(1Byte)
- bool GetCommand(DIOSCMDTYPE &Cmd);
- bool SetCommand(DIOSCMDTYPE Cmd);
- bool GetRetcode(char &Ret);
- bool SetRetcode(char Ret);
- //DataLenArea(the size of DataLenArea is determined by DataLenAreaSize.The DataLenArea's size is 2^n(n = .111b means no DataLenArea))
- bool GetDataLen(unsigned long long &Len);
- bool SetDataLen(unsigned long long Len);
- //Head's Crc16
- bool GetHeadsCrc(unsigned short &headCrc16);
- bool SetHeadsCrc(unsigned short headCrc16);
- //dataArea
- char* GetData();
- RawPacketObject *GetDataObject();
- bool SetData(const char *pData, size_t dataLen);//update LenArea too...
- bool AddData(const char *pData, size_t dataLen);//update LenArea too...
- //dataArea Crc16
- bool GetDataCrc(unsigned short &dataCrc16);
- bool SetDataCrc(unsigned short dataCrc16);
- //normal access---------------------------
- //prepare
- bool InitPacket();//init packet
- bool ReadyPacket();//1.check head,2.make crc,3.check the data area?
- size_t GetHeadSize();
- unsigned long long GetDiosPacketSize();
- bool GetParam(DWORD Idx,char *&pAddress,size_t &ParamSize);//Idx:paramidx,pAddress:address after key,ParamSize:the size of the params context
- };
- DIOSPACKET_C_API PVOID MallocDiosPacket();//申请Dios PACKET
- DIOSPACKET_C_API void ReleaseDiosPacket(PVOID pObj);//释放Dios PACKET
- DIOSPACKET_C_API PACKET_RET DiosPacketParser(const char * pRecData, DWORD nLength, DWORD & PacketLength);
|