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 c9d5070..69ae29b 100644 (file)
 #include "ttd-cmd.h"
 #include "ttd-log.h"
 
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+
 #define WORKER_PKG_MGR "org.tizen.package-manager-worker"
-#define WORKER_INFO_SYS "org.tizen.ttsd-worker-system"
+#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"
 
-int __worker_launch_info(ttd_cmd_data *c_data)
+static int _exec_application_sysinfo(const char *cmd_id)
 {
        app_control_h ac_h = NULL;
-       int ac_ret = APP_CONTROL_ERROR_NONE;
-       int ret = 0;
-       const char *cmd_id = NULL;
-       const char *app_id = 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);
 
-       cmd_id = ttd_cmd_get_id(c_data);
+       app_control_add_extra_data(ac_h, "id", cmd_id);
 
-       switch (ttd_cmd_get_command(c_data)) {
-       case TTD_CMD_INFO_GET_SYSINFO:
-               app_id = WORKER_INFO_SYS;
-               break;
-       case TTD_CMD_INFO_GET_TASKINFO:
-               app_id = WORKER_INFO_TASK;
-               break;
-       case TTD_CMD_INFO_GET_MAX:
-       default:
+       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;
-               break;
        }
+       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);
 
-       ac_ret = app_control_create(&ac_h);
        if (ac_ret != APP_CONTROL_ERROR_NONE) {
-               ret = -1;
-               goto FREE_N_RETURN;
+               return -1;
        }
        app_control_set_operation(ac_h, APP_CONTROL_OPERATION_DEFAULT);
-       app_control_set_app_id(ac_h, app_id);
+       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 (cmd_id)
-               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) {
-               ret = -1;
-               goto FREE_N_RETURN;
+               if (ac_h)
+                       app_control_destroy(ac_h);
+               return -1;
        }
+       return 0;
+}
 
-FREE_N_RETURN:
-       if (ac_h)
-               app_control_destroy(ac_h);
+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 __worker_launch_pkgmgr(ttd_cmd_data *c_data)
+int ttd_worker_handle_pkgmgr(ttd_cmd_data *c_data)
 {
-       app_control_h ac_h = NULL;
-       int ac_ret = APP_CONTROL_ERROR_NONE;
        const char *op = NULL;
        char *extra = NULL;
        unsigned int length = 0;
@@ -81,6 +133,7 @@ int __worker_launch_pkgmgr(ttd_cmd_data *c_data)
        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:
@@ -102,54 +155,10 @@ int __worker_launch_pkgmgr(ttd_cmd_data *c_data)
                return -1;
        }
 
-       ac_ret = app_control_create(&ac_h);
-       if (ac_ret != APP_CONTROL_ERROR_NONE) {
-               ret = -1;
-               goto FREE_N_RETURN;
-       }
-       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);
-
-       if (cmd_id)
-               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) {
-               ret = -1;
-               goto FREE_N_RETURN;
-       }
-
-FREE_N_RETURN:
-       if (ac_h)
-               app_control_destroy(ac_h);
+       ret = _exec_application_pkgmgr(cmd_id, op, extra);
 
        if (extra)
                g_free(extra);
 
        return ret;
 }
-
-int ttd_worker_launch(ttd_cmd_data *cmd_data)
-{
-       ttd_cmd_data *c_data = cmd_data;
-       int ret = 0;
-       ttd_cmd_type_e cmd_type = TTD_CMD_TYPE_UNKNOWN;
-
-       retv_if(!cmd_data, -1);
-
-       cmd_type = ttd_cmd_get_type(c_data);
-
-       if (cmd_type == TTD_CMD_TYPE_PACKAGE) {
-               ret = __worker_launch_pkgmgr(c_data);
-       } else if (cmd_type == TTD_CMD_TYPE_INFO) {
-               ret = __worker_launch_info(c_data);
-       } else {
-               _E("not supported cmd type - %d", cmd_type);
-               ret = -1;
-       }
-       return ret;
-}