X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=plugins%2Fafm-main-plugin%2Fafm-main-plugin.c;h=fa184bf869f69683ee7f551a937efc7b623f4227;hb=cdd17e5b4a9f99361d4809317e62a585ac55521a;hp=bca28fe05375d2e8944b068d8e8f8f305e7a6f3b;hpb=5d293eac235f2d59eccc9b23de803821b821c12e;p=src%2Fapp-framework-binder.git diff --git a/plugins/afm-main-plugin/afm-main-plugin.c b/plugins/afm-main-plugin/afm-main-plugin.c index bca28fe0..fa184bf8 100644 --- a/plugins/afm-main-plugin/afm-main-plugin.c +++ b/plugins/afm-main-plugin/afm-main-plugin.c @@ -21,17 +21,62 @@ #include "utils-jbus.h" -static const char _id_[] = "id"; +static const char _id_[] = "id"; +static const char _runid_[] = "runid"; +static char _runnables_[] = "runnables"; +static char _detail_[] = "detail"; +static char _start_[] = "start"; +static char _terminate_[] = "terminate"; +static char _stop_[] = "stop"; +static char _continue_[] = "continue"; +static char _runners_[] = "runners"; +static char _state_[] = "state"; +static char _install_[] = "install"; +static char _uninstall_[] = "uninstall"; + static struct jbus *jbus; -static struct json_object *call_void(AFB_request *request) +static struct json_object *embed(AFB_request *request, const char *tag, struct json_object *obj) +{ + struct json_object *result; + + if (obj == NULL) + result = NULL; + else if (!tag) { + request->errcode = MHD_HTTP_OK; + result = obj; + } + else { + result = json_object_new_object(); + if (result == NULL) { + /* can't embed */ + result = obj; + request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR; + } + else { + /* TODO why is json-c not returning a status? */ + json_object_object_add(result, tag, obj); + request->errcode = MHD_HTTP_OK; + } + } + return result; +} + +static struct json_object *call(AFB_request *request, AFB_PostItem *item, const char *tag, struct json_object *(*fun)(AFB_request*,AFB_PostItem*)) +{ + return embed(request, tag, fun(request, item)); +} + +static struct json_object *call_void(AFB_request *request, AFB_PostItem *item) { struct json_object *obj = jbus_call_sj_sync(jbus, request->api, "true"); + if (verbose) + fprintf(stderr, "(afm-main-plugin) call_void: true -> %s\n", obj ? json_object_to_json_string(obj) : "NULL"); request->errcode = obj ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY; return obj; } -static struct json_object *call_appid(AFB_request *request) +static struct json_object *call_appid(AFB_request *request, AFB_PostItem *item) { struct json_object *obj; char *sid; @@ -45,34 +90,81 @@ static struct json_object *call_appid(AFB_request *request) return NULL; } obj = jbus_call_sj_sync(jbus, request->api, sid); + if (verbose) + fprintf(stderr, "(afm-main-plugin) call_appid: %s -> %s\n", sid, obj ? json_object_to_json_string(obj) : "NULL"); free(sid); request->errcode = obj ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY; return obj; } -static struct json_object *call_runid(AFB_request *request) +static struct json_object *call_runid(AFB_request *request, AFB_PostItem *item) { struct json_object *obj; - const char *id = getQueryValue(request, _id_); + const char *id = getQueryValue(request, _runid_); if (id == NULL) { request->errcode = MHD_HTTP_BAD_REQUEST; return NULL; } obj = jbus_call_sj_sync(jbus, request->api, id); + if (verbose) + fprintf(stderr, "(afm-main-plugin) call_runid: %s -> %s\n", id, obj ? json_object_to_json_string(obj) : "NULL"); request->errcode = obj ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY; return obj; } +static struct json_object *call_void__runnables(AFB_request *request, AFB_PostItem *item) +{ + return embed(request, _runnables_, call_void(request, item)); +} + +static struct json_object *call_appid__runid(AFB_request *request, AFB_PostItem *item) +{ + return embed(request, _runid_, call_appid(request, item)); +} + +static struct json_object *call_void__runners(AFB_request *request, AFB_PostItem *item) +{ + return embed(request, _runners_, call_void(request, item)); +} + +static struct json_object *call_file__appid(AFB_request *request, AFB_PostItem *item) +{ + if (item == NULL) { + const char *filename = getPostPath(request); + if (filename != NULL) { + struct json_object *obj; + char *query; + request->jresp = NULL; + if (0 >= asprintf(&query, "\"%s\"", filename)) + request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR; + else { + obj = jbus_call_sj_sync(jbus, request->api, query); + if (verbose) + fprintf(stderr, "(afm-main-plugin) call_file_appid: %s -> %s\n", query, obj ? json_object_to_json_string(obj) : "NULL"); + free(query); + if (obj) + request->jresp = embed(request, _id_, obj); + else + request->errcode = MHD_HTTP_FAILED_DEPENDENCY; + } + unlink(filename); + } + } + return getPostFile (request, item, "/tmp/upload"); +} + static AFB_restapi plug_apis[] = { - {"runnables", AFB_SESSION_CHECK, (AFB_apiCB)call_void, "Get list of runnable applications"}, - {"detail" , AFB_SESSION_CHECK, (AFB_apiCB)call_appid, "Get the details for one application"}, - {"start" , AFB_SESSION_CHECK, (AFB_apiCB)call_appid, "Start an application"}, - {"terminate", AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Terminate a running application"}, - {"stop" , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Stop (pause) a running application"}, - {"continue" , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Continue (resume) a stopped application"}, - {"runners" , AFB_SESSION_CHECK, (AFB_apiCB)call_void, "Get the list of running applications"}, - {"state" , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Get the state of a running application"}, + {_runnables_, AFB_SESSION_CHECK, (AFB_apiCB)call_void__runnables, "Get list of runnable applications"}, + {_detail_ , AFB_SESSION_CHECK, (AFB_apiCB)call_appid, "Get the details for one application"}, + {_start_ , AFB_SESSION_CHECK, (AFB_apiCB)call_appid__runid, "Start an application"}, + {_terminate_, AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Terminate a running application"}, + {_stop_ , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Stop (pause) a running application"}, + {_continue_ , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Continue (resume) a stopped application"}, + {_runners_ , AFB_SESSION_CHECK, (AFB_apiCB)call_void__runners, "Get the list of running applications"}, + {_state_ , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Get the state of a running application"}, + {_install_ , AFB_SESSION_CHECK, (AFB_apiCB)call_file__appid, "Install an application using a widget file"}, + {_uninstall_, AFB_SESSION_CHECK, (AFB_apiCB)call_appid, "Uninstall an application"}, {NULL} }; @@ -86,6 +178,9 @@ static AFB_plugin plug_desc = { AFB_plugin *pluginRegister() { jbus = create_jbus(1, "/org/AGL/afm/user"); - return jbus ? &plug_desc : NULL; + if (jbus) + return &plug_desc; + fprintf(stderr, "ERROR: %s:%d: can't connect to DBUS session\n", __FILE__, __LINE__); + return NULL; }