libamxd  6.4.1
Data Model Manager
amxd_dm_set_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 static void amxd_add_set(amxd_object_t* object,
71  amxd_trans_t* transaction,
72  amxc_var_t* args) {
73  amxd_trans_select_object(transaction, object);
74  amxd_trans_add_action(transaction, action_object_write, args);
75 }
76 
77 static void amxd_add_result(amxc_var_t* result,
78  amxc_var_t* retval,
80  amxc_var_for_each(r, result) {
81  const char* path = amxc_var_key(r);
82  amxc_var_t* updated_params = GET_ARG(retval, path);
83 
84  if(updated_params == NULL) {
85  updated_params = amxc_var_add_key(amxc_htable_t, retval, path, NULL);
86  }
87  amxc_var_for_each(param, GET_ARG(r, "parameters")) {
88  const char* param_name = amxc_var_key(param);
89  amxc_var_take_it(param);
90  if(status != amxd_status_ok) {
91  if(!GET_BOOL(param, "required") ||
92  (amxc_var_type_of(param) != AMXC_VAR_ID_HTABLE)) {
93  amxc_var_delete(&param);
94  continue;
95  }
96  }
97  amxc_var_set_key(updated_params, param_name, param, AMXC_VAR_FLAG_DEFAULT);
98  }
99  if(amxc_htable_is_empty(amxc_var_constcast(amxc_htable_t, updated_params))) {
100  amxc_var_delete(&updated_params);
101  }
102  }
103 }
104 
106  UNUSED amxd_function_t* func,
107  amxc_var_t* args,
108  amxc_var_t* ret) {
110  amxc_var_t* var_rel_path = GET_ARG(args, "rel_path");
111  const char* rel_path = GET_CHAR(var_rel_path, NULL);
112  amxc_llist_t paths;
113  amxd_dm_t* dm = amxd_object_get_dm(object);
114  amxd_trans_t transaction;
115 
117  amxd_trans_init(&transaction);
118  amxc_llist_init(&paths);
119  amxc_var_set_type(ret, AMXC_VAR_ID_HTABLE);
120 
121  amxc_var_take_it(var_rel_path);
122  if((rel_path != NULL) && (*rel_path != 0)) {
123  retval = amxd_object_resolve_pathf(object, &paths, "%s", rel_path);
124  when_failed(retval, exit);
125  if(amxc_llist_is_empty(&paths)) {
127  goto exit;
128  }
129  amxc_llist_for_each(it, (&paths)) {
130  amxc_string_t* path = amxc_string_from_llist_it(it);
131  object = amxd_dm_findf(dm, "%s", amxc_string_get(path, 0));
132  amxd_add_set(object, &transaction, args);
133  }
134  } else {
135  amxd_add_set(object, &transaction, args);
136  }
137 
138  retval = amxd_trans_apply(&transaction, amxd_object_get_dm(object));
139 
140  if((rel_path != NULL) && (*rel_path != 0)) {
141  uint32_t index = 1;
142  amxc_llist_for_each(it, (&paths)) {
143  amxc_var_t* result = GETI_ARG(&transaction.retvals, index);
144  amxd_add_result(result, ret, retval);
145  index += 2;
146  }
147  } else {
148  amxc_var_t* result = GETI_ARG(&transaction.retvals, 1);
149  amxd_add_result(result, ret, retval);
150  }
151 
152  if(amxc_htable_is_empty(amxc_var_constcast(amxc_htable_t, ret))) {
153  amxc_var_set_type(ret, AMXC_VAR_ID_NULL);
154  }
155 
156 exit:
157  amxc_var_delete(&var_rel_path);
158  amxc_llist_clean(&paths, amxc_string_list_it_free);
159  amxd_trans_clean(&transaction);
160  return retval;
161 }
Ambiorix Data Model API header file.
amxd_object_t * amxd_dm_findf(amxd_dm_t *const dm, const char *abs_path,...) __attribute__((format(printf
void PRIVATE amxd_def_funcs_remove_args(amxc_var_t *args)
amxd_status_t amxd_object_func_set(amxd_object_t *object, UNUSED amxd_function_t *func, amxc_var_t *args, amxc_var_t *ret)
static void amxd_add_result(amxc_var_t *result, amxc_var_t *retval, amxd_status_t status)
static void amxd_add_set(amxd_object_t *object, amxd_trans_t *transaction, amxc_var_t *args)
Ambiorix Data Model API header file.
Ambiorix path API header file.
Ambiorix Data Model API header file.
@ action_object_write
Definition: amxd_types.h:118
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_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.
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
static amxd_status_t status