X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=plugins%2Fafm-main-plugin%2Fafm-main-plugin.c;h=8b26fe7eb0c13ab99f9c30325ba0ed54f6869f15;hb=f1b901ed676b2d45ec8e6ae3d6ef2f94d79f9ee6;hp=1fcc554bee46f47482aa7802cc67bb9fead41756;hpb=8ea2dc9f4df0a6d61e430d53d19eb0ea3633bf23;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 1fcc554b..8b26fe7e 100644 --- a/plugins/afm-main-plugin/afm-main-plugin.c +++ b/plugins/afm-main-plugin/afm-main-plugin.c @@ -2,226 +2,362 @@ * Copyright (C) 2015 "IoT.bzh" * Author "Fulup Ar Foll" * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * http://www.apache.org/licenses/LICENSE-2.0 * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #define _GNU_SOURCE /* See feature_test_macros(7) */ #include +#include +#include +#include -#include "local-def.h" +#include "afb-plugin.h" #include "utils-jbus.h" -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 const char _mode_[] = "mode"; -static const char _local_[] = "local"; -static const char _remote_[]= "remote"; -static const char _auto_[] = "auto"; -static const char _uri_[] = "uri"; +static const char _added_[] = "added"; +static const char _auto_[] = "auto"; +static const char _continue_[] = "continue"; +static const char _changed_[] = "changed"; +static const char _detail_[] = "detail"; +static const char _id_[] = "id"; +static const char _install_[] = "install"; +static const char _local_[] = "local"; +static const char _mode_[] = "mode"; +static const char _remote_[] = "remote"; +static const char _runid_[] = "runid"; +static const char _runnables_[] = "runnables"; +static const char _runners_[] = "runners"; +static const char _start_[] = "start"; +static const char _state_[] = "state"; +static const char _stop_[] = "stop"; +static const char _terminate_[] = "terminate"; +static const char _uninstall_[] = "uninstall"; +static const char _uri_[] = "uri"; + +static const struct AFB_interface *interface; +static struct afb_evmgr evmgr; static struct jbus *jbus; -static struct json_object *embed(AFB_request *request, const char *tag, struct json_object *obj) +struct memo +{ + struct afb_req request; + const char *method; +}; + +static struct memo *make_memo(struct afb_req request, const char *method) +{ + struct memo *memo = malloc(sizeof *memo); + if (memo != NULL) { + memo->request = request; + memo->method = method; + afb_req_addref(request); + } + return memo; +} + +static void application_list_changed(const char *data, void *closure) +{ + afb_evmgr_push(evmgr, "application-list-changed", NULL); +} + +static struct json_object *embed(const char *tag, struct json_object *obj) { struct json_object *result; if (obj == NULL) result = NULL; - else if (!tag) { - request->errcode = MHD_HTTP_OK; + else if (!tag) 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*)) +static void embed_call_void_callback(int status, struct json_object *obj, struct memo *memo) { - return embed(request, tag, fun(request, item)); + if (interface->verbosity) + fprintf(stderr, "(afm-main-plugin) %s(true) -> %s\n", memo->method, + obj ? json_object_to_json_string(obj) : "NULL"); + if (obj == NULL) { + afb_req_fail(memo->request, "failed", "framework daemon failure"); + } else { + obj = json_object_get(obj); + obj = embed(memo->method, obj); + if (obj == NULL) { + afb_req_fail(memo->request, "failed", "framework daemon failure"); + } else { + afb_req_success(memo->request, obj, NULL); + } + } + afb_req_unref(memo->request); + free(memo); } -static struct json_object *call_void(AFB_request *request, AFB_PostItem *item) +static void embed_call_void(struct afb_req request, const char *method) { - 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; + struct memo *memo = make_memo(request, method); + if (memo == NULL) + afb_req_fail(request, "failed", "out of memory"); + else if (jbus_call_sj(jbus, method, "true", (void*)embed_call_void_callback, memo) < 0) { + afb_req_fail(request, "failed", "dbus failure"); + free(memo); + } } -static struct json_object *call_appid(AFB_request *request, AFB_PostItem *item) +static void call_appid_callback(int status, struct json_object *obj, struct memo *memo) { - struct json_object *obj; + if (interface->verbosity) + fprintf(stderr, "(afm-main-plugin) %s -> %s\n", memo->method, + obj ? json_object_to_json_string(obj) : "NULL"); + if (obj == NULL) { + afb_req_fail(memo->request, "failed", "framework daemon failure"); + } else { + obj = json_object_get(obj); + afb_req_success(memo->request, obj, NULL); + } + afb_req_unref(memo->request); + free(memo); +} + +static void call_appid(struct afb_req request, const char *method) +{ + struct memo *memo; char *sid; - const char *id = getQueryValue(request, _id_); + const char *id = afb_req_value(request, _id_); if (id == NULL) { - request->errcode = MHD_HTTP_BAD_REQUEST; - return NULL; + afb_req_fail(request, "bad-request", "missing 'id'"); + return; } - if (0 >= asprintf(&sid, "\"%s\"", id)) { - request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR; - return NULL; + memo = make_memo(request, method); + if (asprintf(&sid, "\"%s\"", id) <= 0 || memo == NULL) { + afb_req_fail(request, "server-error", "out of memory"); + free(memo); + return; + } + if (jbus_call_sj(jbus, method, sid, (void*)call_appid_callback, memo) < 0) { + afb_req_fail(request, "failed", "dbus failure"); + free(memo); } - 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, AFB_PostItem *item) +static void call_runid(struct afb_req request, const char *method) { struct json_object *obj; - const char *id = getQueryValue(request, _runid_); + const char *id = afb_req_value(request, _runid_); if (id == NULL) { - request->errcode = MHD_HTTP_BAD_REQUEST; - return NULL; + afb_req_fail(request, "bad-request", "missing 'runid'"); + return; } - 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; + obj = jbus_call_sj_sync(jbus, method, id); + if (interface->verbosity) + fprintf(stderr, "(afm-main-plugin) %s(%s) -> %s\n", method, id, + obj ? json_object_to_json_string(obj) : "NULL"); + if (obj == NULL) { + afb_req_fail(request, "failed", "framework daemon failure"); + return; + } + obj = json_object_get(obj); + afb_req_success(request, obj, NULL); +} + +/************************** entries ******************************/ + +static void runnables(struct afb_req request) +{ + embed_call_void(request, _runnables_); } -static struct json_object *call_void__runnables(AFB_request *request, AFB_PostItem *item) +static void detail(struct afb_req request) { - return embed(request, _runnables_, call_void(request, item)); + call_appid(request, _detail_); } -static struct json_object *call_start(AFB_request *request, AFB_PostItem *item) +static void start(struct afb_req request) { - struct json_object *resp; + struct json_object *obj; const char *id, *mode; char *query; int rc; /* get the id */ - id = getQueryValue(request, _id_); + id = afb_req_value(request, _id_); if (id == NULL) { - request->errcode = MHD_HTTP_BAD_REQUEST; - return NULL; + afb_req_fail(request, "bad-request", "missing 'id'"); + return; } /* get the mode */ - mode = getQueryValue(request, _mode_); + mode = afb_req_value(request, _mode_); if (mode == NULL || !strcmp(mode, _auto_)) { - mode = request->config->mode == AFB_MODE_REMOTE ? _remote_ : _local_; + mode = interface->mode == AFB_MODE_REMOTE ? _remote_ : _local_; } /* create the query */ rc = asprintf(&query, "{\"id\":\"%s\",\"mode\":\"%s\"}", id, mode); if (rc < 0) { - request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR; - return NULL; + afb_req_fail(request, "server-error", "out of memory"); + return; } /* calls the service */ - resp = jbus_call_sj_sync(jbus, _start_, query); - if (verbose) - fprintf(stderr, "(afm-main-plugin) call_start: %s -> %s\n", query, resp ? json_object_to_json_string(resp) : "NULL"); + obj = jbus_call_sj_sync(jbus, _start_, query); + if (interface->verbosity) + fprintf(stderr, "(afm-main-plugin) start(%s) -> %s\n", query, + obj ? json_object_to_json_string(obj) : "NULL"); free(query); + /* check status */ + obj = json_object_get(obj); + if (obj == NULL) { + afb_req_fail(request, "failed", "framework daemon failure"); + return; + } + /* embed if needed */ - if (json_object_get_type(resp) == json_type_int) - resp = embed(request, _runid_, resp); - request->errcode = resp ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY; - return resp; -} - -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); - } + if (json_object_get_type(obj) == json_type_int) + obj = embed(_runid_, obj); + afb_req_success(request, obj, NULL); +} + +static void terminate(struct afb_req request) +{ + call_runid(request, _terminate_); +} + +static void stop(struct afb_req request) +{ + call_runid(request, _stop_); +} + +static void continue_(struct afb_req request) +{ + call_runid(request, _continue_); +} + +static void runners(struct afb_req request) +{ + embed_call_void(request, _runners_); +} + +static void state(struct afb_req request) +{ + call_runid(request, _state_); +} + +static void install(struct afb_req request) +{ + struct json_object *obj, *added; + char *query; + const char *filename; + struct afb_arg arg; + + /* get the argument */ + arg = afb_req_get(request, "widget"); + filename = arg.path; + if (filename == NULL) { + afb_req_fail(request, "bad-request", "missing 'widget' file"); + return; + } + + /* makes the query */ + if (0 >= asprintf(&query, "\"%s\"", filename)) { + afb_req_fail(request, "server-error", "out of memory"); + return; } - return getPostFile (request, item, "/tmp/upload"); + + obj = jbus_call_sj_sync(jbus, _install_, query); + if (interface->verbosity) + fprintf(stderr, "(afm-main-plugin) install(%s) -> %s\n", query, + obj ? json_object_to_json_string(obj) : "NULL"); + free(query); + + /* check status */ + if (obj == NULL) { + afb_req_fail(request, "failed", "framework daemon failure"); + return; + } + + /* embed if needed */ + if (json_object_object_get_ex(obj, _added_, &added)) + obj = added; + obj = json_object_get(obj); + obj = embed(_id_, obj); + afb_req_success(request, obj, NULL); } -static AFB_restapi plug_apis[] = +static void uninstall(struct afb_req request) { - {_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_start, "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} + call_appid(request, _uninstall_); +} + +static const struct AFB_restapi plug_apis[] = +{ + {_runnables_, AFB_SESSION_CHECK, runnables, "Get list of runnable applications"}, + {_detail_ , AFB_SESSION_CHECK, detail, "Get the details for one application"}, + {_start_ , AFB_SESSION_CHECK, start, "Start an application"}, + {_terminate_, AFB_SESSION_CHECK, terminate, "Terminate a running application"}, + {_stop_ , AFB_SESSION_CHECK, stop, "Stop (pause) a running application"}, + {_continue_ , AFB_SESSION_CHECK, continue_, "Continue (resume) a stopped application"}, + {_runners_ , AFB_SESSION_CHECK, runners, "Get the list of running applications"}, + {_state_ , AFB_SESSION_CHECK, state, "Get the state of a running application"}, + {_install_ , AFB_SESSION_CHECK, install, "Install an application using a widget file"}, + {_uninstall_, AFB_SESSION_CHECK, uninstall, "Uninstall an application"}, + { NULL, 0, NULL, NULL } }; -static AFB_plugin plug_desc = { +static const struct AFB_plugin plug_desc = { .type = AFB_PLUGIN_JSON, .info = "Application Framework Master Service", .prefix = "afm-main", .apis = plug_apis }; -AFB_plugin *pluginRegister() +const struct AFB_plugin *pluginRegister(const struct AFB_interface *itf) { - jbus = create_jbus(1, "/org/AGL/afm/user"); - if (jbus) - return &plug_desc; - fprintf(stderr, "ERROR: %s:%d: can't connect to DBUS session\n", __FILE__, __LINE__); - return NULL; + int rc; + struct sd_bus *sbus; + + /* records the interface */ + assert (interface == NULL); + interface = itf; + evmgr = afb_daemon_get_evmgr(itf->daemon); + + /* creates the jbus for accessing afm-user-daemon */ + sbus = afb_daemon_get_user_bus(itf->daemon); + if (sbus == NULL) + return NULL; + jbus = create_jbus(sbus, "/org/AGL/afm/user"); + if (jbus == NULL) + return NULL; + + /* records the signal handler */ + rc = jbus_on_signal_s(jbus, _changed_, application_list_changed, NULL); + if (rc < 0) { + jbus_unref(jbus); + return NULL; + } + + return &plug_desc; }