libamxd  6.4.1
Data Model Manager
amxd_function_args.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 #include <ctype.h>
62 
63 #include "amxd_priv.h"
64 
65 #include <amxd/amxd_common.h>
66 #include <amxd/amxd_object.h>
67 #include <amxd/amxd_function.h>
68 
69 #include "amxd_assert.h"
70 
72  const char* name,
73  uint32_t type,
74  amxc_var_t* default_value) {
76 
77  amxc_llist_it_init(&arg->it);
78 
79  if((type == AMXC_VAR_ID_ANY) ||
80  ( amxc_var_get_type(type) != NULL)) {
81  arg->name = strdup(name);
82  if(arg->name != NULL) {
83  arg->attr.in = 0;
84  arg->attr.out = 0;
85  arg->attr.mandatory = 0;
86  arg->attr.strict = 0;
87 
88  amxc_var_init(&arg->default_value);
89  amxc_var_copy(&arg->default_value, default_value);
90  arg->type = type;
91  retval = amxd_status_ok;
92  }
93  }
94 
95  return retval;
96 }
97 
98 static uint32_t amxd_function_arg_get_attributes(const amxd_func_arg_t* const arg) {
99  uint32_t attributes = 0;
100 
101  attributes |= arg->attr.in << amxd_aattr_in;
102  attributes |= arg->attr.out << amxd_aattr_out;
103  attributes |= arg->attr.mandatory << amxd_aattr_mandatory;
104  attributes |= arg->attr.strict << amxd_aattr_strict;
105 
106  return attributes;
107 }
108 
110  const uint32_t attr) {
111  arg->attr.in = IS_BIT_SET(attr, amxd_aattr_in);
112  arg->attr.out = IS_BIT_SET(attr, amxd_aattr_out);
114  arg->attr.strict = IS_BIT_SET(attr, amxd_aattr_strict);
115 }
116 
118  const char* name) {
119  bool retval = true;
120 
121  amxc_llist_for_each(it, (&func->args)) {
122  amxd_func_arg_t* arg = amxc_llist_it_get_data(it, amxd_func_arg_t, it);
123  if(strcmp(arg->name, name) == 0) {
124  retval = false;
125  break;
126  }
127  }
128 
129  return retval;
130 }
131 
132 void PRIVATE amxd_function_arg_clean(amxd_func_arg_t* const arg) {
133  amxc_llist_it_take(&arg->it);
134 
135  amxc_var_clean(&arg->default_value);
136  free(arg->name);
137  arg->name = NULL;
138 }
139 
141  const char* name,
142  const uint32_t type,
143  amxc_var_t* default_value) {
145  amxd_func_arg_t* arg = NULL;
146  when_null(func, exit);
147 
148  when_str_empty_status(name, exit, retval = amxd_status_invalid_name);
149  when_true_status(!amxd_function_arg_valid_name(func, name),
150  exit,
151  retval = amxd_status_invalid_name);
152 
153  arg = (amxd_func_arg_t*) calloc(1, sizeof(amxd_func_arg_t));
154  when_null(arg, exit);
155 
156  retval = amxd_function_arg_init(arg, name, type, default_value);
157 
158  if(retval == amxd_status_ok) {
159  amxc_llist_append(&func->args, &arg->it);
160  }
161 exit:
162  if(retval != amxd_status_ok) {
163  free(arg);
164  }
165  return retval;
166 }
167 
168 void amxd_function_del_arg(amxd_function_t* func, const char* name) {
169  amxd_func_arg_t* arg = NULL;
170  when_null(func, exit);
171  when_str_empty(name, exit);
172 
173  arg = amxd_function_get_arg(func, name);
174  when_null(arg, exit);
175 
177  free(arg);
178 
179 exit:
180  return;
181 }
182 
184  const char* name,
185  const amxd_aattr_id_t attr,
186  const bool enable) {
188  uint32_t flags = 0;
189  amxd_func_arg_t* arg = NULL;
190  when_null(func, exit);
191 
192  when_true_status(attr < 0 || attr > amxd_aattr_max,
193  exit,
194  retval = amxd_status_invalid_attr);
195 
196  arg = amxd_function_get_arg(func, name);
197  when_null(arg, exit);
199 
200  // when type any is set, it can not be a strict typed argument
201  if((attr == amxd_aattr_strict) &&
202  ( arg->type == AMXC_VAR_ID_ANY)) {
203  retval = amxd_status_invalid_attr;
204  goto exit;
205  }
206 
207  if(enable) {
208  flags |= (1 << attr);
209  } else {
210  flags &= ~(1 << attr);
211  }
212 
214  retval = amxd_status_ok;
215 
216 exit:
217  return retval;
218 }
219 
221  const char* name,
222  const uint32_t bitmask,
223  bool enable) {
225  uint32_t flags = 0;
226  amxd_func_arg_t* arg = NULL;
227  uint32_t all = 0;
228 
229  for(int i = 0; i <= amxd_aattr_max; i++) {
230  all |= SET_BIT(i);
231  }
232 
233  when_null(func, exit);
234  when_true_status(bitmask > all, exit, retval = amxd_status_invalid_attr);
235 
236  arg = amxd_function_get_arg(func, name);
237  when_null(arg, exit);
239 
240  // when type any is set, it can not be a strict typed argument
241  if(((bitmask & SET_BIT(amxd_aattr_strict)) != 0) &&
242  ( arg->type == AMXC_VAR_ID_ANY)) {
243  retval = amxd_status_invalid_attr;
244  goto exit;
245  }
246 
247  if(enable) {
248  flags |= bitmask;
249  } else {
250  flags &= ~bitmask;
251  }
252 
254  retval = amxd_status_ok;
255 
256 exit:
257  return retval;
258 }
259 
261  const char* name,
262  const amxd_aattr_id_t attr) {
263  uint32_t flags = 0;
264  amxd_func_arg_t* arg = NULL;
265  bool retval = false;
266  when_null(func, exit);
267  when_true(attr < 0 || attr > amxd_aattr_max, exit);
268 
269  arg = amxd_function_get_arg(func, name);
270  when_null(arg, exit);
272  retval = (flags & (1 << attr)) != 0 ? true : false;
273 
274 exit:
275  return retval;
276 }
277 
279  amxc_var_t* const value) {
281  amxc_var_t* table = NULL;
282  uint32_t attrs = 0;
283  static const char* attr_name[] = {
284  "in",
285  "out",
286  "mandatory",
287  "strict"
288  };
289 
290  when_null(arg, exit);
291  when_null(value, exit);
292 
293  amxc_var_set_type(value, AMXC_VAR_ID_HTABLE);
294  amxc_var_add_key(cstring_t, value, "name", arg->name);
295  amxc_var_add_key(uint32_t, value, "type_id", arg->type);
296  amxc_var_add_key(cstring_t,
297  value,
298  "type_name",
299  amxc_var_get_type_name_from_id(arg->type));
300  if(!amxc_var_is_null(&arg->default_value)) {
301  amxc_var_set_key(value, "default", &arg->default_value, AMXC_VAR_FLAG_COPY);
302  }
303  table = amxc_var_add_key(amxc_htable_t, value, "attributes", NULL);
305  for(int i = 0; i <= amxd_aattr_max; i++) {
306  bool is_set = (attrs & (1 << i)) != 0 ? true : false;
307  amxc_var_add_key(bool,
308  table,
309  attr_name[i],
310  is_set);
311  }
312 
314 
315 exit:
316  return status;
317 }
#define IS_BIT_SET(b, f)
Definition: amxd_common.h:66
#define SET_BIT(x)
Definition: amxd_common.h:65
Ambiorix Data Model RPC methods API header file.
static bool amxd_function_arg_valid_name(amxd_function_t *func, const char *name)
static amxd_status_t amxd_function_arg_init(amxd_func_arg_t *const arg, const char *name, uint32_t type, amxc_var_t *default_value)
static uint32_t amxd_function_arg_get_attributes(const amxd_func_arg_t *const arg)
void PRIVATE amxd_function_arg_clean(amxd_func_arg_t *const arg)
static void amxd_function_arg_set_attributes(amxd_func_arg_t *const arg, const uint32_t attr)
Ambiorix Data Model API header file.
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_ok
Definition: amxd_types.h:78
@ amxd_status_unknown_error
Definition: amxd_types.h:79
enum _amxd_aattr_id amxd_aattr_id_t
The method argument attributes.
@ amxd_aattr_max
Definition: amxd_types.h:287
@ amxd_aattr_strict
Definition: amxd_types.h:286
@ amxd_aattr_in
Definition: amxd_types.h:283
@ amxd_aattr_mandatory
Definition: amxd_types.h:285
@ amxd_aattr_out
Definition: amxd_types.h:284
amxd_status_t amxd_function_arg_set_attr(amxd_function_t *const func, const char *name, const amxd_aattr_id_t attr, const bool enable)
Sets or unsets a method argument attribute.
amxd_func_arg_t * amxd_function_get_arg(const amxd_function_t *const func, const char *name)
Gets the argument definition of a RPC method.
bool amxd_function_arg_is_attr_set(const amxd_function_t *const func, const char *name, const amxd_aattr_id_t attr)
Checks if a method argument attribute is set.
amxd_status_t amxd_function_arg_describe(amxd_func_arg_t *const arg, amxc_var_t *const value)
Fetches the argument definition in a variant.
void amxd_function_del_arg(amxd_function_t *func, const char *name)
Removes an argument definition from a RPC method definition.
amxd_status_t amxd_function_new_arg(amxd_function_t *func, const char *name, const uint32_t type, amxc_var_t *default_value)
Adds an argument definition to a RPC method definition.
amxd_status_t amxd_function_arg_set_attrs(amxd_function_t *func, const char *name, const uint32_t bitmask, bool enable)
Sets or unsets method argument attributes using a bitmap.
uint32_t in
Definition: amxd_types.h:291
uint32_t strict
Definition: amxd_types.h:294
uint32_t mandatory
Definition: amxd_types.h:293
uint32_t out
Definition: amxd_types.h:292
amxc_llist_it_t it
Definition: amxd_types.h:298
amxc_var_t default_value
Definition: amxd_types.h:302
uint32_t type
Definition: amxd_types.h:301
amxd_arg_attr_t attr
Definition: amxd_types.h:299
RPC method structure.
Definition: amxd_types.h:341
amxc_llist_t args
Definition: amxd_types.h:346
static amxd_status_t status