DiosPacket.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. #pragma once
  2. // 下列 ifdef 块是创建使从 DLL 导出更简单的
  3. // 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 DIOSPACKET_EXPORTS
  4. // 符号编译的。在使用此 DLL 的
  5. // 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将
  6. // DIOSPACKET_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的
  7. // 符号视为是被导出的。
  8. #ifndef DIOSPACKET_EXPORTS
  9. #ifdef _WIN64
  10. #ifdef _DEBUG
  11. #pragma comment(lib, "DiosPacketX64D.lib")
  12. #else
  13. #pragma comment(lib, "DiosPacketX64.lib")
  14. #endif
  15. #else
  16. #ifdef _DEBUG
  17. #pragma comment(lib, "DiosPacketD.lib")
  18. #else
  19. #pragma comment(lib, "DiosPacket.lib")
  20. #endif
  21. #endif
  22. #endif
  23. #ifdef DIOSPACKET_EXPORTS
  24. #define DIOSPACKET_API __declspec(dllexport)
  25. #define DIOSPACKET_C_API extern "C" __declspec(dllexport)
  26. #else
  27. #define DIOSPACKET_API __declspec(dllimport)
  28. #define DIOSPACKET_C_API extern "C" __declspec(dllimport)
  29. #endif
  30. #include "SCF.h"
  31. #include "DiosPackComDefs.h"
  32. //Raw packet object------------
  33. class DIOSPACKET_API RawPacketObjectExption
  34. {
  35. std::string *m_ExpContext;
  36. public:
  37. RawPacketObjectExption(const RawPacketObjectExption &tValue);
  38. RawPacketObjectExption(const char *pExp);
  39. ~RawPacketObjectExption(void);
  40. RawPacketObjectExption& operator = (const RawPacketObjectExption &tValue);
  41. const char *what();
  42. };
  43. class DIOSPACKET_API RawPacketObject
  44. {
  45. //有主key,由SetKey自行设定
  46. //如果无主key,add操作是并列填充到末尾,不能赋值&读取常量,只能读取子对象和添加子对象
  47. //either way,更新PacketSize!!
  48. //Parent存在的话,HasKey必须是对象
  49. //Parent不存在情况,比较复杂,必须SetKey命令显示调用则有KEY,其他情况假设无KEY.
  50. bool m_HasKey;
  51. bool m_InternalMalloc;
  52. vector<unsigned char> *m_pInternalBuff;
  53. RawPacketObject *m_pParent;
  54. vector<RawPacketObject*> *m_vec;
  55. unsigned char *m_pValue;
  56. //below three is main vals
  57. unsigned char *m_pPacketBuff;
  58. size_t m_PacketSize;
  59. size_t m_BuffSize;
  60. void AddPacketSize(size_t addsize);//called by subobject,update this,and call upper layer
  61. void NotifySubPacketIsObject();//called by subobject,update this,and call upper layer
  62. public:
  63. RawPacketObject(RawPacketObject *pParent);//malloc internal
  64. RawPacketObject(RawPacketObject *pParent,unsigned char *p, size_t packetsize, size_t buffsize);//using outside memory
  65. ~RawPacketObject();
  66. //construct
  67. RawPacketObject(const RawPacketObject &tValue);
  68. //init procedure---------------------------------------------------------
  69. BOOL HasKey(); //函数需要
  70. DIOSKEYTYPE GetKey();//需要实现
  71. size_t sizeOfRaw();//raw数据的真实长度
  72. size_t sizeOfObject();//object的个数
  73. size_t sizeOfBuff();
  74. void Clear();
  75. void delet();
  76. bool ReloadObject();
  77. void Reload(RawPacketObject *pParent, unsigned char *p, size_t packetsize, size_t buffsize);//重新构造对象树
  78. //add procedure----------------------------------------------------------
  79. //val is empty,reserve the space with zero
  80. void SetKey(DIOSKEYTYPE key);
  81. //normal add,静态或可变长值,但不是对象!!!
  82. template <class T>
  83. bool add_Template(DIOSKEYTYPE Key, T tValue)
  84. {
  85. unsigned char *pTempBuff;
  86. size_t addSize = 0;
  87. bool Flexible;
  88. size_t LengthSize = 0;
  89. size_t ContextSize = 0;
  90. if(m_PacketSize + sizeof(Key)+ sizeof(tValue) > sizeOfBuff())
  91. {
  92. if(m_InternalMalloc)
  93. {
  94. ReloadInternalMalloc(sizeof(Key)+sizeof(tValue));
  95. }
  96. else
  97. {
  98. throw RawPacketObjectExption("add size overflow ");
  99. return false;
  100. }
  101. }
  102. if(m_HasKey)
  103. {
  104. GetKeyFlexble(m_pPacketBuff,Flexible,LengthSize,ContextSize);
  105. if(!Flexible)
  106. {
  107. throw RawPacketObjectExption("fixed length can not use add function!");
  108. return false;
  109. }
  110. }
  111. pTempBuff = &(m_pPacketBuff[m_PacketSize]);
  112. memcpy(pTempBuff,&Key,sizeof(Key));
  113. GetKeyFlexble(pTempBuff,Flexible,LengthSize,ContextSize);
  114. if(Flexible)
  115. {
  116. throw RawPacketObjectExption("Wring key to satatic values ");
  117. return false;
  118. }
  119. else
  120. {
  121. addSize = sizeof(Key) + ContextSize;
  122. pTempBuff += sizeof(Key);
  123. memcpy(pTempBuff,&tValue,ContextSize);
  124. }
  125. if(!SetKeyLengthValue(addSize))
  126. {
  127. throw RawPacketObjectExption("update Length zore value error!");
  128. return false;
  129. }
  130. RawPacketObject * subRPO = new RawPacketObject(this, (unsigned char *)&(m_pPacketBuff[m_PacketSize]), addSize, addSize);
  131. (*m_vec).push_back(subRPO);
  132. AddPacketSize(addSize);
  133. return true;
  134. }
  135. bool add(DIOSKEYTYPE Key, bool tValue);
  136. bool add(DIOSKEYTYPE Key, char tValue);
  137. bool add(DIOSKEYTYPE Key, unsigned char tValue);
  138. bool add(DIOSKEYTYPE Key, short tValue);
  139. bool add(DIOSKEYTYPE Key, unsigned short tValue);
  140. bool add(DIOSKEYTYPE Key, int tValue);
  141. bool add(DIOSKEYTYPE Key, unsigned int tValue);
  142. bool add(DIOSKEYTYPE Key, long tValue);
  143. bool add(DIOSKEYTYPE Key, unsigned long tValue);
  144. bool add(DIOSKEYTYPE Key, long long tValue);
  145. bool add(DIOSKEYTYPE Key, unsigned long long tValue);
  146. bool add(DIOSKEYTYPE Key, float tValue);
  147. bool add(DIOSKEYTYPE Key, double tValue);
  148. bool add(DIOSKEYTYPE Key, const char* pValue,size_t size);
  149. void ReloadInternalMalloc(size_t sizeofadd);
  150. //normal add,静态或可变长值,是对象!!!
  151. bool add(RawPacketObject &dataobj);
  152. bool SetKeyLengthValue(size_t addSize);
  153. //赋值 procedure----------------------------------------------------------
  154. RawPacketObject& operator = (const RawPacketObject &tValue);
  155. template <class T1>
  156. bool SignedAssignmentProcess(T1 temp)
  157. {
  158. bool Flexible;
  159. size_t LengthSize = 0;
  160. size_t ContextSize = 0;
  161. GetKeyFlexble(m_pPacketBuff,Flexible,LengthSize,ContextSize);
  162. if(Flexible)
  163. {
  164. throw RawPacketObjectExption("Flexible length can not use = function!");
  165. return false;
  166. }
  167. else
  168. {
  169. switch(ContextSize)
  170. {
  171. case 8:
  172. {
  173. //64bit
  174. long long Local = (long long)temp;
  175. unsigned long long LocalLen = (unsigned long long)Local;
  176. memcpy(m_pValue,&LocalLen,sizeof(unsigned long long));
  177. break;
  178. }
  179. case 4:
  180. {
  181. //32bit
  182. int Local = (int)temp;
  183. unsigned int LocalLen = (unsigned int)Local;
  184. memcpy(m_pValue,&LocalLen,sizeof(unsigned int));
  185. break;
  186. }
  187. case 2:
  188. {
  189. //16bit
  190. short Local = (short)temp;
  191. unsigned short LocalLen = (unsigned short)Local;
  192. memcpy(m_pValue,&LocalLen,sizeof(unsigned short));
  193. break;
  194. }
  195. case 1:
  196. {
  197. //8bit
  198. char Local = (char)temp;
  199. unsigned char LocalLen = (unsigned char)Local;
  200. memcpy(m_pValue,&LocalLen,sizeof(unsigned char));
  201. break;
  202. }
  203. default:
  204. throw RawPacketObjectExption("ContextSize error!");
  205. break;
  206. }
  207. AddPacketSize(ContextSize);
  208. }
  209. return true;
  210. }
  211. template <class T2>
  212. bool UnSignedAssignmentProcess(T2 temp)
  213. {
  214. bool Flexible;
  215. size_t LengthSize = 0;
  216. size_t ContextSize = 0;
  217. GetKeyFlexble(m_pPacketBuff,Flexible,LengthSize,ContextSize);
  218. if(Flexible)
  219. {
  220. throw RawPacketObjectExption("Flexible length can not use = function!");
  221. return false;
  222. }
  223. else
  224. {
  225. switch(ContextSize)
  226. {
  227. case 8:
  228. {
  229. //64bit
  230. unsigned long long LocalLen = (unsigned long long)temp;
  231. memcpy(m_pValue,&LocalLen,sizeof(unsigned long long));
  232. break;
  233. }
  234. case 4:
  235. {
  236. //32bit
  237. unsigned int LocalLen = (unsigned int)temp;
  238. memcpy(m_pValue,&LocalLen,sizeof(unsigned int));
  239. break;
  240. }
  241. case 2:
  242. {
  243. //16bit
  244. unsigned short LocalLen = (unsigned short)temp;
  245. memcpy(m_pValue,&LocalLen,sizeof(unsigned short));
  246. break;
  247. }
  248. case 1:
  249. {
  250. //8bit
  251. unsigned char LocalLen = (unsigned char)temp;
  252. memcpy(m_pValue,&LocalLen,sizeof(unsigned char));
  253. break;
  254. }
  255. default:
  256. throw RawPacketObjectExption("ContextSize error!");
  257. break;
  258. }
  259. AddPacketSize(ContextSize);
  260. return true;
  261. }
  262. }
  263. RawPacketObject& operator = (const bool tValue);
  264. RawPacketObject& operator = (const char tValue);
  265. RawPacketObject& operator = (const unsigned char tValue);
  266. RawPacketObject& operator = (const short tValue);
  267. RawPacketObject& operator = (const unsigned short tValue);
  268. RawPacketObject& operator = (const int tValue);
  269. RawPacketObject& operator = (const unsigned int tValue);
  270. RawPacketObject& operator = (const long tValue);
  271. RawPacketObject& operator = (const unsigned long tValue);
  272. RawPacketObject& operator = (const long long tValue);
  273. RawPacketObject& operator = (const unsigned long long tValue);
  274. RawPacketObject& operator = (const float tValue);
  275. RawPacketObject& operator = (const double tValue);
  276. bool update(const char* pValue, size_t size);
  277. //读取 procedure----------------------------------------------------------
  278. RawPacketObject &operator [](size_t idx);
  279. RawPacketObject &operator [](int idx);
  280. RawPacketObject &operator [](DIOSKEYTYPE Key);
  281. template <class T3>
  282. bool ReadSignedValueByType(T3 & value)
  283. {
  284. if(m_HasKey)
  285. {
  286. bool Flexible;
  287. size_t LengthSize = 0;
  288. size_t ContextSize = 0;
  289. GetKeyFlexble(m_pPacketBuff,Flexible,LengthSize,ContextSize);
  290. if(Flexible)
  291. {
  292. throw RawPacketObjectExption("Flexible length can not use = function!");
  293. return false;
  294. }
  295. else
  296. {
  297. switch(ContextSize)
  298. {
  299. case 8:
  300. {
  301. //64bit
  302. unsigned long long LocalLen;
  303. memcpy(&LocalLen,m_pValue,sizeof(unsigned long long));
  304. long long Local = (long long)LocalLen;
  305. value = (T3)Local;
  306. break;
  307. }
  308. case 4:
  309. {
  310. //32bit
  311. unsigned int LocalLen;
  312. memcpy(&LocalLen,m_pValue,sizeof(unsigned int));
  313. int Local = (int)LocalLen;
  314. value = (T3)Local;
  315. break;
  316. }
  317. case 2:
  318. {
  319. //16bit
  320. unsigned short LocalLen;
  321. memcpy(&LocalLen,m_pValue,sizeof(unsigned short));
  322. short Local = (short)LocalLen;
  323. value = (T3)Local;
  324. break;
  325. }
  326. case 1:
  327. {
  328. //8bit
  329. unsigned char LocalLen;
  330. memcpy(&LocalLen,m_pValue,sizeof(unsigned char));
  331. char Local = (char)LocalLen;
  332. value = (T3)Local;
  333. break;
  334. }
  335. default:
  336. throw RawPacketObjectExption("ContextSize error!");
  337. break;
  338. }
  339. return true;
  340. }
  341. }
  342. else
  343. {
  344. throw RawPacketObjectExption("Do not have key, can not get value");
  345. return false;
  346. }
  347. }
  348. template <class T4>
  349. bool ReadUnSignedValueByType(T4 & value)
  350. {
  351. if(m_HasKey)
  352. {
  353. bool Flexible;
  354. size_t LengthSize = 0;
  355. size_t ContextSize = 0;
  356. GetKeyFlexble(m_pPacketBuff,Flexible,LengthSize,ContextSize);
  357. if(Flexible)
  358. {
  359. throw RawPacketObjectExption("Flexible length can not use = function!");
  360. return false;
  361. }
  362. else
  363. {
  364. switch(ContextSize)
  365. {
  366. case 8:
  367. {
  368. //64bit
  369. unsigned long long LocalLen;
  370. memcpy(&LocalLen,m_pValue,sizeof(unsigned long long));
  371. value = (T4)LocalLen;
  372. break;
  373. }
  374. case 4:
  375. {
  376. //32bit
  377. unsigned int LocalLen;
  378. memcpy(&LocalLen,m_pValue,sizeof(unsigned int));
  379. value = (T4)LocalLen;
  380. break;
  381. }
  382. case 2:
  383. {
  384. //16bit
  385. unsigned short LocalLen;
  386. memcpy(&LocalLen,m_pValue,sizeof(unsigned short));
  387. value = (T4)LocalLen;
  388. break;
  389. }
  390. case 1:
  391. {
  392. //8bit
  393. unsigned char LocalLen;
  394. memcpy(&LocalLen,m_pValue,sizeof(unsigned char));
  395. value = (T4)LocalLen;
  396. break;
  397. }
  398. default:
  399. throw RawPacketObjectExption("ContextSize error!");
  400. break;
  401. }
  402. return true;
  403. }
  404. }
  405. else
  406. {
  407. throw RawPacketObjectExption("Do not have key, can not get value");
  408. return false;
  409. }
  410. }
  411. operator bool();
  412. operator char();
  413. operator unsigned char();
  414. operator short();
  415. operator unsigned short();
  416. operator int();
  417. operator unsigned int();
  418. operator long();
  419. operator unsigned long();
  420. operator long long();
  421. operator unsigned long long();
  422. operator float();
  423. operator double();
  424. operator const char*();
  425. bool IsObject();
  426. //比较 procedure----------------------------------------------------------
  427. bool operator== (const RawPacketObject& ar) const;//未实现
  428. };
  429. //Dios短指令协议包
  430. class DIOSPACKET_API DiosPacket : public SCFPacket
  431. {
  432. protected:
  433. RawPacketObject *m_pDataObj;
  434. bool m_bReady;//Packet的有效性
  435. unsigned int m_LenzoneSize;//in byte
  436. bool m_CrcEffective;
  437. unsigned int m_Direction;
  438. unsigned int m_FreezeLenzone;//01:freeze
  439. bool UpdateLastEnv();
  440. public:
  441. DiosPacket();
  442. DiosPacket(char* pData, size_t Length);
  443. virtual ~DiosPacket();
  444. //normal access---------------------------
  445. //Head(1Byte:0xFF)
  446. //bool GetHeadInfo(char &HeadInfo);
  447. //bool SetHeadInfo(char &PhyDevIdx);
  448. //Ver(1Byte)
  449. //bool GetVerInfo(char &VerInfo);
  450. //bool SetVerInfo(char &VerInfo);
  451. //PhyIdx(1Byte)
  452. bool GetPhyDevIdx(PHYIDXTYPE &PhyDevIdx);
  453. bool SetPhyDevIdx(PHYIDXTYPE PhyDevIdx);
  454. //LogicIdx(1Byte)
  455. bool GetLogicDevIdx(LOGICIDXTYPE &LogicDevIdx);
  456. bool SetLogicDevIdx(LOGICIDXTYPE LogicDevIdx);
  457. //8bit
  458. //crcEffective Flag:1bit (After DataArea we have crc16,the Flag indicates the crc16 whether effective or not)
  459. bool GetDataCrcEffectiveFlag(bool &Effective);
  460. bool SetDataCrcEffectiveFlag(bool Effective);
  461. //targetdirection:2bit (00:REQ,01:RES,10:NOTIFY)
  462. bool GetTargetDirection(unsigned int &Direction);
  463. bool SetTargetDirection(unsigned int Direction);
  464. //dataLenArea Flag:2bit (01 means dataLen Area is not flexible)
  465. bool GetFreezeLenZoneFlag(unsigned int &FreezeFlag);
  466. bool SetFreezeLenZoneFlag(unsigned int FreezeFlag);
  467. //dataLenAreaSize:3bit (DataLenArea's size depends on this value. )
  468. bool GetLenZoneSize(unsigned int &LenzoneSize);//it might useless...
  469. bool SetLenZoneSize(unsigned int LenzoneSize);//it might useless...
  470. //command(1Byte)
  471. bool GetCommand(DIOSCMDTYPE &Cmd);
  472. bool SetCommand(DIOSCMDTYPE Cmd);
  473. bool GetRetcode(char &Ret);
  474. bool SetRetcode(char Ret);
  475. //DataLenArea(the size of DataLenArea is determined by DataLenAreaSize.The DataLenArea's size is 2^n(n = .111b means no DataLenArea))
  476. bool GetDataLen(unsigned long long &Len);
  477. bool SetDataLen(unsigned long long Len);
  478. //Head's Crc16
  479. bool GetHeadsCrc(unsigned short &headCrc16);
  480. bool SetHeadsCrc(unsigned short headCrc16);
  481. //dataArea
  482. char* GetData();
  483. RawPacketObject *GetDataObject();
  484. bool SetData(const char *pData, size_t dataLen);//update LenArea too...
  485. bool AddData(const char *pData, size_t dataLen);//update LenArea too...
  486. //dataArea Crc16
  487. bool GetDataCrc(unsigned short &dataCrc16);
  488. bool SetDataCrc(unsigned short dataCrc16);
  489. //normal access---------------------------
  490. //prepare
  491. bool InitPacket();//init packet
  492. bool ReadyPacket();//1.check head,2.make crc,3.check the data area?
  493. size_t GetHeadSize();
  494. unsigned long long GetDiosPacketSize();
  495. bool GetParam(DWORD Idx,char *&pAddress,size_t &ParamSize);//Idx:paramidx,pAddress:address after key,ParamSize:the size of the params context
  496. };
  497. DIOSPACKET_C_API PVOID MallocDiosPacket();//申请Dios PACKET
  498. DIOSPACKET_C_API void ReleaseDiosPacket(PVOID pObj);//释放Dios PACKET
  499. DIOSPACKET_C_API PACKET_RET DiosPacketParser(const char * pRecData, DWORD nLength, DWORD & PacketLength);