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