libamxd  6.4.1
Data Model Manager
amxd_object.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 <stdio.h>
61 #include <string.h>
62 #include <ctype.h>
63 
64 #include <amxc/amxc.h>
65 #include <amxp/amxp.h>
66 
67 #include <amxd/amxd_types.h>
68 #include <amxd/amxd_object.h>
70 #include <amxd/amxd_dm.h>
71 
72 #include "amxd_priv.h"
73 #include "amxd_dm_priv.h"
74 #include "amxd_object_priv.h"
75 #include "amxd_assert.h"
76 
77 static void amxd_object_set_attributes(amxd_object_t* const object,
78  const uint32_t attr) {
79  object->attr.read_only = IS_BIT_SET(attr, amxd_oattr_read_only);
80  object->attr.persistent = IS_BIT_SET(attr, amxd_oattr_persistent);
81  object->attr.priv = IS_BIT_SET(attr, amxd_oattr_private);
82  object->attr.prot = IS_BIT_SET(attr, amxd_oattr_protected);
83  object->attr.locked = IS_BIT_SET(attr, amxd_oattr_locked);
84 }
85 
87  return (type == amxd_object_singleton ||
88  type == amxd_object_template ||
89  type == amxd_object_mib);
90 }
91 
93  amxd_object_t* const counted) {
94  bool retval = false;
95 
96  when_null(counter, exit);
97  when_null(counted, exit);
98 
99  when_true(amxd_object_get_type(counted) != amxd_object_template, exit);
100 
101  retval = true;
102 
103 exit:
104  return retval;
105 }
106 
108  amxd_object_t* const counted,
109  const char* name) {
110  amxd_param_t* param = NULL;
111  amxc_var_t count;
113  amxd_dm_t* dm = amxd_object_get_dm(container);
114 
115  amxc_var_init(&count);
116  when_str_empty(name, exit);
117  param = amxd_object_get_param_def(container, name);
118  if(param != NULL) {
121  param = NULL;
122  }
123  } else {
124  amxd_param_new(&param, name, AMXC_VAR_ID_UINT32);
125  status = amxd_object_add_param(container, param);
126  when_failed(status, exit);
127  param->priv = amxd_object_get_rel_path(counted, container, AMXD_OBJECT_NAMED);
131  dm);
132  }
133 
134  when_null(param, exit);
137 
138  amxc_var_set(uint32_t, &count, amxd_object_get_instance_count(counted));
139  status = amxd_param_set_value(param, &count);
140  when_failed(status, exit);
141 
143 
144 exit:
145  if(status != amxd_status_ok) {
146  amxd_param_free(&param);
147  param = NULL;
148  }
149  amxc_var_clean(&count);
150  return param;
151 }
152 
154  amxd_object_type_t object_type;
155  amxd_param_t* counter = NULL;
156 
157  when_null(object, exit);
158  when_null((*object), exit);
159 
160  object_type = amxd_object_get_type(*object);
161  if((object_type == amxd_object_instance) || (object_type == amxd_object_template)) {
163  }
164 
165  amxd_object_clean((*object));
166 
167  if(amxc_llist_is_empty(&(*object)->derived_objects)) {
168  free((*object));
169  }
170 
171  *object = NULL;
172 
173  when_null(counter, exit);
174 
175  if(object_type == amxd_object_instance) {
177  } else if(object_type == amxd_object_template) {
179  }
180 
181 exit:
182  return;
183 }
184 
186  const amxd_object_type_t type,
187  const char* name) {
189  when_null(object, exit);
190  when_true_status(!amxd_object_type_is_valid(type),
191  exit,
192  retval = amxd_status_invalid_type);
193  when_true_status(!amxd_name_is_valid(name),
194  exit,
195  retval = amxd_status_invalid_name);
196 
197  *object = (amxd_object_t*) calloc(1, sizeof(amxd_object_t));
198  when_null((*object), exit);
199 
200  retval = amxd_object_init(*object, type, name, NULL, NULL);
201  when_failed(retval, exit);
202 
203 exit:
204  return retval;
205 }
206 
208  amxd_object_t* const child) {
210 
211  when_null(parent, exit);
212  when_null(child, exit);
213  when_true(parent == child, exit);
214 
215  when_true_status(child->it.llist == &parent->objects,
216  exit,
217  retval = amxd_status_ok);
218  when_not_null(child->it.llist, exit);
219 
220  when_true_status(!amxd_object_type_is_valid(child->type),
221  exit,
222  retval = amxd_status_invalid_type);
223  when_true_status(child->type == amxd_object_mib,
224  exit,
225  retval = amxd_status_invalid_type);
226  when_not_null_status(amxd_object_get_child(parent, child->name),
227  exit,
228  retval = amxd_status_duplicate);
229 
230  when_failed(amxc_llist_append(&parent->objects, &child->it), exit);
231  amxd_dm_event("dm:object-added", child, NULL, false);
232 
233  retval = amxd_status_ok;
234 
235 exit:
236  return retval;
237 }
238 
239 const char* amxd_object_get_name(const amxd_object_t* const object,
240  const uint32_t flags) {
241  const char* name = NULL;
242 
243  when_null(object, exit);
244  if(object->type == amxd_object_instance) {
245  if((flags & AMXD_OBJECT_INDEXED) == AMXD_OBJECT_INDEXED) {
246  name = object->index_name;
247  } else {
248  name = object->name;
249  }
250  } else {
251  name = object->name;
252  if((name == NULL) && (object->derived_from.llist != NULL)) {
253  amxd_object_t* super = amxc_container_of(object->derived_from.llist,
255  derived_objects);
256 
258  }
259  }
260 
261 exit:
262  return name;
263 }
264 
265 uint32_t amxd_object_get_index(const amxd_object_t* const object) {
266  return object == NULL ? 0 : object->index;
267 }
268 
270  const amxd_oattr_id_t attr,
271  const bool enable) {
273  uint32_t flags = 0;
274  when_null(object, exit);
275 
276  when_true_status(object->type == amxd_object_mib,
277  exit,
278  retval = amxd_status_invalid_type);
279 
280  when_true_status(attr < 0 || attr > amxd_oattr_max,
281  exit,
282  retval = amxd_status_invalid_attr);
283 
284  when_true(amxd_object_is_attr_set(object, amxd_oattr_locked), exit);
285 
286  flags = amxd_object_get_attrs(object);
287 
288  if(enable) {
289  flags |= SET_BIT(attr);
290  } else {
291  flags &= ~SET_BIT(attr);
292  }
293 
294  amxd_object_set_attributes(object, flags);
295  retval = amxd_status_ok;
296 
297 exit:
298  return retval;
299 }
300 
302  const uint32_t bitmask,
303  bool enable) {
305  uint32_t flags = 0;
306  uint32_t all = 0;
307 
308  when_null(object, exit);
309  when_true_status(object->type == amxd_object_mib,
310  exit,
311  retval = amxd_status_invalid_type);
312 
313  for(int i = 0; i <= amxd_oattr_max; i++) {
314  all |= SET_BIT(i);
315  }
316 
317  when_true_status(bitmask > all, exit, retval = amxd_status_invalid_attr);
318 
319  flags = amxd_object_get_attrs(object);
320 
321  if(enable) {
322  flags |= bitmask;
323  } else {
324  flags &= ~bitmask;
325  }
326 
327  amxd_object_set_attributes(object, flags);
328  retval = amxd_status_ok;
329 
330 exit:
331  return retval;
332 }
333 
334 uint32_t amxd_object_get_attrs(const amxd_object_t* const object) {
335  uint32_t attributes = 0;
336  when_null(object, exit);
337 
338  attributes |= object->attr.read_only << amxd_oattr_read_only;
339  attributes |= object->attr.persistent << amxd_oattr_persistent;
340  attributes |= object->attr.priv << amxd_oattr_private;
341  attributes |= object->attr.prot << amxd_oattr_protected;
342  attributes |= object->attr.locked << amxd_oattr_locked;
343 
344 exit:
345  return attributes;
346 }
347 
348 bool amxd_object_is_attr_set(const amxd_object_t* const object,
349  const amxd_oattr_id_t attr) {
350  uint32_t flags = 0;
351  bool retval = false;
352  when_null(object, exit);
353  when_true(attr < 0 || attr > amxd_oattr_max, exit);
354 
355  flags = amxd_object_get_attrs(object);
356  retval = (flags & (1 << attr)) != 0 ? true : false;
357 
358 exit:
359  return retval;
360 }
361 
363  const char* name) {
365  amxd_param_t* param = NULL;
366  amxd_object_t* container = NULL;
367 
368  container = amxd_object_get_parent(object);
369  when_true(!amxd_is_valid_counter(container, object), exit);
370  param = amxd_create_counter_param(container, object, name);
371  when_null(param, exit);
372 
373  if((amxd_object_get_type(container) == amxd_object_instance) ||
375  (amxd_object_get_type(container) == amxd_object_template)) {
377  }
378 
379 exit:
380  if(status != amxd_status_ok) {
381  if(param != NULL) {
382  free(param->priv);
383  }
384  amxd_param_free(&param);
385  }
386  return status;
387 }
388 
390  amxd_object_t* base = NULL;
391 
392  when_null(object, exit);
393  when_null(object->derived_from.llist, exit);
394  base = amxc_container_of(object->derived_from.llist, amxd_object_t, derived_objects);
395 
396 exit:
397  return base;
398 }
#define IS_BIT_SET(b, f)
Definition: amxd_common.h:66
bool amxd_name_is_valid(const char *name)
Definition: amxd_common.c:115
#define SET_BIT(x)
Definition: amxd_common.h:65
Ambiorix Data Model API header file.
void PRIVATE amxd_dm_event(const char *signal, const amxd_object_t *const object, amxc_var_t *const data, bool trigger)
Definition: amxd_dm_priv.c:550
static amxd_param_t * amxd_create_counter_param(amxd_object_t *const container, amxd_object_t *const counted, const char *name)
Definition: amxd_object.c:107
static void amxd_object_set_attributes(amxd_object_t *const object, const uint32_t attr)
Definition: amxd_object.c:77
amxd_object_t * amxd_object_get_base(const amxd_object_t *const object)
Definition: amxd_object.c:389
static bool amxd_object_type_is_valid(const amxd_object_type_t type)
Definition: amxd_object.c:86
amxd_status_t amxd_object_set_counter(amxd_object_t *const object, const char *name)
Definition: amxd_object.c:362
static bool amxd_is_valid_counter(amxd_object_t *const counter, amxd_object_t *const counted)
Definition: amxd_object.c:92
Ambiorix Data Model API header file.
static uint32_t amxd_object_get_instance_count(const amxd_object_t *object)
amxd_param_t * amxd_object_get_param_counter_by_counted_object(const amxd_object_t *const object)
Gets a parameter definition associated with a counted object.
PRIVATE void amxd_object_clean(amxd_object_t *const object)
PRIVATE 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)
amxd_status_t amxd_param_counter_update(amxd_param_t *counter)
bool amxd_param_is_attr_set(const amxd_param_t *const param, const amxd_pattr_id_t attr)
amxd_status_t amxd_param_new(amxd_param_t **param, const char *name, const uint32_t type)
amxd_status_t amxd_param_set_value(amxd_param_t *const param, const amxc_var_t *const value)
amxd_status_t amxd_param_set_attr(amxd_param_t *param, const amxd_pattr_id_t attr, const bool enable)
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)
void PRIVATE amxd_param_free(amxd_param_t **param)
amxd_status_t PRIVATE amxd_param_counter_destroy(amxd_object_t *const object, amxd_param_t *const param, amxd_action_t reason, const amxc_var_t *const args, amxc_var_t *const retval, void *priv)
@ amxd_pattr_read_only
Definition: amxd_types.h:358
@ amxd_pattr_counter
Definition: amxd_types.h:361
@ action_param_destroy
Definition: amxd_types.h:116
enum _amxd_status amxd_status_t
@ amxd_status_invalid_name
Definition: amxd_types.h:86
@ amxd_status_invalid_attr
Definition: amxd_types.h:87
@ amxd_status_invalid_type
Definition: amxd_types.h:90
@ amxd_status_ok
Definition: amxd_types.h:78
@ amxd_status_unknown_error
Definition: amxd_types.h:79
@ amxd_status_duplicate
Definition: amxd_types.h:91
#define AMXD_OBJECT_INDEXED
Name and path format flag - use index for instance objects.
Definition: amxd_object.h:176
enum _amxd_object_type amxd_object_type_t
The different object types.
#define AMXD_OBJECT_NAMED
Name and path format flag - default behavior, use name for instance objects.
Definition: amxd_object.h:164
enum _amxd_oattr_id amxd_oattr_id_t
The object attributes.
@ amxd_oattr_read_only
Definition: amxd_types.h:199
@ amxd_oattr_locked
Definition: amxd_types.h:206
@ amxd_oattr_protected
Definition: amxd_types.h:210
@ amxd_oattr_max
Definition: amxd_types.h:211
@ amxd_oattr_persistent
Definition: amxd_types.h:200
@ amxd_oattr_private
Definition: amxd_types.h:202
@ amxd_object_template
Definition: amxd_types.h:183
@ amxd_object_mib
Definition: amxd_types.h:188
@ amxd_object_singleton
Definition: amxd_types.h:181
@ amxd_object_instance
Definition: amxd_types.h:186
amxd_object_t * amxd_object_get_parent(const amxd_object_t *const object)
Get the parent object.
amxd_object_t * amxd_object_get_child(const amxd_object_t *object, const char *name)
Get a child of the object.
char * amxd_object_get_rel_path(const amxd_object_t *child, const amxd_object_t *parent, const uint32_t flags)
Get the relative path of the object.
amxd_dm_t * amxd_object_get_dm(const amxd_object_t *const object)
Get the data model.
amxd_status_t amxd_object_add_object(amxd_object_t *const parent, amxd_object_t *const child)
Adds an object in the data model tree.
Definition: amxd_object.c:207
amxd_param_t * amxd_object_get_param_def(const amxd_object_t *const object, const char *name)
Gets a parameter definition from an object.
amxd_status_t amxd_object_add_param(amxd_object_t *const object, amxd_param_t *const param)
Adds a parameter definition to an object.
uint32_t amxd_object_get_index(const amxd_object_t *const object)
Get the index of an instance object.
Definition: amxd_object.c:265
amxd_status_t amxd_object_set_attr(amxd_object_t *const object, const amxd_oattr_id_t attr, const bool enable)
Sets or unsets an object attribute.
Definition: amxd_object.c:269
amxd_status_t amxd_object_new(amxd_object_t **object, const amxd_object_type_t type, const char *name)
Data model object constructor function.
Definition: amxd_object.c:185
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
bool amxd_object_is_attr_set(const amxd_object_t *const object, const amxd_oattr_id_t attr)
Checks if an attribute is set.
Definition: amxd_object.c:348
const char * amxd_object_get_name(const amxd_object_t *const object, const uint32_t flags)
Get the name of the object (or index as a string for instance objects)
Definition: amxd_object.c:239
amxd_status_t amxd_object_set_attrs(amxd_object_t *const object, const uint32_t bitmask, bool enable)
Sets or unsets object attributes using a bitmap.
Definition: amxd_object.c:301
uint32_t amxd_object_get_attrs(const amxd_object_t *const object)
Gets the set attributes of an object.
Definition: amxd_object.c:334
amxc_llist_it_t derived_from
Definition: amxd_types.h:249
amxd_object_type_t type
Definition: amxd_types.h:236
amxc_llist_t objects
Definition: amxd_types.h:243
char * name
Definition: amxd_types.h:238
amxc_llist_it_t it
Definition: amxd_types.h:230
static amxd_dm_t dm
static int counter
static amxd_status_t status