File: new_sim_sensor.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 (582 lines) | stat: -rw-r--r-- 15,830 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/** 
 * @file    new_sim_sensor.cpp
 *
 * The file includes an abstract class for sensor handling:\n
 * NewSimulatorSensor
 * 
 * @author  Lars Wetzel <larswetzel@users.sourceforge.net>
 * @version 0.3
 * @date    2010
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY 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.
 * 
 * The new Simulator plugin is adapted from the ipmidirect plugin.
 * Thanks to \n
 *     Thomas Kanngieser <thomas.kanngieser@fci.com>\n
 *     Pierre Sangouard  <psangouard@eso-tech.com>
 *      
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <glib.h>
#include <math.h>

#include <oh_error.h>

#include "new_sim_domain.h"
#include "new_sim_sensor.h"
#include "new_sim_entity.h"
#include "new_sim_utils.h"
#include "new_sim_text_buffer.h"



/**
 * Constructor
 **/
NewSimulatorSensor::NewSimulatorSensor( NewSimulatorResource *res )
  : NewSimulatorRdr( res, SAHPI_SENSOR_RDR ), 
    m_enabled( SAHPI_TRUE ),
    m_events_enabled( SAHPI_TRUE ),
    m_read_support( SAHPI_TRUE ),
    m_assert_mask( 0 ),
    m_deassert_mask( 0 ){
    	
   memset( &m_sensor_record, 0, sizeof( SaHpiSensorRecT ));
   memset( &m_read_data, 0, sizeof( SaHpiSensorReadingT ));
   memset( &m_event_data, 0, sizeof( SaHpiEventStateT ));
}


/**
 * Full qualified constructor to fill an object with the parsed data
 **/
NewSimulatorSensor::NewSimulatorSensor( NewSimulatorResource *res,
                                        SaHpiRdrT rdr, 
                                        SaHpiSensorReadingT data, 
                                        SaHpiEventStateT event_state, 
                                        SaHpiEventStateT event_amask, 
                                        SaHpiEventStateT event_dmask,
                                        SaHpiBoolT enabled, 
                                        SaHpiBoolT event_enabled)
  : NewSimulatorRdr( res, SAHPI_SENSOR_RDR, rdr.Entity, rdr.IsFru, rdr.IdString ),
    m_enabled( enabled ),
    m_events_enabled( event_enabled ),
    m_read_support( SAHPI_TRUE ),
    m_assert_mask( event_amask ),
    m_deassert_mask( event_dmask ),
    m_event_data( event_state ) {

   memcpy(&m_sensor_record, &rdr.RdrTypeUnion.SensorRec, sizeof( SaHpiSensorRecT ));
   memcpy(&m_read_data, &data, sizeof( SaHpiSensorReadingT ));
    
}
  
                      
/**
 * Destructor
 **/
NewSimulatorSensor::~NewSimulatorSensor()
{
}


/**
 * TBD: Check where and whether it is needed
 **/
void NewSimulatorSensor::HandleNew( NewSimulatorDomain *domain )
{
//  m_sensor_type_string        = IpmiSensorTypeToString( m_sensor_type );
//  m_event_reading_type_string = IpmiEventReadingTypeToString( m_event_reading_type );
}


/**
 * TBD: Check where and whether it is needed
 * TODO: Change it properly due to new data structure
 **/
bool NewSimulatorSensor::Cmp( const NewSimulatorSensor &s2 ) const {
/**	
  if ( m_entity_path != s2.m_entity_path )
       return false;

  if ( m_sensor_init_scanning    != s2.m_sensor_init_scanning )
       return false;

  if ( m_sensor_init_events      != s2.m_sensor_init_events )
       return false;

  if ( m_sensor_init_type        != s2.m_sensor_init_type )
       return false;

  if ( m_sensor_init_pu_events   != s2.m_sensor_init_pu_events )
       return false;

  if ( m_sensor_init_pu_scanning != s2.m_sensor_init_pu_scanning )
       return false;

  if ( m_ignore_if_no_entity     != s2.m_ignore_if_no_entity )
       return false;

  if ( m_supports_auto_rearm     != s2.m_supports_auto_rearm )
       return false;

  if ( m_event_support           != s2.m_event_support )
       return false;

  if ( m_sensor_type             != s2.m_sensor_type )
       return false;

  if ( m_event_reading_type      != s2.m_event_reading_type )
       return false;

  if ( m_oem != s2.m_oem )
       return false;

  if ( IdString() != s2.IdString() )
       return false;
**/
  return true;
}


/** 
 * Check if val1 > val2
 * 
 * @param val1 SensorReading
 * @param val2 SensorReading 
 * 
 * @return false also in error case
 **/
bool NewSimulatorSensor::gt(const SaHpiSensorReadingT &val1, 
                             const SaHpiSensorReadingT &val2) {

   if (val1.Type != val2.Type) {
   	  err("Different sensor reading types in comparision.");
      return false;
   }
   
   switch(val1.Type) {

   case SAHPI_SENSOR_READING_TYPE_INT64:
      return (val1.Value.SensorInt64 > val2.Value.SensorInt64) ? true : false;
      break;

   case SAHPI_SENSOR_READING_TYPE_UINT64:
      return (val1.Value.SensorUint64 > val2.Value.SensorUint64) ? true : false;
      break;

   case SAHPI_SENSOR_READING_TYPE_FLOAT64:
      return (val1.Value.SensorFloat64 > val2.Value.SensorFloat64) ? true : false;
      break;
   
   case SAHPI_SENSOR_READING_TYPE_BUFFER:
      return (memcmp(&val1.Value.SensorBuffer, 
                      &val2.Value.SensorBuffer,
                      SAHPI_SENSOR_BUFFER_LENGTH) > 0) ? true : false;

   default:
      err("Invalid sensor reading type.");
      return false;
   }
}


/** 
 * Check if val1 == val2
 * 
 * @param val1 SensorReading
 * @param val2 SensorReading 
 * 
 * @return false also in error case
 **/
bool NewSimulatorSensor::eq(const SaHpiSensorReadingT &val1, 
                             const SaHpiSensorReadingT &val2) {

   if (val1.Type != val2.Type) {
   	  err("Different sensor reading types in comparision.");
      return false;
   }
   
   switch(val1.Type) {

   case SAHPI_SENSOR_READING_TYPE_INT64:
      return (val1.Value.SensorInt64 == val2.Value.SensorInt64) ? true : false;
      break;

   case SAHPI_SENSOR_READING_TYPE_UINT64:
      return (val1.Value.SensorUint64 == val2.Value.SensorUint64) ? true : false;
      break;

   case SAHPI_SENSOR_READING_TYPE_FLOAT64:
      return (val1.Value.SensorFloat64 == val2.Value.SensorFloat64) ? true : false;
      break;
   
   case SAHPI_SENSOR_READING_TYPE_BUFFER:
      return (memcmp(&val1.Value.SensorBuffer, 
                      &val2.Value.SensorBuffer,
                      SAHPI_SENSOR_BUFFER_LENGTH) == 0) ? true : false;

   default:
      err("Invalid sensor reading type.");
      return false;
   }
}


/** 
 * Check if val1 >= val2
 * 
 * @param val1 SensorReading
 * @param val2 SensorReading 
 * 
 * @return false also in error case
 **/
bool NewSimulatorSensor::ge(const SaHpiSensorReadingT &val1, 
                             const SaHpiSensorReadingT &val2) {

   if (val1.Type != val2.Type) {
   	  err("Different sensor reading types in comparision.");
      return false;
   }
   
   return (  gt( val1, val2 ) 
           || eq( val1, val2 ) );
}


/** 
 * Check if val1 < val2
 * 
 * @param val1 SensorReading
 * @param val2 SensorReading 
 * 
 * @return false also in error case
 **/
bool NewSimulatorSensor::lt(const SaHpiSensorReadingT &val1, 
                             const SaHpiSensorReadingT &val2) {

   if (val1.Type != val2.Type) {
   	  err("Different sensor reading types in comparision.");
      return false;
   }
   
   return ( ! ge( val1, val2 ) ) ;
}


/** 
 * Check if val1 <= val2
 * 
 * @param val1 SensorReading
 * @param val2 SensorReading 
 * 
 * @return false also in error case
 **/
bool NewSimulatorSensor::le(const SaHpiSensorReadingT &val1, 
                             const SaHpiSensorReadingT &val2) {

   if (val1.Type != val2.Type) {
   	  err("Different sensor reading types in comparision.");
      return false;
   }
   
   return ( ! gt( val1, val2 ) );
}


/** 
 * Check if val1 < 0
 * 
 * @param val1 SensorReading
 * 
 * @return return false also in error case
 **/
bool NewSimulatorSensor::ltZero(const SaHpiSensorReadingT &val1) {
   
   
   switch(val1.Type) {

   case SAHPI_SENSOR_READING_TYPE_INT64:
      return (val1.Value.SensorInt64 < 0) ? true : false;
      break;

   case SAHPI_SENSOR_READING_TYPE_UINT64:
      return false;
      break;

   case SAHPI_SENSOR_READING_TYPE_FLOAT64:
      return (val1.Value.SensorFloat64 < 0) ? true : false;
      break;
      
   case SAHPI_SENSOR_READING_TYPE_BUFFER:
      SaHpiUint8T zeroBuffer[SAHPI_SENSOR_BUFFER_LENGTH];
      memset(&zeroBuffer, 0, (sizeof(SaHpiUint8T)*SAHPI_SENSOR_BUFFER_LENGTH));
      return (memcmp(&val1.Value.SensorBuffer, 
                      &zeroBuffer,
                      SAHPI_SENSOR_BUFFER_LENGTH) < 0) ? true : false;
      break;
      
   default:
      err("Invalid sensor reading type.");
      return false;
   }
}


/** 
 * Dump the sensor information
 * 
 * @param dump Address of the log
 * 
 **/
void NewSimulatorSensor::Dump( NewSimulatorLog &dump ) const {
  char str[256];
  IdString().GetAscii( str, 256 );

  dump << "Sensor: " << m_sensor_record.Num << " " << str << "\n";
}


/**
 * A rdr structure is filled with the data
 * 
 * This method is called by method NewSimulatorRdr::Populate().
 * 
 * @param resource Address of resource structure
 * @param rdr Address of rdr structure
 * 
 * @return true
 **/
bool NewSimulatorSensor::CreateRdr( SaHpiRptEntryT &resource, SaHpiRdrT &rdr ) {
  
  if ( NewSimulatorRdr::CreateRdr( resource, rdr ) == false )
       return false;

  // sensor record
  memcpy(&rdr.RdrTypeUnion.SensorRec, &m_sensor_record, sizeof(SaHpiSensorRecT));

  return true;
}


/**
 * HPI function saHpiSensorEnableGet()
 * 
 * See also the description of the function inside the specification or header file.
 * Copying the internal reading values (if a read is allowed).
 * 
 * @param enable address of enable return value
 * 
 * @return HPI return code
 **/
SaErrorT NewSimulatorSensor::GetEnable( SaHpiBoolT &enable ) {
    enable = m_enabled;

    return SA_OK;
}


/**
 * HPI function  saHpiSensorEnableSet()
 * 
 * See also the description of the function inside the specification or header file.
 * Copying the internal reading values (if a read is allowed).
 * 
 * @param enable address of enable value to be set internally
 * 
 * @return HPI return code
 **/
SaErrorT NewSimulatorSensor::SetEnable( const SaHpiBoolT &enable ) {
    if (m_enabled == enable)
        return SA_OK;

    m_enabled = enable;

    CreateEnableChangeEvent();

    return SA_OK;
}

/**
 * HPI function saHpiSensorEventEnableGet()
 * 
 * See also the description of the function inside the specification or header file.
 * Copying the internal reading values (if a read is allowed).
 * 
 * @param enables address of event enable return value 
 * 
 * @return HPI return code
 **/
SaErrorT NewSimulatorSensor::GetEventEnables( SaHpiBoolT &enables ) {

    enables = m_events_enabled;

    return SA_OK;
}


/**
 * HPI function saHpiSensorEventEnableSet()
 * 
 * See also the description of the function inside the specification or header file.
 * Copying the internal reading values (if a read is allowed).
 *
 * @param enables address of event enable value to be set internally 
 * 
 * @return HPI return code
 **/
SaErrorT NewSimulatorSensor::SetEventEnables( const SaHpiBoolT &enables ) {

    if ( EventCtrl() == SAHPI_SEC_READ_ONLY )
        return SA_ERR_HPI_READ_ONLY;

    if ( m_events_enabled == enables )
        return SA_OK;

    m_events_enabled = enables;

    CreateEnableChangeEvent();

    return SA_OK;
}


/**
 * HPI function saHpiSensorEventMasksGet()
 * 
 * See also the description of the function inside the specification or header file.
 * Copying the internal reading values (if a read is allowed).
 * 
 * @param AssertEventMask address of mask to be filled with assertion mask 
 * @param  DeassertEventMask address of mask to be filled with deassertion mask
 * 
 * @return HPI return code
 **/
SaErrorT NewSimulatorSensor::GetEventMasks( SaHpiEventStateT &AssertEventMask,
                            SaHpiEventStateT &DeassertEventMask) {

    if (&AssertEventMask) AssertEventMask = m_assert_mask;
    if (&DeassertEventMask) DeassertEventMask = m_deassert_mask;

    return SA_OK;
}

/**
 * HPI function saHpiSensorEventMasksSet()
 * 
 * See also the description of the function inside the specification or header file.
 * Copying the internal reading values (if a read is allowed).
 * 
 * @param act address of variable which includes the action to be done
 * @param AssertEventMask address of variable which include new assertion mask data
 * @param DeassertEventMask address of variable which include new deassertion mask data
 * 
 * @return HPI return code
 **/
SaErrorT NewSimulatorSensor::SetEventMasks( const SaHpiSensorEventMaskActionT &act,
                                            SaHpiEventStateT                   &AssertEventMask,
                                            SaHpiEventStateT                   &DeassertEventMask ) {
	
   if ( EventCtrl() != SAHPI_SEC_PER_EVENT )
      return SA_ERR_HPI_READ_ONLY;

   if ( AssertEventMask == SAHPI_ALL_EVENT_STATES )
      AssertEventMask = EventStates();

   if ( DeassertEventMask == SAHPI_ALL_EVENT_STATES )
      DeassertEventMask = EventStates();

   if ( act == SAHPI_SENS_ADD_EVENTS_TO_MASKS ) {
      if ((( AssertEventMask & ~EventStates() ) != 0 )
       || (( DeassertEventMask & ~EventStates() ) != 0 ))
         return SA_ERR_HPI_INVALID_DATA;
   }

   SaHpiEventStateT save_assert_mask = m_assert_mask;
   SaHpiEventStateT save_deassert_mask = m_deassert_mask;
   
   if ( act == SAHPI_SENS_ADD_EVENTS_TO_MASKS ) {
      m_assert_mask   |= AssertEventMask;
      m_deassert_mask |= DeassertEventMask;
    
   } else if ( act == SAHPI_SENS_REMOVE_EVENTS_FROM_MASKS ) {
      m_assert_mask   &= (AssertEventMask ^ SAHPI_ALL_EVENT_STATES);
      m_deassert_mask &= (DeassertEventMask ^ SAHPI_ALL_EVENT_STATES);
    
   } else 
      return SA_ERR_HPI_INVALID_PARAMS;
    
   stdlog << "SetEventMasks sensor " << m_sensor_record.Num << " assert " << m_assert_mask << " deassert " << m_deassert_mask << "\n";

   if (( save_assert_mask == m_assert_mask )
    && ( save_deassert_mask == m_deassert_mask ))
      return SA_OK;

   CreateEnableChangeEvent();

   return SA_OK;
}


/**
 * Generate an event if the enabling / mask of the sensor was changed 
 **/
void NewSimulatorSensor::CreateEnableChangeEvent()
{
  NewSimulatorResource *res = Resource();
  if( !res )
     {
       stdlog << "CreateEnableChangeEvent: No resource !\n";
       return;
     }

  oh_event *e = (oh_event *)g_malloc0( sizeof( struct oh_event ) );
  
  e->event.EventType = SAHPI_ET_SENSOR_ENABLE_CHANGE;

  SaHpiRptEntryT *rptentry = oh_get_resource_by_id( res->Domain()->GetHandler()->rptcache, res->ResourceId() );
  SaHpiRdrT *rdrentry = oh_get_rdr_by_id( res->Domain()->GetHandler()->rptcache, res->ResourceId(), m_record_id );

  if ( rptentry )
      e->resource = *rptentry;
  else
      e->resource.ResourceCapabilities = 0;

  if ( rdrentry )
      e->rdrs = g_slist_append(e->rdrs, g_memdup(rdrentry, sizeof(SaHpiRdrT)));
  else
      e->rdrs = NULL;

  // hpi events
  e->event.Source    = res->ResourceId();
  e->event.EventType = SAHPI_ET_SENSOR_ENABLE_CHANGE;
  e->event.Severity  = SAHPI_INFORMATIONAL;
  
  oh_gettimeofday(&e->event.Timestamp);
  
  // sensor enable event
  SaHpiSensorEnableChangeEventT *se = &e->event.EventDataUnion.SensorEnableChangeEvent;
  se->SensorNum     = m_sensor_record.Num;
  se->SensorType    = Type();
  se->EventCategory = EventCategory();
  se->SensorEnable  = m_enabled;
  se->SensorEventEnable = m_events_enabled;
  se->AssertEventMask   = m_assert_mask;
  se->DeassertEventMask = m_deassert_mask;
  
  stdlog << "NewSimulatorSensor::CreateEnableChangeEvent OH_ET_HPI Event enable change resource " << res-> ResourceId() << "\n";
  res->Domain()->AddHpiEvent( e );

  return;
}