2 * Copyright (C) 2018, 2019 "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.
25 #include "afb-hook-flags.h"
27 /* structure for searching flags by names */
30 const char *name; /** the name */
31 int value; /** the value */
40 #define FLAGS(x) ((struct flags){ .flags = x, .count = (int)(sizeof x / sizeof * x) })
42 static struct flag xreq_flags[] = { /* must be sorted by names */
43 { "addref", afb_hook_flag_req_addref },
44 { "all", afb_hook_flags_req_all },
45 { "args", afb_hook_flags_req_args },
46 { "begin", afb_hook_flag_req_begin },
47 { "common", afb_hook_flags_req_common },
48 { "context", afb_hook_flags_req_context },
49 { "context_get", afb_hook_flag_req_legacy_context_get },
50 { "context_make", afb_hook_flag_req_context_make },
51 { "context_set", afb_hook_flag_req_legacy_context_set },
52 { "end", afb_hook_flag_req_end },
53 { "event", afb_hook_flags_req_event },
54 { "extra", afb_hook_flags_req_extra },
55 { "get", afb_hook_flag_req_get },
56 { "get_application_id", afb_hook_flag_req_get_application_id },
57 { "get_client_info", afb_hook_flag_req_get_client_info },
58 { "get_uid", afb_hook_flag_req_get_uid },
59 { "has_permission", afb_hook_flag_req_has_permission },
60 { "json", afb_hook_flag_req_json },
61 { "life", afb_hook_flags_req_life },
62 { "ref", afb_hook_flags_req_ref },
63 { "reply", afb_hook_flag_req_reply },
64 { "security", afb_hook_flags_req_security },
65 { "session", afb_hook_flags_req_session },
66 { "session_close", afb_hook_flag_req_session_close },
67 { "session_set_LOA", afb_hook_flag_req_session_set_LOA },
68 { "store", afb_hook_flag_req_legacy_store },
69 { "stores", afb_hook_flags_req_stores },
70 { "subcall", afb_hook_flag_req_subcall },
71 { "subcall_result", afb_hook_flag_req_subcall_result },
72 { "subcalls", afb_hook_flags_req_subcalls },
73 { "subcallsync", afb_hook_flag_req_subcallsync },
74 { "subcallsync_result", afb_hook_flag_req_subcallsync_result },
75 { "subscribe", afb_hook_flag_req_subscribe },
76 { "unref", afb_hook_flag_req_unref },
77 { "unstore", afb_hook_flag_req_legacy_unstore },
78 { "unsubscribe", afb_hook_flag_req_unsubscribe },
79 { "vverbose", afb_hook_flag_req_vverbose },
82 static struct flag api_flags[] = { /* must be sorted by names */
83 { "add_alias", afb_hook_flag_api_add_alias },
84 { "all", afb_hook_flags_api_all },
85 { "api_add_verb", afb_hook_flag_api_api_add_verb },
86 { "api", afb_hook_flags_api_api },
87 { "api_del_verb", afb_hook_flag_api_api_del_verb },
88 { "api_seal", afb_hook_flag_api_api_seal },
89 { "api_set_on_event", afb_hook_flag_api_api_set_on_event },
90 { "api_set_on_init", afb_hook_flag_api_api_set_on_init },
91 { "api_set_verbs", afb_hook_flag_api_api_set_verbs },
92 { "call", afb_hook_flag_api_call },
93 { "callsync", afb_hook_flag_api_callsync },
94 { "class_provide", afb_hook_flag_api_class_provide },
95 { "class_require", afb_hook_flag_api_class_require },
96 { "common", afb_hook_flags_api_common },
97 { "delete_api", afb_hook_flag_api_delete_api },
98 { "event", afb_hook_flags_api_event },
99 { "event_broadcast", afb_hook_flag_api_event_broadcast },
100 { "event_handler_add", afb_hook_flag_api_event_handler_add },
101 { "event_handler_del", afb_hook_flag_api_event_handler_del },
102 { "event_make", afb_hook_flag_api_event_make },
103 { "extra", afb_hook_flags_api_extra },
104 { "get_event_loop", afb_hook_flag_api_get_event_loop },
105 { "get_system_bus", afb_hook_flag_api_get_system_bus },
106 { "get_user_bus", afb_hook_flag_api_get_user_bus },
107 { "legacy_unstore_req", afb_hook_flag_api_legacy_unstore_req },
108 { "new_api", afb_hook_flag_api_new_api },
109 { "on_event", afb_hook_flag_api_on_event },
110 { "on_event_handler", afb_hook_flag_api_on_event_handler },
111 { "queue_job", afb_hook_flag_api_queue_job },
112 { "require_api", afb_hook_flag_api_require_api },
113 { "rootdir_get_fd", afb_hook_flag_api_rootdir_get_fd },
114 { "rootdir_open_locale",afb_hook_flag_api_rootdir_open_locale },
115 { "settings", afb_hook_flag_api_settings },
116 { "start", afb_hook_flag_api_start },
117 { "vverbose", afb_hook_flag_api_vverbose },
120 static struct flag evt_flags[] = { /* must be sorted by names */
121 { "addref", afb_hook_flag_evt_addref },
122 { "all", afb_hook_flags_evt_all },
123 { "broadcast_after", afb_hook_flag_evt_broadcast_after },
124 { "broadcast_before", afb_hook_flag_evt_broadcast_before },
125 { "common", afb_hook_flags_evt_common },
126 { "create", afb_hook_flag_evt_create },
127 { "extra", afb_hook_flags_evt_extra },
128 { "name", afb_hook_flag_evt_name },
129 { "push_after", afb_hook_flag_evt_push_after },
130 { "push_before", afb_hook_flag_evt_push_before },
131 { "unref", afb_hook_flag_evt_unref },
134 static struct flag session_flags[] = { /* must be sorted by names */
135 { "addref", afb_hook_flag_session_addref },
136 { "all", afb_hook_flags_session_all },
137 { "close", afb_hook_flag_session_close },
138 { "common", afb_hook_flags_session_common },
139 { "create", afb_hook_flag_session_create },
140 { "destroy", afb_hook_flag_session_destroy },
141 { "renew", afb_hook_flag_session_renew },
142 { "unref", afb_hook_flag_session_unref },
145 static struct flag global_flags[] = { /* must be sorted by names */
146 { "all", afb_hook_flags_global_all },
147 { "vverbose", afb_hook_flag_global_vverbose },
150 static int compare(const char *query, const char *value, size_t query_length)
155 for (i = 0 ; i < query_length ; i++) {
160 v = v == '_' ? '-' : (char)toupper(v);
161 q = q == '_' ? '-' : (char)toupper(q);
163 return (int)((unsigned)q - (unsigned)v);
168 /* get the value of the flag of 'name' in the array 'flags' of 'count elements */
169 static int get_flag(const char *name, struct flags flags, size_t length)
171 /* replace "*" by "all" */
172 if (length == 1 && *name == '*') {
177 /* dichotomic search */
178 int lower = 0, upper = flags.count;
179 while (lower < upper) {
180 int mid = (lower + upper) >> 1;
181 int cmp = compare(name, flags.flags[mid].name, length);
183 return flags.flags[mid].value;
190 return -(compare(name, "no", length) && compare(name, "none", length));
193 static int from_text(const char *text, struct flags flags)
195 static const char sep[] = " \t,";
201 text += strspn(text, sep);
204 s = strcspn(text, sep);
205 val = get_flag(text, flags, s);
215 static char *to_text(int value, struct flags flags)
217 int borrow = 0, mask = 0, i, v, imask;
222 return strdup("none");
226 result = malloc(s + 1);
231 while (borrow != value) {
235 v = flags.flags[--i].value;
236 if ((mask & v) == mask && (borrow & v) == 0 && (value & v) == v) {
245 s += strlen(flags.flags[imask].name) + !!s;
249 strcpy(&result[s], flags.flags[imask].name);
250 s += strlen(flags.flags[imask].name);
258 int afb_hook_flags_xreq_from_text(const char *text)
260 return from_text(text, FLAGS(xreq_flags));
263 int afb_hook_flags_api_from_text(const char *text)
265 return from_text(text, FLAGS(api_flags));
268 int afb_hook_flags_evt_from_text(const char *text)
270 return from_text(text, FLAGS(evt_flags));
273 int afb_hook_flags_session_from_text(const char *text)
275 return from_text(text, FLAGS(session_flags));
278 int afb_hook_flags_global_from_text(const char *text)
280 return from_text(text, FLAGS(global_flags));
283 char *afb_hook_flags_xreq_to_text(int value)
285 return to_text(value, FLAGS(xreq_flags));
288 char *afb_hook_flags_api_to_text(int value)
290 return to_text(value, FLAGS(api_flags));
293 char *afb_hook_flags_evt_to_text(int value)
295 return to_text(value, FLAGS(evt_flags));
298 char *afb_hook_flags_session_to_text(int value)
300 return to_text(value, FLAGS(session_flags));
303 char *afb_hook_flags_global_to_text(int value)
305 return to_text(value, FLAGS(global_flags));
308 #if !defined(REMOVE_LEGACY_TRACE)
309 static struct flag legacy_ditf_flags[] = { /* must be sorted by names */
310 { "all", afb_hook_flags_api_ditf_all },
311 { "common", afb_hook_flags_api_ditf_common },
312 { "event_broadcast_after", afb_hook_flag_api_event_broadcast },
313 { "event_broadcast_before", afb_hook_flag_api_event_broadcast },
314 { "event_make", afb_hook_flag_api_event_make },
315 { "extra", afb_hook_flags_api_ditf_extra },
316 { "get_event_loop", afb_hook_flag_api_get_event_loop },
317 { "get_system_bus", afb_hook_flag_api_get_system_bus },
318 { "get_user_bus", afb_hook_flag_api_get_user_bus },
319 { "queue_job", afb_hook_flag_api_queue_job },
320 { "require_api", afb_hook_flag_api_require_api },
321 { "require_api_result", afb_hook_flag_api_require_api },
322 { "rootdir_get_fd", afb_hook_flag_api_rootdir_get_fd },
323 { "rootdir_open_locale", afb_hook_flag_api_rootdir_open_locale },
324 { "unstore_req", afb_hook_flag_api_legacy_unstore_req },
325 { "vverbose", afb_hook_flag_api_vverbose },
328 static struct flag legacy_svc_flags[] = { /* must be sorted by names */
329 { "all", afb_hook_flags_api_svc_all },
330 { "call", afb_hook_flag_api_call },
331 { "call_result", afb_hook_flag_api_call },
332 { "callsync", afb_hook_flag_api_callsync },
333 { "callsync_result", afb_hook_flag_api_callsync },
334 { "on_event_after", afb_hook_flag_api_on_event },
335 { "on_event_before", afb_hook_flag_api_on_event },
336 { "start_after", afb_hook_flag_api_start },
337 { "start_before", afb_hook_flag_api_start },
340 int afb_hook_flags_legacy_ditf_from_text(const char *text)
342 return from_text(text, FLAGS(legacy_ditf_flags));
345 int afb_hook_flags_legacy_svc_from_text(const char *text)
347 return from_text(text, FLAGS(legacy_svc_flags));
350 char *afb_hook_flags_legacy_ditf_to_text(int value)
352 return to_text(value, FLAGS(legacy_ditf_flags));
355 char *afb_hook_flags_legacy_svc_to_text(int value)
357 return to_text(value, FLAGS(legacy_svc_flags));