2 * Copyright (C) 2016, 2017 "IoT.bzh"
3 * Author: José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #include "afb-dynapi-itf.h"
22 static inline const char *afb_dynapi_name(struct afb_dynapi *dynapi)
24 return dynapi->apiname;
27 static inline void *afb_dynapi_get_userdata(struct afb_dynapi *dynapi)
29 return dynapi->userdata;
32 static inline void afb_dynapi_set_userdata(struct afb_dynapi *dynapi, void *userdata)
34 dynapi->userdata = userdata;
38 * Send a message described by 'fmt' and following parameters
39 * to the journal for the verbosity 'level'.
41 * 'file', 'line' and 'func' are indicators of position of the code in source files
42 * (see macros __FILE__, __LINE__ and __func__).
45 * 'level' is defined by syslog standard:
46 * EMERGENCY 0 System is unusable
47 * ALERT 1 Action must be taken immediately
48 * CRITICAL 2 Critical conditions
49 * ERROR 3 Error conditions
50 * WARNING 4 Warning conditions
51 * NOTICE 5 Normal but significant condition
52 * INFO 6 Informational
53 * DEBUG 7 Debug-level messages
55 static inline void afb_dynapi_verbose(struct afb_dynapi *dynapi, int level, const char *file, int line, const char *func, const char *fmt, ...) __attribute__((format(printf, 6, 7)));
56 static inline void afb_dynapi_verbose(struct afb_dynapi *dynapi, int level, const char *file, int line, const char *func, const char *fmt, ...)
60 dynapi->itf->vverbose(dynapi, level, file, line, func, fmt, args);
63 static inline void afb_dynapi_vverbose(struct afb_dynapi *dynapi, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
65 dynapi->itf->vverbose(dynapi, level, file, line, func, fmt, args);
69 * Retrieves the common systemd's event loop of AFB
71 static inline struct sd_event *afb_dynapi_get_event_loop(struct afb_dynapi *dynapi)
73 return dynapi->itf->get_event_loop(dynapi);
77 * Retrieves the common systemd's user/session d-bus of AFB
79 static inline struct sd_bus *afb_dynapi_get_user_bus(struct afb_dynapi *dynapi)
81 return dynapi->itf->get_user_bus(dynapi);
85 * Retrieves the common systemd's system d-bus of AFB
87 static inline struct sd_bus *afb_dynapi_get_system_bus(struct afb_dynapi *dynapi)
89 return dynapi->itf->get_system_bus(dynapi);
93 * Get the root directory file descriptor. This file descriptor can
94 * be used with functions 'openat', 'fstatat', ...
96 static inline int afb_dynapi_rootdir_get_fd(struct afb_dynapi *dynapi)
98 return dynapi->itf->rootdir_get_fd(dynapi);
102 * Opens 'filename' within the root directory with 'flags' (see function openat)
103 * using the 'locale' definition (example: "jp,en-US") that can be NULL.
104 * Returns the file descriptor or -1 in case of error.
106 static inline int afb_dynapi_rootdir_open_locale(struct afb_dynapi *dynapi, const char *filename, int flags, const char *locale)
108 return dynapi->itf->rootdir_open_locale(dynapi, filename, flags, locale);
112 * Queue the job defined by 'callback' and 'argument' for being executed asynchronously
113 * in this thread (later) or in an other thread.
114 * If 'group' is not NUL, the jobs queued with a same value (as the pointer value 'group')
115 * are executed in sequence in the order of there submission.
116 * If 'timeout' is not 0, it represent the maximum execution time for the job in seconds.
117 * At first, the job is called with 0 as signum and the given argument.
118 * The job is executed with the monitoring of its time and some signals like SIGSEGV and
119 * SIGFPE. When a such signal is catched, the job is terminated and reexecuted but with
120 * signum being the signal number (SIGALRM when timeout expired).
122 * Returns 0 in case of success or -1 in case of error.
124 static inline int afb_dynapi_queue_job(struct afb_dynapi *dynapi, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
126 return dynapi->itf->queue_job(dynapi, callback, argument, group, timeout);
130 * Tells that it requires the API of "name" to exist
131 * and if 'initialized' is not null to be initialized.
132 * Calling this function is only allowed within init.
133 * Returns 0 in case of success or -1 in case of error.
135 static inline int afb_dynapi_require_api(struct afb_dynapi *dynapi, const char *name, int initialized)
137 return dynapi->itf->require_api(dynapi, name, initialized);
141 * Set the name of the API to 'name'.
142 * Calling this function is only allowed within preinit.
143 * Returns 0 in case of success or -1 in case of error.
145 static inline int afb_dynapi_rename_api(struct afb_dynapi *dynapi, const char *name)
147 return dynapi->itf->rename_api(dynapi, name);
151 * Broadcasts widely the event of 'name' with the data 'object'.
152 * 'object' can be NULL.
154 * For convenience, the function calls 'json_object_put' for 'object'.
155 * Thus, in the case where 'object' should remain available after
156 * the function returns, the function 'json_object_get' shall be used.
158 * Calling this function is only forbidden during preinit.
160 * Returns the count of clients that received the event.
162 static inline int afb_dynapi_broadcast_event(struct afb_dynapi *dynapi, const char *name, struct json_object *object)
164 return dynapi->itf->event_broadcast(dynapi, name, object);
168 * Creates an event of 'name' and returns it.
170 * Calling this function is only forbidden during preinit.
172 * See afb_event_is_valid to check if there is an error.
174 static inline struct afb_eventid *afb_dynapi_make_eventid(struct afb_dynapi *dynapi, const char *name)
176 return dynapi->itf->eventid_make(dynapi, name);
180 * Calls the 'verb' of the 'api' with the arguments 'args' and 'verb' in the name of the binding.
181 * The result of the call is delivered to the 'callback' function with the 'callback_closure'.
183 * For convenience, the function calls 'json_object_put' for 'args'.
184 * Thus, in the case where 'args' should remain available after
185 * the function returns, the function 'json_object_get' shall be used.
187 * The 'callback' receives 3 arguments:
188 * 1. 'closure' the user defined closure pointer 'callback_closure',
189 * 2. 'status' a status being 0 on success or negative when an error occured,
190 * 2. 'result' the resulting data as a JSON object.
192 * @param dynapi The dynapi
193 * @param api The api name of the method to call
194 * @param verb The verb name of the method to call
195 * @param args The arguments to pass to the method
196 * @param callback The to call on completion
197 * @param callback_closure The closure to pass to the callback
199 * @see also 'afb_req_subcall'
201 static inline void afb_dynapi_call(
202 struct afb_dynapi *dynapi,
205 struct json_object *args,
206 void (*callback)(void *closure, int status, struct json_object *result, struct afb_dynapi *dynapi),
207 void *callback_closure)
209 dynapi->itf->call(dynapi, api, verb, args, callback, callback_closure);
213 * Calls the 'verb' of the 'api' with the arguments 'args' and 'verb' in the name of the binding.
214 * 'result' will receive the response.
216 * For convenience, the function calls 'json_object_put' for 'args'.
217 * Thus, in the case where 'args' should remain available after
218 * the function returns, the function 'json_object_get' shall be used.
220 * @param dynapi The dynapi
221 * @param api The api name of the method to call
222 * @param verb The verb name of the method to call
223 * @param args The arguments to pass to the method
224 * @param result Where to store the result - should call json_object_put on it -
226 * @returns 0 in case of success or a negative value in case of error.
228 * @see also 'afb_req_subcall'
230 static inline int afb_dynapi_call_sync(
231 struct afb_dynapi *dynapi,
234 struct json_object *args,
235 struct json_object **result)
237 return dynapi->itf->call_sync(dynapi, api, verb, args, result);
241 static inline int afb_dynapi_new_api(
242 struct afb_dynapi *dynapi,
245 int (*preinit)(void*, struct afb_dynapi *),
248 return dynapi->itf->api_new_api(dynapi, api, info, preinit, closure);
251 static inline int afb_dynapi_set_verbs_v2(
252 struct afb_dynapi *dynapi,
253 const struct afb_verb_v2 *verbs)
255 return dynapi->itf->api_set_verbs_v2(dynapi, verbs);
258 static inline int afb_dynapi_add_verb(
259 struct afb_dynapi *dynapi,
262 void (*callback)(struct afb_request *request),
264 const struct afb_auth *auth,
267 return dynapi->itf->api_add_verb(dynapi, verb, info, callback, vcbdata, auth, session);
271 static inline int afb_dynapi_sub_verb(
272 struct afb_dynapi *dynapi,
275 return dynapi->itf->api_sub_verb(dynapi, verb);
279 static inline int afb_dynapi_on_event(
280 struct afb_dynapi *dynapi,
281 void (*onevent)(struct afb_dynapi *dynapi, const char *event, struct json_object *object))
283 return dynapi->itf->api_set_on_event(dynapi, onevent);
287 static inline int afb_dynapi_on_init(
288 struct afb_dynapi *dynapi,
289 int (*oninit)(struct afb_dynapi *dynapi))
291 return dynapi->itf->api_set_on_init(dynapi, oninit);
295 static inline void afb_dynapi_seal(
296 struct afb_dynapi *dynapi)
298 dynapi->itf->api_seal(dynapi);