Merge origin/master
[src/app-framework-binder.git] / plugins / afm-main-plugin / afm-main-plugin.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 #define _GNU_SOURCE         /* See feature_test_macros(7) */
20 #include <stdio.h>
21
22 #include "local-def.h"
23
24 #include "utils-jbus.h"
25
26 static const char _id_[]    = "id";
27 static const char _runid_[] = "runid";
28 static char _runnables_[]   = "runnables";
29 static char _detail_[]      = "detail";
30 static char _start_[]       = "start";
31 static char _terminate_[]   = "terminate";
32 static char _stop_[]        = "stop";
33 static char _continue_[]    = "continue";
34 static char _runners_[]     = "runners";
35 static char _state_[]       = "state";
36 static char _install_[]     = "install";
37 static char _uninstall_[]   = "uninstall";
38 static const char _mode_[]  = "mode";
39 static const char _local_[] = "local";
40 static const char _remote_[]= "remote";
41 static const char _auto_[]  = "auto";
42 static const char _uri_[]   = "uri";
43
44 static struct jbus *jbus;
45
46 static struct json_object *embed(AFB_request *request, const char *tag, struct json_object *obj)
47 {
48         struct json_object *result;
49
50         if (obj == NULL)
51                 result = NULL;
52         else if (!tag) {
53                 request->errcode = MHD_HTTP_OK;
54                 result = obj;
55         }
56         else {
57                 result = json_object_new_object();
58                 if (result == NULL) {
59                         /* can't embed */
60                         result = obj;
61                         request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR;
62                 }
63                 else {
64                         /* TODO why is json-c not returning a status? */
65                         json_object_object_add(result, tag, obj);
66                         request->errcode = MHD_HTTP_OK;
67                 }
68         }
69         return result;
70 }
71
72 static struct json_object *call(AFB_request *request, AFB_PostItem *item, const char *tag, struct json_object *(*fun)(AFB_request*,AFB_PostItem*))
73 {
74         return embed(request, tag, fun(request, item));
75 }
76
77 static struct json_object *call_void(AFB_request *request, AFB_PostItem *item)
78 {
79         struct json_object *obj = jbus_call_sj_sync(jbus, request->api, "true");
80         if (verbose)
81                 fprintf(stderr, "(afm-main-plugin) call_void: true -> %s\n", obj ? json_object_to_json_string(obj) : "NULL");
82         request->errcode = obj ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
83         return obj;
84 }
85
86 static struct json_object *call_appid(AFB_request *request, AFB_PostItem *item)
87 {
88         struct json_object *obj;
89         char *sid;
90         const char *id = getQueryValue(request, _id_);
91         if (id == NULL) {
92                 request->errcode = MHD_HTTP_BAD_REQUEST;
93                 return NULL;
94         }
95         if (0 >= asprintf(&sid, "\"%s\"", id)) {
96                 request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR;
97                 return NULL;
98         }
99         obj = jbus_call_sj_sync(jbus, request->api, sid);
100         if (verbose)
101                 fprintf(stderr, "(afm-main-plugin) call_appid: %s -> %s\n", sid, obj ? json_object_to_json_string(obj) : "NULL");
102         free(sid);
103         request->errcode = obj ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
104         return obj;
105 }
106
107 static struct json_object *call_runid(AFB_request *request, AFB_PostItem *item)
108 {
109         struct json_object *obj;
110         const char *id = getQueryValue(request, _runid_);
111         if (id == NULL) {
112                 request->errcode = MHD_HTTP_BAD_REQUEST;
113                 return NULL;
114         }
115         obj = jbus_call_sj_sync(jbus, request->api, id);
116         if (verbose)
117                 fprintf(stderr, "(afm-main-plugin) call_runid: %s -> %s\n", id, obj ? json_object_to_json_string(obj) : "NULL");
118         request->errcode = obj ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
119         return obj;
120 }
121
122 static struct json_object *call_void__runnables(AFB_request *request, AFB_PostItem *item)
123 {
124         return embed(request, _runnables_, call_void(request, item));
125 }
126
127 static struct json_object *call_start(AFB_request *request, AFB_PostItem *item)
128 {
129         struct json_object *resp;
130         const char *id, *mode;
131         char *query;
132         int rc;
133
134         /* get the id */
135         id = getQueryValue(request, _id_);
136         if (id == NULL) {
137                 request->errcode = MHD_HTTP_BAD_REQUEST;
138                 return NULL;
139         }
140         /* get the mode */
141         mode = getQueryValue(request, _mode_);
142         if (mode == NULL || !strcmp(mode, _auto_)) {
143                 mode = request->config->mode == AFB_MODE_REMOTE ? _remote_ : _local_;
144         }
145
146         /* create the query */
147         rc = asprintf(&query, "{\"id\":\"%s\",\"mode\":\"%s\"}", id, mode);
148         if (rc < 0) {
149                 request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR;
150                 return NULL;
151         }
152
153         /* calls the service */
154         resp = jbus_call_sj_sync(jbus, _start_, query);
155         if (verbose)
156                 fprintf(stderr, "(afm-main-plugin) call_start: %s -> %s\n", query, resp ? json_object_to_json_string(resp) : "NULL");
157         free(query);
158
159         /* embed if needed */
160         if (json_object_get_type(resp) == json_type_int)
161                 resp = embed(request, _runid_, resp);
162         request->errcode = resp ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
163         return resp;
164 }
165
166 static struct json_object *call_void__runners(AFB_request *request, AFB_PostItem *item)
167 {
168         return embed(request, _runners_, call_void(request, item));
169 }
170
171 static struct json_object *call_file__appid(AFB_request *request, AFB_PostItem *item)
172 {
173         if (item == NULL) {
174                 const char *filename = getPostPath(request);
175                 if (filename != NULL) {
176                         struct json_object *obj;
177                         char *query;
178                         request->jresp = NULL;
179                         if (0 >= asprintf(&query, "\"%s\"", filename))
180                                 request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR;
181                         else {
182                                 obj = jbus_call_sj_sync(jbus, request->api, query);
183                                 if (verbose)
184                                         fprintf(stderr, "(afm-main-plugin) call_file_appid: %s -> %s\n", query, obj ? json_object_to_json_string(obj) : "NULL");
185                                 free(query);
186                                 if (obj)
187                                         request->jresp = embed(request, _id_, obj);
188                                 else
189                                         request->errcode = MHD_HTTP_FAILED_DEPENDENCY;
190                         }
191                         unlink(filename);
192                 }
193         }
194         return getPostFile (request, item, "/tmp/upload");
195 }
196
197 static AFB_restapi plug_apis[] =
198 {
199         {_runnables_, AFB_SESSION_CHECK, (AFB_apiCB)call_void__runnables,  "Get list of runnable applications"},
200         {_detail_   , AFB_SESSION_CHECK, (AFB_apiCB)call_appid, "Get the details for one application"},
201         {_start_    , AFB_SESSION_CHECK, (AFB_apiCB)call_start, "Start an application"},
202         {_terminate_, AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Terminate a running application"},
203         {_stop_     , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Stop (pause) a running application"},
204         {_continue_ , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Continue (resume) a stopped application"},
205         {_runners_  , AFB_SESSION_CHECK, (AFB_apiCB)call_void__runners,  "Get the list of running applications"},
206         {_state_    , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Get the state of a running application"},
207         {_install_  , AFB_SESSION_CHECK, (AFB_apiCB)call_file__appid,  "Install an application using a widget file"},
208         {_uninstall_, AFB_SESSION_CHECK, (AFB_apiCB)call_appid, "Uninstall an application"},
209         {NULL}
210 };
211
212 static AFB_plugin plug_desc = {
213         .type = AFB_PLUGIN_JSON,
214         .info = "Application Framework Master Service",
215         .prefix = "afm-main",
216         .apis = plug_apis
217 };
218
219 AFB_plugin *pluginRegister()
220 {
221         jbus = create_jbus(1, "/org/AGL/afm/user");
222         if (jbus)
223                 return &plug_desc;
224         fprintf(stderr, "ERROR: %s:%d: can't connect to DBUS session\n", __FILE__, __LINE__);
225         return NULL;
226 }
227