File: rpt_utils.c

package info (click to toggle)
openhpi 3.8.0-2.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 31,796 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 (981 lines) | stat: -rw-r--r-- 33,574 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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
/*      -*- linux-c -*-
 *
 * (C) Copyright IBM Corp. 2003, 2005
 *
 * 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.
 *
 * Author(s):
 *      Renier Morales <renier@openhpi.org>
 */

#include <string.h>

#include <oh_utils.h>
#include <oh_error.h>

typedef struct {
        SaHpiRptEntryT rpt_entry;
        int owndata;
        void *data; /* private data for the owner of the RPTable */
        SaHpiUint32T update_count; /* RDR Update counter */
        GSList *rdrlist; /* Contains RDRecords for sequence lookups */
        GHashTable *rdrtable; /* Contains RDRecords for fast RecordId lookups */
} RPTEntry;

typedef struct {
       SaHpiRdrT rdr;
       int owndata;
       void *data; /* private data for the owner of the rpt entry. */
} RDRecord;


static RPTEntry *get_rptentry_by_rid(RPTable *table, SaHpiResourceIdT rid)
{
        GSList *rptnode = NULL;
        RPTEntry *rptentry = NULL;

        if (!table) {
                return NULL;
        }

        if (!(table->rptlist)) {
                /*DBG("Info: RPT is empty.");*/
                return NULL;
        }

        if (rid == SAHPI_FIRST_ENTRY) {
                rptentry = (RPTEntry *) (table->rptlist->data);
        } else {
                rptnode = (GSList *)g_hash_table_lookup(table->rptable, &rid);
                rptentry = rptnode ? (RPTEntry *)rptnode->data : NULL;
        }

        return rptentry;
}

static GSList *get_rptnode_by_rid(RPTable *table, SaHpiResourceIdT rid)
{
        GSList *rptnode = NULL;

        if (!table) {
                return NULL;
        }

        if (!(table->rptlist)) {
                /*DBG("Info: RPT is empty.");*/
                return NULL;
        }

        if (rid == SAHPI_FIRST_ENTRY) {
                rptnode = (GSList *) (table->rptlist);
        } else {
                rptnode = (GSList *)g_hash_table_lookup(table->rptable, &rid);
        }

        return rptnode;
}

static RDRecord *get_rdrecord_by_id(RPTEntry *rptentry, SaHpiEntryIdT id)
{
        GSList *rdrnode = NULL;
        RDRecord *rdrecord = NULL;

        if (!rptentry) {
                return NULL;
        }

        if (!rptentry->rdrlist) {
                /*DBG("Info: RDR repository is empty.");*/
                return NULL;
        }

        if (id == SAHPI_FIRST_ENTRY) {
                rdrecord = (RDRecord *) (rptentry->rdrlist->data);
        } else {
                rdrnode = (GSList *)g_hash_table_lookup(rptentry->rdrtable, &id);
                rdrecord = rdrnode ? (RDRecord *)rdrnode->data : NULL;
        }

        return rdrecord;
}

static GSList *get_rdrnode_by_id(RPTEntry *rptentry, SaHpiEntryIdT id)
{
        GSList *rdrnode = NULL;

        if (!rptentry) {
                return NULL;
        }

        if (!rptentry->rdrlist) {
                /*DBG("Info: RPT is empty.");*/
                return NULL;
        }

        if (id == SAHPI_FIRST_ENTRY) {
                rdrnode = (GSList *) (rptentry->rdrlist);
        } else {
                rdrnode = (GSList *)g_hash_table_lookup(rptentry->rdrtable, &id);
        }

        return rdrnode;
}

static int check_instrument_id(SaHpiRptEntryT *rptentry, SaHpiRdrT *rdr)
{
        int result = 0;
        static const SaHpiInstrumentIdT SENSOR_AGGREGATE_MAX = 0x0000010F;

        switch (rdr->RdrType) {
                case SAHPI_SENSOR_RDR:
                        if (rdr->RdrTypeUnion.SensorRec.Num >= SAHPI_STANDARD_SENSOR_MIN &&
                            rdr->RdrTypeUnion.SensorRec.Num <= SAHPI_STANDARD_SENSOR_MAX) {
                                if (rdr->RdrTypeUnion.SensorRec.Num > SENSOR_AGGREGATE_MAX) {
                                        result = -1;
                                } else if (rptentry->ResourceCapabilities &
                                           SAHPI_CAPABILITY_AGGREGATE_STATUS) {
                                        result = 0;
                                } else {
                                        result = -1;
                                }
                        } else {
                                result = 0;
                        }
                        break;
                default:
                        result = 0;
        }

        return result;
}

static void update_rptable(RPTable *table) {
        if (!table) {
                return;
        }
        oh_gettimeofday(&table->update_timestamp);
        table->update_count++;
}

/**
 * oh_get_rdr_uid
 * @type: type of rdr
 * @num: id number of the RDR unique withing the RDR type for that resource
 *
 * Helper function to derive the Record id of an rdr from its @type and @num
 *
 * Returns: a derived Record Id used to identify RDRs within Resources
 */
SaHpiEntryIdT oh_get_rdr_uid(SaHpiRdrTypeT type, SaHpiInstrumentIdT num)
{
        SaHpiEntryIdT uid;

        uid = ((SaHpiEntryIdT)type) << 16;
        uid = uid + (SaHpiEntryIdT)num;

        return uid;
}

SaHpiInstrumentIdT oh_get_rdr_num(SaHpiEntryIdT rdrid) {
        return rdrid & 0x0000ffff;
}

/**
 * oh_get_instrument_id
 * @rdr: RDR
 *
 * Helper function to derive the Instrument Id of the rdr
 *
 * Returns: an instrument id or zero
 */
SaHpiInstrumentIdT oh_get_instrument_id(const SaHpiRdrT *rdr)
{
        SaHpiInstrumentIdT num = 0;

        switch (rdr->RdrType) {
                case SAHPI_CTRL_RDR:
                        num = rdr->RdrTypeUnion.CtrlRec.Num;
                        break;
                case SAHPI_SENSOR_RDR:
                        num = rdr->RdrTypeUnion.SensorRec.Num;
                        break;
                case SAHPI_INVENTORY_RDR:
                        num = rdr->RdrTypeUnion.InventoryRec.IdrId;
                        break;
                case SAHPI_WATCHDOG_RDR:
                        num = rdr->RdrTypeUnion.WatchdogRec.WatchdogNum;
                        break;
                case SAHPI_ANNUNCIATOR_RDR:
                        num = rdr->RdrTypeUnion.AnnunciatorRec.AnnunciatorNum;
                        break;
               case SAHPI_DIMI_RDR:
                        num = rdr->RdrTypeUnion.DimiRec.DimiNum;
                        break;
                case SAHPI_FUMI_RDR:
                        num = rdr->RdrTypeUnion.FumiRec.Num;
                        break;
                default:
                        num = 0;
        }

        return num;
}


/**
 * General RPT calls
 **/

/**
 * oh_init_rpt
 * @table: Pointer to RPTable structure to be initialized.
 *
 *
 * Returns: SA_OK on success Or minus SA_OK on error.
 **/
SaErrorT oh_init_rpt(RPTable *table)
{
        if (!table) {
                return SA_ERR_HPI_INVALID_PARAMS;
        }

        table->update_timestamp = SAHPI_TIME_UNSPECIFIED;
        table->update_count = 0;
        table->rptlist = NULL;
        table->rptable = NULL;

        return SA_OK;
}

/**
 * oh_flush_rpt
 * @table: Pointer to the RPT to flush.
 *
 * Cleans RPT from all entries and RDRs and frees the memory
 * associated with them.
 *
 * Returns: SA_OK on success Or minus SA_OK on error.
 **/
SaErrorT oh_flush_rpt(RPTable *table)
{
        SaHpiRptEntryT *tmp_entry;

        while ((tmp_entry = oh_get_resource_by_id(table, SAHPI_FIRST_ENTRY)) != NULL) {
                oh_remove_resource(table, SAHPI_FIRST_ENTRY);
        }

        return SA_OK;
}

/**
 * rpt_diff
 * @cur_rpt: IN. Pointer to RPTable that represents the current state of resources
 * and rdrs.
 * @new_rpt: IN. Pointer to RPTable that contains rpt entries and rdrs just recently
 * discovered.
 * @res_new: OUT. List of new or changed rpt entries
 * @rdr_new: OUT. List of new or changed rdrs
 * @res_gone: OUT. List of old and not present resources.
 * @rdr_gone: OUT. List of old and not present rdrs.
 *
 * Extracts from current the resources and rdrs that are not found
 * in new and puts them in res_gone and rdr_gone. Also, puts in res_new and rdr_new
 * the resources and rdrs that are not already in current Or that are not identical
 * to the ones in current.
 *
 * Returns: SA_ERR_HPI_INVALID_PARAMS if any argument is NULL, otherwise SA_OK.
 **/
SaErrorT rpt_diff(RPTable *cur_rpt, RPTable *new_rpt,
                  GSList **res_new, GSList **rdr_new,
                  GSList **res_gone, GSList **rdr_gone) {

        SaHpiRptEntryT *res = NULL;

        if (!cur_rpt || !new_rpt ||
            !res_new || !rdr_new || !res_gone || !rdr_gone)
                return SA_ERR_HPI_INVALID_PARAMS;

        /* Look for absent resources and rdrs */
        for (res = oh_get_resource_by_id(cur_rpt, SAHPI_FIRST_ENTRY);
             res != NULL;
             res = oh_get_resource_next(cur_rpt, res->ResourceId)) {

                SaHpiRptEntryT *tmp_res = oh_get_resource_by_id(new_rpt, res->ResourceId);

                if (tmp_res == NULL) *res_gone = g_slist_append(*res_gone, (gpointer)res);
                else {
                        SaHpiRdrT *rdr = NULL;

                        for (rdr = oh_get_rdr_by_id(cur_rpt, res->ResourceId, SAHPI_FIRST_ENTRY);
                             rdr != NULL;
                             rdr = oh_get_rdr_next(cur_rpt, res->ResourceId, rdr->RecordId)) {

                                SaHpiRdrT *tmp_rdr =
                                        oh_get_rdr_by_id(new_rpt, res->ResourceId, rdr->RecordId);

                                if (tmp_rdr == NULL)
                                        *rdr_gone = g_slist_append(*rdr_gone, (gpointer)rdr);
                        }
                }
        }

        /* Look for new resources and rdrs*/
        for (res = oh_get_resource_by_id(new_rpt, SAHPI_FIRST_ENTRY);
             res != NULL;
             res = oh_get_resource_next(new_rpt, res->ResourceId)) {

                SaHpiRptEntryT *tmp_res = oh_get_resource_by_id(cur_rpt, res->ResourceId);
                SaHpiRdrT *rdr = NULL;
                if (tmp_res == NULL || memcmp(res, tmp_res, sizeof(SaHpiRptEntryT))) {
                        *res_new = g_slist_append(*res_new, (gpointer)res);
                }

                for (rdr = oh_get_rdr_by_id(new_rpt, res->ResourceId, SAHPI_FIRST_ENTRY);
                     rdr != NULL;
                     rdr = oh_get_rdr_next(new_rpt, res->ResourceId, rdr->RecordId)) {

                        SaHpiRdrT *tmp_rdr = NULL;

                        if (tmp_res != NULL)
                                tmp_rdr = oh_get_rdr_by_id(cur_rpt, res->ResourceId, rdr->RecordId);

                        if (tmp_rdr == NULL || memcmp(rdr, tmp_rdr, sizeof(SaHpiRdrT)))
                                *rdr_new = g_slist_append(*rdr_new, (gpointer)rdr);

                }
        }
        
        return SA_OK;
}

/**
 * oh_get_rpt_info
 * @table: pointer to RPT
 * @update_count: pointer of where to place the rpt's update count
 * @update_timestamp: pointer of where to place the rpt's update timestamp
 *
 * Returns: SA_OK on success Or minus SA_OK on error.
 **/
SaErrorT oh_get_rpt_info(RPTable *table,
                         SaHpiUint32T *update_count,
                         SaHpiTimeT *update_timestamp)
{
        if (!table || !update_count || !update_timestamp) {
                return SA_ERR_HPI_INVALID_PARAMS;
        }

        *update_count = table->update_count;
        *update_timestamp = table->update_timestamp;

        return SA_OK;
}

/**
 * Resource interface functions
 */

/**
 * oh_add_resource
 * @table: Pointer to the RPT to which the RPT entry will be added.
 * @entry: The RPT entry (resource) to be added to the RPT.
 * @data: Pointer to private data for storing along with the RPT entry.
 * @owndata: boolean flag. true (%KEEP_RPT_DATA) to tell the interface *not*
 * to free the data when the resource is removed. false (%FREE_RPT_DATA) to tell
 * the interface to free the data when the resource is removed.
 *
 * Add a RPT entry to the RPT along with some private data.
 * If an RPT entry with the same resource id exists int the RPT, it will be
 * overlayed with the new RPT entry. Also, this function will assign the
 * resource id as its entry id since it is expected to be unique for the table.
 * The update count and timestamp will not be updated if the entry being added
 * already existed in the table and was the same.
 *
 * Returns: SA_OK on success Or minus SA_OK on error. SA_ERR_HPI_INVALID_PARAMS will
 * be returned if @table is NULL, @entry is NULL, ResourceId in @entry equals
 * SAHPI_FIRST_ENTRY, ResourceId in @entry equals SAHPI_UNSPECIFIED_RESOURCE_ID,
 * or ResourceEntity in @entry has a malformed entity path (look at the
 * entity path utils documentation).
 **/
SaErrorT oh_add_resource(RPTable *table, SaHpiRptEntryT *entry, void *data, int owndata)
{
        RPTEntry *rptentry;
        int update_info = 0;

        if (!table) {
                return SA_ERR_HPI_INVALID_PARAMS;
        } else if (!entry) {
                return SA_ERR_HPI_INVALID_PARAMS;
        } else if (entry->ResourceId == SAHPI_FIRST_ENTRY) {
                return SA_ERR_HPI_INVALID_PARAMS;
        } else if (entry->ResourceId == SAHPI_UNSPECIFIED_RESOURCE_ID) {
                return SA_ERR_HPI_INVALID_PARAMS;
        } else if (!oh_valid_ep(&(entry->ResourceEntity))) {
                return SA_ERR_HPI_INVALID_PARAMS;
        }

        entry->EntryId = entry->ResourceId;
        /* Check to see if the entry is in the RPTable already */
        rptentry = get_rptentry_by_rid(table, entry->ResourceId);
        /* If not, create new RPTEntry */
        if (!rptentry) {
                rptentry = g_new0(RPTEntry, 1);
                if (!rptentry) {
                        return SA_ERR_HPI_OUT_OF_MEMORY;
                }
                update_info = 1; /* Have a new changed entry */
                /* Put new RPTEntry in RPTable */
                table->rptlist = g_slist_append(table->rptlist, (gpointer)rptentry);

                /* Add to rpt hash table */
                if (!table->rptable) /* Create hash table if it doesn't exist */
                        table->rptable = g_hash_table_new(g_int_hash, g_int_equal);

                rptentry->rpt_entry.EntryId = entry->ResourceId;
                g_hash_table_insert(table->rptable,
                                    &(rptentry->rpt_entry.EntryId),
                                    g_slist_last(table->rptlist));
        }
        /* Else, modify existing RPTEntry */
        if (rptentry->data && rptentry->data != data && !rptentry->owndata)
                g_free(rptentry->data);
        rptentry->data = data;
        rptentry->owndata = owndata;
        /* Check if we really have a new/changed entry */
        if (update_info || memcmp(entry, &(rptentry->rpt_entry), sizeof(SaHpiRptEntryT))) {
                update_info = 1;
                rptentry->rpt_entry = *entry;
        }

        if (update_info) update_rptable(table);

        return SA_OK;
}

/**
 * oh_remove_resource
 * @table: Pointer to the RPT from which an RPT entry will be removed.
 * @rid: Resource id of the RPT entry to be removed.
 *
 * Remove a resource from the RPT. If the @rid is
 * %SAHPI_FIRST_ENTRY, the first RPT entry in the table will be removed.
 * The void data will be freed if @owndata was false (%FREE_RPT_DATA) when adding
 * the resource, otherwise if @owndata was true (%KEEP_RPT_DATA) it will not be freed.
 *
 * Returns: SA_OK on success Or minus SA_OK on error.
 **/
SaErrorT oh_remove_resource(RPTable *table, SaHpiResourceIdT rid)
{
        RPTEntry *rptentry;

        rptentry = get_rptentry_by_rid(table, rid);
        if (!rptentry) {
                return SA_ERR_HPI_NOT_PRESENT;
        } else {
                SaHpiRdrT *tmp_rdr;
                /* Remove all RDRs for the resource first */
                while ((tmp_rdr = oh_get_rdr_by_id(table, rid, SAHPI_FIRST_ENTRY)) != NULL) {
                        oh_remove_rdr(table, rid, SAHPI_FIRST_ENTRY);
                }
                /* then remove the resource itself. */
                table->rptlist = g_slist_remove(table->rptlist, (gpointer)rptentry);
                if (!rptentry->owndata) g_free(rptentry->data);
                g_hash_table_remove(table->rptable, &(rptentry->rpt_entry.EntryId));
                g_free((gpointer)rptentry);
                if (!table->rptlist) {
                        g_hash_table_destroy(table->rptable);
                        table->rptable = NULL;
                }
        }

        update_rptable(table);

        return SA_OK;
}

/**
 * oh_get_resource_data
 * @table: Pointer to the RPT for looking up the RPT entry's private data.
 * @rid: Resource id of the RPT entry that holds the private data.
 *
 * Get the private data for a RPT entry.  If the @rid is
 * %SAHPI_FIRST_ENTRY, the first RPT entry's data in the table will be returned.
 *
 * Returns: A void pointer to the private data for the RPT entry requested, or NULL
 * if the RPT entry was not found or the table was a NULL pointer.
 **/
void *oh_get_resource_data(RPTable *table, SaHpiResourceIdT rid)
{

        RPTEntry *rptentry;

        rptentry = get_rptentry_by_rid(table, rid);
        if (!rptentry) {
                /*DBG("Warning: RPT entry not found. Returning NULL.");*/
                return NULL;
        }

        return rptentry->data;
}

/**
 * oh_get_resource_by_id
 * @table: Pointer to the RPT for looking up the RPT entry.
 * @rid: Resource id of the RPT entry to be looked up.
 *
 * Get a RPT entry from the RPT by using the resource id.
 * If @rid is %SAHPI_FIRST_ENTRY, the first RPT entry in the table will be returned.
 *
 * Returns:
 * Pointer to the RPT entry found or NULL if an RPT entry by that
 * id was not found or the table was a NULL pointer.
 **/
SaHpiRptEntryT *oh_get_resource_by_id(RPTable *table, SaHpiResourceIdT rid)
{
        RPTEntry *rptentry;

        rptentry = get_rptentry_by_rid(table, rid);
        if (!rptentry) {
                /*DBG("Warning: RPT entry not found. Returning NULL.");*/
                return NULL;
        }

        return &(rptentry->rpt_entry);
}

/**
 * oh_get_resource_by_ep
 * @table: Pointer to the RPT for looking up the RPT entry.
 * @ep: Entity path of the RPT entry to be looked up.
 *
 * Get a RPT entry from the RPT by using the entity path.
 *
 * Returns:
 * Pointer to the RPT entry found or NULL if an RPT entry by that
 * entity path was not found or the table was a NULL pointer.
 **/
SaHpiRptEntryT *oh_get_resource_by_ep(RPTable *table, SaHpiEntityPathT *ep)
{
        RPTEntry *rptentry = NULL;
        GSList *node = NULL;
        SaHpiResourceIdT rid = 0;

        if (!table) {
                return NULL;
        }
        /* Check the uid database first */
        rid = oh_uid_is_initialized() ? oh_uid_lookup(ep) : 0;
        if (rid > 0) {
                /* Found it in uid database */
                return oh_get_resource_by_id(table, rid);
        } else {
                DBG("Didn't find the EP in the Uid table so "
                    "looking manually in the RPTable");
        }

        for (node = table->rptlist; node != NULL; node = node->next) {
                rptentry = (RPTEntry *) node->data;
                if (oh_cmp_ep(&(rptentry->rpt_entry.ResourceEntity), ep))
                        break;
                else rptentry = NULL;
        }

        if (!rptentry) {
                /*DBG("Warning: RPT entry not found. Returning NULL.");*/
                return NULL;
        }

        return &(rptentry->rpt_entry);
}

/**
 * oh_get_resource_next
 * @table: Pointer to the RPT for looking up the RPT entry.
 * @rid_prev: Resource id of the RPT entry previous to the one being looked up.
 *
 * Get the RPT entry next to the specified RPT entry
 * from the RPT. If @rid_prev is %SAHPI_FIRST_ENTRY, the first RPT entry
 * in the table will be returned.
 *
 * Returns:
 * Pointer to the RPT entry found or NULL if the previous RPT entry by that
 * id was not found or the table was a NULL pointer.
 **/
SaHpiRptEntryT *oh_get_resource_next(RPTable *table, SaHpiResourceIdT rid_prev)
{
        RPTEntry *rptentry = NULL;
        GSList *rptnode = NULL;

        if (rid_prev == SAHPI_FIRST_ENTRY) {
                rptentry = get_rptentry_by_rid(table, rid_prev);
        } else {
                rptnode = get_rptnode_by_rid(table, rid_prev);
                if (rptnode && rptnode->next) {
                        rptentry = (RPTEntry *)rptnode->next->data;
                }
        }

        return rptentry ? &(rptentry->rpt_entry) : NULL;
}

/**
 * oh_get_rdr_update_count
 * @table: Pointer to the RPT for looking up the RPT entry.
 * @rid: Resource id of the RPT entry to be looked up.
 * @update_count: pointer of where to place the rdr update count
 *
 * Get a RDR Repository Update Counter for the resource using the resource id.
 *
 * Returns:
 * Returns: SA_OK on success.
 * Will return SA_ERR_HPI_INVALID_PARAMS if update_count is NULL.
 * Will return SA_ERR_HPI_NOT_PRESENT if there is no resource
 * with specified rid in RPT.
 **/
SaErrorT oh_get_rdr_update_count(RPTable *table,
                                 SaHpiResourceIdT rid,
                                 SaHpiUint32T *update_count)
{
    RPTEntry *rptentry = get_rptentry_by_rid(table, rid);
    if ( !rptentry ) {
        return SA_ERR_HPI_NOT_PRESENT;
    }
    if ( !update_count ) {
        return SA_ERR_HPI_INVALID_PARAMS;
    }
    *update_count = rptentry->update_count;
    return SA_OK;
}

/**
 * RDR interface functions
 */

/**
 * oh_add_rdr
 * @table: Pointer to RPT table containig the RPT entry to which the RDR will belong.
 * @rid: Id of the RPT entry that will own the RDR to be added.
 * @rdr: RDR to be added to an RPT entry's RDR repository.
 * @data: Pointer to private data belonging to the RDR that is being added.
 * @owndata: boolean flag. true (%KEEP_RPT_DATA) to tell the interface *not*
 * to free the data when the rdr is removed. false (%FREE_RPT_DATA) to tell
 * the interface to free the data when the rdr is removed.
 *
 * Add an RDR to a RPT entry's RDR repository.
 * If an RDR is found with the same record id as the one being added, the RDR being
 * added will overlay the existing one. Also, a unique record id will be assigned
 * to it based on the RDR type and its type's numeric id.
 * All rdr interface funtions, except oh_add_rdr() will act in the context of
 * the first RPT entry in the table, if @rid is %SAHPI_FIRST_ENTRY.
 *
 * Returns: SA_OK on success Or minus SA_OK on error. Will return
 * SA_ERR_HPI_INVALID_PARAMS if instrument id is invalid. An invalid
 * intrument id for a sensor is in the range of 0x100-0x1FF. An aggregate type
 * of sensor can have its instrument id in the range of 0x100-0x10F, but
 * the resource must have the aggregate sensor capabilitiy bit set.
 **/
SaErrorT oh_add_rdr(RPTable *table, SaHpiResourceIdT rid, SaHpiRdrT *rdr, void *data, int owndata)
{
        RPTEntry *rptentry;
        RDRecord *rdrecord;
        SaHpiInstrumentIdT instr_id;

        if (!rdr) {
                return SA_ERR_HPI_INVALID_PARAMS;
        }

        rptentry = get_rptentry_by_rid(table, rid);
        if (!rptentry){
                return SA_ERR_HPI_NOT_PRESENT;
        }

        if (check_instrument_id(&(rptentry->rpt_entry), rdr)) {
                CRIT("Invalid instrument id found in RDR.");
                return SA_ERR_HPI_INVALID_PARAMS;
        }

        instr_id = oh_get_instrument_id(rdr);

        /* Form correct RecordId. */
        rdr->RecordId = oh_get_rdr_uid(rdr->RdrType, instr_id);
        /* Check if record exists */
        rdrecord = get_rdrecord_by_id(rptentry, rdr->RecordId);
        /* If not, create new rdr */
        if (!rdrecord) {
                rdrecord = g_new0(RDRecord, 1);
                if (!rdrecord) {
                        return SA_ERR_HPI_OUT_OF_MEMORY;
                }
                /* Put new rdrecord in rdr repository */
                rptentry->rdrlist = g_slist_append(rptentry->rdrlist, (gpointer)rdrecord);
                /* Create rdr hash table if first rdr here */
                if (!rptentry->rdrtable)
                        rptentry->rdrtable = g_hash_table_new(g_int_hash, g_int_equal);

                rdrecord->rdr.RecordId = rdr->RecordId;
                g_hash_table_insert(rptentry->rdrtable,
                                    &(rdrecord->rdr.RecordId),
                                    g_slist_last(rptentry->rdrlist));
        }
        /* Else, modify existing rdrecord */
        if (rdrecord->data && rdrecord->data != data && !rdrecord->owndata)
                g_free(rdrecord->data);
        rdrecord->data = data;
        rdrecord->owndata = owndata;
        rdrecord->rdr = *rdr;

        ++rptentry->update_count;

        return SA_OK;
}

/**
 * oh_remove_rdr
 * @table: Pointer to RPT table containig the RPT entry from which the RDR will
 * be removed.
 * @rid: Id of the RPT entry from which the RDR will be removed.
 * @rdrid: Record id of the RDR to remove.
 *
 *
 * Remove an RDR from a RPT entry's RDR repository.
 * If @rdrid is %SAHPI_FIRST_ENTRY, the first RDR in the repository will be removed.
 * If @owndata was set to false (%FREE_RPT_DATA) on the rdr when it was added,
 * the data will be freed, otherwise if it was set to true (%KEEP_RPT_DATA),
 * it will not be freed.
 * All rdr interface funtions, except oh_add_rdr() will act in the context of
 * the first RPT entry in the table, if @rid is %SAHPI_FIRST_ENTRY.
 *
 * Returns: SA_OK on success Or minus SA_OK on error.
 **/
SaErrorT oh_remove_rdr(RPTable *table, SaHpiResourceIdT rid, SaHpiEntryIdT rdrid)
{
        RPTEntry *rptentry;
        RDRecord *rdrecord;

        rptentry = get_rptentry_by_rid(table, rid);
        if (!rptentry) {
                return SA_ERR_HPI_NOT_PRESENT;
        }

        rdrecord = get_rdrecord_by_id(rptentry, rdrid);
        if (!rdrecord) {
                return SA_ERR_HPI_NOT_PRESENT;
        } else {
                rptentry->rdrlist = g_slist_remove(rptentry->rdrlist, (gpointer)rdrecord);
                if (!rdrecord->owndata) g_free(rdrecord->data);
                g_hash_table_remove(rptentry->rdrtable, &(rdrecord->rdr.RecordId));
                g_free((gpointer)rdrecord);
                if (!rptentry->rdrlist) {
                        g_hash_table_destroy(rptentry->rdrtable);
                        rptentry->rdrtable = NULL;
                }
                ++rptentry->update_count;
        }

        return SA_OK;
}

/**
 * oh_get_rdr_data
 * @table: Pointer to RPT table containig the RPT entry from which the RDR's data
 * will be read.
 * @rid: Id of the RPT entry from which the RDR's data will be read.
 * @rdrid: Record id of the RDR to read data from.
 *
 *
 * Get the private data associated to an RDR.
 * If @rdrid is %SAHPI_FIRST_ENTRY, the first RDR's data in the repository will be returned.
 * All rdr interface funtions, except oh_add_rdr() will act in the context of
 * the first RPT entry in the table, if @rid is %SAHPI_FIRST_ENTRY.
 *
 * Returns: A void pointer to the RDR data, or NULL if no data for that RDR was found or
 * the table pointer is NULL.
 **/
void *oh_get_rdr_data(RPTable *table, SaHpiResourceIdT rid, SaHpiEntryIdT rdrid)
{
        RPTEntry *rptentry;
        RDRecord *rdrecord;

        rptentry = get_rptentry_by_rid(table, rid);
        if (!rptentry) {
                return NULL; /* No resource found by that id */
        }

        rdrecord = get_rdrecord_by_id(rptentry, rdrid);
        if (!rdrecord) {
                /*DBG("Warning: RDR not found. Returning NULL.");*/
                return NULL;
        }

        return rdrecord->data;
}

/**
 * oh_get_rdr_by_id
 * @table: Pointer to RPT table containig the RPT entry tha has the RDR
 * being looked up.
 * @rid: Id of the RPT entry containing the RDR being looked up.
 * @rdrid: Record id of the RDR being looked up.
 *
 * Get a reference to an RDR by its record id.
 * If @rdrid is %SAHPI_FIRST_ENTRY, the first RDR in the repository will be returned.
 * All rdr interface funtions, except oh_add_rdr() will act in the context of
 * the first RPT entry in the table, if @rid is %SAHPI_FIRST_ENTRY.
 *
 * Returns:
 * Reference to the RDR looked up or NULL if no RDR was found.
 **/
SaHpiRdrT *oh_get_rdr_by_id(RPTable *table, SaHpiResourceIdT rid, SaHpiEntryIdT rdrid)
{
        RPTEntry *rptentry;
        RDRecord *rdrecord;

        rptentry = get_rptentry_by_rid(table, rid);
        if (!rptentry) {
                return NULL; /* No resource found by that id */
        }

        rdrecord = get_rdrecord_by_id(rptentry, rdrid);
        if (!rdrecord) {
                /*DBG("Warning: RDR not found. Returning NULL.");*/
                return NULL;
        }

        return &(rdrecord->rdr);
}

/**
 * oh_get_rdr_by_type
 * @table: Pointer to RPT table containig the RPT entry tha has the RDR
 * being looked up.
 * @rid: Id of the RPT entry containing the RDR being looked up.
 * @type: RDR Type of the RDR being looked up.
 * @num: RDR id within the RDR type for the specified RPT entry.
 *
 * Get a reference to an RDR by its type and number.
 * All rdr interface funtions, except oh_add_rdr() will act in the context of
 * the first RPT entry in the table, if @rid is %SAHPI_FIRST_ENTRY.
 *
 * Returns:
 * Reference to the RDR looked up or NULL if no RDR was found.
 **/
SaHpiRdrT *oh_get_rdr_by_type(RPTable *table, SaHpiResourceIdT rid,
                              SaHpiRdrTypeT type, SaHpiInstrumentIdT num)
{
        RPTEntry *rptentry = NULL;
        RDRecord *rdrecord = NULL;
        SaHpiEntryIdT rdr_uid;

        rptentry = get_rptentry_by_rid(table, rid);
        if (!rptentry) {
                return NULL; /* No resource found by that id */
        }
        
        /* Get rdr_uid from type/num combination */
        rdr_uid = oh_get_rdr_uid(type, num);
        rdrecord = get_rdrecord_by_id(rptentry, rdr_uid);
        if (!rdrecord) {
                /*DBG("Warning: RDR not found. Returning NULL.");*/
                return NULL;
        }

        return &(rdrecord->rdr);        
}

/**
 * oh_get_rdr_next
 * @table: Pointer to the RPT containing the RPT entry to look up the RDR in.
 * @rid: Id of the RPT entry containing the RDR being looked up.
 * @rdrid_prev: Record id of the RDR previous to the one being looked up, relative
 * to the specified RPT entry.
 *
 * Get the RDR next to the specified RDR in the specified
 * RPT entry's repository. If @rdrid_prev is %SAHPI_FIRST_ENTRY, the first RDR
 * in the repository will be returned.
 * All rdr interface funtions, except oh_add_rdr() will act in the context of
 * the first RPT entry in the table, if @rid is %SAHPI_FIRST_ENTRY.
 *
 * Returns:
 * Pointer to the RDR found or NULL if the previous RDR by that
 * id was not found. If the @rdrid_prev was %SAHPI_FIRST_ENTRY, the first RDR in the list
 * will be returned.
 **/
SaHpiRdrT *oh_get_rdr_next(RPTable *table, SaHpiResourceIdT rid, SaHpiEntryIdT rdrid_prev)
{
        RPTEntry *rptentry = NULL;
        RDRecord *rdrecord = NULL;
        GSList *rdrnode = NULL;

        rptentry = get_rptentry_by_rid(table, rid);
        if (!rptentry) {
                return NULL; /* No resource found by that id */
        }

        if (rdrid_prev == SAHPI_FIRST_ENTRY) {
                rdrecord = get_rdrecord_by_id(rptentry, rdrid_prev);
        } else {
                rdrnode = get_rdrnode_by_id(rptentry, rdrid_prev);
                if (rdrnode && rdrnode->next) {
                        rdrecord = (RDRecord *)rdrnode->next->data;
                }
        }

        return rdrecord ? &(rdrecord->rdr) : NULL;
}

SaHpiRdrT *oh_get_rdr_by_type_first(RPTable *table, SaHpiResourceIdT rid,
                                    SaHpiRdrTypeT type)
{
        RPTEntry *rptentry = NULL;
        RDRecord *rdrecord = NULL;
        GSList *node = NULL;

        rptentry = get_rptentry_by_rid(table, rid);
        if (!rptentry) {
                return NULL; /* No resource found by that id */
        }
        
        /* Get first RDR matching the type */
        for (node = rptentry->rdrlist; node; node = node->next) {
                RDRecord *temp = (RDRecord *)node->data;
                if (temp->rdr.RdrType == type) {
                        rdrecord = temp;
                        break;
                }
        }                
        if (!rdrecord) return NULL;

        return &(rdrecord->rdr);
}

SaHpiRdrT *oh_get_rdr_by_type_next(RPTable *table, SaHpiResourceIdT rid,
                                   SaHpiRdrTypeT type, SaHpiInstrumentIdT num)
{
        RPTEntry *rptentry = NULL;
        RDRecord *rdrecord = NULL;
        GSList *node = NULL;

        rptentry = get_rptentry_by_rid(table, rid);
        if (!rptentry) {
                return NULL; /* No resource found by that id */
        }
        
        /* Get rdr_uid from type/num combination */
        node = get_rdrnode_by_id(rptentry, oh_get_rdr_uid(type, num));
        if (!node) return NULL;
        
        for (node = node->next; node; node = node->next) {
                RDRecord *temp = (RDRecord *)node->data;
                if (temp->rdr.RdrType == type) {
                        rdrecord = temp;
                        break;
                }
        }
        if (!rdrecord) return NULL;

        return &(rdrecord->rdr);
}