1 /**************************************************************************//**
3 * Implementation of EVEL functions relating to Other.
8 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *****************************************************************************/
27 #include "evel_internal.h"
29 /**************************************************************************//**
30 * Create a new Other event.
32 * @note The mandatory fields on the Other must be supplied to this factory
33 * function and are immutable once set. Optional fields have explicit
34 * setter functions, but again values may only be set once so that the
35 * Other has immutable properties.
36 * @returns pointer to the newly manufactured ::EVENT_OTHER. If the event is
37 * not used (i.e. posted) it must be released using ::evel_free_other.
38 * @retval NULL Failed to create the event.
39 *****************************************************************************/
40 EVENT_OTHER * evel_new_other()
42 EVENT_OTHER * other = NULL;
45 /***************************************************************************/
46 /* Check preconditions. */
47 /***************************************************************************/
49 /***************************************************************************/
50 /* Allocate the Other. */
51 /***************************************************************************/
52 other = malloc(sizeof(EVENT_OTHER));
55 log_error_state("Out of memory");
58 memset(other, 0, sizeof(EVENT_OTHER));
59 EVEL_DEBUG("New Other is at %lp", other);
61 /***************************************************************************/
62 /* Initialize the header & the Other fields. Optional string values are */
63 /* uninitialized (NULL). */
64 /***************************************************************************/
65 evel_init_header(&other->header);
66 other->header.event_domain = EVEL_DOMAIN_OTHER;
67 dlist_initialize(&other->other_fields);
74 /**************************************************************************//**
75 * Set the Event Type property of the Other.
77 * @note The property is treated as immutable: it is only valid to call
78 * the setter once. However, we don't assert if the caller tries to
79 * overwrite, just ignoring the update instead.
81 * @param other Pointer to the Other.
82 * @param type The Event Type to be set. ASCIIZ string. The caller
83 * does not need to preserve the value once the function
85 *****************************************************************************/
86 void evel_other_type_set(EVENT_OTHER * other,
87 const char * const type)
91 /***************************************************************************/
92 /* Check preconditions and call evel_header_type_set. */
93 /***************************************************************************/
94 assert(other != NULL);
95 assert(other->header.event_domain == EVEL_DOMAIN_OTHER);
96 evel_header_type_set(&other->header, type);
101 /**************************************************************************//**
102 * Add a field name/value pair to the Other.
104 * The name and value are null delimited ASCII strings. The library takes
105 * a copy so the caller does not have to preserve values after the function
108 * @param other Pointer to the Other.
109 * @param name ASCIIZ string with the field's name. The caller does not
110 * need to preserve the value once the function returns.
111 * @param value ASCIIZ string with the field's value. The caller does not
112 * need to preserve the value once the function returns.
113 *****************************************************************************/
114 void evel_other_field_add(EVENT_OTHER * other, char * name, char * value)
116 OTHER_FIELD * other_field = NULL;
119 /***************************************************************************/
120 /* Check preconditions. */
121 /***************************************************************************/
122 assert(other != NULL);
123 assert(other->header.event_domain == EVEL_DOMAIN_OTHER);
124 assert(name != NULL);
125 assert(value != NULL);
127 EVEL_DEBUG("Adding name=%s value=%s", name, value);
128 other_field = malloc(sizeof(OTHER_FIELD));
129 assert(other_field != NULL);
130 memset(other_field, 0, sizeof(OTHER_FIELD));
131 other_field->name = strdup(name);
132 other_field->value = strdup(value);
133 assert(other_field->name != NULL);
134 assert(other_field->value != NULL);
136 dlist_push_last(&other->other_fields, other_field);
141 /**************************************************************************//**
142 * Encode the Other in JSON according to AT&T's schema for the event type.
144 * @param jbuf Pointer to the ::EVEL_JSON_BUFFER to encode into.
145 * @param event Pointer to the ::EVENT_HEADER to encode.
146 *****************************************************************************/
147 void evel_json_encode_other(EVEL_JSON_BUFFER * jbuf,
150 OTHER_FIELD * other_field = NULL;
151 DLIST_ITEM * other_field_item = NULL;
155 /***************************************************************************/
156 /* Check preconditions. */
157 /***************************************************************************/
158 assert(event != NULL);
159 assert(event->header.event_domain == EVEL_DOMAIN_OTHER);
161 evel_json_encode_header(jbuf, &event->header);
162 evel_json_open_named_list(jbuf, "otherFields");
163 other_field_item = dlist_get_first(&event->other_fields);
164 while (other_field_item != NULL)
166 other_field = (OTHER_FIELD *) other_field_item->item;
167 assert(other_field != NULL);
169 evel_json_open_object(jbuf);
170 evel_enc_kv_string(jbuf, "name", other_field->name);
171 evel_enc_kv_string(jbuf, "value", other_field->value);
172 evel_json_close_object(jbuf);
173 other_field_item = dlist_get_next(other_field_item);
175 evel_json_close_list(jbuf);
180 /**************************************************************************//**
183 * Free off the Other supplied. Will free all the contained allocated memory.
185 * @note It does not free the Other itself, since that may be part of a
187 *****************************************************************************/
188 void evel_free_other(EVENT_OTHER * event)
190 OTHER_FIELD * other_field = NULL;
194 /***************************************************************************/
195 /* Check preconditions. As an internal API we don't allow freeing NULL */
196 /* events as we do on the public API. */
197 /***************************************************************************/
198 assert(event != NULL);
199 assert(event->header.event_domain == EVEL_DOMAIN_OTHER);
201 /***************************************************************************/
202 /* Free all internal strings then the header itself. */
203 /***************************************************************************/
204 other_field = dlist_pop_last(&event->other_fields);
205 while (other_field != NULL)
207 EVEL_DEBUG("Freeing Other Field (%s, %s)",
210 free(other_field->name);
211 free(other_field->value);
213 other_field = dlist_pop_last(&event->other_fields);
215 evel_free_header(&event->header);