Allow dynamic creation of APIs
[src/app-framework-binder.git] / include / afb / afb-dynapi-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 /* declared here */
21 struct afb_dynapi;
22 struct afb_dynapi_itf;
23
24 /* referenced here */
25 #include <stdarg.h>
26 struct sd_event;
27 struct sd_bus;
28 struct afb_request;
29 struct afb_eventid;
30 struct afb_auth;
31 struct afb_verb_v2;
32
33 /*
34  * structure for the dynapi
35  */
36 struct afb_dynapi
37 {
38         /* interface for the dynapi */
39         const struct afb_dynapi_itf *itf;
40
41         /* user defined data */
42         void *userdata;
43
44         /* current verbosity level */
45         int verbosity;
46 };
47
48 /*
49  * Definition of the interface for the API
50  */
51 struct afb_dynapi_itf
52 {
53         /* CAUTION: respect the order, add at the end */
54
55         void (*vverbose)(
56                 void *dynapi,
57                 int level,
58                 const char *file,
59                 int line,
60                 const char * func,
61                 const char *fmt,
62                 va_list args);
63
64         /* gets the common systemd's event loop */
65         struct sd_event *(*get_event_loop)(
66                 void *dynapi);
67
68         /* gets the common systemd's user d-bus */
69         struct sd_bus *(*get_user_bus)(
70                 void *dynapi);
71
72         /* gets the common systemd's system d-bus */
73         struct sd_bus *(*get_system_bus)(
74                 void *dynapi);
75
76         int (*rootdir_get_fd)(
77                 void *dynapi);
78
79         int (*rootdir_open_locale)(
80                 void *dynapi,
81                 const char *filename,
82                 int flags,
83                 const char *locale);
84
85         int (*queue_job)(
86                 void *dynapi,
87                 void (*callback)(int signum, void *arg),
88                 void *argument,
89                 void *group,
90                 int timeout);
91
92         int (*require_api)(
93                 void *dynapi,
94                 const char *name,
95                 int initialized);
96
97         int (*rename_api)(
98                 void *dynapi,
99                 const char *name);
100
101         /* broadcasts event 'name' with 'object' */
102         int (*event_broadcast)(
103                 void *dynapi,
104                 const char *name,
105                 struct json_object *object);
106
107         /* creates an event of 'name' */
108         struct afb_eventid *(*eventid_make)(
109                 void *dynapi,
110                 const char *name);
111
112         void (*call)(
113                 struct afb_dynapi *dynapi,
114                 const char *api,
115                 const char *verb,
116                 struct json_object *args,
117                 void (*callback)(void*, int, struct json_object*, struct afb_dynapi *),
118                 void *callback_closure);
119
120         int (*call_sync)(
121                 void *dynapi,
122                 const char *api,
123                 const char *verb,
124                 struct json_object *args,
125                 struct json_object **result);
126
127         int (*api_new_api)(
128                 void *dynapi,
129                 const char *api,
130                 const char *info,
131                 int (*preinit)(void*, struct afb_dynapi *),
132                 void *closure);
133
134         int (*api_set_verbs_v2)(
135                 struct afb_dynapi *dynapi,
136                 const struct afb_verb_v2 *verbs);
137
138         int (*api_add_verb)(
139                 struct afb_dynapi *dynapi,
140                 const char *verb,
141                 const char *info,
142                 void (*callback)(struct afb_request *request),
143                 const struct afb_auth *auth,
144                 uint32_t session);
145
146         int (*api_sub_verb)(
147                 struct afb_dynapi *dynapi,
148                 const char *verb);
149
150         int (*api_set_on_event)(
151                 struct afb_dynapi *dynapi,
152                 void (*onevent)(struct afb_dynapi *dynapi, const char *event, struct json_object *object));
153
154         int (*api_set_on_init)(
155                 struct afb_dynapi *dynapi,
156                 int (*oninit)(struct afb_dynapi *dynapi));
157
158         void (*api_seal)(
159                 struct afb_dynapi *dynapi);
160 };
161