libamxb  4.8.2
Bus Agnostic C API
amxb_ba_op_set_multiple.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 <stdio.h>
61 #include <errno.h>
62 #include <fcntl.h>
63 #include <string.h>
64 
65 #include <amxc/amxc.h>
66 #include <amxp/amxp.h>
67 
68 #include <amxd/amxd_common.h>
69 #include <amxd/amxd_dm.h>
70 #include <amxd/amxd_path.h>
71 #include <amxd/amxd_object.h>
72 
73 #include <amxb/amxb_be_intf.h>
74 #include <amxb/amxb.h>
75 
76 #include "amxb_priv.h"
77 
78 static void amxb_filter_params(amxc_var_t* current,
79  amxc_var_t* parameters,
80  amxc_var_t* oparameters) {
81  const amxc_htable_t* htparams = amxc_var_constcast(amxc_htable_t, parameters);
82  const amxc_htable_t* htoparams = amxc_var_constcast(amxc_htable_t, oparameters);
83 
84  amxc_var_for_each(object, current) {
85  amxc_var_for_each(param, object) {
86  const char* pname = amxc_var_key(param);
87  if(amxc_htable_contains(htparams, pname) ||
88  amxc_htable_contains(htoparams, pname)) {
89  continue;
90  }
91  amxc_var_delete(&param);
92  }
93  }
94 }
95 
96 static void amxb_add_rollback(amxc_var_t* current,
97  amxc_var_t* rollback) {
98 
99  amxc_var_for_each(object, current) {
100  const char* path = amxc_var_key(object);
101  amxc_var_t* data = amxc_var_get_pathf(rollback, AMXC_VAR_FLAG_DEFAULT, "'%s'", path);
102  if(data != NULL) {
103  amxc_var_delete(&object);
104  continue;
105  }
106  amxc_var_take_it(object);
107  amxc_var_set_key(rollback, path, object, AMXC_VAR_FLAG_DEFAULT);
108  }
109 }
110 
111 static void amxb_set_result(const char* req_path,
112  amxc_var_t* ret,
113  amxc_var_t* sub_ret,
114  int rv) {
115  amxc_var_for_each(result, sub_ret) {
116  amxc_var_t* rp = amxc_var_add(amxc_htable_t, ret, NULL);
117  amxc_var_add_key(cstring_t, rp, "path", req_path);
118  amxc_var_add_key(uint32_t, rp, "status", rv);
119  if(!amxc_htable_is_empty(amxc_var_constcast(amxc_htable_t, result))) {
120  amxc_var_take_it(result);
121  amxc_var_set_key(rp, "result", result, AMXC_VAR_FLAG_DEFAULT);
122  }
123  }
124 }
125 
127  amxc_var_t* rollback) {
128  amxc_var_t ret;
129  amxc_var_init(&ret);
130 
131  amxc_var_for_each(params, rollback) {
132  const char* path = amxc_var_key(params);
133  amxc_var_clean(&ret);
134  amxb_set(bus_ctx, path, params, &ret, 5);
135  amxc_var_delete(&params);
136  }
137 
138  amxc_var_clean(&ret);
139 }
140 
142  uint32_t flags,
143  amxc_var_t* req_paths,
144  amxc_var_t* ret,
145  int timeout) {
146  int retval = amxd_status_unknown_error;
147  amxc_var_t sub_ret;
148  amxc_var_t rollback;
149  amxc_var_t current;
150  int count = 0;
151  bool allow_partial = ((flags & AMXB_FLAG_PARTIAL) != 0);
152  amxc_var_init(&sub_ret);
153  amxc_var_init(&rollback);
154  amxc_var_init(&current);
155  amxc_var_set_type(ret, AMXC_VAR_ID_LIST);
156  amxc_var_set_type(&rollback, AMXC_VAR_ID_HTABLE);
157 
158  when_false(amxc_var_type_of(req_paths) == AMXC_VAR_ID_LIST, exit);
159 
160  count = amxc_llist_size(amxc_var_constcast(amxc_llist_t, req_paths));
161 
162  retval = amxd_status_ok;
163 
164  amxc_var_for_each(req_path, req_paths) {
165  int rv = 0;
166  const char* object = GET_CHAR(req_path, "path");
167  amxc_var_t* params = GET_ARG(req_path, "parameters");
168  amxc_var_t* oparams = GET_ARG(req_path, "oparameters");
169  if(!allow_partial && (count > 1)) {
170  amxc_var_clean(&current);
171  amxb_get(bus_ctx, object, 0, &current, 5);
172  amxb_filter_params(GETI_ARG(&current, 0), params, oparams);
173  }
174  rv = amxb_set_impl(bus_ctx, object, flags, params, oparams, &sub_ret, timeout);
175 
176  if((rv != 0) && !allow_partial) {
177  // reset return variant, only keep failed set
178  amxc_var_set_type(ret, AMXC_VAR_ID_LIST);
179  }
180 
181  // add results
182  amxb_set_result(object, ret, &sub_ret, rv);
183 
184  // what is next? continue or fail.
185  if(rv != 0) {
186  if(!allow_partial) {
187  if(count > 1) {
188  amxb_set_rollback(bus_ctx, &rollback);
189  }
190  retval = rv;
191  break;
192  }
193  } else {
194  if(!allow_partial && (count > 1)) {
195  amxb_add_rollback(GETI_ARG(&current, 0), &rollback);
196  }
197  }
198 
199  amxc_var_clean(&sub_ret);
200  }
201 
202 exit:
203  amxc_var_clean(&current);
204  amxc_var_clean(&rollback);
205  amxc_var_clean(&sub_ret);
206  return retval;
207 }
Ambiorix bus agnostic API header file.
static void amxb_filter_params(amxc_var_t *current, amxc_var_t *parameters, amxc_var_t *oparameters)
static void amxb_set_rollback(amxb_bus_ctx_t *const bus_ctx, amxc_var_t *rollback)
static void amxb_add_rollback(amxc_var_t *current, amxc_var_t *rollback)
static void amxb_set_result(const char *req_path, amxc_var_t *ret, amxc_var_t *sub_ret, int rv)
Ambiorix Bus Backend Interface.
#define AMXB_FLAG_PARTIAL
int PRIVATE amxb_set_impl(amxb_bus_ctx_t *const bus_ctx, const char *object, uint32_t flags, amxc_var_t *values, amxc_var_t *ovalues, amxc_var_t *ret, int timeout)
int amxb_set(amxb_bus_ctx_t *const bus_ctx, const char *object, amxc_var_t *values, amxc_var_t *ret, int timeout)
Sets parameter values of one single object or of multiple instance objects.
int amxb_get(amxb_bus_ctx_t *const bus_ctx, const char *object, int32_t depth, amxc_var_t *ret, int timeout)
Fetches one or more objects or a single parameter.
int amxb_set_multiple(amxb_bus_ctx_t *const bus_ctx, uint32_t flags, amxc_var_t *req_paths, amxc_var_t *ret, int timeout)
Sets parameter values for multiple objects (request paths)
static amxb_bus_ctx_t * bus_ctx
Definition: test_amxb_e2e.c:84