c9d5070b807d743fa36d5de37ccbf0f06ae144b8
[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 #define WORKER_PKG_MGR "org.tizen.package-manager-worker"
23 #define WORKER_INFO_SYS "org.tizen.ttsd-worker-system"
24 #define WORKER_INFO_TASK "org.tizen.task-worker"
25
26 int __worker_launch_info(ttd_cmd_data *c_data)
27 {
28         app_control_h ac_h = NULL;
29         int ac_ret = APP_CONTROL_ERROR_NONE;
30         int ret = 0;
31         const char *cmd_id = NULL;
32         const char *app_id = NULL;
33
34         cmd_id = ttd_cmd_get_id(c_data);
35
36         switch (ttd_cmd_get_command(c_data)) {
37         case TTD_CMD_INFO_GET_SYSINFO:
38                 app_id = WORKER_INFO_SYS;
39                 break;
40         case TTD_CMD_INFO_GET_TASKINFO:
41                 app_id = WORKER_INFO_TASK;
42                 break;
43         case TTD_CMD_INFO_GET_MAX:
44         default:
45                 return -1;
46                 break;
47         }
48
49         ac_ret = app_control_create(&ac_h);
50         if (ac_ret != APP_CONTROL_ERROR_NONE) {
51                 ret = -1;
52                 goto FREE_N_RETURN;
53         }
54         app_control_set_operation(ac_h, APP_CONTROL_OPERATION_DEFAULT);
55         app_control_set_app_id(ac_h, app_id);
56
57         if (cmd_id)
58                 app_control_add_extra_data(ac_h, "id", cmd_id);
59
60         ac_ret = app_control_send_launch_request(ac_h, NULL, NULL);
61         if (ac_ret != APP_CONTROL_ERROR_NONE) {
62                 ret = -1;
63                 goto FREE_N_RETURN;
64         }
65
66 FREE_N_RETURN:
67         if (ac_h)
68                 app_control_destroy(ac_h);
69
70         return ret;
71 }
72
73 int __worker_launch_pkgmgr(ttd_cmd_data *c_data)
74 {
75         app_control_h ac_h = NULL;
76         int ac_ret = APP_CONTROL_ERROR_NONE;
77         const char *op = NULL;
78         char *extra = NULL;
79         unsigned int length = 0;
80         const char *cmd_id = NULL;
81         int ret = 0;
82
83         cmd_id = ttd_cmd_get_id(c_data);
84
85         switch (ttd_cmd_get_command(c_data)) {
86         case TTD_CMD_PACKAGE_INSTALL:
87                 op = "install";
88                 ttd_cmd_get_data(c_data, (void *)&extra, &length);
89                 break;
90         case TTD_CMD_PACKAGE_REMOVE:
91                 op = "uninstall";
92                 break;
93         case TTD_CMD_PACKAGE_GET_APP_LIST:
94                 op = "application";
95                 break;
96         case TTD_CMD_PACKAGE_GET_PACKAGE_LIST:
97                 op = "package";
98                 break;
99         case TTD_CMD_PACKAGE_MAX:
100         default:
101                 _E("unknown command - %d", ttd_cmd_get_command(c_data));
102                 return -1;
103         }
104
105         ac_ret = app_control_create(&ac_h);
106         if (ac_ret != APP_CONTROL_ERROR_NONE) {
107                 ret = -1;
108                 goto FREE_N_RETURN;
109         }
110         app_control_set_operation(ac_h, APP_CONTROL_OPERATION_DEFAULT);
111         app_control_set_app_id(ac_h, WORKER_PKG_MGR);
112         app_control_add_extra_data(ac_h, "operation", op);
113
114         if (cmd_id)
115                 app_control_add_extra_data(ac_h, "id", cmd_id);
116
117         if (extra)
118                 app_control_add_extra_data(ac_h, "meta", extra);
119
120         ac_ret = app_control_send_launch_request(ac_h, NULL, NULL);
121         if (ac_ret != APP_CONTROL_ERROR_NONE) {
122                 ret = -1;
123                 goto FREE_N_RETURN;
124         }
125
126 FREE_N_RETURN:
127         if (ac_h)
128                 app_control_destroy(ac_h);
129
130         if (extra)
131                 g_free(extra);
132
133         return ret;
134 }
135
136 int ttd_worker_launch(ttd_cmd_data *cmd_data)
137 {
138         ttd_cmd_data *c_data = cmd_data;
139         int ret = 0;
140         ttd_cmd_type_e cmd_type = TTD_CMD_TYPE_UNKNOWN;
141
142         retv_if(!cmd_data, -1);
143
144         cmd_type = ttd_cmd_get_type(c_data);
145
146         if (cmd_type == TTD_CMD_TYPE_PACKAGE) {
147                 ret = __worker_launch_pkgmgr(c_data);
148         } else if (cmd_type == TTD_CMD_TYPE_INFO) {
149                 ret = __worker_launch_info(c_data);
150         } else {
151                 _E("not supported cmd type - %d", cmd_type);
152                 ret = -1;
153         }
154         return ret;
155 }