iec10x.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*******************************************************************
  2. Copyright (C):
  3. File name : Iec10x.h
  4. DESCRIPTION :
  5. AUTHOR :
  6. Version : 1.0
  7. Date : 2014/07/23
  8. Others :
  9. History :
  10. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  11. 1) Date: 2014/07/23 Author: ChenDajie
  12. content:
  13. add iec104
  14. *******************************************************************/
  15. #ifndef _IEC10X_H
  16. #define _IEC10X_H
  17. #include "PRIO_QUEUE_Iec10x.h"
  18. #include "project_var.h"
  19. #include <stdio.h>
  20. #include <string.h>
  21. /*
  22. * CONFIGURE
  23. * */
  24. #define IEC10XLOCK
  25. #define PRIO_QUEUE
  26. #ifdef IEC101_STM32
  27. #define IEC10X_PRIO_MAX 7
  28. #elif defined(IEC104_STM32)
  29. #define IEC10X_PRIO_MAX 7
  30. #endif
  31. #define IEC10X_HEADER_LENGTH 1
  32. /*
  33. * PRIO
  34. * */
  35. #define IEC10X_PRIO_INITLINK 0
  36. #define IEC10X_PRIO_CALLALL 1
  37. #define IEC10X_PRIO_CALLGROUP 2
  38. #define IEC10X_PRIO_CLOCK 3
  39. #define IEC10X_PRIO_DELAY 3
  40. #define IEC10X_PRIO_PULSE 0
  41. #define IEC10X_PRIO_SPON 0
  42. /*
  43. * Transmission Priority Queue.
  44. */
  45. typedef struct IEC10X_CallbackArg
  46. {
  47. uint8_t *value;
  48. uint32_t PicSn;
  49. uint32_t FramSerialNum;
  50. } Iec10x_CallbackArg_T;
  51. typedef struct IEC10X_NODE
  52. {
  53. uint16_t Length;
  54. struct IEC10X_NODE *Next;
  55. void (*CallBack)(Iec10x_CallbackArg_T *Arg);
  56. Iec10x_CallbackArg_T CallBackArg;
  57. uint8_t value[1];
  58. } Iec10x_PrioNode_T;
  59. typedef struct IEC10X_QUEUE
  60. {
  61. struct IEC10X_NODE *Header;
  62. struct IEC10X_NODE *Tail;
  63. unsigned char ElementNum;
  64. } Iec10x_PrioQueue_T;
  65. /*
  66. *********************************************************************************************************
  67. * BLOCK SIZE AND NUMBER
  68. *
  69. * if add part, please change OS_MAX_MEM_PART in ucosii.h.
  70. *********************************************************************************************************
  71. */
  72. /* IEC10X_PARTITION 1 size==256*8 */
  73. #define IEC10X_PARTITION_NUM_1 5
  74. #define IEC10X_PARTITION_SIZE_1 512
  75. #define IEC10X_PARTITION_1 (IEC10X_PARTITION_SIZE_1 - 16)
  76. /* IEC10X_PARTITION 2 size==(1300+16)*1 */
  77. #define IEC10X_PARTITION_NUM_2 20
  78. #define IEC10X_PARTITION_2 128
  79. #define IEC10X_PARTITION_SIZE_2 (IEC10X_PARTITION_2 + sizeof(Iec10x_PrioNode_T))
  80. /* IEC10X_PARTITION 1 size==64*20 */
  81. #define IEC10X_PARTITION_NUM_3 10
  82. #define IEC10X_PARTITION_3 32
  83. #define IEC10X_PARTITION_SIZE_3 (IEC10X_PARTITION_3 + sizeof(Iec10x_PrioNode_T))
  84. /*
  85. *********************************************************************************************************
  86. * ASDU
  87. *********************************************************************************************************
  88. */
  89. /*
  90. * ASDU TYPE
  91. * */
  92. #define Iec10x_M_EI_NA_1 0X46
  93. #define IEC10X_C_IC_NA_1 0X64 /*CALL*/
  94. #define IEC10X_C_CS_NA_1 103
  95. #define IEC10X_C_CD_NA_1 106 /*CALL*/
  96. /*Set data*/
  97. #define IEC10X_C_SE_NA_1 48
  98. #define IEC10X_C_SE_NC_1 50
  99. /*
  100. * ASDU REASON
  101. * */
  102. #define IEC10X_ASDU_REASON_INIT 0X04
  103. #define IEC10X_ASDU_REASON_ACT 0X06
  104. #define IEC10X_ASDU_REASON_ACTCON 0X07 /*CALL*/
  105. #define IEC10X_ASDU_REASON_ACTFIN 0X08 /*CALL FINISH*/
  106. #define IEC10X_ASDU_REASON_ACTTERM 0X0a /*CALL Terminal*/
  107. #define IEC10X_ASDU_COT_UNKNOW 45
  108. /*
  109. * INFO ADDR
  110. * */
  111. #define IEC10X_INFO_ADDR_NONE 0X00
  112. /*
  113. *********************************************************************************************************
  114. * REMOTE SIGNAL
  115. *********************************************************************************************************
  116. */
  117. /*
  118. * Asdu type (TI)
  119. * */
  120. #define IEC10X_M_SP_NA_1 0X01
  121. #define IEC10X_M_DP_NA_1 0X03
  122. #define IEC10X_M_SP_TB_1 30
  123. #define IEC10X_M_DP_TB_1 31
  124. /*
  125. * Asdu reason (COT)
  126. * */
  127. #define IEC10X_COT_BACK 2
  128. #define IEC10X_COT_SPONT 3
  129. #define IEC10X_COT_ACT 6
  130. #define IEC10X_COT_ACTCON 7
  131. #define IEC10X_COT_REQ 5
  132. #define IEC10X_COT_RETREM 11
  133. #define IEC10X_COT_RETLOC 12
  134. #define IEC10X_COT_INTROGEN 20
  135. #define IEC10X_COT_INTRO1 21
  136. #define IEC10X_COT_INTRO2 22
  137. /*
  138. * Asdu addr
  139. * */
  140. #define IEC10X_INFO_ADDR_SIG_BASE 0X0001
  141. #define IEC10X_INFO_ADDR_SIG_TEMP_HX_OFF 0x1000
  142. /*
  143. *********************************************************************************************************
  144. * REMOTE DECTET
  145. *********************************************************************************************************
  146. */
  147. /*
  148. * Asdu type (TI)
  149. * */
  150. #define IEC10X_M_ME_NA_1 9
  151. #define IEC10X_M_ME_NC_1 13
  152. #define IEC10X_M_ME_TD_1 34
  153. #define IEC10X_M_ME_TF_1 36
  154. /*
  155. * Asdu reason (COT)
  156. * */
  157. #define IEC10X_COT_PER_CYC 1
  158. #define IEC10X_COT_BACK 2
  159. #define IEC10X_COT_SPONT 3
  160. #define IEC10X_COT_REQ 5
  161. #define IEC10X_COT_INTROGEN 20
  162. #define IEC10X_COT_INTRO9 29
  163. #define IEC10X_COT_INTRO10 30
  164. /*
  165. * Asdu addr
  166. * */
  167. #define IEC10X_INFO_ADDR_DET 0X4001
  168. #define IEC10X_INFO_ADDR_DET_TEMP_HX_OFF 0x1000
  169. /*
  170. *********************************************************************************************************
  171. * CALL QOI
  172. *********************************************************************************************************
  173. */
  174. #define IEC10X_CALL_QOI_TOTAL 20
  175. #define IEC10X_CALL_QOI_GROUP1 21
  176. #define IEC10X_CALL_QOI_GROUP2 22
  177. #define IEC10X_CALL_QOI_GROUP9 29
  178. #define IEC10X_CALL_QOI_GROUP10 30
  179. /*
  180. *********************************************************************************************************
  181. * INFO ADDREST
  182. * TD -- temprature device
  183. * PD -- pullotion device
  184. *********************************************************************************************************
  185. */
  186. #define IEC10X_SET_TD_UPCYCLE 0X100001
  187. #define IEC10X_SET_TD_MAXIMUM 0X100002
  188. #define IEC10X_SET_TD_MINIMUM 0X100003
  189. #define IEC10X_SET_TD_DETCYCLE 0X100004
  190. #define IEC10X_SET_TD_TEMPRISE 0X100005
  191. #define IEC10X_SET_PD_DETCYCLE 0X100006
  192. #define IEC10X_SET_PD_PULPSE 0X100007
  193. #define IEC10X_SET_PD_SELFDETCYCLE 0X100008
  194. /*
  195. *********************************************************************************************************
  196. * Fujian Huixing Add
  197. *********************************************************************************************************
  198. */
  199. /* TI. */
  200. #define IEC10X_TI_FIRM_UPDATE 128
  201. #define IEC10X_TI_AP_FIRM_BACKOFF 129
  202. #define IEC10X_TI_AP_BASE_INFO 130
  203. /* COT. */
  204. #define IEC10X_COT_ACT_TERMINAL 8
  205. #define IEC10X_COT_ACT_TERMINAL_ACK 9
  206. #define IEC10X_COT_DATA 14
  207. #define IEC10X_COT_DATA_ACK 15
  208. #define IEC10X_COT_DATA_FIN 16
  209. #define IEC10X_COT_DATA_FIN_ACK 17
  210. #define IEC10X_COT_DATA_NEEDACK 0X100E
  211. #pragma pack(1)
  212. /*
  213. * asdu number
  214. * */
  215. typedef struct
  216. {
  217. uint8_t _num : 7;
  218. uint8_t _sq : 1;
  219. } ASDU_NUM_T;
  220. /*
  221. * asdu reason
  222. * */
  223. typedef struct
  224. {
  225. #ifdef IEC101_STM32
  226. uint8_t _reason : 6;
  227. uint8_t _pn : 1;
  228. uint8_t _test : 1;
  229. #elif defined(IEC104_STM32)
  230. uint16_t _reason : 14;
  231. uint16_t _pn : 1;
  232. uint16_t _test : 1;
  233. #endif
  234. } ASDU_REASON_T;
  235. /*
  236. * asdu
  237. * */
  238. typedef struct
  239. {
  240. uint8_t _type;
  241. ASDU_NUM_T _num;
  242. ASDU_REASON_T _reason;
  243. uint16_t _addr;
  244. uint8_t _info[1];
  245. } IEC10X_ASDU_T, *PIEC10X_ASDU_T;
  246. /*
  247. * asdu info
  248. * */
  249. typedef struct
  250. {
  251. #ifdef IEC101_STM32
  252. uint16_t _addr;
  253. #elif defined(IEC104_STM32)
  254. uint8_t _addr[3];
  255. #endif
  256. uint8_t _element[1];
  257. } ASDU_INFO_T, *PASDU_INFO_T;
  258. /*
  259. * asdu info remote signal with time flag
  260. * */
  261. typedef struct
  262. {
  263. uint8_t _signal;
  264. uint8_t _time;
  265. } SIGNAL_TIME_T, *PSIGNAL_TIME_T;
  266. /*
  267. * asdu info remote detect with time flag
  268. * */
  269. /*int */
  270. typedef struct
  271. {
  272. int16_t _detect;
  273. uint8_t _qds;
  274. } IEC10X_DETECT_T, *PIEC10X_DETECT_T;
  275. /*float*/
  276. typedef struct
  277. {
  278. float _detect;
  279. uint8_t _qds;
  280. } IEC10X_DETECT_F_T, *PIEC10X_DETECT_F_T;
  281. /*int sq=0 */
  282. typedef struct
  283. {
  284. #ifdef IEC101_STM32
  285. uint16_t _addr;
  286. #elif defined(IEC104_STM32)
  287. uint8_t _addr[3];
  288. #endif
  289. int16_t _detect;
  290. uint8_t _qds;
  291. } IEC10X_DETECT_SQ0_T, *PIEC10X_DETECT_SQ0_T;
  292. /*float sq=0*/
  293. typedef struct
  294. {
  295. #ifdef IEC101_STM32
  296. uint16_t _addr;
  297. #elif defined(IEC104_STM32)
  298. uint8_t _addr[3];
  299. #endif
  300. float _detect;
  301. uint8_t _qds;
  302. } IEC10X_DETECT_SQ0_F_T, *PIEC10X_DETECT_SQ0_F_T;
  303. /*
  304. * asdu info CP56Time2a
  305. * */
  306. typedef struct
  307. {
  308. uint8_t _minutes : 6;
  309. uint8_t _res : 1;
  310. uint8_t _iv : 1;
  311. } IEC10X_Time_Min_T, *PIEC10X_Time_Min_T;
  312. typedef struct
  313. {
  314. uint8_t _hours : 5;
  315. uint8_t _res : 2;
  316. uint8_t _su : 1;
  317. } IEC10X_Time_Hour_T, *PIEC10X_Time_Hour_T;
  318. typedef struct
  319. {
  320. uint8_t _dayofmonth : 5;
  321. uint8_t _dayofweek : 3;
  322. } IEC10X_Time_Day_T, *PIEC10X_Time_Day_T;
  323. typedef struct
  324. {
  325. uint8_t _month : 4;
  326. uint8_t _res : 3;
  327. } IEC10X_Time_Month_T, *PIEC10X_Time_Month_T;
  328. typedef struct
  329. {
  330. uint8_t _year : 7;
  331. uint8_t _res : 1;
  332. } IEC10X_Time_Year_T, *PIEC10X_Time_Year_T;
  333. typedef struct
  334. {
  335. uint16_t _milliseconds;
  336. IEC10X_Time_Min_T _min;
  337. IEC10X_Time_Hour_T _hour;
  338. IEC10X_Time_Day_T _day;
  339. IEC10X_Time_Month_T _month;
  340. IEC10X_Time_Year_T _year;
  341. } CP56Time2a_T, *PCP56Time2a_T;
  342. /*
  343. * asdu info remote detect with time flag
  344. * */
  345. typedef struct
  346. {
  347. uint16_t _detect;
  348. uint8_t _qds;
  349. CP56Time2a_T _time;
  350. } DETECT_TIME_T, *PDETECT_TIME_T;
  351. #pragma pack()
  352. /*
  353. * Iec10x init
  354. */
  355. typedef struct _iec10x
  356. {
  357. char *name;
  358. int (*Init)(void);
  359. void (*Delay_ms)(uint16_t);
  360. void (*CloseLink)(void);
  361. void *(*Malloc)(uint8_t NumByte);
  362. void (*Free)(void *buffer);
  363. uint8_t (*enqueue)(Iec10x_PrioQueue_T *QueueHdr, Iec10x_PrioNode_T *NewNode);
  364. Iec10x_PrioNode_T *(*dequeue)(Iec10x_PrioQueue_T *QueueHdr);
  365. Iec10x_PrioNode_T *(*FindQHead)(Iec10x_PrioQueue_T *QueueHdr);
  366. char (*GetPrio)(void);
  367. void (*InitQueue)(Iec10x_PrioQueue_T *PrioQueue);
  368. void (*ClearQueue)(Iec10x_PrioQueue_T *QueueHdr);
  369. uint8_t (*Send)(int socketfd, char *data, int len);
  370. uint32_t (*SetTime)(PCP56Time2a_T time);
  371. uint32_t (*GetTime)(PCP56Time2a_T time);
  372. int8_t (*GetStationState)(uint16_t Addr, uint8_t DevType);
  373. float (*GetStaValue)(uint16_t Addr, uint8_t DevType);
  374. uint16_t (*GetLinkAddr)(void);
  375. int8_t (*GetInfoNum)(uint8_t *InfoNum, uint8_t DevType);
  376. int8_t (*SetConfig)(long Value, uint32_t addr);
  377. int8_t (*SaveFirmware)(uint8_t FirmLen, uint8_t *buf, uint32_t FirmwareType, uint32_t Iec10x_Update_SeekAddr);
  378. int8_t (*CheckFirmware)(uint32_t FirmwareType, uint32_t TotalLen);
  379. int8_t (*UpdateFirmware)(uint32_t FirmwareType);
  380. int8_t (*BackoffFirmware)(uint32_t FirmwareType);
  381. #ifdef IEC10XLOCK
  382. void (*LOCK)(void);
  383. void (*UNLOCK)(void);
  384. #endif
  385. } * PIEC10X_T, IEC10X_T;
  386. /*
  387. * BIG ENDIAN
  388. * */
  389. #ifdef BIG_ENDIAN
  390. #define n2hs(x) (x)
  391. #define h2ns(x) (x)
  392. #define n2hl(x) (x)
  393. #define h2nl(x) (x)
  394. /* LITTLE ENDIAN */
  395. #else
  396. #define n2hs(x) ((((x) >> 8) & 0xFF) | (((x)&0xFF) << 8))
  397. #define h2ns(x) ((((x) >> 8) & 0xFF) | (((x)&0xFF) << 8))
  398. #define n2hl(x) ((((x) >> 24) & 0xFF) | (((x) >> 8) & 0xFF00) | \
  399. (((x)&0xFF00) << 8) | (((x)&0xFF) << 24))
  400. #define h2nl(x) ((((x) >> 24) & 0xFF) | (((x) >> 8) & 0xFF00) | \
  401. (((x)&0xFF00) << 8) | (((x)&0xFF) << 24))
  402. #endif /* BIG_ENDIAN */
  403. /*
  404. *********************************************************************************************************
  405. * EXTERN VARIABLE
  406. *********************************************************************************************************
  407. */
  408. extern PIEC10X_T IEC10X;
  409. extern uint16_t Iec10x_Sta_Addr;
  410. extern int32_t Iec104_BuildSendSn;
  411. extern int32_t Iec104_BuildRecvSn;
  412. extern uint16_t IEC10X_Cp16time2a;
  413. extern uint16_t IEC10X_Cp16time2a_V;
  414. extern CP56Time2a_T IEC10X_Cp56time2a;
  415. extern uint8_t Iec10x_FirmwareUpdateFlag;
  416. /*
  417. *********************************************************************************************************
  418. * QUEUE FUNCTION PROTOTYPES
  419. *********************************************************************************************************
  420. */
  421. /*
  422. * Prio
  423. * */
  424. uint8_t IEC10X_PrioEnQueue(Iec10x_PrioQueue_T *QueueHdr, Iec10x_PrioNode_T *new_p);
  425. Iec10x_PrioNode_T *IEC10X_PrioDeQueue(Iec10x_PrioQueue_T *QueueHdr);
  426. char IEC10X_HighestPrio(void);
  427. void IEC10X_PrioInitQueue(Iec10x_PrioQueue_T *PrioQueue);
  428. void IEC10X_Prio_ClearQueue(Iec10x_PrioQueue_T *QueueHdr);
  429. Iec10x_PrioNode_T *IEC10X_PrioFindQueueHead(Iec10x_PrioQueue_T *QueueHdr);
  430. /*
  431. * Iec10x queue
  432. * */
  433. void IEC10X_InitQ(void);
  434. void IEC10X_ClearQ(void);
  435. uint8_t IEC10X_GetPrio(uint8_t State);
  436. void IEC10X_Enqueue(uint8_t *EnQBuf, uint16_t Length, uint8_t Prio,
  437. void (*IEC10XCallBack)(Iec10x_CallbackArg_T *Arg), Iec10x_CallbackArg_T *CallbackArg);
  438. void Iec10x_Scheduled(int socketfd);
  439. Iec10x_PrioNode_T *IEC10X_Dequeue(void);
  440. int32_t RegisterIEC10XMoudle(void *_IEC10X);
  441. void IEC10X_ClearQ(void);
  442. void Iec10x_Lock(void);
  443. void Iec10x_UnLock(void);
  444. #endif /*_IEC10X_H*/