2 * Copyright (C) 2015 "IoT.bzh"
3 * Author "Fulup Ar Foll"
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "afb-plugin.h"
24 #include "afb-req-itf.h"
26 // Sample Generic Ping Debug API
27 static void ping(struct afb_req request, json_object *jresp, const char *tag)
29 static int pingcount = 0;
30 json_object *query = afb_req_json(request);
32 afb_req_success_f(request, jresp, "Ping Binder Daemon tag=%s count=%d query=%s", tag, ++pingcount, json_object_to_json_string(query));
35 static void pingSample (struct afb_req request)
37 ping(request, json_object_new_string ("Some String"), "pingSample");
40 static void pingFail (struct afb_req request)
42 afb_req_fail(request, "failed", "Ping Binder Daemon fails");
45 static void pingNull (struct afb_req request)
47 ping(request, NULL, "pingNull");
50 static void pingBug (struct afb_req request)
52 ping((struct afb_req){NULL,NULL,NULL}, NULL, "pingBug");
56 // For samples https://linuxprograms.wordpress.com/2010/05/20/json-c-libjson-tutorial/
57 static void pingJson (struct afb_req request) {
58 json_object *jresp, *embed;
60 jresp = json_object_new_object();
61 json_object_object_add(jresp, "myString", json_object_new_string ("Some String"));
62 json_object_object_add(jresp, "myInt", json_object_new_int (1234));
64 embed = json_object_new_object();
65 json_object_object_add(embed, "subObjString", json_object_new_string ("Some String"));
66 json_object_object_add(embed, "subObjInt", json_object_new_int (5678));
68 json_object_object_add(jresp,"eobj", embed);
70 ping(request, jresp, "pingJson");
73 // NOTE: this sample does not use session to keep test a basic as possible
74 // in real application most APIs should be protected with AFB_SESSION_CHECK
75 static const struct AFB_restapi pluginApis[]= {
76 {"ping" , AFB_SESSION_NONE, pingSample , "Ping Application Framework"},
77 {"pingfail" , AFB_SESSION_NONE, pingFail , "Fails"},
78 {"pingnull" , AFB_SESSION_NONE, pingNull , "Return NULL"},
79 {"pingbug" , AFB_SESSION_NONE, pingBug , "Do a Memory Violation"},
80 {"pingJson" , AFB_SESSION_NONE, pingJson , "Return a JSON object"},
84 static const struct AFB_plugin plugin_desc = {
85 .type = AFB_PLUGIN_JSON,
86 .info = "Minimal Hello World Sample",
91 const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)