refactoring (in progress, tbf)
[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
19
20 #include "local-def.h"
21
22 STATIC json_object* pingSample (AFB_request *request) {
23     static int pingcount = 0;
24     json_object *response;
25     char query [512];
26     int len;
27
28     // request all query key/value
29     len = getQueryAll (request, query, sizeof(query));
30     if (len == 0) strcpy (query,"NoSearchQueryList");
31     
32     // return response to caller
33     response = jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon %d query={%s}", pingcount++, query);
34     
35     if (verbose) fprintf(stderr, "%d: \n", pingcount);
36     return (response);
37 }
38
39 STATIC json_object* pingFail (AFB_request *request) {
40     return NULL;
41 }
42
43 STATIC json_object* pingBug (AFB_request *request) {
44     int a,b,c;
45     
46     fprintf (stderr, "Use --timeout=10 to trap error\n");
47     b=4;
48     c=0;
49     a=b/c;
50     
51     // should never return
52     return NULL;
53 }
54
55
56 // For samples https://linuxprograms.wordpress.com/2010/05/20/json-c-libjson-tutorial/
57 STATIC json_object* pingJson (AFB_request *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     return jresp;
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  AFB_restapi pluginApis[]= {
76   {"ping"     , AFB_SESSION_NONE, (AFB_apiCB)pingSample  , "Ping Application Framework"},
77   {"pingnull" , AFB_SESSION_NONE, (AFB_apiCB)pingFail    , "Return NULL"},
78   {"pingbug"  , AFB_SESSION_NONE, (AFB_apiCB)pingBug     , "Do a Memory Violation"},
79   {"pingJson" , AFB_SESSION_NONE, (AFB_apiCB)pingJson    , "Return a JSON object"},
80   {NULL}
81 };
82
83
84 PUBLIC AFB_plugin *pluginRegister () {
85     AFB_plugin *plugin = malloc (sizeof (AFB_plugin));
86     plugin->type  = AFB_PLUGIN_JSON;
87     plugin->info  = "Minimal Hello World Sample";
88     plugin->prefix= "hello";
89     plugin->apis  = pluginApis;
90     return (plugin);
91 };