replace value to macro for HTTP OK
[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 "ttd-worker-system"
24 #define WORKER_INFO_TASK "org.tizen.task-worker"
25
26 int ttd_worker_handle_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         retv_if(!cmd_id, -1);
36
37         switch (ttd_cmd_get_command(c_data)) {
38         case TTD_CMD_INFO_GET_SYSINFO:
39                 app_id = WORKER_INFO_SYS;
40                 break;
41         case TTD_CMD_INFO_GET_TASKINFO:
42                 app_id = WORKER_INFO_TASK;
43                 break;
44         case TTD_CMD_INFO_GET_MAX:
45         default:
46                 return -1;
47                 break;
48         }
49
50         ac_ret = app_control_create(&ac_h);
51         if (ac_ret != APP_CONTROL_ERROR_NONE) {
52                 ret = -1;
53                 goto FREE_N_RETURN;
54         }
55         app_control_set_operation(ac_h, APP_CONTROL_OPERATION_DEFAULT);
56         app_control_set_app_id(ac_h, app_id);
57
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 ttd_worker_handle_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         retv_if(!cmd_id, -1);
85
86         switch (ttd_cmd_get_command(c_data)) {
87         case TTD_CMD_PACKAGE_INSTALL:
88                 op = "install";
89                 ttd_cmd_get_data(c_data, (void *)&extra, &length);
90                 break;
91         case TTD_CMD_PACKAGE_REMOVE:
92                 op = "uninstall";
93                 break;
94         case TTD_CMD_PACKAGE_GET_APP_LIST:
95                 op = "application";
96                 break;
97         case TTD_CMD_PACKAGE_GET_PACKAGE_LIST:
98                 op = "package";
99                 break;
100         case TTD_CMD_PACKAGE_MAX:
101         default:
102                 _E("unknown command - %d", ttd_cmd_get_command(c_data));
103                 return -1;
104         }
105
106         ac_ret = app_control_create(&ac_h);
107         if (ac_ret != APP_CONTROL_ERROR_NONE) {
108                 ret = -1;
109                 goto FREE_N_RETURN;
110         }
111         app_control_set_operation(ac_h, APP_CONTROL_OPERATION_DEFAULT);
112         app_control_set_app_id(ac_h, WORKER_PKG_MGR);
113         app_control_add_extra_data(ac_h, "operation", op);
114         app_control_add_extra_data(ac_h, "id", cmd_id);
115
116         if (extra)
117                 app_control_add_extra_data(ac_h, "meta", extra);
118
119         ac_ret = app_control_send_launch_request(ac_h, NULL, NULL);
120         if (ac_ret != APP_CONTROL_ERROR_NONE) {
121                 ret = -1;
122                 goto FREE_N_RETURN;
123         }
124
125 FREE_N_RETURN:
126         if (ac_h)
127                 app_control_destroy(ac_h);
128
129         if (extra)
130                 g_free(extra);
131
132         return ret;
133 }