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