Prepare bindings version 2
[src/app-framework-binder.git] / include / afb / afb-daemon-itf.h
1 /*
2  * Copyright (C) 2016, 2017 "IoT.bzh"
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #pragma once
19
20 #include <stdarg.h>
21
22 /* declaration of features of libsystemd */
23 struct sd_event;
24 struct sd_bus;
25
26 /*
27  * Definition of the facilities provided by the daemon.
28  */
29 struct afb_daemon_itf {
30         int (*event_broadcast)(void *closure, const char *name, struct json_object *object); /* broadcasts evant 'name' with 'object' */
31         struct sd_event *(*get_event_loop)(void *closure);      /* gets the common systemd's event loop */
32         struct sd_bus *(*get_user_bus)(void *closure);          /* gets the common systemd's user d-bus */
33         struct sd_bus *(*get_system_bus)(void *closure);        /* gets the common systemd's system d-bus */
34         void (*vverbose)(void*closure, int level, const char *file, int line, const char *fmt, va_list args);
35         struct afb_event (*event_make)(void *closure, const char *name); /* creates an event of 'name' */
36         int (*rootdir_get_fd)(void *closure);
37         int (*rootdir_open_locale)(void *closure, const char *filename, int flags, const char *locale);
38         int (*queue_job)(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout);
39 };
40
41 /*
42  * Structure for accessing daemon.
43  * See also: afb_daemon_get_event_sender, afb_daemon_get_event_loop, afb_daemon_get_user_bus, afb_daemon_get_system_bus
44  */
45 struct afb_daemon {
46         const struct afb_daemon_itf *itf;       /* the interfacing functions */
47         void *closure;                          /* the closure when calling these functions */
48 };
49
50 /*
51  * Retrieves the common systemd's event loop of AFB
52  * 'daemon' MUST be the daemon given in interface when activating the binding.
53  */
54 static inline struct sd_event *afb_daemon_get_event_loop(struct afb_daemon daemon)
55 {
56         return daemon.itf->get_event_loop(daemon.closure);
57 }
58
59 /*
60  * Retrieves the common systemd's user/session d-bus of AFB
61  * 'daemon' MUST be the daemon given in interface when activating the binding.
62  */
63 static inline struct sd_bus *afb_daemon_get_user_bus(struct afb_daemon daemon)
64 {
65         return daemon.itf->get_user_bus(daemon.closure);
66 }
67
68 /*
69  * Retrieves the common systemd's system d-bus of AFB
70  * 'daemon' MUST be the daemon given in interface when activating the binding.
71  */
72 static inline struct sd_bus *afb_daemon_get_system_bus(struct afb_daemon daemon)
73 {
74         return daemon.itf->get_system_bus(daemon.closure);
75 }
76
77 /*
78  * Broadcasts widely the event of 'name' with the data 'object'.
79  * 'object' can be NULL.
80  * 'daemon' MUST be the daemon given in interface when activating the binding.
81  *
82  * For convenience, the function calls 'json_object_put' for 'object'.
83  * Thus, in the case where 'object' should remain available after
84  * the function returns, the function 'json_object_get' shall be used.
85  *
86  * Returns the count of clients that received the event.
87  */
88 static inline int afb_daemon_broadcast_event(struct afb_daemon daemon, const char *name, struct json_object *object)
89 {
90         return daemon.itf->event_broadcast(daemon.closure, name, object);
91 }
92
93 /*
94  * Creates an event of 'name' and returns it.
95  * 'daemon' MUST be the daemon given in interface when activating the binding.
96  */
97 static inline struct afb_event afb_daemon_make_event(struct afb_daemon daemon, const char *name)
98 {
99         return daemon.itf->event_make(daemon.closure, name);
100 }
101
102 /*
103  * Send a message described by 'fmt' and following parameters
104  * to the journal for the verbosity 'level'.
105  * 'file' and 'line' are indicators of position of the code in source files.
106  * 'daemon' MUST be the daemon given in interface when activating the binding.
107  */
108 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)));
109 static inline void afb_daemon_verbose(struct afb_daemon daemon, int level, const char *file, int line, const char *fmt, ...)
110 {
111         va_list args;
112         va_start(args, fmt);
113         daemon.itf->vverbose(daemon.closure, level, file, line, fmt, args);
114         va_end(args);
115 }
116
117 /*
118  * Get the root directory file descriptor. This file descriptor can
119  * be used with functions 'openat', 'fstatat', ...
120  */
121 static inline int afb_daemon_rootdir_get_fd(struct afb_daemon daemon)
122 {
123         return daemon.itf->rootdir_get_fd(daemon.closure);
124 }
125
126 /*
127  * Opens 'filename' within the root directory with 'flags' (see function openat)
128  * using the 'locale' definition (example: "jp,en-US") that can be NULL.
129  * Returns the file descriptor or -1 in case of error.
130  */
131 static inline int afb_daemon_rootdir_open_locale(struct afb_daemon daemon, const char *filename, int flags, const char *locale)
132 {
133         return daemon.itf->rootdir_open_locale(daemon.closure, filename, flags, locale);
134 }
135
136 /*
137  * Queue the job defined by 'callback' and 'argument' for being executed asynchronously
138  * in this thread (later) or in an other thread.
139  * If 'group' is not NUL, the jobs queued with a same value (as the pointer value 'group')
140  * are executed in sequence in the order of there submission.
141  * If 'timeout' is not 0, it represent the maximum execution time for the job in seconds.
142  * At first, the job is called with 0 as signum and the given argument.
143  * The job is executed with the monitoring of its time and some signals like SIGSEGV and
144  * SIGFPE. When a such signal is catched, the job is terminated and reexecuted but with
145  * signum being the signal number (SIGALRM when timeout expired).
146  *
147  * Returns 0 in case of success or -1 in case of error.
148  */
149 static inline int afb_daemon_queue_job(struct afb_daemon daemon, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
150 {
151         return daemon.itf->queue_job(daemon.closure, callback, argument, group, timeout);
152 }