2 * Copyright (C) 2015, 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.
22 #include <json-c/json.h>
24 #define AFB_BINDING_VERSION 0
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_eventid_unref(e->eventid);
67 /* creates the event of tag */
68 static int event_add(afb_dynapi *dynapi, const char *tag, const char *name)
77 e = malloc(strlen(tag) + sizeof *e);
82 e->eventid = afb_dynapi_make_eventid(dynapi, name);
83 if (!e->eventid) { free(e); return -1; }
91 static int event_subscribe(afb_request *request, const char *tag)
95 return e ? afb_request_subscribe(request, e->eventid) : -1;
98 static int event_unsubscribe(afb_request *request, const char *tag)
102 return e ? afb_request_unsubscribe(request, e->eventid) : -1;
105 static int event_push(struct json_object *args, const char *tag)
109 return e ? afb_eventid_push(e->eventid, json_object_get(args)) : -1;
112 static int event_broadcast(struct json_object *args, const char *tag)
116 return e ? afb_eventid_broadcast(e->eventid, json_object_get(args)) : -1;
119 // Sample Generic Ping Debug API
120 static void ping(afb_request *request, json_object *jresp, const char *tag)
122 static int pingcount = 0;
123 json_object *query = afb_request_json(request);
124 afb_request_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_request *request)
129 ping(request, json_object_new_string ("Some String"), "pingSample");
132 static void pingFail (afb_request *request)
134 afb_request_fail(request, "failed", "Ping Binder Daemon fails");
137 static void pingNull (afb_request *request)
139 ping(request, NULL, "pingNull");
142 static void pingBug (afb_request *request)
144 ping(NULL, NULL, "pingBug");
147 static void pingEvent(afb_request *request)
149 json_object *query = afb_request_json(request);
150 afb_dynapi_broadcast_event(request->dynapi, "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_request *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 *closure, int status, json_object *object, afb_request *request)
175 afb_request_fail(request, "failed", json_object_to_json_string(object));
177 afb_request_success(request, json_object_get(object), NULL);
180 static void subcall (afb_request *request)
182 const char *api = afb_request_value(request, "api");
183 const char *verb = afb_request_value(request, "verb");
184 const char *args = afb_request_value(request, "args");
185 json_object *object = api && verb && args ? json_tokener_parse(args) : NULL;
188 afb_request_fail(request, "failed", "bad arguments");
190 afb_request_subcall(request, api, verb, object, subcallcb, NULL);
193 static void subcallsync (afb_request *request)
196 const char *api = afb_request_value(request, "api");
197 const char *verb = afb_request_value(request, "verb");
198 const char *args = afb_request_value(request, "args");
199 json_object *result, *object = api && verb && args ? json_tokener_parse(args) : NULL;
202 afb_request_fail(request, "failed", "bad arguments");
204 rc = afb_request_subcall_sync(request, api, verb, object, &result);
206 afb_request_success(request, result, NULL);
208 afb_request_fail(request, "failed", json_object_to_json_string(result));
209 json_object_put(result);
214 static void eventadd (afb_request *request)
216 const char *tag = afb_request_value(request, "tag");
217 const char *name = afb_request_value(request, "name");
219 pthread_mutex_lock(&mutex);
220 if (tag == NULL || name == NULL)
221 afb_request_fail(request, "failed", "bad arguments");
222 else if (0 != event_add(request->dynapi, tag, name))
223 afb_request_fail(request, "failed", "creation error");
225 afb_request_success(request, NULL, NULL);
226 pthread_mutex_unlock(&mutex);
229 static void eventdel (afb_request *request)
231 const char *tag = afb_request_value(request, "tag");
233 pthread_mutex_lock(&mutex);
235 afb_request_fail(request, "failed", "bad arguments");
236 else if (0 != event_del(tag))
237 afb_request_fail(request, "failed", "deletion error");
239 afb_request_success(request, NULL, NULL);
240 pthread_mutex_unlock(&mutex);
243 static void eventsub (afb_request *request)
245 const char *tag = afb_request_value(request, "tag");
247 pthread_mutex_lock(&mutex);
249 afb_request_fail(request, "failed", "bad arguments");
250 else if (0 != event_subscribe(request, tag))
251 afb_request_fail(request, "failed", "subscription error");
253 afb_request_success(request, NULL, NULL);
254 pthread_mutex_unlock(&mutex);
257 static void eventunsub (afb_request *request)
259 const char *tag = afb_request_value(request, "tag");
261 pthread_mutex_lock(&mutex);
263 afb_request_fail(request, "failed", "bad arguments");
264 else if (0 != event_unsubscribe(request, tag))
265 afb_request_fail(request, "failed", "unsubscription error");
267 afb_request_success(request, NULL, NULL);
268 pthread_mutex_unlock(&mutex);
271 static void eventpush (afb_request *request)
273 const char *tag = afb_request_value(request, "tag");
274 const char *data = afb_request_value(request, "data");
275 json_object *object = data ? json_tokener_parse(data) : NULL;
277 pthread_mutex_lock(&mutex);
279 afb_request_fail(request, "failed", "bad arguments");
280 else if (0 > event_push(object, tag))
281 afb_request_fail(request, "failed", "push error");
283 afb_request_success(request, NULL, NULL);
284 pthread_mutex_unlock(&mutex);
285 json_object_put(object);
288 static void callcb (void *prequest, int status, json_object *object, afb_dynapi *dynapi)
290 afb_request *request = prequest;
292 afb_request_fail(request, "failed", json_object_to_json_string(object));
294 afb_request_success(request, json_object_get(object), NULL);
295 afb_request_unref(request);
298 static void call (afb_request *request)
300 const char *api = afb_request_value(request, "api");
301 const char *verb = afb_request_value(request, "verb");
302 const char *args = afb_request_value(request, "args");
303 json_object *object = api && verb && args ? json_tokener_parse(args) : NULL;
306 afb_request_fail(request, "failed", "bad arguments");
308 afb_dynapi_call(request->dynapi, api, verb, object, callcb, afb_request_addref(request));
311 static void callsync (afb_request *request)
314 const char *api = afb_request_value(request, "api");
315 const char *verb = afb_request_value(request, "verb");
316 const char *args = afb_request_value(request, "args");
317 json_object *result, *object = api && verb && args ? json_tokener_parse(args) : NULL;
320 afb_request_fail(request, "failed", "bad arguments");
322 rc = afb_dynapi_call_sync(request->dynapi, api, verb, object, &result);
324 afb_request_success(request, result, NULL);
326 afb_request_fail(request, "failed", json_object_to_json_string(result));
327 json_object_put(result);
332 static void verbose (afb_request *request)
335 json_object *query = afb_request_json(request), *l;
337 if (json_object_is_type(query,json_type_int))
338 level = json_object_get_int(query);
339 else if (json_object_object_get_ex(query, "level", &l) && json_object_is_type(l, json_type_int))
340 level = json_object_get_int(l);
342 if (!json_object_object_get_ex(query,"message",&l))
345 AFB_REQUEST_VERBOSE(request, level, "verbose called for %s", json_object_get_string(l));
346 afb_request_success(request, NULL, NULL);
349 static void exitnow (afb_request *request)
352 json_object *query = afb_request_json(request), *l;
354 if (json_object_is_type(query,json_type_int))
355 code = json_object_get_int(query);
356 else if (json_object_object_get_ex(query, "code", &l) && json_object_is_type(l, json_type_int))
357 code = json_object_get_int(l);
359 if (!json_object_object_get_ex(query,"reason",&l))
362 AFB_REQUEST_NOTICE(request, "in phase of exiting with code %d, reason: %s", code, l ? json_object_get_string(l) : "unknown");
363 afb_request_success(request, NULL, NULL);
367 static void broadcast(afb_request *request)
369 const char *tag = afb_request_value(request, "tag");
370 const char *name = afb_request_value(request, "name");
371 const char *data = afb_request_value(request, "data");
372 json_object *object = data ? json_tokener_parse(data) : NULL;
375 pthread_mutex_lock(&mutex);
376 if (0 > event_broadcast(object, tag))
377 afb_request_fail(request, "failed", "broadcast error");
379 afb_request_success(request, NULL, NULL);
380 pthread_mutex_unlock(&mutex);
381 } else if (name != NULL) {
382 if (0 > afb_dynapi_broadcast_event(request->dynapi, name, object))
383 afb_request_fail(request, "failed", "broadcast error");
385 afb_request_success(request, NULL, NULL);
387 afb_request_fail(request, "failed", "bad arguments");
389 json_object_put(object);
392 static void hasperm (afb_request *request)
394 const char *perm = afb_request_value(request, "perm");
395 if (afb_request_has_permission(request, perm))
396 afb_request_success_f(request, NULL, "permission %s granted", perm?:"(null)");
398 afb_request_fail_f(request, "not-granted", "permission %s NOT granted", perm?:"(null)");
401 static void appid (afb_request *request)
403 char *aid = afb_request_get_application_id(request);
404 afb_request_success_f(request, aid ? json_object_new_string(aid) : NULL, "application is %s", aid?:"?");
408 static int init(afb_dynapi *dynapi)
410 AFB_DYNAPI_NOTICE(dynapi, "dynamic binding AVE(%s) starting", (const char*)dynapi->userdata);
414 static void onevent(afb_dynapi *dynapi, const char *event, struct json_object *object)
416 AFB_DYNAPI_NOTICE(dynapi, "received event %s(%s) by AVE(%s)",
417 event, json_object_to_json_string(object),
418 (const char*)afb_dynapi_get_userdata(dynapi));
421 // NOTE: this sample does not use session to keep test a basic as possible
422 // in real application most APIs should be protected with AFB_SESSION_CHECK
423 static const struct {
425 void (*callback)(afb_request*); } verbs[] =
427 { .verb="ping", .callback=pingSample },
428 { .verb="pingfail", .callback=pingFail },
429 { .verb="pingnull", .callback=pingNull },
430 { .verb="pingbug", .callback=pingBug },
431 { .verb="pingJson", .callback=pingJson },
432 { .verb="pingevent", .callback=pingEvent },
433 { .verb="subcall", .callback=subcall },
434 { .verb="subcallsync", .callback=subcallsync },
435 { .verb="eventadd", .callback=eventadd },
436 { .verb="eventdel", .callback=eventdel },
437 { .verb="eventsub", .callback=eventsub },
438 { .verb="eventunsub", .callback=eventunsub },
439 { .verb="eventpush", .callback=eventpush },
440 { .verb="call", .callback=call },
441 { .verb="callsync", .callback=callsync },
442 { .verb="verbose", .callback=verbose },
443 { .verb="broadcast", .callback=broadcast },
444 { .verb="hasperm", .callback=hasperm },
445 { .verb="appid", .callback=appid },
446 { .verb="exit", .callback=exitnow },
450 static void pingoo(afb_req req)
452 json_object *args = afb_req_json(req);
453 afb_req_success_f(req, json_object_get(args), "You reached pingoo \\o/ nice args: %s", json_object_to_json_string(args));
456 static const afb_verb_v2 verbsv2[]= {
457 { .verb="pingoo", .callback=pingoo },
458 { .verb="ping", .callback=pingoo },
462 static const char *apis[] = { "ave", "hi", "salut", NULL };
464 static int build_api(void *closure, afb_dynapi *dynapi)
468 afb_dynapi_set_userdata(dynapi, closure);
469 AFB_DYNAPI_NOTICE(dynapi, "dynamic binding AVE(%s) comes to live", (const char*)afb_dynapi_get_userdata(dynapi));
470 afb_dynapi_on_init(dynapi, init);
471 afb_dynapi_on_event(dynapi, onevent);
473 rc = afb_dynapi_set_verbs_v2(dynapi, verbsv2);
474 for (i = rc = 0; verbs[i].verb && rc >= 0 ; i++) {
475 rc = afb_dynapi_add_verb(dynapi, verbs[i].verb, NULL, verbs[i].callback, (void*)(intptr_t)i, NULL, 0);
477 afb_dynapi_seal(dynapi);
481 int afbBindingVdyn(afb_dynapi *dynapi)
485 for (i = 0; apis[i] ; i++) {
486 rc = afb_dynapi_new_api(dynapi, apis[i], NULL, 0, build_api, (void*)apis[i]);
488 AFB_DYNAPI_ERROR(dynapi, "can't create API %s", apis[i]);