2 * Copyright (C) 2016, 2017 "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.
19 #define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
23 #include <json-c/json.h>
24 #include <afb/afb-binding.h>
27 #include "afb-apiset.h"
28 #include "afb-api-so-v2.h"
33 #include "monitor-api.inc"
35 extern struct afb_apiset *main_apiset;
37 int afb_monitor_init()
40 return afb_api_so_v2_add_binding(&_afb_binding_v2_monitor, NULL, main_apiset, &v);
43 /******************************************************************************
44 **** Monitoring verbosity
45 ******************************************************************************/
47 static const char _debug_[] = "debug";
48 static const char _info_[] = "info";
49 static const char _notice_[] = "notice";
50 static const char _warning_[] = "warning";
51 static const char _error_[] = "error";
54 * Translate verbosity indication to an integer value.
55 * @param v the verbosity indication
56 * @return the verbosity level (0, 1, 2 or 3) or -1 in case of error
58 static int decode_verbosity(struct json_object *v)
62 if (json_object_is_type(v, json_type_int)) {
63 level = json_object_get_int(v);
64 level = level < 0 ? 0 : level > 3 ? 3 : level;
65 } else if (json_object_is_type(v, json_type_string)) {
66 s = json_object_get_string(v);
69 if (!strcasecmp(s, _debug_))
73 if (!strcasecmp(s, _info_))
77 if (!strcasecmp(s, _notice_))
81 if (!strcasecmp(s, _warning_))
85 if (!strcasecmp(s, _error_))
94 * callback for setting verbosity on all apis
95 * @param set the apiset
96 * @param the name of the api to set
97 * @param closure the verbosity to set as an integer casted to a pointer
99 static void set_verbosity_to_all_cb(struct afb_apiset *set, const char *name, void *closure)
101 afb_apiset_set_verbosity(set, name, (int)(intptr_t)closure);
105 * set the verbosity 'level' of the api of 'name'
106 * @param name the api name or "*" for any api or NULL or "" for global verbosity
107 * @param level the verbosity level to set
109 static void set_verbosity_to(const char *name, int level)
111 if (!name || !name[0])
113 else if (name[0] == '*' && !name[1])
114 afb_apiset_enum(main_apiset, set_verbosity_to_all_cb, (void*)(intptr_t)level);
116 afb_apiset_set_verbosity(main_apiset, name, level);
120 * Set verbosities accordling to specification in 'spec'
121 * @param spec specification of the verbosity to set
123 static void set_verbosity(struct json_object *spec)
126 struct json_object_iterator it, end;
128 if (json_object_is_type(spec, json_type_object)) {
129 it = json_object_iter_begin(spec);
130 end = json_object_iter_end(spec);
131 while (!json_object_iter_equal(&it, &end)) {
132 l = decode_verbosity(json_object_iter_peek_value(&it));
134 set_verbosity_to(json_object_iter_peek_name(&it), l);
135 json_object_iter_next(&it);
138 l = decode_verbosity(spec);
140 set_verbosity_to("", l);
141 set_verbosity_to("*", l);
147 * Translate verbosity level to a protocol indication.
148 * @param level the verbosity
149 * @return the encoded verbosity
151 static struct json_object *encode_verbosity(int level)
154 case 0: return json_object_new_string(_error_);
155 case 1: return json_object_new_string(_notice_);
156 case 2: return json_object_new_string(_info_);
157 case 3: return json_object_new_string(_debug_);
158 default: return json_object_new_int(level);
163 * callback for getting verbosity of all apis
164 * @param set the apiset
165 * @param the name of the api to set
166 * @param closure the json object to build
168 static void get_verbosity_of_all_cb(struct afb_apiset *set, const char *name, void *closure)
170 struct json_object *resu = closure;
171 int l = afb_apiset_get_verbosity(set, name);
173 json_object_object_add(resu, name, encode_verbosity(l));
177 * get in resu the verbosity of the api of 'name'
178 * @param resu the json object to build
179 * @param name the api name or "*" for any api or NULL or "" for global verbosity
181 static void get_verbosity_of(struct json_object *resu, const char *name)
184 if (!name || !name[0])
185 json_object_object_add(resu, "", encode_verbosity(verbosity));
186 else if (name[0] == '*' && !name[1])
187 afb_apiset_enum(main_apiset, get_verbosity_of_all_cb, resu);
189 l = afb_apiset_get_verbosity(main_apiset, name);
191 json_object_object_add(resu, name, encode_verbosity(l));
196 * get verbosities accordling to specification in 'spec'
197 * @param resu the json object to build
198 * @param spec specification of the verbosity to set
200 static void get_verbosity(struct json_object *resu, struct json_object *spec)
203 struct json_object_iterator it, end;
205 if (json_object_is_type(spec, json_type_object)) {
206 it = json_object_iter_begin(spec);
207 end = json_object_iter_end(spec);
208 while (!json_object_iter_equal(&it, &end)) {
209 get_verbosity_of(resu, json_object_iter_peek_name(&it));
210 json_object_iter_next(&it);
212 } else if (json_object_is_type(spec, json_type_array)) {
213 n = json_object_array_length(spec);
214 for (i = 0 ; i < n ; i++)
215 get_verbosity_of(resu, json_object_get_string(json_object_array_get_idx(spec, i)));
216 } else if (json_object_get_boolean(spec)) {
217 get_verbosity_of(resu, "");
218 get_verbosity_of(resu, "*");
222 /******************************************************************************
224 ******************************************************************************/
227 * get apis accordling to specification in 'spec'
228 * @param resu the json object to build
229 * @param spec specification of the verbosity to set
231 static void get_one_api(struct json_object *resu, const char *name, struct json_object *spec)
233 struct json_object *o;
237 rc = afb_apiset_lookup(main_apiset, name, &api);
239 o = api.itf->describe ? api.itf->describe(api.closure) : NULL;
240 json_object_object_add(resu, name, o);
245 * callback for getting verbosity of all apis
246 * @param set the apiset
247 * @param the name of the api to set
248 * @param closure the json object to build
250 static void get_apis_of_all_cb(struct afb_apiset *set, const char *name, void *closure)
252 struct json_object *resu = closure;
253 get_one_api(resu, name, NULL);
257 * get apis accordling to specification in 'spec'
258 * @param resu the json object to build
259 * @param spec specification of the verbosity to set
261 static void get_apis(struct json_object *resu, struct json_object *spec)
264 struct json_object_iterator it, end;
266 if (json_object_is_type(spec, json_type_object)) {
267 it = json_object_iter_begin(spec);
268 end = json_object_iter_end(spec);
269 while (!json_object_iter_equal(&it, &end)) {
270 get_one_api(resu, json_object_iter_peek_name(&it), json_object_iter_peek_value(&it));
271 json_object_iter_next(&it);
273 } else if (json_object_is_type(spec, json_type_array)) {
274 n = json_object_array_length(spec);
275 for (i = 0 ; i < n ; i++)
276 get_one_api(resu, json_object_get_string(json_object_array_get_idx(spec, i)), NULL);
277 } else if (json_object_get_boolean(spec)) {
278 afb_apiset_enum(main_apiset, get_apis_of_all_cb, resu);
282 /******************************************************************************
283 **** Implementation monitoring verbs
284 ******************************************************************************/
286 static const char _verbosity_[] = "verbosity";
287 static const char _apis_[] = "apis";
289 static void f_get(struct afb_req req)
291 struct json_object *o, *v, *r, *x;
293 r = json_object_new_object();
294 o = afb_req_json(req);
296 if (json_object_object_get_ex(o, _verbosity_, &v)) {
297 x = json_object_new_object();
298 json_object_object_add(r, _verbosity_, x);
302 if (json_object_object_get_ex(o, _apis_, &v)) {
303 x = json_object_new_object();
304 json_object_object_add(r, _apis_, x);
308 afb_req_success(req, json_object_get(r), NULL);
312 static void f_set(struct afb_req req)
314 struct json_object *o, *v;
316 o = afb_req_json(req);
317 if (json_object_object_get_ex(o, _verbosity_, &v)) {
321 afb_req_success(req, NULL, NULL);
325 static void f_hook(struct afb_xreq *xreq)
327 struct json_object *o, *v;
329 o = afb_xreq_json(xreq);
330 if (json_object_object_get_ex(o, _verbosity_, &v)) {
335 afb_xreq_success(xreq, NULL, NULL);