Make single afb_request_subcall
[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_eventid;
29 struct afb_verb_desc_v1;
30 struct afb_verb_v2;
31 struct afb_req;
32 struct afb_req_itf;
33 struct afb_request;
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         const char *api;                /**< the requested API */
63         const char *verb;               /**< the requested VERB */
64         struct json_object *json;       /**< the json object (or NULL) */
65         const struct afb_xreq_query_itf *queryitf; /**< interface of xreq implmentation functions */
66         int refcount;                   /**< current ref count */
67         int replied;                    /**< is replied? */
68         int hookflags;                  /**< flags for hooking */
69         int hookindex;                  /**< hook index of the request if hooked */
70         struct afb_evt_listener *listener; /**< event listener for the request */
71         struct afb_cred *cred;          /**< client credential if revelant */
72         struct afb_xreq *caller;        /**< caller request if any */
73 };
74
75 /**
76  * Macro for retrieve the pointer of a structure of 'type' having a field named 'field'
77  * of adress 'ptr'.
78  * @param type the type that has the 'field' (ex: "struct mystruct")
79  * @param field the name of the field within the structure 'type'
80  * @param ptr the pointer to an element 'field'
81  * @return the pointer to the structure that contains the 'field' at address 'ptr'
82  */
83 #define CONTAINER_OF(type,field,ptr) ((type*)(((intptr_t)(ptr))-((intptr_t)&(((type*)NULL)->field))))
84
85 /**
86  * Macro for retrieve the pointer of a structure of 'type' having a field named "xreq"
87  * of adress 'x'.
88  * @param type the type that has the field "xreq" (ex: "struct mystruct")
89  * @param x the pointer to the field "xreq"
90  * @return the pointer to the structure that contains the field "xreq" of address 'x'
91  */
92 #define CONTAINER_OF_XREQ(type,x) CONTAINER_OF(type,xreq,x)
93
94 /* req wrappers for xreq */
95 extern struct afb_req afb_xreq_unstore(struct afb_stored_req *sreq);
96 extern void afb_xreq_addref(struct afb_xreq *xreq);
97 extern void afb_xreq_unref(struct afb_xreq *xreq);
98 extern void afb_xreq_unhooked_addref(struct afb_xreq *xreq);
99 extern void afb_xreq_unhooked_unref(struct afb_xreq *xreq);
100
101 extern struct json_object *afb_xreq_json(struct afb_xreq *xreq);
102
103 extern void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info);
104 extern void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...);
105
106 extern void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info);
107 extern void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...);
108 extern void afb_xreq_fail_unknown_api(struct afb_xreq *xreq);
109 extern void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq);
110
111 extern const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size);
112
113 extern int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_eventid *eventid);
114 extern int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_eventid *eventid);
115
116 extern void afb_xreq_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*, struct afb_request *),
122                 void *cb_closure);
123 extern void afb_xreq_unhooked_subcall(
124                 struct afb_xreq *xreq,
125                 const char *api,
126                 const char *verb,
127                 struct json_object *args,
128                 void (*callback)(void*, int, struct json_object*, struct afb_request *),
129                 void *cb_closure);
130
131 extern int afb_xreq_unhooked_subcall_sync(
132                 struct afb_xreq *xreq,
133                 const char *api,
134                 const char *verb,
135                 struct json_object *args,
136                 struct json_object **result);
137 extern int afb_xreq_subcall_sync(
138                 struct afb_xreq *xreq,
139                 const char *api,
140                 const char *verb,
141                 struct json_object *args,
142                 struct json_object **result);
143
144 /* initialisation and processing of xreq */
145 extern void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf);
146
147 extern void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset);
148
149 extern void afb_xreq_call_verb_v1(struct afb_xreq *xreq, const struct afb_verb_desc_v1 *verb);
150 extern void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb);
151