2 * Copyright (C) 2015-2018 "IoT.bzh"
3 * Author "Fulup Ar Foll"
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.
22 #include <json-c/json.h>
24 #define AFB_BINDING_VERSION 3
25 #include <afb/afb-binding.h>
27 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
36 static struct event *events = 0;
38 /* searchs the event of tag */
39 static struct event *event_get(const char *tag)
41 struct event *e = events;
42 while(e && strcmp(e->tag, tag))
47 /* deletes the event of tag */
48 static int event_del(const char *tag)
58 while(*p != e) p = &(*p)->next;
62 afb_event_unref(e->event);
67 /* creates the event of tag */
68 static int event_add(const char *tag, const char *name)
77 e = malloc(strlen(tag) + sizeof *e);
82 e->event = afb_daemon_make_event(name);
83 if (!e->event) { free(e); return -1; }
91 static int event_subscribe(afb_req_t request, const char *tag)
95 return e ? afb_req_subscribe(request, e->event) : -1;
98 static int event_unsubscribe(afb_req_t request, const char *tag)
102 return e ? afb_req_unsubscribe(request, e->event) : -1;
105 static int event_push(struct json_object *args, const char *tag)
109 return e ? afb_event_push(e->event, json_object_get(args)) : -1;
112 static int event_broadcast(struct json_object *args, const char *tag)
116 return e ? afb_event_broadcast(e->event, json_object_get(args)) : -1;
119 // Sample Generic Ping Debug API
120 static void ping(afb_req_t request, json_object *jresp, const char *tag)
122 static int pingcount = 0;
123 json_object *query = afb_req_json(request);
124 afb_req_success_f(request, jresp, "Ping Binder Daemon tag=%s count=%d query=%s", tag, ++pingcount, json_object_to_json_string(query));
127 static void pingSample (afb_req_t request)
129 ping(request, json_object_new_string ("Some String"), "pingSample");
132 static void pingFail (afb_req_t request)
134 afb_req_fail(request, "failed", "Ping Binder Daemon fails");
137 static void pingNull (afb_req_t request)
139 ping(request, NULL, "pingNull");
142 static void pingBug (afb_req_t request)
144 ping(NULL, NULL, "pingBug");
147 static void pingEvent(afb_req_t request)
149 json_object *query = afb_req_json(request);
150 afb_daemon_broadcast_event("event", json_object_get(query));
151 ping(request, json_object_get(query), "event");
155 // For samples https://linuxprograms.wordpress.com/2010/05/20/json-c-libjson-tutorial/
156 static void pingJson (afb_req_t request) {
157 json_object *jresp, *embed;
159 jresp = json_object_new_object();
160 json_object_object_add(jresp, "myString", json_object_new_string ("Some String"));
161 json_object_object_add(jresp, "myInt", json_object_new_int (1234));
163 embed = json_object_new_object();
164 json_object_object_add(embed, "subObjString", json_object_new_string ("Some String"));
165 json_object_object_add(embed, "subObjInt", json_object_new_int (5678));
167 json_object_object_add(jresp,"eobj", embed);
169 ping(request, jresp, "pingJson");
172 static void subcallcb (void *prequest, int status, json_object *object, afb_req_t request)
175 afb_req_fail(request, "failed", json_object_to_json_string(object));
177 afb_req_success(request, json_object_get(object), NULL);
178 afb_req_unref(request);
181 static void subcall (afb_req_t request)
183 const char *api = afb_req_value(request, "api");
184 const char *verb = afb_req_value(request, "verb");
185 const char *args = afb_req_value(request, "args");
186 json_object *object = api && verb && args ? json_tokener_parse(args) : NULL;
189 afb_req_fail(request, "failed", "bad arguments");
191 afb_req_subcall_legacy(request, api, verb, object, subcallcb, NULL);
194 static void subcallreqcb (void *prequest, int status, json_object *object, afb_req_t request)
197 afb_req_fail(request, "failed", json_object_to_json_string(object));
199 afb_req_success(request, json_object_get(object), NULL);
202 static void subcallreq (afb_req_t request)
204 const char *api = afb_req_value(request, "api");
205 const char *verb = afb_req_value(request, "verb");
206 const char *args = afb_req_value(request, "args");
207 json_object *object = api && verb && args ? json_tokener_parse(args) : NULL;
210 afb_req_fail(request, "failed", "bad arguments");
212 afb_req_subcall_legacy(request, api, verb, object, subcallreqcb, NULL);
215 static void subcallsync (afb_req_t request)
218 const char *api = afb_req_value(request, "api");
219 const char *verb = afb_req_value(request, "verb");
220 const char *args = afb_req_value(request, "args");
221 json_object *result, *object = api && verb && args ? json_tokener_parse(args) : NULL;
224 afb_req_fail(request, "failed", "bad arguments");
226 rc = afb_req_subcall_sync_legacy(request, api, verb, object, &result);
228 afb_req_success(request, result, NULL);
230 afb_req_fail(request, "failed", json_object_to_json_string(result));
231 json_object_put(result);
236 static void eventadd (afb_req_t request)
238 const char *tag = afb_req_value(request, "tag");
239 const char *name = afb_req_value(request, "name");
241 pthread_mutex_lock(&mutex);
242 if (tag == NULL || name == NULL)
243 afb_req_fail(request, "failed", "bad arguments");
244 else if (0 != event_add(tag, name))
245 afb_req_fail(request, "failed", "creation error");
247 afb_req_success(request, NULL, NULL);
248 pthread_mutex_unlock(&mutex);
251 static void eventdel (afb_req_t request)
253 const char *tag = afb_req_value(request, "tag");
255 pthread_mutex_lock(&mutex);
257 afb_req_fail(request, "failed", "bad arguments");
258 else if (0 != event_del(tag))
259 afb_req_fail(request, "failed", "deletion error");
261 afb_req_success(request, NULL, NULL);
262 pthread_mutex_unlock(&mutex);
265 static void eventsub (afb_req_t request)
267 const char *tag = afb_req_value(request, "tag");
269 pthread_mutex_lock(&mutex);
271 afb_req_fail(request, "failed", "bad arguments");
272 else if (0 != event_subscribe(request, tag))
273 afb_req_fail(request, "failed", "subscription error");
275 afb_req_success(request, NULL, NULL);
276 pthread_mutex_unlock(&mutex);
279 static void eventunsub (afb_req_t request)
281 const char *tag = afb_req_value(request, "tag");
283 pthread_mutex_lock(&mutex);
285 afb_req_fail(request, "failed", "bad arguments");
286 else if (0 != event_unsubscribe(request, tag))
287 afb_req_fail(request, "failed", "unsubscription error");
289 afb_req_success(request, NULL, NULL);
290 pthread_mutex_unlock(&mutex);
293 static void eventpush (afb_req_t request)
295 const char *tag = afb_req_value(request, "tag");
296 const char *data = afb_req_value(request, "data");
297 json_object *object = data ? json_tokener_parse(data) : NULL;
299 pthread_mutex_lock(&mutex);
301 afb_req_fail(request, "failed", "bad arguments");
302 else if (0 > event_push(object, tag))
303 afb_req_fail(request, "failed", "push error");
305 afb_req_success(request, NULL, NULL);
306 pthread_mutex_unlock(&mutex);
307 json_object_put(object);
310 static void callcb (void *prequest, json_object *object, const char *error, const char *info, afb_api_t api)
312 afb_req_t request = afb_req_unstore(prequest);
313 afb_req_reply(request, json_object_get(object), error, info);
314 afb_req_unref(request);
317 static void call (afb_req_t request)
319 const char *api = afb_req_value(request, "api");
320 const char *verb = afb_req_value(request, "verb");
321 const char *args = afb_req_value(request, "args");
322 json_object *object = api && verb && args ? json_tokener_parse(args) : NULL;
325 afb_req_fail(request, "failed", "bad arguments");
327 afb_api_call(request->api, api, verb, object, callcb, afb_req_store(request));
330 static void callsync (afb_req_t request)
333 const char *api = afb_req_value(request, "api");
334 const char *verb = afb_req_value(request, "verb");
335 const char *args = afb_req_value(request, "args");
336 json_object *result, *object = api && verb && args ? json_tokener_parse(args) : NULL;
339 afb_req_fail(request, "failed", "bad arguments");
341 rc = afb_service_call_sync(api, verb, object, &result);
343 afb_req_success(request, result, NULL);
345 afb_req_fail(request, "failed", json_object_to_json_string(result));
346 json_object_put(result);
351 static void verbose (afb_req_t request)
354 json_object *query = afb_req_json(request), *l;
356 if (json_object_is_type(query,json_type_int))
357 level = json_object_get_int(query);
358 else if (json_object_object_get_ex(query, "level", &l) && json_object_is_type(l, json_type_int))
359 level = json_object_get_int(l);
361 if (!json_object_object_get_ex(query,"message",&l))
364 AFB_REQ_VERBOSE(request, level, "verbose called for %s", json_object_get_string(l));
365 afb_req_success(request, NULL, NULL);
368 static void exitnow (afb_req_t request)
371 json_object *query = afb_req_json(request), *l;
373 if (json_object_is_type(query,json_type_int))
374 code = json_object_get_int(query);
375 else if (json_object_object_get_ex(query, "code", &l) && json_object_is_type(l, json_type_int))
376 code = json_object_get_int(l);
378 if (!json_object_object_get_ex(query,"reason",&l))
381 AFB_REQ_NOTICE(request, "in phase of exiting with code %d, reason: %s", code, l ? json_object_get_string(l) : "unknown");
382 afb_req_success(request, NULL, NULL);
386 static void broadcast(afb_req_t request)
388 const char *tag = afb_req_value(request, "tag");
389 const char *name = afb_req_value(request, "name");
390 const char *data = afb_req_value(request, "data");
391 json_object *object = data ? json_tokener_parse(data) : NULL;
394 pthread_mutex_lock(&mutex);
395 if (0 > event_broadcast(object, tag))
396 afb_req_fail(request, "failed", "broadcast error");
398 afb_req_success(request, NULL, NULL);
399 pthread_mutex_unlock(&mutex);
400 } else if (name != NULL) {
401 if (0 > afb_daemon_broadcast_event(name, object))
402 afb_req_fail(request, "failed", "broadcast error");
404 afb_req_success(request, NULL, NULL);
406 afb_req_fail(request, "failed", "bad arguments");
408 json_object_put(object);
411 static void hasperm (afb_req_t request)
413 const char *perm = afb_req_value(request, "perm");
414 if (afb_req_has_permission(request, perm))
415 afb_req_success_f(request, NULL, "permission %s granted", perm?:"(null)");
417 afb_req_fail_f(request, "not-granted", "permission %s NOT granted", perm?:"(null)");
420 static void appid (afb_req_t request)
422 char *aid = afb_req_get_application_id(request);
423 afb_req_success_f(request, aid ? json_object_new_string(aid) : NULL, "application is %s", aid?:"?");
427 static void uid (afb_req_t request)
429 int uid = afb_req_get_uid(request);
430 afb_req_success_f(request, json_object_new_int(uid), "uid is %d", uid);
435 AFB_NOTICE("hello binding comes to live");
441 AFB_NOTICE("hello binding starting");
445 static void onevent(afb_api_t api, const char *event, struct json_object *object)
447 AFB_NOTICE("received event %s(%s)", event, json_object_to_json_string(object));
450 // NOTE: this sample does not use session to keep test a basic as possible
451 // in real application most APIs should be protected with AFB_SESSION_CHECK
452 static const struct afb_verb_v3 verbs[]= {
453 { .verb="ping", .callback=pingSample },
454 { .verb="pingfail", .callback=pingFail },
455 { .verb="pingnull", .callback=pingNull },
456 { .verb="pingbug", .callback=pingBug },
457 { .verb="pingJson", .callback=pingJson },
458 { .verb="pingevent", .callback=pingEvent },
459 { .verb="subcall", .callback=subcall },
460 { .verb="subcallreq", .callback=subcallreq },
461 { .verb="subcallsync", .callback=subcallsync },
462 { .verb="eventadd", .callback=eventadd },
463 { .verb="eventdel", .callback=eventdel },
464 { .verb="eventsub", .callback=eventsub },
465 { .verb="eventunsub", .callback=eventunsub },
466 { .verb="eventpush", .callback=eventpush },
467 { .verb="call", .callback=call },
468 { .verb="callsync", .callback=callsync },
469 { .verb="verbose", .callback=verbose },
470 { .verb="broadcast", .callback=broadcast },
471 { .verb="hasperm", .callback=hasperm },
472 { .verb="appid", .callback=appid },
473 { .verb="uid", .callback=uid },
474 { .verb="exit", .callback=exitnow },
478 const struct afb_binding_v3 afbBindingV3 = {
480 .specification = NULL,