libamxd  6.4.1
Data Model Manager
amxd_action_param_describe.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 #include <amxc/amxc.h>
56 #include <amxp/amxp_signal.h>
57 
58 #include <amxd/amxd_types.h>
59 #include <amxd/amxd_common.h>
60 #include <amxd/amxd_dm.h>
61 #include <amxd/amxd_object.h>
62 #include <amxd/amxd_action.h>
63 #include <amxd/amxd_parameter.h>
64 
65 #include "amxd_priv.h"
66 #include "amxd_assert.h"
67 
69  amxd_param_t* param,
70  amxc_var_t* actions,
71  amxd_action_t action) {
72  amxc_llist_t* cb_fns = &param->cb_fns;
73  amxd_object_t* super = NULL;
74  amxd_param_t* super_param = NULL;
75 
76  when_null(object, exit);
77  when_null(param, exit);
78 
79  amxc_llist_for_each(it, cb_fns) {
80  amxd_dm_cb_t* cb_fn = amxc_llist_it_get_data(it, amxd_dm_cb_t, it);
81  if(cb_fn->reason != action) {
82  continue;
83  }
84  cb_fn->fn(object, param, action_describe_action, NULL, actions, cb_fn->priv);
85  }
86 
87  when_false(amxc_htable_is_empty(amxc_var_constcast(amxc_htable_t, actions)), exit);
88 
90  super = amxd_object_get_parent(object);
91  } else {
92  super = (object->derived_from.llist == NULL) ?
93  NULL :
94  amxc_container_of(object->derived_from.llist,
96  derived_objects);
97  }
98  if(super != NULL) {
99  super_param = amxd_object_get_param_def(object, amxd_param_get_name(param));
100  if(super_param != NULL) {
101  amxd_param_describe_actions(super, super_param, actions, action);
102  }
103  }
104 
105 exit:
106  return;
107 }
108 
109 void amxd_param_build_description(amxc_var_t* description,
110  const char* name,
111  uint32_t type_id,
112  uint32_t attrs,
113  amxc_var_t* flags) {
114  static const char* attr_name[] = {
115  "template", "instance", "private", "read-only", "persistent",
116  "volatile", "counter", "key", "unique", "protected", "mutable"
117  };
118  amxc_var_t* subvar = NULL;
119  amxc_var_set_type(description, AMXC_VAR_ID_HTABLE);
120 
121  amxc_var_add_key(cstring_t, description, "name", name);
122  amxc_var_add_key(uint32_t, description, "type_id", type_id);
123  amxc_var_add_key(cstring_t, description, "type_name", amxc_var_get_type_name_from_id(type_id));
124  amxc_var_add_new_key(description, "value");
125  subvar = amxc_var_add_key(amxc_htable_t, description, "attributes", NULL);
126  for(int i = 0; i <= amxd_pattr_max; i++) {
127  amxc_var_add_key(bool, subvar, attr_name[i], IS_BIT_SET(attrs, i));
128  }
129  subvar = amxc_var_add_key(amxc_llist_t, description, "flags", NULL);
130  if(flags != NULL) {
131  const amxc_htable_t* ht_flags = amxc_var_constcast(amxc_htable_t, flags);
132  amxc_htable_iterate(it, ht_flags) {
133  amxc_var_t* flag = amxc_var_from_htable_it(it);
134  if(amxc_var_dyncast(bool, flag)) {
135  amxc_var_add(cstring_t, subvar, amxc_htable_it_get_key(it));
136  }
137  }
138  }
139 }
140 
142  amxd_param_t* param,
143  amxd_action_t reason,
144  const amxc_var_t* const args,
145  amxc_var_t* const retval,
146  UNUSED void* priv) {
148  amxc_var_t* value = NULL;
149  amxc_var_t* constraints = NULL;
150 
151  when_null(param, exit);
152  when_null(retval, exit);
153 
154  when_true_status(reason != action_param_describe,
155  exit,
157 
159  amxd_param_get_name(param),
160  amxd_param_get_type(param),
161  amxd_param_get_attrs(param),
162  param->flags);
163 
164  constraints = amxc_var_add_key(amxc_htable_t, retval, "validate", NULL);
165  amxd_param_describe_actions(object, param, constraints, action_param_validate);
166  if(amxc_htable_is_empty(amxc_var_constcast(amxc_htable_t, constraints))) {
167  amxc_var_delete((&constraints));
168  }
169 
170  if((GET_ARG(args, "no-param-value") == NULL) || !GET_BOOL(args, "no-param-value")) {
171  value = amxc_var_get_key(retval, "value", AMXC_VAR_FLAG_DEFAULT);
172  amxd_dm_invoke_action(object, param, action_param_read, args, value);
173  }
174 
176 
177 exit:
178  return status;
179 }
180 
182  amxc_var_t* const value) {
184  amxd_object_t* object = NULL;
185 
186  when_null(param, exit);
187  when_null(value, exit);
188 
189  object = amxd_param_get_owner(param);
190  retval = amxd_dm_invoke_action(object,
191  param,
193  NULL,
194  value);
195 
196 exit:
197  return retval;
198 }
Ambiorix Data Model Default actions header file.
amxd_status_t amxd_param_describe(amxd_param_t *const param, amxc_var_t *const value)
void amxd_param_build_description(amxc_var_t *description, const char *name, uint32_t type_id, uint32_t attrs, amxc_var_t *flags)
static void amxd_param_describe_actions(amxd_object_t *object, amxd_param_t *param, amxc_var_t *actions, amxd_action_t action)
amxd_status_t amxd_action_param_describe(amxd_object_t *object, amxd_param_t *param, amxd_action_t reason, const amxc_var_t *const args, amxc_var_t *const retval, UNUSED void *priv)
#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
Ambiorix Data Model API header file.
const char * amxd_param_get_name(const amxd_param_t *const param)
static uint32_t amxd_param_get_type(const 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)
@ amxd_pattr_max
Definition: amxd_types.h:366
@ action_describe_action
Definition: amxd_types.h:127
@ action_param_read
Definition: amxd_types.h:112
@ action_param_validate
Definition: amxd_types.h:114
@ action_param_describe
Definition: amxd_types.h:115
enum _amxd_action amxd_action_t
enum _amxd_status amxd_status_t
@ amxd_status_function_not_implemented
Definition: amxd_types.h:83
@ amxd_status_ok
Definition: amxd_types.h:78
@ amxd_status_unknown_error
Definition: amxd_types.h:79
@ 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_param_t * amxd_object_get_param_def(const amxd_object_t *const object, const char *name)
Gets a parameter definition 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_it_t derived_from
Definition: amxd_types.h:249
amxc_var_t * flags
Definition: amxd_types.h:393
amxc_llist_t cb_fns
Definition: amxd_types.h:391
static int cb_fn(amxd_object_t *start_object, amxd_object_t *matching_object, UNUSED void *priv)
static amxd_status_t status