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