iec10x.h 13 KB

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