📄 testresource.cpp
字号:
//=================================================================//// TestResource.cpp//// Resource test class////=================================================================//####COPYRIGHTBEGIN####//// -------------------------------------------// The contents of this file are subject to the Cygnus eCos Public License// Version 1.0 (the "License"); you may not use this file except in// compliance with the License. You may obtain a copy of the License at// http://sourceware.cygnus.com/ecos// // Software distributed under the License is distributed on an "AS IS"// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the// License for the specific language governing rights and limitations under// the License.// // The Original Code is eCos - Embedded Cygnus Operating System, released// September 30, 1998.// // The Initial Developer of the Original Code is Cygnus. Portions created// by Cygnus are Copyright (C) 1998, 1999 Cygnus Solutions.// All Rights Reserved.// -------------------------------------------////####COPYRIGHTEND####//=================================================================//#####DESCRIPTIONBEGIN####//// Author(s): sdf// Contributors: sdf// Date: 1999-04-01// Description: This class abstracts a test resource for use in the testing infrastructure// Usage:////####DESCRIPTIONEND#####include "stdafx.h"#include "eCosTestUtils.h"#include "TestResource.h"CTestResource *CTestResource::pFirstInstance=0;unsigned int CTestResource::nCount=0;CTestResource::CTestResource( const char *pszName, CeCosTest::TargetType Target, const char *pszHost, int nPort ): m_strName(pszName), m_Target(Target), m_strHost(pszHost), m_nPort(nPort), m_bSim(true), m_bLocked(false), m_cControl1('\0'), m_cControl2('\0'), m_nBaud(0), m_nResetPort(0){ TRACE("Test resource %10s %10s %10s:%04d %d\n", Name(),CeCosTest::Image(m_Target),Host(),Port(),m_bSim); Chain();}CTestResource::CTestResource( const char *pszName, CeCosTest::TargetType Target, const char *pszHost, int nPort, const char * pszDownloadPort, int nBaud, const char *pszResetHost,unsigned int nResetPort, char cControl1, char cControl2 ): m_strName(pszName), m_Target(Target), m_strHost(pszHost), m_nPort(nPort), m_bSim(false), m_bLocked(false), m_cControl1(cControl1), m_cControl2(cControl2), m_nBaud(nBaud), m_strPort(pszDownloadPort), m_strResetHost(pszResetHost), m_nResetPort(nResetPort){ TRACE("Test resource %10s %10s %10s:%04d %d\n", Name(),CeCosTest::Image(m_Target),Host(),Port(),m_bSim); Chain();}CTestResource::~CTestResource(){ ENTERCRITICAL; if(m_pPrevInstance || m_pNextInstance){ nCount--; } if(pFirstInstance==this){ pFirstInstance=m_pNextInstance; } if(m_pPrevInstance){ m_pPrevInstance->m_pNextInstance=m_pNextInstance; } if(m_pNextInstance){ m_pNextInstance->m_pPrevInstance=m_pPrevInstance; } LEAVECRITICAL;}CTestResource * CTestResource::Lookup(const char * pszHost, int nPort, bool bSim){ LOCKRESOURCES; CTestResource *pResource; for(pResource=pFirstInstance;pResource;pResource=pResource->m_pNextInstance){ if(nPort==pResource->Port() && bSim==pResource->m_bSim && 0==strcmp(pszHost,pResource->Host())){ break; } } UNLOCKRESOURCES; return pResource;}unsigned int CTestResource::GetMatchCount (const CeCosTest::ExecutionParameters &e,bool bIgnoreLocking){ LOCKRESOURCES; unsigned int i=0; for(const CTestResource *pResource=pFirstInstance;pResource;pResource=pResource->m_pNextInstance){ if(pResource->Matches(e,bIgnoreLocking)){ i++; } } UNLOCKRESOURCES; return i;}unsigned int CTestResource::GetMatches (const CeCosTest::ExecutionParameters &e, const CTestResource **&ar,bool bIgnoreLocking){ LOCKRESOURCES; ar=new const CTestResource *[GetMatchCount(e)]; unsigned int i=0; for(CTestResource *pResource=pFirstInstance;pResource;pResource=pResource->m_pNextInstance){ if(pResource->Matches(e,bIgnoreLocking)){ ar[i++]=pResource; } } UNLOCKRESOURCES; return i;}unsigned int CTestResource::Choose (const CeCosTest::ExecutionParameters &e, const CTestResource * const * const &ar, unsigned int nCount, int nExclude){ LOCKRESOURCES; // Initial implementation is stochastic assert(nCount>0); assert(nExclude==-1 || (nCount>1 && (unsigned)nExclude<=nCount)); unsigned int rc; do { rc=rand() % nCount; } while (rc==(unsigned)nExclude); UNLOCKRESOURCES; return rc;}bool CTestResource::LoadFile(const char * pszFileName){ #ifdef _WIN32 if(0==pszFileName || '\0'==*pszFileName){ return LoadReg(); } #endif LOCKRESOURCES; DeleteAllInstances(); bool rc=true; FILE *f=fopen(pszFileName,"rt"); if(0==f){ TRACE("Failed to open %s - %s\n",pszFileName,strerror(errno)); rc=false; } else { int nLine=0; char szLine[256]; while(0!=fgets(szLine,sizeof szLine-1,f)){ nLine++; // Boardname target host port sim char szName[256]={'\0'}; char szTarget[256]={'\0'}; char szDownloadPort[256]={'\0'}; char szHost[256]={'\0'}; int nPort=0; char szSim[256]={'\0'}; sscanf(szLine,"%s %s %s %d %s",szName,szTarget,szHost,&nPort,szSim,szDownloadPort); if('\0'!=szName[0]){ CeCosTest::TargetType target=CeCosTest::FromStr(szTarget); if(target==CeCosTest::TargetTypeMax){ TRACE("%s:Illegal target type '%s' at line %d:\n%s\n",pszFileName,szTarget,nLine,szLine); rc=false; continue; } if('\0'==szHost[0]){ TRACE("%s:Illegal download host at line %d:\n%s\n",pszFileName,nLine,szLine); rc=false; continue; } if(0==nPort){ TRACE("%s:Illegal download port at line %d:\n%s\n",pszFileName,nLine,szLine); rc=false; continue; } bool bSim; switch(szSim[0]){ case '1': case 't': case 'T': case 'y': case 'Y': bSim=true; break; case '0': case 'f': case 'F': case 'n': case 'N': bSim=false; break; default: TRACE("%s:Illegal sim indicator '%c' at line %d:\n%s\n",pszFileName,szSim[0],nLine,szLine); rc=false; continue; } if(bSim){ new CTestResource(szName, target, szHost,nPort); } else { new CTestResource(szName, target, szHost,nPort,szDownloadPort,38400,"aloo",5000,'A','5'); } } } fclose(f); } UNLOCKRESOURCES; return rc;}void CTestResource::DeleteAllInstances(){ LOCKRESOURCES; while(pFirstInstance){ delete pFirstInstance; } UNLOCKRESOURCES;}void CTestResource::Output(FILE *f){ CTestResource *pResource; LOCKRESOURCES; for(pResource=pFirstInstance;pResource;pResource=pResource->m_pNextInstance){ fprintf(f,"%s\t%s:%04d %d %s\n",pResource->Name(),pResource->Host(),pResource->Port(),pResource->m_bSim,pResource->DownloadPort()); } UNLOCKRESOURCES;}bool CTestResource::SaveFile(const char * pszFileName){ #ifdef _WIN32 if(0==pszFileName || '\0'==*pszFileName){ return SaveReg(); } #endif LOCKRESOURCES; bool rc=false; FILE *f=fopen(pszFileName,"wt"); if(f){ Output(f); fclose(f); rc=true; } UNLOCKRESOURCES; return rc;}#ifdef _WIN32static const char szRegKey[]="Software\\Cygnus Solutions\\eCos\\testing";bool CTestResource::LoadReg (){ LOCKRESOURCES; DeleteAllInstances(); bool rc=true; HKEY hKey; if(ERROR_SUCCESS==RegOpenKeyEx (HKEY_CURRENT_USER, szRegKey, 0L, KEY_ENUMERATE_SUB_KEYS, &hKey)){ char szName[256]; DWORD dwSizeName=sizeof szName; FILETIME ftLastWriteTime; for(DWORD dwIndex=0;ERROR_SUCCESS==RegEnumKeyEx(hKey, dwIndex, szName, &dwSizeName, NULL, NULL, NULL, &ftLastWriteTime); dwIndex++){ HKEY hKey2; if(ERROR_SUCCESS!=RegOpenKeyEx (hKey, szName, 0L, KEY_READ, &hKey2)){ TRACE("Failed to open %s\\%s\n",szRegKey,szName); } else { char szHost[256]; DWORD dwSizeHost=sizeof szHost; char szTarget[MAX_PATH]; DWORD dwSizeTarget=sizeof szTarget; char szDownloadPort[256]={'\0'}; DWORD dwSizeDownloadPort=sizeof szDownloadPort; //char szDownloadPort2[256]={'\0'}; //DWORD dwSizeDownloadPort2=sizeof szDownloadPort2; char szResetHost[256]={'\0'}; DWORD dwSizeResetHost=sizeof szResetHost; int nResetPort=5000; DWORD dwSizeResetPort=sizeof nResetPort; int nPort; DWORD dwSizePort=sizeof nPort; int bSim; DWORD dwSizeSim=sizeof bSim; int cControl1='A'; DWORD dwSizeControl1=sizeof cControl1; int cControl2='1'; DWORD dwSizeControl2=sizeof cControl2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -