libamxd  6.4.1
Data Model Manager
amxd_function_deferred.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 <string.h>
61 #include <ctype.h>
62 
63 #include <amxc/amxc.h>
64 
65 #include <amxp/amxp_signal.h>
66 #include <amxp/amxp_slot.h>
67 
68 #include <amxd/amxd_common.h>
69 #include <amxd/amxd_types.h>
70 #include <amxd/amxd_object.h>
71 #include <amxd/amxd_function.h>
72 
73 #include "amxd_priv.h"
74 #include "amxd_assert.h"
75 #include "amxd_dm_priv.h"
76 
77 static amxc_llist_t deferred;
78 static uint64_t g_call_id;
79 
80 static void amxd_delete_deferred(amxc_llist_it_t* it) {
81  amxd_deferred_ctx_t* data = amxc_container_of(it, amxd_deferred_ctx_t, it);
82  amxc_llist_it_take(it);
83  amxc_llist_it_take(&data->dm_it);
84  free(data);
85 }
86 
87 static void amxd_cancel_deferred(amxd_deferred_ctx_t* call, amxc_var_t* sig_data) {
88  if(call->cancel != NULL) {
89  call->cancel(call->call_id, call->called_priv);
90  }
91  if(call->cb != NULL) {
92  call->cb(sig_data, call->caller_priv);
93  }
94 }
95 
97  amxd_deferred_ctx_t* data = NULL;
98  amxc_llist_for_each(it, &deferred) {
99  data = amxc_container_of(it, amxd_deferred_ctx_t, it);
100  if(data->call_id == call_id) {
101  break;
102  }
103  data = NULL;
104  }
105 
106  return data;
107 }
108 
110  amxc_var_t sig_data;
111 
112  amxc_var_init(&sig_data);
113  amxc_var_set_type(&sig_data, AMXC_VAR_ID_HTABLE);
114  amxc_var_add_new_key(&sig_data, "retval");
115  amxc_var_add_key(uint32_t, &sig_data, "status", amxd_status_unknown_error);
116 
117  amxc_llist_for_each(it, &dm->deferred) {
118  amxd_deferred_ctx_t* call = amxc_container_of(it, amxd_deferred_ctx_t, dm_it);
119  amxd_cancel_deferred(call, &sig_data);
120  amxd_delete_deferred(&call->it);
121  }
122 
123  amxc_var_clean(&sig_data);
124 }
125 
127  uint64_t* call_id,
128  amxc_var_t* const ret,
129  amxd_deferred_cancel_t cancel_fn,
130  void* priv) {
132  amxd_deferred_ctx_t* call = NULL;
133 
134  when_null(func, exit);
135  when_null(ret, exit);
136  when_null(call_id, exit)
137 
138  call = (amxd_deferred_ctx_t*) calloc(1, sizeof(amxd_deferred_ctx_t));
139  when_null(call, exit);
140 
141  call->call_id = g_call_id++;
142  call->called_priv = priv;
143  call->cancel = cancel_fn;
144  amxc_llist_append(&deferred, &call->it);
145  amxc_var_set(uint64_t, ret, call->call_id);
146  *call_id = call->call_id;
147 
149 
150 exit:
151  return status;
152 }
153 
156  amxc_var_t sig_data;
157 
158  amxc_var_init(&sig_data);
159  when_null(call, exit);
160 
161  amxc_var_set_type(&sig_data, AMXC_VAR_ID_HTABLE);
162  amxc_var_add_new_key(&sig_data, "retval");
163  amxc_var_add_key(uint32_t, &sig_data, "status", amxd_status_unknown_error);
164 
165  amxd_cancel_deferred(call, &sig_data);
166  amxd_delete_deferred(&call->it);
167 
168 exit:
169  amxc_var_clean(&sig_data);
170  return;
171 }
172 
174  void* data = NULL;
176 
177  when_null(call, exit);
178  data = call->called_priv;
179 
180 exit:
181  return data;
182 }
183 
185  amxp_deferred_fn_t cb,
186  void* priv) {
189 
190  when_null(call, exit);
191 
192  call->cb = cb;
193  call->caller_priv = priv;
194 
195  rv = amxd_status_ok;
196 
197 exit:
198  return rv;
199 }
200 
203  amxc_var_t* out_args,
204  amxc_var_t* ret,
205  bool now) {
207  amxc_var_t sig_data;
209 
210  amxc_var_init(&sig_data);
211 
212  when_null(call, exit);
213  when_null_status(call->cb, exit, rv = amxd_status_ok);
214 
215  amxc_var_set_type(&sig_data, AMXC_VAR_ID_HTABLE);
216  if(ret != NULL) {
217  amxc_var_set_key(&sig_data, "retval", ret, AMXC_VAR_FLAG_COPY);
218  } else {
219  amxc_var_add_new_key(&sig_data, "retval");
220  }
221  if(out_args != NULL) {
222  amxc_var_set_key(&sig_data, "args", out_args, AMXC_VAR_FLAG_COPY);
223  }
224  amxc_var_add_key(uint32_t, &sig_data, "status", status);
225 
226  if(now) {
227  call->cb(&sig_data, call->caller_priv);
228  } else {
229  amxp_sigmngr_deferred_call(NULL, call->cb, &sig_data, call->caller_priv);
230  }
231 
232  rv = amxd_status_ok;
233 
234 exit:
235  if(call != NULL) {
236  amxd_delete_deferred(&call->it);
237  }
238  amxc_var_clean(&sig_data);
239  return rv;
240 }
241 
244  amxc_var_t* out_args,
245  amxc_var_t* ret) {
246  return amxd_function_deferred_done_impl(call_id, status, out_args, ret, false);
247 }
248 
251  amxc_var_t* out_args,
252  amxc_var_t* ret) {
253  return amxd_function_deferred_done_impl(call_id, status, out_args, ret, true);
254 }
255 
256 
257 static CONSTRUCTOR void amxd_function_deferred_init(void) {
258  amxc_llist_init(&deferred);
259 }
Ambiorix Data Model RPC methods API header file.
void(* amxd_deferred_cancel_t)(uint64_t call_id, void *const priv)
Definition: amxd_function.h:77
static CONSTRUCTOR void amxd_function_deferred_init(void)
static amxc_llist_t deferred
static void amxd_delete_deferred(amxc_llist_it_t *it)
static uint64_t g_call_id
static amxd_status_t amxd_function_deferred_done_impl(uint64_t call_id, amxd_status_t status, amxc_var_t *out_args, amxc_var_t *ret, bool now)
void amxd_dm_cancel_deferred(amxd_dm_t *dm)
static void amxd_cancel_deferred(amxd_deferred_ctx_t *call, amxc_var_t *sig_data)
amxd_deferred_ctx_t * amxd_find_deferred(uint64_t call_id)
Ambiorix Data Model API header file.
enum _amxd_status amxd_status_t
@ amxd_status_invalid_function
Definition: amxd_types.h:84
@ amxd_status_ok
Definition: amxd_types.h:78
@ amxd_status_unknown_error
Definition: amxd_types.h:79
amxd_status_t amxd_function_deferred_call_done(uint64_t call_id, amxd_status_t status, amxc_var_t *out_args, amxc_var_t *ret)
Finishes a deferred method and removes the deferred function context.
void amxd_function_deferred_remove(uint64_t call_id)
Removes a deferred RPC context.
amxd_status_t amxd_function_defer(const amxd_function_t *const func, uint64_t *call_id, amxc_var_t *const ret, amxd_deferred_cancel_t cancel_fn, void *priv)
Creates a deferred RPC context.
amxd_status_t amxd_function_deferred_done(uint64_t call_id, amxd_status_t status, amxc_var_t *out_args, amxc_var_t *ret)
Finishes a deferred method and removes the deferred function context.
amxd_status_t amxd_function_set_deferred_cb(uint64_t call_id, amxp_deferred_fn_t cb, void *priv)
Sets a callback function to get the result of the deferred call.
void * amxd_function_deferred_get_priv(uint64_t call_id)
Gets the callee private data of an deferred function.
amxp_deferred_fn_t cb
Definition: amxd_dm_priv.h:65
amxc_llist_it_t it
Definition: amxd_dm_priv.h:63
amxc_llist_it_t dm_it
Definition: amxd_dm_priv.h:64
amxd_deferred_cancel_t cancel
Definition: amxd_dm_priv.h:66
amxc_llist_t deferred
Definition: amxd_types.h:264
RPC method structure.
Definition: amxd_types.h:341
static amxd_dm_t dm
static amxd_status_t status
static uint64_t call_id