OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [host/] [tools/] [ecostest/] [common/] [TestResource.cpp] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
// ####ECOSHOSTGPLCOPYRIGHTBEGIN####                                        
2
// -------------------------------------------                              
3
// This file is part of the eCos host tools.                                
4
// Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.            
5
//
6
// This program is free software; you can redistribute it and/or modify     
7
// it under the terms of the GNU General Public License as published by     
8
// the Free Software Foundation; either version 2 or (at your option) any   
9
// later version.                                                           
10
//
11
// This program is distributed in the hope that it will be useful, but      
12
// WITHOUT ANY WARRANTY; without even the implied warranty of               
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        
14
// General Public License for more details.                                 
15
//
16
// You should have received a copy of the GNU General Public License        
17
// along with this program; if not, write to the                            
18
// Free Software Foundation, Inc., 51 Franklin Street,                      
19
// Fifth Floor, Boston, MA  02110-1301, USA.                                
20
// -------------------------------------------                              
21
// ####ECOSHOSTGPLCOPYRIGHTEND####                                          
22
//=================================================================
23
//
24
//        TestResource.cpp
25
//
26
//        Resource test class
27
//
28
//=================================================================
29
//=================================================================
30
//#####DESCRIPTIONBEGIN####
31
//
32
// Author(s):     sdf
33
// Contributors:  sdf
34
// Date:          1999-04-01
35
// Description:   This class abstracts a test resource for use in the testing infrastructure
36
// Usage:
37
//
38
//####DESCRIPTIONEND####
39
#include "eCosStd.h"
40
#include "eCosTestUtils.h"
41
#include "eCosTrace.h"
42
#include "Subprocess.h"
43
 
44
#include "TestResource.h"
45
 
46
CTestResource *CTestResource::pFirstInstance=0;
47
unsigned int CTestResource::nCount=0;
48
 
49
String CTestResource::strResourceHostPort;
50
 
51
CTestResource::CTestResource(LPCTSTR pszHostPort, LPCTSTR target, LPCTSTR  pszDownloadPort, int nBaud, LPCTSTR pszResetString):
52
  m_strReset(pszResetString),
53
  m_bInUse(false),
54
  m_nBaud(nBaud),
55
  m_strPort(pszDownloadPort),
56
  m_bLocked(false),
57
  m_Target(target)
58
{
59
  CeCosSocket::ParseHostPort(pszHostPort,m_strHost,m_nPort);
60
  VTRACE(_T("@@@ Created resource %08x %s\n"),(unsigned int)this,(LPCTSTR)Image());
61
  Chain();
62
}
63
 
64
CTestResource::~CTestResource()
65
{
66
  ENTERCRITICAL;
67
  VTRACE(_T("@@@ Destroy resource %08x %s\n"),this,(LPCTSTR)Image());
68
  if(m_pPrevInstance || m_pNextInstance){
69
    nCount--;
70
  }
71
  if(pFirstInstance==this){
72
    pFirstInstance=m_pNextInstance;
73
  }
74
  if(m_pPrevInstance){
75
    m_pPrevInstance->m_pNextInstance=m_pNextInstance;
76
  }
77
  if(m_pNextInstance){
78
    m_pNextInstance->m_pPrevInstance=m_pPrevInstance;
79
  }
80
  LEAVECRITICAL;
81
}
82
 
83
// Find the resource matching the given host:port specification
84
// Returns 0 if no such host:port found
85
CTestResource * CTestResource::Lookup(LPCTSTR pszHostPort)
86
{
87
  CTestResource *pResource=NULL;
88
  ENTERCRITICAL;
89
  String strHost;
90
  int nPort;
91
  if(CeCosSocket::ParseHostPort(pszHostPort,strHost,nPort)){
92
    for(pResource=pFirstInstance;pResource;pResource=pResource->m_pNextInstance){
93
      if(nPort==pResource->TcpIPPort() && CeCosSocket::SameHost(strHost,pResource->Host())){
94
        break;
95
      }
96
    }
97
  }
98
  LEAVECRITICAL;
99
  return pResource;
100
}
101
 
102
unsigned int CTestResource::GetMatchCount (const CeCosTest::ExecutionParameters &e,bool bIgnoreLocking)
103
{
104
  unsigned int i=0;
105
  ENTERCRITICAL;
106
  for(const CTestResource *pResource=pFirstInstance;pResource;pResource=pResource->m_pNextInstance){
107
    if(pResource->Matches(e,bIgnoreLocking)){
108
      i++;
109
    }
110
  }
111
  LEAVECRITICAL;
112
  return i;
113
}
114
 
115
bool CTestResource::GetMatches (const CeCosTest::ExecutionParameters &e, StringArray &arstr, bool bIgnoreLocking)
116
{
117
  bool rc=false;
118
  arstr.clear();
119
  if(Load()){
120
    ENTERCRITICAL;
121
    for(CTestResource *pResource=pFirstInstance;pResource;pResource=pResource->m_pNextInstance){
122
                  if(pResource->Matches(e,bIgnoreLocking)){
123
        arstr.push_back(pResource->HostPort());
124
      }
125
    }
126
    LEAVECRITICAL;
127
    rc=true;
128
  }
129
  return rc;
130
}
131
 
132
void CTestResource::DeleteAllInstances()
133
{
134
  ENTERCRITICAL;
135
  while(pFirstInstance){
136
    delete pFirstInstance;
137
  }
138
  LEAVECRITICAL;
139
}
140
 
141
bool CTestResource::LoadFromDirectory (LPCTSTR psz)
142
{
143
  bool rc=true;
144
  ENTERCRITICAL;
145
  DeleteAllInstances();
146
  // Find all the files in directory "psz" and load from each of them
147
  TCHAR szOrigDir[256];
148
  _tgetcwd(szOrigDir,sizeof szOrigDir-1);
149
  if(0==_tchdir(psz)){
150
    String strFile;
151
    void *pHandle;
152
    for(bool b=CeCosTestUtils::StartSearch(pHandle,strFile);b;b=CeCosTestUtils::NextFile(pHandle,strFile)){
153
      if(CeCosTestUtils::IsFile(strFile)){
154
        CTestResource *pResource=new CTestResource(_T(""),_T(""));
155
        CTestResourceProperties prop(pResource);
156
        prop.LoadFromFile(strFile);
157
      }
158
    }
159
    CeCosTestUtils::EndSearch(pHandle);
160
  } else {
161
    TRACE(_T("Failed to change to %s from %s\n"),psz,szOrigDir);
162
  }
163
  _tchdir(szOrigDir);
164
  LEAVECRITICAL;
165
 
166
  return rc;
167
}
168
 
169
bool CTestResource::SaveToDirectory (LPCTSTR pszDir)
170
{
171
  bool rc=false;
172
  ENTERCRITICAL;
173
  {
174
    // Delete all the files under directory "pszDir"
175
    void *pHandle;
176
    TCHAR szOrigDir[256];
177
    _tgetcwd(szOrigDir,sizeof szOrigDir-1);
178
    if(0==_tchdir(pszDir)){
179
      String strFile;
180
      for(bool b=CeCosTestUtils::StartSearch(pHandle,strFile);b;b=CeCosTestUtils::NextFile(pHandle,strFile)){
181
        if(CeCosTestUtils::IsFile(strFile)){
182
          _tunlink(strFile);
183
        }
184
      }
185
      CeCosTestUtils::EndSearch(pHandle);
186
      rc=true;
187
      for(CTestResource *pResource=pFirstInstance;pResource;pResource=pResource->m_pNextInstance){
188
        CTestResourceProperties prop(pResource);
189
        rc&=prop.SaveToFile(pResource->FileName());
190
      }
191
    } else {
192
      fprintf(stderr,"Failed to change to %s from %s\n",pszDir,szOrigDir);
193
    }
194
    _tchdir(szOrigDir);
195
  }
196
 
197
  LEAVECRITICAL;
198
 
199
  return rc;
200
}
201
 
202
#ifdef _WIN32
203
bool CTestResource::LoadFromRegistry(HKEY key,LPCTSTR psz)
204
{
205
  // Find all the keys under "psz" and load from each of them
206
  bool rc=false;
207
  ENTERCRITICAL;
208
  HKEY hKey;
209
  if(ERROR_SUCCESS==RegOpenKeyEx ((HKEY)key, psz, 0L, KEY_ENUMERATE_SUB_KEYS, &hKey)){
210
                TCHAR szName[256];
211
    DWORD dwSizeName=sizeof szName;
212
    FILETIME ftLastWriteTime;
213
    for(DWORD dwIndex=0;ERROR_SUCCESS==RegEnumKeyEx(hKey, dwIndex, szName, &dwSizeName, NULL, NULL, NULL, &ftLastWriteTime); dwIndex++){
214
      CTestResource *pResource=new CTestResource(_T(""),_T(""));
215
      String strKey;
216
      strKey.Format(_T("%s\\%s"),psz,szName);
217
      CTestResourceProperties prop1(pResource);
218
      prop1.LoadFromRegistry(key,strKey);
219
      dwSizeName=sizeof szName;
220
    }
221
    RegCloseKey(hKey);
222
  }
223
  LEAVECRITICAL;
224
  return rc;
225
}
226
 
227
bool CTestResource::SaveToRegistry(HKEY key,LPCTSTR psz)
228
{
229
  bool rc=false;
230
  ENTERCRITICAL;
231
  // Delete all the keys under "psz"
232
  HKEY hKey;
233
  if(ERROR_SUCCESS==RegOpenKeyEx ((HKEY)key, psz, 0L, KEY_ENUMERATE_SUB_KEYS, &hKey)){
234
    TCHAR szName[256];
235
    DWORD dwSizeName=sizeof szName;
236
    FILETIME ftLastWriteTime;
237
    DWORD dwIndex;
238
    if(ERROR_SUCCESS==RegQueryInfoKey(hKey,0,0,0,&dwIndex,0,0,0,0,0,0,0)){
239
      while((signed)--dwIndex>=0){
240
        if(ERROR_SUCCESS!=RegEnumKeyEx(hKey, dwIndex, szName, &dwSizeName, NULL, NULL, NULL, &ftLastWriteTime) ||
241
          ERROR_SUCCESS!=RegDeleteKey(hKey,szName)){
242
          rc=false;
243
        }
244
        dwSizeName=sizeof szName;
245
      }
246
    }
247
    RegCloseKey(hKey);
248
  }
249
  rc=true;
250
  for(CTestResource *pResource=pFirstInstance;pResource;pResource=pResource->m_pNextInstance){
251
    CTestResourceProperties prop1(pResource);
252
    rc&=prop1.SaveToRegistry(key,pResource->FileName());
253
  }
254
 
255
  LEAVECRITICAL;
256
  return rc;
257
}
258
 
259
#endif
260
 
261
CTestResource::CTestResourceProperties::CTestResourceProperties(CTestResource *pResource)
262
{
263
  Add(_T("Baud"),       pResource->m_nBaud);
264
  Add(_T("BoardId"),    pResource->m_strBoardID);
265
  Add(_T("Date"),       pResource->m_strDate);
266
  Add(_T("Email"),      pResource->m_strEmail);
267
  Add(_T("Host"),       pResource->m_strHost);
268
  Add(_T("Port"),       pResource->m_nPort);
269
  Add(_T("Locked"),     pResource->m_bLocked);
270
  Add(_T("Originator"), pResource->m_strUser);
271
  Add(_T("Reason"),     pResource->m_strReason);
272
  Add(_T("Reset"),      pResource->m_strReset);
273
  Add(_T("Serial"),     pResource->m_strPort);
274
  Add(_T("Target"),     pResource->m_Target);
275
  Add(_T("User"),       pResource->m_strUser);
276
}
277
 
278
bool CTestResource::Lock()
279
{
280
  if(!m_bLocked){
281
    m_bLocked=true;
282
    return true;
283
  } else {
284
    return false;
285
  }
286
}
287
 
288
bool CTestResource::Unlock()
289
{
290
  if(m_bLocked){
291
    m_bLocked=false;
292
    return true;
293
  } else {
294
    return false;
295
  }
296
}
297
 
298
bool CTestResource::LoadSocket(LPCTSTR pszResourceHostPort,Duration dTimeout/*=10*1000*/)
299
{
300
  bool rc=false;
301
  ENTERCRITICAL;
302
  CTestResource *pResource;
303
  // If the resource is in use, we don't dare delete it!
304
  for(pResource=CTestResource::First();pResource;pResource=pResource->Next()){
305
    pResource->m_bFlag=false;
306
  }
307
  CeCosSocket sock;
308
  if(sock.Connect(pszResourceHostPort,dTimeout)){
309
    // Write the message to the socket
310
    int nRequest=0; // read
311
    if(!sock.sendInteger(nRequest)){
312
      ERROR(_T("Failed to write to socket\n"));
313
    } else {
314
      int nResources;
315
      if(sock.recvInteger(nResources,_T(""),dTimeout)){
316
        rc=true;
317
        while(nResources--){
318
          String strImage;
319
          if(sock.recvString(strImage,_T(""),dTimeout)){
320
            VTRACE(_T("Recv \"%s\"\n"),(LPCTSTR)strImage);
321
            CTestResource tmp;
322
            tmp.FromStr(strImage);
323
            CTestResource *pResource=Lookup(tmp.HostPort());
324
            if(0==pResource){
325
              pResource=new CTestResource(_T(""),_T(""));
326
            }
327
            pResource->FromStr(strImage);
328
            pResource->m_bFlag=true;
329
          } else {
330
            rc=false;
331
            break;
332
          }
333
        }
334
      }
335
    }
336
  }
337
  // If the resource is in use, we don't dare delete it!
338
  CTestResource *pNext;
339
  for(pResource=CTestResource::First();pResource;pResource=pNext){
340
    pNext=pResource->Next();
341
    if(!pResource->m_bFlag && !pResource->m_bInUse){
342
      delete pResource;
343
    }
344
  }
345
 
346
  LEAVECRITICAL;
347
  return rc;
348
}
349
 
350
bool CTestResource::SaveSocket(LPCTSTR pszResourceHostPort,Duration dTimeout)
351
{
352
  bool rc=true;
353
  ENTERCRITICAL;
354
  CeCosSocket sock(pszResourceHostPort, dTimeout);
355
  if(sock.Ok()){
356
    // Write the message to the socket
357
    int nRequest=1; //write
358
    if(!sock.sendInteger(nRequest, _T(""),dTimeout)){
359
      ERROR(_T("Failed to write to socket\n"));
360
      rc=false;
361
    } else {
362
      int nResources=0;
363
      CTestResource *pResource;
364
      for(pResource=CTestResource::First();pResource;pResource=pResource->Next()){
365
        nResources++;
366
      }
367
      if(sock.sendInteger(nResources,_T("resource count"),dTimeout)){
368
        for(pResource=CTestResource::First();pResource;pResource=pResource->Next()){
369
          String strImage;
370
          CTestResourceProperties prop(pResource);
371
          strImage=prop.MakeCommandString();
372
          TRACE(_T("Send \"%s\"\n"),(LPCTSTR)strImage);
373
          if(!sock.sendString (strImage, _T("reply"),dTimeout)){
374
            rc=false;
375
            break;
376
          }
377
        }
378
      } else {
379
        rc=false;
380
      }
381
    }
382
  } else {
383
    rc=false;
384
  }
385
  LEAVECRITICAL;
386
  return rc;
387
}
388
 
389
/*
390
void CTestResource::Image(String &str)
391
{
392
  CTestResourceProperties prop(this);
393
  str=prop.MakeCommandString();
394
  VTRACE(_T("Make command string %s\n"),(LPCTSTR)str);
395
}
396
*/
397
 
398
bool CTestResource::FromStr(LPCTSTR pszImage)
399
{
400
  CTestResourceProperties prop(this);
401
  prop.LoadFromCommandString(pszImage);
402
  VTRACE(_T("From command string %s\n"),pszImage);
403
  return true;
404
}
405
 
406
void CTestResource::Chain()
407
{
408
  ENTERCRITICAL;
409
  nCount++;
410
  m_pNextInstance=pFirstInstance;
411
  if(m_pNextInstance){
412
    m_pNextInstance->m_pPrevInstance=this;
413
  }
414
  m_pPrevInstance=0;
415
  pFirstInstance=this;
416
  LEAVECRITICAL;
417
}
418
 
419
bool CTestResource::Matches  (const CeCosTest::ExecutionParameters &e,bool bIgnoreLocking) const
420
{
421
  return (bIgnoreLocking||(!m_bLocked)) && (0==_tcsicmp(e.PlatformName(),m_Target));
422
};
423
 
424
CeCosTest::ServerStatus CTestResource::Query()
425
{
426
  CeCosTest::ExecutionParameters e(CeCosTest::ExecutionParameters::QUERY,m_Target);
427
  CeCosSocket *pSock=0;
428
  CeCosTest::ServerStatus s=CeCosTest::Connect(HostPort(),pSock,e,m_strInfo);
429
  delete pSock;
430
  return s;
431
}
432
 
433
int CTestResource::Count(const CeCosTest::ExecutionParameters &e)
434
{
435
  int nCount=0;
436
  for(const CTestResource *p=pFirstInstance;p;p=p->m_pNextInstance){
437
    if(p->Matches(e)){
438
      nCount++;
439
    }
440
  }
441
  return nCount;
442
}
443
 
444
bool CTestResource::Use()
445
{
446
  bool rc;
447
  ENTERCRITICAL;
448
  if(m_bInUse){
449
    rc=false;
450
  } else {
451
    m_bInUse=true;
452
    rc=true;
453
  }
454
  LEAVECRITICAL;
455
  return rc;
456
}
457
 
458
CTestResource *CTestResource::GetResource (const CeCosTest::ExecutionParameters &e)
459
{
460
  CTestResource *p=0;
461
  if(0==Count(e)){
462
    ERROR(_T("GetResource: no candidates available\n"));
463
  } else {
464
    ENTERCRITICAL;
465
    for(p=pFirstInstance;p;p=p->m_pNextInstance){
466
      if(p->Matches(e) && !p->m_bInUse){
467
        TRACE(_T("Acquired %s\n"),p->Serial());
468
                                    p->Use();
469
            break;
470
      }
471
    }
472
    LEAVECRITICAL;
473
  }
474
  return p;
475
}
476
 
477
const String CTestResource::Image() const
478
{
479
  String str;
480
  str.Format(_T("%10s %20s %8s"),(LPCTSTR)HostPort(),(LPCTSTR)Target(),(LPCTSTR)Serial());
481
  if(IsLocked()){
482
    str+=_T(" [RL]");
483
  }
484
  return str;
485
}
486
 
487
bool CTestResource::Matches(LPCTSTR pszHostPort, const CeCosTest::ExecutionParameters &e)
488
{
489
  bool rc=false;
490
  ENTERCRITICAL;
491
  if(Load()){
492
    CTestResource *pResource=Lookup(pszHostPort);
493
    if(pResource){
494
      rc=pResource->Matches(e);
495
    }
496
  }
497
  LEAVECRITICAL;
498
  return rc;
499
}
500
 
501
CResetAttributes::ResetResult CTestResource::Reset(LogFunc *pfnLog, void *pfnLogparam)
502
{
503
  String strReset;
504
  strReset.Format(_T("port(%s,%d) %s"),Serial(),Baud(),ResetString());
505
  return CResetAttributes(strReset).Reset(pfnLog,pfnLogparam);
506
}
507
 
508
CResetAttributes::ResetResult CTestResource::Reset(String &str)
509
{
510
  return Reset(StringLogFunc,&str);
511
}
512
 
513
void CALLBACK CTestResource::StringLogFunc (void *pParam,LPCTSTR psz)
514
{
515
  *((String *)pParam)+=psz;
516
}
517
 
518
 
519
CResetAttributes::ResetResult CTestResource::RemoteReset(LogFunc *pfnLog, void *pfnLogparam)
520
{
521
  String strHost;
522
  int nPort;
523
  CeCosSocket::ParseHostPort(HostPort(),strHost,nPort);
524
  String strCmd;
525
  strCmd.Format(_T("rsh %s x10reset %s\n"),(LPCTSTR)strHost,ResetString());
526
  pfnLog(pfnLogparam,strCmd);
527
 
528
  CSubprocess sp;
529
  sp.Run(pfnLog,pfnLogparam,strCmd);
530
  return CResetAttributes::RESET_OK; // FIXME
531
}
532
 
533
String CTestResource::FileName() const
534
{
535
  String strHost;
536
  int nPort;
537
  CeCosSocket::ParseHostPort(HostPort(),strHost,nPort);
538
  return String::SFormat(_T("%s-%d"),(LPCTSTR)strHost,nPort);
539
}
540
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.