d02fd117ad922b34abcfd927851fbe8463d92aac
[src/app-framework-binder.git] / plugins / samples / HelloWorld.c
1 /*
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <string.h>
20 #include <json.h>
21
22 #include "afb-plugin.h"
23 #include "afb-req-itf.h"
24
25 // Sample Generic Ping Debug API
26 static void ping(struct afb_req request, json_object *jresp, const char *tag)
27 {
28     static int pingcount = 0;
29     json_object *query = afb_req_json(request);
30
31     afb_req_success_f(request, jresp, "Ping Binder Daemon tag=%s count=%d query=%s", tag, ++pingcount, json_object_to_json_string(query));
32 }
33
34 static void pingSample (struct afb_req request)
35 {
36         ping(request, json_object_new_string ("Some String"), "pingSample");
37 }
38
39 static void pingFail (struct afb_req request)
40 {
41         afb_req_fail(request, "failed", "Ping Binder Daemon fails");
42 }
43
44 static void pingNull (struct afb_req request)
45 {
46         ping(request, NULL, "pingNull");
47 }
48
49 static void pingBug (struct afb_req request)
50 {
51         ping((struct afb_req){NULL,NULL,NULL}, NULL, "pingBug");
52 }
53
54
55 // For samples https://linuxprograms.wordpress.com/2010/05/20/json-c-libjson-tutorial/
56 static void pingJson (struct afb_req request) {
57     json_object *jresp, *embed;    
58     
59     jresp = json_object_new_object();
60     json_object_object_add(jresp, "myString", json_object_new_string ("Some String"));
61     json_object_object_add(jresp, "myInt", json_object_new_int (1234));
62      
63     embed  = json_object_new_object();
64     json_object_object_add(embed, "subObjString", json_object_new_string ("Some String"));
65     json_object_object_add(embed, "subObjInt", json_object_new_int (5678));
66     
67     json_object_object_add(jresp,"eobj", embed);
68
69     ping(request, jresp, "pingJson");
70 }
71
72 // NOTE: this sample does not use session to keep test a basic as possible
73 //       in real application most APIs should be protected with AFB_SESSION_CHECK
74 static const struct AFB_restapi pluginApis[]= {
75   {"ping"     , AFB_SESSION_NONE, pingSample  , "Ping Application Framework"},
76   {"pingfail" , AFB_SESSION_NONE, pingFail    , "Fails"},
77   {"pingnull" , AFB_SESSION_NONE, pingNull    , "Return NULL"},
78   {"pingbug"  , AFB_SESSION_NONE, pingBug     , "Do a Memory Violation"},
79   {"pingJson" , AFB_SESSION_NONE, pingJson    , "Return a JSON object"},
80   {NULL}
81 };
82
83 static const struct AFB_plugin plugin_desc = {
84         .type = AFB_PLUGIN_JSON,
85         .info = "Minimal Hello World Sample",
86         .prefix = "hello",
87         .apis = pluginApis
88 };
89
90 const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)
91 {
92         return &plugin_desc;
93 }