iec104_queue.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. #ifndef _IEC104_QUEUE_H
  2. #define _IEC104_QUEUE_H
  3. #include "iec104_conf.h"
  4. #include "iec104_types.h"
  5. #include <stdio.h>
  6. #include <string.h>
  7. /*
  8. * IEC104配置
  9. * */
  10. #define IEC104LOCK
  11. #define PRIO_QUEUE
  12. #define IEC10X_PRIO_MAX 7
  13. #define IEC10X_HEADER_LENGTH 1
  14. /*
  15. * PRIO
  16. * */
  17. #define IEC10X_PRIO_INITLINK 0
  18. #define IEC10X_PRIO_CALLALL 1
  19. #define IEC10X_PRIO_CALLGROUP 2
  20. #define IEC10X_PRIO_CLOCK 3
  21. #define IEC10X_PRIO_DELAY 3
  22. #define IEC10X_PRIO_PULSE 0
  23. #define IEC10X_PRIO_SPON 0
  24. /*
  25. * Transmission Priority Queue.
  26. */
  27. typedef struct iec104_callback_arg
  28. {
  29. iec_8u *value;
  30. iec_32u pic_sn;
  31. iec_32u FramSerialNum;
  32. } iec104_callback_arg_t;
  33. typedef struct iec104_node
  34. {
  35. iec_16u Length;
  36. struct iec104_node *Next;
  37. void (*CallBack)(iec104_callback_arg_t *Arg);
  38. iec104_callback_arg_t CallBackArg;
  39. iec_8u value[1];
  40. } iec104_prio_node_t;
  41. typedef struct iec104_queue
  42. {
  43. struct iec104_node *header;
  44. struct iec104_node *tail;
  45. unsigned char element_num;
  46. } iec104_prio_queue_t;
  47. /*
  48. *********************************************************************************************************
  49. * BLOCK SIZE AND NUMBER
  50. *
  51. * if add part, please change OS_MAX_MEM_PART in ucosii.h.
  52. *********************************************************************************************************
  53. */
  54. /* IEC10X_PARTITION 1 size==256*8 */
  55. #define IEC10X_PARTITION_NUM_1 5
  56. #define IEC10X_PARTITION_SIZE_1 512
  57. #define IEC10X_PARTITION_1 (IEC10X_PARTITION_SIZE_1 - 16)
  58. /* IEC10X_PARTITION 2 size==(1300+16)*1 */
  59. #define IEC10X_PARTITION_NUM_2 20
  60. #define IEC10X_PARTITION_2 128
  61. #define IEC10X_PARTITION_SIZE_2 (IEC10X_PARTITION_2 + sizeof(Iec10x_PrioNode_T))
  62. /* IEC10X_PARTITION 1 size==64*20 */
  63. #define IEC10X_PARTITION_NUM_3 10
  64. #define IEC10X_PARTITION_3 32
  65. #define IEC10X_PARTITION_SIZE_3 (IEC10X_PARTITION_3 + sizeof(Iec10x_PrioNode_T))
  66. /*
  67. *********************************************************************************************************
  68. * ASDU
  69. *********************************************************************************************************
  70. */
  71. /*
  72. * ASDU TYPE
  73. * */
  74. #define Iec10x_M_EI_NA_1 0X46
  75. #define IEC10X_C_IC_NA_1 0X64 /*CALL*/
  76. #define IEC10X_C_CS_NA_1 103
  77. #define IEC10X_C_CD_NA_1 106 /*CALL*/
  78. /*Set data*/
  79. #define IEC10X_C_SE_NA_1 48
  80. #define IEC10X_C_SE_NC_1 50
  81. /*
  82. * ASDU REASON
  83. * */
  84. #define IEC10X_ASDU_REASON_INIT 0X04
  85. #define IEC10X_ASDU_REASON_ACT 0X06
  86. #define IEC10X_ASDU_REASON_ACTCON 0X07 /*CALL*/
  87. #define IEC10X_ASDU_REASON_ACTFIN 0X08 /*CALL FINISH*/
  88. #define IEC10X_ASDU_REASON_ACTTERM 0X0a /*CALL Terminal*/
  89. #define IEC10X_ASDU_COT_UNKNOW 45
  90. /*
  91. * INFO addr
  92. * */
  93. #define IEC10X_INFO_addr_NONE 0X00
  94. /*
  95. *********************************************************************************************************
  96. * REMOTE SIGNAL
  97. *********************************************************************************************************
  98. */
  99. /*
  100. * Asdu type (TI)
  101. * */
  102. #define IEC10X_M_SP_NA_1 0X01
  103. #define IEC10X_M_DP_NA_1 0X03
  104. #define IEC10X_M_SP_TB_1 30
  105. #define IEC10X_M_DP_TB_1 31
  106. /*
  107. * Asdu reason (COT)
  108. * */
  109. #define IEC10X_COT_BACK 2
  110. #define IEC10X_COT_SPONT 3
  111. #define IEC10X_COT_ACT 6
  112. #define IEC10X_COT_ACTCON 7
  113. #define IEC10X_COT_REQ 5
  114. #define IEC10X_COT_RETREM 11
  115. #define IEC10X_COT_RETLOC 12
  116. #define IEC10X_COT_INTROGEN 20
  117. #define IEC10X_COT_INTRO1 21
  118. #define IEC10X_COT_INTRO2 22
  119. /*
  120. * Asdu addr
  121. * */
  122. #define IEC10X_INFO_ADDR_SIG_BASE 0X0001
  123. #define IEC10X_INFO_ADDR_SIG_TEMP_HX_OFF 0x1000
  124. /*
  125. *********************************************************************************************************
  126. * REMOTE DECTET
  127. *********************************************************************************************************
  128. */
  129. /*
  130. * Asdu type (TI)
  131. * */
  132. #define IEC10X_M_ME_NA_1 9
  133. #define IEC10X_M_ME_NC_1 13
  134. #define IEC10X_M_ME_TD_1 34
  135. #define IEC10X_M_ME_TF_1 36
  136. /*
  137. * Asdu reason (COT)
  138. * */
  139. #define IEC10X_COT_PER_CYC 1
  140. #define IEC10X_COT_BACK 2
  141. #define IEC10X_COT_SPONT 3
  142. #define IEC10X_COT_REQ 5
  143. #define IEC10X_COT_INTROGEN 20
  144. #define IEC10X_COT_INTRO9 29
  145. #define IEC10X_COT_INTRO10 30
  146. /*
  147. * Asdu addr
  148. * */
  149. #define IEC10X_INFO_addr_DET 0X4001
  150. #define IEC10X_INFO_addr_DET_TEMP_HX_OFF 0x1000
  151. /*
  152. *********************************************************************************************************
  153. * CALL QOI
  154. *********************************************************************************************************
  155. */
  156. #define IEC10X_CALL_QOI_TOTAL 20
  157. #define IEC10X_CALL_QOI_GROUP1 21
  158. #define IEC10X_CALL_QOI_GROUP2 22
  159. #define IEC10X_CALL_QOI_GROUP9 29
  160. #define IEC10X_CALL_QOI_GROUP10 30
  161. /*
  162. *********************************************************************************************************
  163. * INFO addrEST
  164. * TD -- temprature device
  165. * PD -- pullotion device
  166. *********************************************************************************************************
  167. */
  168. #define IEC10X_SET_TD_UPCYCLE 0X100001
  169. #define IEC10X_SET_TD_MAXIMUM 0X100002
  170. #define IEC10X_SET_TD_MINIMUM 0X100003
  171. #define IEC10X_SET_TD_DETCYCLE 0X100004
  172. #define IEC10X_SET_TD_TEMPRISE 0X100005
  173. #define IEC10X_SET_PD_DETCYCLE 0X100006
  174. #define IEC10X_SET_PD_PULPSE 0X100007
  175. #define IEC10X_SET_PD_SELFDETCYCLE 0X100008
  176. /*
  177. *********************************************************************************************************
  178. * Fujian Huixing Add
  179. *********************************************************************************************************
  180. */
  181. /* TI. */
  182. #define IEC10X_TI_FIRM_UPDATE 128
  183. #define IEC10X_TI_AP_FIRM_BACKOFF 129
  184. #define IEC10X_TI_AP_BASE_INFO 130
  185. /* COT. */
  186. #define IEC10X_COT_ACT_TERMINAL 8
  187. #define IEC10X_COT_ACT_TERMINAL_ACK 9
  188. #define IEC10X_COT_DATA 14
  189. #define IEC10X_COT_DATA_ACK 15
  190. #define IEC10X_COT_DATA_FIN 16
  191. #define IEC10X_COT_DATA_FIN_ACK 17
  192. #define IEC10X_COT_DATA_NEEDACK 0X100E
  193. #pragma pack(1)
  194. /*
  195. * asdu number
  196. * */
  197. typedef struct
  198. {
  199. iec_8u _num : 7;
  200. iec_8u _sq : 1;
  201. } ASDU_NUM_T;
  202. /*
  203. * asdu reason
  204. * */
  205. typedef struct
  206. {
  207. iec_16u _reason : 14;
  208. iec_16u _pn : 1;
  209. iec_16u _test : 1;
  210. } ASDU_REASON_T;
  211. /*
  212. * asdu
  213. * */
  214. typedef struct
  215. {
  216. iec_8u _type;
  217. ASDU_NUM_T _num;
  218. ASDU_REASON_T _reason;
  219. iec_16u _addr;
  220. iec_8u _info[1];
  221. } iec10x_asdu_t, *p_iec10x_asdu_t;
  222. /*
  223. * asdu info
  224. * */
  225. typedef struct
  226. {
  227. iec_8u _addr[3];
  228. iec_8u _element[1];
  229. } asdu_info_t, *p_asdu_info_t;
  230. /*
  231. * asdu info remote signal with time flag
  232. * */
  233. typedef struct
  234. {
  235. iec_8u _signal;
  236. iec_8u _time;
  237. } SIGNAL_TIME_T, *PSIGNAL_TIME_T;
  238. /*
  239. * asdu info remote detect with time flag
  240. * */
  241. /*int */
  242. typedef struct
  243. {
  244. iec_16s _detect;
  245. iec_8u _qds;
  246. } IEC10X_DETECT_T, *PIEC10X_DETECT_T;
  247. /*float*/
  248. typedef struct
  249. {
  250. float _detect;
  251. iec_8u _qds;
  252. } IEC10X_DETECT_F_T, *PIEC10X_DETECT_F_T;
  253. /*int sq=0 */
  254. typedef struct
  255. {
  256. iec_8u _addr[3];
  257. iec_16s _detect;
  258. iec_8u _qds;
  259. } IEC10X_DETECT_SQ0_T, *PIEC10X_DETECT_SQ0_T;
  260. /*float sq=0*/
  261. typedef struct
  262. {
  263. iec_8u _addr[3];
  264. float _detect;
  265. iec_8u _qds;
  266. } IEC10X_DETECT_SQ0_F_T, *PIEC10X_DETECT_SQ0_F_T;
  267. /*
  268. * asdu info CP56Time2a
  269. * */
  270. typedef struct
  271. {
  272. iec_8u _minutes : 6;
  273. iec_8u _res : 1;
  274. iec_8u _iv : 1;
  275. } iec10x_time_min_t, *p_iec10x_time_min_t;
  276. typedef struct
  277. {
  278. iec_8u _hours : 5;
  279. iec_8u _res : 2;
  280. iec_8u _su : 1;
  281. } iec10x_time_hour_t, *p_iec10x_time_hour_t;
  282. typedef struct
  283. {
  284. iec_8u _dayofmonth : 5;
  285. iec_8u _dayofweek : 3;
  286. } IEC10X_Time_Day_T, *PIEC10X_Time_Day_T;
  287. typedef struct
  288. {
  289. iec_8u _month : 4;
  290. iec_8u _res : 3;
  291. } IEC10X_Time_Month_T, *PIEC10X_Time_Month_T;
  292. typedef struct
  293. {
  294. iec_8u _year : 7;
  295. iec_8u _res : 1;
  296. } IEC10X_Time_Year_T, *PIEC10X_Time_Year_T;
  297. typedef struct
  298. {
  299. iec_16u _milliseconds;
  300. iec10x_time_min_t _min;
  301. iec10x_time_hour_t _hour;
  302. IEC10X_Time_Day_T _day;
  303. IEC10X_Time_Month_T _month;
  304. IEC10X_Time_Year_T _year;
  305. } CP56Time2a_T, *PCP56Time2a_T;
  306. /*
  307. * asdu info remote detect with time flag
  308. * */
  309. typedef struct
  310. {
  311. iec_16u _detect;
  312. iec_8u _qds;
  313. CP56Time2a_T _time;
  314. } DETECT_TIME_T, *PDETECT_TIME_T;
  315. #pragma pack()
  316. /*
  317. * Iec10x init
  318. */
  319. typedef struct _iec104
  320. {
  321. char *name;
  322. int (*init)(void);
  323. void (*delay_ms)(iec_16u);
  324. void (*close_link)(void);
  325. void *(*malloc)(iec_8u nbyte);
  326. void (*free)(void *buffer);
  327. iec_8u (*enqueue)(iec104_prio_queue_t *queue_hdr, iec104_prio_node_t *newnode);
  328. iec104_prio_node_t *(*dequeue)(iec104_prio_queue_t *queue_hdr);
  329. iec104_prio_node_t *(*find_qhead)(iec104_prio_queue_t *queue_hdr);
  330. iec_8u (*get_prio)(void);
  331. void (*queue_init)(iec104_prio_queue_t *PrioQueue);
  332. void (*queue_clear)(iec104_prio_queue_t *queue_hdr);
  333. iec_8u (*send)(int socketfd, char *data, int len);
  334. iec_32u (*set_time)(PCP56Time2a_T time);
  335. iec_32u (*get_time)(PCP56Time2a_T time);
  336. iec_8s (*get_station_state)(iec_16u addr, iec_8u dev_type);
  337. float (*get_sta_value)(iec_16u addr, iec_8u dev_type);
  338. iec_16u (*get_link_addr)(void);
  339. iec_8s (*get_info_num)(iec_8u *info_num, iec_8u dev_type);
  340. iec_8s (*set_config)(long value, iec_32u addr);
  341. iec_8s (*save_firmware)(iec_8u firm_len, iec_8u *buf, iec_32u firmware_type, iec_32u iec104_update_seek_addr);
  342. iec_8s (*check_firmware)(iec_32u firmware_type, iec_32u total_len);
  343. iec_8s (*update_firmware)(iec_32u firmware_type);
  344. iec_8s (*backoff_firmware)(iec_32u firmware_type);
  345. #ifdef IEC104LOCK
  346. void (*lock)(void);
  347. void (*unlock)(void);
  348. #endif
  349. } *p_iec104_t, iec104_t;
  350. /*
  351. * BIG ENDIAN
  352. * */
  353. #ifdef BIG_ENDIAN
  354. #define n2hs(x) (x)
  355. #define h2ns(x) (x)
  356. #define n2hl(x) (x)
  357. #define h2nl(x) (x)
  358. /* LITTLE ENDIAN */
  359. #else
  360. #define n2hs(x) ((((x) >> 8) & 0xFF) | (((x)&0xFF) << 8))
  361. #define h2ns(x) ((((x) >> 8) & 0xFF) | (((x)&0xFF) << 8))
  362. #define n2hl(x) ((((x) >> 24) & 0xFF) | (((x) >> 8) & 0xFF00) | \
  363. (((x)&0xFF00) << 8) | (((x)&0xFF) << 24))
  364. #define h2nl(x) ((((x) >> 24) & 0xFF) | (((x) >> 8) & 0xFF00) | \
  365. (((x)&0xFF00) << 8) | (((x)&0xFF) << 24))
  366. #endif /* BIG_ENDIAN */
  367. /*
  368. *********************************************************************************************************
  369. * EXTERN VARIABLE
  370. *********************************************************************************************************
  371. */
  372. extern p_iec104_t iec104;
  373. extern iec_16u iec104_state_addr;
  374. extern iec_32s iec104_build_send_sn;
  375. extern iec_32s iec104_build_recv_sn;
  376. extern iec_16u IEC10X_Cp16time2a;
  377. extern iec_16u IEC10X_Cp16time2a_V;
  378. extern CP56Time2a_T IEC10X_Cp56time2a;
  379. extern iec_8u Iec10x_FirmwareUpdateFlag;
  380. /*
  381. * Prio
  382. * */
  383. iec_8u iec104_prio_enqueue(iec104_prio_queue_t *queue_hdr, iec104_prio_node_t *new_p);
  384. iec104_prio_node_t *iec104_prio_dequeue(iec104_prio_queue_t *queue_hdr);
  385. iec_8u iec10x_prio_highest(void);
  386. void iec104_prio_queue_init(iec104_prio_queue_t *PrioQueue);
  387. void iec104_prio_queue_clear(iec104_prio_queue_t *queue_hdr);
  388. iec104_prio_node_t *iec104_prio_queue_find_head(iec104_prio_queue_t *queue_hdr);
  389. /*
  390. * iec104 队列
  391. * */
  392. void iec104_queue_init(void);
  393. void iec104_queue_clear(void);
  394. iec_8u iec104_prio_get(iec_8u State);
  395. void iec104_enqueue(iec_8u *enq_buf, iec_16u Length, iec_8u Prio,
  396. void (*IEC10XCallBack)(iec104_callback_arg_t *Arg), iec104_callback_arg_t *CallbackArg);
  397. void iec104_scheduled(int socketfd);
  398. iec104_prio_node_t *iec104_dequeue(void);
  399. iec_32s iec104_module_register(void *p_iec10x);
  400. void iec104_lock(void);
  401. void iec104_unlock(void);
  402. #endif /*_IEC10X_H*/