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