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