7d99f6df7ec9bd98be2859be09f0ed94f0d4745f
[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     if (arg.path != NULL)
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 */
37 }
38
39 // Sample Generic Ping Debug API
40 static void ping(struct afb_req request, json_object *jresp, const char *tag)
41 {
42     static int pingcount = 0;
43     json_object *query;
44
45     query = json_object_new_object();
46     afb_req_iterate(request, (void*)fillargs, query);
47
48     afb_req_success_f(request, jresp, "Ping Binder Daemon tag=%s count=%d query=%s", tag, ++pingcount, json_object_to_json_string(query));
49 }
50
51 static void pingSample (struct afb_req request)
52 {
53         ping(request, json_object_new_string ("Some String"), "pingSample");
54 }
55
56 static void pingFail (struct afb_req request)
57 {
58         afb_req_fail(request, "failed", "Ping Binder Daemon fails");
59 }
60
61 static void pingNull (struct afb_req request)
62 {
63         ping(request, NULL, "pingNull");
64 }
65
66 static void pingBug (struct afb_req request)
67 {
68         ping((struct afb_req){NULL,NULL,NULL}, NULL, "pingBug");
69 }
70
71
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;    
75     
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));
79      
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));
83     
84     json_object_object_add(jresp,"eobj", embed);
85
86     ping(request, jresp, "pingJson");
87 }
88
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"},
97   {NULL}
98 };
99
100 static const struct AFB_plugin plugin_desc = {
101         .type = AFB_PLUGIN_JSON,
102         .info = "Minimal Hello World Sample",
103         .prefix = "hello",
104         .apis = pluginApis
105 };
106
107 const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)
108 {
109         return &plugin_desc;
110 }