2 * Copyright (C) 2015, 2016, 2017 "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 2
25 #include <afb/afb-binding.h>
27 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
32 struct afb_event event;
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_drop(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.closure) { free(e); return -1; }
91 static int event_subscribe(afb_req request, const char *tag)
95 return e ? afb_req_subscribe(request, e->event) : -1;
98 static int event_unsubscribe(afb_req 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 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 request)
129 ping(request, json_object_new_string ("Some String"), "pingSample");
132 static void pingFail (afb_req request)
134 afb_req_fail(request, "failed", "Ping Binder Daemon fails");
137 static void pingNull (afb_req request)
139 ping(request, NULL, "pingNull");
142 static void pingBug (afb_req request)
144 ping((afb_req){NULL,NULL}, NULL, "pingBug");
147 static void pingEvent(afb_req 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 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)
174 afb_req request = afb_req_unstore(prequest);
176 afb_req_fail(request, "failed", json_object_to_json_string(object));
178 afb_req_success(request, json_object_get(object), NULL);
179 afb_req_unref(request);
182 static void subcall (afb_req request)
184 const char *api = afb_req_value(request, "api");
185 const char *verb = afb_req_value(request, "verb");
186 const char *args = afb_req_value(request, "args");
187 json_object *object = api && verb && args ? json_tokener_parse(args) : NULL;
190 afb_req_fail(request, "failed", "bad arguments");
192 afb_req_subcall(request, api, verb, object, subcallcb, afb_req_store(request));
195 static void subcallsync (afb_req request)
198 const char *api = afb_req_value(request, "api");
199 const char *verb = afb_req_value(request, "verb");
200 const char *args = afb_req_value(request, "args");
201 json_object *result, *object = api && verb && args ? json_tokener_parse(args) : NULL;
204 afb_req_fail(request, "failed", "bad arguments");
206 rc = afb_req_subcall_sync(request, api, verb, object, &result);
208 afb_req_success(request, result, NULL);
210 afb_req_fail(request, "failed", json_object_to_json_string(result));
211 json_object_put(result);
216 static void eventadd (afb_req request)
218 const char *tag = afb_req_value(request, "tag");
219 const char *name = afb_req_value(request, "name");
221 pthread_mutex_lock(&mutex);
222 if (tag == NULL || name == NULL)
223 afb_req_fail(request, "failed", "bad arguments");
224 else if (0 != event_add(tag, name))
225 afb_req_fail(request, "failed", "creation error");
227 afb_req_success(request, NULL, NULL);
228 pthread_mutex_unlock(&mutex);
231 static void eventdel (afb_req request)
233 const char *tag = afb_req_value(request, "tag");
235 pthread_mutex_lock(&mutex);
237 afb_req_fail(request, "failed", "bad arguments");
238 else if (0 != event_del(tag))
239 afb_req_fail(request, "failed", "deletion error");
241 afb_req_success(request, NULL, NULL);
242 pthread_mutex_unlock(&mutex);
245 static void eventsub (afb_req request)
247 const char *tag = afb_req_value(request, "tag");
249 pthread_mutex_lock(&mutex);
251 afb_req_fail(request, "failed", "bad arguments");
252 else if (0 != event_subscribe(request, tag))
253 afb_req_fail(request, "failed", "subscription error");
255 afb_req_success(request, NULL, NULL);
256 pthread_mutex_unlock(&mutex);
259 static void eventunsub (afb_req request)
261 const char *tag = afb_req_value(request, "tag");
263 pthread_mutex_lock(&mutex);
265 afb_req_fail(request, "failed", "bad arguments");
266 else if (0 != event_unsubscribe(request, tag))
267 afb_req_fail(request, "failed", "unsubscription error");
269 afb_req_success(request, NULL, NULL);
270 pthread_mutex_unlock(&mutex);
273 static void eventpush (afb_req request)
275 const char *tag = afb_req_value(request, "tag");
276 const char *data = afb_req_value(request, "data");
277 json_object *object = data ? json_tokener_parse(data) : NULL;
279 pthread_mutex_lock(&mutex);
281 afb_req_fail(request, "failed", "bad arguments");
282 else if (0 > event_push(object, tag))
283 afb_req_fail(request, "failed", "push error");
285 afb_req_success(request, NULL, NULL);
286 pthread_mutex_unlock(&mutex);
287 json_object_put(object);
290 static void callcb (void *prequest, int status, json_object *object)
292 afb_req request = afb_req_unstore(prequest);
294 afb_req_fail(request, "failed", json_object_to_json_string(object));
296 afb_req_success(request, json_object_get(object), NULL);
297 afb_req_unref(request);
300 static void call (afb_req request)
302 const char *api = afb_req_value(request, "api");
303 const char *verb = afb_req_value(request, "verb");
304 const char *args = afb_req_value(request, "args");
305 json_object *object = api && verb && args ? json_tokener_parse(args) : NULL;
308 afb_req_fail(request, "failed", "bad arguments");
310 afb_service_call(api, verb, object, callcb, afb_req_store(request));
313 static void callsync (afb_req request)
316 const char *api = afb_req_value(request, "api");
317 const char *verb = afb_req_value(request, "verb");
318 const char *args = afb_req_value(request, "args");
319 json_object *result, *object = api && verb && args ? json_tokener_parse(args) : NULL;
322 afb_req_fail(request, "failed", "bad arguments");
324 rc = afb_service_call_sync(api, verb, object, &result);
326 afb_req_success(request, result, NULL);
328 afb_req_fail(request, "failed", json_object_to_json_string(result));
329 json_object_put(result);
334 static void verbose (afb_req request)
337 json_object *query = afb_req_json(request), *l;
339 if (json_object_is_type(query,json_type_int))
340 level = json_object_get_int(query);
341 else if (json_object_object_get_ex(query, "level", &l) && json_object_is_type(l, json_type_int))
342 level = json_object_get_int(l);
344 if (!json_object_object_get_ex(query,"message",&l))
347 AFB_REQ_VERBOSE(request, level, "verbose called for %s", json_object_get_string(l));
348 afb_req_success(request, NULL, NULL);
351 static void exitnow (afb_req request)
354 json_object *query = afb_req_json(request), *l;
356 if (json_object_is_type(query,json_type_int))
357 code = json_object_get_int(query);
358 else if (json_object_object_get_ex(query, "code", &l) && json_object_is_type(l, json_type_int))
359 code = json_object_get_int(l);
361 if (!json_object_object_get_ex(query,"reason",&l))
364 AFB_REQ_NOTICE(request, "in phase of exiting with code %d, reason: %s", code, l ? json_object_get_string(l) : "unknown");
365 afb_req_success(request, NULL, NULL);
369 static void broadcast(afb_req request)
371 const char *tag = afb_req_value(request, "tag");
372 const char *name = afb_req_value(request, "name");
373 const char *data = afb_req_value(request, "data");
374 json_object *object = data ? json_tokener_parse(data) : NULL;
377 pthread_mutex_lock(&mutex);
378 if (0 > event_broadcast(object, tag))
379 afb_req_fail(request, "failed", "broadcast error");
381 afb_req_success(request, NULL, NULL);
382 pthread_mutex_unlock(&mutex);
383 } else if (name != NULL) {
384 if (0 > afb_daemon_broadcast_event(name, object))
385 afb_req_fail(request, "failed", "broadcast error");
387 afb_req_success(request, NULL, NULL);
389 afb_req_fail(request, "failed", "bad arguments");
391 json_object_put(object);
396 AFB_NOTICE("hello binding comes to live");
402 AFB_NOTICE("hello binding starting");
406 static void onevent(const char *event, struct json_object *object)
408 AFB_NOTICE("received event %s(%s)", event, json_object_to_json_string(object));
411 // NOTE: this sample does not use session to keep test a basic as possible
412 // in real application most APIs should be protected with AFB_SESSION_CHECK
413 static const afb_verb_v2 verbs[]= {
414 { .verb="ping", .callback=pingSample },
415 { .verb="pingfail", .callback=pingFail },
416 { .verb="pingnull", .callback=pingNull },
417 { .verb="pingbug", .callback=pingBug },
418 { .verb="pingJson", .callback=pingJson },
419 { .verb="pingevent", .callback=pingEvent },
420 { .verb="subcall", .callback=subcall },
421 { .verb="subcallsync", .callback=subcallsync },
422 { .verb="eventadd", .callback=eventadd },
423 { .verb="eventdel", .callback=eventdel },
424 { .verb="eventsub", .callback=eventsub },
425 { .verb="eventunsub", .callback=eventunsub },
426 { .verb="eventpush", .callback=eventpush },
427 { .verb="call", .callback=call },
428 { .verb="callsync", .callback=callsync },
429 { .verb="verbose", .callback=verbose },
430 { .verb="broadcast", .callback=broadcast },
431 { .verb="exit", .callback=exitnow },
435 const afb_binding_v2 afbBindingV2 = {
437 .specification = NULL,