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 static int fillargs(json_object *args, struct afb_arg arg)
30 obj = json_object_new_object();
31 json_object_object_add (obj, "value", json_object_new_string(arg.value));
33 json_object_object_add (obj, "path", json_object_new_string(arg.path));
34 json_object_object_add (obj, "size", json_object_new_int64((int64_t)arg.size));
35 json_object_object_add (args, arg.name && *arg.name ? arg.name : "<empty-string>", obj);
36 return 1; /* continue to iterate */
39 // Sample Generic Ping Debug API
40 static void ping(struct afb_req request, json_object *jresp)
42 static int pingcount = 0;
45 query = json_object_new_object();
46 afb_req_iterate(request, (void*)fillargs, query);
48 afb_req_success_f(request, jresp, "Ping Binder Daemon count=%d query=%s", ++pingcount, json_object_to_json_string(query));
51 static void pingSample (struct afb_req request)
53 ping(request, json_object_new_string ("Some String"));
56 static void pingFail (struct afb_req request)
58 afb_req_fail(request, "failed", "Ping Binder Daemon fails");
61 static void pingNull (struct afb_req request)
66 static void pingBug (struct afb_req request)
68 pingNull((struct afb_req){NULL,NULL,NULL});
72 // For samples https://linuxprograms.wordpress.com/2010/05/20/json-c-libjson-tutorial/
73 static void pingJson (struct afb_req request) {
74 json_object *jresp, *embed;
76 jresp = json_object_new_object();
77 json_object_object_add(jresp, "myString", json_object_new_string ("Some String"));
78 json_object_object_add(jresp, "myInt", json_object_new_int (1234));
80 embed = json_object_new_object();
81 json_object_object_add(embed, "subObjString", json_object_new_string ("Some String"));
82 json_object_object_add(embed, "subObjInt", json_object_new_int (5678));
84 json_object_object_add(jresp,"eobj", embed);
89 // NOTE: this sample does not use session to keep test a basic as possible
90 // in real application most APIs should be protected with AFB_SESSION_CHECK
91 static const struct AFB_restapi pluginApis[]= {
92 {"ping" , AFB_SESSION_NONE, pingSample , "Ping Application Framework"},
93 {"pingfail" , AFB_SESSION_NONE, pingFail , "Fails"},
94 {"pingnull" , AFB_SESSION_NONE, pingNull , "Return NULL"},
95 {"pingbug" , AFB_SESSION_NONE, pingBug , "Do a Memory Violation"},
96 {"pingJson" , AFB_SESSION_NONE, pingJson , "Return a JSON object"},
100 static const struct AFB_plugin plugin_desc = {
101 .type = AFB_PLUGIN_JSON,
102 .info = "Minimal Hello World Sample",
107 const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)