add command function to upload log
[apps/native/tizen-things-daemon.git] / daemon / src / ttd-cmd-func.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 <glib/gstdio.h>
19 #include "ttd-log.h"
20 #include "ttd-cmd.h"
21 #include "ttd-config.h"
22 #include "ttd-worker-handle.h"
23 #include "ttd-diag.h"
24 #include "ttd-http.h"
25 #include "ttd-cmd-mgr.h"
26
27 static int __config_write(ttd_cmd_data *c_data)
28 {
29         void *data = NULL;
30         unsigned int data_len = 0;
31         int ret = 0;
32
33         retv_if(!c_data, -1);
34
35         ret = ttd_cmd_get_data(c_data, &data, &data_len);
36         retv_if(ret < 0, -1);
37
38         switch (ttd_cmd_get_command(c_data))
39         {
40         case TTD_CMD_CONFIG_SET_SERVER_URL:
41                 ret = ttd_config_write_string("url", "server", (char *)data);
42                 if (ret < 0) {
43                         _E("Failed to set server url[%s]", (char *)data);
44                         return -1;
45                 }
46                 break;
47         case TTD_CMD_CONFIG_SET_APP_ID:
48                 ret = ttd_config_write_string("worker", "appid", (char *)data);
49                 if (ret < 0) {
50                         _E("Failed to set app id[%s]", (char *)data);
51                         return -1;
52                 }
53                 break;
54         default:
55                 _E("Strange action");
56                 return -1;
57                 break;
58         }
59
60         ttd_cmd_mgr_push_result(
61                 ttd_cmd_get_id(c_data), TTD_CMD_RESULT_SUCCESS, NULL);
62
63         return 0;
64 }
65
66 static int __worker_launch(ttd_cmd_data *c_data)
67 {
68         int ret = 0;
69         ttd_cmd_type_e cmd_type = TTD_CMD_TYPE_UNKNOWN;
70
71         retv_if(!c_data, -1);
72
73         cmd_type = ttd_cmd_get_type(c_data);
74
75         if (cmd_type == TTD_CMD_TYPE_PACKAGE) {
76                 ret = ttd_worker_handle_pkgmgr(c_data);
77         } else if (cmd_type == TTD_CMD_TYPE_INFO) {
78                 ret = ttd_worker_handle_info(c_data);
79         } else {
80                 _E("not supported cmd type - %d", cmd_type);
81                 ret = -1;
82         }
83         return ret;
84 }
85
86 static int __diagnosis_run(ttd_cmd_data *c_data)
87 {
88         int ret = 0;
89         ttd_cmd_diagnosis_e diag_type;
90         char *zip_file = NULL;
91         long res_code = 0;
92         char *res_msg = NULL;
93         char *result_json = NULL;
94
95         retv_if(!c_data, -1);
96
97         diag_type = ttd_cmd_get_command(c_data);
98
99         if (diag_type != TTD_CMD_DIAGNOSIS_GET_LOG)
100                 return -1;
101
102         zip_file = ttd_diag_collect_log_to_zip();
103         retv_if(!zip_file, -1);
104
105         ret = ttd_http_logfile_post(zip_file, &res_code, &res_msg);
106         if (ret || (res_code != TTD_HTTP_OK)) {
107                 _E("failed to upload logfile[%s]", zip_file);
108                 _E("res_code [%ls] with res_msg[%s]", res_code, res_msg);
109                 g_free(res_msg);
110                 g_remove(zip_file);
111                 g_free(zip_file);
112                 return -1;
113         }
114
115         result_json = g_strdup_printf("{ \"url\" : \"%s\" }", res_msg);
116         g_free(res_msg);
117         res_msg = NULL;
118
119         ttd_cmd_mgr_push_result(
120                 ttd_cmd_get_id(c_data), TTD_CMD_RESULT_SUCCESS, result_json);
121
122         g_free(result_json);
123         g_remove(zip_file);
124         g_free(zip_file);
125
126         return ret;
127 }
128
129 ttd_cmd_launch_func ttd_cmd_get_launch_func(ttd_cmd_type_e type)
130 {
131         ttd_cmd_launch_func func = NULL;
132
133         switch (type) {
134         case TTD_CMD_TYPE_POWER:
135                 break;
136         case TTD_CMD_TYPE_CONFIG:
137                 func = __config_write;
138                 break;
139         case TTD_CMD_TYPE_PACKAGE:
140         case TTD_CMD_TYPE_INFO:
141                 func = __worker_launch;
142                 break;
143         case TTD_CMD_TYPE_DIAGNOSIS:
144                 func = __diagnosis_run;
145                 break;
146         case TTD_CMD_TYPE_LOCAL:
147                 break;
148         case TTD_CMD_TYPE_UNKNOWN:
149         case TTD_CMD_TYPE_MAX:
150         default:
151                 break;
152         }
153
154         return func;
155 }