libamxd  6.4.1
Data Model Manager
amxd_dm_add_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 
71  amxd_object_t* object,
72  const char* rel_path,
73  amxc_llist_t* paths,
74  amxc_var_t* args) {
76  amxd_dm_t* dm = amxd_object_get_dm(object);
77  amxd_path_t rpath;
78  amxd_path_init(&rpath, NULL);
79 
80  if((rel_path != NULL) && (*rel_path != 0)) {
81  amxd_path_setf(&rpath, false, "%s", rel_path);
82  }
83 
84  if((rel_path != NULL) && (*rel_path != 0) && amxd_path_is_search_path(&rpath)) {
85  retval = amxd_object_resolve_pathf(object, paths, "%s", rel_path);
86  when_failed(retval, exit);
87  amxc_llist_for_each(it, (paths)) {
88  amxc_string_t* path = amxc_string_from_llist_it(it);
89  object = amxd_dm_findf(dm, "%s", amxc_string_get(path, 0));
90  when_true_status(amxd_object_get_type(object) != amxd_object_template,
91  exit,
92  retval = amxd_status_invalid_type);
93  amxd_trans_select_object(transaction, object);
95  }
96  } else {
97  if((rel_path != NULL) && (*rel_path != 0)) {
98  object = amxd_object_findf(object, "%s", rel_path);
99  when_null_status(object, exit, retval = amxd_status_object_not_found);
100  }
101  when_true_status(amxd_object_get_type(object) != amxd_object_template,
102  exit,
103  retval = amxd_status_invalid_type);
104 
105  amxd_trans_select_object(transaction, object);
106  amxd_trans_add_action(transaction, action_object_add_inst, args);
107  }
108 
109  retval = amxd_status_ok;
110 
111 exit:
112  amxd_path_clean(&rpath);
113  return retval;
114 }
115 
117  UNUSED amxd_function_t* func,
118  amxc_var_t* args,
119  amxc_var_t* ret) {
121  amxc_var_t* var_rel_path = GET_ARG(args, "rel_path");
122  const char* rel_path = GET_CHAR(var_rel_path, NULL);
123  amxc_llist_t paths;
124  amxd_trans_t transaction;
125  amxd_dm_t* dm = amxd_object_get_dm(object);
126 
127  amxc_var_take_it(var_rel_path);
129  amxd_trans_init(&transaction);
130  amxc_llist_init(&paths);
131  amxc_var_clean(ret);
132 
133  retval = build_transaction(&transaction, object, rel_path, &paths, args);
134  when_failed(retval, exit);
135 
136  retval = amxd_trans_apply(&transaction, dm);
137 
138  if(retval == amxd_status_ok) {
139  // for backwards compatibility reasons,
140  // when only one single instance is added (no search path or wildcard path)
141  // return a htable variant
142  if(amxc_llist_is_empty(&paths)) {
143  amxc_var_copy(ret,
144  amxc_var_get_path(&transaction.retvals,
145  "1",
146  AMXC_VAR_FLAG_DEFAULT));
147  } else {
148  // otherwise (new), return an array of htable variants.
149  // Each entry is a new added instance.
150  uint32_t transaction_index = 1;
151  amxc_var_set_type(ret, AMXC_VAR_ID_LIST);
152  amxc_llist_for_each(it, &paths) {
153  amxc_var_t* data = amxc_var_get_index(&transaction.retvals, transaction_index, AMXC_VAR_FLAG_DEFAULT);
154  amxc_var_set_index(ret, -1, data, AMXC_VAR_FLAG_DEFAULT);
155  transaction_index += 1;
156  }
157  }
158  }
159 
160 exit:
161  amxc_var_delete(&var_rel_path);
162  amxc_llist_clean(&paths, amxc_string_list_it_free);
163  amxd_trans_clean(&transaction);
164  return retval;
165 }
Ambiorix Data Model API header file.
amxd_object_t * amxd_dm_findf(amxd_dm_t *const dm, const char *abs_path,...) __attribute__((format(printf
amxd_status_t amxd_object_func_add(amxd_object_t *object, UNUSED amxd_function_t *func, amxc_var_t *args, amxc_var_t *ret)
static amxd_status_t build_transaction(amxd_trans_t *transaction, amxd_object_t *object, const char *rel_path, amxc_llist_t *paths, amxc_var_t *args)
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_add_inst
Definition: amxd_types.h:123
enum _amxd_status amxd_status_t
@ amxd_status_invalid_type
Definition: amxd_types.h:90
@ 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_object_template
Definition: amxd_types.h:183
amxd_object_t * amxd_object_findf(amxd_object_t *object, const char *rel_path,...) __attribute__((format(printf
Find an object in the data model tree, starting from an object.
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.
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_path_init(amxd_path_t *path, const char *object_path)
Initializes an amxd_path_t structure.
Definition: amxd_path.c:328
static bool amxd_path_is_search_path(amxd_path_t *path)
Checks if the path is a search path.
Definition: amxd_path.h:544
void amxd_path_clean(amxd_path_t *path)
Cleans an amxd_path_t structure.
Definition: amxd_path.c:351
amxd_status_t amxd_path_setf(amxd_path_t *path, bool add_dot, const char *obj_path,...) __attribute__((format(printf
Sets or replaces the path contained in the amxd_path_t structure.
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
static amxd_dm_t dm