Add hooking for events
[src/app-framework-binder.git] / bindings / samples / HelloWorld.c
1 /*
2  * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <string.h>
20 #include <pthread.h>
21
22 #include <json-c/json.h>
23
24 #define AFB_BINDING_VERSION 2
25 #include <afb/afb-binding.h>
26
27 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
28
29 struct event
30 {
31         struct event *next;
32         struct afb_event event;
33         char tag[1];
34 };
35
36 static struct event *events = 0;
37
38 /* searchs the event of tag */
39 static struct event *event_get(const char *tag)
40 {
41         struct event *e = events;
42         while(e && strcmp(e->tag, tag))
43                 e = e->next;
44         return e;
45 }
46
47 /* deletes the event of tag */
48 static int event_del(const char *tag)
49 {
50         struct event *e, **p;
51
52         /* check exists */
53         e = event_get(tag);
54         if (!e) return -1;
55
56         /* unlink */
57         p = &events;
58         while(*p != e) p = &(*p)->next;
59         *p = e->next;
60
61         /* destroys */
62         afb_event_drop(e->event);
63         free(e);
64         return 0;
65 }
66
67 /* creates the event of tag */
68 static int event_add(const char *tag, const char *name)
69 {
70         struct event *e;
71
72         /* check valid tag */
73         e = event_get(tag);
74         if (e) return -1;
75
76         /* creation */
77         e = malloc(strlen(tag) + sizeof *e);
78         if (!e) return -1;
79         strcpy(e->tag, tag);
80
81         /* make the event */
82         e->event = afb_daemon_make_event(name);
83         if (!e->event.closure) { free(e); return -1; }
84
85         /* link */
86         e->next = events;
87         events = e;
88         return 0;
89 }
90
91 static int event_subscribe(afb_req request, const char *tag)
92 {
93         struct event *e;
94         e = event_get(tag);
95         return e ? afb_req_subscribe(request, e->event) : -1;
96 }
97
98 static int event_unsubscribe(afb_req request, const char *tag)
99 {
100         struct event *e;
101         e = event_get(tag);
102         return e ? afb_req_unsubscribe(request, e->event) : -1;
103 }
104
105 static int event_push(struct json_object *args, const char *tag)
106 {
107         struct event *e;
108         e = event_get(tag);
109         return e ? afb_event_push(e->event, json_object_get(args)) : -1;
110 }
111
112 static int event_broadcast(struct json_object *args, const char *tag)
113 {
114         struct event *e;
115         e = event_get(tag);
116         return e ? afb_event_broadcast(e->event, json_object_get(args)) : -1;
117 }
118
119 // Sample Generic Ping Debug API
120 static void ping(afb_req request, json_object *jresp, const char *tag)
121 {
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));
125 }
126
127 static void pingSample (afb_req request)
128 {
129         ping(request, json_object_new_string ("Some String"), "pingSample");
130 }
131
132 static void pingFail (afb_req request)
133 {
134         afb_req_fail(request, "failed", "Ping Binder Daemon fails");
135 }
136
137 static void pingNull (afb_req request)
138 {
139         ping(request, NULL, "pingNull");
140 }
141
142 static void pingBug (afb_req request)
143 {
144         ping((afb_req){NULL,NULL}, NULL, "pingBug");
145 }
146
147 static void pingEvent(afb_req request)
148 {
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");
152 }
153
154
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;
158
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));
162
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));
166
167     json_object_object_add(jresp,"eobj", embed);
168
169     ping(request, jresp, "pingJson");
170 }
171
172 static void subcallcb (void *prequest, int iserror, json_object *object)
173 {
174         afb_req request = afb_req_unstore(prequest);
175         if (iserror)
176                 afb_req_fail(request, "failed", json_object_to_json_string(object));
177         else
178                 afb_req_success(request, json_object_get(object), NULL);
179         afb_req_unref(request);
180 }
181
182 static void subcall (afb_req request)
183 {
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;
188
189         if (object == NULL)
190                 afb_req_fail(request, "failed", "bad arguments");
191         else
192                 afb_req_subcall(request, api, verb, object, subcallcb, afb_req_store(request));
193 }
194
195 static void subcallsync (afb_req request)
196 {
197         int rc;
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;
202
203         if (object == NULL)
204                 afb_req_fail(request, "failed", "bad arguments");
205         else {
206                 rc = afb_req_subcall_sync(request, api, verb, object, &result);
207                 if (rc)
208                         afb_req_success(request, result, NULL);
209                 else {
210                         afb_req_fail(request, "failed", json_object_to_json_string(result));
211                         json_object_put(result);
212                 }
213         }
214 }
215
216 static void eventadd (afb_req request)
217 {
218         const char *tag = afb_req_value(request, "tag");
219         const char *name = afb_req_value(request, "name");
220
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");
226         else
227                 afb_req_success(request, NULL, NULL);
228         pthread_mutex_unlock(&mutex);
229 }
230
231 static void eventdel (afb_req request)
232 {
233         const char *tag = afb_req_value(request, "tag");
234
235         pthread_mutex_lock(&mutex);
236         if (tag == NULL)
237                 afb_req_fail(request, "failed", "bad arguments");
238         else if (0 != event_del(tag))
239                 afb_req_fail(request, "failed", "deletion error");
240         else
241                 afb_req_success(request, NULL, NULL);
242         pthread_mutex_unlock(&mutex);
243 }
244
245 static void eventsub (afb_req request)
246 {
247         const char *tag = afb_req_value(request, "tag");
248
249         pthread_mutex_lock(&mutex);
250         if (tag == NULL)
251                 afb_req_fail(request, "failed", "bad arguments");
252         else if (0 != event_subscribe(request, tag))
253                 afb_req_fail(request, "failed", "subscription error");
254         else
255                 afb_req_success(request, NULL, NULL);
256         pthread_mutex_unlock(&mutex);
257 }
258
259 static void eventunsub (afb_req request)
260 {
261         const char *tag = afb_req_value(request, "tag");
262
263         pthread_mutex_lock(&mutex);
264         if (tag == NULL)
265                 afb_req_fail(request, "failed", "bad arguments");
266         else if (0 != event_unsubscribe(request, tag))
267                 afb_req_fail(request, "failed", "unsubscription error");
268         else
269                 afb_req_success(request, NULL, NULL);
270         pthread_mutex_unlock(&mutex);
271 }
272
273 static void eventpush (afb_req request)
274 {
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;
278
279         pthread_mutex_lock(&mutex);
280         if (tag == NULL)
281                 afb_req_fail(request, "failed", "bad arguments");
282         else if (0 > event_push(object, tag))
283                 afb_req_fail(request, "failed", "push error");
284         else
285                 afb_req_success(request, NULL, NULL);
286         pthread_mutex_unlock(&mutex);
287         json_object_put(object);
288 }
289
290 static void callcb (void *prequest, int iserror, json_object *object)
291 {
292         afb_req request = afb_req_unstore(prequest);
293         if (iserror)
294                 afb_req_fail(request, "failed", json_object_to_json_string(object));
295         else
296                 afb_req_success(request, json_object_get(object), NULL);
297         afb_req_unref(request);
298 }
299
300 static void call (afb_req request)
301 {
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;
306
307         if (object == NULL)
308                 afb_req_fail(request, "failed", "bad arguments");
309         else
310                 afb_service_call(api, verb, object, callcb, afb_req_store(request));
311 }
312
313 static void callsync (afb_req request)
314 {
315         int rc;
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;
320
321         if (object == NULL)
322                 afb_req_fail(request, "failed", "bad arguments");
323         else {
324                 rc = afb_service_call_sync(api, verb, object, &result);
325                 if (rc)
326                         afb_req_success(request, result, NULL);
327                 else {
328                         afb_req_fail(request, "failed", json_object_to_json_string(result));
329                         json_object_put(result);
330                 }
331         }
332 }
333
334 static void verbose (afb_req request)
335 {
336         int level = 5;
337         json_object *query = afb_req_json(request), *l;
338
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);
343
344         if (!json_object_object_get_ex(query,"message",&l))
345                 l = query;
346
347         AFB_REQ_VERBOSE(request, level, "verbose called for %s", json_object_get_string(l));
348         afb_req_success(request, NULL, NULL);
349 }
350
351 static void exitnow (afb_req request)
352 {
353         int code = 0;
354         json_object *query = afb_req_json(request), *l;
355
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);
360
361         if (!json_object_object_get_ex(query,"reason",&l))
362                 l = NULL;
363
364         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);
366         exit(code);
367 }
368
369 static void broadcast(afb_req request)
370 {
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;
375
376         if (tag != NULL) {
377                 pthread_mutex_lock(&mutex);
378                 if (0 > event_broadcast(object, tag))
379                         afb_req_fail(request, "failed", "broadcast error");
380                 else
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");
386                 else
387                         afb_req_success(request, NULL, NULL);
388         } else {
389                 afb_req_fail(request, "failed", "bad arguments");
390         }
391         json_object_put(object);
392 }
393
394 static int preinit()
395 {
396         NOTICE("hello binding comes to live");
397         return 0;
398 }
399
400 static int init()
401 {
402         NOTICE("hello binding starting");
403         return 0;
404 }
405
406 static void onevent(const char *event, struct json_object *object)
407 {
408         NOTICE("received event %s(%s)", event, json_object_to_json_string(object));
409 }
410
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   { "ping"     ,    pingSample , NULL, AFB_SESSION_NONE },
415   { "pingfail" ,    pingFail   , NULL, AFB_SESSION_NONE },
416   { "pingnull" ,    pingNull   , NULL, AFB_SESSION_NONE },
417   { "pingbug"  ,    pingBug    , NULL, AFB_SESSION_NONE },
418   { "pingJson" ,    pingJson   , NULL, AFB_SESSION_NONE },
419   { "pingevent",    pingEvent  , NULL, AFB_SESSION_NONE },
420   { "subcall",      subcall    , NULL, AFB_SESSION_NONE },
421   { "subcallsync",  subcallsync, NULL, AFB_SESSION_NONE },
422   { "eventadd",     eventadd   , NULL, AFB_SESSION_NONE },
423   { "eventdel",     eventdel   , NULL, AFB_SESSION_NONE },
424   { "eventsub",     eventsub   , NULL, AFB_SESSION_NONE },
425   { "eventunsub",   eventunsub , NULL, AFB_SESSION_NONE },
426   { "eventpush",    eventpush  , NULL, AFB_SESSION_NONE },
427   { "call",         call       , NULL, AFB_SESSION_NONE },
428   { "callsync",     callsync   , NULL, AFB_SESSION_NONE },
429   { "verbose",      verbose    , NULL, AFB_SESSION_NONE },
430   { "broadcast",    broadcast  , NULL, AFB_SESSION_NONE },
431   { "exit",         exitnow    , NULL, AFB_SESSION_NONE },
432   { NULL}
433 };
434
435 const afb_binding_v2 afbBindingV2 = {
436         .api = "hello",
437         .specification = NULL,
438         .verbs = verbs,
439         .preinit = preinit,
440         .init = init,
441         .onevent = onevent
442 };
443