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