1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
/* -*- c++ -*-
*
* (C) Copyright Pigeon Point Systems. 2011
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTWATCHDOGLITY or FITNESS FOR A PARTICULAR PURPOSE. This
* file and program are licensed under a BSD style license. See
* the Copying file included with the OpenHPI distribution for
* full licensing terms.
*
* Author(s):
* Anton Pak <anton.pak@pigeonpoint.com>
*/
#include <algorithm>
#include <string>
#include "codec.h"
#include "dimi.h"
#include "test.h"
namespace TA {
/**************************************************************
* Helper functions
*************************************************************/
static SaHpiRdrTypeUnionT MakeDefaultDimiRec( SaHpiDimiNumT num )
{
SaHpiRdrTypeUnionT data;
SaHpiDimiRecT& rec = data.DimiRec;
rec.DimiNum = num;
rec.Oem = 0;
return data;
}
/**************************************************************
* Functors for working with tests
*************************************************************/
struct TestDeleter
{
void operator ()( cTest * test )
{
delete test;
}
};
struct ObjectCollector
{
explicit ObjectCollector( cObject::Children& _children )
: children( _children )
{
// empty
}
void operator ()( cTest * test )
{
if ( test ) {
children.push_back( test );
}
}
cObject::Children& children;
};
/**************************************************************
* class cDimi
*************************************************************/
const std::string cDimi::classname( "dimi" );
cDimi::cDimi( cHandler& handler, cResource& resource, SaHpiDimiNumT num )
: cInstrument( handler,
resource,
AssembleNumberedObjectName( classname, num ),
SAHPI_DIMI_RDR,
MakeDefaultDimiRec( num ) ),
m_rec( GetRdr().RdrTypeUnion.DimiRec ),
m_update_count( 0 )
{
// empty
}
cDimi::~cDimi()
{
TestDeleter deleter;
std::for_each( m_tests.begin(), m_tests.end(), deleter );
m_tests.clear();
}
cTest * cDimi::GetTest( SaHpiDimiTestNumT tnum ) const
{
return ( tnum < m_tests.size() ) ? m_tests[tnum] : 0;
}
void cDimi::PostEvent( SaHpiDimiTestNumT tnum,
SaHpiDimiTestRunStatusT status,
SaHpiDimiTestPercentCompletedT progress )
{
SaHpiEventUnionT data;
SaHpiDimiEventT& de = data.DimiEvent;
de.DimiNum = m_rec.DimiNum;
de.TestNum = tnum;
de.DimiTestRunStatus = status;
de.DimiTestPercentCompleted = progress;
cInstrument::PostEvent( SAHPI_ET_DIMI, data, SAHPI_INFORMATIONAL );
}
// HPI interface
SaErrorT cDimi::GetInfo( SaHpiDimiInfoT& info ) const
{
info.NumberOfTests = m_tests.size();
info.TestNumUpdateCounter = m_update_count;
return SA_OK;
}
// cObject virtual functions
void cDimi::GetNB( std::string& nb ) const
{
cObject::GetNB( nb );
nb += "- Test Agent supports creation of a DIMI test with\n";
nb += " id == current number of tests.\n";
nb += "- Test Agent supports removal of a DIMI test with\n";
nb += " id == (current number of tests - 1).\n";
nb += "- Be careful when removing a test:\n";
nb += "-- Any DIMI API directed to the removed test will fail.\n";
nb += "-- Any DIMI asynchronous operation on the test can fail or cause crash.\n";
}
void cDimi::GetNewNames( cObject::NewNames& names ) const
{
cObject::GetNewNames( names );
names.push_back( cTest::classname + "-XXX" );
}
bool cDimi::CreateChild( const std::string& name )
{
bool rc;
rc = cObject::CreateChild( name );
if ( rc ) {
return true;
}
std::string cname;
SaHpiUint32T id;
rc = DisassembleNumberedObjectName( name, cname, id );
if ( !rc ) {
return false;
}
if ( cname != cTest::classname ) {
return false;
}
if ( id != m_tests.size() ) {
return false;
}
m_tests.push_back( new cTest( m_handler, *this, id ) );
Update();
return true;
}
bool cDimi::RemoveChild( const std::string& name )
{
bool rc;
rc = cObject::RemoveChild( name );
if ( rc ) {
return true;
}
std::string cname;
SaHpiUint32T id;
rc = DisassembleNumberedObjectName( name, cname, id );
if ( !rc ) {
return false;
}
if ( ( id + 1 ) != m_tests.size() ) {
return false;
}
delete m_tests[id];
m_tests[id] = 0;
m_tests.resize( id );
Update();
return true;
}
void cDimi::GetChildren( Children& children ) const
{
cObject::GetChildren( children );
ObjectCollector collector( children );
std::for_each( m_tests.begin(), m_tests.end(), collector );
}
void cDimi::Update()
{
++m_update_count;
SaHpiEventUnionT data;
SaHpiDimiUpdateEventT& due = data.DimiUpdateEvent;
due.DimiNum = m_rec.DimiNum;
cInstrument::PostEvent( SAHPI_ET_DIMI_UPDATE, data, SAHPI_INFORMATIONAL );
}
}; // namespace TA
|