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 #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 typedef struct queryHandleT {
27      char    *msg;
28      size_t  idx;
29      size_t  len;
30 } queryHandleT;
31
32 static int getQueryCB (queryHandleT *query, struct afb_arg arg) {
33     if (query->idx >= query->len)
34         return 0;
35     query->idx += (unsigned)snprintf (&query->msg[query->idx], query->len-query->idx, " %s: %s\'%s\',", arg.name, arg.is_file?"FILE=":"", arg.value);
36     return 1; /* continue to iterate */
37 }
38
39 // Helper to retrieve argument from  connection
40 static size_t getQueryAll(struct afb_req request, char *buffer, size_t len) {
41     queryHandleT query;
42     buffer[0] = '\0'; // start with an empty string
43     query.msg = buffer;
44     query.len = len;
45     query.idx = 0;
46
47     afb_req_iterate(request, (void*)getQueryCB, &query);
48     buffer[len-1] = 0;
49     return query.idx >= len ? len - 1 : query.idx;
50 }
51
52 static void ping (struct afb_req request, json_object *jresp)
53 {
54     static int pingcount = 0;
55     char query [512];
56     size_t len;
57
58     // request all query key/value
59     len = getQueryAll (request, query, sizeof(query));
60     if (len == 0) strcpy (query,"NoSearchQueryList");
61     
62     // return response to caller
63 //    response = jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon %d query={%s}", pingcount++, query);
64     afb_req_success_f(request, jresp, "Ping Binder Daemon %d query={%s}", pingcount++, query);
65     
66     fprintf(stderr, "%d: \n", pingcount);
67 }
68
69 static void pingSample (struct afb_req request)
70 {
71         ping(request, json_object_new_string ("Some String"));
72 }
73
74 static void pingFail (struct afb_req request)
75 {
76         afb_req_fail(request, "failed", "Ping Binder Daemon fails");
77 }
78
79 static void pingNull (struct afb_req request)
80 {
81         ping(request, NULL);
82 }
83
84 static void pingBug (struct afb_req request)
85 {
86     int a,b,c;
87     
88     fprintf (stderr, "Use --timeout=10 to trap error\n");
89     b=4;
90     c=0;
91     a=b/c;
92     
93 }
94
95
96 // For samples https://linuxprograms.wordpress.com/2010/05/20/json-c-libjson-tutorial/
97 static void pingJson (struct afb_req request) {
98     json_object *jresp, *embed;    
99     
100     jresp = json_object_new_object();
101     json_object_object_add(jresp, "myString", json_object_new_string ("Some String"));
102     json_object_object_add(jresp, "myInt", json_object_new_int (1234));
103      
104     embed  = json_object_new_object();
105     json_object_object_add(embed, "subObjString", json_object_new_string ("Some String"));
106     json_object_object_add(embed, "subObjInt", json_object_new_int (5678));
107     
108     json_object_object_add(jresp,"eobj", embed);
109
110     ping(request, jresp);
111 }
112
113 // NOTE: this sample does not use session to keep test a basic as possible
114 //       in real application most APIs should be protected with AFB_SESSION_CHECK
115 static const struct AFB_restapi pluginApis[]= {
116   {"ping"     , AFB_SESSION_NONE, pingSample  , "Ping Application Framework"},
117   {"pingfail" , AFB_SESSION_NONE, pingFail    , "Fails"},
118   {"pingnull" , AFB_SESSION_NONE, pingNull    , "Return NULL"},
119   {"pingbug"  , AFB_SESSION_NONE, pingBug     , "Do a Memory Violation"},
120   {"pingJson" , AFB_SESSION_NONE, pingJson    , "Return a JSON object"},
121   {NULL}
122 };
123
124 static const struct AFB_plugin plugin_desc = {
125         .type = AFB_PLUGIN_JSON,
126         .info = "Minimal Hello World Sample",
127         .prefix = "hello",
128         .apis = pluginApis
129 };
130
131 const struct AFB_plugin *pluginRegister ()
132 {
133         return &plugin_desc;
134 }