libamxd  6.4.1
Data Model Manager
amxd_dm_del_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_assert.h"
67 #include "amxd_object_priv.h"
68 #include "amxd_dm_priv.h"
69 
70 typedef struct _del_data {
71  amxc_var_t* retval;
72  bool key_path;
74 
75 static bool amxd_object_del_rv_filter(amxd_object_t* const object,
76  UNUSED int32_t depth,
77  UNUSED void* priv) {
78  amxd_object_t* parent = amxd_object_get_parent(object);
80  return (amxd_object_get_type(object) == amxd_object_instance);
81  }
82  return true;
83 }
84 
85 static void amxd_object_del_rv(amxd_object_t* const object,
86  UNUSED int32_t depth,
87  void* priv) {
88  del_data_t* data = (del_data_t*) priv;
89  amxc_var_t* retval = data->retval;
90  uint32_t flags = data->key_path ? AMXD_OBJECT_NAMED : AMXD_OBJECT_INDEXED;
91  char* path = amxd_object_get_path(object, flags | AMXD_OBJECT_TERMINATE);
92 
93  amxc_var_add(cstring_t, retval, path);
94  free(path);
95 }
96 
98  amxc_var_t* args) {
99  bool is_valid = false;
100  const char* rel_path = GET_CHAR(args, "rel_path");
101  char* obj_path = amxd_object_get_path(object, AMXD_OBJECT_TERMINATE |
103  amxc_llist_t paths;
105  amxd_dm_t* dm = amxd_object_get_dm(object);
106 
107  amxc_llist_init(&paths);
108 
109  is_valid = amxd_object_is_supported(object, rel_path);
110  when_false(is_valid, exit);
111 
112  is_valid = false;
113  retval = amxd_object_resolve_pathf(object, &paths, "%s", rel_path);
114  when_failed(retval, exit);
115  when_true(amxc_llist_is_empty(&paths), exit);
116 
117  is_valid = true;
118  amxc_llist_for_each(it, (&paths)) {
119  amxc_string_t* path = amxc_string_from_llist_it(it);
120  object = amxd_dm_findf(dm, "%s", amxc_string_get(path, 0));
121  if((amxd_object_get_type(object) != amxd_object_template) &&
123  is_valid = false;
124  break;
125  }
126  }
127 
128 exit:
129  free(obj_path);
130  amxc_llist_clean(&paths, amxc_string_list_it_free);
131 
132  return is_valid;
133 }
134 
136  bool key_path,
137  amxd_trans_t* transaction,
138  amxc_var_t* args,
139  amxc_var_t* retval) {
140  amxc_var_t* var_index = GET_ARG(args, "index");
141  uint32_t index = 0;
142  const char* name = NULL;
143  del_data_t data = {
144  .retval = retval,
145  .key_path = key_path
146  };
147 
149  if(var_index == NULL) {
150  amxc_var_add_key(uint32_t, args, "index", amxd_object_get_index(object));
151  } else {
152  amxc_var_set(uint32_t, var_index, amxd_object_get_index(object));
153  }
154  object = amxd_object_get_parent(object);
155  }
156 
157  index = GET_UINT32(args, "index");
158  name = GET_CHAR(args, "name");
159 
160  amxd_trans_select_object(transaction, object);
161  amxd_trans_add_action(transaction, action_object_del_inst, args);
162 
163  object = amxd_object_get_instance(object, name, index);
168  INT32_MAX,
169  &data);
170 }
171 
173  UNUSED amxd_function_t* func,
174  amxc_var_t* args,
175  amxc_var_t* ret) {
176 
178  amxc_var_t* var_rel_path = GET_ARG(args, "rel_path");
179  const char* rel_path = GET_CHAR(var_rel_path, NULL);
180  amxc_llist_t paths;
181  amxd_trans_t transaction;
182  amxd_dm_t* dm = amxd_object_get_dm(object);
183 
185  amxd_trans_init(&transaction);
186  amxc_llist_init(&paths);
187  amxc_var_set_type(ret, AMXC_VAR_ID_LIST);
188 
189  if(!amxd_del_is_path_valid(object, args)) {
191  goto exit;
192  }
193 
194  amxc_var_take_it(var_rel_path);
195  if((rel_path != NULL) && (*rel_path != 0)) {
196  retval = amxd_object_resolve_pathf(object, &paths, "%s", rel_path);
197  when_failed(retval, exit);
198  amxc_llist_for_each(it, (&paths)) {
199  amxc_string_t* path = amxc_string_from_llist_it(it);
200  object = amxd_dm_findf(dm, "%s", amxc_string_get(path, 0));
201  amxd_add_del_instance(object, false, &transaction, args, ret);
202  }
203  } else {
204  amxd_add_del_instance(object, false, &transaction, args, ret);
205  }
206 
207  retval = amxd_trans_apply(&transaction, dm);
208 
209 exit:
210  if(retval != amxd_status_ok) {
211  amxc_var_clean(ret);
212  }
213  amxc_var_delete(&var_rel_path);
214  amxc_llist_clean(&paths, amxc_string_list_it_free);
215  amxd_trans_clean(&transaction);
216  return retval;
217 }
Ambiorix Data Model API header file.
amxd_object_t * amxd_dm_findf(amxd_dm_t *const dm, const char *abs_path,...) __attribute__((format(printf
static void amxd_add_del_instance(amxd_object_t *object, bool key_path, amxd_trans_t *transaction, amxc_var_t *args, amxc_var_t *retval)
struct _del_data del_data_t
static void amxd_object_del_rv(amxd_object_t *const object, UNUSED int32_t depth, void *priv)
static bool amxd_del_is_path_valid(amxd_object_t *object, amxc_var_t *args)
amxd_status_t amxd_object_func_del(amxd_object_t *object, UNUSED amxd_function_t *func, amxc_var_t *args, amxc_var_t *ret)
static bool amxd_object_del_rv_filter(amxd_object_t *const object, UNUSED int32_t depth, UNUSED void *priv)
void PRIVATE amxd_def_funcs_remove_args(amxc_var_t *args)
Ambiorix Data Model API header file.
Ambiorix path API header file.
Ambiorix Data Model API header file.
@ action_object_del_inst
Definition: amxd_types.h:124
enum _amxd_status amxd_status_t
@ 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_INDEXED
Name and path format flag - use index for instance objects.
Definition: amxd_object.h:176
#define AMXD_OBJECT_NAMED
Name and path format flag - default behavior, use name for instance objects.
Definition: amxd_object.h:164
#define AMXD_OBJECT_TERMINATE
Path format flag - when set the object path is terminated with a dot.
Definition: amxd_object.h:214
@ 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.
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_dm_t * amxd_object_get_dm(const amxd_object_t *const object)
Get the data model.
amxd_object_t amxd_status_t amxd_object_resolve_pathf(amxd_object_t *object, amxc_llist_t *paths, const char *rel_path,...) __attribute__((format(printf
Resolves a search path into a list of matching object paths.
bool amxd_object_is_supported(amxd_object_t *object, const char *rel_path)
Checks if a path is in the supported data model.
amxd_object_t * amxd_object_get_instance(const amxd_object_t *object, const char *name, uint32_t index)
Get an instance of the template object.
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
amxd_status_t amxd_trans_add_action(amxd_trans_t *const trans, const amxd_action_t reason, const amxc_var_t *data)
Adds an action to a transaction.
void amxd_trans_clean(amxd_trans_t *const trans)
Cleans the transaction object.
amxd_status_t amxd_trans_apply(amxd_trans_t *const trans, amxd_dm_t *const dm)
Applies all previously added actions.
amxd_status_t amxd_status_t amxd_trans_select_object(amxd_trans_t *trans, const amxd_object_t *const object)
Selects an object using an object pointer.
amxd_status_t amxd_trans_init(amxd_trans_t *const trans)
Initializes a transaction object.
RPC method structure.
Definition: amxd_types.h:341
amxc_var_t * retval
static amxd_dm_t dm