iec10x.h 13 KB

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