GCOV Execution Analysis for el_utils.c

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

Exec CodeLine #
 
Source:el_utils.c
1
 
Object:t/el/el_utils.bb
2
 
/*      -*- linux-c -*-
3
 
 *
4
 
 * (C) Copyright IBM Corp. 2003, 2004
5
 
 * Copyright (c) 2003 by Intel Corp.
6
 
 *
7
 
 * This program is distributed in the hope that it will be useful,
8
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  This
10
 
 * file and program are licensed under a BSD style license.  See
11
 
 * the Copying file included with the OpenHPI distribution for
12
 
 * full licensing terms.
13
 
 *
14
 
 * Authors:
15
 
 *      David Ashley < address removed >
16
 
 *      Renier Morales < address removed >
17
 
 */
18
 
19
 
#include <stdio.h>
20
 
#include <stdlib.h>
21
 
#include <glib.h>
22
 
#include <fcntl.h>
23
 
#include <string.h>
24
 
#include <unistd.h>
25
 
#include <time.h>
26
 
27
 
#include <SaHpi.h>
28
 
#include <oh_utils.h>
29
 
30
 
/* allocate and initialize an EL */
31
 
oh_el *oh_el_create(SaHpiUint32T size)
32
25 
{
33
25 
        oh_el *el;
34
 
35
25 
        el = (oh_el *) g_malloc0(sizeof(oh_el));
36
25 
        if (el != NULL) {
37
25 
                el->enabled = TRUE;
38
25 
                el->overflow = FALSE;
39
25 
                el->gentimestamp = TRUE;
40
25 
                el->lastUpdate = SAHPI_TIME_UNSPECIFIED;
41
25 
                el->offset = 0;
42
25 
                el->maxsize = size;
43
25 
                el->nextId = SAHPI_OLDEST_ENTRY + 1; // always start at 1
44
25 
                el->elentries = NULL;
45
 
        }
46
25 
        return el;
47
 
}
48
 
49
 
50
 
/* close and free all memory associated with an EL */
51
 
SaErrorT oh_el_close(oh_el *el)
52
24 
{
53
 
54
24 
        if (el == NULL) {
55
1 
                return SA_ERR_HPI_INVALID_PARAMS;
56
 
        }
57
 
58
23 
        oh_el_clear(el);
59
23 
        free(el);
60
23 
        return SA_OK;
61
 
}
62
 
63
 
64
 
/* append a new entry to the EL */
65
 
SaErrorT oh_el_append(oh_el *el, SaHpiEventT *event, SaHpiRdrT *rdr,
66
 
                      SaHpiRptEntryT *res)
67
80 
{
68
80 
        oh_el_entry *entry;
69
80 
        time_t tt1;
70
80 
        GList *temp;
71
 
72
 
        /* check for valid el params and state */
73
80 
        if (el == NULL || event == NULL) {
74
3 
                return SA_ERR_HPI_INVALID_PARAMS;
75
 
        }
76
77 
        if ((el->enabled == FALSE) && (event->EventType != SAHPI_ET_USER)) {
77
0 
                return SA_ERR_HPI_INVALID_REQUEST;
78
 
        }
79
 
80
 
        /* alloc the new entry */
81
77 
        entry = (oh_el_entry *) g_malloc0(sizeof(oh_el_entry));
82
77 
        if (entry == NULL) {
83
0 
                el->overflow = TRUE;
84
0 
                return SA_ERR_HPI_OUT_OF_SPACE;
85
 
        }
86
77 
        if (rdr != NULL) {
87
35 
                memcpy(&(entry->rdr), rdr, sizeof(SaHpiRdrT));
88
 
        }
89
77 
        if (res != NULL) {
90
35 
                memcpy(&(entry->res), res, sizeof(SaHpiRptEntryT));
91
 
        }
92
 
93
 
        /* if necessary, wrap the el entries */
94
77 
        if (el->maxsize != OH_EL_MAX_SIZE && g_list_length(el->elentries) == el->maxsize) {
95
0 
                gpointer tempdata;
96
0 
                temp = g_list_first(el->elentries);
97
0 
                tempdata = temp->data;
98
0 
                el->elentries = g_list_remove(el->elentries, temp->data);
99
0 
                g_free(tempdata);
100
0 
                el->overflow = SAHPI_TRUE;
101
 
        }
102
 
103
 
        /* append the new entry */
104
77 
        entry->event.EntryId = el->nextId;
105
77 
        el->nextId++;
106
77 
        if (el->gentimestamp) {
107
77 
                time(&tt1);
108
77 
                el->lastUpdate = (SaHpiTimeT) (tt1 * 1000000000) + el->offset;
109
 
        } else {
110
0 
                el->lastUpdate = event->Timestamp;
111
 
        }
112
77 
        entry->event.Timestamp = el->lastUpdate;
113
77 
        memcpy(&(entry->event.Event), event, sizeof(SaHpiEventT));
114
77 
        el->elentries = g_list_append(el->elentries, entry);
115
77 
        return SA_OK;
116
 
}
117
 
118
 
119
 
/* prepend a new entry to the EL */
120
 
SaErrorT oh_el_prepend(oh_el *el, SaHpiEventT *event, SaHpiRdrT *rdr,
121
 
                       SaHpiRptEntryT *res)
122
3 
{
123
3 
        oh_el_entry *entry, *tmpentry;
124
3 
        SaHpiEventLogEntryT *tmplog;
125
3 
        time_t tt1;
126
3 
        GList *ellist;
127
 
128
 
        /* check for valid el params and state */
129
3 
        if (el == NULL || event == NULL) {
130
3 
                return SA_ERR_HPI_INVALID_PARAMS;
131
 
        }
132
0 
        if (el->enabled == FALSE) {
133
0 
                return SA_ERR_HPI_INVALID_REQUEST;
134
 
        }
135
 
136
 
        /* see if el is full */
137
0 
        if (el->maxsize != OH_EL_MAX_SIZE && g_list_length(el->elentries) == el->maxsize) {
138
0 
                return SA_ERR_HPI_OUT_OF_SPACE;
139
 
        }
140
 
141
 
        /* alloc the new entry */
142
0 
        entry = (oh_el_entry *) g_malloc0(sizeof(oh_el_entry));
143
0 
        if (entry == NULL) {
144
0 
                el->overflow = TRUE;
145
0 
                return SA_ERR_HPI_OUT_OF_SPACE;
146
 
        }
147
0 
        if (rdr != NULL) {
148
0 
                memcpy(&(entry->rdr), rdr, sizeof(SaHpiRdrT));
149
 
        }
150
0 
        if (res != NULL) {
151
0 
                memcpy(&(entry->res), res, sizeof(SaHpiRptEntryT));
152
 
        }
153
 
154
 
        /* since we are adding entries in reverse order we have to renumber
155
 
         * existing entries
156
 
         */
157
0 
        ellist = g_list_first(el->elentries);
158
0 
        while (ellist != NULL) {
159
0 
                tmpentry = (oh_el_entry *) ellist->data;
160
0 
                tmplog = (SaHpiEventLogEntryT *) &(tmpentry->event);
161
0 
                tmplog->EntryId++;
162
0 
                ellist = g_list_next(ellist);
163
 
        }
164
 
165
 
        /* prepend the new entry */
166
0 
        entry->event.EntryId = 1;
167
0 
        el->nextId++;
168
0 
        if (el->gentimestamp) {
169
0 
                time(&tt1);
170
0 
                el->lastUpdate = (SaHpiTimeT) (tt1 * 1000000000) + el->offset;
171
 
        } else {
172
0 
                el->lastUpdate = event->Timestamp;
173
 
        }
174
0 
        entry->event.Timestamp = el->lastUpdate;
175
0 
        memcpy(&(entry->event.Event), event, sizeof(SaHpiEventT));
176
0 
        el->elentries = g_list_prepend(el->elentries, entry);
177
0 
        return SA_OK;
178
 
}
179
 
180
 
181
 
/* clear all EL entries */
182
 
SaErrorT oh_el_clear(oh_el *el)
183
31 
{
184
31 
        GList *temp;
185
 
186
31 
        if (el == NULL) {
187
1 
                return SA_ERR_HPI_INVALID_PARAMS;
188
 
        }
189
 
        
190
 
        /* free the list data elements */
191
30 
        temp = g_list_first(el->elentries);
192
102 
        while (temp != NULL) {
193
72 
                g_free(temp->data);
194
72 
                temp = g_list_next(temp);
195
 
        }
196
 
        /* free the list nodes */
197
30 
        g_list_free(el->elentries);
198
 
        /* reset the control structure */
199
30 
        el->overflow = FALSE;
200
30 
        el->lastUpdate = SAHPI_TIME_UNSPECIFIED;
201
30 
        el->nextId = SAHPI_OLDEST_ENTRY + 1; // always start at 1
202
30 
        el->elentries = NULL;
203
 
        
204
30 
        return SA_OK;
205
 
}
206
 
207
 
208
 
/* get an EL entry */
209
 
SaErrorT oh_el_get(oh_el *el, SaHpiEventLogEntryIdT entryid, SaHpiEventLogEntryIdT *prev,
210
 
                    SaHpiEventLogEntryIdT *next, oh_el_entry **entry)
211
51 
{
212
51 
        oh_el_entry *myentry;
213
51 
        GList *ellist;
214
51 
        SaHpiEventLogEntryIdT srchentryid, firstid, lastid;
215
 
216
51 
        if (el == NULL || prev == NULL || next == NULL || entry == NULL) {
217
3 
                return SA_ERR_HPI_INVALID_PARAMS;
218
 
        }
219
48 
        if (g_list_length(el->elentries) == 0) {
220
1 
                return SA_ERR_HPI_NOT_PRESENT;
221
 
        }
222
 
223
 
        /* get the first and last entry ids for possible translation */
224
47 
        ellist = g_list_last(el->elentries);
225
47 
        myentry = (oh_el_entry *)ellist->data;
226
47 
        lastid = myentry->event.EntryId;
227
47 
        ellist = g_list_first(el->elentries);
228
47 
        myentry = (oh_el_entry *)ellist->data;
229
47 
        firstid = myentry->event.EntryId;
230
47 
        if (entryid == SAHPI_NEWEST_ENTRY) {
231
0 
                srchentryid = lastid;
232
 
        }
233
47 
        else if (entryid == SAHPI_OLDEST_ENTRY) {
234
1 
                srchentryid = firstid;
235
 
        }
236
 
        else {
237
46 
                srchentryid = entryid;
238
 
        }
239
 
240
47 
        ellist = g_list_first(el->elentries);
241
62 
        while (ellist != NULL) {
242
61 
                myentry = (oh_el_entry *) ellist->data;
243
61 
                if (srchentryid == myentry->event.EntryId) {
244
46 
                        *entry = myentry;
245
 
                        /* is this the first entry? */
246
46 
                        if (myentry->event.EntryId == firstid) {
247
42 
                                *prev = SAHPI_NO_MORE_ENTRIES;
248
 
                        }
249
 
                        else {
250
4 
                                *prev = myentry->event.EntryId - 1;
251
 
                        }
252
 
                        /* is this the last entry? */
253
46 
                        if (myentry->event.EntryId == lastid) {
254
6 
                                *next = SAHPI_NO_MORE_ENTRIES;
255
 
                        }
256
 
                        else {
257
40 
                                *next = myentry->event.EntryId + 1;
258
 
                        }
259
46 
                        return SA_OK;
260
 
                }
261
15 
                else if (entryid < myentry->event.EntryId) {
262
0 
                        return SA_ERR_HPI_NOT_PRESENT;
263
 
                }
264
15 
                ellist = g_list_next(ellist);
265
 
        }
266
1 
        return SA_ERR_HPI_NOT_PRESENT;
267
 
}
268
 
269
 
270
 
/* get EL info */
271
 
SaErrorT oh_el_info(oh_el *el, SaHpiEventLogInfoT *info)
272
4 
{
273
4 
        time_t tt1;
274
 
275
4 
        if (el == NULL || info == NULL) {
276
2 
                return SA_ERR_HPI_INVALID_PARAMS;
277
 
        }
278
 
279
2 
        info->Entries = g_list_length(el->elentries);
280
2 
        info->Size = el->maxsize;
281
2 
        info->UpdateTimestamp = el->lastUpdate;
282
2 
        time(&tt1);
283
2 
        info->CurrentTime = (SaHpiTimeT) (tt1 * 1000000000) + el->offset;
284
2 
        info->Enabled = el->enabled;
285
2 
        info->OverflowFlag = el->overflow;
286
2 
	info->OverflowResetable = SAHPI_TRUE;
287
2 
        info->OverflowAction = SAHPI_EL_OVERFLOW_OVERWRITE;
288
2 
        return SA_OK;
289
 
}
290
 
291
 
292
 
/* reset EL overflowflag */
293
 
SaErrorT oh_el_overflowreset(oh_el *el)
294
1 
{
295
 
     
296
 
297
1 
        if (el == NULL)
298
1 
                return SA_ERR_HPI_INVALID_PARAMS;
299
 
		
300
0 
	el->overflow = SAHPI_FALSE;
301
0 
        return SA_OK;
302
 
}
303
 
304
 
305
 
/* write a EL entry list to a file */
306
 
SaErrorT oh_el_map_to_file(oh_el *el, char *filename)
307
5 
{
308
5 
        int file;
309
5 
        GList *ellist;
310
 
311
5 
        if (el == NULL || filename == NULL) {
312
2 
                return SA_ERR_HPI_INVALID_PARAMS;
313
 
        }
314
 
315
3 
        file = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0660 );
316
3 
        if (file < 0) {
317
0 
                dbg("EL file '%s' could not be opened", filename);
318
0 
                return SA_ERR_HPI_ERROR;
319
 
        }
320
 
321
3 
        ellist = g_list_first(el->elentries);
322
23 
        while (ellist != NULL) {
323
20 
                write(file, (void *)ellist->data, sizeof(oh_el_entry));
324
20 
                ellist = g_list_next(ellist);
325
 
        }
326
 
327
3 
        if(close(file) != 0) {
328
0 
                dbg("Couldn't close file '%s'.", filename);
329
0 
                return SA_ERR_HPI_ERROR;
330
 
        }
331
 
332
3 
        return SA_OK;
333
 
}
334
 
335
 
336
 
/* read a EL entry list from a file */
337
 
SaErrorT oh_el_map_from_file(oh_el *el, char *filename)
338
10 
{
339
10 
        int file;
340
10 
        oh_el_entry entry;
341
10 
        SaErrorT retc;
342
 
343
 
        /* check el params and state */
344
10 
        if (el == NULL || filename == NULL) {
345
2 
                return SA_ERR_HPI_INVALID_PARAMS;
346
 
        }
347
8 
        if (el->enabled == FALSE) {
348
1 
                return SA_ERR_HPI_INVALID_REQUEST;
349
 
        }
350
 
351
7 
        file = open(filename, O_RDONLY);
352
7 
        if (file < 0) {
353
1 
                dbg("EL file '%s' could not be opened", filename);
354
1 
                return SA_ERR_HPI_ERROR;
355
 
        }
356
 
357
6 
        oh_el_clear(el); // ensure list is empty
358
41 
        while (read(file, &entry, sizeof(oh_el_entry)) == sizeof(oh_el_entry)) {
359
35 
                el->nextId = entry.event.EntryId;
360
 
                /* Need a way to preserve the original entry's timestamp
361
 
                 * if that is of use. -- RM
362
 
                 */
363
35 
                retc = oh_el_append(el, &(entry.event.Event), &(entry.rdr),
364
 
                                    &(entry.res));
365
35 
                if (retc) {
366
0 
                        close(file);
367
0 
                        return retc;
368
 
                }
369
 
        }
370
 
371
6 
        if(close(file) != 0) {
372
0 
                dbg("Couldn't close file '%s'.", filename);
373
0 
                return SA_ERR_HPI_ERROR;
374
 
        }
375
 
376
6 
        return SA_OK;
377
 
}
378
 
379
 
380
 
/* set the EL timestamp offset */
381
 
SaErrorT oh_el_timeset(oh_el *el, SaHpiTimeT timestamp)
382
3 
{
383
3 
        if (el == NULL || timestamp == SAHPI_TIME_UNSPECIFIED) {
384
2 
                return SA_ERR_HPI_INVALID_PARAMS;
385
1 
        } else if (timestamp > SAHPI_TIME_MAX_RELATIVE) {
386
 
                /* We accept absolute timestamp here to be
387
 
                   compliant with the spec. But we set it to zero
388
 
                   anyway because we use time() to get the current
389
 
                   time (absolute) to stamp new entries with.
390
 
                */
391
0 
                timestamp = 0;
392
 
        }
393
 
394
1 
        el->offset = timestamp;
395
1 
        return SA_OK;
396
 
}
397
 
398
 
399
 
/* set the timestamp generate flag */
400
 
SaErrorT oh_el_setgentimestampflag(oh_el *el, SaHpiBoolT flag)
401
1 
{
402
1 
        if (el == NULL) {
403
1 
                return SA_ERR_HPI_INVALID_PARAMS;
404
 
        }
405
 
406
0 
        el->gentimestamp = flag;
407
0 
        return SA_OK;
408
 
}
409