libamxd  6.4.1
Data Model Manager
amxd_parameter.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 
62 #include <amxc/amxc.h>
63 #include <amxp/amxp_signal.h>
64 #include <amxp/amxp_slot.h>
65 
66 #include <amxd/amxd_common.h>
67 #include <amxd/amxd_dm.h>
68 #include <amxd/amxd_object.h>
69 #include <amxd/amxd_object_event.h>
70 
71 #include "amxd_priv.h"
72 #include "amxd_assert.h"
73 #include "amxd_dm_priv.h"
74 
75 static void amxd_param_free_cb_it(amxc_llist_it_t* it) {
76  amxd_dm_cb_t* cb = amxc_llist_it_get_data(it, amxd_dm_cb_t, it);
77  free(cb);
78 }
79 
80 static bool amxd_param_type_is_valid(const uint32_t type) {
81  return (amxc_var_get_type(type) != NULL &&
82  type < AMXC_VAR_ID_CUSTOM_BASE &&
83  type != AMXC_VAR_ID_LIST &&
84  type != AMXC_VAR_ID_HTABLE &&
85  type != AMXC_VAR_ID_FD &&
86  type != AMXC_VAR_ID_ANY);
87 }
88 
90  const char* name,
91  uint32_t type) {
93 
94  amxc_llist_it_init(&param->it);
95  amxc_llist_init(&param->cb_fns);
96 
97  param->attr.templ = 0;
98  param->attr.instance = 0;
99  param->attr.priv = 0;
100  param->attr.read_only = 0;
101  param->attr.persistent = 0;
102  param->attr.variable = 0;
103  param->attr.counter = 0;
104  param->attr.key = 0;
105  param->attr.unique = 0;
106  param->attr.prot = 0;
107  param->attr.changeable = 0;
108 
109  amxc_var_init(&param->value);
110  param->name = strdup(name);
111  when_null(param->name, exit);
112  amxc_var_set_type(&param->value, type);
113  retval = amxd_status_ok;
114 
115 exit:
116  return retval;
117 }
118 
119 static int amxd_param_clean(amxd_param_t* const param) {
120  amxc_llist_it_take(&param->it);
121  amxc_llist_clean(&param->cb_fns, amxd_param_free_cb_it);
122  amxc_var_delete(&param->flags);
123 
124  amxc_var_clean(&param->value);
125  free(param->name);
126  param->name = NULL;
127 
128  return 0;
129 }
130 
132  const uint32_t attr) {
134  amxd_object_t* owner = amxd_param_get_owner(param);
135 
136  if(param->attr.key == 1) {
137  when_true(!IS_BIT_SET(attr, amxd_pattr_key), exit);
138  }
139 
140  when_true_status(!IS_BIT_SET(attr, amxd_pattr_key),
141  exit,
143 
144  if(owner != NULL) {
145  if(((amxd_object_get_type(owner) == amxd_object_template) &&
146  !amxc_llist_is_empty(&owner->instances)) ||
148  ( amxd_object_get_type(owner) == amxd_object_mib) ||
150  goto exit;
151  }
152  }
153 
155 
156 exit:
157  return status;
158 }
159 
161  const uint32_t attr) {
163  amxd_object_t* owner = amxd_param_get_owner(param);
164 
165  when_null(owner, exit);
167  if(IS_BIT_SET(attr, amxd_pattr_template)) {
169  }
170  }
171 
172 exit:
173  return status;
174 }
175 
177  const uint32_t attr) {
179  bool was_counter = param->attr.counter == 1 ? true : false;
180  bool is_counter = false;
181 
182  status = amxd_param_check_templ_attr(param, attr);
183  when_failed(status, exit);
184  status = amxd_param_check_key_attr(param, attr);
185  when_failed(status, exit);
186 
187  param->attr.templ = IS_BIT_SET(attr, amxd_pattr_template);
189  param->attr.priv = IS_BIT_SET(attr, amxd_pattr_private);
190  param->attr.prot = IS_BIT_SET(attr, amxd_pattr_protected);
194  param->attr.counter = IS_BIT_SET(attr, amxd_pattr_counter);
195  param->attr.key = IS_BIT_SET(attr, amxd_pattr_key);
196  param->attr.unique = IS_BIT_SET(attr, amxd_pattr_unique);
198 
199  is_counter = param->attr.counter == 1 ? true : false;
200  if(was_counter && !is_counter) {
201  free(param->priv);
202  param->priv = NULL;
203  }
204 
205 exit:
206  return status;
207 }
208 
211  amxd_object_t* owner = NULL;
212  amxd_object_t* counted = NULL;
213  const char* template_path = NULL;
214  amxc_var_t old_values;
215 
216  when_null(counter, exit);
218 
219  owner = amxd_param_get_owner(counter);
220  template_path = (const char*) counter->priv;
221  counted = amxd_object_findf(owner, "%s", template_path);
222  when_null(counted, exit);
223  amxc_var_init(&old_values);
224  amxc_var_set_type(&old_values, AMXC_VAR_ID_HTABLE);
225  amxc_var_add_key(uint32_t,
226  &old_values,
228  GET_UINT32(&counter->value, NULL));
229  amxc_var_set(uint32_t, &counter->value, amxd_object_get_instance_count(counted));
230  amxd_object_send_changed(owner, &old_values, false);
231  amxc_var_clean(&old_values);
232 
234 
235 exit:
236  return status;
237 }
238 
240  const char* name,
241  const uint32_t type) {
242 
244  when_null(param, exit);
245  when_true_status(!amxd_name_is_valid(name),
246  exit,
247  retval = amxd_status_invalid_name);
248 
249  when_true_status(!amxd_param_type_is_valid(type),
250  exit,
251  retval = amxd_status_invalid_type);
252 
253  *param = (amxd_param_t*) calloc(1, sizeof(amxd_param_t));
254  when_null((*param), exit);
255 
256  retval = amxd_param_init(*param, name, type);
257 
258 exit:
259  if((retval != 0) && (param != NULL)) {
260  free(*param);
261  *param = NULL;
262  }
263  return retval;
264 }
265 
267  when_null(param, exit);
268  when_null((*param), exit);
269 
270  amxd_param_clean((*param));
271  amxc_llist_it_take(&(*param)->it);
272 
273  free(*param);
274  *param = NULL;
275 
276 exit:
277  return;
278 }
279 
281  const amxd_param_t* const source) {
283  when_null(dest, exit);
284  when_null(source, exit);
285 
286  *dest = (amxd_param_t*) calloc(1, sizeof(amxd_param_t));
287  when_null((*dest), exit);
288 
289  when_failed(amxd_param_init(*dest,
290  source->name,
291  amxc_var_type_of(&source->value)),
292  exit);
293  (*dest)->attr = source->attr;
294  amxc_var_copy(&(*dest)->value, &source->value);
295  if(source->flags != NULL) {
296  amxc_var_new(&(*dest)->flags);
297  amxc_var_copy((*dest)->flags, source->flags);
298  }
299 
300  retval = amxd_status_ok;
301 
302 exit:
303  if((retval != amxd_status_ok) && (dest != NULL) && (*dest != NULL)) {
304  amxd_param_clean(*dest);
305  free(*dest);
306  *dest = NULL;
307  }
308  return retval;
309 }
310 
312  amxd_object_t* object = NULL;
313  when_null(param, exit);
314  when_null(param->it.llist, exit);
315 
316  object = amxc_container_of(param->it.llist, amxd_object_t, parameters);
317 
318 exit:
319  return object;
320 }
321 
322 const char* amxd_param_get_name(const amxd_param_t* const param) {
323  return param == NULL ? NULL : param->name;
324 }
325 
327  const amxd_pattr_id_t attr,
328  const bool enable) {
330  uint32_t flags = 0;
331  when_null(param, exit);
332  when_true_status(attr < 0 || attr > amxd_pattr_max,
333  exit,
334  retval = amxd_status_invalid_attr);
335 
336  flags = amxd_param_get_attrs(param);
337 
338  if(enable) {
339  flags |= (1 << attr);
340  } else {
341  flags &= ~(1 << attr);
342  }
343 
344  retval = amxd_param_set_attributes(param, flags);
345 
346 exit:
347  return retval;
348 }
349 
351  const uint32_t bitmask,
352  bool enable) {
354  uint32_t flags = 0;
355  uint32_t all = 0;
356  for(int i = 0; i <= amxd_pattr_max; i++) {
357  all |= SET_BIT(i);
358  }
359 
360  when_null(param, exit);
361  when_true_status(bitmask > all, exit, retval = amxd_status_invalid_attr);
362 
363  flags = amxd_param_get_attrs(param);
364 
365  if(enable) {
366  flags |= bitmask;
367  } else {
368  flags &= ~bitmask;
369  }
370 
371  retval = amxd_param_set_attributes(param, flags);
372 
373 exit:
374  return retval;
375 }
376 
377 uint32_t amxd_param_get_attrs(const amxd_param_t* const param) {
378  uint32_t attributes = 0;
379  when_null(param, exit);
380 
381  attributes |= param->attr.templ << amxd_pattr_template;
382  attributes |= param->attr.instance << amxd_pattr_instance;
383  attributes |= param->attr.priv << amxd_pattr_private;
384  attributes |= param->attr.prot << amxd_pattr_protected;
385  attributes |= param->attr.read_only << amxd_pattr_read_only;
386  attributes |= param->attr.persistent << amxd_pattr_persistent;
387  attributes |= param->attr.variable << amxd_pattr_variable;
388  attributes |= param->attr.counter << amxd_pattr_counter;
389  attributes |= param->attr.key << amxd_pattr_key;
390  attributes |= param->attr.unique << amxd_pattr_unique;
391  attributes |= param->attr.changeable << amxd_pattr_mutable;
392 
393 exit:
394  return attributes;
395 }
396 
397 bool amxd_param_is_attr_set(const amxd_param_t* const param,
398  const amxd_pattr_id_t attr) {
399  uint32_t flags = 0;
400  bool retval = false;
401  when_null(param, exit);
402  when_true(attr < 0 || attr > amxd_pattr_max, exit);
403 
404  flags = amxd_param_get_attrs(param);
405  retval = (flags & (1 << attr)) != 0 ? true : false;
406 
407 exit:
408  return retval;
409 }
410 
411 void amxd_param_set_flag(amxd_param_t* param, const char* flag) {
412  when_null(param, exit);
413  when_null(flag, exit);
414  when_true(*flag == 0, exit);
415 
416  amxd_common_set_flag(&param->flags, flag);
417 
418 exit:
419  return;
420 }
421 
422 void amxd_param_unset_flag(amxd_param_t* param, const char* flag) {
423  when_null(param, exit);
424  when_null(flag, exit);
425  when_true(*flag == 0, exit);
426 
427  amxd_common_unset_flag(&param->flags, flag);
428 
429 exit:
430  return;
431 }
432 
433 bool amxd_param_has_flag(const amxd_param_t* const param, const char* flag) {
434  bool retval = false;
435  when_null(param, exit);
436  when_null(flag, exit);
437  when_true(*flag == 0, exit);
438 
439  retval = amxd_common_has_flag(param->flags, flag);
440 
441 exit:
442  return retval;
443 }
#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.
bool PRIVATE amxd_common_has_flag(const amxc_var_t *const flags, const char *flag)
Definition: amxd_dm_priv.c:652
void PRIVATE amxd_common_set_flag(amxc_var_t **flags, const char *flag)
Definition: amxd_dm_priv.c:617
void PRIVATE amxd_common_unset_flag(amxc_var_t **flags, const char *flag)
Definition: amxd_dm_priv.c:634
Ambiorix Data Model API header file.
Ambiorix Data Model API header file.
static uint32_t amxd_object_get_instance_count(const amxd_object_t *object)
amxd_status_t amxd_param_copy(amxd_param_t **dest, const amxd_param_t *const source)
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_set_attrs(amxd_param_t *param, const uint32_t bitmask, bool enable)
const char * amxd_param_get_name(const amxd_param_t *const param)
void amxd_param_unset_flag(amxd_param_t *param, const char *flag)
amxd_status_t amxd_param_new(amxd_param_t **param, const char *name, const uint32_t type)
static amxd_status_t amxd_param_init(amxd_param_t *const param, const char *name, uint32_t type)
static bool amxd_param_type_is_valid(const uint32_t type)
amxd_status_t amxd_param_set_attr(amxd_param_t *param, const amxd_pattr_id_t attr, const bool enable)
void amxd_param_free(amxd_param_t **param)
bool amxd_param_has_flag(const amxd_param_t *const param, const char *flag)
void amxd_param_set_flag(amxd_param_t *param, const char *flag)
static amxd_status_t amxd_param_check_templ_attr(amxd_param_t *const param, const uint32_t attr)
static amxd_status_t amxd_param_set_attributes(amxd_param_t *const param, const uint32_t attr)
static int amxd_param_clean(amxd_param_t *const param)
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)
static void amxd_param_free_cb_it(amxc_llist_it_t *it)
static amxd_status_t amxd_param_check_key_attr(amxd_param_t *const param, const uint32_t attr)
@ amxd_pattr_template
Definition: amxd_types.h:355
@ amxd_pattr_private
Definition: amxd_types.h:357
@ amxd_pattr_persistent
Definition: amxd_types.h:359
@ amxd_pattr_variable
Definition: amxd_types.h:360
@ amxd_pattr_instance
Definition: amxd_types.h:356
@ amxd_pattr_unique
Definition: amxd_types.h:363
@ amxd_pattr_protected
Definition: amxd_types.h:364
@ amxd_pattr_mutable
Definition: amxd_types.h:365
@ amxd_pattr_key
Definition: amxd_types.h:362
@ amxd_pattr_read_only
Definition: amxd_types.h:358
@ amxd_pattr_counter
Definition: amxd_types.h:361
@ amxd_pattr_max
Definition: amxd_types.h:366
enum _amxd_pattr_id amxd_pattr_id_t
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_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
void amxd_object_send_changed(amxd_object_t *object, amxc_var_t *params, bool trigger)
Send an object changed event.
amxd_object_t * amxd_object_findf(amxd_object_t *object, const char *rel_path,...) __attribute__((format(printf
Find an object in the data model tree, starting from an 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
amxc_llist_t instances
Definition: amxd_types.h:244
uint32_t unique
Definition: amxd_types.h:381
uint32_t templ
Definition: amxd_types.h:370
uint32_t persistent
Definition: amxd_types.h:375
uint32_t read_only
Definition: amxd_types.h:374
uint32_t variable
Definition: amxd_types.h:377
uint32_t changeable
Definition: amxd_types.h:383
uint32_t instance
Definition: amxd_types.h:371
uint32_t counter
Definition: amxd_types.h:379
amxc_llist_it_t it
Definition: amxd_types.h:387
amxc_var_t value
Definition: amxd_types.h:390
amxd_param_attr_t attr
Definition: amxd_types.h:388
amxc_var_t * flags
Definition: amxd_types.h:393
amxc_llist_t cb_fns
Definition: amxd_types.h:391
static int counter
static amxd_status_t status