File: new_sim_inventory.cpp

package info (click to toggle)
openhpi 3.8.0-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 31,792 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 (617 lines) | stat: -rw-r--r-- 16,926 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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/** 
 * @file    new_sim_inventory.cpp
 *
 * The file includes a class for inventory handling:\n
 * NewSimulatorInventory
 * 
 * @author  Lars Wetzel <larswetzel@users.sourceforge.net>
 * @version 0.1
 * @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.
 * 
 */

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

#include "new_sim_domain.h"
#include "new_sim_inventory.h"
#include "new_sim_inventory_data.h"
#include "new_sim_entity.h"


/**
 * Constructor
 **/
NewSimulatorInventory::NewSimulatorInventory( NewSimulatorResource *res )
  : NewSimulatorRdr( res, SAHPI_INVENTORY_RDR ), 
    m_area_id( 0 ) {
    	
   memset( &m_inv_rec, 0, sizeof( SaHpiInventoryRecT ));
   memset( &m_inv_info, 0, sizeof( SaHpiIdrInfoT ));
}


/**
 * Full qualified constructor to fill an object with the parsed data
 **/
NewSimulatorInventory::NewSimulatorInventory( NewSimulatorResource *res,
                                        SaHpiRdrT rdr)
  : NewSimulatorRdr( res, SAHPI_INVENTORY_RDR, rdr.Entity, rdr.IsFru, rdr.IdString ),
    m_area_id( 0 ) {

   memcpy(&m_inv_rec, &rdr.RdrTypeUnion.InventoryRec, sizeof( SaHpiInventoryRecT ));
   memset( &m_inv_info, 0, sizeof( SaHpiIdrInfoT ));
}

/**
 * Full qualified constructor to fill an object with the parsed data including idr_info
 **/
NewSimulatorInventory::NewSimulatorInventory( NewSimulatorResource *res,
                                        SaHpiRdrT rdr, 
                                        SaHpiIdrInfoT inv_info)
  : NewSimulatorRdr( res, SAHPI_INVENTORY_RDR, rdr.Entity, rdr.IsFru, rdr.IdString ),
    m_area_id( 0 ) {

   memcpy(&m_inv_rec, &rdr.RdrTypeUnion.InventoryRec, sizeof( SaHpiInventoryRecT ));
   memcpy(&m_inv_info, &inv_info, sizeof( SaHpiIdrInfoT ));
}
  
                      
/**
 * Destructor
 **/
NewSimulatorInventory::~NewSimulatorInventory() {
   m_areas.RemAll();
}


/** 
 * Dump the Inventory information
 * 
 * @param dump Address of the log
 * 
 **/
void NewSimulatorInventory::Dump( NewSimulatorLog &dump ) const {

  dump << "Inventory: " << m_inv_rec.IdrId << "\n";
  dump << "Persistent: " << m_inv_rec.Persistent << "\n";
  dump << "Oem: " << m_inv_rec.Oem << "\n";
  dump << "Area(s): " << "\n";
  dump << "-------------------\n";
  for (int i= 0; i < m_areas.Num(); i++) {
      m_areas[i]->Dump( dump );
  }
}


/**
 * 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 NewSimulatorInventory::CreateRdr( SaHpiRptEntryT &resource, SaHpiRdrT &rdr ) {
  
  if ( NewSimulatorRdr::CreateRdr( resource, rdr ) == false )
       return false;

  // Inventory record
  memcpy(&rdr.RdrTypeUnion.InventoryRec, &m_inv_rec, sizeof(SaHpiInventoryRecT));

  return true;
}


/**
 * Find area by area pointer
 * 
 * @param area pointer on the inventory area to be found 
 * @return return the same pointer if it could be found successfully 
 **/
NewSimulatorInventoryArea *NewSimulatorInventory::FindInventoryArea( 
                                           NewSimulatorInventoryArea *area ) {
	
   for( int i = 0; i < m_areas.Num(); i++ ) {
      NewSimulatorInventoryArea *ia = m_areas[i];
      if ( ia == area ) return area;
   }

   return 0;
}


/**
 * Add a inventory area to the array if it isn't already included in the array
 * 
 * @param area pointer to area to be added
 * @return bool if successful 
 **/ 
bool NewSimulatorInventory::AddInventoryArea( NewSimulatorInventoryArea *area ) {
	
   if ( FindInventoryArea( area ) ) {
      return false;
   }
   
   if (area->Num() > m_area_id)
      m_area_id = area->Num();
      
   m_areas.Add( area );
   m_inv_info.NumAreas = m_areas.Num();

   return true;
}


/**
 * Set inventory record data 
 * 
 * @param idrRec InventoryRecord data
 * @return true (simple copy)
 **/ 
bool NewSimulatorInventory::SetData( SaHpiInventoryRecT idrRec ) {
   
   memcpy( &m_inv_rec, &idrRec, sizeof( SaHpiInventoryRecT ));

   return true;
}


/**
 * Set inventory information data 
 * The fields UpdateCount and NumAreas are filled with internal values and are not 
 * overwritten.
 * 
 * @param idrInfo record with IdrInfo data
 * @return true 
 **/
bool NewSimulatorInventory::SetInfo( SaHpiIdrInfoT idrInfo ) {
   
   m_inv_info.IdrId = idrInfo.IdrId;
   m_inv_info.ReadOnly = idrInfo.ReadOnly;
   
   // UpdateCount is incremented if an official HPI function is called    
   // NumAreas is filled with the own value
   m_inv_info.NumAreas = m_areas.Num();
   
   return true;
}


// Official HPI functions
 
 /**
 * HPI function saHpiIdrInfoGet()
 * 
 * See also the description of the function inside the specification or header file.
 * Copying the internal reading values (if a read is allowed).
 * 
 * @param idrInfo address of the IdrInfo record to be filled
 * 
 * @return HPI return code
 **/    
SaErrorT NewSimulatorInventory::GetIdrInfo( SaHpiIdrInfoT &idrInfo ) {
   
   if ( &idrInfo == NULL)
      return SA_ERR_HPI_INVALID_PARAMS;
      
   m_inv_info.NumAreas = m_areas.Num();
   memcpy( &idrInfo, &m_inv_info, sizeof( SaHpiIdrInfoT ));

   return SA_OK;
}


/**
 * HPI function saHpiIdrAreaHeaderGet()
 * 
 * See also the description of the function inside the specification or header file.
 * Copying the internal reading values (if a read is allowed).
 * 
 * @param type type of area to be found
 * @param areaId Id of area to be found
 * @param nextId address to copy the Id of the next area header
 * @param header address to be filled with the area header record
 * 
 * @return HPI return code
 **/
SaErrorT NewSimulatorInventory::GetAreaHeader( SaHpiIdrAreaTypeT type,
                                               SaHpiEntryIdT areaId,
                                               SaHpiEntryIdT &nextId, 
                                               SaHpiIdrAreaHeaderT &header) {
   bool found = false, foundId = false, foundType = false;
   
   if ((areaId == SAHPI_LAST_ENTRY) ||
        (&nextId == NULL) || (&header == NULL)) 
      return SA_ERR_HPI_INVALID_PARAMS;

   for (int i = 0; i < m_areas.Num(); i++) {
      if ( (areaId == SAHPI_FIRST_ENTRY) ||
            (areaId == m_areas[i]->Num()) )
         foundId = true;
      
      if ( (type == SAHPI_IDR_AREATYPE_UNSPECIFIED) ||
            (type ==  m_areas[i]->Type()) ) 
         foundType = true;

      if (found) {
        nextId = m_areas[i]->Num();
        return SA_OK; 
      }
      
      if (foundType && foundId) {
         if (!found) {
            found = true;
            memcpy( &header, &m_areas[i]->AreaHeader(), sizeof( SaHpiIdrAreaHeaderT ));
         }
      }
      foundId = false;
      foundType = false;
   }
   if (found) {
      nextId = SAHPI_LAST_ENTRY;
      return SA_OK;
   }
   
   return SA_ERR_HPI_NOT_PRESENT;
}


/**
 * HPI function saHpiIdrAreaAdd()
 * 
 * See also the description of the function inside the specification or header file.
 * 
 * @param type type of area to add
 * @param newId address to copy the Id of the new area header
 * 
 * @return HPI return code
 **/
SaErrorT  NewSimulatorInventory::AddArea( SaHpiIdrAreaTypeT type, 
                                          SaHpiEntryIdT &newId ) {

   if ( IsReadOnly() )
      return SA_ERR_HPI_READ_ONLY;
      
   if ( &newId == NULL ) 
      return SA_ERR_HPI_INVALID_PARAMS;
      
   if ( type == SAHPI_IDR_AREATYPE_UNSPECIFIED )
      return SA_ERR_HPI_INVALID_DATA;
   
   if ( !(( type == SAHPI_IDR_AREATYPE_INTERNAL_USE ) ||
           ( type == SAHPI_IDR_AREATYPE_CHASSIS_INFO ) ||
           ( type == SAHPI_IDR_AREATYPE_BOARD_INFO ) ||
           ( type == SAHPI_IDR_AREATYPE_PRODUCT_INFO ) || 
           ( type == SAHPI_IDR_AREATYPE_OEM )) )
      return SA_ERR_HPI_INVALID_PARAMS;
   
   // Ok, we can add a new area
   SaHpiIdrAreaHeaderT ah;
   ah.AreaId    = ValidAreaId();
   ah.Type      = type;
   ah.ReadOnly  = SAHPI_FALSE;
   ah.NumFields = 0;
   
   NewSimulatorInventoryArea *ida = new NewSimulatorInventoryArea( ah );
   if ( AddInventoryArea( ida ) ) {
   	   newId = ida->Num();
   	   IncUpdateCount();
   	   return SA_OK;

   } else {
   	   return SA_ERR_HPI_INVALID_DATA;
   }
}

/**
 * HPI function saHpiIdrAreaAddById()
 *
 * See also the description of the function inside the specification or header file.
 * 
 * @param type Type of Area to add
 * @param id AreaId for the new Area to be created
 * 
 * @return HPI return code
 **/  
SaErrorT NewSimulatorInventory::AddAreaById( SaHpiIdrAreaTypeT type, 
                                             SaHpiEntryIdT id ) {
                                             	
   // Ok, we can try to add a new area
   stdlog << "DBG: NewSimulatorInventory::AddAreaById Try to add a new area by id.\n";
   
   if ( IsReadOnly() )
      return SA_ERR_HPI_READ_ONLY;
   
   if ( type == SAHPI_IDR_AREATYPE_UNSPECIFIED )
      return SA_ERR_HPI_INVALID_DATA;
   
   if ( !(( type == SAHPI_IDR_AREATYPE_INTERNAL_USE ) ||
           ( type == SAHPI_IDR_AREATYPE_CHASSIS_INFO ) ||
           ( type == SAHPI_IDR_AREATYPE_BOARD_INFO ) ||
           ( type == SAHPI_IDR_AREATYPE_PRODUCT_INFO ) || 
           ( type == SAHPI_IDR_AREATYPE_OEM )) )
      return SA_ERR_HPI_INVALID_PARAMS;
   
   if ( id == SAHPI_LAST_ENTRY )
      return SA_ERR_HPI_INVALID_PARAMS;
   
   SaHpiIdrAreaHeaderT ah;
   NewSimulatorInventoryArea *ida;
   
   ah.Type      = type;
   ah.ReadOnly  = SAHPI_FALSE;
   ah.NumFields = 0;
   
   if ( id == SAHPI_FIRST_ENTRY ) {
     ah.AreaId    = ValidAreaId();
     ida = new NewSimulatorInventoryArea( ah );
     m_areas.Insert( 0, ida );
     IncUpdateCount();
     stdlog << "DBG: Area was added with id " << ah.AreaId << "\n";
     
     return SA_OK;	  
   } 
   
   ah.AreaId    = id;
   for (int i=0; i < m_areas.Num(); i++) {
      if ( m_areas[i]->Num() == id )
         return SA_ERR_HPI_DUPLICATE;
   }

   ida = new NewSimulatorInventoryArea( ah );
   if ( AddInventoryArea( ida ) ) {
   	   IncUpdateCount();
   	   return SA_OK;
   	   
   } else {
   	   return SA_ERR_HPI_INVALID_DATA;
   }
}


/**
 * HPI function saHpiIdrAreaDel()
 *
 * See also the description of the function inside the specification or header file.
 * 
 * @param id Identifier of Area entry to delete from the IDR 
 * 
 * @return HPI return code
 **/
SaErrorT NewSimulatorInventory::DeleteArea( SaHpiEntryIdT id ) {
   NewSimulatorInventoryArea *ida = NULL;
   int idx = -1;
   
   if ( IsReadOnly() )
      return SA_ERR_HPI_READ_ONLY;
   
   if ( id == SAHPI_LAST_ENTRY )
      return SA_ERR_HPI_INVALID_PARAMS;

   // Find the correct area          
   if ( id == SAHPI_FIRST_ENTRY ) {
      ida = m_areas[0];
      idx = 0;
   } else {
      for (int i=0; i < m_areas.Num(); i++) {
         if (m_areas[i]->Num() == id) {
            ida = m_areas[i];
            idx = i;
            break;
         }
      }
      
      if ( ida == NULL )
         return SA_ERR_HPI_NOT_PRESENT;
   }
   
   if ( ida->IsReadOnly() )
      return SA_ERR_HPI_READ_ONLY;
   
   if ( ida->IncludesReadOnlyField() )
      return SA_ERR_HPI_READ_ONLY;
      
   ida->DeleteFields();
   m_areas.Rem( idx );
   IncUpdateCount();
   
   return SA_OK;
}


/**
 * Start for HPI function saHpiIdrFieldGet()
 * 
 * See also the description of the function inside the specification or header file.
 * Inside the method the correct AreaId is identified and afterwards 
 * NewSimulatorInventoryArea::GetField() is called.
 * 
 * @param areaId  Identifier for the IDA
 * @param fieldType Type of Inventory Data Field
 * @param fieldId Identifier of Field to retrieve
 * @param nextId address to store the FieldId of next field
 * @param field address into which the field information is placed
 * 
 * @return HPI return code
 **/
SaErrorT NewSimulatorInventory::GetField( SaHpiEntryIdT areaId,
                                          SaHpiIdrFieldTypeT fieldType, 
                                          SaHpiEntryIdT fieldId, 
                                          SaHpiEntryIdT &nextId, 
                                          SaHpiIdrFieldT &field ) {
   
   if ( ( areaId == SAHPI_LAST_ENTRY ) ||
         ( fieldId == SAHPI_LAST_ENTRY ) )
      return SA_ERR_HPI_INVALID_PARAMS;
   
   for (int i=0; i < m_areas.Num(); i++) {
      if ( ( m_areas[i]->Num() == areaId ) ||
            ( areaId == SAHPI_FIRST_ENTRY ) )
         return m_areas[i]->GetField( fieldType, fieldId, nextId, field );
   }
   return SA_ERR_HPI_NOT_PRESENT;
}


/**
 * Start for HPI function saHpiIdrFieldAdd()
 * 
 * See also the description of the function inside the specification or header file.
 * Inside the method the correct AreaId is identified and afterwards 
 * NewSimulatorInventoryArea::AddField() is called
 * 
 * @param field  address to the field to add to the IDA
 *
 * @return HPI return code
 **/
SaErrorT NewSimulatorInventory::AddField( SaHpiIdrFieldT &field ) {
   
   int i;
   SaErrorT rv;
   
   if ( ( field.AreaId == SAHPI_LAST_ENTRY ) ||
         ( field.FieldId == SAHPI_LAST_ENTRY ) )
      return SA_ERR_HPI_INVALID_PARAMS;
            
   for (i=0; i < m_areas.Num(); i++) {
      if ( ( m_areas[i]->Num() == field.AreaId ) ||
            ( field.AreaId == SAHPI_FIRST_ENTRY ) ) {
         
         if (  m_areas[i]->IsReadOnly() )
            return SA_ERR_HPI_READ_ONLY;
         
         rv = m_areas[i]->AddField( field );
         if ( rv == SA_OK )
            IncUpdateCount();  	

         return rv;
      }
   }
   return SA_ERR_HPI_NOT_PRESENT;
}


/**
 * Start for HPI function saHpiIdrFieldAddById()
 * 
 * See also the description of the function inside the specification or header file.
 * Inside the method the correct AreaId is identified and afterwards 
 * NewSimulatorInventoryArea::AddFieldById() is called
 * 
 * @param field address of field to add to the IDA
 *
 * @return HPI return code
 **/
SaErrorT NewSimulatorInventory::AddFieldById( SaHpiIdrFieldT &field ) {
   SaErrorT rv;
   int i;
   
   if ( ( field.AreaId == SAHPI_LAST_ENTRY ) ||
         ( field.FieldId == SAHPI_LAST_ENTRY ) )
      return SA_ERR_HPI_INVALID_PARAMS;
   
   for (i=0; i < m_areas.Num(); i++) {
      if ( ( m_areas[i]->Num() == field.AreaId ) ||
            ( field.AreaId == SAHPI_FIRST_ENTRY ) ) {
         
         if (  m_areas[i]->IsReadOnly() )
            return SA_ERR_HPI_READ_ONLY;
         
         rv = m_areas[i]->AddFieldById( field );
         if ( rv == SA_OK )
            IncUpdateCount();   	

         return rv;
      }
   }
   return SA_ERR_HPI_NOT_PRESENT;
}


/**
 * Start for HPI function saHpiIdrFieldSet()
 * 
 * See also the description of the function inside the specification or header file.
 * Inside the method the correct AreaId is identified and afterwards 
 * NewSimulatorInventoryArea::SetField() is called
 * 
 * @param field field to set in the IDA
 *
 * @return HPI return code
 **/
SaErrorT NewSimulatorInventory::SetField( SaHpiIdrFieldT field ) {
   SaErrorT rv;
   int i;
   
   if ( ( field.AreaId == SAHPI_LAST_ENTRY ) ||
         ( field.FieldId == SAHPI_LAST_ENTRY ) )
      return SA_ERR_HPI_INVALID_PARAMS;   
   
   for (i=0; i < m_areas.Num(); i++) {
      if ( ( m_areas[i]->Num() == field.AreaId ) ||
            ( field.AreaId == SAHPI_FIRST_ENTRY ) ) {
      
         rv = m_areas[i]->SetField( field );
         if ( rv == SA_OK )
            IncUpdateCount();

         return rv;
      }
   }
   return SA_ERR_HPI_NOT_PRESENT;

}


/**
 * Start for HPI function saHpiIdrFieldDelete()
 * 
 * See also the description of the function inside the specification or header file.
 * Inside the method the correct AreaId is identified and afterwards 
 * NewSimulatorInventoryArea::DeleteField() is called
 * 
 * @param areaId identifier for the IDA
 * @param fieldId Identifier of Field to delete from the IDA
 *
 * @return HPI return code
 **/
SaErrorT NewSimulatorInventory::DeleteField( SaHpiEntryIdT areaId, SaHpiEntryIdT fieldId ) {
   SaErrorT rv;
   
   if ( ( areaId == SAHPI_LAST_ENTRY ) ||
        ( fieldId == SAHPI_LAST_ENTRY ) )
      return SA_ERR_HPI_INVALID_PARAMS;
      
   for (int i=0; i < m_areas.Num(); i++) {
      if ( ( m_areas[i]->Num() == areaId ) ||
            ( areaId == SAHPI_FIRST_ENTRY ) ) {
         
         if ( m_areas[i]->IsReadOnly() )
            return SA_ERR_HPI_READ_ONLY;
         
         rv = m_areas[i]->DeleteField( fieldId );
         if ( rv == SA_OK )
            IncUpdateCount();   

         return rv;
      }
   }
   
   return SA_ERR_HPI_NOT_PRESENT;

}