Introduce apiset for grouping apis
[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
21 #define NO_BINDING_VERBOSE_MACRO
22 #include <afb/afb-binding.h>
23 #include "afb-context.h"
24 #include "afb-evt.h"
25
26 struct json_object;
27 struct afb_evt_listener;
28 struct afb_xreq;
29 struct afb_cred;
30 struct afb_apiset;
31
32 struct afb_xreq_query_itf {
33         struct json_object *(*json)(struct afb_xreq *xreq);
34         struct afb_arg (*get)(struct afb_xreq *xreq, const char *name);
35         void (*success)(struct afb_xreq *xreq, struct json_object *obj, const char *info);
36         void (*fail)(struct afb_xreq *xreq, const char *status, const char *info);
37         void (*reply)(struct afb_xreq *xreq, int iserror, struct json_object *obj);
38         void (*unref)(struct afb_xreq *xreq);
39         int (*subscribe)(struct afb_xreq *xreq, struct afb_event event);
40         int (*unsubscribe)(struct afb_xreq *xreq, struct afb_event event);
41         void (*subcall)(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure);
42 };
43
44 /**
45  * Internal data for requests
46  */
47 struct afb_xreq
48 {
49         struct afb_context context; /**< context of the request */
50         struct afb_apiset *apiset; /**< apiset of the xreq */
51         const char *api;        /**< the requested API */
52         const char *verb;       /**< the requested VERB */
53         struct json_object *json; /**< the json object (or NULL) */
54         const struct afb_xreq_query_itf *queryitf;
55         int refcount;   /**< current ref count */
56         int replied;    /**< is replied? */
57         int hookflags;  /**< flags for hooking */
58         int hookindex;  /**< index for hooking */
59         struct afb_evt_listener *listener;
60         struct afb_cred *cred;
61 };
62
63 #define CONTAINER_OF_XREQ(type,x) ((type*)(((intptr_t)(x))-((intptr_t)&(((type*)NULL)->xreq))))
64
65 extern void afb_xreq_addref(struct afb_xreq *xreq);
66 extern void afb_xreq_unref(struct afb_xreq *xreq);
67 extern void afb_xreq_success(struct afb_xreq *xreq, struct json_object *obj, const char *info);
68 extern void afb_xreq_fail(struct afb_xreq *xreq, const char *status, const char *info);
69 extern void afb_xreq_fail_f(struct afb_xreq *xreq, const char *status, const char *info, ...);
70 extern void afb_xreq_success_f(struct afb_xreq *xreq, struct json_object *obj, const char *info, ...);
71 extern const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size);
72 extern int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event event);
73 extern int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event event);
74 extern void afb_xreq_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure);
75 extern void afb_xreq_unhooked_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure);
76
77 extern void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf);
78 extern void afb_xreq_begin(struct afb_xreq *xreq);
79 extern void afb_xreq_so_call(struct afb_xreq *xreq, int sessionflags, void (*callback)(struct afb_req req));
80
81 extern void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset);