Simplify functions for calls
[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
38 /* common flags */
39 #define afb_hook_flags_req_args         (afb_hook_flag_req_json|afb_hook_flag_req_get)
40 #define afb_hook_flags_req_result       (afb_hook_flag_req_success|afb_hook_flag_req_fail)
41 #define afb_hook_flags_req_session      (afb_hook_flag_req_session_close|afb_hook_flag_req_session_set_LOA)
42 #define afb_hook_flags_req_event        (afb_hook_flag_req_subscribe|afb_hook_flag_req_unsubscribe)
43 #define afb_hook_flags_req_subcall      (afb_hook_flag_req_subcall|afb_hook_flag_req_subcall_result)
44
45 /* extra flags */
46 #define afb_hook_flags_req_ref          (afb_hook_flag_req_addref|afb_hook_flag_req_unref)
47 #define afb_hook_flags_req_context      (afb_hook_flag_req_context_get|afb_hook_flag_req_context_set)
48
49 /* internal flags */
50 #define afb_hook_flags_req_internal     (afb_hook_flag_req_raw|afb_hook_flag_req_send)
51
52 /* predefined groups */
53 #define afb_hook_flags_req_common       (afb_hook_flags_req_args|afb_hook_flags_req_result|afb_hook_flags_req_session|afb_hook_flags_req_event|afb_hook_flags_req_subcall)
54 #define afb_hook_flags_req_extra        (afb_hook_flags_req_common|afb_hook_flags_req_ref|afb_hook_flags_req_context)
55 #define afb_hook_flags_req_all          (afb_hook_flags_req_extra|afb_hook_flags_req_internal)
56
57 struct req;
58 struct afb_context;
59 struct json_object;
60 struct afb_arg;
61 struct afb_event;
62 struct afb_session;
63
64 struct afb_hook;
65 struct afb_hook_req;
66
67 struct afb_hook_req_itf {
68         /* life cycle of the request (no flag, always called) */
69         void (*hook_req_begin)(void * closure, const struct afb_hook_req *tr);
70         void (*hook_req_end)(void * closure, const struct afb_hook_req *tr);
71
72         /* hook of actions on the request, subject to flags */
73         void (*hook_req_json)(void * closure, const struct afb_hook_req *tr, struct json_object *obj);
74         void (*hook_req_get)(void * closure, const struct afb_hook_req *tr, const char *name, struct afb_arg arg);
75         void (*hook_req_success)(void * closure, const struct afb_hook_req *tr, struct json_object *obj, const char *info);
76         void (*hook_req_fail)(void * closure, const struct afb_hook_req *tr, const char *status, const char *info);
77         void (*hook_req_raw)(void * closure, const struct afb_hook_req *tr, const char *buffer, size_t size);
78         void (*hook_req_send)(void * closure, const struct afb_hook_req *tr, const char *buffer, size_t size);
79         void (*hook_req_context_get)(void * closure, const struct afb_hook_req *tr, void *value);
80         void (*hook_req_context_set)(void * closure, const struct afb_hook_req *tr, void *value, void (*free_value)(void*));
81         void (*hook_req_addref)(void * closure, const struct afb_hook_req *tr);
82         void (*hook_req_unref)(void * closure, const struct afb_hook_req *tr);
83         void (*hook_req_session_close)(void * closure, const struct afb_hook_req *tr);
84         void (*hook_req_session_set_LOA)(void * closure, const struct afb_hook_req *tr, unsigned level, int result);
85         void (*hook_req_subscribe)(void * closure, const struct afb_hook_req *tr, struct afb_event event, int result);
86         void (*hook_req_unsubscribe)(void * closure, const struct afb_hook_req *tr, struct afb_event event, int result);
87         void (*hook_req_subcall)(void * closure, const struct afb_hook_req *tr, const char *api, const char *verb, struct json_object *args);
88         void (*hook_req_subcall_result)(void * closure, const struct afb_hook_req *tr, int status, struct json_object *result);
89 };
90
91 extern struct afb_req afb_hook_req_call(struct afb_req req, struct afb_context *context, const char *api, const char *verb);
92
93 extern struct afb_hook *afb_hook_req_create(const char *api, const char *verb, struct afb_session *session, unsigned flags, struct afb_hook_req_itf *itf, void *closure);
94 extern struct afb_hook *afb_hook_addref(struct afb_hook *spec);
95 extern void afb_hook_unref(struct afb_hook *spec);
96