a3ff9fe0cb81ed464757228ca60090e6c4f0699c
[src/app-framework-binder.git] / include / afb / afb-req-itf.h
1 /*
2  * Copyright (C) 2016 "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 struct json_object;
21
22 struct afb_arg {
23         const char *name;
24         const char *value;
25         const char *path;
26 };
27
28 struct afb_req_itf {
29         struct json_object *(*json)(void *closure);
30         struct afb_arg (*get)(void *closure, const char *name);
31
32         void (*success)(void *closure, struct json_object *obj, const char *info);
33         void (*fail)(void *closure, const char *status, const char *info);
34
35         const char *(*raw)(void *closure, size_t *size);
36         void (*send)(void *closure, const char *buffer, size_t size);
37
38         void *(*context_get)(void *closure);
39         void (*context_set)(void *closure, void *value, void (*free_value)(void*));
40
41         void (*addref)(void *closure);
42         void (*unref)(void *closure);
43 };
44
45 struct afb_req {
46         const struct afb_req_itf *itf;
47         void *closure;
48 };
49
50 static inline struct afb_arg afb_req_get(struct afb_req req, const char *name)
51 {
52         return req.itf->get(req.closure, name);
53 }
54
55 static inline const char *afb_req_value(struct afb_req req, const char *name)
56 {
57         return afb_req_get(req, name).value;
58 }
59
60 static inline const char *afb_req_path(struct afb_req req, const char *name)
61 {
62         return afb_req_get(req, name).path;
63 }
64
65 static inline struct json_object *afb_req_json(struct afb_req req)
66 {
67         return req.itf->json(req.closure);
68 }
69
70 static inline void afb_req_success(struct afb_req req, struct json_object *obj, const char *info)
71 {
72         req.itf->success(req.closure, obj, info);
73 }
74
75 static inline void afb_req_fail(struct afb_req req, const char *status, const char *info)
76 {
77         req.itf->fail(req.closure, status, info);
78 }
79
80 static inline const char *afb_req_raw(struct afb_req req, size_t *size)
81 {
82         return req.itf->raw(req.closure, size);
83 }
84
85 static inline void afb_req_send(struct afb_req req, const char *buffer, size_t size)
86 {
87         req.itf->send(req.closure, buffer, size);
88 }
89
90 static inline void *afb_req_context_get(struct afb_req req)
91 {
92         return req.itf->context_get(req.closure);
93 }
94
95 static inline void afb_req_context_set(struct afb_req req, void *value, void (*free_value)(void*))
96 {
97         return req.itf->context_set(req.closure, value, free_value);
98 }
99
100 static inline void *afb_req_context(struct afb_req req, void *(*create_value)(), void (*free_value)(void*))
101 {
102         void *result = req.itf->context_get(req.closure);
103         if (result == NULL) {
104                 result = create_value();
105                 if (result != NULL)
106                         req.itf->context_set(req.closure, result, free_value);
107         }
108         return result;
109 }
110
111 static inline void afb_req_context_clear(struct afb_req req)
112 {
113         afb_req_context_set(req, NULL, NULL);
114 }
115
116 static inline void afb_req_addref(struct afb_req req)
117 {
118         return req.itf->addref(req.closure);
119 }
120
121 static inline void afb_req_unref(struct afb_req req)
122 {
123         return req.itf->unref(req.closure);
124 }
125
126 #include <stdlib.h>
127
128 static inline struct afb_req *afb_req_store(struct afb_req req)
129 {
130         struct afb_req *result = malloc(sizeof *result);
131         if (result != NULL) {
132                 *result = req;
133                 afb_req_addref(req);
134         }
135         return result;
136 }
137
138 static inline struct afb_req afb_req_unstore(struct afb_req *req)
139 {
140         struct afb_req result = *req;
141         free(req);
142         afb_req_unref(result);
143         return result;
144 }
145
146 #if !defined(_GNU_SOURCE)
147 # error "_GNU_SOURCE must be defined for using vasprintf"
148 #endif
149
150 #include <stdarg.h>
151 #include <stdio.h>
152
153 static inline void afb_req_fail_v(struct afb_req req, const char *status, const char *info, va_list args)
154 {
155         char *message;
156         if (info == NULL || vasprintf(&message, info, args) < 0)
157                 message = NULL;
158         afb_req_fail(req, status, message);
159         free(message);
160 }
161
162 static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...)
163 {
164         va_list args;
165         va_start(args, info);
166         afb_req_fail_v(req, status, info, args);
167         va_end(args);
168 }
169
170 static inline void afb_req_success_v(struct afb_req req, struct json_object *obj, const char *info, va_list args)
171 {
172         char *message;
173         if (info == NULL || vasprintf(&message, info, args) < 0)
174                 message = NULL;
175         afb_req_success(req, obj, message);
176         free(message);
177 }
178
179 static inline void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...)
180 {
181         va_list args;
182         va_start(args, info);
183         afb_req_success_v(req, obj, info, args);
184         va_end(args);
185 }
186