1 /**************************************************************************//**
3 * Implementation of EVEL functions relating to Other.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement: This product includes
18 * software developed by the AT&T.
19 * 4. Neither the name of AT&T nor the names of its contributors may be used to
20 * endorse or promote products derived from this software without specific
21 * prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY AT&T INTELLECTUAL PROPERTY ''AS IS'' AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL AT&T INTELLECTUAL PROPERTY BE LIABLE FOR ANY
27 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *****************************************************************************/
40 #include "evel_internal.h"
42 /**************************************************************************//**
43 * Create a new Other event.
45 * @note The mandatory fields on the Other must be supplied to this factory
46 * function and are immutable once set. Optional fields have explicit
47 * setter functions, but again values may only be set once so that the
48 * Other has immutable properties.
49 * @returns pointer to the newly manufactured ::EVENT_OTHER. If the event is
50 * not used (i.e. posted) it must be released using ::evel_free_other.
51 * @retval NULL Failed to create the event.
52 *****************************************************************************/
53 EVENT_OTHER * evel_new_other()
55 EVENT_OTHER * other = NULL;
58 /***************************************************************************/
59 /* Check preconditions. */
60 /***************************************************************************/
62 /***************************************************************************/
63 /* Allocate the Other. */
64 /***************************************************************************/
65 other = malloc(sizeof(EVENT_OTHER));
68 log_error_state("Out of memory");
71 memset(other, 0, sizeof(EVENT_OTHER));
72 EVEL_DEBUG("New Other is at %lp", other);
74 /***************************************************************************/
75 /* Initialize the header & the Other fields. Optional string values are */
76 /* uninitialized (NULL). */
77 /***************************************************************************/
78 evel_init_header(&other->header);
79 other->header.event_domain = EVEL_DOMAIN_OTHER;
80 dlist_initialize(&other->other_fields);
87 /**************************************************************************//**
88 * Set the Event Type property of the Other.
90 * @note The property is treated as immutable: it is only valid to call
91 * the setter once. However, we don't assert if the caller tries to
92 * overwrite, just ignoring the update instead.
94 * @param other Pointer to the Other.
95 * @param type The Event Type to be set. ASCIIZ string. The caller
96 * does not need to preserve the value once the function
98 *****************************************************************************/
99 void evel_other_type_set(EVENT_OTHER * other,
100 const char * const type)
104 /***************************************************************************/
105 /* Check preconditions and call evel_header_type_set. */
106 /***************************************************************************/
107 assert(other != NULL);
108 assert(other->header.event_domain == EVEL_DOMAIN_OTHER);
109 evel_header_type_set(&other->header, type);
114 /**************************************************************************//**
115 * Add a field name/value pair to the Other.
117 * The name and value are null delimited ASCII strings. The library takes
118 * a copy so the caller does not have to preserve values after the function
121 * @param other Pointer to the Other.
122 * @param name ASCIIZ string with the field's name. The caller does not
123 * need to preserve the value once the function returns.
124 * @param value ASCIIZ string with the field's value. The caller does not
125 * need to preserve the value once the function returns.
126 *****************************************************************************/
127 void evel_other_field_add(EVENT_OTHER * other, char * name, char * value)
129 OTHER_FIELD * other_field = NULL;
132 /***************************************************************************/
133 /* Check preconditions. */
134 /***************************************************************************/
135 assert(other != NULL);
136 assert(other->header.event_domain == EVEL_DOMAIN_OTHER);
137 assert(name != NULL);
138 assert(value != NULL);
140 EVEL_DEBUG("Adding name=%s value=%s", name, value);
141 other_field = malloc(sizeof(OTHER_FIELD));
142 assert(other_field != NULL);
143 memset(other_field, 0, sizeof(OTHER_FIELD));
144 other_field->name = strdup(name);
145 other_field->value = strdup(value);
146 assert(other_field->name != NULL);
147 assert(other_field->value != NULL);
149 dlist_push_last(&other->other_fields, other_field);
154 /**************************************************************************//**
155 * Encode the Other in JSON according to AT&T's schema for the event type.
157 * @param jbuf Pointer to the ::EVEL_JSON_BUFFER to encode into.
158 * @param event Pointer to the ::EVENT_HEADER to encode.
159 *****************************************************************************/
160 void evel_json_encode_other(EVEL_JSON_BUFFER * jbuf,
163 OTHER_FIELD * other_field = NULL;
164 DLIST_ITEM * other_field_item = NULL;
168 /***************************************************************************/
169 /* Check preconditions. */
170 /***************************************************************************/
171 assert(event != NULL);
172 assert(event->header.event_domain == EVEL_DOMAIN_OTHER);
174 evel_json_encode_header(jbuf, &event->header);
175 evel_json_open_named_list(jbuf, "otherFields");
176 other_field_item = dlist_get_first(&event->other_fields);
177 while (other_field_item != NULL)
179 other_field = (OTHER_FIELD *) other_field_item->item;
180 assert(other_field != NULL);
182 evel_json_open_object(jbuf);
183 evel_enc_kv_string(jbuf, "name", other_field->name);
184 evel_enc_kv_string(jbuf, "value", other_field->value);
185 evel_json_close_object(jbuf);
186 other_field_item = dlist_get_next(other_field_item);
188 evel_json_close_list(jbuf);
193 /**************************************************************************//**
196 * Free off the Other supplied. Will free all the contained allocated memory.
198 * @note It does not free the Other itself, since that may be part of a
200 *****************************************************************************/
201 void evel_free_other(EVENT_OTHER * event)
203 OTHER_FIELD * other_field = NULL;
207 /***************************************************************************/
208 /* Check preconditions. As an internal API we don't allow freeing NULL */
209 /* events as we do on the public API. */
210 /***************************************************************************/
211 assert(event != NULL);
212 assert(event->header.event_domain == EVEL_DOMAIN_OTHER);
214 /***************************************************************************/
215 /* Free all internal strings then the header itself. */
216 /***************************************************************************/
217 other_field = dlist_pop_last(&event->other_fields);
218 while (other_field != NULL)
220 EVEL_DEBUG("Freeing Other Field (%s, %s)",
223 free(other_field->name);
224 free(other_field->value);
226 other_field = dlist_pop_last(&event->other_fields);
228 evel_free_header(&event->header);