afb-error-text: Introduce standard error text
[src/app-framework-binder.git] / src / afb-xreq.h
1 /*
2  * Copyright (C) 2017-2019 "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 #include <stdarg.h>
21 #include <afb/afb-req-x1-itf.h>
22 #include <afb/afb-req-x2-itf.h>
23 #include "afb-context.h"
24
25 struct json_object;
26 struct afb_evt_listener;
27 struct afb_xreq;
28 struct afb_cred;
29 struct afb_apiset;
30 struct afb_event_x2;
31 struct afb_verb_desc_v1;
32 struct afb_verb_v2;
33 struct afb_verb_v3;
34 struct afb_req_x1;
35 struct afb_stored_req;
36
37 struct afb_xreq_query_itf {
38         struct json_object *(*json)(struct afb_xreq *xreq);
39         struct afb_arg (*get)(struct afb_xreq *xreq, const char *name);
40         void (*reply)(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info);
41         void (*unref)(struct afb_xreq *xreq);
42         int (*subscribe)(struct afb_xreq *xreq, struct afb_event_x2 *event);
43         int (*unsubscribe)(struct afb_xreq *xreq, struct afb_event_x2 *event);
44 };
45
46 /**
47  * Internal data for requests
48  */
49 struct afb_xreq
50 {
51         struct afb_req_x2 request;      /**< exported request */
52         struct afb_context context;     /**< context of the request */
53         struct afb_apiset *apiset;      /**< apiset of the xreq */
54         struct json_object *json;       /**< the json object (or NULL) */
55         const struct afb_xreq_query_itf *queryitf; /**< interface of xreq implementation functions */
56         int refcount;                   /**< current ref count */
57         int replied;                    /**< is replied? */
58 #if WITH_AFB_HOOK
59         int hookflags;                  /**< flags for hooking */
60         int hookindex;                  /**< hook index of the request if hooked */
61 #endif
62         struct afb_cred *cred;          /**< client credential if revelant */
63         struct afb_xreq *caller;        /**< caller request if any */
64 };
65
66 /**
67  * Macro for retrieve the pointer of a structure of 'type' having a field named 'field'
68  * of address 'ptr'.
69  * @param type the type that has the 'field' (ex: "struct mystruct")
70  * @param field the name of the field within the structure 'type'
71  * @param ptr the pointer to an element 'field'
72  * @return the pointer to the structure that contains the 'field' at address 'ptr'
73  */
74 #define CONTAINER_OF(type,field,ptr) ((type*)(((char*)(ptr))-((char*)&(((type*)NULL)->field))))
75
76 /**
77  * Macro for retrieve the pointer of a structure of 'type' having a field named "xreq"
78  * of address 'x'.
79  * @param type the type that has the field "xreq" (ex: "struct mystruct")
80  * @param x the pointer to the field "xreq"
81  * @return the pointer to the structure that contains the field "xreq" of address 'x'
82  */
83 #define CONTAINER_OF_XREQ(type,x) CONTAINER_OF(type,xreq,x)
84
85 /* req wrappers for xreq */
86 extern struct afb_req_x1 afb_xreq_unstore(struct afb_stored_req *sreq);
87
88 extern void afb_xreq_addref(struct afb_xreq *xreq);
89 extern void afb_xreq_unref(struct afb_xreq *xreq);
90 extern void afb_xreq_unhooked_addref(struct afb_xreq *xreq);
91 extern void afb_xreq_unhooked_unref(struct afb_xreq *xreq);
92
93 extern struct json_object *afb_xreq_unhooked_json(struct afb_xreq *xreq);
94 extern struct json_object *afb_xreq_json(struct afb_xreq *xreq);
95
96 extern void afb_xreq_reply(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info);
97 extern void afb_xreq_reply_f(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info, ...);
98
99 extern int afb_xreq_reply_unknown_api(struct afb_xreq *xreq);
100 extern int afb_xreq_reply_unknown_verb(struct afb_xreq *xreq);
101
102 extern int afb_xreq_reply_invalid_token(struct afb_xreq *xreq);
103 extern int afb_xreq_reply_insufficient_scope(struct afb_xreq *xreq, const char *scope);
104
105
106 extern const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size);
107
108 extern int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event_x2 *event);
109 extern int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event_x2 *event);
110
111 extern void afb_xreq_legacy_subcall(
112                 struct afb_xreq *xreq,
113                 const char *api,
114                 const char *verb,
115                 struct json_object *args,
116                 void (*callback)(void*, int, struct json_object*, struct afb_req_x2 *),
117                 void *cb_closure);
118 extern void afb_xreq_unhooked_legacy_subcall(
119                 struct afb_xreq *xreq,
120                 const char *api,
121                 const char *verb,
122                 struct json_object *args,
123                 void (*callback)(void*, int, struct json_object*, struct afb_req_x2 *),
124                 void *cb_closure);
125
126 extern void afb_xreq_subcall(
127                 struct afb_xreq *xreq,
128                 const char *api,
129                 const char *verb,
130                 struct json_object *args,
131                 int flags,
132                 void (*callback)(void*, struct json_object*, const char*, const char*, struct afb_req_x2 *),
133                 void *closure);
134 extern void afb_xreq_unhooked_subcall(
135                 struct afb_xreq *xreq,
136                 const char *api,
137                 const char *verb,
138                 struct json_object *args,
139                 int flags,
140                 void (*callback)(void*, struct json_object*, const char*, const char*, struct afb_req_x2 *),
141                 void *closure);
142
143 extern int afb_xreq_unhooked_legacy_subcall_sync(
144                 struct afb_xreq *xreq,
145                 const char *api,
146                 const char *verb,
147                 struct json_object *args,
148                 struct json_object **result);
149 extern int afb_xreq_legacy_subcall_sync(
150                 struct afb_xreq *xreq,
151                 const char *api,
152                 const char *verb,
153                 struct json_object *args,
154                 struct json_object **result);
155
156 /* initialisation and processing of xreq */
157 extern void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf);
158
159 extern void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset);
160
161 #if WITH_LEGACY_BINDING_V1
162 extern void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb);
163 #endif
164 #if WITH_LEGACY_BINDING_V2
165 extern void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb);
166 #endif
167
168 extern void afb_xreq_call_verb_v3(struct afb_xreq *xreq, const struct afb_verb_v3 *verb);
169
170 extern const char *xreq_on_behalf_cred_export(struct afb_xreq *xreq);
171
172 /******************************************************************************/
173
174 static inline struct afb_req_x1 xreq_to_req_x1(struct afb_xreq *xreq)
175 {
176         return (struct afb_req_x1){ .itf = xreq->request.itf, .closure = &xreq->request };
177 }
178
179 static inline struct afb_req_x2 *xreq_to_req_x2(struct afb_xreq *xreq)
180 {
181         return &xreq->request;
182 }
183
184 static inline struct afb_xreq *xreq_from_req_x2(struct afb_req_x2 *req)
185 {
186         return CONTAINER_OF(struct afb_xreq, request, req);
187 }
188