work 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 #define _GNU_SOURCE
19
20 #include <stdio.h>
21 #include <string.h>
22
23 #include "local-def.h"
24 #include "afb-req-itf.h"
25
26 static json_object* ping (AFB_request *request, json_object *jresp) {
27     static int pingcount = 0;
28     char query [512];
29     size_t len;
30
31     // request all query key/value
32     len = getQueryAll (request, query, sizeof(query));
33     if (len == 0) strcpy (query,"NoSearchQueryList");
34     
35     // return response to caller
36 //    response = jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon %d query={%s}", pingcount++, query);
37     afb_req_success_f(*request->areq, jresp, "Ping Binder Daemon %d query={%s}", pingcount++, query);
38     
39     if (verbose) fprintf(stderr, "%d: \n", pingcount);
40     return jresp;
41 }
42
43 static json_object* pingSample (AFB_request *request) {
44         return ping(request, json_object_new_string ("Some String"));
45 }
46
47 static json_object* pingFail (AFB_request *request) {
48         return ping(request, NULL);
49 }
50
51 static json_object* pingBug (AFB_request *request) {
52     int a,b,c;
53     
54     fprintf (stderr, "Use --timeout=10 to trap error\n");
55     b=4;
56     c=0;
57     a=b/c;
58     
59     // should never return
60     return NULL;
61 }
62
63
64 // For samples https://linuxprograms.wordpress.com/2010/05/20/json-c-libjson-tutorial/
65 static json_object* pingJson (AFB_request *request) {
66     json_object *jresp, *embed;    
67     
68     jresp = json_object_new_object();
69     json_object_object_add(jresp, "myString", json_object_new_string ("Some String"));
70     json_object_object_add(jresp, "myInt", json_object_new_int (1234));
71      
72     embed  = json_object_new_object();
73     json_object_object_add(embed, "subObjString", json_object_new_string ("Some String"));
74     json_object_object_add(embed, "subObjInt", json_object_new_int (5678));
75     
76     json_object_object_add(jresp,"eobj", embed);
77
78     return ping(request, jresp);
79 }
80
81 // NOTE: this sample does not use session to keep test a basic as possible
82 //       in real application most APIs should be protected with AFB_SESSION_CHECK
83 static  AFB_restapi pluginApis[]= {
84   {"ping"     , AFB_SESSION_NONE, (AFB_apiCB)pingSample  , "Ping Application Framework"},
85   {"pingnull" , AFB_SESSION_NONE, (AFB_apiCB)pingFail    , "Return NULL"},
86   {"pingbug"  , AFB_SESSION_NONE, (AFB_apiCB)pingBug     , "Do a Memory Violation"},
87   {"pingJson" , AFB_SESSION_NONE, (AFB_apiCB)pingJson    , "Return a JSON object"},
88   {NULL}
89 };
90
91 static const AFB_plugin plugin = {
92         .type = AFB_PLUGIN_JSON,
93         .info = "Minimal Hello World Sample",
94         .prefix = "hello",
95         .apis = pluginApis
96 };
97
98 const AFB_plugin *pluginRegister () {
99     return &plugin;
100 };