refactoring req interface
[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 // Sample Generic Ping Debug API
27 static void ping(struct afb_req request, json_object *jresp, const char *tag)
28 {
29     static int pingcount = 0;
30     json_object *query = afb_req_json(request);
31
32     afb_req_success_f(request, jresp, "Ping Binder Daemon tag=%s count=%d query=%s", tag, ++pingcount, json_object_to_json_string(query));
33 }
34
35 static void pingSample (struct afb_req request)
36 {
37         ping(request, json_object_new_string ("Some String"), "pingSample");
38 }
39
40 static void pingFail (struct afb_req request)
41 {
42         afb_req_fail(request, "failed", "Ping Binder Daemon fails");
43 }
44
45 static void pingNull (struct afb_req request)
46 {
47         ping(request, NULL, "pingNull");
48 }
49
50 static void pingBug (struct afb_req request)
51 {
52         ping((struct afb_req){NULL,NULL,NULL}, NULL, "pingBug");
53 }
54
55
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;    
59     
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));
63      
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));
67     
68     json_object_object_add(jresp,"eobj", embed);
69
70     ping(request, jresp, "pingJson");
71 }
72
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"},
81   {NULL}
82 };
83
84 static const struct AFB_plugin plugin_desc = {
85         .type = AFB_PLUGIN_JSON,
86         .info = "Minimal Hello World Sample",
87         .prefix = "hello",
88         .apis = pluginApis
89 };
90
91 const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)
92 {
93         return &plugin_desc;
94 }