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.
22 /*****************************************************************************
23 * This files is the main file to include for writing bindings dedicated to
27 * Functions of bindings of afb-daemon are accessible by authorized clients
28 * through the apis module of afb-daemon.
30 * A binding is a shared library. This shared library must have at least one
31 * exported symbol for being registered in afb-daemon.
36 * Some function of the library are exported to afb-daemon.
39 #include "afb-event-itf.h"
40 #include "afb-req-itf.h"
41 #include "afb-service-itf.h"
42 #include "afb-binding-v1.h"
43 #include "afb-binding-v2.h"
49 AFB_MODE_LOCAL = 0, /* run locally */
50 AFB_MODE_REMOTE, /* run remotely */
51 AFB_MODE_GLOBAL /* run either remotely or locally (DONT USE! reserved for future) */
54 /* declaration of features of libsystemd */
59 * Definition of the facilities provided by the daemon.
61 struct afb_daemon_itf {
62 int (*event_broadcast)(void *closure, const char *name, struct json_object *object); /* broadcasts evant 'name' with 'object' */
63 struct sd_event *(*get_event_loop)(void *closure); /* gets the common systemd's event loop */
64 struct sd_bus *(*get_user_bus)(void *closure); /* gets the common systemd's user d-bus */
65 struct sd_bus *(*get_system_bus)(void *closure); /* gets the common systemd's system d-bus */
66 void (*vverbose)(void*closure, int level, const char *file, int line, const char *fmt, va_list args);
67 struct afb_event (*event_make)(void *closure, const char *name); /* creates an event of 'name' */
68 int (*rootdir_get_fd)(void *closure);
69 int (*rootdir_open_locale)(void *closure, const char *filename, int flags, const char *locale);
70 int (*queue_job)(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout);
74 * Structure for accessing daemon.
75 * See also: afb_daemon_get_event_sender, afb_daemon_get_event_loop, afb_daemon_get_user_bus, afb_daemon_get_system_bus
78 const struct afb_daemon_itf *itf; /* the interfacing functions */
79 void *closure; /* the closure when calling these functions */
83 * Interface between the daemon and the binding.
85 struct afb_binding_interface
87 struct afb_daemon daemon; /* access to the daemon facilies */
88 int verbosity; /* level of verbosity */
89 enum afb_mode mode; /* run mode (local or remote) */
93 * Retrieves the common systemd's event loop of AFB
94 * 'daemon' MUST be the daemon given in interface when activating the binding.
96 static inline struct sd_event *afb_daemon_get_event_loop(struct afb_daemon daemon)
98 return daemon.itf->get_event_loop(daemon.closure);
102 * Retrieves the common systemd's user/session d-bus of AFB
103 * 'daemon' MUST be the daemon given in interface when activating the binding.
105 static inline struct sd_bus *afb_daemon_get_user_bus(struct afb_daemon daemon)
107 return daemon.itf->get_user_bus(daemon.closure);
111 * Retrieves the common systemd's system d-bus of AFB
112 * 'daemon' MUST be the daemon given in interface when activating the binding.
114 static inline struct sd_bus *afb_daemon_get_system_bus(struct afb_daemon daemon)
116 return daemon.itf->get_system_bus(daemon.closure);
120 * Broadcasts widely the event of 'name' with the data 'object'.
121 * 'object' can be NULL.
122 * 'daemon' MUST be the daemon given in interface when activating the binding.
124 * For convenience, the function calls 'json_object_put' for 'object'.
125 * Thus, in the case where 'object' should remain available after
126 * the function returns, the function 'json_object_get' shall be used.
128 * Returns the count of clients that received the event.
130 static inline int afb_daemon_broadcast_event(struct afb_daemon daemon, const char *name, struct json_object *object)
132 return daemon.itf->event_broadcast(daemon.closure, name, object);
136 * Creates an event of 'name' and returns it.
137 * 'daemon' MUST be the daemon given in interface when activating the binding.
139 static inline struct afb_event afb_daemon_make_event(struct afb_daemon daemon, const char *name)
141 return daemon.itf->event_make(daemon.closure, name);
145 * Send a message described by 'fmt' and following parameters
146 * to the journal for the verbosity 'level'.
147 * 'file' and 'line' are indicators of position of the code in source files.
148 * 'daemon' MUST be the daemon given in interface when activating the binding.
150 static inline void afb_daemon_verbose(struct afb_daemon daemon, int level, const char *file, int line, const char *fmt, ...) __attribute__((format(printf, 5, 6)));
151 static inline void afb_daemon_verbose(struct afb_daemon daemon, int level, const char *file, int line, const char *fmt, ...)
155 daemon.itf->vverbose(daemon.closure, level, file, line, fmt, args);
160 * Macros for logging messages
162 #if !defined(NO_BINDING_VERBOSE_MACRO)
163 # if !defined(NO_BINDING_FILE_LINE_INDICATION)
164 # define ERROR(itf,...) do{if(itf->verbosity>=0)afb_daemon_verbose(itf->daemon,3,__FILE__,__LINE__,__VA_ARGS__);}while(0)
165 # define WARNING(itf,...) do{if(itf->verbosity>=1)afb_daemon_verbose(itf->daemon,4,__FILE__,__LINE__,__VA_ARGS__);}while(0)
166 # define NOTICE(itf,...) do{if(itf->verbosity>=1)afb_daemon_verbose(itf->daemon,5,__FILE__,__LINE__,__VA_ARGS__);}while(0)
167 # define INFO(itf,...) do{if(itf->verbosity>=2)afb_daemon_verbose(itf->daemon,6,__FILE__,__LINE__,__VA_ARGS__);}while(0)
168 # define DEBUG(itf,...) do{if(itf->verbosity>=3)afb_daemon_verbose(itf->daemon,7,__FILE__,__LINE__,__VA_ARGS__);}while(0)
170 # define ERROR(itf,...) do{if(itf->verbosity>=0)afb_daemon_verbose(itf->daemon,3,NULL,0,__VA_ARGS__);}while(0)
171 # define WARNING(itf,...) do{if(itf->verbosity>=1)afb_daemon_verbose(itf->daemon,4,NULL,0,__VA_ARGS__);}while(0)
172 # define NOTICE(itf,...) do{if(itf->verbosity>=1)afb_daemon_verbose(itf->daemon,5,NULL,0,__VA_ARGS__);}while(0)
173 # define INFO(itf,...) do{if(itf->verbosity>=2)afb_daemon_verbose(itf->daemon,6,NULL,0,__VA_ARGS__);}while(0)
174 # define DEBUG(itf,...) do{if(itf->verbosity>=3)afb_daemon_verbose(itf->daemon,7,NULL,0,__VA_ARGS__);}while(0)
179 * Get the root directory file descriptor. This file descriptor can
180 * be used with functions 'openat', 'fstatat', ...
182 static inline int afb_daemon_rootdir_get_fd(struct afb_daemon daemon)
184 return daemon.itf->rootdir_get_fd(daemon.closure);
188 * Opens 'filename' within the root directory with 'flags' (see function openat)
189 * using the 'locale' definition (example: "jp,en-US") that can be NULL.
190 * Returns the file descriptor or -1 in case of error.
192 static inline int afb_daemon_rootdir_open_locale(struct afb_daemon daemon, const char *filename, int flags, const char *locale)
194 return daemon.itf->rootdir_open_locale(daemon.closure, filename, flags, locale);
198 * Queue the job defined by 'callback' and 'argument' for being executed asynchronously
199 * in this thread (later) or in an other thread.
200 * If 'group' is not NUL, the jobs queued with a same value (as the pointer value 'group')
201 * are executed in sequence in the order of there submission.
202 * If 'timeout' is not 0, it represent the maximum execution time for the job in seconds.
203 * At first, the job is called with 0 as signum and the given argument.
204 * The job is executed with the monitoring of its time and some signals like SIGSEGV and
205 * SIGFPE. When a such signal is catched, the job is terminated and reexecuted but with
206 * signum being the signal number (SIGALRM when timeout expired).
208 * Returns 0 in case of success or -1 in case of error.
210 static inline int afb_daemon_queue_job(struct afb_daemon daemon, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
212 return daemon.itf->queue_job(daemon.closure, callback, argument, group, timeout);