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