259b42f13457cdb261546c9a3a27b9baf0217e9a
[src/app-framework-binder.git] / plugins / samples / HelloWorld.c
1 /*
2  * Copyright (C) 2015, 2016 "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-c/json.h>
21
22 #include <afb/afb-plugin.h>
23
24 const struct AFB_interface *interface;
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         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 static void pingEvent(struct afb_req request)
55 {
56         json_object *query = afb_req_json(request);
57         afb_daemon_broadcast_event(interface->daemon, "event", json_object_get(query));
58         ping(request, json_object_get(query), "event");
59 }
60
61
62 // For samples https://linuxprograms.wordpress.com/2010/05/20/json-c-libjson-tutorial/
63 static void pingJson (struct afb_req request) {
64     json_object *jresp, *embed;    
65     
66     jresp = json_object_new_object();
67     json_object_object_add(jresp, "myString", json_object_new_string ("Some String"));
68     json_object_object_add(jresp, "myInt", json_object_new_int (1234));
69      
70     embed  = json_object_new_object();
71     json_object_object_add(embed, "subObjString", json_object_new_string ("Some String"));
72     json_object_object_add(embed, "subObjInt", json_object_new_int (5678));
73     
74     json_object_object_add(jresp,"eobj", embed);
75
76     ping(request, jresp, "pingJson");
77 }
78
79 static void subcallcb (void *prequest, int iserror, json_object *object)
80 {
81         struct afb_req request = afb_req_unstore(prequest);
82         if (iserror)
83                 afb_req_fail(request, "failed", json_object_to_json_string(object));
84         else
85                 afb_req_success(request, object, NULL);
86         afb_req_unref(request);
87 }
88
89 static void subcall (struct afb_req request)
90 {
91         const char *api = afb_req_value(request, "api");
92         const char *verb = afb_req_value(request, "verb");
93         const char *args = afb_req_value(request, "args");
94         json_object *object = api && verb && args ? json_tokener_parse(args) : NULL;
95
96         if (object == NULL)
97                 afb_req_fail(request, "failed", "bad arguments");
98         else
99                 afb_req_subcall(request, api, verb, object, subcallcb, afb_req_store(request));
100 }
101
102 // NOTE: this sample does not use session to keep test a basic as possible
103 //       in real application most APIs should be protected with AFB_SESSION_CHECK
104 static const struct AFB_verb_desc_v1 verbs[]= {
105   {"ping"     , AFB_SESSION_NONE, pingSample  , "Ping Application Framework"},
106   {"pingfail" , AFB_SESSION_NONE, pingFail    , "Fails"},
107   {"pingnull" , AFB_SESSION_NONE, pingNull    , "Return NULL"},
108   {"pingbug"  , AFB_SESSION_NONE, pingBug     , "Do a Memory Violation"},
109   {"pingJson" , AFB_SESSION_NONE, pingJson    , "Return a JSON object"},
110   {"pingevent", AFB_SESSION_NONE, pingEvent   , "Send an event"},
111   {"subcall",   AFB_SESSION_NONE, subcall     , "Call api/verb(args)"},
112   {NULL}
113 };
114
115 static const struct AFB_plugin plugin_desc = {
116         .type = AFB_PLUGIN_VERSION_1,
117         .v1 = {
118                 .info = "Minimal Hello World Sample",
119                 .prefix = "hello",
120                 .verbs = verbs
121         }
122 };
123
124 const struct AFB_plugin *pluginAfbV1Register (const struct AFB_interface *itf)
125 {
126         interface = itf;
127         return &plugin_desc;
128 }