2 * Copyright (C) 2015 "IoT.bzh"
3 * Author "Fulup Ar Foll"
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.
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.
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/>.
19 #define _GNU_SOURCE /* See feature_test_macros(7) */
22 #include "local-def.h"
24 #include "utils-jbus.h"
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";
44 static struct jbus *jbus;
46 static struct json_object *embed(AFB_request *request, const char *tag, struct json_object *obj)
48 struct json_object *result;
53 request->errcode = MHD_HTTP_OK;
57 result = json_object_new_object();
61 request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR;
64 /* TODO why is json-c not returning a status? */
65 json_object_object_add(result, tag, obj);
66 request->errcode = MHD_HTTP_OK;
72 static struct json_object *call_void(AFB_request *request, AFB_PostItem *item)
74 struct json_object *obj = jbus_call_sj_sync(jbus, request->api, "true");
76 fprintf(stderr, "(afm-main-plugin) call_void: true -> %s\n", obj ? json_object_to_json_string(obj) : "NULL");
77 request->errcode = obj ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
81 static struct json_object *call_appid(AFB_request *request, AFB_PostItem *item)
83 struct json_object *obj;
85 const char *id = getQueryValue(request, _id_);
87 request->errcode = MHD_HTTP_BAD_REQUEST;
90 if (0 >= asprintf(&sid, "\"%s\"", id)) {
91 request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR;
94 obj = jbus_call_sj_sync(jbus, request->api, sid);
96 fprintf(stderr, "(afm-main-plugin) call_appid: %s -> %s\n", sid, obj ? json_object_to_json_string(obj) : "NULL");
98 request->errcode = obj ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
102 static struct json_object *call_runid(AFB_request *request, AFB_PostItem *item)
104 struct json_object *obj;
105 const char *id = getQueryValue(request, _runid_);
107 request->errcode = MHD_HTTP_BAD_REQUEST;
110 obj = jbus_call_sj_sync(jbus, request->api, id);
112 fprintf(stderr, "(afm-main-plugin) call_runid: %s -> %s\n", id, obj ? json_object_to_json_string(obj) : "NULL");
113 request->errcode = obj ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
117 static struct json_object *call_void__runnables(AFB_request *request, AFB_PostItem *item)
119 return embed(request, _runnables_, call_void(request, item));
122 static struct json_object *call_start(AFB_request *request, AFB_PostItem *item)
124 struct json_object *resp;
125 const char *id, *mode;
130 id = getQueryValue(request, _id_);
132 request->errcode = MHD_HTTP_BAD_REQUEST;
136 mode = getQueryValue(request, _mode_);
137 if (mode == NULL || !strcmp(mode, _auto_)) {
138 mode = request->config->mode == AFB_MODE_REMOTE ? _remote_ : _local_;
141 /* create the query */
142 rc = asprintf(&query, "{\"id\":\"%s\",\"mode\":\"%s\"}", id, mode);
144 request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR;
148 /* calls the service */
149 resp = jbus_call_sj_sync(jbus, _start_, query);
151 fprintf(stderr, "(afm-main-plugin) call_start: %s -> %s\n", query, resp ? json_object_to_json_string(resp) : "NULL");
154 /* embed if needed */
155 if (json_object_get_type(resp) == json_type_int)
156 resp = embed(request, _runid_, resp);
157 request->errcode = resp ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
161 static struct json_object *call_void__runners(AFB_request *request, AFB_PostItem *item)
163 return embed(request, _runners_, call_void(request, item));
166 static struct json_object *call_file__appid(AFB_request *request, AFB_PostItem *item)
169 const char *filename = getPostPath(request);
170 if (filename != NULL) {
171 struct json_object *obj;
173 request->jresp = NULL;
174 if (0 >= asprintf(&query, "\"%s\"", filename))
175 request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR;
177 obj = jbus_call_sj_sync(jbus, request->api, query);
179 fprintf(stderr, "(afm-main-plugin) call_file_appid: %s -> %s\n", query, obj ? json_object_to_json_string(obj) : "NULL");
182 request->jresp = embed(request, _id_, obj);
184 request->errcode = MHD_HTTP_FAILED_DEPENDENCY;
189 return getPostFile (request, item, "/tmp/upload");
192 static AFB_restapi plug_apis[] =
194 {_runnables_, AFB_SESSION_CHECK, (AFB_apiCB)call_void__runnables, "Get list of runnable applications"},
195 {_detail_ , AFB_SESSION_CHECK, (AFB_apiCB)call_appid, "Get the details for one application"},
196 {_start_ , AFB_SESSION_CHECK, (AFB_apiCB)call_start, "Start an application"},
197 {_terminate_, AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Terminate a running application"},
198 {_stop_ , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Stop (pause) a running application"},
199 {_continue_ , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Continue (resume) a stopped application"},
200 {_runners_ , AFB_SESSION_CHECK, (AFB_apiCB)call_void__runners, "Get the list of running applications"},
201 {_state_ , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Get the state of a running application"},
202 {_install_ , AFB_SESSION_CHECK, (AFB_apiCB)call_file__appid, "Install an application using a widget file"},
203 {_uninstall_, AFB_SESSION_CHECK, (AFB_apiCB)call_appid, "Uninstall an application"},
204 { NULL, 0, NULL, NULL }
207 static AFB_plugin plug_desc = {
208 .type = AFB_PLUGIN_JSON,
209 .info = "Application Framework Master Service",
210 .prefix = "afm-main",
214 AFB_plugin *pluginRegister()
216 jbus = create_jbus_session("/org/AGL/afm/user");
219 fprintf(stderr, "ERROR: %s:%d: can't connect to DBUS session\n", __FILE__, __LINE__);