libamxd  6.4.1
Data Model Manager
amxd_object_priv.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
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 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 
55 #ifndef _GNU_SOURCE
56 #define _GNU_SOURCE
57 #endif
58 
59 #include <stdlib.h>
60 #include <string.h>
61 #include <ctype.h>
62 
63 #include <amxc/amxc.h>
64 #include <amxp/amxp.h>
65 
66 #include <amxd/amxd_types.h>
67 #include <amxd/amxd_object.h>
69 #include <amxd/amxd_object_event.h>
70 #include <amxd/amxd_dm.h>
71 #include <amxd/amxd_dm_functions.h>
72 
73 #include "amxd_priv.h"
74 #include "amxd_dm_priv.h"
75 #include "amxd_object_priv.h"
76 #include "amxd_assert.h"
77 
78 static
79 void amxd_dm_llist_clean(amxc_llist_t* const llist,
80  amxc_llist_it_delete_t func) {
81  amxc_llist_for_each(it, llist) {
82  func(it);
83  }
84 }
85 
86 static
87 void amxd_object_free_object_it(amxc_llist_it_t* it) {
88  amxd_object_t* object = amxc_llist_it_get_data(it, amxd_object_t, it);
89  amxd_object_delete(&object);
90 }
91 
92 static
93 void amxd_object_free_param_it(amxc_llist_it_t* it) {
94  amxd_param_t* param = amxc_llist_it_get_data(it, amxd_param_t, it);
95  amxd_object_t* owner = amxd_param_get_owner(param);
96  amxd_dm_invoke_action(owner, param, action_param_destroy, NULL, NULL);
97 }
98 
99 static
100 void amxd_object_free_cb_it(amxc_llist_it_t* it) {
101  amxd_dm_cb_t* cb = amxc_llist_it_get_data(it, amxd_dm_cb_t, it);
102  free(cb);
103 }
104 
105 void amxd_object_free_func_it(amxc_llist_it_t* it) {
106  amxd_function_t* func = amxc_llist_it_get_data(it, amxd_function_t, it);
107  amxd_function_delete(&func);
108 }
109 
110 const char* amxd_object_template_get_alias(amxc_var_t* templ_params,
111  amxc_var_t* values) {
112  amxc_var_t* alias_def = GET_FIELD(templ_params, "Alias");
113  amxc_var_t* alias_val = GET_FIELD(values, "Alias");
114  const char* alias_name = NULL;
115 
116  when_null(alias_def, exit);
117  when_true(!amxc_var_constcast(bool,
118  GET_FIELD(alias_def, "attributes.key")),
119  exit);
120  when_true(!amxc_var_constcast(bool,
121  GET_FIELD(alias_def, "attributes.unique")),
122  exit);
123 
124  alias_name = amxc_var_constcast(cstring_t, alias_val);
125 
126 exit:
127  return alias_name;
128 }
129 
131  const amxd_object_type_t type,
132  const char* name,
133  amxc_var_t* templ_params,
134  amxc_var_t* values) {
136 
137  amxd_object_init_base(object, type);
138 
140  const char* alias = amxd_object_template_get_alias(templ_params, values);
141  if((alias != NULL) && (alias[0] != 0)) {
142  object->name = strdup(alias);
143  when_null(object->name, exit);
144  }
145  }
146  if(object->name == NULL) {
147  if((name != NULL) && (name[0] != 0)) {
148  object->name = strdup(name);
149  when_null(object->name, exit);
150  }
151  }
152  amxd_dm_set_derived_from(object);
153 
154  retval = amxd_status_ok;
155 
156 exit:
157  return retval;
158 }
159 
163 }
164 
165 void amxd_object_clean(amxd_object_t* const object) {
168  amxc_llist_it_take(&object->it);
169 
170  amxc_llist_clean(&object->functions, amxd_object_free_func_it);
171  amxc_llist_clean(&object->cb_fns, amxd_object_free_cb_it);
172  amxc_array_clean(&object->mib_names, NULL);
173  amxc_var_clean(&object->events);
174 
175  if(amxc_llist_it_is_in_list(&object->derived_from)) {
176  amxd_object_t* super = NULL;
177  super = amxc_container_of(object->derived_from.llist,
179  derived_objects);
180  amxc_llist_it_take(&object->derived_from);
181  if(amxc_llist_is_empty(&super->derived_objects) &&
182  ( super->it.llist == NULL)) {
183  amxd_object_free(&super);
184  }
185  }
186 
187  free(object->index_name);
188  object->index_name = NULL;
189  free(object->name);
190  object->name = NULL;
191 }
192 
193 void amxd_fetch_item(amxc_var_t* const full_data,
194  const char* item,
195  amxc_var_t* const data) {
196  amxc_var_t* item_data = amxc_var_get_path(full_data,
197  item,
198  AMXC_VAR_FLAG_DEFAULT);
199 
200  if(amxc_var_type_of(item_data) == AMXC_VAR_ID_LIST) {
201  const amxc_llist_t* src_list = amxc_var_constcast(amxc_llist_t, item_data);
202  amxc_var_set_type(data, AMXC_VAR_ID_LIST);
203  amxc_llist_for_each(it, src_list) {
204  amxc_llist_append(&data->data.vl, it);
205  }
206 
207  } else if(amxc_var_type_of(item_data) == AMXC_VAR_ID_HTABLE) {
208  const amxc_htable_t* src_table = amxc_var_constcast(amxc_htable_t, item_data);
209  amxc_htable_it_t* it = NULL;
210  amxc_var_set_type(data, AMXC_VAR_ID_HTABLE);
211  it = amxc_htable_get_first(src_table);
212  while(it) {
213  const char* key = amxc_htable_it_get_key(it);
214  amxc_htable_insert(&data->data.vm, key, it);
215  it = amxc_htable_get_first(src_table);
216  }
217  }
218 }
219 
221  const char* name) {
222  amxd_function_t* func = NULL;
223 
224  amxc_llist_for_each(it, (&object->functions)) {
225  const char* func_name = NULL;
226  func = amxc_llist_it_get_data(it, amxd_function_t, it);
227  func_name = amxd_function_get_name(func);
228  if(strcmp(func_name, name) == 0) {
229  break;
230  }
231  func = NULL;
232  }
233 
234  return func;
235 }
236 
238  const amxd_object_t* const src) {
240  bool dst_is_inst = (amxd_object_get_type(dst) == amxd_object_instance);
241  bool dst_is_templ = (amxd_object_get_type(dst) == amxd_object_template);
242  bool src_is_mib = (amxd_object_get_type(src) == amxd_object_mib);
243  amxc_llist_for_each(it, (&src->parameters)) {
244  amxd_param_t* base = amxc_llist_it_get_data(it, amxd_param_t, it);
245  amxd_param_t* derived = NULL;
246  uint32_t attrs = amxd_param_get_attrs(base);
247  if(!src_is_mib &&
248  dst_is_inst &&
250  continue;
251  }
252  if(amxd_param_is_attr_set(base, amxd_pattr_counter) && !src_is_mib) {
253  amxd_object_t* counted = amxd_object_get_child(dst, (char*) base->priv);
254  retval = amxd_object_set_counter(counted, amxd_param_get_name(base));
255  when_failed(retval, exit);
256  derived = amxd_object_get_param_def(dst, amxd_param_get_name(base));
257  } else {
258  retval = amxd_param_copy(&derived, base);
259  when_failed(retval, exit);
260  if(src_is_mib) {
261  amxc_llist_iterate(cb_it, (&base->cb_fns)) {
262  amxd_dm_cb_t* mib_cb = amxc_llist_it_get_data(cb_it, amxd_dm_cb_t, it);
263  if(mib_cb->reason == action_param_destroy) {
264  continue;
265  }
266  amxd_param_add_action_cb(derived,
267  mib_cb->reason,
268  mib_cb->fn,
269  mib_cb->priv);
270  }
271  if(dst_is_inst || dst_is_templ) {
272  if(!IS_BIT_SET(attrs, amxd_pattr_instance) &&
273  !IS_BIT_SET(attrs, amxd_pattr_template)) {
275  }
276  if(dst_is_inst) {
278  amxd_param_delete(&derived);
279  continue;
280  }
281  }
282  }
283  }
284  amxc_llist_append(&dst->parameters, &derived->it);
285  }
286  amxd_param_set_attrs(derived, attrs, true);
287  }
288 
289  retval = amxd_status_ok;
290 exit:
291  return retval;
292 }
293 
295  const amxd_object_t* const src) {
297  bool dst_is_inst = (amxd_object_get_type(dst) == amxd_object_instance);
298  bool dst_is_templ = (amxd_object_get_type(dst) == amxd_object_template);
299  bool src_is_mib = (amxd_object_get_type(src) == amxd_object_mib);
300  amxc_llist_for_each(it, (&src->functions)) {
301  amxd_function_t* base = amxc_llist_it_get_data(it, amxd_function_t, it);
302  amxd_function_t* derived = NULL;
303  if(!src_is_mib &&
304  dst_is_inst &&
306  continue;
307  }
308  retval = amxd_function_copy(&derived, base);
309  when_failed(retval, exit);
310  if(src_is_mib && (dst_is_inst || dst_is_templ)) {
311  uint32_t attrs = amxd_function_get_attrs(base);
312  if(!IS_BIT_SET(attrs, amxd_fattr_instance) &&
313  !IS_BIT_SET(attrs, amxd_fattr_template)) {
315  }
316  if(dst_is_inst) {
318  amxd_function_delete(&derived);
319  continue;
320  }
321  }
322  }
323  amxc_llist_append(&dst->functions, &derived->it);
324  }
325 
326  retval = amxd_status_ok;
327 exit:
328  return retval;
329 }
330 
332  const amxd_object_t* const src) {
333  amxc_var_for_each(data, &src->events) {
334  amxc_var_t* new_data = NULL;
335  const char* name = amxc_var_key(data);
336 
337  amxc_var_new(&new_data);
338  amxc_var_copy(new_data, data);
339  amxd_object_add_event_ext(dst, name, new_data);
340  }
341 
342  return amxd_status_ok;
343 }
344 
346  const amxd_object_t* const src) {
348  amxc_array_it_t* ait = amxc_array_get_first(&src->mib_names);
349  while(ait != NULL) {
350  const char* name = (const char*) amxc_array_it_get_data(ait);
351  if(name != NULL) {
352  amxc_array_append_data(&dst->mib_names, (void*) name);
353  }
354  ait = amxc_array_it_get_next(ait);
355  }
356 
357  retval = amxd_status_ok;
358  return retval;
359 }
360 
362  const amxd_object_t* const src) {
364  amxc_llist_for_each(it, (&src->objects)) {
365  amxd_object_t* base = amxc_llist_it_get_data(it, amxd_object_t, it);
366  amxd_object_t* derived = NULL;
367  retval = amxd_object_derive(&derived, base, dst);
368  when_failed(retval, exit);
369  retval = amxd_object_copy_params(derived, base);
370  when_failed(retval, exit);
371  retval = amxd_object_copy_mib_names(derived, base);
372  when_failed(retval, exit);
373  }
374 
375  retval = amxd_status_ok;
376 exit:
377  return retval;
378 }
379 
381  amxd_object_t* const base,
382  amxd_object_t* const parent) {
384  when_null(object, exit);
385 
386  *object = (amxd_object_t*) calloc(1, sizeof(amxd_object_t));
387  when_null((*object), exit);
388 
389  (*object)->attr = base->attr;
390  (*object)->type = base->type;
391  amxc_llist_append(&base->derived_objects, &(*object)->derived_from);
392  amxc_llist_append(&parent->objects, &(*object)->it);
393 
394  retval = amxd_object_copy_children((*object), base);
395  when_failed(retval, exit);
396 
397 exit:
398  if((retval != amxd_status_ok) &&
399  (object != NULL) &&
400  (*object != NULL)) {
401  amxd_object_clean(*object);
402  free(*object);
403  *object = NULL;
404  }
405  return retval;
406 }
#define IS_BIT_SET(b, f)
Definition: amxd_common.h:66
Ambiorix Data Model API header file.
amxd_status_t amxd_dm_invoke_action(amxd_object_t *object, amxd_param_t *param, amxd_action_t reason, const amxc_var_t *const args, amxc_var_t *const retval)
Definition: amxd_dm.c:591
void PRIVATE amxd_object_init_base(amxd_object_t *const object, const amxd_object_type_t type)
Definition: amxd_dm_priv.c:472
void PRIVATE amxd_dm_set_derived_from(amxd_object_t *const object)
Definition: amxd_dm_priv.c:602
Ambiorix Data Model API header file.
amxd_status_t amxd_object_set_counter(amxd_object_t *const object, const char *name)
Definition: amxd_object.c:362
Ambiorix Data Model API header file.
const char * amxd_object_template_get_alias(amxc_var_t *templ_params, amxc_var_t *values)
static void amxd_object_free_object_it(amxc_llist_it_t *it)
amxd_function_t * amxd_object_get_self_func(const amxd_object_t *const object, const char *name)
amxd_status_t amxd_object_derive(amxd_object_t **object, amxd_object_t *const base, amxd_object_t *const parent)
amxd_status_t amxd_object_init(amxd_object_t *const object, const amxd_object_type_t type, const char *name, amxc_var_t *templ_params, amxc_var_t *values)
void amxd_object_destroy_handlers(amxd_object_t *const object)
static void amxd_dm_llist_clean(amxc_llist_t *const llist, amxc_llist_it_delete_t func)
void amxd_object_free_func_it(amxc_llist_it_t *it)
static void amxd_object_free_cb_it(amxc_llist_it_t *it)
static void amxd_object_free_param_it(amxc_llist_it_t *it)
amxd_status_t amxd_object_copy_funcs(amxd_object_t *const dst, const amxd_object_t *const src)
amxd_status_t amxd_object_copy_events(amxd_object_t *const dst, const amxd_object_t *const src)
amxd_status_t amxd_object_copy_mib_names(amxd_object_t *const dst, const amxd_object_t *const src)
void amxd_fetch_item(amxc_var_t *const full_data, const char *item, amxc_var_t *const data)
amxd_status_t amxd_object_copy_children(amxd_object_t *const dst, const amxd_object_t *const src)
amxd_status_t amxd_object_copy_params(amxd_object_t *const dst, const amxd_object_t *const src)
void amxd_object_clean(amxd_object_t *const object)
amxd_status_t amxd_param_copy(amxd_param_t **dest, const amxd_param_t *const source)
bool amxd_param_is_attr_set(const amxd_param_t *const param, const amxd_pattr_id_t attr)
amxd_status_t amxd_param_set_attrs(amxd_param_t *param, const uint32_t bitmask, bool enable)
const char * amxd_param_get_name(const amxd_param_t *const param)
amxd_status_t amxd_param_set_attr(amxd_param_t *param, const amxd_pattr_id_t attr, const bool enable)
uint32_t amxd_param_get_attrs(const amxd_param_t *const param)
amxd_object_t * amxd_param_get_owner(const amxd_param_t *const param)
amxd_status_t amxd_param_delete(amxd_param_t **param)
amxd_status_t amxd_param_add_action_cb(amxd_param_t *const param, const amxd_action_t reason, amxd_action_fn_t fn, void *priv)
#define GET_FIELD(var, field)
Definition: amxd_priv.h:76
@ amxd_pattr_template
Definition: amxd_types.h:355
@ amxd_pattr_instance
Definition: amxd_types.h:356
@ amxd_pattr_counter
Definition: amxd_types.h:361
@ action_param_destroy
Definition: amxd_types.h:116
enum _amxd_status amxd_status_t
@ amxd_status_ok
Definition: amxd_types.h:78
@ amxd_status_unknown_error
Definition: amxd_types.h:79
enum _amxd_object_type amxd_object_type_t
The different object types.
@ amxd_fattr_template
Definition: amxd_types.h:311
@ amxd_fattr_instance
Definition: amxd_types.h:312
@ amxd_object_template
Definition: amxd_types.h:183
@ amxd_object_mib
Definition: amxd_types.h:188
@ amxd_object_instance
Definition: amxd_types.h:186
amxd_status_t amxd_object_add_event_ext(amxd_object_t *const object, const char *event_name, amxc_var_t *event_data)
Adds an event definition to the object.
amxd_object_t * amxd_object_get_child(const amxd_object_t *object, const char *name)
Get a child of the object.
amxd_param_t * amxd_object_get_param_def(const amxd_object_t *const object, const char *name)
Gets a parameter definition from an object.
void amxd_object_delete(amxd_object_t **object)
Invokes the destroy handler(s) of the object.
static amxd_object_type_t amxd_object_get_type(const amxd_object_t *const object)
Returns the object type.
Definition: amxd_object.h:586
void amxd_object_free(amxd_object_t **object)
Data model object destructor function.
Definition: amxd_object.c:153
amxd_status_t amxd_function_set_attr(amxd_function_t *func, const amxd_fattr_id_t attr, const bool enable)
Sets or unsets a method attribute.
const char * amxd_function_get_name(const amxd_function_t *const func)
Get the name of a method.
void amxd_function_delete(amxd_function_t **func)
Data model RPC method destructor function.
amxd_status_t amxd_function_copy(amxd_function_t **dest, const amxd_function_t *const source)
Data model RPC method copy constructor function.
bool amxd_function_is_attr_set(const amxd_function_t *const func, const amxd_fattr_id_t attr)
Checks if a method attribute is set.
uint32_t amxd_function_get_attrs(const amxd_function_t *const func)
Gets the set attributes of a RPC method.
amxd_action_t reason
Definition: amxd_types.h:160
void * priv
Definition: amxd_types.h:161
amxd_action_fn_t fn
Definition: amxd_types.h:159
RPC method structure.
Definition: amxd_types.h:341
amxc_llist_it_t it
Definition: amxd_types.h:342
amxc_llist_t derived_objects
Definition: amxd_types.h:248
amxc_llist_it_t derived_from
Definition: amxd_types.h:249
amxd_object_type_t type
Definition: amxd_types.h:236
amxd_object_attr_t attr
Definition: amxd_types.h:237
amxc_llist_t functions
Definition: amxd_types.h:245
amxc_llist_t cb_fns
Definition: amxd_types.h:253
amxc_llist_t objects
Definition: amxd_types.h:243
amxc_array_t mib_names
Definition: amxd_types.h:254
char * name
Definition: amxd_types.h:238
amxc_llist_it_t it
Definition: amxd_types.h:230
amxc_llist_t instances
Definition: amxd_types.h:244
amxc_var_t events
Definition: amxd_types.h:256
char * index_name
Definition: amxd_types.h:239
amxc_llist_t parameters
Definition: amxd_types.h:246
amxc_llist_it_t it
Definition: amxd_types.h:387
amxc_llist_t cb_fns
Definition: amxd_types.h:391