📄 ecostest.cpp
字号:
VTRACE("RunAgent(): returning false\n"); CeCosTestSocket::CloseSocket(nAuxListenSock); } return false;}CeCosTest::TargetType CeCosTest::TargetTypeValue(const char * pszStr){ // Allow underscores to be equivalent to hyphens CeCosTestUtils::String str(pszStr); for(char *c=str.GetBuffer();*c;c++){ if('_'==*c){ *c='-'; } } int t; for(t=0;t<TargetTypeMax;t++){ if(0==strnccmp(Image((TargetType)t),str)){ break; } } return (TargetType)t;}CeCosTest::StatusType CeCosTest::StatusTypeValue(const char * pszStr){ for(int i=0;i<StatusTypeMax;i++){ StatusType t=(StatusType)i; if(0==strnccmp(Image(t),pszStr)){ return t; } } return StatusTypeMax;}void CeCosTest::SendKeepAlives(bool &b){ VTRACE("Sending keepalives...\n"); while (!b){ CeCosTestUtils::Sleep(1000); AliveInfo ai; ai.Downloading=m_bDownloading; ai.Status=m_Status; ai.Number=1; // ensure non-zero send(&ai,sizeof ai); } VTRACE("Done sending keepalives...\n");}void CeCosTest::WaitForRemoteCompletion(){ CeCosTestUtils::Time ft0=CeCosTestUtils::Time::Now(); StatusType rPrev=StatusTypeMax; Trace("Waiting for result\n"); for(;;){ AliveInfo ai; if(!recv(&ai,sizeof ai,"result")){ Trace("Receive failed\n"); return; } if(0==memcmp(&ai,&AliveInfoZero,sizeof ai)){ break; } CeCosTestUtils::Duration d=CeCosTestUtils::Time::Now()-ft0; if(ai.Downloading){ Trace("Downloading : t=" WFS "\n",WF(d)); } else { StatusType r=ai.Status; if(r!=rPrev){ ft0=CeCosTestUtils::Time::Now(); rPrev=r; } Trace("Status=%s t=" WFS "\n",Image(r),WF(d)); } CeCosTestUtils::Sleep(1000); if(Cancelled==m_Status){ Trace("Cancelled\n"); return; } } VTRACE("Result ready\n"); recvResult(); VTRACE("WaitForRemoteCompletion: Sending an ack\n"); send("",1); // send an ack [n'importe quoi]}void CeCosTest::ConnectSocketToSerialThreadFunc(void *pParam){ int *arInfo=(int *)pParam; int nAuxListenSock=arInfo[0]; bool *pbStop=(bool *)arInfo[1]; TRACE("ConnectSocketToSerialThreadFunc sock=%d\n",nAuxListenSock); { #define MAX_CMD_LEN 128 typedef struct ser_filter_state { bool null_filter; int cmd_i; int cmd_flag; char cmd[MAX_CMD_LEN]; } ser_filter_state_t; ser_filter_state_t state; state.null_filter = 0; state.cmd_i = 0; state.cmd_flag = 0; extern bool CALLBACK serial_filter(void*& pBuf, unsigned int& nRead, CeCosTestSerial& serial, CeCosTestSocket& socket, void* pParem); ConnectSocketToSerial(nAuxListenSock, m_pPort->Name(), m_pPort->Baud(), &serial_filter, 0, &state, pbStop); } //ConnectSocketToSerial (nAuxListenSock, m_pPort->Name(), m_pPort->Baud(), 0,0,0, pbStop);}static bool CALLBACK DerefBool(void *pParam){ return *(bool *)pParam;}// Function called (on a separate thread) to process a successful connectionvoid CeCosTest::AcceptThreadFunc(void *pParam){ pParam; // prevent compiler warnings if(Sim()){ //m_pSock->SetSocketOptions(); int n; if(m_pSock->recvInteger(n,"file size")){ m_nFileSize=n; // Read file from the socket bool bCanRun=true; Trace("AcceptThreadFunc file size=%d reading...\n",m_nFileSize); FILE *f2; f2=fopen(m_strExecutable,"wb"); if(0==f2){ Log("Could not create %s - %s\n",(const char *)m_strExecutable,strerror(errno)); bCanRun=false; } unsigned int nBufSize=min(10000,m_nFileSize); char *buf=new char[nBufSize]; unsigned int nWritten=0; unsigned int nRead=0; while(nRead<m_nFileSize){ int nToRead=min(nBufSize,m_nFileSize-nRead); if(!recv( buf, nToRead, "executable")){ break; } nRead+=nToRead; if(0!=f2){ const char *c=buf; while(nToRead>0){ int w=fwrite(c,1,nToRead,f2); if(-1==w){ Log("Write error on %s - %s\n",(const char *)m_strExecutable,strerror(errno)); bCanRun=false; break; } nWritten+=w; c+=w; nToRead-=w; } } } delete [] buf; Trace("Accept - done reading [%d bytes read, %d bytes written]\n",nRead,nWritten); if(0!=f2){ fclose(f2); chmod(m_strExecutable,00700); // user read, write and execute } if(0!=f2 && m_nFileSize!=nWritten){ Log("Failed to create %s correctly [%d/%d bytes written]\n",(const char *)m_strExecutable, nWritten, m_nFileSize); bCanRun=false; } SetExecutable(m_strExecutable); // to set stripped length and title if(bCanRun){ bool b=false; if(RunLocal(Callback(0,&b))){ SendKeepAlives(b); } } send(&AliveInfoZero,sizeof AliveInfoZero); // send a zero byte unlink(m_strExecutable); } sendResult(); char c; m_pSock->recv(&c,1); // receive an ack } else { // HW bool bTargetReady=false; if('\0'==*(m_pPort->ResetHost())){ bTargetReady=true;TRACE("No reset possible\n"); } else {TRACE("Attempting to reset target\n"); for(int nTry=1;nTry<=3;nTry++){ int nErr; if(m_pPort->Reset(nErr, m_strOutput)){ if(0==m_strOutput.GetLength()){ Log("Could not reset target (board silent after power on) [attempt %d/3]\n",nTry); } else { bTargetReady=true; break; } } else { Log("Could not reset target [attempt %d/3] - rc=%d\n",nTry,nErr); } } }TRACE("bTargetReady=%d\n",bTargetReady); char cAck; int dTimeout=m_ep.ElapsedTimeout()+max(3*m_ep.ActiveTimeout(),15*60*1000); if(bTargetReady){ if(CeCosTestUtils::IsLegalHostPort(m_pPort->Name())){TRACE("Sending %s\n",(const char *)m_pPort->Name()); m_pSock->sendString(m_pPort->Name(),"Port name"); m_pSock->recv(&cAck,1,"Terminating ack",dTimeout);TRACE("Terminating ack=%d\n",cAck); } else { CeCosTestUtils::String strHostPort; char *pszMe=strHostPort.GetBuffer(256); gethostname(pszMe,256); sprintf(pszMe+strlen(pszMe),":%d",nAuxPort); strHostPort.ReleaseBuffer();TRACE("Using %s\n",(const char *)strHostPort); if(m_pSock->sendString(strHostPort)){ // This Boolean signifies that the serial<-->tcp/ip conversation is done. It may be set // on completion of the ConnectSocketToSerial thread (which is why we pass it to runthread) // and also set by us to *cause* the thread to complete. bool bConnectSocketToSerialThreadDone=false; // Indication of termination of ConnectSocketToSerial thread bool bStop=false; // Used to tap ConnectSocketToSerial thread on the shoulder int arInfo[2]={nAuxListenSock,(int)&bStop}; RUNTHREAD(ConnectSocketToSerialThreadFunc,(void *)arInfo,Callback(0,&bConnectSocketToSerialThreadDone)); // Wait for either client or the ConnectSocketToSerial thread to finish. m_pSock->recv(&cAck,1,"Terminating ack",dTimeout,DerefBool,&bConnectSocketToSerialThreadDone);TRACE("Terminating ack=%d\n",cAck); if(!bConnectSocketToSerialThreadDone){ // Tap ConnectSocketToSerial thread on the shoulder TRACE("Waiting for ConnectSocketToSerial thread to terminate...\n"); bStop=true; CeCosTestUtils::WaitFor(bConnectSocketToSerialThreadDone,0x7fffffff); } TRACE("ConnectSocketToSerial thread terminated...\n"); } } } else {TRACE("Sending '%s'\n",(const char *)m_strOutput); m_pSock->sendString(m_strOutput); m_pSock->recv(&cAck,1,"Terminating ack",dTimeout);TRACE("Terminating ack=%d\n",cAck); } } delete m_pSock; m_pSock=0; // Can't delete "this" pointer here (need to allow LogResult to operate) - allow callback to do it}bool CeCosTest::send(const void *pData,unsigned int nLength,const char *pszMsg,CeCosTestUtils::Duration dTimeout){ return m_pSock->send(pData,nLength,pszMsg,dTimeout,IsCancelled,this);}bool CeCosTest::recv(const void *pData,unsigned int nLength,const char *pszMsg,CeCosTestUtils::Duration dTimeout){ return m_pSock->recv(pData,nLength,pszMsg,dTimeout,IsCancelled,this);}void CeCosTest::Log(const char * pszFormat, ...){ va_list args; va_start(args, pszFormat); CeCosTestUtils::String str; str.vFormat(pszFormat,args); va_end(args); LogString(str);}void CeCosTest::LogString(const char *psz){ if(*psz){ Trace("%s",psz); ENTERCRITICAL; if(0==strLogFile.GetLength()){ // Interactive mode fputs(psz,stdout); fflush(stdout); } m_strOutput+=psz; LEAVECRITICAL; }}bool CeCosTest::sendResult(CeCosTestUtils::Duration dTimeout){ bool rc=false; int nSize=sizeof(Result)+m_strOutput.GetLength(); // A result has space for one byte in its definition Result *r=(Result *)malloc(nSize); if(m_pSock->sendInteger(nSize,"result size",dTimeout)){ memcpy(r->buf,m_ep.Marshall(),sizeof(ExecutionParameters::Data)); strcpy(r->szOutput,m_strOutput); r->m_Status=m_Status; r->m_nDownloadTime=m_nDownloadTime; r->m_nTotalTime=m_nTotalTime; r->m_nMaxInactiveTime=m_nMaxInactiveTime; r->m_nStrippedSize=m_nStrippedSize; rc=send(r,nSize,"result"); } free(r); return rc;}bool CeCosTest::recvResult(CeCosTestUtils::Duration dTimeout){ bool rc=false; int nSize; if(m_pSock->recvInteger(nSize),"result size",dTimeout){ Result *r=(Result *)malloc(nSize); // Populate with "safe" default values memset(r,0,nSize); if(recv(r,nSize,"result")){ m_ep=ExecutionParameters(r->buf); m_Status=(StatusType)min(r->m_Status,StatusTypeMax); m_nDownloadTime=r->m_nDownloadTime; m_nTotalTime=r->m_nTotalTime; m_nMaxInactiveTime=r->m_nMaxInactiveTime; m_nStrippedSize=r->m_nStrippedSize; rc=true; } LogString(r->szOutput); free(r); } return rc;}THREAD_ID CeCosTest::RunThread(void (CeCosTest::*pThreadFunc)(void *), void *pParam, const Callback &cb){ VTRACE("Entered RunThread()\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -