libamxd  6.4.1
Data Model Manager
amxd_action_object_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 <stdlib.h>
56 
57 #include "amxd_priv.h"
58 
59 #include <amxd/amxd_dm.h>
60 #include <amxd/amxd_action.h>
61 #include <amxd/amxd_object.h>
62 #include <amxd/amxd_object_event.h>
63 
64 #include "amxd_object_priv.h"
65 #include "amxd_assert.h"
66 
68  UNUSED const amxc_var_t* args,
69  amxc_var_t* const retval) {
71  amxc_var_t* data = NULL;
72  uint32_t obj_type = amxd_object_get_type(object);
73  amxd_object_t* parent = amxd_object_get_parent(object);
74  char* path = NULL;
75  char* obj = NULL;
76  static const char* attr_name[] = {
77  "read-only", "persistent", "private", "locked", "protected"
78  };
79  static const char* type_name[] = {
80  "root", "singleton", "template", "instance", "mib", "invalid",
81  };
82 
84  path = amxd_object_get_path(parent,
86  obj = amxd_object_get_path(parent,
88  } else {
89  path = amxd_object_get_path(object,
91  obj = amxd_object_get_path(object,
93  }
94 
95  amxc_var_add_key(cstring_t,
96  retval,
97  "name",
99  amxc_var_add_key(uint32_t, retval, "type_id", obj_type);
100  amxc_var_add_key(cstring_t, retval, "type_name", type_name[obj_type]);
101  amxc_var_add_key(cstring_t, retval, "path", path == NULL ? "" : path);
102  amxc_var_add_key(cstring_t, retval, "object", obj == NULL ? "" : obj);
104  amxc_var_add_key(uint32_t, retval, "index", amxd_object_get_index(object));
105  }
106 
107  data = amxc_var_add_key(amxc_htable_t, retval, "attributes", NULL);
108  for(int i = 0; i <= amxd_oattr_max; i++) {
109  amxc_var_add_key(bool,
110  data,
111  attr_name[i],
113  }
114 
115  free(path);
116  free(obj);
118 
119  return status;
120 }
121 
123  const amxc_var_t* args,
124  amxc_var_t* const retval) {
126  amxc_var_t* data = NULL;
127  amxd_dm_access_t access = (amxd_dm_access_t) GET_UINT32(args, "access");
129  uint32_t attributes = GET_ARG(args, "attributes") == NULL ? 0xffffffff : GET_UINT32(args, "attributes");
130 
131  data = amxc_var_add_key(amxc_htable_t, retval, "parameters", NULL);
132  amxc_llist_for_each(it, (&object->parameters)) {
133  amxd_param_t* param = amxc_llist_it_get_data(it, amxd_param_t, it);
134  const char* name = amxd_param_get_name(param);
135  uint32_t param_attrs = amxd_param_get_attrs(param);
136  amxc_var_t* desc = NULL;
137  if(((attributes & param_attrs) == 0) && (attributes != 0xffffffff)) {
138  continue;
139  }
140  if(!amxd_action_can_add_param(param_attrs, type, access, true)) {
141  continue;
142  }
143 
144  desc = amxc_var_add_key(amxc_htable_t, data, name, NULL);
145  amxd_dm_invoke_action(object, param, action_param_describe, args, desc);
146  }
147 
149 
150  return status;
151 }
152 
154  const amxc_var_t* args,
155  amxc_var_t* const retval) {
157  amxc_var_t obj_list;
158  amxc_var_t* var_funcs_list = NULL;
159  const amxc_llist_t* ll_funcs = NULL;
160  amxc_var_t* data = NULL;
161  amxd_dm_access_t access = (amxd_dm_access_t) GET_UINT32(args, "access");
162  bool template_info = GET_BOOL(args, "template_info");
163  uint32_t flags = AMXD_OBJECT_FUNC;
164 
165  amxc_var_init(&obj_list);
166 
167  if(access == amxd_dm_access_public) {
168  flags |= AMXD_OBJECT_NO_BASE;
169  }
170 
171  if(template_info) {
172  flags |= AMXD_TEMPLATE_INFO;
173  }
174 
175  amxd_object_list(object, &obj_list, flags, access);
176  var_funcs_list = GET_ARG(&obj_list, "functions");
177  ll_funcs = amxc_var_constcast(amxc_llist_t, var_funcs_list);
178 
179  data = amxc_var_add_key(amxc_htable_t, retval, "functions", NULL);
180  amxc_llist_for_each(it, ll_funcs) {
181  const char* func_name = amxc_var_constcast(cstring_t,
182  amxc_var_from_llist_it(it));
183  amxd_function_t* func = amxd_object_get_function(object, func_name);
184  amxc_var_t* desc = amxc_var_add_key(amxc_htable_t, data, func_name, NULL);
185  amxd_function_describe(func, desc);
186  }
187 
189 
190  amxc_var_clean(&obj_list);
191  return status;
192 }
193 
195  const amxc_var_t* args,
196  amxc_var_t* const retval) {
198  amxc_var_t* data = NULL;
199  amxd_dm_access_t access = (amxd_dm_access_t) GET_UINT32(args, "access");
200 
202  goto exit;
203  }
204 
205  data = amxc_var_add_key(amxc_llist_t, retval, "objects", NULL);
206  amxc_llist_for_each(it, (&object->objects)) {
207  amxd_object_t* child = amxc_llist_it_get_data(it, amxd_object_t, it);
209  continue;
210  }
211  amxc_var_add(cstring_t, data, amxd_object_get_name(child, AMXD_OBJECT_NAMED));
212  }
213 
214 exit:
215  return status;
216 }
217 
219  UNUSED const amxc_var_t* args,
220  amxc_var_t* const retval) {
222  amxc_var_t* data = NULL;
223 
224  data = amxc_var_add_key(amxc_llist_t, retval, "instances", NULL);
225  amxc_llist_for_each(it, (&object->instances)) {
226  amxd_object_t* child = amxc_llist_it_get_data(it, amxd_object_t, it);
227  amxc_var_add(cstring_t, data, amxd_object_get_name(child, AMXD_OBJECT_INDEXED));
228  }
229 
230  return status;
231 }
232 
233 static void amxd_add_event_params(amxc_var_t* event, amxc_var_t* params, const char* name) {
234  amxc_string_t full_name;
235  amxc_string_init(&full_name, 0);
236 
237  amxc_var_for_each(param, event) {
238  const char* param_name = amxc_var_key(param);
239  if((name != NULL) && (*name != 0)) {
240  amxc_string_setf(&full_name, "%s.%s", name, param_name);
241  } else {
242  amxc_string_set(&full_name, param_name);
243  }
244  if(amxc_var_type_of(param) == AMXC_VAR_ID_HTABLE) {
245  amxd_add_event_params(param, params, amxc_string_get(&full_name, 0));
246  } else {
247  amxc_var_add(cstring_t, params, amxc_string_get(&full_name, 0));
248  }
249  }
250 
251  amxc_string_clean(&full_name);
252 }
253 
255  amxc_var_t* events) {
256  amxc_var_for_each(event, (&object->events)) {
257  const char* ename = amxc_var_key(event);
258  amxc_var_t* params = amxc_var_add_key(amxc_llist_t, events, ename, NULL);
259  amxd_add_event_params(event, params, NULL);
260  }
261 }
262 
264  UNUSED const amxc_var_t* args,
265  amxc_var_t* const retval) {
267  amxc_var_t* data = NULL;
268 
269  data = amxc_var_add_key(amxc_htable_t, retval, "events", NULL);
270  amxd_add_event_descriptions(object, data);
271 
273  object = amxd_object_get_parent(object);
274  } else {
275  object = amxd_object_get_base(object);
276  }
277  while(object != NULL) {
278  amxd_add_event_descriptions(object, data);
279  object = amxd_object_get_base(object);
280  }
281 
283 
284  return status;
285 }
286 
288  amxc_var_t* const value,
289  uint32_t flags,
290  amxd_dm_access_t access,
291  const char* item) {
293  amxc_var_t obj_data;
294 
295  amxc_var_init(&obj_data);
296 
297  when_null(value, exit);
298 
299  retval = amxd_object_describe(object, &obj_data, flags, access);
300  if(retval == amxd_status_ok) {
301  amxd_fetch_item(&obj_data, item, value);
302  }
303 
304 exit:
305  amxc_var_clean(&obj_data);
306  return retval;
307 }
308 
310  UNUSED amxd_param_t* param,
311  amxd_action_t reason,
312  const amxc_var_t* const args,
313  amxc_var_t* const retval,
314  UNUSED void* priv) {
317  static list_parts_t parts[5] = {
318  { "parameters", amxd_describe_params},
319  { "functions", amxd_describe_funcs},
320  { "objects", amxd_describe_objects},
321  { "instances", amxd_describe_instances},
322  { "events", amxd_describe_events},
323  };
324  when_null(object, exit);
325  when_null(retval, exit);
326 
327  when_true_status(reason != action_object_describe,
328  exit,
330 
331  access = (amxd_dm_access_t) amxc_var_dyncast(uint32_t, GET_ARG(args, "access"));
332 
333  when_true_status(!amxd_action_verify_access(object, access),
334  exit,
336 
337  amxc_var_set_type(retval, AMXC_VAR_ID_HTABLE);
338  amxd_describe_object_impl(object, args, retval);
339 
340  for(uint32_t i = 0; i < 5; i++) {
341  if(amxd_must_add(args, parts[i].name, object)) {
342  status = parts[i].fn(object, args, retval);
343  when_failed(status, exit);
344  }
345  }
346 
348 
349 exit:
350  return status;
351 }
352 
354  amxc_var_t* const value,
355  uint32_t flags,
356  amxd_dm_access_t access) {
358  amxc_var_t args;
359 
360  amxc_var_init(&args);
361 
362  when_null(object, exit);
363  when_null(value, exit);
364 
365  amxc_var_set_type(&args, AMXC_VAR_ID_HTABLE);
366  amxc_var_add_key(bool, &args, "parameters", flags & (AMXD_OBJECT_PARAM | AMXD_OBJECT_KEY_PARAM));
367  amxc_var_add_key(bool, &args, "functions", flags & AMXD_OBJECT_FUNC);
368  amxc_var_add_key(bool, &args, "objects", flags & AMXD_OBJECT_CHILD);
369  amxc_var_add_key(bool, &args, "instances", flags & AMXD_OBJECT_INSTANCE);
370  amxc_var_add_key(bool, &args, "template_info", flags & AMXD_TEMPLATE_INFO);
371  amxc_var_add_key(bool, &args, "events", flags & AMXD_OBJECT_EVENT);
372  amxc_var_add_key(uint32_t, &args, "access", access);
373 
374  if(flags & (AMXD_OBJECT_KEY_PARAM)) {
375  uint32_t attributes = flags & AMXD_OBJECT_KEY_PARAM ? SET_BIT(amxd_pattr_key) : 0;
376  amxc_var_add_key(uint32_t, &args, "attributes", attributes);
377  }
378 
379  retval = amxd_dm_invoke_action(object,
380  NULL,
382  &args,
383  value);
384 
385 exit:
386  amxc_var_clean(&args);
387  return retval;
388 }
389 
391  amxc_var_t* const value,
392  amxd_dm_access_t access) {
393  return amxd_object_describe_item(object,
394  value,
396  access,
397  "parameters");
398 }
399 
401  amxc_var_t* const value,
402  amxd_dm_access_t access) {
403  return amxd_object_describe_item(object,
404  value,
406  access,
407  "parameters");
408 }
409 
411  amxc_var_t* const value,
412  amxd_dm_access_t access) {
413  return amxd_object_describe_item(object,
414  value,
416  access,
417  "events");
418 }
419 
421  amxc_var_t* const value,
422  amxd_dm_access_t access) {
423  return amxd_object_describe_item(object,
424  value,
426  access,
427  "functions");
428 }
Ambiorix Data Model Default actions header file.
static amxd_status_t amxd_describe_instances(amxd_object_t *object, UNUSED const amxc_var_t *args, amxc_var_t *const retval)
static void amxd_add_event_params(amxc_var_t *event, amxc_var_t *params, const char *name)
amxd_status_t amxd_action_object_describe(amxd_object_t *object, UNUSED amxd_param_t *param, amxd_action_t reason, const amxc_var_t *const args, amxc_var_t *const retval, UNUSED void *priv)
amxd_status_t amxd_object_describe(amxd_object_t *const object, amxc_var_t *const value, uint32_t flags, amxd_dm_access_t access)
amxd_status_t amxd_object_describe_key_params(amxd_object_t *const object, amxc_var_t *const value, amxd_dm_access_t access)
amxd_status_t amxd_object_describe_events(amxd_object_t *const object, amxc_var_t *const value, amxd_dm_access_t access)
static void amxd_add_event_descriptions(amxd_object_t *object, amxc_var_t *events)
static amxd_status_t amxd_describe_objects(amxd_object_t *object, const amxc_var_t *args, amxc_var_t *const retval)
static amxd_status_t amxd_describe_events(amxd_object_t *object, UNUSED const amxc_var_t *args, amxc_var_t *const retval)
static amxd_status_t amxd_object_describe_item(amxd_object_t *const object, amxc_var_t *const value, uint32_t flags, amxd_dm_access_t access, const char *item)
static amxd_status_t amxd_describe_funcs(amxd_object_t *object, const amxc_var_t *args, amxc_var_t *const retval)
amxd_status_t amxd_object_describe_params(amxd_object_t *const object, amxc_var_t *const value, amxd_dm_access_t access)
static amxd_status_t amxd_describe_params(amxd_object_t *object, const amxc_var_t *args, amxc_var_t *const retval)
static amxd_status_t amxd_describe_object_impl(amxd_object_t *object, UNUSED const amxc_var_t *args, amxc_var_t *const retval)
#define SET_BIT(x)
Definition: amxd_common.h:65
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.
amxd_status_t amxd_object_list(amxd_object_t *const object, amxc_var_t *const list, uint32_t flags, amxd_dm_access_t access)
#define AMXD_OBJECT_KEY_PARAM
Definition: amxd_object.h:316
#define AMXD_OBJECT_EVENT
Definition: amxd_object.h:318
amxd_object_t * amxd_object_get_base(const amxd_object_t *const object)
Definition: amxd_object.c:389
bool amxd_action_can_access_object(uint32_t object_attrs, amxd_dm_access_t access)
bool amxd_action_verify_access(amxd_object_t *object, amxd_dm_access_t access)
bool amxd_action_can_add_param(uint32_t param_attrs, const amxd_object_type_t type, amxd_dm_access_t access, bool template_info)
Ambiorix Data Model API header file.
PRIVATE void amxd_fetch_item(amxc_var_t *const full_data, const char *item, amxc_var_t *const data)
const char * amxd_param_get_name(const amxd_param_t *const param)
uint32_t amxd_param_get_attrs(const amxd_param_t *const param)
bool PRIVATE amxd_must_add(const amxc_var_t *const args, const char *name, amxd_object_t *object)
@ amxd_pattr_key
Definition: amxd_types.h:362
@ action_object_describe
Definition: amxd_types.h:121
@ 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_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
#define AMXD_OBJECT_INSTANCE
List flag.
Definition: amxd_object.h:287
#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_TEMPLATE_INFO
List and describe flag.
Definition: amxd_object.h:314
#define AMXD_OBJECT_NAMED
Name and path format flag - default behavior, use name for instance objects.
Definition: amxd_object.h:164
#define AMXD_OBJECT_CHILD
List flag.
Definition: amxd_object.h:273
#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.
enum _amxd_oattr_id amxd_oattr_id_t
The object attributes.
#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_max
Definition: amxd_types.h:211
@ 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.
amxd_object_t * amxd_object_get_parent(const amxd_object_t *const object)
Get the parent object.
amxd_function_t * amxd_object_get_function(const amxd_object_t *const object, const char *name)
Get the definition of a RPC method from an object.
amxd_status_t amxd_object_describe_functions(amxd_object_t *const object, amxc_var_t *const value, amxd_dm_access_t access)
Fetches the full object RPC method definitions in a variant.
uint32_t amxd_object_get_index(const amxd_object_t *const object)
Get the index of an instance object.
Definition: amxd_object.c:265
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
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
uint32_t amxd_object_get_attrs(const amxd_object_t *const object)
Gets the set attributes of an object.
Definition: amxd_object.c:334
amxd_status_t amxd_function_describe(amxd_function_t *const func, amxc_var_t *const value)
Fetches the full RPC method definition in a variant.
RPC method structure.
Definition: amxd_types.h:341
amxc_llist_t objects
Definition: amxd_types.h:243
amxc_llist_t instances
Definition: amxd_types.h:244
amxc_var_t events
Definition: amxd_types.h:256
amxc_llist_t parameters
Definition: amxd_types.h:246
list_part_fn_t fn
Definition: amxd_priv.h:73
static amxd_status_t status