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