File: test.cpp

package info (click to toggle)
openhpi 3.8.0-2.3
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 31,888 kB
  • sloc: ansic: 225,326; cpp: 63,687; java: 16,472; cs: 15,161; python: 11,884; sh: 11,508; makefile: 4,945; perl: 1,529; xml: 36; asm: 13
file content (329 lines) | stat: -rw-r--r-- 9,387 bytes parent folder | download | duplicates (4)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*      -*- 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
 * MERCHANTAREALITY 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 <stddef.h>
#include <stdio.h>
#include <string.h>

#include <algorithm>
#include <string>

#include <oh_utils.h>

#include "codec.h"
#include "dimi.h"
#include "handler.h"
#include "structs.h"
#include "test.h"
#include "utils.h"


namespace TA {


/**************************************************************
 * Helper functions
 *************************************************************/
static SaHpiDimiTestT MakeDefaultTestInfo( SaHpiDimiTestNumT num )
{
    SaHpiDimiTestT info;

    FormatHpiTextBuffer( info.TestName, "test %u", (unsigned int)num );
    info.ServiceImpact = SAHPI_DIMITEST_NONDEGRADING;
    for ( size_t i = 0; i < SAHPI_DIMITEST_MAX_ENTITIESIMPACTED; ++i ) {
        MakeUnspecifiedHpiEntityPath( info.EntitiesImpacted[i].EntityImpacted );
        info.EntitiesImpacted[i].ServiceImpact = SAHPI_DIMITEST_NONDEGRADING;
    }
    info.NeedServiceOS = SAHPI_FALSE;
    MakeHpiTextBuffer( info.ServiceOS, "Unspecified OS" );
    info.ExpectedRunDuration = 2000000000LL; // 2 sec
    info.TestCapabilities = SAHPI_DIMITEST_CAPABILITY_TESTCANCEL;

    for ( size_t i = 0; i < SAHPI_DIMITEST_MAX_PARAMETERS; ++i ) {
        SaHpiDimiTestParamsDefinitionT& pd = info.TestParameters[i];
        memset( &pd.ParamName[0], 0, SAHPI_DIMITEST_PARAM_NAME_LEN );
        snprintf( reinterpret_cast<char *>(&pd.ParamName[0]),
                  SAHPI_DIMITEST_PARAM_NAME_LEN,
                  "Param %u",
                  (unsigned int)i );
        FormatHpiTextBuffer( pd.ParamInfo, "This is param %u", (unsigned int)i );
        pd.ParamType = SAHPI_DIMITEST_PARAM_TYPE_INT32;
        pd.MinValue.IntValue     = 0;
        pd.MaxValue.IntValue     = 255;
        pd.DefaultParam.paramint = i;
    }

    return info;
}

static SaHpiDimiTestResultsT MakeDefaultTestResults()
{
    SaHpiDimiTestResultsT results;

    results.ResultTimeStamp       = SAHPI_TIME_UNSPECIFIED;
    results.RunDuration           = SAHPI_TIMEOUT_IMMEDIATE;
    results.LastRunStatus         = SAHPI_DIMITEST_STATUS_NOT_RUN;
    results.TestErrorCode         = SAHPI_DIMITEST_STATUSERR_NOERR;
    MakeHpiTextBuffer( results.TestResultString, "http://openhpi.org" );
    results.TestResultStringIsURI = SAHPI_TRUE;

    return results;
}


/**************************************************************
 * class cTest
 *************************************************************/
const std::string cTest::classname( "test" );

cTest::cTest( cHandler& handler, cDimi& dimi, SaHpiDimiTestNumT num )
    : cObject( AssembleNumberedObjectName( classname, num ) ),
      m_handler( handler ),
      m_dimi( dimi ),
      m_num( num ),
      m_info( MakeDefaultTestInfo( num ) ),
      m_ready( SAHPI_DIMI_READY ),
      m_status( SAHPI_DIMITEST_STATUS_NOT_RUN ),
      m_progress( 0xff ),
      m_prev_results( MakeDefaultTestResults() ),
      m_start_timestamp( SAHPI_TIME_UNSPECIFIED )
{
    m_next.run_duration         = m_info.ExpectedRunDuration;
    m_next.err                  = SAHPI_DIMITEST_STATUSERR_NOERR;
    MakeHpiTextBuffer( m_next.result_string, "No error has been detected" );
    m_next.result_string_is_uri = SAHPI_FALSE;
}

cTest::~cTest()
{
    m_handler.CancelTimer( this );
}


// HPI Interface
SaErrorT cTest::GetInfo( SaHpiDimiTestT& info ) const
{
    info = m_info;

    return SA_OK;
}

SaErrorT cTest::GetReadiness( SaHpiDimiReadyT& ready )
{
    if ( m_status == SAHPI_DIMITEST_STATUS_RUNNING ) {
        ready = SAHPI_DIMI_BUSY;
    } else {
        ready = m_ready;
    }

    return SA_OK;
}

SaErrorT cTest::Start( SaHpiUint8T nparams,
                       const SaHpiDimiTestVariableParamsT * params )
{
    SaHpiDimiReadyT ready;
    SaErrorT rv = GetReadiness( ready );
    if ( rv != SA_OK ) {
        return rv;
    }
    if ( ready != SAHPI_DIMI_READY ) {
        return SA_ERR_HPI_INVALID_STATE;
    }
    bool rc= CheckParams( nparams, params );
    if ( !rc ) {
        return SA_ERR_HPI_INVALID_DATA;
    }

    ChangeStatus( SAHPI_DIMITEST_STATUS_RUNNING );

    m_handler.SetTimer( this, m_next.run_duration );

    return SA_OK;
}

SaErrorT cTest::Cancel()
{
    if ( m_status != SAHPI_DIMITEST_STATUS_RUNNING ) {
        return SA_ERR_HPI_INVALID_STATE;
    }
    if ( ( m_info.TestCapabilities & SAHPI_DIMITEST_CAPABILITY_TESTCANCEL ) == 0 ) {
        return SA_ERR_HPI_INVALID_REQUEST;
    }

    m_handler.CancelTimer( this );

    ChangeStatus( SAHPI_DIMITEST_STATUS_CANCELED );

    return SA_OK;
}

SaErrorT cTest::GetStatus( SaHpiDimiTestPercentCompletedT& progress,
                           SaHpiDimiTestRunStatusT& status ) const
{
    progress = m_progress;
    status   = m_status;

    return SA_OK;
}

SaErrorT cTest::GetResults( SaHpiDimiTestResultsT& results ) const
{
    results = m_prev_results;

    return SA_OK;
}


// cObject virtual functions
void cTest::GetVars( cVars& vars )
{
    cObject::GetVars( vars );
    Structs::GetVars( m_info, vars );

    vars << "Readiness"
         << dtSaHpiDimiReadyT
         << DATA( m_ready )
         << VAR_END();
    vars << "Status"
         << dtSaHpiDimiTestRunStatusT
         << DATA( m_status )
         << READONLY()
         << VAR_END();
    vars << "Progress"
         << dtSaHpiDimiTestPercentCompletedT
         << DATA( m_progress )
         << READONLY()
         << VAR_END();
    vars << "Next.RunDuration"
         << dtSaHpiTimeoutT
         << DATA( m_next.run_duration )
         << VAR_END();
    vars << "Next.TestErrorCode"
         << dtSaHpiDimiTestErrCodeT
         << DATA( m_next.err )
         << VAR_END();
    vars << "Next.TestResultString"
         << dtSaHpiTextBufferT
         << DATA( m_next.result_string )
         << VAR_END();
    vars << "Next.TestResultStringIsURI"
         << dtSaHpiBoolT
         << DATA( m_next.result_string_is_uri )
         << VAR_END();
}

void cTest::AfterVarSet( const std::string& var_name )
{
    cObject::AfterVarSet( var_name );
}


bool cTest::CheckParams( SaHpiUint8T nparams,
                         const SaHpiDimiTestVariableParamsT * params ) const
{
    for ( size_t i = 0; i < nparams; ++i ) {
        const SaHpiDimiTestVariableParamsT * p = &params[i];
        const SaHpiDimiTestParamsDefinitionT * pd;
        size_t j;
        for ( j = 0; j < SAHPI_DIMITEST_MAX_PARAMETERS; ++j ) {
            pd = &m_info.TestParameters[j];
            int cc = strncmp( reinterpret_cast<const char *>( &p->ParamName[0] ),
                              reinterpret_cast<const char *>( &pd->ParamName[0] ),
                              SAHPI_DIMITEST_PARAM_NAME_LEN );
            if ( cc == 0 ) {
                break;
            }
        }
        if ( j == SAHPI_DIMITEST_MAX_PARAMETERS ) {
            continue;
        }
        if ( p->ParamType != pd->ParamType ) {
            return false;
        }
        if ( p->ParamType == SAHPI_DIMITEST_PARAM_TYPE_INT32 ) {
            if ( p->Value.paramint < pd->MinValue.IntValue ) {
                return false;
            }
            if ( p->Value.paramint > pd->MaxValue.IntValue ) {
                return false;
            }
        }
        if ( p->ParamType == SAHPI_DIMITEST_PARAM_TYPE_FLOAT64 ) {
            if ( p->Value.paramfloat < pd->MinValue.FloatValue ) {
                return false;
            }
            if ( p->Value.paramfloat > pd->MaxValue.FloatValue ) {
                return false;
            }
        }
    }

    return true;
}

void cTest::ChangeStatus( SaHpiDimiTestRunStatusT status )
{
    m_status = status;

    SaHpiTimeT now;
    oh_gettimeofday( &now );

    if ( m_status == SAHPI_DIMITEST_STATUS_RUNNING ) {
        m_start_timestamp = now;
    } else if ( m_status == SAHPI_DIMITEST_STATUS_NOT_RUN ) {
        // Do nothing
    } else {
        // Update results
        m_prev_results.ResultTimeStamp = now;
        m_prev_results.RunDuration     = now - m_start_timestamp;
        m_prev_results.LastRunStatus   = m_status;
        m_prev_results.TestErrorCode   = m_next.err;
        if ( m_status != SAHPI_DIMITEST_STATUS_CANCELED ) {
            m_prev_results.TestResultString      = m_next.result_string;
            m_prev_results.TestResultStringIsURI = m_next.result_string_is_uri;
        } else {
            MakeHpiTextBuffer( m_prev_results.TestResultString, "The test has been cancelled" );
            m_prev_results.TestResultStringIsURI = SAHPI_FALSE;
        }
    }

    // Post event
    if ( !IsVisible() ) {
        return;
    }

    m_dimi.PostEvent( m_num, m_status, m_progress );
}


// cTimerCallback virtual functions
void cTest::TimerEvent()
{
    cLocker<cHandler> al( &m_handler );

    SaHpiDimiTestRunStatusT status;
    if ( m_next.err == SAHPI_DIMITEST_STATUSERR_NOERR ) {
        status = SAHPI_DIMITEST_STATUS_FINISHED_NO_ERRORS;
    } else {
        status = SAHPI_DIMITEST_STATUS_FINISHED_ERRORS;
    }

    ChangeStatus( status );
}


}; // namespace TA