Improve documentation of api v3
[src/app-framework-binder.git] / bindings / samples / hello3.c
1 /*
2  * Copyright (C) 2015-2018 "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 3
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         afb_event_t 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_unref(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) { 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_t 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_t 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_t 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_t request)
128 {
129         ping(request, json_object_new_string ("Some String"), "pingSample");
130 }
131
132 static void pingFail (afb_req_t request)
133 {
134         afb_req_fail(request, "failed", "Ping Binder Daemon fails");
135 }
136
137 static void pingNull (afb_req_t request)
138 {
139         ping(request, NULL, "pingNull");
140 }
141
142 static void pingBug (afb_req_t request)
143 {
144         ping(NULL, NULL, "pingBug");
145 }
146
147 static void pingEvent(afb_req_t 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_t 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 status, json_object *object, afb_req_t request)
173 {
174         if (status < 0)
175                 afb_req_fail(request, "failed", json_object_to_json_string(object));
176         else
177                 afb_req_success(request, json_object_get(object), NULL);
178         afb_req_unref(request);
179 }
180
181 static void subcall (afb_req_t request)
182 {
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;
187
188         if (object == NULL)
189                 afb_req_fail(request, "failed", "bad arguments");
190         else
191                 afb_req_subcall_legacy(request, api, verb, object, subcallcb, NULL);
192 }
193
194 static void subcallreqcb (void *prequest, int status, json_object *object, afb_req_t request)
195 {
196         if (status < 0)
197                 afb_req_fail(request, "failed", json_object_to_json_string(object));
198         else
199                 afb_req_success(request, json_object_get(object), NULL);
200 }
201
202 static void subcallreq (afb_req_t request)
203 {
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;
208
209         if (object == NULL)
210                 afb_req_fail(request, "failed", "bad arguments");
211         else
212                 afb_req_subcall_legacy(request, api, verb, object, subcallreqcb, NULL);
213 }
214
215 static void subcallsync (afb_req_t request)
216 {
217         int rc;
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;
222
223         if (object == NULL)
224                 afb_req_fail(request, "failed", "bad arguments");
225         else {
226                 rc = afb_req_subcall_sync_legacy(request, api, verb, object, &result);
227                 if (rc >= 0)
228                         afb_req_success(request, result, NULL);
229                 else {
230                         afb_req_fail(request, "failed", json_object_to_json_string(result));
231                         json_object_put(result);
232                 }
233         }
234 }
235
236 static void eventadd (afb_req_t request)
237 {
238         const char *tag = afb_req_value(request, "tag");
239         const char *name = afb_req_value(request, "name");
240
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");
246         else
247                 afb_req_success(request, NULL, NULL);
248         pthread_mutex_unlock(&mutex);
249 }
250
251 static void eventdel (afb_req_t request)
252 {
253         const char *tag = afb_req_value(request, "tag");
254
255         pthread_mutex_lock(&mutex);
256         if (tag == NULL)
257                 afb_req_fail(request, "failed", "bad arguments");
258         else if (0 != event_del(tag))
259                 afb_req_fail(request, "failed", "deletion error");
260         else
261                 afb_req_success(request, NULL, NULL);
262         pthread_mutex_unlock(&mutex);
263 }
264
265 static void eventsub (afb_req_t request)
266 {
267         const char *tag = afb_req_value(request, "tag");
268
269         pthread_mutex_lock(&mutex);
270         if (tag == NULL)
271                 afb_req_fail(request, "failed", "bad arguments");
272         else if (0 != event_subscribe(request, tag))
273                 afb_req_fail(request, "failed", "subscription error");
274         else
275                 afb_req_success(request, NULL, NULL);
276         pthread_mutex_unlock(&mutex);
277 }
278
279 static void eventunsub (afb_req_t request)
280 {
281         const char *tag = afb_req_value(request, "tag");
282
283         pthread_mutex_lock(&mutex);
284         if (tag == NULL)
285                 afb_req_fail(request, "failed", "bad arguments");
286         else if (0 != event_unsubscribe(request, tag))
287                 afb_req_fail(request, "failed", "unsubscription error");
288         else
289                 afb_req_success(request, NULL, NULL);
290         pthread_mutex_unlock(&mutex);
291 }
292
293 static void eventpush (afb_req_t request)
294 {
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;
298
299         pthread_mutex_lock(&mutex);
300         if (tag == NULL)
301                 afb_req_fail(request, "failed", "bad arguments");
302         else if (0 > event_push(object, tag))
303                 afb_req_fail(request, "failed", "push error");
304         else
305                 afb_req_success(request, NULL, NULL);
306         pthread_mutex_unlock(&mutex);
307         json_object_put(object);
308 }
309
310 static void callcb (void *prequest, json_object *object, const char *error, const char *info, afb_api_t api)
311 {
312         afb_req_t request = prequest;
313         afb_req_reply(request, json_object_get(object), error, info);
314         afb_req_unref(request);
315 }
316
317 static void call (afb_req_t request)
318 {
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;
323
324         afb_service_call(api, verb, object, callcb, afb_req_addref(request));
325 }
326
327 static void callsync (afb_req_t request)
328 {
329         const char *api = afb_req_value(request, "api");
330         const char *verb = afb_req_value(request, "verb");
331         const char *args = afb_req_value(request, "args");
332         json_object *object = api && verb && args ? json_tokener_parse(args) : NULL;
333         json_object *result;
334         char *error, *info;
335
336         afb_service_call_sync(api, verb, object, &result, &error, &info);
337         afb_req_reply(request, result, error, info);
338         free(error);
339         free(info);
340 }
341
342 static void verbose (afb_req_t request)
343 {
344         int level = 5;
345         json_object *query = afb_req_json(request), *l;
346
347         if (json_object_is_type(query,json_type_int))
348                 level = json_object_get_int(query);
349         else if (json_object_object_get_ex(query, "level", &l) && json_object_is_type(l, json_type_int))
350                 level = json_object_get_int(l);
351
352         if (!json_object_object_get_ex(query,"message",&l))
353                 l = query;
354
355         AFB_REQ_VERBOSE(request, level, "verbose called for %s", json_object_get_string(l));
356         afb_req_success(request, NULL, NULL);
357 }
358
359 static void exitnow (afb_req_t request)
360 {
361         int code = 0;
362         json_object *query = afb_req_json(request), *l;
363
364         if (json_object_is_type(query,json_type_int))
365                 code = json_object_get_int(query);
366         else if (json_object_object_get_ex(query, "code", &l) && json_object_is_type(l, json_type_int))
367                 code = json_object_get_int(l);
368
369         if (!json_object_object_get_ex(query,"reason",&l))
370                 l = NULL;
371
372         AFB_REQ_NOTICE(request, "in phase of exiting with code %d, reason: %s", code, l ? json_object_get_string(l) : "unknown");
373         afb_req_success(request, NULL, NULL);
374         exit(code);
375 }
376
377 static void broadcast(afb_req_t request)
378 {
379         const char *tag = afb_req_value(request, "tag");
380         const char *name = afb_req_value(request, "name");
381         const char *data = afb_req_value(request, "data");
382         json_object *object = data ? json_tokener_parse(data) : NULL;
383
384         if (tag != NULL) {
385                 pthread_mutex_lock(&mutex);
386                 if (0 > event_broadcast(object, tag))
387                         afb_req_fail(request, "failed", "broadcast error");
388                 else
389                         afb_req_success(request, NULL, NULL);
390                 pthread_mutex_unlock(&mutex);
391         } else if (name != NULL) {
392                 if (0 > afb_daemon_broadcast_event(name, object))
393                         afb_req_fail(request, "failed", "broadcast error");
394                 else
395                         afb_req_success(request, NULL, NULL);
396         } else {
397                 afb_req_fail(request, "failed", "bad arguments");
398         }
399         json_object_put(object);
400 }
401
402 static void hasperm (afb_req_t request)
403 {
404         const char *perm = afb_req_value(request, "perm");
405         if (afb_req_has_permission(request, perm))
406                 afb_req_success_f(request, NULL, "permission %s granted", perm?:"(null)");
407         else
408                 afb_req_fail_f(request, "not-granted", "permission %s NOT granted", perm?:"(null)");
409 }
410
411 static void appid (afb_req_t request)
412 {
413         char *aid = afb_req_get_application_id(request);
414         afb_req_success_f(request, aid ? json_object_new_string(aid) : NULL, "application is %s", aid?:"?");
415         free(aid);
416 }
417
418 static void uid (afb_req_t request)
419 {
420         int uid = afb_req_get_uid(request);
421         afb_req_success_f(request, json_object_new_int(uid), "uid is %d", uid);
422 }
423
424 static int preinit(afb_api_t api)
425 {
426         AFB_NOTICE("hello binding comes to live");
427         return 0;
428 }
429
430 static int init(afb_api_t api)
431 {
432         AFB_NOTICE("hello binding starting");
433         return 0;
434 }
435
436 static void onevent(afb_api_t api, const char *event, struct json_object *object)
437 {
438         AFB_NOTICE("received event %s(%s)", event, json_object_to_json_string(object));
439 }
440
441 // NOTE: this sample does not use session to keep test a basic as possible
442 //       in real application most APIs should be protected with AFB_SESSION_CHECK
443 static const struct afb_verb_v3 verbs[]= {
444   { .verb="ping",        .callback=pingSample },
445   { .verb="pingfail",    .callback=pingFail },
446   { .verb="pingnull",    .callback=pingNull },
447   { .verb="pingbug",     .callback=pingBug },
448   { .verb="pingJson",    .callback=pingJson },
449   { .verb="pingevent",   .callback=pingEvent },
450   { .verb="subcall",     .callback=subcall },
451   { .verb="subcallreq",  .callback=subcallreq },
452   { .verb="subcallsync", .callback=subcallsync },
453   { .verb="eventadd",    .callback=eventadd },
454   { .verb="eventdel",    .callback=eventdel },
455   { .verb="eventsub",    .callback=eventsub },
456   { .verb="eventunsub",  .callback=eventunsub },
457   { .verb="eventpush",   .callback=eventpush },
458   { .verb="call",        .callback=call },
459   { .verb="callsync",    .callback=callsync },
460   { .verb="verbose",     .callback=verbose },
461   { .verb="broadcast",   .callback=broadcast },
462   { .verb="hasperm",     .callback=hasperm },
463   { .verb="appid",       .callback=appid },
464   { .verb="uid",         .callback=uid },
465   { .verb="exit",        .callback=exitnow },
466   { .verb=NULL}
467 };
468
469 #if !defined(APINAME)
470 #define APINAME "hello3"
471 #endif
472
473 const struct afb_binding_v3 afbBindingV3 = {
474         .api = APINAME,
475         .specification = NULL,
476         .verbs = verbs,
477         .preinit = preinit,
478         .init = init,
479         .onevent = onevent
480 };
481