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
20 #include "local-def.h"
21
22 #include "utils-jbus.h"
23
24 static const char _id_[] = "id";
25 static struct jbus *jbus;
26
27 static struct json_object *call_void(AFB_request *request)
28 {
29         struct json_object *obj = jbus_call_sj_sync(jbus, request->api, "true");
30         request->errcode = obj ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
31         return obj;
32 }
33
34 static struct json_object *call_appid(AFB_request *request)
35 {
36         struct json_object *obj;
37         char *sid;
38         const char *id = getQueryValue(request, _id_);
39         if (id == NULL) {
40                 request->errcode = MHD_HTTP_BAD_REQUEST;
41                 return NULL;
42         }
43         if (0 >= asprintf(&sid, "\"%s\"", id)) {
44                 request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR;
45                 return NULL;
46         }
47         obj = jbus_call_sj_sync(jbus, request->api, sid);
48         free(sid);
49         request->errcode = obj ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
50         return obj;
51 }
52
53 static struct json_object *call_runid(AFB_request *request)
54 {
55         struct json_object *obj;
56         const char *id = getQueryValue(request, _id_);
57         if (id == NULL) {
58                 request->errcode = MHD_HTTP_BAD_REQUEST;
59                 return NULL;
60         }
61         obj = jbus_call_sj_sync(jbus, request->api, id);
62         request->errcode = obj ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
63         return obj;
64 }
65
66 static AFB_restapi plug_apis[] =
67 {
68         {"runnables", AFB_SESSION_CHECK, (AFB_apiCB)call_void,  "Get list of runnable applications"},
69         {"detail"   , AFB_SESSION_CHECK, (AFB_apiCB)call_appid, "Get the details for one application"},
70         {"start"    , AFB_SESSION_CHECK, (AFB_apiCB)call_appid, "Start an application"},
71         {"terminate", AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Terminate a running application"},
72         {"stop"     , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Stop (pause) a running application"},
73         {"continue" , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Continue (resume) a stopped application"},
74         {"runners"  , AFB_SESSION_CHECK, (AFB_apiCB)call_void,  "Get the list of running applications"},
75         {"state"    , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Get the state of a running application"},
76         {NULL}
77 };
78
79 static AFB_plugin plug_desc = {
80         .type = AFB_PLUGIN_JSON,
81         .info = "Application Framework Master Service",
82         .prefix = "afm-main",
83         .apis = plug_apis
84 };
85
86 AFB_plugin *pluginRegister()
87 {
88         jbus = create_jbus(1, "/org/AGL/afm/user");
89         return jbus ? &plug_desc : NULL;
90 }
91