iec101.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. #include "iec101.h"
  2. #include "iec10x_conf.h"
  3. #include "iec10x_type.h"
  4. #ifdef IEC101_SET
  5. /*
  6. * GLOABLE VARIALBLE
  7. */
  8. iec_8u Iec101_Respon_Confirm = 0;
  9. iec_8u Iec101_Sendbuf[IEC101_MAX_BUF_LEN];
  10. iec_8u IEC10X_Call_AllQoi = 0;
  11. iec_8u IEC10X_Call_GroupQoi = 0;
  12. iec_16u IEC101_Pulse_Cnt = 0;
  13. /*
  14. * STATE
  15. * */
  16. iec_8u IEC101_STATE_FLAG_INIT = IEC101_FLAG_LINK_CLOSED;
  17. iec_8u IEC101_STATE_FLAG_CALLALL = IEC101_FLAG_LINK_CLOSED;
  18. iec_8u IEC101_STATE_FLAG_GROUP = IEC101_FLAG_LINK_CLOSED;
  19. iec_8u IEC101_STATE_FLAG_CLOCK = IEC101_FLAG_LINK_CLOSED;
  20. iec_8u IEC101_STATE_FLAG_DELAY = IEC101_FLAG_LINK_CLOSED;
  21. iec_8u IEC101_STATE_FLAG_PULSE = IEC101_FLAG_LINK_CLOSED;
  22. iec_8u IEC10X_RetStatusOk(iec_16u addr)
  23. {
  24. iec_16u len;
  25. PIEC101_10_T Iec10x = (PIEC101_10_T)Iec101_Sendbuf;
  26. len = IEC101_STABLE_LEN;
  27. Iec10x->_begin = IEC101_STABLE_BEGING;
  28. Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
  29. Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_SLAVE;
  30. Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_NONE_DATA;
  31. Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CAN_REC;
  32. Iec10x->_ctrl.up._func = IEC101_CTRL_RES_LINK_STATUS;
  33. Iec10x->_addr = addr;
  34. Iec10x->_cs = Iec101_Sendbuf[1] + Iec101_Sendbuf[2] + Iec101_Sendbuf[3];
  35. Iec10x->_end = IEC101_STABLE_END;
  36. /* enqueue to the transmisson queue */
  37. IEC10X_Enqueue(Iec101_Sendbuf, len, IEC10X_PRIO_INITLINK, NULL, NULL);
  38. return RET_SUCESS;
  39. }
  40. static iec_8u IEC10X_ResConfirm(iec_8u Prio)
  41. {
  42. iec_16u len;
  43. PIEC101_10_T Iec10x = (PIEC101_10_T)Iec101_Sendbuf;
  44. len = IEC101_STABLE_LEN;
  45. Iec10x->_begin = IEC101_STABLE_BEGING;
  46. Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
  47. Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_SLAVE;
  48. Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_NONE_DATA;
  49. Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CAN_REC;
  50. Iec10x->_ctrl.up._func = IEC101_CTRL_RES_CONFIRM;
  51. Iec10x->_addr = Iec10x_Sta_Addr;
  52. Iec10x->_cs = Iec101_Sendbuf[1] + Iec101_Sendbuf[2] + Iec101_Sendbuf[3];
  53. Iec10x->_end = IEC101_STABLE_END;
  54. /* enqueue to the transmisson queue */
  55. IEC10X_Enqueue(Iec101_Sendbuf, len, Prio, NULL, NULL);
  56. return RET_SUCESS;
  57. }
  58. iec_8u IEC10X_ReqLinkStatus()
  59. {
  60. iec_16u len;
  61. PIEC101_10_T Iec10x = (PIEC101_10_T)Iec101_Sendbuf;
  62. LOG("%s \n", __FUNCTION__);
  63. len = IEC101_STABLE_LEN;
  64. Iec10x->_begin = IEC101_STABLE_BEGING;
  65. /*Ctrol*/
  66. Iec10x->_ctrl.down._dir = IEC101_CTRL_DIR_UP;
  67. Iec10x->_ctrl.down._prm = IEC101_CTRL_PRM_MASTER;
  68. Iec10x->_ctrl.down._fcb = IEC101_CTRL_FCB_OPPO_NONE;
  69. Iec10x->_ctrl.down._fcv = IEC101_CTRL_FCV_DISABLE;
  70. Iec10x->_ctrl.up._func = IEC101_CTRL_REQ_LINK_STATUS;
  71. Iec10x->_addr = Iec10x_Sta_Addr;
  72. Iec10x->_cs = Iec101_Sendbuf[1] + Iec101_Sendbuf[2] + Iec101_Sendbuf[3];
  73. Iec10x->_end = IEC101_STABLE_END;
  74. /* enqueue to the transmisson queue */
  75. IEC10X_Enqueue(Iec101_Sendbuf, len, IEC10X_PRIO_INITLINK, NULL, NULL);
  76. return RET_SUCESS;
  77. }
  78. iec_8u IEC10X_ResetLink(void)
  79. {
  80. iec_16u len;
  81. PIEC101_10_T Iec10x = (PIEC101_10_T)Iec101_Sendbuf;
  82. len = IEC101_STABLE_LEN;
  83. Iec10x->_begin = IEC101_STABLE_BEGING;
  84. /*Ctrol*/
  85. Iec10x->_ctrl.down._dir = IEC101_CTRL_DIR_UP;
  86. Iec10x->_ctrl.down._prm = IEC101_CTRL_PRM_MASTER;
  87. Iec10x->_ctrl.down._fcb = IEC101_CTRL_FCB_OPPO_NONE;
  88. Iec10x->_ctrl.down._fcv = IEC101_CTRL_FCV_DISABLE;
  89. Iec10x->_ctrl.up._func = IEC101_CTRL_RESET_LINK;
  90. Iec10x->_addr = Iec10x_Sta_Addr;
  91. Iec10x->_cs = Iec101_Sendbuf[1] + Iec101_Sendbuf[2] + Iec101_Sendbuf[3];
  92. Iec10x->_end = IEC101_STABLE_END;
  93. /* enqueue to the transmisson queue */
  94. IEC10X_Enqueue(Iec101_Sendbuf, len, IEC10X_PRIO_INITLINK, NULL, NULL);
  95. return RET_SUCESS;
  96. }
  97. iec_8u IEC101_BuildFinInit(void)
  98. {
  99. iec_16u len = 0;
  100. iec_8u cs_temp = 0, i;
  101. PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
  102. PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
  103. PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
  104. len = IEC101_STABLE_LEN;
  105. /*head*/
  106. Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
  107. /*Ctrol*/
  108. Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
  109. Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
  110. Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_WITH_DATA;
  111. Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
  112. Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
  113. Iec10x->_addr = Iec10x_Sta_Addr;
  114. /*asdu*/
  115. asdu->_type = Iec10x_M_EI_NA_1;
  116. asdu->_num._num = 1;
  117. asdu->_reason._reason = IEC10X_ASDU_REASON_INIT;
  118. asdu->_addr = Iec10x_Sta_Addr;
  119. /*info*/
  120. info->_addr = IEC10X_INFO_ADDR_NONE;
  121. info->_element[0] = 0;
  122. /*len*/
  123. len = IEC101_VARIABLE_LEN + asdu->_num._num;
  124. Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
  125. /*end*/
  126. for (i = 4; i < len - 2; i++)
  127. {
  128. cs_temp += Iec101_Sendbuf[i];
  129. }
  130. Iec101_Sendbuf[len - 2] = cs_temp;
  131. Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
  132. // DumpHEX(Iec101_Sendbuf,len);
  133. /* enqueue to the transmisson queue */
  134. IEC10X_Enqueue(Iec101_Sendbuf, len, IEC10X_PRIO_INITLINK, NULL, NULL);
  135. return RET_SUCESS;
  136. }
  137. iec_8u IEC101_BuildActConfirm(iec_8u qoi, iec_8u Prio)
  138. {
  139. iec_16u len = 0;
  140. iec_8u cs_temp = 0, i;
  141. PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
  142. PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
  143. PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
  144. len = IEC101_STABLE_LEN;
  145. /*head*/
  146. Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
  147. /*Ctrol*/
  148. Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
  149. Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
  150. Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_NONE_DATA;
  151. Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
  152. Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
  153. Iec10x->_addr = Iec10x_Sta_Addr;
  154. /*asdu*/
  155. asdu->_type = IEC10X_C_IC_NA_1;
  156. asdu->_num._sq = 0;
  157. asdu->_num._num = 1;
  158. asdu->_reason._pn = 0;
  159. asdu->_reason._test = 0;
  160. asdu->_reason._reason = IEC10X_ASDU_REASON_ACTCON;
  161. asdu->_addr = Iec10x_Sta_Addr;
  162. /*info*/
  163. info->_addr = IEC10X_INFO_ADDR_NONE;
  164. info->_element[0] = qoi;
  165. /*len*/
  166. len = IEC101_VARIABLE_LEN + asdu->_num._num;
  167. Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
  168. /*end*/
  169. for (i = 4; i < len - 2; i++)
  170. {
  171. cs_temp += Iec101_Sendbuf[i];
  172. }
  173. Iec101_Sendbuf[len - 2] = cs_temp;
  174. Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
  175. // DumpHEX(Iec101_Sendbuf,len);
  176. /* enqueue to the transmisson queue */
  177. IEC10X_Enqueue(Iec101_Sendbuf, len, Prio, NULL, NULL);
  178. return RET_SUCESS;
  179. }
  180. iec_8u IEC101_BuildActFinish(iec_8u qoi, iec_8u Prio)
  181. {
  182. iec_16u len = 0;
  183. iec_8u cs_temp = 0, i;
  184. PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
  185. PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
  186. PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
  187. len = IEC101_STABLE_LEN;
  188. /*head*/
  189. Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
  190. /*Ctrol*/
  191. Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
  192. Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
  193. Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_WITH_DATA;
  194. Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
  195. Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
  196. Iec10x->_addr = Iec10x_Sta_Addr;
  197. /*asdu*/
  198. asdu->_type = IEC10X_C_IC_NA_1;
  199. asdu->_num._sq = 0;
  200. asdu->_num._num = 1;
  201. asdu->_reason._pn = 0;
  202. asdu->_reason._test = 0;
  203. asdu->_reason._reason = IEC10X_ASDU_REASON_ACTTERM;
  204. asdu->_addr = Iec10x_Sta_Addr;
  205. /*info*/
  206. info->_addr = IEC10X_INFO_ADDR_NONE;
  207. info->_element[0] = qoi;
  208. /*len*/
  209. len = IEC101_VARIABLE_LEN + asdu->_num._num;
  210. Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
  211. /*end*/
  212. for (i = 4; i < len - 2; i++)
  213. {
  214. cs_temp += Iec101_Sendbuf[i];
  215. }
  216. Iec101_Sendbuf[len - 2] = cs_temp;
  217. Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
  218. // DumpHEX(Iec101_Sendbuf,len);
  219. /* enqueue to the transmisson queue */
  220. IEC10X_Enqueue(Iec101_Sendbuf, len, Prio, NULL, NULL);
  221. return RET_SUCESS;
  222. }
  223. iec_8u IEC101_BuildSignal(iec_8u reason, iec_8u Prio)
  224. {
  225. iec_16u len = 0, addr;
  226. iec_8u cs_temp = 0, i, asdu_num = 0, *ptr = NULL, signal, sum;
  227. /*init struct*/
  228. PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
  229. PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
  230. PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
  231. /*get value*/
  232. IEC10X->GetStationInfo(&addr, 0, &asdu_num);
  233. len = IEC101_STABLE_LEN;
  234. LOG("-%s- total info (%d) \n\n", __FUNCTION__, asdu_num);
  235. /*head*/
  236. Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
  237. /*Ctrol*/
  238. Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
  239. Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
  240. Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_WITH_DATA;
  241. Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
  242. Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
  243. Iec10x->_addr = Iec10x_Sta_Addr;
  244. /*asdu*/
  245. asdu->_type = IEC10X_M_SP_NA_1;
  246. asdu->_num._sq = 1;
  247. asdu->_num._num = asdu_num;
  248. asdu->_reason._pn = 0;
  249. asdu->_reason._test = 0;
  250. asdu->_reason._reason = reason;
  251. asdu->_addr = Iec10x_Sta_Addr;
  252. /*info*/
  253. info->_addr = IEC10X_INFO_ADDR_SIG_BASE + IEC10X_INFO_ADDR_SIG_TEMP_HX_OFF;
  254. /*signal value*/
  255. ptr = info->_element;
  256. for (i = 0; i < asdu_num; i++)
  257. {
  258. if (IEC10X->GetStationInfo(&addr, i, &asdu_num) == RET_SUCESS)
  259. *ptr = 1;
  260. else
  261. *ptr = 0;
  262. ptr++;
  263. }
  264. /*len*/
  265. len = ptr + 2 - Iec101_Sendbuf;
  266. Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
  267. /*end*/
  268. for (i = 4; i < len - 2; i++)
  269. {
  270. cs_temp += Iec101_Sendbuf[i];
  271. }
  272. Iec101_Sendbuf[len - 2] = cs_temp;
  273. Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
  274. // DumpHEX(Iec101_Sendbuf,len);
  275. /* enqueue to the transmisson queue */
  276. IEC10X_Enqueue(Iec101_Sendbuf, len, Prio, NULL, NULL);
  277. return RET_SUCESS;
  278. }
  279. iec_8u IEC101_BuildDetect(iec_8u reason, iec_8u type, iec_8u Prio)
  280. {
  281. iec_16u len = 0, addr;
  282. iec_8u cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
  283. PIEC10X_DETECT_T detect = NULL;
  284. PIEC10X_DETECT_F_T detect_f = NULL;
  285. /*init struct*/
  286. PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
  287. PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
  288. PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
  289. /*get value*/
  290. IEC10X->GetStationInfo(&addr, 0, &asdu_num);
  291. LOG("-%s- total info (%d) \n\n", __FUNCTION__, asdu_num);
  292. len = IEC101_STABLE_LEN;
  293. /*head*/
  294. Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
  295. /*Ctrol*/
  296. Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
  297. Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
  298. Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_NONE_DATA;
  299. Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
  300. Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
  301. Iec10x->_addr = Iec10x_Sta_Addr;
  302. /*asdu*/
  303. asdu->_type = type;
  304. asdu->_num._sq = 1;
  305. asdu->_num._num = asdu_num;
  306. asdu->_reason._pn = 0;
  307. asdu->_reason._test = 0;
  308. asdu->_reason._reason = reason;
  309. asdu->_addr = Iec10x_Sta_Addr;
  310. /*info*/
  311. info->_addr = IEC10X_INFO_ADDR_DET + IEC10X_INFO_ADDR_DET_TEMP_HX_OFF;
  312. /*Detect value*/
  313. ptr = info->_element;
  314. for (i = 0; i < asdu_num; i++)
  315. {
  316. /*short int*/
  317. if (type == IEC10X_M_ME_NA_1)
  318. {
  319. detect = (PIEC10X_DETECT_T)ptr;
  320. detect->_detect = IEC10X->GetStaTemp(i);
  321. detect->_qds = 0;
  322. ptr += sizeof(IEC10X_DETECT_T);
  323. }
  324. /*float*/
  325. else if (type == IEC10X_M_ME_NC_1)
  326. {
  327. detect_f = (PIEC10X_DETECT_F_T)ptr;
  328. detect_f->_detect = IEC10X->GetStaTemp(i);
  329. detect_f->_qds = 0;
  330. ptr += sizeof(IEC10X_DETECT_F_T);
  331. }
  332. }
  333. /*len*/
  334. len = ptr + 2 - Iec101_Sendbuf; /* add cs+end*/
  335. Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
  336. /*end*/
  337. for (i = 4; i < len - 2; i++)
  338. {
  339. cs_temp += Iec101_Sendbuf[i];
  340. }
  341. Iec101_Sendbuf[len - 2] = cs_temp;
  342. Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
  343. // DumpHEX(Iec101_Sendbuf,len);
  344. /* enqueue to the transmisson queue */
  345. IEC10X_Enqueue(Iec101_Sendbuf, len, Prio, NULL, NULL);
  346. return RET_SUCESS;
  347. }
  348. iec_8u IEC101_BuildSignal_Spont(iec_8u TimeFlag, iec_8u signalV, iec_16u addrV)
  349. {
  350. iec_16u len = 0;
  351. iec_8u cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
  352. PCP56Time2a_T time = NULL;
  353. /*init struct*/
  354. PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
  355. PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
  356. PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
  357. if (TimeFlag != 1 && TimeFlag != 0)
  358. {
  359. LOG("-%s-, error time flag(%d) \n", __FUNCTION__, TimeFlag);
  360. return RET_ERROR;
  361. }
  362. LOG("-%s-, time flag(%d) signalV(%d) \n", __FUNCTION__, TimeFlag, signalV);
  363. /*get value*/
  364. asdu_num = 1;
  365. len = IEC101_STABLE_LEN;
  366. /*head*/
  367. Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
  368. /*Ctrol*/
  369. Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
  370. Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
  371. Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_WITH_DATA;
  372. Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
  373. Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
  374. Iec10x->_addr = Iec10x_Sta_Addr;
  375. /*asdu*/
  376. if (TimeFlag == 0)
  377. asdu->_type = IEC10X_M_SP_NA_1;
  378. else
  379. asdu->_type = IEC10X_M_SP_TB_1;
  380. asdu->_num._sq = 0;
  381. asdu->_num._num = asdu_num;
  382. asdu->_reason._pn = 0;
  383. asdu->_reason._test = 0;
  384. asdu->_reason._reason = IEC10X_COT_SPONT;
  385. asdu->_addr = Iec10x_Sta_Addr;
  386. /*info*/
  387. info->_addr = addrV;
  388. /*signal value*/
  389. ptr = info->_element;
  390. for (i = 0; i < asdu_num; i++)
  391. {
  392. *ptr = signalV;
  393. ptr++;
  394. }
  395. if (TimeFlag == 1)
  396. {
  397. time = (PCP56Time2a_T)ptr;
  398. IEC10X->GetTime(time);
  399. ptr += sizeof(CP56Time2a_T);
  400. }
  401. /*len*/
  402. len = ptr + 2 - Iec101_Sendbuf;
  403. Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
  404. /*end*/
  405. for (i = 4; i < len - 2; i++)
  406. {
  407. cs_temp += Iec101_Sendbuf[i];
  408. }
  409. Iec101_Sendbuf[len - 2] = cs_temp;
  410. Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
  411. // DumpHEX(Iec101_Sendbuf,len);
  412. /* enqueue to the transmisson queue */
  413. IEC10X_Enqueue(Iec101_Sendbuf, len, IEC10X_PRIO_SPON, NULL, NULL);
  414. return RET_SUCESS;
  415. }
  416. iec_8u IEC101_BuildDetect_Spont(iec_8u TimeFlag, PIEC10X_DETECT_T detectV, iec_16u addrV)
  417. {
  418. iec_16u len = 0;
  419. iec_8u cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
  420. PIEC10X_DETECT_T detect = NULL;
  421. PCP56Time2a_T time = NULL;
  422. /*init struct*/
  423. PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
  424. PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
  425. PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
  426. if (TimeFlag != 1 && TimeFlag != 0)
  427. {
  428. LOG("-%s-, error time flag(%d) \n", __FUNCTION__, TimeFlag);
  429. return RET_ERROR;
  430. }
  431. /*get value*/
  432. asdu_num = 1;
  433. len = IEC101_STABLE_LEN;
  434. /*head*/
  435. Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
  436. /*Ctrol*/
  437. Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
  438. Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
  439. Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_NONE_DATA;
  440. Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
  441. Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
  442. Iec10x->_addr = Iec10x_Sta_Addr;
  443. /*asdu*/
  444. if (TimeFlag == 0)
  445. asdu->_type = IEC10X_M_ME_NA_1;
  446. else
  447. asdu->_type = IEC10X_M_ME_TD_1;
  448. asdu->_num._sq = 0;
  449. asdu->_num._num = asdu_num;
  450. asdu->_reason._pn = 0;
  451. asdu->_reason._test = 0;
  452. asdu->_reason._reason = IEC10X_COT_SPONT;
  453. asdu->_addr = Iec10x_Sta_Addr;
  454. /*info*/
  455. info->_addr = addrV;
  456. /*Detect value*/
  457. ptr = info->_element;
  458. detect = (PIEC10X_DETECT_T)ptr;
  459. detect->_detect = detectV->_detect;
  460. detect->_qds = detectV->_qds;
  461. ptr += sizeof(IEC10X_DETECT_T);
  462. if (TimeFlag == 1)
  463. {
  464. time = (PCP56Time2a_T)ptr;
  465. IEC10X->GetTime(time);
  466. ptr += sizeof(CP56Time2a_T);
  467. }
  468. /*len*/
  469. len = ptr + 2 - Iec101_Sendbuf; /* add cs+end*/
  470. Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
  471. /*end*/
  472. for (i = 4; i < len - 2; i++)
  473. {
  474. cs_temp += Iec101_Sendbuf[i];
  475. }
  476. Iec101_Sendbuf[len - 2] = cs_temp;
  477. Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
  478. // DumpHEX(Iec101_Sendbuf,len);
  479. /* enqueue to the transmisson queue */
  480. IEC10X_Enqueue(Iec101_Sendbuf, len, IEC10X_PRIO_SPON, NULL, NULL);
  481. return RET_SUCESS;
  482. }
  483. iec_8u IEC101_BuildDetectF_Spont(iec_8u TimeFlag, float detectV, iec_16u addrV)
  484. {
  485. iec_16u len = 0;
  486. iec_8u cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
  487. PIEC10X_DETECT_F_T detect = NULL;
  488. PCP56Time2a_T time = NULL;
  489. /*init struct*/
  490. PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
  491. PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
  492. PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
  493. if (TimeFlag != 1 && TimeFlag != 0)
  494. {
  495. LOG("-%s-, error time flag(%d) \n", __FUNCTION__, TimeFlag);
  496. return RET_ERROR;
  497. }
  498. /*get value*/
  499. asdu_num = 1;
  500. len = IEC101_STABLE_LEN;
  501. /*head*/
  502. Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
  503. /*Ctrol*/
  504. Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
  505. Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
  506. Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_NONE_DATA;
  507. Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
  508. Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
  509. Iec10x->_addr = Iec10x_Sta_Addr;
  510. /*asdu*/
  511. if (TimeFlag == 0)
  512. asdu->_type = IEC10X_M_ME_NC_1;
  513. else
  514. asdu->_type = IEC10X_M_ME_TF_1;
  515. asdu->_num._sq = 0;
  516. asdu->_num._num = asdu_num;
  517. asdu->_reason._pn = 0;
  518. asdu->_reason._test = 0;
  519. asdu->_reason._reason = IEC10X_COT_SPONT;
  520. asdu->_addr = Iec10x_Sta_Addr;
  521. /*info*/
  522. info->_addr = addrV;
  523. /*Detect value*/
  524. ptr = info->_element;
  525. detect = (PIEC10X_DETECT_F_T)ptr;
  526. detect->_detect = detectV;
  527. detect->_qds = 0;
  528. ptr += sizeof(IEC10X_DETECT_F_T);
  529. if (TimeFlag == 1)
  530. {
  531. time = (PCP56Time2a_T)ptr;
  532. IEC10X->GetTime(time);
  533. ptr += sizeof(CP56Time2a_T);
  534. }
  535. /*len*/
  536. len = ptr + 2 - Iec101_Sendbuf; /* add cs+end*/
  537. Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
  538. /*end*/
  539. for (i = 4; i < len - 2; i++)
  540. {
  541. cs_temp += Iec101_Sendbuf[i];
  542. }
  543. Iec101_Sendbuf[len - 2] = cs_temp;
  544. Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
  545. // DumpHEX(Iec101_Sendbuf,len);
  546. /* enqueue to the transmisson queue */
  547. IEC10X_Enqueue(Iec101_Sendbuf, len, IEC10X_PRIO_SPON, NULL, NULL);
  548. return RET_SUCESS;
  549. }
  550. iec_8u IEC101_BuildDelayAct(iec_16u delay_time)
  551. {
  552. iec_16u len = 0;
  553. iec_8u cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
  554. iec_16u cp16time2a = delay_time;
  555. /*init struct*/
  556. PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
  557. PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
  558. PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
  559. /*get value*/
  560. asdu_num = 1;
  561. len = IEC101_STABLE_LEN;
  562. /*head*/
  563. Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
  564. /*Ctrol*/
  565. Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
  566. Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
  567. Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_NONE_DATA;
  568. Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
  569. Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
  570. Iec10x->_addr = Iec10x_Sta_Addr;
  571. /*asdu*/
  572. asdu->_type = IEC10X_C_CD_NA_1;
  573. asdu->_num._sq = 0;
  574. asdu->_num._num = asdu_num;
  575. asdu->_reason._pn = 0;
  576. asdu->_reason._test = 0;
  577. asdu->_reason._reason = IEC10X_COT_ACTCON;
  578. asdu->_addr = Iec10x_Sta_Addr;
  579. /*info*/
  580. info->_addr = IEC10X_INFO_ADDR_NONE;
  581. /*delay value*/
  582. ptr = info->_element;
  583. *(iec_16u *)ptr = IEC10X_Cp16time2a;
  584. ptr += 2;
  585. /*len*/
  586. len = ptr + 2 - Iec101_Sendbuf;
  587. Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
  588. /*end*/
  589. for (i = 4; i < len - 2; i++)
  590. {
  591. cs_temp += Iec101_Sendbuf[i];
  592. }
  593. Iec101_Sendbuf[len - 2] = cs_temp;
  594. Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
  595. // DumpHEX(Iec101_Sendbuf,len);
  596. /* enqueue to the transmisson queue */
  597. IEC10X_Enqueue(Iec101_Sendbuf, len, IEC10X_PRIO_DELAY, NULL, NULL);
  598. return RET_SUCESS;
  599. }
  600. iec_8u IEC101_BuildClockAct()
  601. {
  602. iec_16u len = 0;
  603. iec_8u cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
  604. /*init struct*/
  605. PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
  606. PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
  607. PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
  608. PCP56Time2a_T time = (PCP56Time2a_T)(info->_element);
  609. /*get value*/
  610. asdu_num = 1;
  611. len = IEC101_STABLE_LEN;
  612. /*head*/
  613. Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
  614. /*Ctrol*/
  615. Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
  616. Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
  617. Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_NONE_DATA;
  618. Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
  619. Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
  620. Iec10x->_addr = Iec10x_Sta_Addr;
  621. /*asdu*/
  622. asdu->_type = IEC10X_C_CS_NA_1;
  623. asdu->_num._sq = 0;
  624. asdu->_num._num = asdu_num;
  625. asdu->_reason._pn = 0;
  626. asdu->_reason._test = 0;
  627. asdu->_reason._reason = IEC10X_COT_ACTCON;
  628. asdu->_addr = Iec10x_Sta_Addr;
  629. /*info*/
  630. info->_addr = IEC10X_INFO_ADDR_NONE;
  631. /*clock value*/
  632. ptr = info->_element;
  633. // time;
  634. IEC10X->GetTime(time);
  635. ptr += sizeof(CP56Time2a_T);
  636. /*len*/
  637. len = ptr + 2 - Iec101_Sendbuf;
  638. Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
  639. /*end*/
  640. for (i = 4; i < len - 2; i++)
  641. {
  642. cs_temp += Iec101_Sendbuf[i];
  643. }
  644. Iec101_Sendbuf[len - 2] = cs_temp;
  645. Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
  646. // DumpHEX(Iec101_Sendbuf,len);
  647. /* enqueue to the transmisson queue */
  648. IEC10X_Enqueue(Iec101_Sendbuf, len, IEC10X_PRIO_CLOCK, NULL, NULL);
  649. return RET_SUCESS;
  650. }
  651. IEC10X_ASDU_CALL_Qoi(iec_8u qoi)
  652. {
  653. switch (qoi)
  654. {
  655. case IEC10X_CALL_QOI_TOTAL:
  656. LOG("-%s- call cmd active \n", __FUNCTION__);
  657. IEC101_BuildSignal(IEC10X_COT_INTROGEN, IEC10X_PRIO_CALLALL);
  658. IEC101_STATE_FLAG_CALLALL = IEC101_FLAG_CALL_SIG_TOTAL;
  659. break;
  660. case IEC10X_CALL_QOI_GROUP1:
  661. LOG("-%s- call cmd call group1 \n", __FUNCTION__);
  662. IEC101_STATE_FLAG_GROUP = IEC101_FLAG_CALL_GROUP;
  663. IEC101_BuildSignal(IEC10X_COT_INTRO1, IEC10X_PRIO_CALLGROUP);
  664. break;
  665. case IEC10X_CALL_QOI_GROUP2:
  666. LOG("-%s- call cmd call group2 \n", __FUNCTION__);
  667. IEC101_STATE_FLAG_GROUP = IEC101_FLAG_CALL_GROUP;
  668. IEC101_BuildSignal(IEC10X_COT_INTRO2, IEC10X_PRIO_CALLGROUP);
  669. break;
  670. case IEC10X_CALL_QOI_GROUP9:
  671. LOG("-%s- call cmd call group9 \n", __FUNCTION__);
  672. IEC101_STATE_FLAG_GROUP = IEC101_FLAG_CALL_GROUP;
  673. IEC101_BuildDetect(IEC10X_COT_INTRO9, IEC10X_M_ME_NA_1, IEC10X_PRIO_CALLGROUP);
  674. break;
  675. case IEC10X_CALL_QOI_GROUP10:
  676. LOG("-%s- call cmd call group10 \n", __FUNCTION__);
  677. IEC101_STATE_FLAG_GROUP = IEC101_FLAG_CALL_GROUP;
  678. IEC101_BuildDetect(IEC10X_COT_INTRO10, IEC10X_M_ME_NA_1, IEC10X_PRIO_CALLGROUP);
  679. break;
  680. default:
  681. LOG("-%s- call cmd active error(%d) \n", __FUNCTION__, qoi);
  682. break;
  683. }
  684. }
  685. iec_8u IEC10X_ASDU_Call(PIEC10X_ASDU_T Iec10x_Asdu)
  686. {
  687. PASDU_INFO_T asdu_info = (PASDU_INFO_T)(Iec10x_Asdu->_info);
  688. iec_8u qoi = asdu_info->_element[0];
  689. iec_8u Prio = 0;
  690. if (asdu_info->_addr != 0)
  691. {
  692. LOG("-%s- call cmd active error addr(%x) \n", __FUNCTION__, asdu_info->_addr);
  693. return RET_ERROR;
  694. }
  695. switch (Iec10x_Asdu->_reason._reason)
  696. {
  697. case IEC10X_ASDU_REASON_ACT:
  698. switch (qoi)
  699. {
  700. case IEC10X_CALL_QOI_TOTAL:
  701. Prio = IEC10X_PRIO_CALLALL;
  702. IEC101_STATE_FLAG_CALLALL = IEC101_FLAG_CALL_ACT_RET;
  703. IEC10X_Call_AllQoi = qoi;
  704. break;
  705. case IEC10X_CALL_QOI_GROUP1:
  706. case IEC10X_CALL_QOI_GROUP2:
  707. case IEC10X_CALL_QOI_GROUP9:
  708. case IEC10X_CALL_QOI_GROUP10:
  709. Prio = IEC10X_PRIO_CALLGROUP;
  710. IEC101_STATE_FLAG_GROUP = IEC101_FLAG_CALL_ACT_RET;
  711. IEC10X_Call_GroupQoi = qoi;
  712. break;
  713. default:
  714. LOG("-%s- call cmd error qoi(%d) \n", __FUNCTION__, qoi);
  715. return RET_ERROR;
  716. }
  717. IEC10X_ResConfirm(Prio);
  718. IEC101_BuildActConfirm(qoi, Prio);
  719. break;
  720. default:
  721. LOG("-%s- call cmd error reason(%d) \n", __FUNCTION__, Iec10x_Asdu->_reason._reason);
  722. break;
  723. }
  724. return RET_SUCESS;
  725. }
  726. iec_8u IEC10X_ASDU_Delay(PIEC10X_ASDU_T Iec10x_Asdu)
  727. {
  728. PASDU_INFO_T asdu_info = (PASDU_INFO_T)(Iec10x_Asdu->_info);
  729. if (asdu_info->_addr != 0)
  730. {
  731. LOG("-%s- delay cmd error addr(%d) \n", __FUNCTION__, asdu_info->_addr);
  732. return RET_ERROR;
  733. }
  734. switch (Iec10x_Asdu->_reason._reason)
  735. {
  736. case IEC10X_COT_ACT:
  737. IEC10X_Cp16time2a = *(iec_16u *)(asdu_info->_element);
  738. LOG("-%s- delay cmd (0x%x%x)(%d)ms \n", __FUNCTION__, asdu_info->_element[0], asdu_info->_element[1], IEC10X_Cp16time2a);
  739. IEC101_STATE_FLAG_DELAY = IEC101_FLAG_DELAY_ACT;
  740. IEC10X_ResConfirm(IEC10X_PRIO_DELAY);
  741. IEC101_BuildDelayAct(IEC10X_Cp16time2a);
  742. break;
  743. case IEC10X_COT_SPONT:
  744. IEC10X_Cp16time2a_V = *(iec_16u *)(asdu_info->_element);
  745. LOG("-%s- delay cmd delay value(%d)ms \n", __FUNCTION__, IEC10X_Cp16time2a_V);
  746. IEC10X_ResConfirm(IEC10X_PRIO_DELAY);
  747. break;
  748. default:
  749. LOG("-%s- delay cmd error reason(%d) \n", __FUNCTION__, Iec10x_Asdu->_reason._reason);
  750. break;
  751. }
  752. return RET_SUCESS;
  753. }
  754. iec_8u IEC10X_ASDU_CLOCK(PIEC10X_ASDU_T Iec10x_Asdu)
  755. {
  756. PASDU_INFO_T asdu_info = (PASDU_INFO_T)(Iec10x_Asdu->_info);
  757. PCP56Time2a_T time = (PCP56Time2a_T)(asdu_info->_element);
  758. memcpy(&IEC10X_Cp56time2a, asdu_info->_element, sizeof(CP56Time2a_T));
  759. if (asdu_info->_addr != 0)
  760. {
  761. LOG("-%s- Clock cmd error addr(%d) \n", __FUNCTION__, asdu_info->_addr);
  762. return RET_ERROR;
  763. }
  764. switch (Iec10x_Asdu->_reason._reason)
  765. {
  766. case IEC10X_COT_ACT:
  767. LOG("-%s- Clock cmd (20%d-%d-%d %d %d:%d:%d) delay(%d) \n", __FUNCTION__, IEC10X_Cp56time2a._year._year, IEC10X_Cp56time2a._month._month, IEC10X_Cp56time2a._day._dayofmonth,
  768. IEC10X_Cp56time2a._day._dayofweek, IEC10X_Cp56time2a._hour._hours, IEC10X_Cp56time2a._min._minutes, IEC10X_Cp56time2a._milliseconds, IEC10X_Cp16time2a_V);
  769. IEC10X_Cp56time2a._milliseconds += IEC10X_Cp16time2a_V;
  770. /*get time*/
  771. /*...*/
  772. IEC10X->SetTime(&IEC10X_Cp56time2a);
  773. IEC101_STATE_FLAG_DELAY = IEC101_FLAG_CLOCK_SYS;
  774. IEC10X_ResConfirm(IEC10X_PRIO_CLOCK);
  775. IEC101_BuildClockAct();
  776. break;
  777. case IEC10X_COT_SPONT:
  778. LOG("-%s- Clock cmd spont \n", __FUNCTION__);
  779. break;
  780. default:
  781. LOG("-%s- Clock cmd error reason(%d) \n", __FUNCTION__, Iec10x_Asdu->_reason._reason);
  782. break;
  783. }
  784. return RET_SUCESS;
  785. }
  786. iec_8u Iec10x_Deal_10(iec_8u *buf, iec_16u len)
  787. {
  788. iec_8u cfun, sta, i;
  789. iec_8u cs_temp = 0;
  790. PIEC101_10_T Iec10x_10 = NULL;
  791. Iec10x_10 = (PIEC101_10_T)buf;
  792. /* check check_sum*/
  793. for (i = 1; i < len - 2; i++)
  794. {
  795. cs_temp += buf[i];
  796. }
  797. if (Iec10x_10->_cs != cs_temp)
  798. {
  799. LOG("-%s-,check sum error(%x) \n", __FUNCTION__, cs_temp);
  800. return RET_ERROR;
  801. }
  802. /* check end of the frame*/
  803. if (Iec10x_10->_end != 0x16)
  804. {
  805. LOG("-%s-,iec10x_10 end error(%d) \n", __FUNCTION__, Iec10x_10->_end);
  806. return RET_ERROR;
  807. }
  808. /*deal the function code*/
  809. cfun = Iec10x_10->_ctrl.down._func;
  810. sta = Iec10x_10->_ctrl.down._prm;
  811. // IEC10X->GetLinkAddr();
  812. #ifdef IEC10XLOCK
  813. if (IEC10X->LOCK != NULL)
  814. IEC10X->LOCK();
  815. #endif
  816. if (sta == IEC101_CTRL_PRM_MASTER)
  817. {
  818. switch (cfun)
  819. {
  820. case IEC101_CTRL_RESET_LINK:
  821. LOG("++++Reset link... \n");
  822. IEC101_STATE_FLAG_INIT = IEC101_FLAG_RESET_LINK;
  823. break;
  824. case IEC101_CTRL_PULSE:
  825. LOG("++++PULSE... \n");
  826. IEC101_STATE_FLAG_PULSE = IEC101_FLAG_PULSE;
  827. IEC10X_ResConfirm(IEC10X_PRIO_PULSE);
  828. break;
  829. case IEC101_CTRL_SEND_DATA:
  830. LOG("++++Send data... \n");
  831. break;
  832. case IEC101_CTRL_REQ_LINK_STATUS:
  833. LOG("++++Request link status... \n");
  834. IEC101_STATE_FLAG_INIT = IEC101_FLAG_INIT_LINK;
  835. // Iec10x_Sta_Addr = IEC10X->GetLinkAddr();
  836. // Iec10x_Sta_Addr = 0x138a;
  837. // IEC10X_RetStatusOk(Iec10x_Sta_Addr);
  838. break;
  839. default:
  840. LOG("++++error DOWN function code (%d)... \n", cfun);
  841. break;
  842. }
  843. }
  844. else if (sta == IEC101_CTRL_PRM_SLAVE)
  845. {
  846. switch (cfun)
  847. {
  848. case IEC101_CTRL_RES_LINK_STATUS:
  849. LOG("++++Respon link status... \n");
  850. LOG("IEC101_FLAG_RESET_REMOTE_LINK \n");
  851. IEC10X_ResetLink();
  852. IEC101_STATE_FLAG_INIT = IEC101_FLAG_RESET_REMOTE_LINK;
  853. break;
  854. case IEC101_CTRL_RES_CONFIRM:
  855. LOG("++++Respon confirm... \n");
  856. Iec101_Respon_Confirm = 1;
  857. break;
  858. default:
  859. LOG("++++error UP function code (%d)... \n", cfun);
  860. break;
  861. }
  862. }
  863. #ifdef IEC10XLOCK
  864. if (IEC10X->UNLOCK != NULL)
  865. IEC10X->UNLOCK();
  866. #endif
  867. return RET_SUCESS;
  868. }
  869. iec_8u Iec10x_Deal_68(iec_8u *buf, iec_16u len)
  870. {
  871. iec_8u cfun, sta, i;
  872. iec_8u cs_temp = 0;
  873. PIEC101_68_T Iec10x_68 = NULL;
  874. PIEC10X_ASDU_T Iec10x_Asdu = NULL;
  875. Iec10x_68 = (PIEC101_68_T)buf;
  876. Iec10x_Asdu = (PIEC10X_ASDU_T)Iec10x_68->_asdu;
  877. /* check check_sum*/
  878. for (i = 4; i < len - 2; i++)
  879. {
  880. cs_temp += buf[i];
  881. }
  882. if (buf[len - 2] != cs_temp)
  883. {
  884. LOG("-%s-,iec10x_68 check sum error (%d)(%d) \n", __FUNCTION__, buf[len - 2], cs_temp);
  885. return RET_ERROR;
  886. }
  887. /* check end of the frame*/
  888. if (buf[len - 1] != IEC101_VARIABLE_END)
  889. {
  890. LOG("-%s-,iec10x_68 end error(%d) \n", __FUNCTION__, buf[len - 1]);
  891. return RET_ERROR;
  892. }
  893. /* check len of the receive frame */
  894. if (Iec10x_68->_len + 6 != len)
  895. {
  896. LOG("-%s-,iec10x_68 rec len error(%d)(%d) \n", __FUNCTION__, Iec10x_68->_len, len);
  897. return RET_ERROR;
  898. }
  899. /* check len of the frame */
  900. if (Iec10x_68->_len != Iec10x_68->_len_cfm)
  901. {
  902. LOG("-%s-,iec10x_68 len error(%d)(%d) \n", __FUNCTION__, Iec10x_68->_len, Iec10x_68->_len_cfm);
  903. return RET_ERROR;
  904. }
  905. /*deal the function code*/
  906. cfun = Iec10x_68->_ctrl.down._func;
  907. sta = Iec10x_68->_ctrl.down._prm;
  908. #ifdef IEC10XLOCK
  909. if (IEC10X->LOCK != NULL)
  910. IEC10X->LOCK();
  911. #endif
  912. switch (cfun)
  913. {
  914. case IEC101_CTRL_SEND_USR_DATA_ACK:
  915. Iec101_Respon_Confirm = 0;
  916. LOG("++++Send user data need ack... \n");
  917. switch (Iec10x_Asdu->_type)
  918. {
  919. case IEC10X_C_IC_NA_1:
  920. LOG("++++asdu type call cmd... \n");
  921. IEC10X_ASDU_Call(Iec10x_Asdu);
  922. break;
  923. case IEC10X_C_CD_NA_1:
  924. LOG("++++asdu type delay active... \n");
  925. IEC10X_ASDU_Delay(Iec10x_Asdu);
  926. break;
  927. case IEC10X_C_CS_NA_1:
  928. LOG("++++asdu type clock active... \n");
  929. IEC10X_ASDU_CLOCK(Iec10x_Asdu);
  930. break;
  931. default:
  932. break;
  933. }
  934. break;
  935. default:
  936. LOG("++++error function code (%d)... \n", cfun);
  937. break;
  938. }
  939. #ifdef IEC10XLOCK
  940. if (IEC10X->UNLOCK != NULL)
  941. IEC10X->UNLOCK();
  942. #endif
  943. return RET_SUCESS;
  944. }
  945. void Iex101_main(iec_8u *buf, iec_16u len)
  946. {
  947. iec_8u *BufTemp = NULL;
  948. iec_16s LenRemain, LenTmp;
  949. if (buf == NULL)
  950. {
  951. LOG("-%s-,buffer (null)", __FUNCTION__);
  952. return;
  953. }
  954. if (len <= 0 || len > IEC101_MAX_BUF_LEN)
  955. {
  956. LOG("-%s-,buffer len error(%d) \n", __FUNCTION__, len);
  957. return;
  958. }
  959. BufTemp = buf;
  960. LenRemain = len;
  961. while (BufTemp < buf + len)
  962. {
  963. if (BufTemp[0] == IEC101_STABLE_BEGING)
  964. {
  965. LenTmp = 6;
  966. IEC101_Pulse_Cnt = 0;
  967. if (LenRemain < 6)
  968. {
  969. LOG("_%s_,len error(%d) \n", __FUNCTION__, len);
  970. return;
  971. }
  972. Iec10x_Deal_10(BufTemp, LenTmp);
  973. }
  974. else if (BufTemp[0] == IEC101_VARIABLE_BEGING)
  975. {
  976. LenTmp = BufTemp[1] + 6;
  977. IEC101_Pulse_Cnt = 0;
  978. if (BufTemp[1] != BufTemp[2])
  979. {
  980. LOG("_%s_,len error(%d)(%d) \n", __FUNCTION__, BufTemp[1], BufTemp[2]);
  981. return;
  982. }
  983. Iec10x_Deal_68(BufTemp, LenTmp);
  984. }
  985. else
  986. {
  987. LOG("-%s-,head type error(%d) \n", __FUNCTION__, BufTemp[0]);
  988. return;
  989. }
  990. BufTemp += LenTmp;
  991. LenRemain -= LenTmp;
  992. }
  993. return;
  994. }
  995. iec_8u Iec101_StateMachine(void)
  996. {
  997. /*Init link*/
  998. switch (IEC101_STATE_FLAG_INIT)
  999. {
  1000. case IEC101_FLAG_LINK_CLOSED:
  1001. break;
  1002. case IEC101_FLAG_INIT_LINK:
  1003. LOG("IEC101_FLAG_INIT_LINK \n");
  1004. Iec101_Respon_Confirm = 0;
  1005. Iec10x_Sta_Addr = IEC10X->GetLinkAddr();
  1006. IEC10X_RetStatusOk(Iec10x_Sta_Addr);
  1007. IEC101_STATE_FLAG_INIT = IEC101_FLAG_LINK_IDLE;
  1008. break;
  1009. case IEC101_FLAG_RESET_LINK:
  1010. LOG("IEC101_FLAG_RESET_LINK \n");
  1011. IEC10X_ClearQ();
  1012. IEC10X_ResConfirm(IEC10X_PRIO_INITLINK);
  1013. IEC101_STATE_FLAG_CALLALL = IEC101_FLAG_LINK_CLOSED;
  1014. IEC101_STATE_FLAG_GROUP = IEC101_FLAG_LINK_CLOSED;
  1015. IEC101_STATE_FLAG_DELAY = IEC101_FLAG_LINK_CLOSED;
  1016. IEC101_STATE_FLAG_PULSE = IEC101_FLAG_LINK_CLOSED;
  1017. IEC101_STATE_FLAG_INIT = IEC101_FLAG_REQ_LINK;
  1018. break;
  1019. case IEC101_FLAG_REQ_LINK:
  1020. LOG("IEC101_FLAG_REQ_LINK \n");
  1021. IEC10X_ReqLinkStatus();
  1022. IEC101_STATE_FLAG_INIT = IEC101_FLAG_LINK_IDLE;
  1023. break;
  1024. case IEC101_FLAG_RESET_REMOTE_LINK:
  1025. if (Iec101_Respon_Confirm)
  1026. {
  1027. Iec101_Respon_Confirm = 0;
  1028. IEC101_STATE_FLAG_INIT = IEC101_FLAG_INIT_FIN;
  1029. LOG("IEC101_FLAG_INIT_FIN! \n");
  1030. IEC101_BuildFinInit();
  1031. }
  1032. break;
  1033. case IEC101_FLAG_INIT_FIN:
  1034. if (Iec101_Respon_Confirm)
  1035. {
  1036. Iec101_Respon_Confirm = 0;
  1037. IEC101_STATE_FLAG_INIT = IEC101_FLAG_CONNECT_OK;
  1038. }
  1039. break;
  1040. case IEC101_FLAG_CONNECT_OK:
  1041. case IEC101_FLAG_LINK_IDLE:
  1042. default:
  1043. break;
  1044. }
  1045. /*total call*/
  1046. switch (IEC101_STATE_FLAG_CALLALL)
  1047. {
  1048. case IEC101_FLAG_CALL_ACT:
  1049. break;
  1050. case IEC101_FLAG_CALL_ACT_RET:
  1051. if (Iec101_Respon_Confirm)
  1052. {
  1053. Iec101_Respon_Confirm = 0;
  1054. IEC10X_ASDU_CALL_Qoi(IEC10X_Call_AllQoi);
  1055. }
  1056. break;
  1057. case IEC101_FLAG_CALL_SIG_TOTAL:
  1058. if (Iec101_Respon_Confirm)
  1059. {
  1060. Iec101_Respon_Confirm = 0;
  1061. IEC101_STATE_FLAG_CALLALL = IEC101_FLAG_CALL_DET_TOTAL;
  1062. IEC101_BuildDetect(IEC10X_COT_INTROGEN, IEC10X_M_ME_NA_1, IEC10X_PRIO_CALLALL);
  1063. }
  1064. break;
  1065. case IEC101_FLAG_CALL_DET_TOTAL:
  1066. if (Iec101_Respon_Confirm)
  1067. {
  1068. Iec101_Respon_Confirm = 0;
  1069. IEC101_BuildActFinish(IEC10X_Call_AllQoi, IEC10X_PRIO_CALLALL);
  1070. IEC101_STATE_FLAG_CALLALL = IEC101_FLAG_CALL_ACT_FIN;
  1071. }
  1072. break;
  1073. case IEC101_FLAG_CALL_ACT_FIN:
  1074. if (Iec101_Respon_Confirm)
  1075. {
  1076. Iec101_Respon_Confirm = 0;
  1077. IEC101_STATE_FLAG_CALLALL = IEC101_FLAG_LINK_IDLE;
  1078. }
  1079. break;
  1080. case IEC101_FLAG_LINK_IDLE:
  1081. default:
  1082. break;
  1083. }
  1084. /*group call*/
  1085. switch (IEC101_STATE_FLAG_GROUP)
  1086. {
  1087. case IEC101_FLAG_CALL_ACT:
  1088. break;
  1089. case IEC101_FLAG_CALL_ACT_RET:
  1090. if (Iec101_Respon_Confirm)
  1091. {
  1092. Iec101_Respon_Confirm = 0;
  1093. IEC10X_ASDU_CALL_Qoi(IEC10X_Call_GroupQoi);
  1094. }
  1095. break;
  1096. case IEC101_FLAG_CALL_GROUP:
  1097. if (Iec101_Respon_Confirm)
  1098. {
  1099. Iec101_Respon_Confirm = 0;
  1100. IEC101_STATE_FLAG_GROUP = IEC101_FLAG_CALL_ACT_FIN;
  1101. IEC101_BuildActFinish(IEC10X_Call_GroupQoi, IEC10X_PRIO_CALLGROUP);
  1102. }
  1103. break;
  1104. case IEC101_FLAG_CALL_ACT_FIN:
  1105. if (Iec101_Respon_Confirm)
  1106. {
  1107. Iec101_Respon_Confirm = 0;
  1108. IEC101_STATE_FLAG_GROUP = IEC101_FLAG_LINK_IDLE;
  1109. }
  1110. break;
  1111. case IEC101_FLAG_LINK_IDLE:
  1112. default:
  1113. break;
  1114. }
  1115. /*delay and clock*/
  1116. switch (IEC101_STATE_FLAG_DELAY)
  1117. {
  1118. case IEC101_FLAG_DELAY_ACT:
  1119. break;
  1120. case IEC101_FLAG_CLOCK_SYS:
  1121. IEC101_STATE_FLAG_DELAY = IEC101_FLAG_LINK_IDLE;
  1122. break;
  1123. case IEC101_FLAG_LINK_IDLE:
  1124. default:
  1125. break;
  1126. }
  1127. /*pulse*/
  1128. switch (IEC101_STATE_FLAG_PULSE)
  1129. {
  1130. case IEC101_FLAG_PULSE:
  1131. break;
  1132. case IEC101_FLAG_LINK_IDLE:
  1133. break;
  1134. default:
  1135. break;
  1136. }
  1137. return RET_SUCESS;
  1138. }
  1139. #endif