Finalize 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_begin                 0x000001
22 #define afb_hook_flag_req_end                   0x000002
23 #define afb_hook_flag_req_json                  0x000004
24 #define afb_hook_flag_req_get                   0x000008
25 #define afb_hook_flag_req_success               0x000010
26 #define afb_hook_flag_req_fail                  0x000020
27 #define afb_hook_flag_req_raw                   0x000040
28 #define afb_hook_flag_req_send                  0x000080
29 #define afb_hook_flag_req_context_get           0x000100
30 #define afb_hook_flag_req_context_set           0x000200
31 #define afb_hook_flag_req_addref                0x000400
32 #define afb_hook_flag_req_unref                 0x000800
33 #define afb_hook_flag_req_session_close         0x001000
34 #define afb_hook_flag_req_session_set_LOA       0x002000
35 #define afb_hook_flag_req_subscribe             0x004000
36 #define afb_hook_flag_req_unsubscribe           0x010800
37 #define afb_hook_flag_req_subcall               0x020000
38 #define afb_hook_flag_req_subcall_result        0x040000
39 #define afb_hook_flag_req_subcallsync           0x080000
40 #define afb_hook_flag_req_subcallsync_result    0x100000
41
42 /* common flags */
43 #define afb_hook_flags_req_life         (afb_hook_flag_req_begin|afb_hook_flag_req_end)
44 #define afb_hook_flags_req_args         (afb_hook_flag_req_json|afb_hook_flag_req_get)
45 #define afb_hook_flags_req_result       (afb_hook_flag_req_success|afb_hook_flag_req_fail)
46 #define afb_hook_flags_req_session      (afb_hook_flag_req_session_close|afb_hook_flag_req_session_set_LOA)
47 #define afb_hook_flags_req_event        (afb_hook_flag_req_subscribe|afb_hook_flag_req_unsubscribe)
48 #define afb_hook_flags_req_subcall      (afb_hook_flag_req_subcall|afb_hook_flag_req_subcall_result\
49                                         |afb_hook_flag_req_subcallsync|afb_hook_flag_req_subcallsync_result)
50
51 /* extra flags */
52 #define afb_hook_flags_req_ref          (afb_hook_flag_req_addref|afb_hook_flag_req_unref)
53 #define afb_hook_flags_req_context      (afb_hook_flag_req_context_get|afb_hook_flag_req_context_set)
54
55 /* internal flags */
56 #define afb_hook_flags_req_internal     (afb_hook_flag_req_raw|afb_hook_flag_req_send)
57
58 /* predefined groups */
59 #define afb_hook_flags_req_common       (afb_hook_flags_req_life|afb_hook_flags_req_args|afb_hook_flags_req_result\
60                                         |afb_hook_flags_req_session|afb_hook_flags_req_event|afb_hook_flags_req_subcall)
61 #define afb_hook_flags_req_extra        (afb_hook_flags_req_common|afb_hook_flags_req_ref|afb_hook_flags_req_context)
62 #define afb_hook_flags_req_all          (afb_hook_flags_req_extra|afb_hook_flags_req_internal)
63
64 struct req;
65 struct afb_context;
66 struct json_object;
67 struct afb_arg;
68 struct afb_event;
69 struct afb_session;
70 struct afb_xreq;
71
72 struct afb_hook;
73
74 struct afb_hook_xreq_itf {
75         void (*hook_xreq_begin)(void * closure, const struct afb_xreq *xreq);
76         void (*hook_xreq_end)(void * closure, const struct afb_xreq *xreq);
77         void (*hook_xreq_json)(void * closure, const struct afb_xreq *xreq, struct json_object *obj);
78         void (*hook_xreq_get)(void * closure, const struct afb_xreq *xreq, const char *name, struct afb_arg arg);
79         void (*hook_xreq_success)(void * closure, const struct afb_xreq *xreq, struct json_object *obj, const char *info);
80         void (*hook_xreq_fail)(void * closure, const struct afb_xreq *xreq, const char *status, const char *info);
81         void (*hook_xreq_raw)(void * closure, const struct afb_xreq *xreq, const char *buffer, size_t size);
82         void (*hook_xreq_send)(void * closure, const struct afb_xreq *xreq, const char *buffer, size_t size);
83         void (*hook_xreq_context_get)(void * closure, const struct afb_xreq *xreq, void *value);
84         void (*hook_xreq_context_set)(void * closure, const struct afb_xreq *xreq, void *value, void (*free_value)(void*));
85         void (*hook_xreq_addref)(void * closure, const struct afb_xreq *xreq);
86         void (*hook_xreq_unref)(void * closure, const struct afb_xreq *xreq);
87         void (*hook_xreq_session_close)(void * closure, const struct afb_xreq *xreq);
88         void (*hook_xreq_session_set_LOA)(void * closure, const struct afb_xreq *xreq, unsigned level, int result);
89         void (*hook_xreq_subscribe)(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result);
90         void (*hook_xreq_unsubscribe)(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result);
91         void (*hook_xreq_subcall)(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args);
92         void (*hook_xreq_subcall_result)(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result);
93         void (*hook_xreq_subcallsync)(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args);
94         void (*hook_xreq_subcallsync_result)(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result);
95 };
96
97 extern void afb_hook_init_xreq(struct afb_xreq *xreq);
98
99 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);
100 extern struct afb_hook *afb_hook_addref(struct afb_hook *spec);
101 extern void afb_hook_unref(struct afb_hook *spec);
102
103 /* hooks for xreq */
104 extern void afb_hook_xreq_begin(const struct afb_xreq *xreq);
105 extern void afb_hook_xreq_end(const struct afb_xreq *xreq);
106 extern struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj);
107 extern struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg);
108 extern void afb_hook_xreq_success(const struct afb_xreq *xreq, struct json_object *obj, const char *info);
109 extern void afb_hook_xreq_fail(const struct afb_xreq *xreq, const char *status, const char *info);
110 extern const char *afb_hook_xreq_raw(const struct afb_xreq *xreq, const char *buffer, size_t size);
111 extern void afb_hook_xreq_send(const struct afb_xreq *xreq, const char *buffer, size_t size);
112 extern void *afb_hook_xreq_context_get(const struct afb_xreq *xreq, void *value);
113 extern void afb_hook_xreq_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*));
114 extern void afb_hook_xreq_addref(const struct afb_xreq *xreq);
115 extern void afb_hook_xreq_unref(const struct afb_xreq *xreq);
116 extern void afb_hook_xreq_session_close(const struct afb_xreq *xreq);
117 extern int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result);
118 extern int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_event event, int result);
119 extern int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_event event, int result);
120 extern void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args);
121 extern void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, int status, struct json_object *result);
122 extern void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args);
123 extern int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *result);
124