2 * Copyright (C) 2017 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <afb/afb-request-itf.h>
21 #include <afb/afb-req-itf.h>
22 #include "afb-context.h"
25 struct afb_evt_listener;
29 struct afb_api_dyn_verb;
31 struct afb_verb_desc_v1;
34 struct afb_stored_req;
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);
46 struct afb_xreq *xreq,
49 struct json_object *args,
50 void (*callback)(void*, int, struct json_object*),
55 * Internal data for requests
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 implmentation 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 */
74 * Macro for retrieve the pointer of a structure of 'type' having a field named 'field'
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'
81 #define CONTAINER_OF(type,field,ptr) ((type*)(((char*)(ptr))-((char*)&(((type*)NULL)->field))))
84 * Macro for retrieve the pointer of a structure of 'type' having a field named "xreq"
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'
90 #define CONTAINER_OF_XREQ(type,x) CONTAINER_OF(type,xreq,x)
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);
99 extern struct json_object *afb_xreq_json(struct afb_xreq *xreq);
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, ...);
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);
109 extern const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size);
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);
114 extern void afb_xreq_subcall(
115 struct afb_xreq *xreq,
118 struct json_object *args,
119 void (*callback)(void*, int, struct json_object*, struct afb_request *),
121 extern void afb_xreq_unhooked_subcall(
122 struct afb_xreq *xreq,
125 struct json_object *args,
126 void (*callback)(void*, int, struct json_object*, struct afb_request *),
129 extern int afb_xreq_unhooked_subcall_sync(
130 struct afb_xreq *xreq,
133 struct json_object *args,
134 struct json_object **result);
135 extern int afb_xreq_subcall_sync(
136 struct afb_xreq *xreq,
139 struct json_object *args,
140 struct json_object **result);
142 /* initialisation and processing of xreq */
143 extern void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf);
145 extern void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset);
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);
151 /******************************************************************************/
153 static inline struct afb_req xreq_to_req(struct afb_xreq *xreq)
155 return (struct afb_req){ .itf = xreq->request.itf, .closure = &xreq->request };
158 static inline struct afb_request *xreq_to_request(struct afb_xreq *xreq)
160 return &xreq->request;
163 static inline struct afb_xreq *xreq_from_request(struct afb_request *request)
165 return CONTAINER_OF(struct afb_xreq, request, request);