libamxd  6.4.1
Data Model Manager
amxd_parameter_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 #include <stdlib.h>
56 
57 #include <amxc/amxc.h>
58 #include <amxp/amxp_signal.h>
59 
60 #include <amxd/amxd_types.h>
61 #include <amxd/amxd_object.h>
62 #include <amxd/amxd_parameter.h>
63 
64 #include "amxd_priv.h"
65 #include "amxd_assert.h"
66 
68  const amxd_action_t reason,
70  void* priv) {
72  amxd_dm_cb_t* cb = NULL;
73  when_null(param, exit);
74  when_null(fn, exit);
75 
76  cb = amxd_get_action(&param->cb_fns, reason, fn);
77  if((cb != NULL) && (cb->priv == priv)) {
78  goto exit;
79  }
80 
81  cb = (amxd_dm_cb_t*) calloc(1, sizeof(amxd_dm_cb_t));
82  when_null(cb, exit);
83 
84  cb->fn = fn;
85  cb->reason = reason;
86  cb->priv = priv;
87  cb->enable = true;
88 
89  amxc_llist_append(&param->cb_fns, &cb->it);
91 
92 exit:
93  return status;
94 }
95 
97  const amxd_action_t reason,
98  amxd_action_fn_t fn) {
100  amxd_dm_cb_t* cb = NULL;
101  when_null(param, exit);
102  when_null(fn, exit);
103 
104  cb = amxd_get_action(&param->cb_fns, reason, fn);
105  when_null(cb, exit);
106 
107  amxc_llist_it_take(&cb->it);
108  free(cb);
109 
111 
112 exit:
113  return status;
114 }
115 
116 
118  const amxd_action_t reason,
119  amxd_action_fn_t fn,
120  bool enable) {
121 
122  amxd_dm_cb_t* cb = NULL;
123  when_null(param, exit);
124  when_null(fn, exit);
125 
126  cb = amxd_get_action(&param->cb_fns, reason, fn);
127  when_null(cb, exit);
128 
129  cb->enable = enable;
130 
131 exit:
132  return;
133 }
134 
136  const amxd_action_t reason,
137  amxd_action_fn_t fn) {
138  bool has_cb = false;
139  amxd_dm_cb_t* cb = NULL;
140  when_null(param, exit);
141  when_null(fn, exit);
142 
143  cb = amxd_get_action(&param->cb_fns, reason, fn);
144  when_null(cb, exit);
145 
146  has_cb = true;
147 
148 exit:
149  return has_cb;
150 }
151 
152 bool amxd_param_has_action(amxd_param_t* const param, const amxd_action_t reason) {
153  bool has_cb = false;
154  amxd_dm_cb_t* cb = NULL;
155  amxd_object_t* super = NULL;
156  amxd_param_t* current = NULL;
157  when_null(param, exit);
158 
159  current = param;
160  super = amxd_param_get_owner(current);
161  while(current != NULL && super != NULL) {
162  cb = amxd_get_action(&current->cb_fns, reason, NULL);
163  if(cb != NULL) {
164  has_cb = true;
165  break;
166  }
167 
169  super = amxd_object_get_parent(super);
170  } else {
171  super = (super->derived_from.llist == NULL) ?
172  NULL :
173  amxc_container_of(super->derived_from.llist,
175  derived_objects);
176  }
177 
178  current = amxd_object_get_param_def(super, amxd_param_get_name(param));
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(param, exit);
190  when_null(fn, exit);
191 
192  cb = amxd_get_action(&param->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(param, exit);
208  when_null(fn, exit);
209 
210  cb = amxd_get_action(&param->cb_fns, reason, fn);
211  if(cb != NULL) {
212  cb->priv = data;
213  }
214 
215 exit:
216  return;
217 }
Ambiorix Data Model API header file.
const char * amxd_param_get_name(const amxd_param_t *const param)
amxd_object_t * amxd_param_get_owner(const amxd_param_t *const param)
bool amxd_param_has_action(amxd_param_t *const param, const amxd_action_t reason)
bool amxd_param_has_action_cb(amxd_param_t *const param, const amxd_action_t reason, amxd_action_fn_t fn)
void amxd_param_enable_action_cb(const amxd_param_t *const param, const amxd_action_t reason, amxd_action_fn_t fn, bool enable)
void amxd_param_set_action_cb_data(amxd_param_t *const param, const amxd_action_t reason, amxd_action_fn_t fn, void *data)
void * amxd_param_get_action_cb_data(amxd_param_t *const param, const amxd_action_t reason, amxd_action_fn_t fn)
amxd_status_t amxd_param_add_action_cb(amxd_param_t *const param, const amxd_action_t reason, amxd_action_fn_t fn, void *priv)
amxd_status_t amxd_param_remove_action_cb(amxd_param_t *const param, const amxd_action_t reason, amxd_action_fn_t fn)
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_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
@ 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 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:391
static amxd_status_t status