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.
20 #include <json-c/json.h>
22 #include <afb/afb-binding.h>
24 const struct afb_binding_interface *interface;
29 struct afb_event event;
33 static struct event *events = 0;
35 /* searchs the event of tag */
36 static struct event *event_get(const char *tag)
38 struct event *e = events;
39 while(e && strcmp(e->tag, tag))
44 /* deletes the event of tag */
45 static int event_del(const char *tag)
55 while(*p != e) p = &(*p)->next;
59 afb_event_drop(e->event);
64 /* creates the event of tag */
65 static int event_add(const char *tag, const char *name)
74 e = malloc(strlen(tag) + sizeof *e);
79 e->event = afb_daemon_make_event(interface->daemon, name);
80 if (!e->event.closure) { free(e); return -1; }
88 static int event_subscribe(struct afb_req request, const char *tag)
92 return e ? afb_req_subscribe(request, e->event) : -1;
95 static int event_unsubscribe(struct afb_req request, const char *tag)
99 return e ? afb_req_unsubscribe(request, e->event) : -1;
102 static int event_push(struct json_object *args, const char *tag)
106 return e ? afb_event_push(e->event, json_object_get(args)) : -1;
109 // Sample Generic Ping Debug API
110 static void ping(struct afb_req request, json_object *jresp, const char *tag)
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));
117 static void pingSample (struct afb_req request)
119 ping(request, json_object_new_string ("Some String"), "pingSample");
122 static void pingFail (struct afb_req request)
124 afb_req_fail(request, "failed", "Ping Binder Daemon fails");
127 static void pingNull (struct afb_req request)
129 ping(request, NULL, "pingNull");
132 static void pingBug (struct afb_req request)
134 ping((struct afb_req){NULL,NULL}, NULL, "pingBug");
137 static void pingEvent(struct afb_req request)
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");
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;
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));
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));
157 json_object_object_add(jresp,"eobj", embed);
159 ping(request, jresp, "pingJson");
162 static void subcallcb (void *prequest, int iserror, json_object *object)
164 struct afb_req request = afb_req_unstore(prequest);
166 afb_req_fail(request, "failed", json_object_to_json_string(object));
168 afb_req_success(request, json_object_get(object), NULL);
169 afb_req_unref(request);
172 static void subcall (struct afb_req request)
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;
180 afb_req_fail(request, "failed", "bad arguments");
182 afb_req_subcall(request, api, verb, object, subcallcb, afb_req_store(request));
183 json_object_put(object);
187 static void subcallsync (struct afb_req request)
190 const char *api = afb_req_value(request, "api");
191 const char *verb = afb_req_value(request, "verb");
192 const char *args = afb_req_value(request, "args");
193 json_object *result, *object = api && verb && args ? json_tokener_parse(args) : NULL;
196 afb_req_fail(request, "failed", "bad arguments");
198 rc = afb_req_subcall_sync(request, api, verb, object, &result);
200 afb_req_success(request, result, NULL);
202 afb_req_fail(request, "failed", json_object_to_json_string(result));
203 json_object_put(result);
205 json_object_put(object);
209 static void eventadd (struct afb_req request)
211 const char *tag = afb_req_value(request, "tag");
212 const char *name = afb_req_value(request, "name");
214 if (tag == NULL || name == NULL)
215 afb_req_fail(request, "failed", "bad arguments");
216 else if (0 != event_add(tag, name))
217 afb_req_fail(request, "failed", "creation error");
219 afb_req_success(request, NULL, NULL);
222 static void eventdel (struct afb_req request)
224 const char *tag = afb_req_value(request, "tag");
227 afb_req_fail(request, "failed", "bad arguments");
228 else if (0 != event_del(tag))
229 afb_req_fail(request, "failed", "deletion error");
231 afb_req_success(request, NULL, NULL);
234 static void eventsub (struct afb_req request)
236 const char *tag = afb_req_value(request, "tag");
239 afb_req_fail(request, "failed", "bad arguments");
240 else if (0 != event_subscribe(request, tag))
241 afb_req_fail(request, "failed", "subscription error");
243 afb_req_success(request, NULL, NULL);
246 static void eventunsub (struct afb_req request)
248 const char *tag = afb_req_value(request, "tag");
251 afb_req_fail(request, "failed", "bad arguments");
252 else if (0 != event_unsubscribe(request, tag))
253 afb_req_fail(request, "failed", "unsubscription error");
255 afb_req_success(request, NULL, NULL);
258 static void eventpush (struct afb_req request)
260 const char *tag = afb_req_value(request, "tag");
261 const char *data = afb_req_value(request, "data");
262 json_object *object = data ? json_tokener_parse(data) : NULL;
265 afb_req_fail(request, "failed", "bad arguments");
266 else if (0 > event_push(object, tag))
267 afb_req_fail(request, "failed", "push error");
269 afb_req_success(request, NULL, NULL);
272 static void exitnow (struct afb_req request)
277 // NOTE: this sample does not use session to keep test a basic as possible
278 // in real application most APIs should be protected with AFB_SESSION_CHECK
279 static const struct afb_verb_desc_v1 verbs[]= {
280 {"ping" , AFB_SESSION_NONE, pingSample , "Ping Application Framework"},
281 {"pingfail" , AFB_SESSION_NONE, pingFail , "Fails"},
282 {"pingnull" , AFB_SESSION_NONE, pingNull , "Return NULL"},
283 {"pingbug" , AFB_SESSION_NONE, pingBug , "Do a Memory Violation"},
284 {"pingJson" , AFB_SESSION_NONE, pingJson , "Return a JSON object"},
285 {"pingevent", AFB_SESSION_NONE, pingEvent , "Send an event"},
286 {"subcall", AFB_SESSION_NONE, subcall , "Call api/verb(args)"},
287 {"subcallsync", AFB_SESSION_NONE, subcallsync , "Call api/verb(args)"},
288 {"eventadd", AFB_SESSION_NONE, eventadd , "adds the event of 'name' for the 'tag'"},
289 {"eventdel", AFB_SESSION_NONE, eventdel , "deletes the event of 'tag'"},
290 {"eventsub", AFB_SESSION_NONE, eventsub , "subscribes to the event of 'tag'"},
291 {"eventunsub",AFB_SESSION_NONE, eventunsub , "unsubscribes to the event of 'tag'"},
292 {"eventpush", AFB_SESSION_NONE, eventpush , "pushs the event of 'tag' with the 'data'"},
293 {"exit", AFB_SESSION_NONE, exitnow , "exits from afb-daemon"},
297 static const struct afb_binding plugin_desc = {
298 .type = AFB_BINDING_VERSION_1,
300 .info = "Minimal Hello World Sample",
306 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
309 NOTICE(interface, "hello plugin comes to live");