libamxd  6.4.1
Data Model Manager
amxd_dm_get_sup_function.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 <string.h>
56 #include <stdlib.h>
57 
58 #include "amxd_priv.h"
59 
60 #include <amxd/amxd_dm.h>
61 #include <amxd/amxd_object.h>
62 #include <amxd/amxd_path.h>
63 #include <amxd/amxd_dm_functions.h>
64 #include <amxd/amxd_transaction.h>
65 
66 #include "amxd_priv.h"
67 #include "amxd_assert.h"
68 #include "amxd_object_priv.h"
69 #include "amxd_dm_priv.h"
70 
71 static void amxd_describe_object_usp(amxc_var_t* object_data,
72  amxc_var_t* retval) {
73  uint32_t access = 1;
74  uint32_t obj_type = GET_UINT32(object_data, "type_id");
75  amxc_var_t* attrs = GET_ARG(object_data, "attributes");
76 
77  if(GET_BOOL(attrs, "read-only")) {
78  access = 0;
79  }
80  amxc_var_add_key(bool, retval, "is_multi_instance", obj_type == amxd_object_template);
81  if(obj_type == amxd_object_template) {
82  amxc_var_add_key(uint32_t, retval, "access", access);
83  }
84 }
85 
86 static void amxd_describe_param_usp(amxc_var_t* param_data,
87  amxc_var_t* retval) {
88  amxc_var_t* attrs = GET_ARG(param_data, "attributes");
89  amxc_var_t* name = GET_ARG(param_data, "name");
90  uint32_t param_type = GET_UINT32(param_data, "type_id");
91 
92  amxc_var_set_key(retval, "param_name", name, AMXC_VAR_FLAG_DEFAULT);
93  if(GET_BOOL(attrs, "read-only") || (GET_BOOL(attrs, "key") && !GET_BOOL(attrs, "mutable"))) {
94  amxc_var_add_key(uint32_t, retval, "access", 0);
95  } else {
96  amxc_var_add_key(uint32_t, retval, "access", 1);
97  }
98  amxc_var_add_key(uint32_t, retval, "type", param_type);
99  amxc_var_add_key(uint32_t, retval, "value_change", GET_BOOL(attrs, "volatile")? 2:1);
100 
101  amxc_var_delete(&attrs);
102 }
103 
104 static void amxd_describe_func_usp(amxc_var_t* func_data,
105  amxc_var_t* retval) {
106  amxc_string_t* name = amxc_var_take(amxc_string_t, GET_ARG(func_data, "name"));
107  amxc_var_t* args = GET_ARG(func_data, "arguments");
108  amxc_var_t* out_args = NULL;
109  amxc_var_t* in_args = NULL;
110 
111  amxc_string_append(name, "()", 2);
112  amxc_var_add_key(uint32_t, retval, "command_type",
113  GETP_BOOL(func_data, "attributes.asynchronous")? 2:1);
114  amxc_var_add_key(cstring_t, retval, "command_name", amxc_string_get(name, 0));
115  out_args = amxc_var_add_key(amxc_llist_t, retval, "output_arg_names", NULL);
116  in_args = amxc_var_add_key(amxc_llist_t, retval, "input_arg_names", NULL);
117  amxc_var_for_each(arg, args) {
118  amxc_var_t* arg_attrs = GET_ARG(arg, "attributes");
119  if(GET_BOOL(arg_attrs, "out")) {
120  amxc_var_add(cstring_t, out_args, GET_CHAR(arg, "name"));
121  }
122  if(GET_BOOL(arg_attrs, "in")) {
123  amxc_var_add(cstring_t, in_args, GET_CHAR(arg, "name"));
124  }
125  }
126  amxc_string_delete(&name);
127 }
128 
129 static void amxd_get_supported_add_data(amxd_object_t* const object,
130  amxc_var_t* data,
131  get_supported_args_t* sup) {
132  uint32_t flags = 0;
133  amxc_var_t object_data;
134 
135  amxc_var_init(&object_data);
136  flags = AMXD_TEMPLATE_INFO;
137  if(sup->params) {
138  flags |= AMXD_OBJECT_PARAM;
139  }
140  if(sup->functions) {
142  }
143  if(sup->events) {
144  flags |= AMXD_OBJECT_EVENT;
145  }
146  amxd_object_describe(object, &object_data, flags, sup->access);
147 
148  amxd_describe_object_usp(&object_data, data);
149  if(sup->params) {
150  const amxc_var_t* params = GET_ARG(&object_data, "parameters");
151  amxc_var_t* usp_params
152  = amxc_var_add_key(amxc_llist_t, data, "supported_params", NULL);
153  amxc_var_for_each(param, params) {
154  amxc_var_t* usp_param = amxc_var_add(amxc_htable_t, usp_params, NULL);
155  amxd_describe_param_usp(param, usp_param);
156  }
157  }
158 
159  if(sup->functions) {
160  const amxc_var_t* funcs = GET_ARG(&object_data, "functions");
161  amxc_var_t* usp_funcs
162  = amxc_var_add_key(amxc_llist_t, data, "supported_commands", NULL);
163  amxc_var_for_each(func, funcs) {
164  amxc_var_t* usp_func = amxc_var_add(amxc_htable_t, usp_funcs, NULL);
165  amxd_describe_func_usp(func, usp_func);
166  }
167  }
168 
169  if(sup->events) {
170  const amxc_var_t* events = GET_ARG(&object_data, "events");
171  amxc_var_t* usp_events
172  = amxc_var_add_key(amxc_llist_t, data, "supported_events", NULL);
173  amxc_var_copy(usp_events, events);
174  }
175 
176  amxc_var_clean(&object_data);
177 }
178 
179 static bool amxd_get_supported_filter(amxd_object_t* const object,
180  UNUSED int32_t depth,
181  UNUSED void* priv) {
183 
185  return false;
186  }
188  return false;
189  }
191  (sup->access == amxd_dm_access_public)) {
192  return false;
193  }
194  return true;
195 }
196 
197 static void amxd_get_supported_impl(amxd_object_t* const object,
198  int32_t depth,
199  void* priv) {
201  char* sup_path = amxd_object_get_path(object,
204  amxc_var_t* data = amxc_var_add_key(amxc_htable_t, sup->ret, sup_path, NULL);
205  if(sup->first_lvl && (depth == 0)) {
206  sup->functions = false;
207  sup->params = false;
208  sup->events = false;
209  }
210  amxd_get_supported_add_data(object, data, sup);
211  free(sup_path);
212 }
213 
215  get_supported_args_t* sup) {
217 
218  amxc_var_set_type(sup->ret, AMXC_VAR_ID_HTABLE);
223  sup->first_lvl ? 1 : INT32_MAX,
224  sup);
225 
227 
228  return status;
229 }
230 
232  UNUSED amxd_function_t* func,
233  amxc_var_t* args,
234  amxc_var_t* ret) {
236  amxc_var_t* var_rel_path = GET_ARG(args, "rel_path");
237  const char* rel_path = GET_CHAR(var_rel_path, NULL);
238 
240  sup.functions = GET_BOOL(args, "functions");
241  sup.params = GET_BOOL(args, "parameters");
242  sup.events = GET_BOOL(args, "events");
243  sup.first_lvl = GET_BOOL(args, "first_level_only");
244  sup.access = (amxd_dm_access_t) GET_UINT32(args, "access");
245  sup.ret = ret;
246 
247  amxc_var_take_it(var_rel_path);
248  if((rel_path != NULL) && (*rel_path != 0)) {
249  amxd_path_t path;
250  char* obj_path = NULL;
251 
252  amxd_path_init(&path, rel_path);
253  obj_path = amxd_path_get_supported_path(&path);
254  if(obj_path != NULL) {
255  object = amxd_object_findf(object, "%s", obj_path);
256  }
257  free(obj_path);
258  amxd_path_clean(&path);
259  when_null_status(object, exit, retval = amxd_status_object_not_found);
260  } else {
261  char* obj_path = NULL;
262 
264  object = amxd_dm_findf(amxd_object_get_dm(object), "%s", obj_path);
265  free(obj_path);
266  when_null_status(object, exit, retval = amxd_status_object_not_found);
267  }
268 
269  retval = amxd_get_supported(object, &sup);
270 
271 exit:
272  amxc_var_delete(&var_rel_path);
273  return retval;
274 }
Ambiorix Data Model API header file.
amxd_object_t * amxd_dm_findf(amxd_dm_t *const dm, const char *abs_path,...) __attribute__((format(printf
amxd_status_t amxd_object_func_get_supported(amxd_object_t *object, UNUSED amxd_function_t *func, amxc_var_t *args, amxc_var_t *ret)
static void amxd_describe_param_usp(amxc_var_t *param_data, amxc_var_t *retval)
static bool amxd_get_supported_filter(amxd_object_t *const object, UNUSED int32_t depth, UNUSED void *priv)
static void amxd_describe_func_usp(amxc_var_t *func_data, amxc_var_t *retval)
static void amxd_get_supported_impl(amxd_object_t *const object, int32_t depth, void *priv)
static amxd_status_t amxd_get_supported(amxd_object_t *object, get_supported_args_t *sup)
static void amxd_describe_object_usp(amxc_var_t *object_data, amxc_var_t *retval)
static void amxd_get_supported_add_data(amxd_object_t *const object, amxc_var_t *data, get_supported_args_t *sup)
Ambiorix Data Model API header file.
#define AMXD_OBJECT_EVENT
Definition: amxd_object.h:318
amxd_status_t amxd_object_describe(amxd_object_t *const object, amxc_var_t *const value, uint32_t flags, amxd_dm_access_t access)
Ambiorix path API header file.
Ambiorix Data Model API header file.
enum _amxd_status amxd_status_t
@ amxd_status_invalid_path
Definition: amxd_types.h:99
@ amxd_status_object_not_found
Definition: amxd_types.h:80
@ amxd_status_ok
Definition: amxd_types.h:78
@ amxd_status_unknown_error
Definition: amxd_types.h:79
@ amxd_direction_down
Definition: amxd_types.h:216
#define AMXD_OBJECT_SUPPORTED
Path format flag - adds {i} as placeholder for an instance object.
Definition: amxd_object.h:226
#define AMXD_TEMPLATE_INFO
List and describe flag.
Definition: amxd_object.h:314
#define AMXD_OBJECT_NO_BASE
List and describe flag.
Definition: amxd_object.h:301
#define AMXD_OBJECT_FUNC
List and describe flag.
Definition: amxd_object.h:260
enum _amxd_dm_access amxd_dm_access_t
Access level.
#define AMXD_OBJECT_PARAM
List and describe flag.
Definition: amxd_object.h:244
#define AMXD_OBJECT_TERMINATE
Path format flag - when set the object path is terminated with a dot.
Definition: amxd_object.h:214
@ amxd_oattr_protected
Definition: amxd_types.h:210
@ amxd_oattr_private
Definition: amxd_types.h:202
@ amxd_dm_access_public
Definition: amxd_types.h:136
@ amxd_object_template
Definition: amxd_types.h:183
@ amxd_object_instance
Definition: amxd_types.h:186
amxd_object_t amxd_status_t amxd_status_t char * amxd_object_get_path(const amxd_object_t *object, const uint32_t flags)
Get the full path of the object.
void amxd_object_hierarchy_walk(amxd_object_t *const object, const amxd_direction_t direction, amxd_object_filter_fn_t filter, amxd_object_cb_fn_t cb, int32_t depth, void *priv)
Iterates over all objects in the data model tree.
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.
amxd_dm_t * amxd_object_get_dm(const amxd_object_t *const object)
Get the data model.
static amxd_object_type_t amxd_object_get_type(const amxd_object_t *const object)
Returns the object type.
Definition: amxd_object.h:586
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
amxd_status_t amxd_path_init(amxd_path_t *path, const char *object_path)
Initializes an amxd_path_t structure.
Definition: amxd_path.c:328
char * amxd_path_get_supported_path(amxd_path_t *path)
Translates the path into a path that can be used to fetch the object definition.
Definition: amxd_path.c:611
void amxd_path_clean(amxd_path_t *path)
Cleans an amxd_path_t structure.
Definition: amxd_path.c:351
RPC method structure.
Definition: amxd_types.h:341
amxd_dm_access_t access
static amxd_status_t status