2 * Copyright (C) 2015, 2016 "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, 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));
185 static void eventadd (struct afb_req request)
187 const char *tag = afb_req_value(request, "tag");
188 const char *name = afb_req_value(request, "name");
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");
195 afb_req_success(request, NULL, NULL);
198 static void eventdel (struct afb_req request)
200 const char *tag = afb_req_value(request, "tag");
203 afb_req_fail(request, "failed", "bad arguments");
204 else if (0 != event_del(tag))
205 afb_req_fail(request, "failed", "deletion error");
207 afb_req_success(request, NULL, NULL);
210 static void eventsub (struct afb_req request)
212 const char *tag = afb_req_value(request, "tag");
215 afb_req_fail(request, "failed", "bad arguments");
216 else if (0 != event_subscribe(request, tag))
217 afb_req_fail(request, "failed", "subscription error");
219 afb_req_success(request, NULL, NULL);
222 static void eventunsub (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_unsubscribe(request, tag))
229 afb_req_fail(request, "failed", "unsubscription error");
231 afb_req_success(request, NULL, NULL);
234 static void eventpush (struct afb_req request)
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;
241 afb_req_fail(request, "failed", "bad arguments");
242 else if (0 > event_push(object, tag))
243 afb_req_fail(request, "failed", "push error");
245 afb_req_success(request, NULL, NULL);
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'"},
266 static const struct afb_binding plugin_desc = {
267 .type = AFB_BINDING_VERSION_1,
269 .info = "Minimal Hello World Sample",
275 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)