Added tools - mockup of cloud and injector. Changed code to support task-worker.
[apps/native/tizen-things-daemon.git] / daemon / src / ttd-worker-handle.c
index 3160e18..69ae29b 100644 (file)
  * limitations under the License.
  */
 
+#include <glib.h>
+#include <app_control.h>
+#include "ttd-cmd.h"
+#include "ttd-log.h"
 
-int ttd_worker_launch(int worker_type, const char *cmd)
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define WORKER_PKG_MGR "org.tizen.package-manager-worker"
+#define WORKER_INFO_SYS "ttd-worker-system"
+#define WORKER_INFO_TASK "org.tizen.task-worker"
+#define WORKER_INFO_TASK_NAME "task-worker"
+#define WORKER_INFO_TASK_PATH "/usr/bin/task-worker"
+
+static int _exec_application_sysinfo(const char *cmd_id)
+{
+       app_control_h ac_h = NULL;
+       int ac_ret = app_control_create(&ac_h);
+       if (ac_ret != APP_CONTROL_ERROR_NONE) {
+               return -1;
+       }
+       app_control_set_operation(ac_h, APP_CONTROL_OPERATION_DEFAULT);
+       app_control_set_app_id(ac_h, WORKER_INFO_SYS);
+
+       app_control_add_extra_data(ac_h, "id", cmd_id);
+
+       ac_ret = app_control_send_launch_request(ac_h, NULL, NULL);
+       if (ac_ret != APP_CONTROL_ERROR_NONE) {
+               if (ac_h)
+                       app_control_destroy(ac_h);
+               return -1;
+       }
+       return 0;
+}
+
+static int _exec_application_pkgmgr(const char *cmd_id,const char *op,const char *extra)
 {
+       app_control_h ac_h = NULL;
+       int ac_ret = app_control_create(&ac_h);
+
+       if (ac_ret != APP_CONTROL_ERROR_NONE) {
+               return -1;
+       }
+       app_control_set_operation(ac_h, APP_CONTROL_OPERATION_DEFAULT);
+       app_control_set_app_id(ac_h, WORKER_PKG_MGR);
+       app_control_add_extra_data(ac_h, "operation", op);
+       app_control_add_extra_data(ac_h, "id", cmd_id);
+
+       if (extra)
+               app_control_add_extra_data(ac_h, "meta", extra);
+
+       ac_ret = app_control_send_launch_request(ac_h, NULL, NULL);
+       if (ac_ret != APP_CONTROL_ERROR_NONE) {
+               if (ac_h)
+                       app_control_destroy(ac_h);
+               return -1;
+       }
        return 0;
-}
\ No newline at end of file
+}
+
+static int _exec_process(const char *path, const char *name, const char *cmd_id, const char *data)
+{
+       const char * const args[4] = {name, cmd_id, data, NULL};
+
+       int child = fork();
+       if (child == -1)
+       {
+               _D("Failed to fork");
+               return -3;
+       }
+       else if (child > 0)
+       {
+               _D("Forked succesfully");
+               return 0;
+       }
+       else
+       {
+               execv(path, (char * const*) args);
+               _D("Failed to exec %s", path);
+               exit(EXIT_FAILURE);
+       }
+}
+
+int ttd_worker_handle_info(ttd_cmd_data *c_data)
+{
+       int ret = 0;
+       const char *cmd_id = NULL;
+       unsigned int l = 0;
+       void *data = NULL;
+
+       cmd_id = ttd_cmd_get_id(c_data);
+       retv_if(!cmd_id, -1);
+
+       switch (ttd_cmd_get_command(c_data)) {
+       case TTD_CMD_INFO_GET_SYSINFO:
+                       ret = _exec_application_sysinfo(cmd_id);
+                       break;
+               case TTD_CMD_INFO_GET_TASKINFO:
+                       ttd_cmd_get_data(c_data, &data,  &l);
+                       ret = _exec_process(WORKER_INFO_TASK_PATH, WORKER_INFO_TASK_NAME, cmd_id, data);
+                       break;
+       case TTD_CMD_INFO_GET_MAX:
+       default:
+               return -1;
+               break;
+       }
+       return ret;
+}
+
+int ttd_worker_handle_pkgmgr(ttd_cmd_data *c_data)
+{
+       const char *op = NULL;
+       char *extra = NULL;
+       unsigned int length = 0;
+       const char *cmd_id = NULL;
+       int ret = 0;
+
+       cmd_id = ttd_cmd_get_id(c_data);
+       retv_if(!cmd_id, -1);
+
+       switch (ttd_cmd_get_command(c_data)) {
+       case TTD_CMD_PACKAGE_INSTALL:
+               op = "install";
+               ttd_cmd_get_data(c_data, (void *)&extra, &length);
+               break;
+       case TTD_CMD_PACKAGE_REMOVE:
+               op = "uninstall";
+               break;
+       case TTD_CMD_PACKAGE_GET_APP_LIST:
+               op = "application";
+               break;
+       case TTD_CMD_PACKAGE_GET_PACKAGE_LIST:
+               op = "package";
+               break;
+       case TTD_CMD_PACKAGE_MAX:
+       default:
+               _E("unknown command - %d", ttd_cmd_get_command(c_data));
+               return -1;
+       }
+
+       ret = _exec_application_pkgmgr(cmd_id, op, extra);
+
+       if (extra)
+               g_free(extra);
+
+       return ret;
+}