2 * Copyright (C) 2018 "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 { "start", afb_hook_flag_api_start },
116 { "vverbose", afb_hook_flag_api_vverbose },
119 static struct flag evt_flags[] = { /* must be sorted by names */
120 { "addref", afb_hook_flag_evt_addref },
121 { "all", afb_hook_flags_evt_all },
122 { "broadcast_after", afb_hook_flag_evt_broadcast_after },
123 { "broadcast_before", afb_hook_flag_evt_broadcast_before },
124 { "common", afb_hook_flags_evt_common },
125 { "create", afb_hook_flag_evt_create },
126 { "extra", afb_hook_flags_evt_extra },
127 { "name", afb_hook_flag_evt_name },
128 { "push_after", afb_hook_flag_evt_push_after },
129 { "push_before", afb_hook_flag_evt_push_before },
130 { "unref", afb_hook_flag_evt_unref },
133 static struct flag session_flags[] = { /* must be sorted by names */
134 { "addref", afb_hook_flag_session_addref },
135 { "all", afb_hook_flags_session_all },
136 { "close", afb_hook_flag_session_close },
137 { "common", afb_hook_flags_session_common },
138 { "create", afb_hook_flag_session_create },
139 { "destroy", afb_hook_flag_session_destroy },
140 { "renew", afb_hook_flag_session_renew },
141 { "unref", afb_hook_flag_session_unref },
144 static struct flag global_flags[] = { /* must be sorted by names */
145 { "all", afb_hook_flags_global_all },
146 { "vverbose", afb_hook_flag_global_vverbose },
149 static int compare(const char *query, const char *value, size_t query_length)
154 for (i = 0 ; i < query_length ; i++) {
159 v = v == '_' ? '-' : (char)toupper(v);
160 q = q == '_' ? '-' : (char)toupper(q);
162 return (int)((unsigned)q - (unsigned)v);
167 /* get the value of the flag of 'name' in the array 'flags' of 'count elements */
168 static int get_flag(const char *name, struct flags flags, size_t length)
170 /* replace "*" by "all" */
171 if (length == 1 && *name == '*') {
176 /* dichotomic search */
177 int lower = 0, upper = flags.count;
178 while (lower < upper) {
179 int mid = (lower + upper) >> 1;
180 int cmp = compare(name, flags.flags[mid].name, length);
182 return flags.flags[mid].value;
189 return -(compare(name, "no", length) && compare(name, "none", length));
192 static int from_text(const char *text, struct flags flags)
194 static const char sep[] = " \t,";
200 text += strspn(text, sep);
203 s = strcspn(text, sep);
204 val = get_flag(text, flags, s);
214 static char *to_text(int value, struct flags flags)
216 int borrow = 0, mask = 0, i, v, imask;
221 return strdup("none");
225 result = malloc(s + 1);
230 while (borrow != value) {
234 v = flags.flags[--i].value;
235 if ((mask & v) == mask && (borrow & v) == 0 && (value & v) == v) {
244 s += strlen(flags.flags[imask].name) + !!s;
248 strcpy(&result[s], flags.flags[imask].name);
249 s += strlen(flags.flags[imask].name);
257 int afb_hook_flags_xreq_from_text(const char *text)
259 return from_text(text, FLAGS(xreq_flags));
262 int afb_hook_flags_api_from_text(const char *text)
264 return from_text(text, FLAGS(api_flags));
267 int afb_hook_flags_evt_from_text(const char *text)
269 return from_text(text, FLAGS(evt_flags));
272 int afb_hook_flags_session_from_text(const char *text)
274 return from_text(text, FLAGS(session_flags));
277 int afb_hook_flags_global_from_text(const char *text)
279 return from_text(text, FLAGS(global_flags));
282 char *afb_hook_flags_xreq_to_text(int value)
284 return to_text(value, FLAGS(xreq_flags));
287 char *afb_hook_flags_api_to_text(int value)
289 return to_text(value, FLAGS(api_flags));
292 char *afb_hook_flags_evt_to_text(int value)
294 return to_text(value, FLAGS(evt_flags));
297 char *afb_hook_flags_session_to_text(int value)
299 return to_text(value, FLAGS(session_flags));
302 char *afb_hook_flags_global_to_text(int value)
304 return to_text(value, FLAGS(global_flags));
307 #if !defined(REMOVE_LEGACY_TRACE)
308 static struct flag legacy_ditf_flags[] = { /* must be sorted by names */
309 { "all", afb_hook_flags_api_ditf_all },
310 { "common", afb_hook_flags_api_ditf_common },
311 { "event_broadcast_after", afb_hook_flag_api_event_broadcast },
312 { "event_broadcast_before", afb_hook_flag_api_event_broadcast },
313 { "event_make", afb_hook_flag_api_event_make },
314 { "extra", afb_hook_flags_api_ditf_extra },
315 { "get_event_loop", afb_hook_flag_api_get_event_loop },
316 { "get_system_bus", afb_hook_flag_api_get_system_bus },
317 { "get_user_bus", afb_hook_flag_api_get_user_bus },
318 { "queue_job", afb_hook_flag_api_queue_job },
319 { "require_api", afb_hook_flag_api_require_api },
320 { "require_api_result", afb_hook_flag_api_require_api },
321 { "rootdir_get_fd", afb_hook_flag_api_rootdir_get_fd },
322 { "rootdir_open_locale", afb_hook_flag_api_rootdir_open_locale },
323 { "unstore_req", afb_hook_flag_api_legacy_unstore_req },
324 { "vverbose", afb_hook_flag_api_vverbose },
327 static struct flag legacy_svc_flags[] = { /* must be sorted by names */
328 { "all", afb_hook_flags_api_svc_all },
329 { "call", afb_hook_flag_api_call },
330 { "call_result", afb_hook_flag_api_call },
331 { "callsync", afb_hook_flag_api_callsync },
332 { "callsync_result", afb_hook_flag_api_callsync },
333 { "on_event_after", afb_hook_flag_api_on_event },
334 { "on_event_before", afb_hook_flag_api_on_event },
335 { "start_after", afb_hook_flag_api_start },
336 { "start_before", afb_hook_flag_api_start },
339 int afb_hook_flags_legacy_ditf_from_text(const char *text)
341 return from_text(text, FLAGS(legacy_ditf_flags));
344 int afb_hook_flags_legacy_svc_from_text(const char *text)
346 return from_text(text, FLAGS(legacy_svc_flags));
349 char *afb_hook_flags_legacy_ditf_to_text(int value)
351 return to_text(value, FLAGS(legacy_ditf_flags));
354 char *afb_hook_flags_legacy_svc_to_text(int value)
356 return to_text(value, FLAGS(legacy_svc_flags));