Setting and checking LOA
[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         void (*session_close)(void *closure);
45         int (*session_set_LOA)(void *closure, unsigned level);
46 };
47
48 struct afb_req {
49         const struct afb_req_itf *itf;
50         void *closure;
51 };
52
53 static inline struct afb_arg afb_req_get(struct afb_req req, const char *name)
54 {
55         return req.itf->get(req.closure, name);
56 }
57
58 static inline const char *afb_req_value(struct afb_req req, const char *name)
59 {
60         return afb_req_get(req, name).value;
61 }
62
63 static inline const char *afb_req_path(struct afb_req req, const char *name)
64 {
65         return afb_req_get(req, name).path;
66 }
67
68 static inline struct json_object *afb_req_json(struct afb_req req)
69 {
70         return req.itf->json(req.closure);
71 }
72
73 static inline void afb_req_success(struct afb_req req, struct json_object *obj, const char *info)
74 {
75         req.itf->success(req.closure, obj, info);
76 }
77
78 static inline void afb_req_fail(struct afb_req req, const char *status, const char *info)
79 {
80         req.itf->fail(req.closure, status, info);
81 }
82
83 static inline const char *afb_req_raw(struct afb_req req, size_t *size)
84 {
85         return req.itf->raw(req.closure, size);
86 }
87
88 static inline void afb_req_send(struct afb_req req, const char *buffer, size_t size)
89 {
90         req.itf->send(req.closure, buffer, size);
91 }
92
93 static inline void *afb_req_context_get(struct afb_req req)
94 {
95         return req.itf->context_get(req.closure);
96 }
97
98 static inline void afb_req_context_set(struct afb_req req, void *value, void (*free_value)(void*))
99 {
100         req.itf->context_set(req.closure, value, free_value);
101 }
102
103 static inline void *afb_req_context(struct afb_req req, void *(*create_value)(), void (*free_value)(void*))
104 {
105         void *result = req.itf->context_get(req.closure);
106         if (result == NULL) {
107                 result = create_value();
108                 if (result != NULL)
109                         req.itf->context_set(req.closure, result, free_value);
110         }
111         return result;
112 }
113
114 static inline void afb_req_context_clear(struct afb_req req)
115 {
116         afb_req_context_set(req, NULL, NULL);
117 }
118
119 static inline void afb_req_addref(struct afb_req req)
120 {
121         req.itf->addref(req.closure);
122 }
123
124 static inline void afb_req_unref(struct afb_req req)
125 {
126         req.itf->unref(req.closure);
127 }
128
129 static inline void afb_req_session_close(struct afb_req req)
130 {
131         req.itf->session_close(req.closure);
132 }
133
134 static inline int afb_req_session_set_LOA(struct afb_req req, unsigned level)
135 {
136         return req.itf->session_set_LOA(req.closure, level);
137 }
138
139 #include <stdlib.h>
140
141 static inline struct afb_req *afb_req_store(struct afb_req req)
142 {
143         struct afb_req *result = malloc(sizeof *result);
144         if (result != NULL) {
145                 *result = req;
146                 afb_req_addref(req);
147         }
148         return result;
149 }
150
151 static inline struct afb_req afb_req_unstore(struct afb_req *req)
152 {
153         struct afb_req result = *req;
154         free(req);
155         afb_req_unref(result);
156         return result;
157 }
158
159 #if !defined(_GNU_SOURCE)
160 # error "_GNU_SOURCE must be defined for using vasprintf"
161 #endif
162
163 #include <stdarg.h>
164 #include <stdio.h>
165
166 static inline void afb_req_fail_v(struct afb_req req, const char *status, const char *info, va_list args)
167 {
168         char *message;
169         if (info == NULL || vasprintf(&message, info, args) < 0)
170                 message = NULL;
171         afb_req_fail(req, status, message);
172         free(message);
173 }
174
175 static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...)
176 {
177         va_list args;
178         va_start(args, info);
179         afb_req_fail_v(req, status, info, args);
180         va_end(args);
181 }
182
183 static inline void afb_req_success_v(struct afb_req req, struct json_object *obj, const char *info, va_list args)
184 {
185         char *message;
186         if (info == NULL || vasprintf(&message, info, args) < 0)
187                 message = NULL;
188         afb_req_success(req, obj, message);
189         free(message);
190 }
191
192 static inline void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...)
193 {
194         va_list args;
195         va_start(args, info);
196         afb_req_success_v(req, obj, info, args);
197         va_end(args);
198 }
199