GCOV Execution Analysis for rpt_utils.c

The left column is the number of times the code was executed during the unit test suites.

Exec CodeLine #
 
Source:rpt_utils.c
1
 
Graph:.libs/rpt_utils.gcno
2
 
Data:.libs/rpt_utils.gcda
3
 
Runs:378
4
 
Programs:378
5
 
/*      -*- linux-c -*-
6
 
 *
7
 
 * (C) Copyright IBM Corp. 2003, 2004
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  This
12
 
 * file and program are licensed under a BSD style license.  See
13
 
 * the Copying file included with the OpenHPI distribution for
14
 
 * full licensing terms.
15
 
 *
16
 
 * Author(s):
17
 
 *      Renier Morales < address removed >
18
 
 */
19
 
20
 
#include <string.h>
21
 
#include <sys/time.h>
22
 
23
 
#include <oh_utils.h>
24
 
25
 
static RPTEntry *get_rptentry_by_rid(RPTable *table, SaHpiResourceIdT rid)
26
16031 
{
27
16031 
        RPTEntry *rptentry = NULL;
28
 
29
16031 
        if (!table) {
30
0 
                dbg("ERROR: Cannot work on a null table pointer.");
31
0 
                return NULL;
32
 
        }
33
 
34
16031 
        if (!(table->rptlist)) {
35
 
                /*dbg("Info: RPT is empty.");*/
36
740 
                return NULL;
37
 
        }
38
 
39
15291 
        if (rid == SAHPI_FIRST_ENTRY) {
40
0 
                rptentry = (RPTEntry *) (table->rptlist->data);
41
 
        } else {
42
15291 
                rptentry = (RPTEntry *)g_hash_table_lookup(table->rptable, &rid);
43
 
        }
44
 
45
15291 
        return rptentry;
46
 
}
47
 
48
 
static RDRecord *get_rdrecord_by_id(RPTEntry *rptentry, SaHpiEntryIdT id)
49
6736 
{
50
6736 
        RDRecord *rdrecord = NULL;
51
 
52
6736 
        if (!rptentry) {
53
0 
                dbg("ERROR: Cannot lookup rdr inside null resource.");
54
0 
                return NULL;
55
 
        }
56
 
57
6736 
        if (!rptentry->rdrlist) {
58
 
                /*dbg("Info: RDR repository is empty.");*/
59
1481 
                return NULL;
60
 
        }
61
 
62
5255 
        if (id == SAHPI_FIRST_ENTRY) {
63
0 
                rdrecord = (RDRecord *) (rptentry->rdrlist->data);
64
 
        } else {
65
5255 
                rdrecord = (RDRecord *)g_hash_table_lookup(rptentry->rdrtable, &id);
66
 
        }
67
 
68
5255 
        return rdrecord;
69
 
}
70
 
71
 
static int check_instrument_id(SaHpiRptEntryT *rptentry, SaHpiRdrT *rdr)
72
5920 
{
73
5920 
        int result = 0;
74
5920 
        static const SaHpiInstrumentIdT SENSOR_AGGREGATE_MAX = 0x0000010F;
75
 
        
76
5920 
        switch (rdr->RdrType) {
77
 
                case SAHPI_SENSOR_RDR:
78
2960 
                        if (rdr->RdrTypeUnion.SensorRec.Num >= SAHPI_STANDARD_SENSOR_MIN &&
79
 
                            rdr->RdrTypeUnion.SensorRec.Num <= SAHPI_STANDARD_SENSOR_MAX) {
80
0 
                                if (rdr->RdrTypeUnion.SensorRec.Num > SENSOR_AGGREGATE_MAX) {
81
0 
                                        result = -1;
82
0 
                                } else if (rptentry->ResourceCapabilities &
83
 
                                           SAHPI_CAPABILITY_AGGREGATE_STATUS) {
84
0 
                                        result = 0;                                        
85
 
                                } else {
86
0 
                                        result = -1;
87
 
                                }
88
 
                        } else {
89
2960 
                                result = 0;
90
 
                        }
91
2960 
                        break;
92
 
                default:
93
2960 
                        result = 0;
94
 
        }
95
 
        
96
5920 
        return result;
97
 
}
98
 
99
 
static SaHpiInstrumentIdT get_rdr_type_num(SaHpiRdrT *rdr)
100
5920 
{
101
5920 
        SaHpiInstrumentIdT num = 0;
102
 
103
5920 
        switch (rdr->RdrType) {
104
 
                case SAHPI_CTRL_RDR:
105
1480 
                        num = rdr->RdrTypeUnion.CtrlRec.Num;
106
1480 
                        break;
107
 
                case SAHPI_SENSOR_RDR:
108
2960 
                        num = rdr->RdrTypeUnion.SensorRec.Num;
109
2960 
                        break;
110
 
                case SAHPI_INVENTORY_RDR:
111
1480 
                        num = rdr->RdrTypeUnion.InventoryRec.IdrId;
112
1480 
                        break;
113
 
                case SAHPI_WATCHDOG_RDR:
114
0 
                        num = rdr->RdrTypeUnion.WatchdogRec.WatchdogNum;
115
0 
                        break;
116
 
                case SAHPI_ANNUNCIATOR_RDR:
117
0 
                        num = rdr->RdrTypeUnion.AnnunciatorRec.AnnunciatorNum;
118
0 
                        break;
119
 
                default:
120
0 
                        num = 0;
121
 
        }
122
 
123
5920 
        return num;
124
 
}
125
 
126
2221 
static void update_rptable(RPTable *table) {
127
2221 
        struct timeval tv;
128
2221 
        SaHpiTimeT time;
129
 
130
2221 
        if (!table) {
131
0 
                dbg("ERROR: Cannot work on a null table pointer.");
132
0 
                return;
133
 
        }
134
 
135
2221 
        gettimeofday(&tv, NULL);
136
2221 
        time = (SaHpiTimeT) tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
137
 
138
2221 
        table->update_timestamp = time;
139
2221 
        table->update_count++;
140
 
}
141
 
142
 
/**
143
 
 * get_rdr_uid
144
 
 * @type: type of rdr
145
 
 * @num: id number of the RDR unique withing the RDR type for that resource
146
 
 *
147
 
 * Helper function to derive the Record id of an rdr from its @type and @num
148
 
 *
149
 
 * Returns: a derived Record Id used to identify RDRs within Resources
150
 
 */
151
 
SaHpiEntryIdT get_rdr_uid(SaHpiRdrTypeT type, SaHpiInstrumentIdT num)
152
6139 
{
153
6139 
        SaHpiEntryIdT uid;
154
 
155
6139 
        uid = ((SaHpiEntryIdT)type) << 16;
156
6139 
        uid = uid + (SaHpiEntryIdT)num;
157
 
158
6139 
        return uid;
159
 
}
160
 
161
 
/**
162
 
 * General RPT calls
163
 
 **/
164
 
165
 
/**
166
 
 * oh_init_rpt
167
 
 * @table: Pointer to RPTable structure to be initialized.
168
 
 *
169
 
 *
170
 
 * Returns: SA_OK on success Or minus SA_OK on error.
171
 
 **/
172
 
SaErrorT oh_init_rpt(RPTable *table)
173
748 
{
174
748 
        if (!table) {
175
0 
                dbg("ERROR: Cannot work on a null table pointer.");
176
0 
                return SA_ERR_HPI_INVALID_PARAMS;
177
 
        }
178
 
179
748 
        table->update_timestamp = SAHPI_TIME_UNSPECIFIED;
180
748 
        table->update_count = 0;
181
748 
        table->rptlist = NULL;
182
748 
        table->rptable = NULL;        
183
 
184
748 
        return SA_OK;
185
 
}
186
 
 
187
 
/**
188
 
 * oh_flush_rpt
189
 
 * @table: Pointer to the RPT to flush.
190
 
 *
191
 
 * Cleans RPT from all entries and RDRs and frees the memory
192
 
 * associated with them.
193
 
 *
194
 
 * Returns: SA_OK on success Or minus SA_OK on error.
195
 
 **/
196
 
SaErrorT oh_flush_rpt(RPTable *table)
197
0 
{
198
0 
        SaHpiRptEntryT *tmp_entry;
199
 
200
0 
        if (!table) {
201
0 
                dbg("ERROR: Cannot work on a null table pointer.");
202
0 
                return SA_ERR_HPI_INVALID_PARAMS;
203
 
        }
204
 
205
0 
        while ((tmp_entry = oh_get_resource_by_id(table, SAHPI_FIRST_ENTRY)) != NULL) {
206
0 
                oh_remove_resource(table, SAHPI_FIRST_ENTRY);
207
 
        }
208
 
209
0 
        return SA_OK;
210
 
}
211
 
212
 
/**
213
 
 * rpt_diff
214
 
 * @cur_rpt: IN. Pointer to RPTable that represents the current state of resources
215
 
 * and rdrs.
216
 
 * @new_rpt: IN. Pointer to RPTable that contains rpt entries and rdrs just recently
217
 
 * discovered.
218
 
 * @res_new: OUT. List of new or changed rpt entries
219
 
 * @rdr_new: OUT. List of new or changed rdrs
220
 
 * @res_gone: OUT. List of old and not present resources.
221
 
 * @rdr_gone: OUT. List of old and not present rdrs.
222
 
 *
223
 
 * Extracts from current the resources and rdrs that are not found
224
 
 * in new and puts them in res_gone and rdr_gone. Also, puts in res_new and rdr_new
225
 
 * the resources and rdrs that are not already in current Or that are not identical
226
 
 * to the ones in current.
227
 
 *
228
 
 * Returns: void.
229
 
 **/
230
 
void rpt_diff(RPTable *cur_rpt, RPTable *new_rpt,
231
 
              GSList **res_new, GSList **rdr_new,
232
0 
              GSList **res_gone, GSList **rdr_gone) {
233
 
234
0 
        SaHpiRptEntryT *res = NULL;
235
 
236
 
        /* Look for absent resources and rdrs */
237
0 
        for (res = oh_get_resource_by_id(cur_rpt, SAHPI_FIRST_ENTRY);
238
 
             res != NULL;
239
 
             res = oh_get_resource_next(cur_rpt, res->ResourceId)) {
240
 
241
0 
                SaHpiRptEntryT *tmp_res = oh_get_resource_by_id(new_rpt, res->ResourceId);
242
 
243
0 
                if (tmp_res == NULL) *res_gone = g_slist_append(*res_gone, (gpointer)res);
244
 
                else {
245
0 
                        SaHpiRdrT *rdr = NULL;
246
 
247
0 
                        for (rdr = oh_get_rdr_by_id(cur_rpt, res->ResourceId, SAHPI_FIRST_ENTRY);
248
 
                             rdr != NULL;
249
 
                             rdr = oh_get_rdr_next(cur_rpt, res->ResourceId, rdr->RecordId)) {
250
 
251
0 
                                SaHpiRdrT *tmp_rdr =
252
0 
                                        oh_get_rdr_by_id(new_rpt, res->ResourceId, rdr->RecordId);
253
 
254
0 
                                if (tmp_rdr == NULL)
255
0 
                                        *rdr_gone = g_slist_append(*rdr_gone, (gpointer)rdr);
256
 
                        }
257
 
                }
258
 
        }
259
 
260
 
        /* Look for new resources and rdrs*/
261
0 
        for (res = oh_get_resource_by_id(new_rpt, SAHPI_FIRST_ENTRY);
262
 
             res != NULL;
263
 
             res = oh_get_resource_next(new_rpt, res->ResourceId)) {
264
 
265
0 
                SaHpiRptEntryT *tmp_res = oh_get_resource_by_id(cur_rpt, res->ResourceId);
266
0 
                SaHpiRdrT *rdr = NULL;
267
0 
                if (!tmp_res || memcmp(res, tmp_res, sizeof(SaHpiRptEntryT))) {
268
0 
                        *res_new = g_slist_append(*res_new, (gpointer)res);
269
 
                }
270
 
271
 
272
 
273
0 
                for (rdr = oh_get_rdr_by_id(new_rpt, res->ResourceId, SAHPI_FIRST_ENTRY);
274
 
                     rdr != NULL;
275
 
                     rdr = oh_get_rdr_next(new_rpt, res->ResourceId, rdr->RecordId)) {
276
 
277
0 
                        SaHpiRdrT *tmp_rdr = NULL;
278
 
279
0 
                        if (tmp_res != NULL)
280
0 
                                tmp_rdr = oh_get_rdr_by_id(cur_rpt, res->ResourceId, rdr->RecordId);
281
 
282
0 
                        if (tmp_rdr == NULL || memcmp(rdr, tmp_rdr, sizeof(SaHpiRdrT)))
283
0 
                                *rdr_new = g_slist_append(*rdr_new, (gpointer)rdr);
284
 
285
 
                }
286
 
        }
287
 
}
288
 
289
 
/**
290
 
 * oh_get_rpt_info
291
 
 * @table: pointer to RPT
292
 
 * @update_count: pointer of where to place the rpt's update count
293
 
 * @update_timestamp: pointer of where to place the rpt's update timestamp
294
 
 *
295
 
 * Returns: SA_OK on success Or minus SA_OK on error.
296
 
 **/
297
 
SaErrorT oh_get_rpt_info(RPTable *table,
298
 
                         SaHpiUint32T *update_count,
299
 
                         SaHpiTimeT *update_timestamp)
300
0 
{
301
0 
        if (!table || !update_count || !update_timestamp) {
302
0 
                dbg("ERROR: Invalid parameters.");
303
0 
                return SA_ERR_HPI_INVALID_PARAMS;
304
 
        }
305
 
306
0 
        *update_count = table->update_count;
307
0 
        *update_timestamp = table->update_timestamp;
308
 
309
0 
        return SA_OK;
310
 
}
311
 
312
 
/**
313
 
 * Resource interface functions
314
 
 */
315
 
316
 
/**
317
 
 * oh_add_resource
318
 
 * @table: Pointer to the RPT to which the RPT entry will be added.
319
 
 * @entry: The RPT entry (resource) to be added to the RPT.
320
 
 * @data: Pointer to private data for storing along with the RPT entry.
321
 
 * @owndata: boolean flag. true (%KEEP_RPT_DATA) to tell the interface *not*
322
 
 * to free the data when the resource is removed. false (%FREE_RPT_DATA) to tell
323
 
 * the interface to free the data when the resource is removed.
324
 
 *
325
 
 * Add a RPT entry to the RPT along with some private data.
326
 
 * If an RPT entry with the same resource id exists int the RPT, it will be
327
 
 * overlayed with the new RPT entry. Also, this function will assign the
328
 
 * resource id as its entry id since it is expected to be unique for the table.
329
 
 * The update count and timestamp will not be updated if the entry being added 
330
 
 * already existed in the table and was the same.
331
 
 * 
332
 
 * Returns: SA_OK on success Or minus SA_OK on error. SA_ERR_HPI_INVALID_PARAMS will
333
 
 * be returned if @table is NULL, @entry is NULL, ResourceId in @entry equals
334
 
 * SAHPI_FIRST_ENTRY, ResourceId in @entry equals SAHPI_UNSPECIFIED_RESOURCE_ID,
335
 
 * or ResourceEntity in @entry has a malformed entity path (look at the
336
 
 * entity path utils documentation).
337
 
 **/
338
 
SaErrorT oh_add_resource(RPTable *table, SaHpiRptEntryT *entry, void *data, int owndata)
339
2220 
{
340
2220 
        RPTEntry *rptentry;
341
2220 
        int update_info = 0;
342
 
343
2220 
        if (!table) {
344
0 
                dbg("ERROR: Cannot work on a null table pointer.");
345
0 
                return SA_ERR_HPI_INVALID_PARAMS;
346
2220 
        } else if (!entry) {
347
0 
                dbg("Failed to add. RPT entry is NULL.");
348
0 
                return SA_ERR_HPI_INVALID_PARAMS;
349
2220 
        } else if (entry->ResourceId == SAHPI_FIRST_ENTRY) {
350
0 
                dbg("Failed to add. RPT entry needs a resource id before being added");
351
0 
                return SA_ERR_HPI_INVALID_PARAMS;
352
2220 
        } else if (entry->ResourceId == SAHPI_UNSPECIFIED_RESOURCE_ID) {
353
0 
                dbg("Failed to add. RPT entry has an invalid/reserved id assigned. (SAHPI_UNSPECIFIED_RESOURCE_ID)");
354
0 
                return SA_ERR_HPI_INVALID_PARAMS;
355
2220 
        } else if (validate_ep(&(entry->ResourceEntity))) {
356
0 
                dbg("Failed to add RPT entry. Entity path does not contain root element.");
357
0 
                return SA_ERR_HPI_INVALID_PARAMS;
358
 
        }
359
 
360
2220 
        entry->EntryId = entry->ResourceId;
361
 
        /* Check to see if the entry is in the RPTable already */
362
2220 
        rptentry = get_rptentry_by_rid(table, entry->ResourceId);
363
 
        /* If not, create new RPTEntry */
364
2220 
        if (!rptentry) {
365
2220 
                rptentry = (RPTEntry *)g_malloc0(sizeof(RPTEntry));
366
2220 
                if (!rptentry) {
367
0 
                        dbg("Not enough memory to add RPT entry.");
368
0 
                        return SA_ERR_HPI_OUT_OF_MEMORY;
369
 
                }
370
2220 
                update_info = 1; /* Have a new changed entry */
371
 
                /* Put new RPTEntry in RPTable */
372
2220 
                table->rptlist = g_slist_append(table->rptlist, (gpointer)rptentry);
373
 
374
 
                /* Add to rpt hash table */
375
2220 
                if (!table->rptable) /* Create hash table if it doesn't exist */
376
740 
                        table->rptable = g_hash_table_new(g_int_hash, g_int_equal);
377
 
378
2220 
                rptentry->rpt_entry.EntryId = entry->ResourceId;
379
2220 
                g_hash_table_insert(table->rptable,
380
 
                                    &(rptentry->rpt_entry.EntryId),
381
 
                                    rptentry);
382
 
        }
383
 
        /* Else, modify existing RPTEntry */
384
2220 
        if (rptentry->data && rptentry->data != data && !rptentry->owndata)
385
0 
                g_free(rptentry->data);
386
2220 
        rptentry->data = data;
387
2220 
        rptentry->owndata = owndata;
388
 
        /* Check if we really have a new/changed entry */
389
2220 
        if (update_info || memcmp(entry, &(rptentry->rpt_entry), sizeof(SaHpiRptEntryT))) {
390
2220 
                update_info = 1;
391
2220 
                rptentry->rpt_entry = *entry;
392
 
        }
393
 
        
394
2220 
        if (update_info) update_rptable(table);
395
 
396
2220 
        return SA_OK;
397
 
}
398
 
399
 
/**
400
 
 * oh_remove_resource
401
 
 * @table: Pointer to the RPT from which an RPT entry will be removed.
402
 
 * @rid: Resource id of the RPT entry to be removed.
403
 
 *
404
 
 * Remove a resource from the RPT. If the @rid is
405
 
 * %SAHPI_FIRST_ENTRY, the first RPT entry in the table will be removed.
406
 
 * The void data will be freed if @owndata was false (%FREE_RPT_DATA) when adding
407
 
 * the resource, otherwise if @owndata was true (%KEEP_RPT_DATA) it will not be freed.
408
 
 *
409
 
 * Returns: SA_OK on success Or minus SA_OK on error.
410
 
 **/
411
 
SaErrorT oh_remove_resource(RPTable *table, SaHpiResourceIdT rid)
412
1 
{
413
1 
        RPTEntry *rptentry;
414
 
415
1 
        if (!table) {
416
0 
                dbg("ERROR: Cannot work on a null table pointer.");
417
0 
                return SA_ERR_HPI_INVALID_PARAMS;
418
 
        }
419
 
420
1 
        rptentry = get_rptentry_by_rid(table, rid);
421
1 
        if (!rptentry) {
422
0 
                dbg("Failed to remove RPT entry. No Resource found by that id");
423
0 
                return SA_ERR_HPI_NOT_PRESENT;
424
 
        } else {
425
1 
                SaHpiRdrT *tmp_rdr;
426
 
                /* Remove all RDRs for the resource first */
427
1 
                while ((tmp_rdr = oh_get_rdr_by_id(table, rid, SAHPI_FIRST_ENTRY)) != NULL) {
428
0 
                        oh_remove_rdr(table, rid, SAHPI_FIRST_ENTRY);
429
 
                }
430
 
                /* then remove the resource itself. */
431
1 
                table->rptlist = g_slist_remove(table->rptlist, (gpointer)rptentry);
432
1 
                if (!rptentry->owndata) g_free(rptentry->data);
433
1 
                g_hash_table_remove(table->rptable, &(rptentry->rpt_entry.EntryId));
434
1 
                g_free((gpointer)rptentry);
435
1 
                if (!table->rptlist) {
436
0 
                        g_hash_table_destroy(table->rptable);
437
0 
                        table->rptable = NULL;
438
 
                }
439
 
        }
440
 
441
1 
        update_rptable(table);
442
 
443
1 
        return SA_OK;
444
 
}
445
 
446
 
/**
447
 
 * oh_get_resource_data
448
 
 * @table: Pointer to the RPT for looking up the RPT entry's private data.
449
 
 * @rid: Resource id of the RPT entry that holds the private data.
450
 
 *
451
 
 * Get the private data for a RPT entry.  If the @rid is
452
 
 * %SAHPI_FIRST_ENTRY, the first RPT entry's data in the table will be returned.
453
 
 *
454
 
 * Returns: A void pointer to the private data for the RPT entry requested, or NULL
455
 
 * if the RPT entry was not found or the table was a NULL pointer.
456
 
 **/
457
 
void *oh_get_resource_data(RPTable *table, SaHpiResourceIdT rid)
458
357 
{
459
 
460
357 
        RPTEntry *rptentry;
461
 
462
357 
        if (!table) {
463
0 
                dbg("ERROR: Cannot work on a null table pointer.");
464
0 
                return NULL;
465
 
        }
466
 
467
357 
        rptentry = get_rptentry_by_rid(table, rid);
468
357 
        if (!rptentry) {
469
 
                /*dbg("Warning: RPT entry not found. Returning NULL.");*/
470
2 
                return NULL;
471
 
        }
472
 
473
355 
        return rptentry->data;
474
 
}
475
 
476
 
/**
477
 
 * oh_get_resource_by_id
478
 
 * @table: Pointer to the RPT for looking up the RPT entry.
479
 
 * @rid: Resource id of the RPT entry to be looked up.
480
 
 *
481
 
 * Get a RPT entry from the RPT by using the resource id.
482
 
 * If @rid is %SAHPI_FIRST_ENTRY, the first RPT entry in the table will be returned.
483
 
 *
484
 
 * Returns:
485
 
 * Pointer to the RPT entry found or NULL if an RPT entry by that
486
 
 * id was not found or the table was a NULL pointer.
487
 
 **/
488
 
SaHpiRptEntryT *oh_get_resource_by_id(RPTable *table, SaHpiResourceIdT rid)
489
1662 
{
490
1662 
        RPTEntry *rptentry;
491
 
492
1662 
        if (!table) {
493
0 
                dbg("ERROR: Cannot work on a null table pointer.");
494
0 
                return NULL;
495
 
        }
496
 
497
1662 
        rptentry = get_rptentry_by_rid(table, rid);
498
1662 
        if (!rptentry) {
499
 
                /*dbg("Warning: RPT entry not found. Returning NULL.");*/
500
46 
                return NULL;
501
 
        }
502
 
503
1616 
        return &(rptentry->rpt_entry);
504
 
}
505
 
506
 
/**
507
 
 * oh_get_resource_by_ep
508
 
 * @table: Pointer to the RPT for looking up the RPT entry.
509
 
 * @ep: Entity path of the RPT entry to be looked up.
510
 
 *
511
 
 * Get a RPT entry from the RPT by using the entity path.
512
 
 *
513
 
 * Returns:
514
 
 * Pointer to the RPT entry found or NULL if an RPT entry by that
515
 
 * entity path was not found or the table was a NULL pointer.
516
 
 **/
517
 
SaHpiRptEntryT *oh_get_resource_by_ep(RPTable *table, SaHpiEntityPathT *ep)
518
2 
{
519
2 
        RPTEntry *rptentry = NULL;
520
2 
        GSList *node;
521
 
522
2 
        if (!(table)) {
523
0 
                dbg("ERROR: Cannot work on a null table pointer.");
524
0 
                return NULL;
525
 
        }
526
 
527
8 
        for (node = table->rptlist; node != NULL; node = node->next) {
528
6 
                rptentry = (RPTEntry *) node->data;
529
6 
                if (!ep_cmp(&(rptentry->rpt_entry.ResourceEntity), ep))
530
0 
                        break;
531
6 
                else rptentry = NULL;
532
 
        }
533
 
534
2 
        if (!rptentry) {
535
 
                /*dbg("Warning: RPT entry not found. Returning NULL.");*/
536
2 
                return NULL;
537
 
        }
538
 
539
0 
        return &(rptentry->rpt_entry);
540
 
}
541
 
542
 
/**
543
 
 * oh_get_resource_next
544
 
 * @table: Pointer to the RPT for looking up the RPT entry.
545
 
 * @rid_prev: Resource id of the RPT entry previous to the one being looked up.
546
 
 *
547
 
 * Get the RPT entry next to the specified RPT entry
548
 
 * from the RPT. If @rid_prev is %SAHPI_FIRST_ENTRY, the first RPT entry
549
 
 * in the table will be returned.
550
 
 *
551
 
 * Returns:
552
 
 * Pointer to the RPT entry found or NULL if the previous RPT entry by that
553
 
 * id was not found or the table was a NULL pointer.
554
 
 **/
555
 
SaHpiRptEntryT *oh_get_resource_next(RPTable *table, SaHpiResourceIdT rid_prev)
556
2818 
{
557
2818 
        RPTEntry *rptentry = NULL;
558
2818 
        GSList *node;
559
 
560
2818 
        if (!(table)) {
561
0 
                dbg("ERROR: Cannot work on a null table pointer.");
562
0 
                return NULL;
563
 
        }
564
 
565
2818 
        if (rid_prev == SAHPI_FIRST_ENTRY) {
566
1021 
                if (table->rptlist) {
567
1021 
                        rptentry = (RPTEntry *)(table->rptlist->data);
568
 
                } else {
569
 
                        /*dbg("Info: RPT is empty. Returning NULL.");*/
570
0 
                        return NULL;
571
 
                }
572
 
        } else {
573
3485 
                for (node = table->rptlist; node != NULL; node = node->next) {
574
3485 
                        rptentry = (RPTEntry *) node->data;
575
3485 
                        if (rptentry->rpt_entry.ResourceId == rid_prev) {
576
1797 
                                if (node->next) rptentry = (RPTEntry *)(node->next->data);
577
 
                                else {
578
 
                                        /*dbg("Info: Reached end of RPT.");*/
579
546 
                                        return NULL;
580
 
                                }
581
1688 
                                break;
582
1688 
                        } else rptentry = NULL;
583
 
                }
584
 
        }
585
 
586
2272 
        if (!rptentry) {
587
 
                /*dbg("Warning: RPT entry not found. Returning NULL.");*/
588
0 
                return NULL;
589
 
        }
590
 
591
2272 
        return &(rptentry->rpt_entry);
592
 
}
593
 
594
 
/**
595
 
 * RDR interface functions
596
 
 */
597
 
598
 
/**
599
 
 * oh_add_rdr
600
 
 * @table: Pointer to RPT table containig the RPT entry to which the RDR will belong.
601
 
 * @rid: Id of the RPT entry that will own the RDR to be added.
602
 
 * @rdr: RDR to be added to an RPT entry's RDR repository.
603
 
 * @data: Pointer to private data belonging to the RDR that is being added.
604
 
 * @owndata: boolean flag. true (%KEEP_RPT_DATA) to tell the interface *not*
605
 
 * to free the data when the rdr is removed. false (%FREE_RPT_DATA) to tell
606
 
 * the interface to free the data when the rdr is removed.
607
 
 *
608
 
 * Add an RDR to a RPT entry's RDR repository.
609
 
 * If an RDR is found with the same record id as the one being added, the RDR being
610
 
 * added will overlay the existing one. Also, a unique record id will be assigned
611
 
 * to it based on the RDR type and its type's numeric id.
612
 
 * All rdr interface funtions, except oh_add_rdr() will act in the context of
613
 
 * the first RPT entry in the table, if @rid is %SAHPI_FIRST_ENTRY.
614
 
 *
615
 
 * Returns: SA_OK on success Or minus SA_OK on error. Will return
616
 
 * SA_ERR_HPI_INVALID_PARAMS if instrument id is invalid. An invalid
617
 
 * intrument id for a sensor is in the range of 0x100-0x1FF. An aggregate type
618
 
 * of sensor can have its instrument id in the range of 0x100-0x10F, but
619
 
 * the resource must have the aggregate sensor capabilitiy bit set.
620
 
 **/
621
 
SaErrorT oh_add_rdr(RPTable *table, SaHpiResourceIdT rid, SaHpiRdrT *rdr, void *data, int owndata)
622
5920 
{
623
5920 
        RPTEntry *rptentry;
624
5920 
        RDRecord *rdrecord;
625
5920 
        SaHpiInstrumentIdT type_num;
626
 
627
5920 
        if (!table) {
628
0 
                dbg("Error: Cannot work on a null table pointer.");
629
0 
                return SA_ERR_HPI_INVALID_PARAMS;
630
5920 
        } else if (!rdr) {
631
0 
                dbg("Failed to add. RDR is NULL.");
632
0 
                return SA_ERR_HPI_INVALID_PARAMS;
633
 
        }
634
 
635
5920 
        rptentry = get_rptentry_by_rid(table, rid);
636
5920 
        if (!rptentry){
637
0 
                dbg("Failed to add RDR. Parent RPT entry was not found in table.");
638
0 
                return SA_ERR_HPI_NOT_PRESENT;
639
 
        }
640
 
        
641
5920 
        if (check_instrument_id(&(rptentry->rpt_entry), rdr)) {
642
0 
                dbg("Invalid instrument id found in RDR.");
643
0 
                return SA_ERR_HPI_INVALID_PARAMS;
644
 
        }
645
 
646
5920 
        type_num = get_rdr_type_num(rdr);
647
 
648
 
        /* Form correct RecordId. */
649
5920 
        rdr->RecordId = get_rdr_uid(rdr->RdrType, type_num);
650
 
        /* Check if record exists */
651
5920 
        rdrecord = get_rdrecord_by_id(rptentry, rdr->RecordId);
652
 
        /* If not, create new rdr */
653
5920 
        if (!rdrecord) {
654
5920 
                rdrecord = (RDRecord *)g_malloc0(sizeof(RDRecord));
655
5920 
                if (!rdrecord) {
656
0 
                        dbg("Not enough memory to add RDR.");
657
0 
                        return SA_ERR_HPI_OUT_OF_MEMORY;
658
 
                }
659
 
                /* Put new rdrecord in rdr repository */
660
5920 
                rptentry->rdrlist = g_slist_append(rptentry->rdrlist, (gpointer)rdrecord);
661
 
                /* Create rdr hash table if first rdr here */
662
5920 
                if (!rptentry->rdrtable)
663
1480 
                        rptentry->rdrtable = g_hash_table_new(g_int_hash, g_int_equal);
664
 
665
5920 
                rdrecord->rdr.RecordId = rdr->RecordId;
666
5920 
                g_hash_table_insert(rptentry->rdrtable,
667
 
                                    &(rdrecord->rdr.RecordId),
668
 
                                    rdrecord);
669
 
        }
670
 
        /* Else, modify existing rdrecord */
671
5920 
        if (rdrecord->data && rdrecord->data != data && !rdrecord->owndata)
672
0 
                g_free(rdrecord->data);
673
5920 
        rdrecord->data = data;
674
5920 
        rdrecord->owndata = owndata;
675
5920 
        rdrecord->rdr = *rdr;
676
 
677
5920 
        return SA_OK;
678
 
}
679
 
680
 
/**
681
 
 * oh_remove_rdr
682
 
 * @table: Pointer to RPT table containig the RPT entry from which the RDR will
683
 
 * be removed.
684
 
 * @rid: Id of the RPT entry from which the RDR will be removed.
685
 
 * @rdrid: Record id of the RDR to remove.
686
 
 *
687
 
 *
688
 
 * Remove an RDR from a RPT entry's RDR repository.
689
 
 * If @rdrid is %SAHPI_FIRST_ENTRY, the first RDR in the repository will be removed.
690
 
 * If @owndata was set to false (%FREE_RPT_DATA) on the rdr when it was added,
691
 
 * the data will be freed, otherwise if it was set to true (%KEEP_RPT_DATA),
692
 
 * it will not be freed.
693
 
 * All rdr interface funtions, except oh_add_rdr() will act in the context of
694
 
 * the first RPT entry in the table, if @rid is %SAHPI_FIRST_ENTRY.
695
 
 *
696
 
 * Returns: SA_OK on success Or minus SA_OK on error.
697
 
 **/
698
 
SaErrorT oh_remove_rdr(RPTable *table, SaHpiResourceIdT rid, SaHpiEntryIdT rdrid)
699
0 
{
700
0 
        RPTEntry *rptentry;
701
0 
        RDRecord *rdrecord;
702
 
703
0 
        if (!table) {
704
0 
                dbg("Error: Cannot work on a null table pointer.");
705
0 
                return SA_ERR_HPI_INVALID_PARAMS;
706
 
        }
707
 
708
0 
        rptentry = get_rptentry_by_rid(table, rid);
709
0 
        if (!rptentry) {
710
0 
                dbg("Failed to remove RDR. Parent RPT entry was not found.");
711
0 
                return SA_ERR_HPI_NOT_PRESENT;
712
 
        }
713
 
714
0 
        rdrecord = get_rdrecord_by_id(rptentry, rdrid);
715
0 
        if (!rdrecord) {
716
0 
                dbg("Failed to remove RDR. Could not be found.");
717
0 
                return SA_ERR_HPI_NOT_PRESENT;
718
 
        } else {
719
0 
                rptentry->rdrlist = g_slist_remove(rptentry->rdrlist, (gpointer)rdrecord);
720
0 
                if (!rdrecord->owndata) g_free(rdrecord->data);
721
0 
                g_hash_table_remove(rptentry->rdrtable, &(rdrecord->rdr.RecordId));
722
0 
                g_free((gpointer)rdrecord);
723
0 
                if (!rptentry->rdrlist) {
724
0 
                        g_hash_table_destroy(rptentry->rdrtable);
725
0 
                        rptentry->rdrtable = NULL;
726
 
                }
727
 
        }
728
 
729
0 
        return SA_OK;
730
 
}
731
 
732
 
/**
733
 
 * oh_get_rdr_data
734
 
 * @table: Pointer to RPT table containig the RPT entry from which the RDR's data
735
 
 * will be read.
736
 
 * @rid: Id of the RPT entry from which the RDR's data will be read.
737
 
 * @rdrid: Record id of the RDR to read data from.
738
 
 *
739
 
 *
740
 
 * Get the private data associated to an RDR.
741
 
 * If @rdrid is %SAHPI_FIRST_ENTRY, the first RDR's data in the repository will be returned.
742
 
 * All rdr interface funtions, except oh_add_rdr() will act in the context of
743
 
 * the first RPT entry in the table, if @rid is %SAHPI_FIRST_ENTRY.
744
 
 *
745
 
 * Returns: A void pointer to the RDR data, or NULL if no data for that RDR was found or
746
 
 * the table pointer is NULL.
747
 
 **/
748
 
void *oh_get_rdr_data(RPTable *table, SaHpiResourceIdT rid, SaHpiEntryIdT rdrid)
749
0 
{
750
0 
        RPTEntry *rptentry;
751
0 
        RDRecord *rdrecord;
752
 
753
0 
        if (!table) {
754
0 
                dbg("Error: Cannot work on a null table pointer.");
755
0 
                return NULL;
756
 
        }
757
 
758
0 
        rptentry = get_rptentry_by_rid(table, rid);
759
0 
        if (!rptentry) {
760
0 
                dbg("Warning: RPT entry not found. Cannot find RDR.");
761
0 
                return NULL; /* No resource found by that id */
762
 
        }
763
 
764
0 
        rdrecord = get_rdrecord_by_id(rptentry, rdrid);
765
0 
        if (!rdrecord) {
766
 
                /*dbg("Warning: RDR not found. Returning NULL.");*/
767
0 
                return NULL;
768
 
        }
769
 
770
0 
        return rdrecord->data;
771
 
}
772
 
773
 
/**
774
 
 * oh_get_rdr_by_id
775
 
 * @table: Pointer to RPT table containig the RPT entry tha has the RDR
776
 
 * being looked up.
777
 
 * @rid: Id of the RPT entry containing the RDR being looked up.
778
 
 * @rdrid: Record id of the RDR being looked up.
779
 
 *
780
 
 * Get a reference to an RDR by its record id.
781
 
 * If @rdrid is %SAHPI_FIRST_ENTRY, the first RDR in the repository will be returned.
782
 
 * All rdr interface funtions, except oh_add_rdr() will act in the context of
783
 
 * the first RPT entry in the table, if @rid is %SAHPI_FIRST_ENTRY.
784
 
 *
785
 
 * Returns:
786
 
 * Reference to the RDR looked up or NULL if no RDR was found.
787
 
 **/
788
 
SaHpiRdrT *oh_get_rdr_by_id(RPTable *table, SaHpiResourceIdT rid, SaHpiEntryIdT rdrid)
789
597 
{
790
597 
        RPTEntry *rptentry;
791
597 
        RDRecord *rdrecord;
792
 
793
597 
        if (!table) {
794
0 
                dbg("Error: Cannot work on a null table pointer.");
795
0 
                return NULL;
796
 
        }
797
 
798
597 
        rptentry = get_rptentry_by_rid(table, rid);
799
597 
        if (!rptentry) {
800
0 
                dbg("Warning: RPT entry not found. Cannot find RDR.");
801
0 
                return NULL; /* No resource found by that id */
802
 
        }
803
 
804
597 
        rdrecord = get_rdrecord_by_id(rptentry, rdrid);
805
597 
        if (!rdrecord) {
806
 
                /*dbg("Warning: RDR not found. Returning NULL.");*/
807
2 
                return NULL;
808
 
        }
809
 
810
595 
        return &(rdrecord->rdr);
811
 
}
812
 
813
 
/**
814
 
 * oh_get_rdr_by_type
815
 
 * @table: Pointer to RPT table containig the RPT entry tha has the RDR
816
 
 * being looked up.
817
 
 * @rid: Id of the RPT entry containing the RDR being looked up.
818
 
 * @type: RDR Type of the RDR being looked up.
819
 
 * @num: RDR id within the RDR type for the specified RPT entry.
820
 
 *
821
 
 * Get a reference to an RDR by its type and number.
822
 
 * All rdr interface funtions, except oh_add_rdr() will act in the context of
823
 
 * the first RPT entry in the table, if @rid is %SAHPI_FIRST_ENTRY.
824
 
 *
825
 
 * Returns:
826
 
 * Reference to the RDR looked up or NULL if no RDR was found.
827
 
 **/
828
 
SaHpiRdrT *oh_get_rdr_by_type(RPTable *table, SaHpiResourceIdT rid,
829
 
                              SaHpiRdrTypeT type, SaHpiInstrumentIdT num)
830
219 
{
831
219 
        RPTEntry *rptentry;
832
219 
        RDRecord *rdrecord;
833
219 
        SaHpiEntryIdT rdr_uid;
834
 
835
219 
        if (!table) {
836
0 
                dbg("Error: Cannot work on a null table pointer.");
837
0 
                return NULL;
838
 
        }
839
 
840
219 
        rptentry = get_rptentry_by_rid(table, rid);
841
219 
        if (!rptentry) {
842
0 
                dbg("Warning: RPT entry not found. Cannot find RDR.");
843
0 
                return NULL; /* No resource found by that id */
844
 
        }
845
 
846
 
        /* Get rdr_uid from type/num combination */
847
219 
        rdr_uid = get_rdr_uid(type, num);
848
219 
        rdrecord = get_rdrecord_by_id(rptentry, rdr_uid);
849
219 
        if (!rdrecord) {
850
 
                /*dbg("Warning: RDR not found. Returning NULL.");*/
851
11 
                return NULL;
852
 
        }
853
 
854
208 
        return &(rdrecord->rdr);
855
 
}
856
 
857
 
/**
858
 
 * oh_get_rdr_next
859
 
 * @table: Pointer to the RPT containing the RPT entry to look up the RDR in.
860
 
 * @rid: Id of the RPT entry containing the RDR being looked up.
861
 
 * @rdrid_prev: Record id of the RDR previous to the one being looked up, relative
862
 
 * to the specified RPT entry.
863
 
 *
864
 
 * Get the RDR next to the specified RDR in the specified
865
 
 * RPT entry's repository. If @rdrid_prev is %SAHPI_FIRST_ENTRY, the first RDR
866
 
 * in the repository will be returned.
867
 
 * All rdr interface funtions, except oh_add_rdr() will act in the context of
868
 
 * the first RPT entry in the table, if @rid is %SAHPI_FIRST_ENTRY.
869
 
 *
870
 
 * Returns:
871
 
 * Pointer to the RDR found or NULL if the previous RDR by that
872
 
 * id was not found. If the @rdrid_prev was %SAHPI_FIRST_ENTRY, the first RDR in the list
873
 
 * will be returned.
874
 
 **/
875
 
SaHpiRdrT *oh_get_rdr_next(RPTable *table, SaHpiResourceIdT rid, SaHpiEntryIdT rdrid_prev)
876
5055 
{
877
5055 
        RPTEntry *rptentry;
878
5055 
        RDRecord *rdrecord = NULL;
879
5055 
        GSList *node;
880
 
881
5055 
        if (!table) {
882
0 
                dbg("Error: Cannot work on a null table pointer.");
883
0 
                return NULL;
884
 
        }
885
 
886
5055 
        rptentry = get_rptentry_by_rid(table, rid);
887
5055 
        if (!rptentry) {
888
0 
                dbg("Warning: RPT entry not found. Cannot find RDR.");
889
0 
                return NULL; /* No resource found by that id */
890
 
        }
891
 
892
5055 
        if (rdrid_prev == SAHPI_FIRST_ENTRY) {
893
1315 
                if (rptentry->rdrlist) {
894
949 
                        rdrecord = (RDRecord *)(rptentry->rdrlist->data);
895
 
                } else {
896
 
                        /*dbg("Info: RDR repository is empty. Returning NULL.");*/
897
366 
                        return NULL;
898
 
                }
899
 
        } else {
900
11263 
                for (node = rptentry->rdrlist; node != NULL; node = node->next) {
901
11263 
                        rdrecord = (RDRecord *)node->data;
902
11263 
                        if (rdrecord->rdr.RecordId == rdrid_prev) {
903
3740 
                                if (node->next) rdrecord = (RDRecord *)(node->next->data);
904
 
                                else {
905
 
                                        /*dbg("Info: Reached end of RDR repository.")*/
906
881 
                                        return NULL;
907
 
                                }
908
7523 
                                break;
909
 
                        }
910
7523 
                        else rdrecord = NULL;
911
 
                }
912
 
        }
913
 
914
3808 
        if (!rdrecord) {
915
 
                /*dbg("Warning: RDR not found. Returning NULL.");*/
916
0 
                return NULL;
917
 
        }
918
 
919
3808 
        return &(rdrecord->rdr);
920
 
}
921