12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358 |
- #include "iec101.h"
- #include "iec10x_conf.h"
- #include "iec10x_type.h"
- #ifdef IEC101_SET
- /*
- * GLOABLE VARIALBLE
- */
- iec_8u Iec101_Respon_Confirm = 0;
- iec_8u Iec101_Sendbuf[IEC101_MAX_BUF_LEN];
- iec_8u IEC10X_Call_AllQoi = 0;
- iec_8u IEC10X_Call_GroupQoi = 0;
- iec_16u IEC101_Pulse_Cnt = 0;
- /*
- * STATE
- * */
- iec_8u IEC101_STATE_FLAG_INIT = IEC101_FLAG_LINK_CLOSED;
- iec_8u IEC101_STATE_FLAG_CALLALL = IEC101_FLAG_LINK_CLOSED;
- iec_8u IEC101_STATE_FLAG_GROUP = IEC101_FLAG_LINK_CLOSED;
- iec_8u IEC101_STATE_FLAG_CLOCK = IEC101_FLAG_LINK_CLOSED;
- iec_8u IEC101_STATE_FLAG_DELAY = IEC101_FLAG_LINK_CLOSED;
- iec_8u IEC101_STATE_FLAG_PULSE = IEC101_FLAG_LINK_CLOSED;
- iec_8u IEC10X_RetStatusOk(iec_16u addr)
- {
- iec_16u len;
- PIEC101_10_T Iec10x = (PIEC101_10_T)Iec101_Sendbuf;
- len = IEC101_STABLE_LEN;
- Iec10x->_begin = IEC101_STABLE_BEGING;
- Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
- Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_SLAVE;
- Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_NONE_DATA;
- Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CAN_REC;
- Iec10x->_ctrl.up._func = IEC101_CTRL_RES_LINK_STATUS;
- Iec10x->_addr = addr;
- Iec10x->_cs = Iec101_Sendbuf[1] + Iec101_Sendbuf[2] + Iec101_Sendbuf[3];
- Iec10x->_end = IEC101_STABLE_END;
- /* enqueue to the transmisson queue */
- IEC10X_Enqueue(Iec101_Sendbuf, len, IEC10X_PRIO_INITLINK, NULL, NULL);
- return RET_SUCESS;
- }
- static iec_8u IEC10X_ResConfirm(iec_8u Prio)
- {
- iec_16u len;
- PIEC101_10_T Iec10x = (PIEC101_10_T)Iec101_Sendbuf;
- len = IEC101_STABLE_LEN;
- Iec10x->_begin = IEC101_STABLE_BEGING;
- Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
- Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_SLAVE;
- Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_NONE_DATA;
- Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CAN_REC;
- Iec10x->_ctrl.up._func = IEC101_CTRL_RES_CONFIRM;
- Iec10x->_addr = Iec10x_Sta_Addr;
- Iec10x->_cs = Iec101_Sendbuf[1] + Iec101_Sendbuf[2] + Iec101_Sendbuf[3];
- Iec10x->_end = IEC101_STABLE_END;
- /* enqueue to the transmisson queue */
- IEC10X_Enqueue(Iec101_Sendbuf, len, Prio, NULL, NULL);
- return RET_SUCESS;
- }
- iec_8u IEC10X_ReqLinkStatus()
- {
- iec_16u len;
- PIEC101_10_T Iec10x = (PIEC101_10_T)Iec101_Sendbuf;
- LOG("%s \n", __FUNCTION__);
- len = IEC101_STABLE_LEN;
- Iec10x->_begin = IEC101_STABLE_BEGING;
- /*Ctrol*/
- Iec10x->_ctrl.down._dir = IEC101_CTRL_DIR_UP;
- Iec10x->_ctrl.down._prm = IEC101_CTRL_PRM_MASTER;
- Iec10x->_ctrl.down._fcb = IEC101_CTRL_FCB_OPPO_NONE;
- Iec10x->_ctrl.down._fcv = IEC101_CTRL_FCV_DISABLE;
- Iec10x->_ctrl.up._func = IEC101_CTRL_REQ_LINK_STATUS;
- Iec10x->_addr = Iec10x_Sta_Addr;
- Iec10x->_cs = Iec101_Sendbuf[1] + Iec101_Sendbuf[2] + Iec101_Sendbuf[3];
- Iec10x->_end = IEC101_STABLE_END;
- /* enqueue to the transmisson queue */
- IEC10X_Enqueue(Iec101_Sendbuf, len, IEC10X_PRIO_INITLINK, NULL, NULL);
- return RET_SUCESS;
- }
- iec_8u IEC10X_ResetLink(void)
- {
- iec_16u len;
- PIEC101_10_T Iec10x = (PIEC101_10_T)Iec101_Sendbuf;
- len = IEC101_STABLE_LEN;
- Iec10x->_begin = IEC101_STABLE_BEGING;
- /*Ctrol*/
- Iec10x->_ctrl.down._dir = IEC101_CTRL_DIR_UP;
- Iec10x->_ctrl.down._prm = IEC101_CTRL_PRM_MASTER;
- Iec10x->_ctrl.down._fcb = IEC101_CTRL_FCB_OPPO_NONE;
- Iec10x->_ctrl.down._fcv = IEC101_CTRL_FCV_DISABLE;
- Iec10x->_ctrl.up._func = IEC101_CTRL_RESET_LINK;
- Iec10x->_addr = Iec10x_Sta_Addr;
- Iec10x->_cs = Iec101_Sendbuf[1] + Iec101_Sendbuf[2] + Iec101_Sendbuf[3];
- Iec10x->_end = IEC101_STABLE_END;
- /* enqueue to the transmisson queue */
- IEC10X_Enqueue(Iec101_Sendbuf, len, IEC10X_PRIO_INITLINK, NULL, NULL);
- return RET_SUCESS;
- }
- iec_8u IEC101_BuildFinInit(void)
- {
- iec_16u len = 0;
- iec_8u cs_temp = 0, i;
- PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
- PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
- PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
- len = IEC101_STABLE_LEN;
- /*head*/
- Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
- /*Ctrol*/
- Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
- Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
- Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_WITH_DATA;
- Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
- Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
- Iec10x->_addr = Iec10x_Sta_Addr;
- /*asdu*/
- asdu->_type = Iec10x_M_EI_NA_1;
- asdu->_num._num = 1;
- asdu->_reason._reason = IEC10X_ASDU_REASON_INIT;
- asdu->_addr = Iec10x_Sta_Addr;
- /*info*/
- info->_addr = IEC10X_INFO_ADDR_NONE;
- info->_element[0] = 0;
- /*len*/
- len = IEC101_VARIABLE_LEN + asdu->_num._num;
- Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
- /*end*/
- for (i = 4; i < len - 2; i++)
- {
- cs_temp += Iec101_Sendbuf[i];
- }
- Iec101_Sendbuf[len - 2] = cs_temp;
- Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
- // DumpHEX(Iec101_Sendbuf,len);
- /* enqueue to the transmisson queue */
- IEC10X_Enqueue(Iec101_Sendbuf, len, IEC10X_PRIO_INITLINK, NULL, NULL);
- return RET_SUCESS;
- }
- iec_8u IEC101_BuildActConfirm(iec_8u qoi, iec_8u Prio)
- {
- iec_16u len = 0;
- iec_8u cs_temp = 0, i;
- PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
- PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
- PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
- len = IEC101_STABLE_LEN;
- /*head*/
- Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
- /*Ctrol*/
- Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
- Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
- Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_NONE_DATA;
- Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
- Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
- Iec10x->_addr = Iec10x_Sta_Addr;
- /*asdu*/
- asdu->_type = IEC10X_C_IC_NA_1;
- asdu->_num._sq = 0;
- asdu->_num._num = 1;
- asdu->_reason._pn = 0;
- asdu->_reason._test = 0;
- asdu->_reason._reason = IEC10X_ASDU_REASON_ACTCON;
- asdu->_addr = Iec10x_Sta_Addr;
- /*info*/
- info->_addr = IEC10X_INFO_ADDR_NONE;
- info->_element[0] = qoi;
- /*len*/
- len = IEC101_VARIABLE_LEN + asdu->_num._num;
- Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
- /*end*/
- for (i = 4; i < len - 2; i++)
- {
- cs_temp += Iec101_Sendbuf[i];
- }
- Iec101_Sendbuf[len - 2] = cs_temp;
- Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
- // DumpHEX(Iec101_Sendbuf,len);
- /* enqueue to the transmisson queue */
- IEC10X_Enqueue(Iec101_Sendbuf, len, Prio, NULL, NULL);
- return RET_SUCESS;
- }
- iec_8u IEC101_BuildActFinish(iec_8u qoi, iec_8u Prio)
- {
- iec_16u len = 0;
- iec_8u cs_temp = 0, i;
- PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
- PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
- PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
- len = IEC101_STABLE_LEN;
- /*head*/
- Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
- /*Ctrol*/
- Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
- Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
- Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_WITH_DATA;
- Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
- Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
- Iec10x->_addr = Iec10x_Sta_Addr;
- /*asdu*/
- asdu->_type = IEC10X_C_IC_NA_1;
- asdu->_num._sq = 0;
- asdu->_num._num = 1;
- asdu->_reason._pn = 0;
- asdu->_reason._test = 0;
- asdu->_reason._reason = IEC10X_ASDU_REASON_ACTTERM;
- asdu->_addr = Iec10x_Sta_Addr;
- /*info*/
- info->_addr = IEC10X_INFO_ADDR_NONE;
- info->_element[0] = qoi;
- /*len*/
- len = IEC101_VARIABLE_LEN + asdu->_num._num;
- Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
- /*end*/
- for (i = 4; i < len - 2; i++)
- {
- cs_temp += Iec101_Sendbuf[i];
- }
- Iec101_Sendbuf[len - 2] = cs_temp;
- Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
- // DumpHEX(Iec101_Sendbuf,len);
- /* enqueue to the transmisson queue */
- IEC10X_Enqueue(Iec101_Sendbuf, len, Prio, NULL, NULL);
- return RET_SUCESS;
- }
- iec_8u IEC101_BuildSignal(iec_8u reason, iec_8u Prio)
- {
- iec_16u len = 0, addr;
- iec_8u cs_temp = 0, i, asdu_num = 0, *ptr = NULL, signal, sum;
- /*init struct*/
- PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
- PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
- PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
- /*get value*/
- IEC10X->GetStationInfo(&addr, 0, &asdu_num);
- len = IEC101_STABLE_LEN;
- LOG("-%s- total info (%d) \n\n", __FUNCTION__, asdu_num);
- /*head*/
- Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
- /*Ctrol*/
- Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
- Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
- Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_WITH_DATA;
- Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
- Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
- Iec10x->_addr = Iec10x_Sta_Addr;
- /*asdu*/
- asdu->_type = IEC10X_M_SP_NA_1;
- asdu->_num._sq = 1;
- asdu->_num._num = asdu_num;
- asdu->_reason._pn = 0;
- asdu->_reason._test = 0;
- asdu->_reason._reason = reason;
- asdu->_addr = Iec10x_Sta_Addr;
- /*info*/
- info->_addr = IEC10X_INFO_ADDR_SIG_BASE + IEC10X_INFO_ADDR_SIG_TEMP_HX_OFF;
- /*signal value*/
- ptr = info->_element;
- for (i = 0; i < asdu_num; i++)
- {
- if (IEC10X->GetStationInfo(&addr, i, &asdu_num) == RET_SUCESS)
- *ptr = 1;
- else
- *ptr = 0;
- ptr++;
- }
- /*len*/
- len = ptr + 2 - Iec101_Sendbuf;
- Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
- /*end*/
- for (i = 4; i < len - 2; i++)
- {
- cs_temp += Iec101_Sendbuf[i];
- }
- Iec101_Sendbuf[len - 2] = cs_temp;
- Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
- // DumpHEX(Iec101_Sendbuf,len);
- /* enqueue to the transmisson queue */
- IEC10X_Enqueue(Iec101_Sendbuf, len, Prio, NULL, NULL);
- return RET_SUCESS;
- }
- iec_8u IEC101_BuildDetect(iec_8u reason, iec_8u type, iec_8u Prio)
- {
- iec_16u len = 0, addr;
- iec_8u cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
- PIEC10X_DETECT_T detect = NULL;
- PIEC10X_DETECT_F_T detect_f = NULL;
- /*init struct*/
- PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
- PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
- PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
- /*get value*/
- IEC10X->GetStationInfo(&addr, 0, &asdu_num);
- LOG("-%s- total info (%d) \n\n", __FUNCTION__, asdu_num);
- len = IEC101_STABLE_LEN;
- /*head*/
- Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
- /*Ctrol*/
- Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
- Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
- Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_NONE_DATA;
- Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
- Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
- Iec10x->_addr = Iec10x_Sta_Addr;
- /*asdu*/
- asdu->_type = type;
- asdu->_num._sq = 1;
- asdu->_num._num = asdu_num;
- asdu->_reason._pn = 0;
- asdu->_reason._test = 0;
- asdu->_reason._reason = reason;
- asdu->_addr = Iec10x_Sta_Addr;
- /*info*/
- info->_addr = IEC10X_INFO_ADDR_DET + IEC10X_INFO_ADDR_DET_TEMP_HX_OFF;
- /*Detect value*/
- ptr = info->_element;
- for (i = 0; i < asdu_num; i++)
- {
- /*short int*/
- if (type == IEC10X_M_ME_NA_1)
- {
- detect = (PIEC10X_DETECT_T)ptr;
- detect->_detect = IEC10X->GetStaTemp(i);
- detect->_qds = 0;
- ptr += sizeof(IEC10X_DETECT_T);
- }
- /*float*/
- else if (type == IEC10X_M_ME_NC_1)
- {
- detect_f = (PIEC10X_DETECT_F_T)ptr;
- detect_f->_detect = IEC10X->GetStaTemp(i);
- detect_f->_qds = 0;
- ptr += sizeof(IEC10X_DETECT_F_T);
- }
- }
- /*len*/
- len = ptr + 2 - Iec101_Sendbuf; /* add cs+end*/
- Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
- /*end*/
- for (i = 4; i < len - 2; i++)
- {
- cs_temp += Iec101_Sendbuf[i];
- }
- Iec101_Sendbuf[len - 2] = cs_temp;
- Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
- // DumpHEX(Iec101_Sendbuf,len);
- /* enqueue to the transmisson queue */
- IEC10X_Enqueue(Iec101_Sendbuf, len, Prio, NULL, NULL);
- return RET_SUCESS;
- }
- iec_8u IEC101_BuildSignal_Spont(iec_8u TimeFlag, iec_8u signalV, iec_16u addrV)
- {
- iec_16u len = 0;
- iec_8u cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
- PCP56Time2a_T time = NULL;
- /*init struct*/
- PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
- PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
- PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
- if (TimeFlag != 1 && TimeFlag != 0)
- {
- LOG("-%s-, error time flag(%d) \n", __FUNCTION__, TimeFlag);
- return RET_ERROR;
- }
- LOG("-%s-, time flag(%d) signalV(%d) \n", __FUNCTION__, TimeFlag, signalV);
- /*get value*/
- asdu_num = 1;
- len = IEC101_STABLE_LEN;
- /*head*/
- Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
- /*Ctrol*/
- Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
- Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
- Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_WITH_DATA;
- Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
- Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
- Iec10x->_addr = Iec10x_Sta_Addr;
- /*asdu*/
- if (TimeFlag == 0)
- asdu->_type = IEC10X_M_SP_NA_1;
- else
- asdu->_type = IEC10X_M_SP_TB_1;
- asdu->_num._sq = 0;
- asdu->_num._num = asdu_num;
- asdu->_reason._pn = 0;
- asdu->_reason._test = 0;
- asdu->_reason._reason = IEC10X_COT_SPONT;
- asdu->_addr = Iec10x_Sta_Addr;
- /*info*/
- info->_addr = addrV;
- /*signal value*/
- ptr = info->_element;
- for (i = 0; i < asdu_num; i++)
- {
- *ptr = signalV;
- ptr++;
- }
- if (TimeFlag == 1)
- {
- time = (PCP56Time2a_T)ptr;
- IEC10X->GetTime(time);
- ptr += sizeof(CP56Time2a_T);
- }
- /*len*/
- len = ptr + 2 - Iec101_Sendbuf;
- Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
- /*end*/
- for (i = 4; i < len - 2; i++)
- {
- cs_temp += Iec101_Sendbuf[i];
- }
- Iec101_Sendbuf[len - 2] = cs_temp;
- Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
- // DumpHEX(Iec101_Sendbuf,len);
- /* enqueue to the transmisson queue */
- IEC10X_Enqueue(Iec101_Sendbuf, len, IEC10X_PRIO_SPON, NULL, NULL);
- return RET_SUCESS;
- }
- iec_8u IEC101_BuildDetect_Spont(iec_8u TimeFlag, PIEC10X_DETECT_T detectV, iec_16u addrV)
- {
- iec_16u len = 0;
- iec_8u cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
- PIEC10X_DETECT_T detect = NULL;
- PCP56Time2a_T time = NULL;
- /*init struct*/
- PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
- PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
- PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
- if (TimeFlag != 1 && TimeFlag != 0)
- {
- LOG("-%s-, error time flag(%d) \n", __FUNCTION__, TimeFlag);
- return RET_ERROR;
- }
- /*get value*/
- asdu_num = 1;
- len = IEC101_STABLE_LEN;
- /*head*/
- Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
- /*Ctrol*/
- Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
- Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
- Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_NONE_DATA;
- Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
- Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
- Iec10x->_addr = Iec10x_Sta_Addr;
- /*asdu*/
- if (TimeFlag == 0)
- asdu->_type = IEC10X_M_ME_NA_1;
- else
- asdu->_type = IEC10X_M_ME_TD_1;
- asdu->_num._sq = 0;
- asdu->_num._num = asdu_num;
- asdu->_reason._pn = 0;
- asdu->_reason._test = 0;
- asdu->_reason._reason = IEC10X_COT_SPONT;
- asdu->_addr = Iec10x_Sta_Addr;
- /*info*/
- info->_addr = addrV;
- /*Detect value*/
- ptr = info->_element;
- detect = (PIEC10X_DETECT_T)ptr;
- detect->_detect = detectV->_detect;
- detect->_qds = detectV->_qds;
- ptr += sizeof(IEC10X_DETECT_T);
- if (TimeFlag == 1)
- {
- time = (PCP56Time2a_T)ptr;
- IEC10X->GetTime(time);
- ptr += sizeof(CP56Time2a_T);
- }
- /*len*/
- len = ptr + 2 - Iec101_Sendbuf; /* add cs+end*/
- Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
- /*end*/
- for (i = 4; i < len - 2; i++)
- {
- cs_temp += Iec101_Sendbuf[i];
- }
- Iec101_Sendbuf[len - 2] = cs_temp;
- Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
- // DumpHEX(Iec101_Sendbuf,len);
- /* enqueue to the transmisson queue */
- IEC10X_Enqueue(Iec101_Sendbuf, len, IEC10X_PRIO_SPON, NULL, NULL);
- return RET_SUCESS;
- }
- iec_8u IEC101_BuildDetectF_Spont(iec_8u TimeFlag, float detectV, iec_16u addrV)
- {
- iec_16u len = 0;
- iec_8u cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
- PIEC10X_DETECT_F_T detect = NULL;
- PCP56Time2a_T time = NULL;
- /*init struct*/
- PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
- PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
- PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
- if (TimeFlag != 1 && TimeFlag != 0)
- {
- LOG("-%s-, error time flag(%d) \n", __FUNCTION__, TimeFlag);
- return RET_ERROR;
- }
- /*get value*/
- asdu_num = 1;
- len = IEC101_STABLE_LEN;
- /*head*/
- Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
- /*Ctrol*/
- Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
- Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
- Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_NONE_DATA;
- Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
- Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
- Iec10x->_addr = Iec10x_Sta_Addr;
- /*asdu*/
- if (TimeFlag == 0)
- asdu->_type = IEC10X_M_ME_NC_1;
- else
- asdu->_type = IEC10X_M_ME_TF_1;
- asdu->_num._sq = 0;
- asdu->_num._num = asdu_num;
- asdu->_reason._pn = 0;
- asdu->_reason._test = 0;
- asdu->_reason._reason = IEC10X_COT_SPONT;
- asdu->_addr = Iec10x_Sta_Addr;
- /*info*/
- info->_addr = addrV;
- /*Detect value*/
- ptr = info->_element;
- detect = (PIEC10X_DETECT_F_T)ptr;
- detect->_detect = detectV;
- detect->_qds = 0;
- ptr += sizeof(IEC10X_DETECT_F_T);
- if (TimeFlag == 1)
- {
- time = (PCP56Time2a_T)ptr;
- IEC10X->GetTime(time);
- ptr += sizeof(CP56Time2a_T);
- }
- /*len*/
- len = ptr + 2 - Iec101_Sendbuf; /* add cs+end*/
- Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
- /*end*/
- for (i = 4; i < len - 2; i++)
- {
- cs_temp += Iec101_Sendbuf[i];
- }
- Iec101_Sendbuf[len - 2] = cs_temp;
- Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
- // DumpHEX(Iec101_Sendbuf,len);
- /* enqueue to the transmisson queue */
- IEC10X_Enqueue(Iec101_Sendbuf, len, IEC10X_PRIO_SPON, NULL, NULL);
- return RET_SUCESS;
- }
- iec_8u IEC101_BuildDelayAct(iec_16u delay_time)
- {
- iec_16u len = 0;
- iec_8u cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
- iec_16u cp16time2a = delay_time;
- /*init struct*/
- PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
- PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
- PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
- /*get value*/
- asdu_num = 1;
- len = IEC101_STABLE_LEN;
- /*head*/
- Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
- /*Ctrol*/
- Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
- Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
- Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_NONE_DATA;
- Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
- Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
- Iec10x->_addr = Iec10x_Sta_Addr;
- /*asdu*/
- asdu->_type = IEC10X_C_CD_NA_1;
- asdu->_num._sq = 0;
- asdu->_num._num = asdu_num;
- asdu->_reason._pn = 0;
- asdu->_reason._test = 0;
- asdu->_reason._reason = IEC10X_COT_ACTCON;
- asdu->_addr = Iec10x_Sta_Addr;
- /*info*/
- info->_addr = IEC10X_INFO_ADDR_NONE;
- /*delay value*/
- ptr = info->_element;
- *(iec_16u *)ptr = IEC10X_Cp16time2a;
- ptr += 2;
- /*len*/
- len = ptr + 2 - Iec101_Sendbuf;
- Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
- /*end*/
- for (i = 4; i < len - 2; i++)
- {
- cs_temp += Iec101_Sendbuf[i];
- }
- Iec101_Sendbuf[len - 2] = cs_temp;
- Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
- // DumpHEX(Iec101_Sendbuf,len);
- /* enqueue to the transmisson queue */
- IEC10X_Enqueue(Iec101_Sendbuf, len, IEC10X_PRIO_DELAY, NULL, NULL);
- return RET_SUCESS;
- }
- iec_8u IEC101_BuildClockAct()
- {
- iec_16u len = 0;
- iec_8u cs_temp = 0, i, asdu_num = 0, *ptr = NULL;
- /*init struct*/
- PIEC101_68_T Iec10x = (PIEC101_68_T)Iec101_Sendbuf;
- PIEC10X_ASDU_T asdu = (PIEC10X_ASDU_T)(Iec10x->_asdu);
- PASDU_INFO_T info = (PASDU_INFO_T)(asdu->_info);
- PCP56Time2a_T time = (PCP56Time2a_T)(info->_element);
- /*get value*/
- asdu_num = 1;
- len = IEC101_STABLE_LEN;
- /*head*/
- Iec10x->_begin = Iec10x->_begin_cfm = IEC101_VARIABLE_BEGING;
- /*Ctrol*/
- Iec10x->_ctrl.up._dir = IEC101_CTRL_DIR_UP;
- Iec10x->_ctrl.up._prm = IEC101_CTRL_PRM_MASTER;
- Iec10x->_ctrl.up._acd = IEC101_CTRL_ACD_NONE_DATA;
- Iec10x->_ctrl.up._dfc = IEC101_CTRL_DFC_CANNOT_REC;
- Iec10x->_ctrl.up._func = IEC101_CTRL_SEND_USR_DATA_ACK;
- Iec10x->_addr = Iec10x_Sta_Addr;
- /*asdu*/
- asdu->_type = IEC10X_C_CS_NA_1;
- asdu->_num._sq = 0;
- asdu->_num._num = asdu_num;
- asdu->_reason._pn = 0;
- asdu->_reason._test = 0;
- asdu->_reason._reason = IEC10X_COT_ACTCON;
- asdu->_addr = Iec10x_Sta_Addr;
- /*info*/
- info->_addr = IEC10X_INFO_ADDR_NONE;
- /*clock value*/
- ptr = info->_element;
- // time;
- IEC10X->GetTime(time);
- ptr += sizeof(CP56Time2a_T);
- /*len*/
- len = ptr + 2 - Iec101_Sendbuf;
- Iec10x->_len = Iec10x->_len_cfm = len - 4 - 2; /*-start-len-len-start -cs-end*/
- /*end*/
- for (i = 4; i < len - 2; i++)
- {
- cs_temp += Iec101_Sendbuf[i];
- }
- Iec101_Sendbuf[len - 2] = cs_temp;
- Iec101_Sendbuf[len - 1] = IEC101_VARIABLE_END;
- // DumpHEX(Iec101_Sendbuf,len);
- /* enqueue to the transmisson queue */
- IEC10X_Enqueue(Iec101_Sendbuf, len, IEC10X_PRIO_CLOCK, NULL, NULL);
- return RET_SUCESS;
- }
- IEC10X_ASDU_CALL_Qoi(iec_8u qoi)
- {
- switch (qoi)
- {
- case IEC10X_CALL_QOI_TOTAL:
- LOG("-%s- call cmd active \n", __FUNCTION__);
- IEC101_BuildSignal(IEC10X_COT_INTROGEN, IEC10X_PRIO_CALLALL);
- IEC101_STATE_FLAG_CALLALL = IEC101_FLAG_CALL_SIG_TOTAL;
- break;
- case IEC10X_CALL_QOI_GROUP1:
- LOG("-%s- call cmd call group1 \n", __FUNCTION__);
- IEC101_STATE_FLAG_GROUP = IEC101_FLAG_CALL_GROUP;
- IEC101_BuildSignal(IEC10X_COT_INTRO1, IEC10X_PRIO_CALLGROUP);
- break;
- case IEC10X_CALL_QOI_GROUP2:
- LOG("-%s- call cmd call group2 \n", __FUNCTION__);
- IEC101_STATE_FLAG_GROUP = IEC101_FLAG_CALL_GROUP;
- IEC101_BuildSignal(IEC10X_COT_INTRO2, IEC10X_PRIO_CALLGROUP);
- break;
- case IEC10X_CALL_QOI_GROUP9:
- LOG("-%s- call cmd call group9 \n", __FUNCTION__);
- IEC101_STATE_FLAG_GROUP = IEC101_FLAG_CALL_GROUP;
- IEC101_BuildDetect(IEC10X_COT_INTRO9, IEC10X_M_ME_NA_1, IEC10X_PRIO_CALLGROUP);
- break;
- case IEC10X_CALL_QOI_GROUP10:
- LOG("-%s- call cmd call group10 \n", __FUNCTION__);
- IEC101_STATE_FLAG_GROUP = IEC101_FLAG_CALL_GROUP;
- IEC101_BuildDetect(IEC10X_COT_INTRO10, IEC10X_M_ME_NA_1, IEC10X_PRIO_CALLGROUP);
- break;
- default:
- LOG("-%s- call cmd active error(%d) \n", __FUNCTION__, qoi);
- break;
- }
- }
- iec_8u IEC10X_ASDU_Call(PIEC10X_ASDU_T Iec10x_Asdu)
- {
- PASDU_INFO_T asdu_info = (PASDU_INFO_T)(Iec10x_Asdu->_info);
- iec_8u qoi = asdu_info->_element[0];
- iec_8u Prio = 0;
- if (asdu_info->_addr != 0)
- {
- LOG("-%s- call cmd active error addr(%x) \n", __FUNCTION__, asdu_info->_addr);
- return RET_ERROR;
- }
- switch (Iec10x_Asdu->_reason._reason)
- {
- case IEC10X_ASDU_REASON_ACT:
- switch (qoi)
- {
- case IEC10X_CALL_QOI_TOTAL:
- Prio = IEC10X_PRIO_CALLALL;
- IEC101_STATE_FLAG_CALLALL = IEC101_FLAG_CALL_ACT_RET;
- IEC10X_Call_AllQoi = qoi;
- break;
- case IEC10X_CALL_QOI_GROUP1:
- case IEC10X_CALL_QOI_GROUP2:
- case IEC10X_CALL_QOI_GROUP9:
- case IEC10X_CALL_QOI_GROUP10:
- Prio = IEC10X_PRIO_CALLGROUP;
- IEC101_STATE_FLAG_GROUP = IEC101_FLAG_CALL_ACT_RET;
- IEC10X_Call_GroupQoi = qoi;
- break;
- default:
- LOG("-%s- call cmd error qoi(%d) \n", __FUNCTION__, qoi);
- return RET_ERROR;
- }
- IEC10X_ResConfirm(Prio);
- IEC101_BuildActConfirm(qoi, Prio);
- break;
- default:
- LOG("-%s- call cmd error reason(%d) \n", __FUNCTION__, Iec10x_Asdu->_reason._reason);
- break;
- }
- return RET_SUCESS;
- }
- iec_8u IEC10X_ASDU_Delay(PIEC10X_ASDU_T Iec10x_Asdu)
- {
- PASDU_INFO_T asdu_info = (PASDU_INFO_T)(Iec10x_Asdu->_info);
- if (asdu_info->_addr != 0)
- {
- LOG("-%s- delay cmd error addr(%d) \n", __FUNCTION__, asdu_info->_addr);
- return RET_ERROR;
- }
- switch (Iec10x_Asdu->_reason._reason)
- {
- case IEC10X_COT_ACT:
- IEC10X_Cp16time2a = *(iec_16u *)(asdu_info->_element);
- LOG("-%s- delay cmd (0x%x%x)(%d)ms \n", __FUNCTION__, asdu_info->_element[0], asdu_info->_element[1], IEC10X_Cp16time2a);
- IEC101_STATE_FLAG_DELAY = IEC101_FLAG_DELAY_ACT;
- IEC10X_ResConfirm(IEC10X_PRIO_DELAY);
- IEC101_BuildDelayAct(IEC10X_Cp16time2a);
- break;
- case IEC10X_COT_SPONT:
- IEC10X_Cp16time2a_V = *(iec_16u *)(asdu_info->_element);
- LOG("-%s- delay cmd delay value(%d)ms \n", __FUNCTION__, IEC10X_Cp16time2a_V);
- IEC10X_ResConfirm(IEC10X_PRIO_DELAY);
- break;
- default:
- LOG("-%s- delay cmd error reason(%d) \n", __FUNCTION__, Iec10x_Asdu->_reason._reason);
- break;
- }
- return RET_SUCESS;
- }
- iec_8u IEC10X_ASDU_CLOCK(PIEC10X_ASDU_T Iec10x_Asdu)
- {
- PASDU_INFO_T asdu_info = (PASDU_INFO_T)(Iec10x_Asdu->_info);
- PCP56Time2a_T time = (PCP56Time2a_T)(asdu_info->_element);
- memcpy(&IEC10X_Cp56time2a, asdu_info->_element, sizeof(CP56Time2a_T));
- if (asdu_info->_addr != 0)
- {
- LOG("-%s- Clock cmd error addr(%d) \n", __FUNCTION__, asdu_info->_addr);
- return RET_ERROR;
- }
- switch (Iec10x_Asdu->_reason._reason)
- {
- case IEC10X_COT_ACT:
- 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,
- IEC10X_Cp56time2a._day._dayofweek, IEC10X_Cp56time2a._hour._hours, IEC10X_Cp56time2a._min._minutes, IEC10X_Cp56time2a._milliseconds, IEC10X_Cp16time2a_V);
- IEC10X_Cp56time2a._milliseconds += IEC10X_Cp16time2a_V;
- /*get time*/
- /*...*/
- IEC10X->SetTime(&IEC10X_Cp56time2a);
- IEC101_STATE_FLAG_DELAY = IEC101_FLAG_CLOCK_SYS;
- IEC10X_ResConfirm(IEC10X_PRIO_CLOCK);
- IEC101_BuildClockAct();
- break;
- case IEC10X_COT_SPONT:
- LOG("-%s- Clock cmd spont \n", __FUNCTION__);
- break;
- default:
- LOG("-%s- Clock cmd error reason(%d) \n", __FUNCTION__, Iec10x_Asdu->_reason._reason);
- break;
- }
- return RET_SUCESS;
- }
- iec_8u Iec10x_Deal_10(iec_8u *buf, iec_16u len)
- {
- iec_8u cfun, sta, i;
- iec_8u cs_temp = 0;
- PIEC101_10_T Iec10x_10 = NULL;
- Iec10x_10 = (PIEC101_10_T)buf;
- /* check check_sum*/
- for (i = 1; i < len - 2; i++)
- {
- cs_temp += buf[i];
- }
- if (Iec10x_10->_cs != cs_temp)
- {
- LOG("-%s-,check sum error(%x) \n", __FUNCTION__, cs_temp);
- return RET_ERROR;
- }
- /* check end of the frame*/
- if (Iec10x_10->_end != 0x16)
- {
- LOG("-%s-,iec10x_10 end error(%d) \n", __FUNCTION__, Iec10x_10->_end);
- return RET_ERROR;
- }
- /*deal the function code*/
- cfun = Iec10x_10->_ctrl.down._func;
- sta = Iec10x_10->_ctrl.down._prm;
- // IEC10X->GetLinkAddr();
- #ifdef IEC10XLOCK
- if (IEC10X->LOCK != NULL)
- IEC10X->LOCK();
- #endif
- if (sta == IEC101_CTRL_PRM_MASTER)
- {
- switch (cfun)
- {
- case IEC101_CTRL_RESET_LINK:
- LOG("++++Reset link... \n");
- IEC101_STATE_FLAG_INIT = IEC101_FLAG_RESET_LINK;
- break;
- case IEC101_CTRL_PULSE:
- LOG("++++PULSE... \n");
- IEC101_STATE_FLAG_PULSE = IEC101_FLAG_PULSE;
- IEC10X_ResConfirm(IEC10X_PRIO_PULSE);
- break;
- case IEC101_CTRL_SEND_DATA:
- LOG("++++Send data... \n");
- break;
- case IEC101_CTRL_REQ_LINK_STATUS:
- LOG("++++Request link status... \n");
- IEC101_STATE_FLAG_INIT = IEC101_FLAG_INIT_LINK;
- // Iec10x_Sta_Addr = IEC10X->GetLinkAddr();
- // Iec10x_Sta_Addr = 0x138a;
- // IEC10X_RetStatusOk(Iec10x_Sta_Addr);
- break;
- default:
- LOG("++++error DOWN function code (%d)... \n", cfun);
- break;
- }
- }
- else if (sta == IEC101_CTRL_PRM_SLAVE)
- {
- switch (cfun)
- {
- case IEC101_CTRL_RES_LINK_STATUS:
- LOG("++++Respon link status... \n");
- LOG("IEC101_FLAG_RESET_REMOTE_LINK \n");
- IEC10X_ResetLink();
- IEC101_STATE_FLAG_INIT = IEC101_FLAG_RESET_REMOTE_LINK;
- break;
- case IEC101_CTRL_RES_CONFIRM:
- LOG("++++Respon confirm... \n");
- Iec101_Respon_Confirm = 1;
- break;
- default:
- LOG("++++error UP function code (%d)... \n", cfun);
- break;
- }
- }
- #ifdef IEC10XLOCK
- if (IEC10X->UNLOCK != NULL)
- IEC10X->UNLOCK();
- #endif
- return RET_SUCESS;
- }
- iec_8u Iec10x_Deal_68(iec_8u *buf, iec_16u len)
- {
- iec_8u cfun, sta, i;
- iec_8u cs_temp = 0;
- PIEC101_68_T Iec10x_68 = NULL;
- PIEC10X_ASDU_T Iec10x_Asdu = NULL;
- Iec10x_68 = (PIEC101_68_T)buf;
- Iec10x_Asdu = (PIEC10X_ASDU_T)Iec10x_68->_asdu;
- /* check check_sum*/
- for (i = 4; i < len - 2; i++)
- {
- cs_temp += buf[i];
- }
- if (buf[len - 2] != cs_temp)
- {
- LOG("-%s-,iec10x_68 check sum error (%d)(%d) \n", __FUNCTION__, buf[len - 2], cs_temp);
- return RET_ERROR;
- }
- /* check end of the frame*/
- if (buf[len - 1] != IEC101_VARIABLE_END)
- {
- LOG("-%s-,iec10x_68 end error(%d) \n", __FUNCTION__, buf[len - 1]);
- return RET_ERROR;
- }
- /* check len of the receive frame */
- if (Iec10x_68->_len + 6 != len)
- {
- LOG("-%s-,iec10x_68 rec len error(%d)(%d) \n", __FUNCTION__, Iec10x_68->_len, len);
- return RET_ERROR;
- }
- /* check len of the frame */
- if (Iec10x_68->_len != Iec10x_68->_len_cfm)
- {
- LOG("-%s-,iec10x_68 len error(%d)(%d) \n", __FUNCTION__, Iec10x_68->_len, Iec10x_68->_len_cfm);
- return RET_ERROR;
- }
- /*deal the function code*/
- cfun = Iec10x_68->_ctrl.down._func;
- sta = Iec10x_68->_ctrl.down._prm;
- #ifdef IEC10XLOCK
- if (IEC10X->LOCK != NULL)
- IEC10X->LOCK();
- #endif
- switch (cfun)
- {
- case IEC101_CTRL_SEND_USR_DATA_ACK:
- Iec101_Respon_Confirm = 0;
- LOG("++++Send user data need ack... \n");
- switch (Iec10x_Asdu->_type)
- {
- case IEC10X_C_IC_NA_1:
- LOG("++++asdu type call cmd... \n");
- IEC10X_ASDU_Call(Iec10x_Asdu);
- break;
- case IEC10X_C_CD_NA_1:
- LOG("++++asdu type delay active... \n");
- IEC10X_ASDU_Delay(Iec10x_Asdu);
- break;
- case IEC10X_C_CS_NA_1:
- LOG("++++asdu type clock active... \n");
- IEC10X_ASDU_CLOCK(Iec10x_Asdu);
- break;
- default:
- break;
- }
- break;
- default:
- LOG("++++error function code (%d)... \n", cfun);
- break;
- }
- #ifdef IEC10XLOCK
- if (IEC10X->UNLOCK != NULL)
- IEC10X->UNLOCK();
- #endif
- return RET_SUCESS;
- }
- void Iex101_main(iec_8u *buf, iec_16u len)
- {
- iec_8u *BufTemp = NULL;
- iec_16s LenRemain, LenTmp;
- if (buf == NULL)
- {
- LOG("-%s-,buffer (null)", __FUNCTION__);
- return;
- }
- if (len <= 0 || len > IEC101_MAX_BUF_LEN)
- {
- LOG("-%s-,buffer len error(%d) \n", __FUNCTION__, len);
- return;
- }
- BufTemp = buf;
- LenRemain = len;
- while (BufTemp < buf + len)
- {
- if (BufTemp[0] == IEC101_STABLE_BEGING)
- {
- LenTmp = 6;
- IEC101_Pulse_Cnt = 0;
- if (LenRemain < 6)
- {
- LOG("_%s_,len error(%d) \n", __FUNCTION__, len);
- return;
- }
- Iec10x_Deal_10(BufTemp, LenTmp);
- }
- else if (BufTemp[0] == IEC101_VARIABLE_BEGING)
- {
- LenTmp = BufTemp[1] + 6;
- IEC101_Pulse_Cnt = 0;
- if (BufTemp[1] != BufTemp[2])
- {
- LOG("_%s_,len error(%d)(%d) \n", __FUNCTION__, BufTemp[1], BufTemp[2]);
- return;
- }
- Iec10x_Deal_68(BufTemp, LenTmp);
- }
- else
- {
- LOG("-%s-,head type error(%d) \n", __FUNCTION__, BufTemp[0]);
- return;
- }
- BufTemp += LenTmp;
- LenRemain -= LenTmp;
- }
- return;
- }
- iec_8u Iec101_StateMachine(void)
- {
- /*Init link*/
- switch (IEC101_STATE_FLAG_INIT)
- {
- case IEC101_FLAG_LINK_CLOSED:
- break;
- case IEC101_FLAG_INIT_LINK:
- LOG("IEC101_FLAG_INIT_LINK \n");
- Iec101_Respon_Confirm = 0;
- Iec10x_Sta_Addr = IEC10X->GetLinkAddr();
- IEC10X_RetStatusOk(Iec10x_Sta_Addr);
- IEC101_STATE_FLAG_INIT = IEC101_FLAG_LINK_IDLE;
- break;
- case IEC101_FLAG_RESET_LINK:
- LOG("IEC101_FLAG_RESET_LINK \n");
- IEC10X_ClearQ();
- IEC10X_ResConfirm(IEC10X_PRIO_INITLINK);
- IEC101_STATE_FLAG_CALLALL = IEC101_FLAG_LINK_CLOSED;
- IEC101_STATE_FLAG_GROUP = IEC101_FLAG_LINK_CLOSED;
- IEC101_STATE_FLAG_DELAY = IEC101_FLAG_LINK_CLOSED;
- IEC101_STATE_FLAG_PULSE = IEC101_FLAG_LINK_CLOSED;
- IEC101_STATE_FLAG_INIT = IEC101_FLAG_REQ_LINK;
- break;
- case IEC101_FLAG_REQ_LINK:
- LOG("IEC101_FLAG_REQ_LINK \n");
- IEC10X_ReqLinkStatus();
- IEC101_STATE_FLAG_INIT = IEC101_FLAG_LINK_IDLE;
- break;
- case IEC101_FLAG_RESET_REMOTE_LINK:
- if (Iec101_Respon_Confirm)
- {
- Iec101_Respon_Confirm = 0;
- IEC101_STATE_FLAG_INIT = IEC101_FLAG_INIT_FIN;
- LOG("IEC101_FLAG_INIT_FIN! \n");
- IEC101_BuildFinInit();
- }
- break;
- case IEC101_FLAG_INIT_FIN:
- if (Iec101_Respon_Confirm)
- {
- Iec101_Respon_Confirm = 0;
- IEC101_STATE_FLAG_INIT = IEC101_FLAG_CONNECT_OK;
- }
- break;
- case IEC101_FLAG_CONNECT_OK:
- case IEC101_FLAG_LINK_IDLE:
- default:
- break;
- }
- /*total call*/
- switch (IEC101_STATE_FLAG_CALLALL)
- {
- case IEC101_FLAG_CALL_ACT:
- break;
- case IEC101_FLAG_CALL_ACT_RET:
- if (Iec101_Respon_Confirm)
- {
- Iec101_Respon_Confirm = 0;
- IEC10X_ASDU_CALL_Qoi(IEC10X_Call_AllQoi);
- }
- break;
- case IEC101_FLAG_CALL_SIG_TOTAL:
- if (Iec101_Respon_Confirm)
- {
- Iec101_Respon_Confirm = 0;
- IEC101_STATE_FLAG_CALLALL = IEC101_FLAG_CALL_DET_TOTAL;
- IEC101_BuildDetect(IEC10X_COT_INTROGEN, IEC10X_M_ME_NA_1, IEC10X_PRIO_CALLALL);
- }
- break;
- case IEC101_FLAG_CALL_DET_TOTAL:
- if (Iec101_Respon_Confirm)
- {
- Iec101_Respon_Confirm = 0;
- IEC101_BuildActFinish(IEC10X_Call_AllQoi, IEC10X_PRIO_CALLALL);
- IEC101_STATE_FLAG_CALLALL = IEC101_FLAG_CALL_ACT_FIN;
- }
- break;
- case IEC101_FLAG_CALL_ACT_FIN:
- if (Iec101_Respon_Confirm)
- {
- Iec101_Respon_Confirm = 0;
- IEC101_STATE_FLAG_CALLALL = IEC101_FLAG_LINK_IDLE;
- }
- break;
- case IEC101_FLAG_LINK_IDLE:
- default:
- break;
- }
- /*group call*/
- switch (IEC101_STATE_FLAG_GROUP)
- {
- case IEC101_FLAG_CALL_ACT:
- break;
- case IEC101_FLAG_CALL_ACT_RET:
- if (Iec101_Respon_Confirm)
- {
- Iec101_Respon_Confirm = 0;
- IEC10X_ASDU_CALL_Qoi(IEC10X_Call_GroupQoi);
- }
- break;
- case IEC101_FLAG_CALL_GROUP:
- if (Iec101_Respon_Confirm)
- {
- Iec101_Respon_Confirm = 0;
- IEC101_STATE_FLAG_GROUP = IEC101_FLAG_CALL_ACT_FIN;
- IEC101_BuildActFinish(IEC10X_Call_GroupQoi, IEC10X_PRIO_CALLGROUP);
- }
- break;
- case IEC101_FLAG_CALL_ACT_FIN:
- if (Iec101_Respon_Confirm)
- {
- Iec101_Respon_Confirm = 0;
- IEC101_STATE_FLAG_GROUP = IEC101_FLAG_LINK_IDLE;
- }
- break;
- case IEC101_FLAG_LINK_IDLE:
- default:
- break;
- }
- /*delay and clock*/
- switch (IEC101_STATE_FLAG_DELAY)
- {
- case IEC101_FLAG_DELAY_ACT:
- break;
- case IEC101_FLAG_CLOCK_SYS:
- IEC101_STATE_FLAG_DELAY = IEC101_FLAG_LINK_IDLE;
- break;
- case IEC101_FLAG_LINK_IDLE:
- default:
- break;
- }
- /*pulse*/
- switch (IEC101_STATE_FLAG_PULSE)
- {
- case IEC101_FLAG_PULSE:
- break;
- case IEC101_FLAG_LINK_IDLE:
- break;
- default:
- break;
- }
- return RET_SUCESS;
- }
- #endif
|