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
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.1 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <glib.h>
18 #include <app_control.h>
19 #include "ttd-cmd.h"
20 #include "ttd-log.h"
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25
26 #define WORKER_PKG_MGR "org.tizen.package-manager-worker"
27 #define WORKER_INFO_SYS "ttd-worker-system"
28 #define WORKER_INFO_TASK "org.tizen.task-worker"
29 #define WORKER_INFO_TASK_NAME "task-worker"
30 #define WORKER_INFO_TASK_PATH "/usr/bin/task-worker"
31
32 static int _exec_application_sysinfo(const char *cmd_id)
33 {
34         app_control_h ac_h = NULL;
35         int ac_ret = app_control_create(&ac_h);
36         if (ac_ret != APP_CONTROL_ERROR_NONE) {
37                 return -1;
38         }
39         app_control_set_operation(ac_h, APP_CONTROL_OPERATION_DEFAULT);
40         app_control_set_app_id(ac_h, WORKER_INFO_SYS);
41
42         app_control_add_extra_data(ac_h, "id", cmd_id);
43
44         ac_ret = app_control_send_launch_request(ac_h, NULL, NULL);
45         if (ac_ret != APP_CONTROL_ERROR_NONE) {
46                 if (ac_h)
47                         app_control_destroy(ac_h);
48                 return -1;
49         }
50         return 0;
51 }
52
53 static int _exec_application_pkgmgr(const char *cmd_id,const char *op,const char *extra)
54 {
55         app_control_h ac_h = NULL;
56         int ac_ret = app_control_create(&ac_h);
57
58         if (ac_ret != APP_CONTROL_ERROR_NONE) {
59                 return -1;
60         }
61         app_control_set_operation(ac_h, APP_CONTROL_OPERATION_DEFAULT);
62         app_control_set_app_id(ac_h, WORKER_PKG_MGR);
63         app_control_add_extra_data(ac_h, "operation", op);
64         app_control_add_extra_data(ac_h, "id", cmd_id);
65
66         if (extra)
67                 app_control_add_extra_data(ac_h, "meta", extra);
68
69         ac_ret = app_control_send_launch_request(ac_h, NULL, NULL);
70         if (ac_ret != APP_CONTROL_ERROR_NONE) {
71                 if (ac_h)
72                         app_control_destroy(ac_h);
73                 return -1;
74         }
75         return 0;
76 }
77
78 static int _exec_process(const char *path, const char *name, const char *cmd_id, const char *data)
79 {
80         const char * const args[4] = {name, cmd_id, data, NULL};
81
82         int child = fork();
83         if (child == -1)
84         {
85                 _D("Failed to fork");
86                 return -3;
87         }
88         else if (child > 0)
89         {
90                 _D("Forked succesfully");
91                 return 0;
92         }
93         else
94         {
95                 execv(path, (char * const*) args);
96                 _D("Failed to exec %s", path);
97                 exit(EXIT_FAILURE);
98         }
99 }
100
101 int ttd_worker_handle_info(ttd_cmd_data *c_data)
102 {
103         int ret = 0;
104         const char *cmd_id = NULL;
105         unsigned int l = 0;
106         void *data = NULL;
107
108         cmd_id = ttd_cmd_get_id(c_data);
109         retv_if(!cmd_id, -1);
110
111         switch (ttd_cmd_get_command(c_data)) {
112         case TTD_CMD_INFO_GET_SYSINFO:
113                         ret = _exec_application_sysinfo(cmd_id);
114                         break;
115                 case TTD_CMD_INFO_GET_TASKINFO:
116                         ttd_cmd_get_data(c_data, &data,  &l);
117                         ret = _exec_process(WORKER_INFO_TASK_PATH, WORKER_INFO_TASK_NAME, cmd_id, data);
118                         break;
119         case TTD_CMD_INFO_GET_MAX:
120         default:
121                 return -1;
122                 break;
123         }
124         return ret;
125 }
126
127 int ttd_worker_handle_pkgmgr(ttd_cmd_data *c_data)
128 {
129         const char *op = NULL;
130         char *extra = NULL;
131         unsigned int length = 0;
132         const char *cmd_id = NULL;
133         int ret = 0;
134
135         cmd_id = ttd_cmd_get_id(c_data);
136         retv_if(!cmd_id, -1);
137
138         switch (ttd_cmd_get_command(c_data)) {
139         case TTD_CMD_PACKAGE_INSTALL:
140                 op = "install";
141                 ttd_cmd_get_data(c_data, (void *)&extra, &length);
142                 break;
143         case TTD_CMD_PACKAGE_REMOVE:
144                 op = "uninstall";
145                 break;
146         case TTD_CMD_PACKAGE_GET_APP_LIST:
147                 op = "application";
148                 break;
149         case TTD_CMD_PACKAGE_GET_PACKAGE_LIST:
150                 op = "package";
151                 break;
152         case TTD_CMD_PACKAGE_MAX:
153         default:
154                 _E("unknown command - %d", ttd_cmd_get_command(c_data));
155                 return -1;
156         }
157
158         ret = _exec_application_pkgmgr(cmd_id, op, extra);
159
160         if (extra)
161                 g_free(extra);
162
163         return ret;
164 }