work in progress (tbf)
[src/app-framework-binder.git] / include / afb-req-itf.h
1 /*
2  * Copyright 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 struct json_object;
19
20 struct afb_arg {
21         const char *name;
22         const char *value;
23         size_t size;
24         int is_file;
25 };
26
27 struct afb_req_itf {
28         struct afb_arg (*get)(void *data, const char *name);
29         void (*iterate)(void *data, int (*iterator)(void *closure, struct afb_arg arg), void *closure);
30         void (*fail)(void *data, const char *status, const char *info);
31         void (*success)(void *data, struct json_object *obj, const char *info);
32 };
33
34 struct afb_req {
35         const struct afb_req_itf *itf;
36         void *data;
37         void **context;
38 };
39
40 static inline struct afb_arg afb_req_get(struct afb_req req, const char *name)
41 {
42         return req.itf->get(req.data, name);
43 }
44
45 static inline const char *afb_req_argument(struct afb_req req, const char *name)
46 {
47         return afb_req_get(req, name).value;
48 }
49
50 static inline int afb_req_is_argument_file(struct afb_req req, const char *name)
51 {
52         return afb_req_get(req, name).is_file;
53 }
54
55 static inline void afb_req_iterate(struct afb_req req, int (*iterator)(void *closure, struct afb_arg arg), void *closure)
56 {
57         req.itf->iterate(req.data, iterator, closure);
58 }
59
60 static inline void afb_req_fail(struct afb_req req, const char *status, const char *info)
61 {
62         req.itf->fail(req.data, status, info);
63 }
64
65 static inline void afb_req_success(struct afb_req req, struct json_object *obj, const char *info)
66 {
67         req.itf->success(req.data, obj, info);
68 }
69
70 #if !defined(_GNU_SOURCE)
71 # error "_GNU_SOURCE must be defined for using vasprintf"
72 #endif
73
74 #include <stdarg.h>
75 #include <stdlib.h>
76 #include <stdio.h>
77
78 static inline void afb_req_fail_v(struct afb_req req, const char *status, const char *info, va_list args)
79 {
80         char *message;
81         if (info == NULL || vasprintf(&message, info, args) < 0)
82                 message = NULL;
83         afb_req_fail(req, status, message);
84         free(message);
85 }
86
87 static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...)
88 {
89         va_list args;
90         va_start(args, info);
91         afb_req_fail_v(req, status, info, args);
92         va_end(args);
93 }
94
95 static inline void afb_req_success_v(struct afb_req req, struct json_object *obj, const char *info, va_list args)
96 {
97         char *message;
98         if (info == NULL || vasprintf(&message, info, args) < 0)
99                 message = NULL;
100         afb_req_success(req, obj, message);
101         free(message);
102 }
103
104 static inline void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...)
105 {
106         va_list args;
107         va_start(args, info);
108         afb_req_success_v(req, obj, info, args);
109         va_end(args);
110 }
111