implements worker launch function
[apps/native/tizen-things-daemon.git] / daemon / src / ttd-worker-handle.c
index 3160e18..c9d5070 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)
+#define WORKER_PKG_MGR "org.tizen.package-manager-worker"
+#define WORKER_INFO_SYS "org.tizen.ttsd-worker-system"
+#define WORKER_INFO_TASK "org.tizen.task-worker"
+
+int __worker_launch_info(ttd_cmd_data *c_data)
+{
+       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;
+
+       cmd_id = ttd_cmd_get_id(c_data);
+
+       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:
+               return -1;
+               break;
+       }
+
+       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, app_id);
+
+       if (cmd_id)
+               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) {
+               ret = -1;
+               goto FREE_N_RETURN;
+       }
+
+FREE_N_RETURN:
+       if (ac_h)
+               app_control_destroy(ac_h);
+
+       return ret;
+}
+
+int __worker_launch_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;
+       const char *cmd_id = NULL;
+       int ret = 0;
+
+       cmd_id = ttd_cmd_get_id(c_data);
+
+       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;
+       }
+
+       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);
+
+       if (extra)
+               g_free(extra);
+
+       return ret;
+}
+
+int ttd_worker_launch(ttd_cmd_data *cmd_data)
 {
-       return 0;
-}
\ No newline at end of file
+       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;
+}