fd780e20981fafb38e2d1f60c44e141e7ff13a25
[src/app-framework-binder.git] / plugins / samples / HelloWorld.c
1 /*
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  *
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.
9  *
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.
14  *
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/>.
17  */
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <string.h>
21 #include <json.h>
22
23 #include "afb-plugin.h"
24 #include "afb-req-itf.h"
25
26 static int fillargs(json_object *args, struct afb_arg arg)
27 {
28     json_object *obj;
29
30     obj = json_object_new_object();
31     json_object_object_add (obj, "value", json_object_new_string(arg.value));
32     json_object_object_add (obj, "path", json_object_new_string(arg.path));
33     json_object_object_add (obj, "size", json_object_new_int64((int64_t)arg.size));
34     json_object_object_add (args, arg.name && *arg.name ? arg.name : "<empty-string>", obj);
35     return 1; /* continue to iterate */
36 }
37
38 // Sample Generic Ping Debug API
39 static void ping(struct afb_req request, json_object *jresp)
40 {
41     static int pingcount = 0;
42     json_object *query;
43
44     query = json_object_new_object();
45     afb_req_iterate(request, (void*)fillargs, query);
46
47     afb_req_success_f(request, jresp, "Ping Binder Daemon count=%d query=%s", ++pingcount, json_object_to_json_string(query));
48 }
49
50 static void pingSample (struct afb_req request)
51 {
52         ping(request, json_object_new_string ("Some String"));
53 }
54
55 static void pingFail (struct afb_req request)
56 {
57         afb_req_fail(request, "failed", "Ping Binder Daemon fails");
58 }
59
60 static void pingNull (struct afb_req request)
61 {
62         ping(request, NULL);
63 }
64
65 static void pingBug (struct afb_req request)
66 {
67         pingNull((struct afb_req){NULL,NULL,NULL});
68 }
69
70
71 // For samples https://linuxprograms.wordpress.com/2010/05/20/json-c-libjson-tutorial/
72 static void pingJson (struct afb_req request) {
73     json_object *jresp, *embed;    
74     
75     jresp = json_object_new_object();
76     json_object_object_add(jresp, "myString", json_object_new_string ("Some String"));
77     json_object_object_add(jresp, "myInt", json_object_new_int (1234));
78      
79     embed  = json_object_new_object();
80     json_object_object_add(embed, "subObjString", json_object_new_string ("Some String"));
81     json_object_object_add(embed, "subObjInt", json_object_new_int (5678));
82     
83     json_object_object_add(jresp,"eobj", embed);
84
85     ping(request, jresp);
86 }
87
88 // NOTE: this sample does not use session to keep test a basic as possible
89 //       in real application most APIs should be protected with AFB_SESSION_CHECK
90 static const struct AFB_restapi pluginApis[]= {
91   {"ping"     , AFB_SESSION_NONE, pingSample  , "Ping Application Framework"},
92   {"pingfail" , AFB_SESSION_NONE, pingFail    , "Fails"},
93   {"pingnull" , AFB_SESSION_NONE, pingNull    , "Return NULL"},
94   {"pingbug"  , AFB_SESSION_NONE, pingBug     , "Do a Memory Violation"},
95   {"pingJson" , AFB_SESSION_NONE, pingJson    , "Return a JSON object"},
96   {NULL}
97 };
98
99 static const struct AFB_plugin plugin_desc = {
100         .type = AFB_PLUGIN_JSON,
101         .info = "Minimal Hello World Sample",
102         .prefix = "hello",
103         .apis = pluginApis
104 };
105
106 const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)
107 {
108         return &plugin_desc;
109 }