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 const struct afb_binding_interface *interface;
28 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
33 struct afb_event event;
37 static struct event *events = 0;
39 /* searchs the event of tag */
40 static struct event *event_get(const char *tag)
42 struct event *e = events;
43 while(e && strcmp(e->tag, tag))
48 /* deletes the event of tag */
49 static int event_del(const char *tag)
59 while(*p != e) p = &(*p)->next;
63 afb_event_drop(e->event);
68 /* creates the event of tag */
69 static int event_add(const char *tag, const char *name)
78 e = malloc(strlen(tag) + sizeof *e);
83 e->event = afb_daemon_make_event(name);
84 if (!e->event.closure) { free(e); return -1; }
92 static int event_subscribe(struct afb_req request, const char *tag)
96 return e ? afb_req_subscribe(request, e->event) : -1;
99 static int event_unsubscribe(struct afb_req request, const char *tag)
103 return e ? afb_req_unsubscribe(request, e->event) : -1;
106 static int event_push(struct json_object *args, const char *tag)
110 return e ? afb_event_push(e->event, json_object_get(args)) : -1;
113 // Sample Generic Ping Debug API
114 static void ping(struct afb_req request, json_object *jresp, const char *tag)
116 static int pingcount = 0;
117 json_object *query = afb_req_json(request);
118 afb_req_success_f(request, jresp, "Ping Binder Daemon tag=%s count=%d query=%s", tag, ++pingcount, json_object_to_json_string(query));
121 static void pingSample (struct afb_req request)
123 ping(request, json_object_new_string ("Some String"), "pingSample");
126 static void pingFail (struct afb_req request)
128 afb_req_fail(request, "failed", "Ping Binder Daemon fails");
131 static void pingNull (struct afb_req request)
133 ping(request, NULL, "pingNull");
136 static void pingBug (struct afb_req request)
138 ping((struct afb_req){NULL,NULL}, NULL, "pingBug");
141 static void pingEvent(struct afb_req request)
143 json_object *query = afb_req_json(request);
144 afb_daemon_broadcast_event("event", json_object_get(query));
145 ping(request, json_object_get(query), "event");
149 // For samples https://linuxprograms.wordpress.com/2010/05/20/json-c-libjson-tutorial/
150 static void pingJson (struct afb_req request) {
151 json_object *jresp, *embed;
153 jresp = json_object_new_object();
154 json_object_object_add(jresp, "myString", json_object_new_string ("Some String"));
155 json_object_object_add(jresp, "myInt", json_object_new_int (1234));
157 embed = json_object_new_object();
158 json_object_object_add(embed, "subObjString", json_object_new_string ("Some String"));
159 json_object_object_add(embed, "subObjInt", json_object_new_int (5678));
161 json_object_object_add(jresp,"eobj", embed);
163 ping(request, jresp, "pingJson");
166 static void subcallcb (void *prequest, int iserror, json_object *object)
168 struct afb_req request = afb_req_unstore(prequest);
170 afb_req_fail(request, "failed", json_object_to_json_string(object));
172 afb_req_success(request, json_object_get(object), NULL);
173 afb_req_unref(request);
176 static void subcall (struct afb_req request)
178 const char *api = afb_req_value(request, "api");
179 const char *verb = afb_req_value(request, "verb");
180 const char *args = afb_req_value(request, "args");
181 json_object *object = api && verb && args ? json_tokener_parse(args) : NULL;
184 afb_req_fail(request, "failed", "bad arguments");
186 afb_req_subcall(request, api, verb, object, subcallcb, afb_req_store(request));
189 static void subcallsync (struct afb_req request)
192 const char *api = afb_req_value(request, "api");
193 const char *verb = afb_req_value(request, "verb");
194 const char *args = afb_req_value(request, "args");
195 json_object *result, *object = api && verb && args ? json_tokener_parse(args) : NULL;
198 afb_req_fail(request, "failed", "bad arguments");
200 rc = afb_req_subcall_sync(request, api, verb, object, &result);
202 afb_req_success(request, result, NULL);
204 afb_req_fail(request, "failed", json_object_to_json_string(result));
205 json_object_put(result);
210 static void eventadd (struct afb_req request)
212 const char *tag = afb_req_value(request, "tag");
213 const char *name = afb_req_value(request, "name");
215 pthread_mutex_lock(&mutex);
216 if (tag == NULL || name == NULL)
217 afb_req_fail(request, "failed", "bad arguments");
218 else if (0 != event_add(tag, name))
219 afb_req_fail(request, "failed", "creation error");
221 afb_req_success(request, NULL, NULL);
222 pthread_mutex_unlock(&mutex);
225 static void eventdel (struct afb_req request)
227 const char *tag = afb_req_value(request, "tag");
229 pthread_mutex_lock(&mutex);
231 afb_req_fail(request, "failed", "bad arguments");
232 else if (0 != event_del(tag))
233 afb_req_fail(request, "failed", "deletion error");
235 afb_req_success(request, NULL, NULL);
236 pthread_mutex_unlock(&mutex);
239 static void eventsub (struct afb_req request)
241 const char *tag = afb_req_value(request, "tag");
243 pthread_mutex_lock(&mutex);
245 afb_req_fail(request, "failed", "bad arguments");
246 else if (0 != event_subscribe(request, tag))
247 afb_req_fail(request, "failed", "subscription error");
249 afb_req_success(request, NULL, NULL);
250 pthread_mutex_unlock(&mutex);
253 static void eventunsub (struct afb_req request)
255 const char *tag = afb_req_value(request, "tag");
257 pthread_mutex_lock(&mutex);
259 afb_req_fail(request, "failed", "bad arguments");
260 else if (0 != event_unsubscribe(request, tag))
261 afb_req_fail(request, "failed", "unsubscription error");
263 afb_req_success(request, NULL, NULL);
264 pthread_mutex_unlock(&mutex);
267 static void eventpush (struct afb_req request)
269 const char *tag = afb_req_value(request, "tag");
270 const char *data = afb_req_value(request, "data");
271 json_object *object = data ? json_tokener_parse(data) : NULL;
273 pthread_mutex_lock(&mutex);
275 afb_req_fail(request, "failed", "bad arguments");
276 else if (0 > event_push(object, tag))
277 afb_req_fail(request, "failed", "push error");
279 afb_req_success(request, NULL, NULL);
280 pthread_mutex_unlock(&mutex);
281 json_object_put(object);
284 static void callcb (void *prequest, int iserror, json_object *object)
286 struct afb_req request = afb_req_unstore(prequest);
288 afb_req_fail(request, "failed", json_object_to_json_string(object));
290 afb_req_success(request, json_object_get(object), NULL);
291 afb_req_unref(request);
294 static void call (struct afb_req request)
296 const char *api = afb_req_value(request, "api");
297 const char *verb = afb_req_value(request, "verb");
298 const char *args = afb_req_value(request, "args");
299 json_object *object = api && verb && args ? json_tokener_parse(args) : NULL;
302 afb_req_fail(request, "failed", "bad arguments");
304 afb_service_call(api, verb, object, callcb, afb_req_store(request));
307 static void callsync (struct afb_req request)
310 const char *api = afb_req_value(request, "api");
311 const char *verb = afb_req_value(request, "verb");
312 const char *args = afb_req_value(request, "args");
313 json_object *result, *object = api && verb && args ? json_tokener_parse(args) : NULL;
316 afb_req_fail(request, "failed", "bad arguments");
318 rc = afb_service_call_sync(api, verb, object, &result);
320 afb_req_success(request, result, NULL);
322 afb_req_fail(request, "failed", json_object_to_json_string(result));
323 json_object_put(result);
328 static void exitnow (struct afb_req request)
335 NOTICE("hello binding comes to live");
341 NOTICE("hello binding starting");
345 // NOTE: this sample does not use session to keep test a basic as possible
346 // in real application most APIs should be protected with AFB_SESSION_CHECK
347 static const struct afb_verb_v2 verbs[]= {
348 { "ping" , pingSample , NULL, AFB_SESSION_NONE },
349 { "pingfail" , pingFail , NULL, AFB_SESSION_NONE },
350 { "pingnull" , pingNull , NULL, AFB_SESSION_NONE },
351 { "pingbug" , pingBug , NULL, AFB_SESSION_NONE },
352 { "pingJson" , pingJson , NULL, AFB_SESSION_NONE },
353 { "pingevent", pingEvent , NULL, AFB_SESSION_NONE },
354 { "subcall", subcall , NULL, AFB_SESSION_NONE },
355 { "subcallsync", subcallsync, NULL, AFB_SESSION_NONE },
356 { "eventadd", eventadd , NULL, AFB_SESSION_NONE },
357 { "eventdel", eventdel , NULL, AFB_SESSION_NONE },
358 { "eventsub", eventsub , NULL, AFB_SESSION_NONE },
359 { "eventunsub", eventunsub , NULL, AFB_SESSION_NONE },
360 { "eventpush", eventpush , NULL, AFB_SESSION_NONE },
361 { "call", call , NULL, AFB_SESSION_NONE },
362 { "callsync", callsync , NULL, AFB_SESSION_NONE },
363 { "exit", exitnow , NULL, AFB_SESSION_NONE },
367 const struct afb_binding_v2 afbBindingV2 = {
369 .specification = NULL,