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 typedef struct queryHandleT {
32 static int getQueryCB (queryHandleT *query, struct afb_arg arg) {
33 if (query->idx >= query->len)
35 query->idx += (unsigned)snprintf (&query->msg[query->idx], query->len-query->idx, " %s: %s\'%s\',", arg.name, arg.is_file?"FILE=":"", arg.value);
36 return 1; /* continue to iterate */
39 // Helper to retrieve argument from connection
40 static size_t getQueryAll(struct afb_req request, char *buffer, size_t len) {
42 buffer[0] = '\0'; // start with an empty string
47 afb_req_iterate(request, (void*)getQueryCB, &query);
49 return query.idx >= len ? len - 1 : query.idx;
52 static void ping (struct afb_req request, json_object *jresp)
54 static int pingcount = 0;
58 // request all query key/value
59 len = getQueryAll (request, query, sizeof(query));
60 if (len == 0) strcpy (query,"NoSearchQueryList");
62 // return response to caller
63 // response = jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon %d query={%s}", pingcount++, query);
64 afb_req_success_f(request, jresp, "Ping Binder Daemon %d query={%s}", pingcount++, query);
66 fprintf(stderr, "%d: \n", pingcount);
69 static void pingSample (struct afb_req request)
71 ping(request, json_object_new_string ("Some String"));
74 static void pingFail (struct afb_req request)
76 afb_req_fail(request, "failed", "Ping Binder Daemon fails");
79 static void pingNull (struct afb_req request)
84 static void pingBug (struct afb_req request)
86 pingNull((struct afb_req){NULL,NULL,NULL});
90 // For samples https://linuxprograms.wordpress.com/2010/05/20/json-c-libjson-tutorial/
91 static void pingJson (struct afb_req request) {
92 json_object *jresp, *embed;
94 jresp = json_object_new_object();
95 json_object_object_add(jresp, "myString", json_object_new_string ("Some String"));
96 json_object_object_add(jresp, "myInt", json_object_new_int (1234));
98 embed = json_object_new_object();
99 json_object_object_add(embed, "subObjString", json_object_new_string ("Some String"));
100 json_object_object_add(embed, "subObjInt", json_object_new_int (5678));
102 json_object_object_add(jresp,"eobj", embed);
104 ping(request, jresp);
107 // NOTE: this sample does not use session to keep test a basic as possible
108 // in real application most APIs should be protected with AFB_SESSION_CHECK
109 static const struct AFB_restapi pluginApis[]= {
110 {"ping" , AFB_SESSION_NONE, pingSample , "Ping Application Framework"},
111 {"pingfail" , AFB_SESSION_NONE, pingFail , "Fails"},
112 {"pingnull" , AFB_SESSION_NONE, pingNull , "Return NULL"},
113 {"pingbug" , AFB_SESSION_NONE, pingBug , "Do a Memory Violation"},
114 {"pingJson" , AFB_SESSION_NONE, pingJson , "Return a JSON object"},
118 static const struct AFB_plugin plugin_desc = {
119 .type = AFB_PLUGIN_JSON,
120 .info = "Minimal Hello World Sample",
125 const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)