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 NO_BINDING_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()
39 return afb_api_so_v2_add_binding(&_afb_binding_v2_, NULL, main_apiset);
42 /******************************************************************************
43 **** Monitoring verbosity
44 ******************************************************************************/
46 static const char _debug_[] = "debug";
47 static const char _info_[] = "info";
48 static const char _notice_[] = "notice";
49 static const char _warning_[] = "warning";
50 static const char _error_[] = "error";
53 * Translate verbosity indication to an integer value.
54 * @param v the verbosity indication
55 * @return the verbosity level (0, 1, 2 or 3) or -1 in case of error
57 static int decode_verbosity(struct json_object *v)
61 if (json_object_is_type(v, json_type_int)) {
62 level = json_object_get_int(v);
63 level = level < 0 ? 0 : level > 3 ? 3 : level;
64 } else if (json_object_is_type(v, json_type_string)) {
65 s = json_object_get_string(v);
68 if (!strcasecmp(s, _debug_))
72 if (!strcasecmp(s, _info_))
76 if (!strcasecmp(s, _notice_))
80 if (!strcasecmp(s, _warning_))
84 if (!strcasecmp(s, _error_))
93 * callback for setting verbosity on all apis
94 * @param set the apiset
95 * @param the name of the api to set
96 * @param closure the verbosity to set as an integer casted to a pointer
98 static void set_verbosity_to_all_cb(struct afb_apiset *set, const char *name, void *closure)
100 afb_apiset_set_verbosity(set, name, (int)(intptr_t)closure);
104 * set the verbosity 'level' of the api of 'name'
105 * @param name the api name or "*" for any api or NULL or "" for global verbosity
106 * @param level the verbosity level to set
108 static void set_verbosity_to(const char *name, int level)
110 if (!name || !name[0])
112 else if (name[0] == '*' && !name[1])
113 afb_apiset_enum(main_apiset, set_verbosity_to_all_cb, (void*)(intptr_t)level);
115 afb_apiset_set_verbosity(main_apiset, name, level);
119 * Set verbosities accordling to specification in 'spec'
120 * @param spec specification of the verbosity to set
122 static void set_verbosity(struct json_object *spec)
125 struct json_object_iterator it, end;
127 if (json_object_is_type(spec, json_type_object)) {
128 it = json_object_iter_begin(spec);
129 end = json_object_iter_end(spec);
130 while (!json_object_iter_equal(&it, &end)) {
131 l = decode_verbosity(json_object_iter_peek_value(&it));
133 set_verbosity_to(json_object_iter_peek_name(&it), l);
134 json_object_iter_next(&it);
137 l = decode_verbosity(spec);
139 set_verbosity_to("", l);
140 set_verbosity_to("*", l);
146 * Translate verbosity level to a protocol indication.
147 * @param level the verbosity
148 * @return the encoded verbosity
150 static struct json_object *encode_verbosity(int level)
153 case 0: return json_object_new_string(_error_);
154 case 1: return json_object_new_string(_notice_);
155 case 2: return json_object_new_string(_info_);
156 case 3: return json_object_new_string(_debug_);
157 default: return json_object_new_int(level);
162 * callback for getting verbosity of all apis
163 * @param set the apiset
164 * @param the name of the api to set
165 * @param closure the json object to build
167 static void get_verbosity_of_all_cb(struct afb_apiset *set, const char *name, void *closure)
169 struct json_object *resu = closure;
170 int l = afb_apiset_get_verbosity(set, name);
172 json_object_object_add(resu, name, encode_verbosity(l));
176 * get in resu the verbosity of the api of 'name'
177 * @param resu the json object to build
178 * @param name the api name or "*" for any api or NULL or "" for global verbosity
180 static void get_verbosity_of(struct json_object *resu, const char *name)
183 if (!name || !name[0])
184 json_object_object_add(resu, "", encode_verbosity(verbosity));
185 else if (name[0] == '*' && !name[1])
186 afb_apiset_enum(main_apiset, get_verbosity_of_all_cb, resu);
188 l = afb_apiset_get_verbosity(main_apiset, name);
190 json_object_object_add(resu, name, encode_verbosity(l));
195 * get verbosities accordling to specification in 'spec'
196 * @param resu the json object to build
197 * @param spec specification of the verbosity to set
199 static void get_verbosity(struct json_object *resu, struct json_object *spec)
202 struct json_object_iterator it, end;
204 if (json_object_is_type(spec, json_type_object)) {
205 it = json_object_iter_begin(spec);
206 end = json_object_iter_end(spec);
207 while (!json_object_iter_equal(&it, &end)) {
208 get_verbosity_of(resu, json_object_iter_peek_name(&it));
209 json_object_iter_next(&it);
211 } else if (json_object_is_type(spec, json_type_array)) {
212 n = json_object_array_length(spec);
213 for (i = 0 ; i < n ; i++)
214 get_verbosity_of(resu, json_object_get_string(json_object_array_get_idx(spec, i)));
215 } else if (json_object_get_boolean(spec)) {
216 get_verbosity_of(resu, "");
217 get_verbosity_of(resu, "*");
221 /******************************************************************************
223 ******************************************************************************/
226 * get apis accordling to specification in 'spec'
227 * @param resu the json object to build
228 * @param spec specification of the verbosity to set
230 static void get_one_api(struct json_object *resu, const char *name, struct json_object *spec)
232 struct json_object *o;
236 rc = afb_apiset_lookup(main_apiset, name, &api);
238 o = api.itf->describe ? api.itf->describe(api.closure) : NULL;
239 json_object_object_add(resu, name, o);
244 * callback for getting verbosity of all apis
245 * @param set the apiset
246 * @param the name of the api to set
247 * @param closure the json object to build
249 static void get_apis_of_all_cb(struct afb_apiset *set, const char *name, void *closure)
251 struct json_object *resu = closure;
252 get_one_api(resu, name, NULL);
256 * get apis accordling to specification in 'spec'
257 * @param resu the json object to build
258 * @param spec specification of the verbosity to set
260 static void get_apis(struct json_object *resu, struct json_object *spec)
263 struct json_object_iterator it, end;
265 if (json_object_is_type(spec, json_type_object)) {
266 it = json_object_iter_begin(spec);
267 end = json_object_iter_end(spec);
268 while (!json_object_iter_equal(&it, &end)) {
269 get_one_api(resu, json_object_iter_peek_name(&it), json_object_iter_peek_value(&it));
270 json_object_iter_next(&it);
272 } else if (json_object_is_type(spec, json_type_array)) {
273 n = json_object_array_length(spec);
274 for (i = 0 ; i < n ; i++)
275 get_one_api(resu, json_object_get_string(json_object_array_get_idx(spec, i)), NULL);
276 } else if (json_object_get_boolean(spec)) {
277 afb_apiset_enum(main_apiset, get_apis_of_all_cb, resu);
281 /******************************************************************************
282 **** Implementation monitoring verbs
283 ******************************************************************************/
285 static const char _verbosity_[] = "verbosity";
286 static const char _apis_[] = "apis";
288 static void f_get(struct afb_req req)
290 struct json_object *o, *v, *r, *x;
292 r = json_object_new_object();
293 o = afb_req_json(req);
295 if (json_object_object_get_ex(o, _verbosity_, &v)) {
296 x = json_object_new_object();
297 json_object_object_add(r, _verbosity_, x);
301 if (json_object_object_get_ex(o, _apis_, &v)) {
302 x = json_object_new_object();
303 json_object_object_add(r, _apis_, x);
307 afb_req_success(req, json_object_get(r), NULL);
311 static void f_set(struct afb_req req)
313 struct json_object *o, *v;
315 o = afb_req_json(req);
316 if (json_object_object_get_ex(o, _verbosity_, &v)) {
320 afb_req_success(req, NULL, NULL);
324 static void f_hook(struct afb_xreq *xreq)
326 struct json_object *o, *v;
328 o = afb_xreq_json(xreq);
329 if (json_object_object_get_ex(o, _verbosity_, &v)) {
334 afb_xreq_success(xreq, NULL, NULL);