#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 *m_pInternalBuff; RawPacketObject *m_pParent; vector *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 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 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 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 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 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);