Reactivate hooking of requests
[src/app-framework-binder.git] / src / afb-hook.h
1 /*
2  * Copyright (C) 2016, 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 /* individual flags */
21 #define afb_hook_flag_req_json                  1
22 #define afb_hook_flag_req_get                   2
23 #define afb_hook_flag_req_success               4
24 #define afb_hook_flag_req_fail                  8
25 #define afb_hook_flag_req_raw                   16
26 #define afb_hook_flag_req_send                  32
27 #define afb_hook_flag_req_context_get           64
28 #define afb_hook_flag_req_context_set           128
29 #define afb_hook_flag_req_addref                256
30 #define afb_hook_flag_req_unref                 512
31 #define afb_hook_flag_req_session_close         1024
32 #define afb_hook_flag_req_session_set_LOA       2048
33 #define afb_hook_flag_req_subscribe             4096
34 #define afb_hook_flag_req_unsubscribe           8192
35 #define afb_hook_flag_req_subcall               16384
36 #define afb_hook_flag_req_subcall_result        32768
37 #define afb_hook_flag_req_begin                 65536
38 #define afb_hook_flag_req_end                   131072
39
40 /* common flags */
41 #define afb_hook_flags_req_life         (afb_hook_flag_req_begin|afb_hook_flag_req_end)
42 #define afb_hook_flags_req_args         (afb_hook_flag_req_json|afb_hook_flag_req_get)
43 #define afb_hook_flags_req_result       (afb_hook_flag_req_success|afb_hook_flag_req_fail)
44 #define afb_hook_flags_req_session      (afb_hook_flag_req_session_close|afb_hook_flag_req_session_set_LOA)
45 #define afb_hook_flags_req_event        (afb_hook_flag_req_subscribe|afb_hook_flag_req_unsubscribe)
46 #define afb_hook_flags_req_subcall      (afb_hook_flag_req_subcall|afb_hook_flag_req_subcall_result)
47
48 /* extra flags */
49 #define afb_hook_flags_req_ref          (afb_hook_flag_req_addref|afb_hook_flag_req_unref)
50 #define afb_hook_flags_req_context      (afb_hook_flag_req_context_get|afb_hook_flag_req_context_set)
51
52 /* internal flags */
53 #define afb_hook_flags_req_internal     (afb_hook_flag_req_raw|afb_hook_flag_req_send)
54
55 /* predefined groups */
56 #define afb_hook_flags_req_common       (afb_hook_flags_req_life|afb_hook_flags_req_args|afb_hook_flags_req_result\
57                                         |afb_hook_flags_req_session|afb_hook_flags_req_event|afb_hook_flags_req_subcall)
58 #define afb_hook_flags_req_extra        (afb_hook_flags_req_common|afb_hook_flags_req_ref|afb_hook_flags_req_context)
59 #define afb_hook_flags_req_all          (afb_hook_flags_req_extra|afb_hook_flags_req_internal)
60
61 struct req;
62 struct afb_context;
63 struct json_object;
64 struct afb_arg;
65 struct afb_event;
66 struct afb_session;
67 struct afb_xreq;
68
69 struct afb_hook;
70
71 struct afb_hook_xreq_itf {
72         void (*hook_xreq_begin)(void * closure, const struct afb_xreq *xreq);
73         void (*hook_xreq_end)(void * closure, const struct afb_xreq *xreq);
74         void (*hook_xreq_json)(void * closure, const struct afb_xreq *xreq, struct json_object *obj);
75         void (*hook_xreq_get)(void * closure, const struct afb_xreq *xreq, const char *name, struct afb_arg arg);
76         void (*hook_xreq_success)(void * closure, const struct afb_xreq *xreq, struct json_object *obj, const char *info);
77         void (*hook_xreq_fail)(void * closure, const struct afb_xreq *xreq, const char *status, const char *info);
78         void (*hook_xreq_raw)(void * closure, const struct afb_xreq *xreq, const char *buffer, size_t size);
79         void (*hook_xreq_send)(void * closure, const struct afb_xreq *xreq, const char *buffer, size_t size);
80         void (*hook_xreq_context_get)(void * closure, const struct afb_xreq *xreq, void *value);
81         void (*hook_xreq_context_set)(void * closure, const struct afb_xreq *xreq, void *value, void (*free_value)(void*));
82         void (*hook_xreq_addref)(void * closure, const struct afb_xreq *xreq);
83         void (*hook_xreq_unref)(void * closure, const struct afb_xreq *xreq);
84         void (*hook_xreq_session_close)(void * closure, const struct afb_xreq *xreq);
85         void (*hook_xreq_session_set_LOA)(void * closure, const struct afb_xreq *xreq, unsigned level, int result);
86         void (*hook_xreq_subscribe)(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result);
87         void (*hook_xreq_unsubscribe)(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result);
88         void (*hook_xreq_subcall)(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args);
89         void (*hook_xreq_subcall_result)(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result);
90 };
91
92 extern void afb_hook_init_xreq(struct afb_xreq *xreq);
93
94 extern struct afb_hook *afb_hook_xreq_create(const char *api, const char *verb, struct afb_session *session, unsigned flags, struct afb_hook_xreq_itf *itf, void *closure);
95 extern struct afb_hook *afb_hook_addref(struct afb_hook *spec);
96 extern void afb_hook_unref(struct afb_hook *spec);
97
98 /* hooks for xreq */
99 extern void afb_hook_xreq_begin(const struct afb_xreq *xreq);
100 extern void afb_hook_xreq_end(const struct afb_xreq *xreq);
101 extern struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj);
102 extern struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg);
103 extern void afb_hook_xreq_success(const struct afb_xreq *xreq, struct json_object *obj, const char *info);
104 extern void afb_hook_xreq_fail(const struct afb_xreq *xreq, const char *status, const char *info);
105 extern const char *afb_hook_xreq_raw(const struct afb_xreq *xreq, const char *buffer, size_t size);
106 extern void afb_hook_xreq_send(const struct afb_xreq *xreq, const char *buffer, size_t size);
107 extern void *afb_hook_xreq_context_get(const struct afb_xreq *xreq, void *value);
108 extern void afb_hook_xreq_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*));
109 extern void afb_hook_xreq_addref(const struct afb_xreq *xreq);
110 extern void afb_hook_xreq_unref(const struct afb_xreq *xreq);
111 extern void afb_hook_xreq_session_close(const struct afb_xreq *xreq);
112 extern int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result);
113 extern int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_event event, int result);
114 extern int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_event event, int result);
115 extern void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args);
116 extern void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, int status, struct json_object *result);
117