int BuildConnection(PLC *plc) { int path_size=0; char ip[16],path[40]; Log(LOG_DEBUG,"Building connection for %s (%d connections)\n",plc->PlcName,CONNECTIONs.Count); if ((plc==NULL)||(plc->Connection!=NULL)) return(0); Eip_Session *session=plc->Session; Eip_Connection *connection=plc->Connection; path_size=ParsePath(plc->PlcPath,ip,path); if (path_size>0) { // Creating Sessions if (plc->Connection==NULL) { // Creating Connections connection=FindConnection(plc->PlcPath,&PLCs); if (connection==NULL) // not found { if (plc->NetWork) connection=_ConnectPLCOverDHP(session, plc->PlcType, _Priority,_TimeOut_Ticks, (int)session, //TO_ConnID, GetSerial(), //ConnSerialNumber _OriginatorVendorID,_OriginatorSerialNumber,_TimeOutMultiplier, MAX_SAMPLE, _Transport, plc->NetWork, path, path_size); else connection=_ConnectPLCOverCNET(session, plc->PlcType, _Priority,_TimeOut_Ticks, (int)session, //TO_ConnID, GetSerial(), //ConnSerialNumber _OriginatorVendorID,_OriginatorSerialNumber,_TimeOutMultiplier, MAX_SAMPLE, _Transport, path, path_size); if (connection!=NULL) { plc->Connection=connection; AddChListe(&CONNECTIONs,connection); Log(LOG_DEBUG,"Connection (%p) created for PLC : %s (%s) (%d connections)\n",connection,plc->PlcName,cip_err_msg,CONNECTIONs.Count); connection->References=1; return(1); //CONNECTIONs[Connection_count++]=connection; } else { Log(LOG_CRIT,"Unable to create connection for Plc: %s (%s)\n",plc->PlcName,cip_err_msg); return(0); } } else { plc->Connection=connection; connection->References++; Log(LOG_DEBUG,"%s Sharing Connection (%p) with another PLC (%d references)\n",plc->PlcName,connection,connection->References); return(1); } } else return(0); } else { Log(LOG_CRIT,"Error while parsing IP/Path for Plc : %s\n",plc->PlcName); return(0); } }
int CheckConnection(PLC *plc) { if (plc==NULL) return(0); Log(LOG_DEBUG,"Checking Connection for %s\n",plc->PlcName); Eip_Connection *new_connection=NULL; Eip_Connection *connection=plc->Connection; Eip_Session *session=plc->Session; if (session==NULL) return(0); if (connection==NULL) { Log(LOG_DEBUG,"Connection for %s is NULL\n",plc->PlcName); return(BuildConnection(plc)); } BYTE *path=(BYTE*)(((void*)connection)+sizeof(Eip_Connection)); if (plc->NetWork) new_connection=_ConnectPLCOverDHP(session, plc->PlcType, _Priority,_TimeOut_Ticks, (int)session, //TO_ConnID, connection->ConnectionSerialNumber, //ConnSerialNumber connection->OriginatorVendorID, connection->OriginatorSerialNumber, _TimeOutMultiplier, MAX_SAMPLE, _Transport, plc->NetWork, path, connection->Path_size); else new_connection=_ConnectPLCOverCNET(session, plc->PlcType, _Priority,_TimeOut_Ticks, (int)session, //TO_ConnID, connection->ConnectionSerialNumber, //ConnSerialNumber connection->OriginatorVendorID, connection->OriginatorSerialNumber, _TimeOutMultiplier, MAX_SAMPLE, _Transport, path, connection->Path_size); //flush(session->query); //flush(session->reply); Log(LOG_DEBUG,"Checking Connection for %s : %s (%X/%X) -> %p\n",plc->PlcName,cip_err_msg,cip_errno,cip_ext_errno,new_connection); if (new_connection==NULL) { if ((cip_errno==0x01)&&(cip_ext_errno==0x100)) { Log(LOG_WARNING,"Connection OK for %s\n",plc->PlcName); return(1); // duplicate Forward open } // error }else { if (cip_errno) { free(new_connection); new_connection=NULL; } }; //Log(LOG_WARNING,"connection = %p\n",connection); RemoveChListe(&CONNECTIONs,connection); ELEMENT *elt=GetFirst(&PLCs); if (elt!=NULL) do { PLC *plc=elt->Data; if (plc->Connection==connection) plc->Connection=new_connection; } while ((elt=GetNext(&PLCs,elt))!=NULL); if (new_connection!=NULL) { AddChListe(&CONNECTIONs,new_connection); Log(LOG_WARNING,"Connection OK for %s (%d connections)\n",plc->PlcName,CONNECTIONs.Count); return(1); }else { Log(LOG_WARNING,"Connection Error for %s (%d connections)\n",plc->PlcName,CONNECTIONs.Count); return(0); } }
int readwritedata(PLC *plc,LISTE *var) { int res,i,path_size; char ip[16],path[40]; Eip_Session *session=NULL; Eip_Connection *connection=NULL; path_size=ParsePath(plc->PlcPath,ip,path); if (path_size<=0) return(1); session=OpenSession(ip); if (session!=NULL) { session->Sender_ContextL=getpid(); if (RegisterSession(session)>=0) { if (plc->NetWork) connection=_ConnectPLCOverDHP(session, plc->PlcType, _Priority,_TimeOut_Ticks, (int)session, //TO_ConnID, GetSerial(), //ConnSerialNumber _OriginatorVendorID,_OriginatorSerialNumber,_TimeOutMultiplier, UPDATERATE*500, _Transport, plc->NetWork, path, path_size); else connection=_ConnectPLCOverCNET(session, plc->PlcType, _Priority,_TimeOut_Ticks, (int)session, //TO_ConnID, GetSerial(), //ConnSerialNumber _OriginatorVendorID,_OriginatorSerialNumber,_TimeOutMultiplier, UPDATERATE*500, _Transport, path, path_size); /******* Connection is Ok **********/ if (connection!=NULL) { Log(LOG_DEBUG,"Connection (%p) created for PLC : %s\n",connection,cip_err_msg); /*********** Reading / Writing Value from Lgx ******************/ for(i=0;i<AB_VARCOUNT;i++) { double val_float; int val_int; TAG *atag=var->Data[i]; Log(LOG_INFO,"entering ReadData %s\n",atag->Value_Address); res=readplc(plc,session,connection,atag->Value_Address,AB_REAL,&val_float); if (res) Log(LOG_WARNING,"ReadData (%d): %s\n",res,cip_err_msg); Log(LOG_INFO,"ReadData (%d): %s\n",res,cip_err_msg); atag->Value=val_float; val_int=1; Log(LOG_INFO,"entering WriteData %s = %f\n",atag->Reset_Address,val_int); res=writeplc(plc,session,connection,atag->Reset_Address,AB_BIT,&val_int); if (res) Log(LOG_WARNING,"WriteData (%d): %s\n",res,cip_err_msg); Log(LOG_INFO,"WriteData (%d): %s\n",res,cip_err_msg); } /***************************************************************/ if ((res=Forward_Close(connection))>=0) Log(LOG_DEBUG,"Connection (%p) Killed\n",connection); else Log(LOG_WARNING,"Unable to kill Connection (%p)\n",connection); } else //connection=NULL { Log(LOG_CRIT,"Unable to create connection : %s\n",cip_err_msg); } if ((res=UnRegisterSession(session))>=0) Log(LOG_DEBUG,"Session (%p) Killed\n",session); else Log(LOG_WARNING,"Unable to kill session (%p)\n",session); //Log(LOG_WARNING,"closing session (%p)\n",session); CloseSession(session); return(0); }else // Prob RegisterSession { Log(LOG_CRIT,"Unable to register session : %s \n",cip_err_msg); CloseSession(session); return(1); } } else { Log(LOG_CRIT,"Unable to open session for : %s\n",cip_err_msg); return(1); } }