libamxd  6.4.1
Data Model Manager
amxd_object_action.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_action.h>
67 #include <amxd/amxd_object.h>
68 #include <amxd/amxd_function.h>
69 #include <amxd/amxd_parameter.h>
70 
71 #include "amxd_assert.h"
72 
74  const amxd_action_t reason,
76  void* priv) {
78  amxd_dm_cb_t* cb = NULL;
79  when_null(object, exit);
80  when_null(fn, exit);
81 
82  cb = amxd_get_action(&object->cb_fns, reason, fn);
83  if((cb != NULL) && (cb->priv == priv)) {
84  goto exit;
85  }
86 
87  cb = (amxd_dm_cb_t*) calloc(1, sizeof(amxd_dm_cb_t));
88  when_null(cb, exit);
89 
90  cb->fn = fn;
91  cb->reason = reason;
92  cb->priv = priv;
93  cb->enable = true;
94 
95  amxc_llist_append(&object->cb_fns, &cb->it);
97 
98 exit:
99  return status;
100 }
101 
103  const amxd_action_t reason,
104  amxd_action_fn_t fn) {
106  amxd_dm_cb_t* cb = NULL;
107  when_null(object, exit);
108  when_null(fn, exit);
109 
110  cb = amxd_get_action(&object->cb_fns, reason, fn);
111  when_null(cb, exit);
112 
113  amxc_llist_it_take(&cb->it);
114  free(cb);
115 
117 
118 exit:
119  return status;
120 }
121 
123  const amxd_action_t reason,
124  amxd_action_fn_t fn,
125  bool enable) {
126  amxd_dm_cb_t* cb = NULL;
127  when_null(object, exit);
128  when_null(fn, exit);
129 
130  cb = amxd_get_action(&object->cb_fns, reason, fn);
131  when_null(cb, exit);
132 
133  cb->enable = enable;
134 
135 exit:
136  return;
137 }
138 
140  const amxd_action_t reason,
141  amxd_action_fn_t fn) {
142  bool has_cb = false;
143  amxd_dm_cb_t* cb = NULL;
144  when_null(object, exit);
145  when_null(fn, exit);
146 
147  cb = amxd_get_action(&object->cb_fns, reason, fn);
148  when_null(cb, exit);
149 
150  has_cb = true;
151 
152 exit:
153  return has_cb;
154 }
155 
156 bool amxd_object_has_action(amxd_object_t* const object, const amxd_action_t reason) {
157  bool has_cb = false;
158  amxd_dm_cb_t* cb = NULL;
159  amxd_object_t* super = object;
160  when_null(object, exit);
161 
162  while(super != NULL) {
163  cb = amxd_get_action(&object->cb_fns, reason, NULL);
164  if(cb != NULL) {
165  has_cb = true;
166  break;
167  }
168 
170  super = amxd_object_get_parent(super);
171  } else {
172  super = (super->derived_from.llist == NULL) ?
173  NULL :
174  amxc_container_of(super->derived_from.llist,
176  derived_objects);
177  }
178 
179  }
180 
181 exit:
182  return has_cb;
183 }
184 
186  const amxd_action_t reason,
187  amxd_action_fn_t fn) {
188  amxd_dm_cb_t* cb = NULL;
189  when_null(object, exit);
190  when_null(fn, exit);
191 
192  cb = amxd_get_action(&object->cb_fns, reason, fn);
193 
194 exit:
195  if(cb != NULL) {
196  return cb->priv;
197  } else {
198  return NULL;
199  }
200 }
201 
203  const amxd_action_t reason,
204  amxd_action_fn_t fn,
205  void* data) {
206  amxd_dm_cb_t* cb = NULL;
207  when_null(object, exit);
208  when_null(fn, exit);
209 
210  cb = amxd_get_action(&object->cb_fns, reason, fn);
211  if(cb != NULL) {
212  cb->priv = data;
213  }
214 
215 exit:
216  return;
217 }
218 
219 bool amxd_action_can_add_function(uint32_t func_attrs,
220  const amxd_object_type_t type,
221  amxd_dm_access_t access,
222  bool template_info) {
223  bool retval = false;
224  when_true((type == amxd_object_instance) &&
225  !IS_BIT_SET(func_attrs, amxd_fattr_instance), exit);
226  when_true((type == amxd_object_template) &&
227  !IS_BIT_SET(func_attrs, amxd_fattr_template) &&
228  !template_info, exit);
229  switch(access) {
231  when_true(IS_BIT_SET(func_attrs, amxd_fattr_private) ||
232  IS_BIT_SET(func_attrs, amxd_fattr_protected), exit);
233  break;
235  when_true(IS_BIT_SET(func_attrs, amxd_fattr_private), exit);
236  break;
238  break;
239  }
240 
241  retval = true;
242 
243 exit:
244  return retval;
245 }
246 
247 bool amxd_action_can_add_param(uint32_t param_attrs,
248  const amxd_object_type_t type,
249  amxd_dm_access_t access,
250  bool template_info) {
251  bool retval = false;
252  when_true((type == amxd_object_instance) &&
253  !IS_BIT_SET(param_attrs, amxd_pattr_instance), exit);
254  when_true((type == amxd_object_template) &&
255  !IS_BIT_SET(param_attrs, amxd_pattr_template) &&
256  !template_info, exit);
257  switch(access) {
259  when_true(IS_BIT_SET(param_attrs, amxd_pattr_private) ||
260  IS_BIT_SET(param_attrs, amxd_pattr_protected), exit);
261  break;
263  when_true(IS_BIT_SET(param_attrs, amxd_pattr_private), exit);
264  break;
266  break;
267  }
268 
269  retval = true;
270 
271 exit:
272  return retval;
273 }
274 
275 bool amxd_action_can_access_object(uint32_t object_attrs,
276  amxd_dm_access_t access) {
277  bool retval = false;
278  switch(access) {
280  when_true(IS_BIT_SET(object_attrs, amxd_oattr_private) ||
281  IS_BIT_SET(object_attrs, amxd_oattr_protected), exit);
282  break;
284  when_true(IS_BIT_SET(object_attrs, amxd_oattr_private), exit);
285  break;
287  break;
288  }
289 
290  retval = true;
291 
292 exit:
293  return retval;
294 }
295 
297  amxd_dm_access_t access) {
298  uint32_t obj_attrs = amxd_object_get_attrs(object);
299  bool retval = amxd_action_can_access_object(obj_attrs, access);
300 
301  if(retval) {
302  amxd_object_t* parent = amxd_object_get_parent(object);
303  if(parent != NULL) {
304  retval = amxd_action_verify_access(parent, access);
305  }
306  }
307 
308  return retval;
309 }
Ambiorix Data Model Default actions header file.
#define IS_BIT_SET(b, f)
Definition: amxd_common.h:66
Ambiorix Data Model RPC methods API header file.
Ambiorix Data Model API header file.
bool amxd_object_has_action(amxd_object_t *const object, const amxd_action_t reason)
bool amxd_action_can_add_function(uint32_t func_attrs, const amxd_object_type_t type, amxd_dm_access_t access, bool template_info)
bool amxd_action_can_access_object(uint32_t object_attrs, amxd_dm_access_t access)
bool amxd_object_has_action_cb(amxd_object_t *const object, const amxd_action_t reason, amxd_action_fn_t fn)
amxd_status_t amxd_object_add_action_cb(amxd_object_t *const object, const amxd_action_t reason, amxd_action_fn_t fn, void *priv)
bool amxd_action_verify_access(amxd_object_t *object, amxd_dm_access_t access)
void amxd_object_enable_action_cb(const amxd_object_t *const object, const amxd_action_t reason, amxd_action_fn_t fn, bool enable)
void amxd_object_set_action_cb_data(amxd_object_t *const object, const amxd_action_t reason, amxd_action_fn_t fn, void *data)
void * amxd_object_get_action_cb_data(amxd_object_t *const object, const amxd_action_t reason, amxd_action_fn_t fn)
amxd_status_t amxd_object_remove_action_cb(amxd_object_t *const object, const amxd_action_t reason, amxd_action_fn_t fn)
bool amxd_action_can_add_param(uint32_t param_attrs, const amxd_object_type_t type, amxd_dm_access_t access, bool template_info)
amxd_dm_cb_t *PRIVATE amxd_get_action(const amxc_llist_t *const cb_fns, const amxd_action_t reason, amxd_action_fn_t fn)
Definition: amxd_common.c:99
@ amxd_pattr_template
Definition: amxd_types.h:355
@ amxd_pattr_private
Definition: amxd_types.h:357
@ amxd_pattr_instance
Definition: amxd_types.h:356
@ amxd_pattr_protected
Definition: amxd_types.h:364
amxd_status_t(* amxd_action_fn_t)(amxd_object_t *const object, amxd_param_t *const param, amxd_action_t reason, const amxc_var_t *const args, amxc_var_t *const retval, void *priv)
Definition: amxd_types.h:150
enum _amxd_action amxd_action_t
enum _amxd_status amxd_status_t
@ amxd_status_ok
Definition: amxd_types.h:78
@ amxd_status_unknown_error
Definition: amxd_types.h:79
enum _amxd_object_type amxd_object_type_t
The different object types.
enum _amxd_dm_access amxd_dm_access_t
Access level.
@ amxd_fattr_template
Definition: amxd_types.h:311
@ amxd_fattr_private
Definition: amxd_types.h:313
@ amxd_fattr_protected
Definition: amxd_types.h:317
@ amxd_fattr_instance
Definition: amxd_types.h:312
@ amxd_oattr_protected
Definition: amxd_types.h:210
@ amxd_oattr_private
Definition: amxd_types.h:202
@ amxd_dm_access_private
Definition: amxd_types.h:141
@ amxd_dm_access_protected
Definition: amxd_types.h:139
@ 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_object_get_parent(const amxd_object_t *const object)
Get the parent 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
uint32_t amxd_object_get_attrs(const amxd_object_t *const object)
Gets the set attributes of an object.
Definition: amxd_object.c:334
amxc_llist_it_t it
Definition: amxd_types.h:158
amxd_action_t reason
Definition: amxd_types.h:160
void * priv
Definition: amxd_types.h:161
amxd_action_fn_t fn
Definition: amxd_types.h:159
amxc_llist_it_t derived_from
Definition: amxd_types.h:249
amxc_llist_t cb_fns
Definition: amxd_types.h:253
static amxd_status_t status