Fix a compiler error
[src/app-framework-binder.git] / bindings / samples / HelloWorld.c
1 /*
2  * Copyright (C) 2015, 2016 "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 <json-c/json.h>
21
22 #include <afb/afb-binding.h>
23
24 const struct afb_binding_interface *interface;
25
26 struct event
27 {
28         struct event *next;
29         struct afb_event event;
30         char tag[1];
31 };
32
33 static struct event *events = 0;
34
35 /* searchs the event of tag */
36 static struct event *event_get(const char *tag)
37 {
38         struct event *e = events;
39         while(e && strcmp(e->tag, tag))
40                 e = e->next;
41         return e;
42 }
43
44 /* deletes the event of tag */
45 static int event_del(const char *tag)
46 {
47         struct event *e, **p;
48
49         /* check exists */
50         e = event_get(tag);
51         if (!e) return -1;
52
53         /* unlink */
54         p = &events;
55         while(*p != e) p = &(*p)->next;
56         *p = e->next;
57
58         /* destroys */
59         afb_event_drop(e->event);
60         free(e);
61         return 0;
62 }
63
64 /* creates the event of tag */
65 static int event_add(const char *tag, const char *name)
66 {
67         struct event *e;
68
69         /* check valid tag */
70         e = event_get(tag);
71         if (e) return -1;
72
73         /* creation */
74         e = malloc(strlen(tag) + sizeof *e);
75         if (!e) return -1;
76         strcpy(e->tag, tag);
77
78         /* make the event */
79         e->event = afb_daemon_make_event(interface->daemon, name);
80         if (!e->event.closure) { free(e); return -1; }
81
82         /* link */
83         e->next = events;
84         events = e;
85         return 0;
86 }
87
88 static int event_subscribe(struct afb_req request, const char *tag)
89 {
90         struct event *e;
91         e = event_get(tag);
92         return e ? afb_req_subscribe(request, e->event) : -1;
93 }
94
95 static int event_unsubscribe(struct afb_req request, const char *tag)
96 {
97         struct event *e;
98         e = event_get(tag);
99         return e ? afb_req_unsubscribe(request, e->event) : -1;
100 }
101
102 static int event_push(struct json_object *args, const char *tag)
103 {
104         struct event *e;
105         e = event_get(tag);
106         return e ? afb_event_push(e->event, json_object_get(args)) : -1;
107 }
108
109 // Sample Generic Ping Debug API
110 static void ping(struct afb_req request, json_object *jresp, const char *tag)
111 {
112         static int pingcount = 0;
113         json_object *query = afb_req_json(request);
114         afb_req_success_f(request, jresp, "Ping Binder Daemon tag=%s count=%d query=%s", tag, ++pingcount, json_object_to_json_string(query));
115 }
116
117 static void pingSample (struct afb_req request)
118 {
119         ping(request, json_object_new_string ("Some String"), "pingSample");
120 }
121
122 static void pingFail (struct afb_req request)
123 {
124         afb_req_fail(request, "failed", "Ping Binder Daemon fails");
125 }
126
127 static void pingNull (struct afb_req request)
128 {
129         ping(request, NULL, "pingNull");
130 }
131
132 static void pingBug (struct afb_req request)
133 {
134         ping((struct afb_req){NULL,NULL}, NULL, "pingBug");
135 }
136
137 static void pingEvent(struct afb_req request)
138 {
139         json_object *query = afb_req_json(request);
140         afb_daemon_broadcast_event(interface->daemon, "event", json_object_get(query));
141         ping(request, json_object_get(query), "event");
142 }
143
144
145 // For samples https://linuxprograms.wordpress.com/2010/05/20/json-c-libjson-tutorial/
146 static void pingJson (struct afb_req request) {
147     json_object *jresp, *embed;
148
149     jresp = json_object_new_object();
150     json_object_object_add(jresp, "myString", json_object_new_string ("Some String"));
151     json_object_object_add(jresp, "myInt", json_object_new_int (1234));
152
153     embed  = json_object_new_object();
154     json_object_object_add(embed, "subObjString", json_object_new_string ("Some String"));
155     json_object_object_add(embed, "subObjInt", json_object_new_int (5678));
156
157     json_object_object_add(jresp,"eobj", embed);
158
159     ping(request, jresp, "pingJson");
160 }
161
162 static void subcallcb (void *prequest, int iserror, json_object *object)
163 {
164         struct afb_req request = afb_req_unstore(prequest);
165         if (iserror)
166                 afb_req_fail(request, "failed", json_object_to_json_string(object));
167         else
168                 afb_req_success(request, object, NULL);
169         afb_req_unref(request);
170 }
171
172 static void subcall (struct afb_req request)
173 {
174         const char *api = afb_req_value(request, "api");
175         const char *verb = afb_req_value(request, "verb");
176         const char *args = afb_req_value(request, "args");
177         json_object *object = api && verb && args ? json_tokener_parse(args) : NULL;
178
179         if (object == NULL)
180                 afb_req_fail(request, "failed", "bad arguments");
181         else
182                 afb_req_subcall(request, api, verb, object, subcallcb, afb_req_store(request));
183 }
184
185 static void eventadd (struct afb_req request)
186 {
187         const char *tag = afb_req_value(request, "tag");
188         const char *name = afb_req_value(request, "name");
189
190         if (tag == NULL || name == NULL)
191                 afb_req_fail(request, "failed", "bad arguments");
192         else if (0 != event_add(tag, name))
193                 afb_req_fail(request, "failed", "creation error");
194         else
195                 afb_req_success(request, NULL, NULL);
196 }
197
198 static void eventdel (struct afb_req request)
199 {
200         const char *tag = afb_req_value(request, "tag");
201
202         if (tag == NULL)
203                 afb_req_fail(request, "failed", "bad arguments");
204         else if (0 != event_del(tag))
205                 afb_req_fail(request, "failed", "deletion error");
206         else
207                 afb_req_success(request, NULL, NULL);
208 }
209
210 static void eventsub (struct afb_req request)
211 {
212         const char *tag = afb_req_value(request, "tag");
213
214         if (tag == NULL)
215                 afb_req_fail(request, "failed", "bad arguments");
216         else if (0 != event_subscribe(request, tag))
217                 afb_req_fail(request, "failed", "subscription error");
218         else
219                 afb_req_success(request, NULL, NULL);
220 }
221
222 static void eventunsub (struct afb_req request)
223 {
224         const char *tag = afb_req_value(request, "tag");
225
226         if (tag == NULL)
227                 afb_req_fail(request, "failed", "bad arguments");
228         else if (0 != event_unsubscribe(request, tag))
229                 afb_req_fail(request, "failed", "unsubscription error");
230         else
231                 afb_req_success(request, NULL, NULL);
232 }
233
234 static void eventpush (struct afb_req request)
235 {
236         const char *tag = afb_req_value(request, "tag");
237         const char *data = afb_req_value(request, "data");
238         json_object *object = data ? json_tokener_parse(data) : NULL;
239
240         if (tag == NULL)
241                 afb_req_fail(request, "failed", "bad arguments");
242         else if (0 > event_push(object, tag))
243                 afb_req_fail(request, "failed", "push error");
244         else
245                 afb_req_success(request, NULL, NULL);
246 }
247
248 // NOTE: this sample does not use session to keep test a basic as possible
249 //       in real application most APIs should be protected with AFB_SESSION_CHECK
250 static const struct afb_verb_desc_v1 verbs[]= {
251   {"ping"     , AFB_SESSION_NONE, pingSample  , "Ping Application Framework"},
252   {"pingfail" , AFB_SESSION_NONE, pingFail    , "Fails"},
253   {"pingnull" , AFB_SESSION_NONE, pingNull    , "Return NULL"},
254   {"pingbug"  , AFB_SESSION_NONE, pingBug     , "Do a Memory Violation"},
255   {"pingJson" , AFB_SESSION_NONE, pingJson    , "Return a JSON object"},
256   {"pingevent", AFB_SESSION_NONE, pingEvent   , "Send an event"},
257   {"subcall",   AFB_SESSION_NONE, subcall     , "Call api/verb(args)"},
258   {"eventadd",  AFB_SESSION_NONE, eventadd    , "adds the event of 'name' for the 'tag'"},
259   {"eventdel",  AFB_SESSION_NONE, eventdel    , "deletes the event of 'tag'"},
260   {"eventsub",  AFB_SESSION_NONE, eventsub    , "subscribes to the event of 'tag'"},
261   {"eventunsub",AFB_SESSION_NONE, eventunsub  , "unsubscribes to the event of 'tag'"},
262   {"eventpush", AFB_SESSION_NONE, eventpush   , "pushs the event of 'tag' with the 'data'"},
263   {NULL}
264 };
265
266 static const struct afb_binding plugin_desc = {
267         .type = AFB_BINDING_VERSION_1,
268         .v1 = {
269                 .info = "Minimal Hello World Sample",
270                 .prefix = "hello",
271                 .verbs = verbs
272         }
273 };
274
275 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
276 {
277         interface = itf;
278         return &plugin_desc;
279 }