Change the text of the command for getting log archive and change the pointer type...
[apps/native/iot-device-manager.git] / src / devicemanagerservice.c
1  /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 <tizen.h>
18 #include <service_app.h>
19 #include <Ecore.h>
20 #include <glib.h>
21
22 #include "iot-device-manage.h"
23 #include "iot-device-manage-type.h"
24 #include "log.h"
25
26 typedef struct _app_data {
27         Ecore_Timer *timer;
28         idm_command_h command;
29 } app_data;
30
31 static void __get_taskinfo(int duration, int period, unsigned long id, idm_result_cb result_cb_func, void *data);
32
33 static char *__make_taskinfo_request(int duration, int period)
34 {
35         char *request_json = NULL;
36
37         request_json = g_strdup_printf("{ \"type\" : %d, \"duration\" : %d, \"period\" : %d }", 1, duration, period);
38
39         return request_json;
40 }
41
42 static char *__make_log_archive_request(char *log_path_arr[], const char *archive_file)
43 {
44         char *request_json = NULL;
45         char *log_path_str = NULL;
46
47         log_path_str = g_strjoinv("\", \"", log_path_arr);
48
49         request_json = g_strdup_printf("{ \"logPath\" : [ \"%s\" ], \"archiveFile\" : \"%s\" }", log_path_str, archive_file);
50
51         return request_json;
52 }
53
54 // Make temporary log directories and files using app_get_data_path() API
55 static char *__make_temp_logs(const char *file_name)
56 {
57         char *data_path = NULL;
58         char *file_path = NULL;
59         FILE *fp;
60
61         data_path = app_get_data_path();
62
63         if (!data_path)
64                 return NULL;
65
66         file_path = g_strdup_printf("%s/%s", data_path, file_name);
67         free(data_path);
68
69         fp = fopen(file_path, "w");
70
71         if (!fp) {
72                 free(file_path);
73                 return NULL;
74         }
75
76         fputs("test", fp);
77         fclose(fp);
78
79         return file_path;
80 }
81
82 static void __reboot_result_callback(idm_command_h command, idm_result_e result, idm_reason_e reason, const char *detail, void *data)
83 {
84         app_data *ad = data;
85
86         _D("result : %d, reason : %d, detail : %s", result, reason, detail ? detail : "None");
87
88         if (result != IDM_RESULT_RUNNING) {
89                 idm_command_free(command);
90                 ad->command = NULL;
91         }
92 }
93
94 static void __reboot(void *data)
95 {
96         app_data *ad = data;
97         idm_command_h command = NULL;
98         int ret = -1;
99
100         ret = idm_command_new(&command);
101         if (ret != 0) {
102                 _E("failed to idm_command_new");
103                 return;
104         }
105         ad->command = command;
106
107         ret = idm_command_set_power_cmd(command, IDM_CMD_POWER_REBOOT, NULL);
108         if (ret != 0) {
109                 _E("failed to idm_command_set_power_cmd");
110                 idm_command_free(command);
111                 ad->command = NULL;
112                 return;
113         }
114
115         ret = idm_command_run(command, __reboot_result_callback, ad);
116         if (ret != 0) {
117                 _E("failed to idm_command_run");
118                 idm_command_free(command);
119                 ad->command = NULL;
120                 return;
121         }
122 }
123
124 static void __get_log_archive_result_cb(idm_command_h command, idm_result_e result, idm_reason_e reason, const char *detail, void *data)
125 {
126         unsigned long id;
127         idm_command_get_id(command, &id);
128
129         _D("[%d] result : %d, reason : %d, detail : %s", id, result, reason, detail ? detail : "None");
130
131         if (result != IDM_RESULT_RUNNING) {
132                 app_data *ad = data;
133                 idm_command_free(command);
134                 ad->command = NULL;
135         }
136 }
137
138 static void __get_log_archive(char *log_path_arr[], const char *archive_file, unsigned long id, idm_result_cb result_cb_func, void *data)
139 {
140         app_data *ad = data;
141         idm_command_h command = NULL;
142         char *request = NULL;
143         int ret = -1;
144
145         idm_command_new(&command);
146         ret_if(!command);
147
148         idm_command_set_id(command, id);
149         ad->command = command;
150
151         request = __make_log_archive_request(log_path_arr, archive_file);
152         if (!request) {
153                 _E("failed to make request");
154                 idm_command_free(command);
155                 ad->command = NULL;
156                 return;
157         }
158
159         ret = idm_command_set_diagnosis_cmd(command, IDM_CMD_DIAGNOSIS_GET_LOG_ARCHIVE, request);
160
161         g_free(request);
162         request = NULL;
163
164         if (ret != 0) {
165                 _E("failed to idm_command_set_info_cmd");
166                 idm_command_free(command);
167                 ad->command = NULL;
168                 return;
169         }
170
171         ret = idm_command_run(command, result_cb_func, ad);
172
173         if (ret != 0) {
174                 _E("failed to idm_command_run");
175                 idm_command_free(command);
176                 ad->command = NULL;
177                 return;
178         }
179 }
180
181 static Eina_Bool __command_stop(void *data)
182 {
183         app_data *ad = data;
184         char *data_path = app_get_data_path();
185         char *log_path_arr[3] = {NULL,}; // The last item of this array must be NULL
186         char *archive_file = NULL;
187
188         if (ad->command) {
189                 idm_command_stop(ad->command);
190                 idm_command_free(ad->command);
191                 ad->command = NULL;
192         }
193         ad->timer = NULL;
194
195         log_path_arr[0] = __make_temp_logs("test1.log");
196         log_path_arr[1] = __make_temp_logs("test2.log");
197
198         archive_file = g_strdup_printf("%s/%s", data_path, "log_archive.zip");
199
200         // Get log archive
201         __get_log_archive(log_path_arr, archive_file, 3, __get_log_archive_result_cb, ad);
202
203         g_free(log_path_arr[0]);
204         g_free(log_path_arr[1]);
205         g_free(archive_file);
206
207         // 3. reboot cmd 실행
208         __reboot(ad);
209
210         return ECORE_CALLBACK_CANCEL;
211 }
212
213 static void __get_tinfo_result_cb(idm_command_h command, idm_result_e result, idm_reason_e reason, const char *detail, void *data)
214 {
215         unsigned long id;
216         idm_command_get_id(command, &id);
217
218         _D("[%d] result : %d, reason : %d, detail : %s", id, result, reason, detail ? detail : "None");
219
220         if (result != IDM_RESULT_RUNNING) {
221                 app_data *ad = data;
222                 idm_command_free(command);
223                 ad->command = NULL;
224
225                 if (id == 1) {
226                         // 2. 2초 간격으로 1분간 task info 받도록 cmd 실행하고, 30초 뒤에 cmd stop 호출
227                         __get_taskinfo(60, 2, 2, __get_tinfo_result_cb, ad);
228
229                         if (ad->timer)
230                                 ecore_timer_del(ad->timer);
231                         ad->timer = ecore_timer_add(30.0, __command_stop, ad);
232                 }
233         }
234 }
235
236 static void __get_taskinfo(int duration, int period, unsigned long id, idm_result_cb result_cb_func, void *data)
237 {
238         app_data *ad = data;
239         idm_command_h command = NULL;
240         char *request = NULL;
241         int ret = -1;
242
243         idm_command_new(&command);
244         ret_if(!command);
245
246         idm_command_set_id(command, id);
247         ad->command = command;
248
249         request = __make_taskinfo_request(duration, period);
250         if (!request) {
251                 _E("failed to make request");
252                 idm_command_free(command);
253                 ad->command = NULL;
254                 return;
255         }
256
257         ret = idm_command_set_info_cmd(command, IDM_CMD_INFO_GET_TASKINFO, request);
258
259         g_free(request);
260         request = NULL;
261
262         if (ret != 0) {
263                 _E("failed to idm_command_set_info_cmd");
264                 idm_command_free(command);
265                 ad->command = NULL;
266                 return;
267         }
268
269         ret = idm_command_run(command, result_cb_func, ad);
270         if (ret != 0) {
271                 _E("failed to idm_command_run");
272                 idm_command_free(command);
273                 ad->command = NULL;
274                 return;
275         }
276 }
277
278 static bool service_app_create(void *data)
279 {
280         _D("app create");
281         return true;
282 }
283
284 static void service_app_terminate(void *data)
285 {
286         _D("app terminate");
287         app_data *ad = data;
288
289         if (ad->command) {
290                 idm_command_stop(ad->command);
291                 idm_command_free(ad->command);
292                 ad->command = NULL;
293         }
294
295         if (ad->timer) {
296                 ecore_timer_del(ad->timer);
297                 ad->timer = NULL;
298         }
299
300         return;
301 }
302
303 static void service_app_control(app_control_h app_control, void *data)
304 {
305         _D("app control");
306         app_data *ad = data;
307
308         if (ad->command) {
309                 idm_command_stop(ad->command);
310                 idm_command_free(ad->command);
311                 ad->command = NULL;
312         }
313
314         if (ad->timer) {
315                 ecore_timer_del(ad->timer);
316                 ad->timer = NULL;
317         }
318
319         // 1. 1초 간격으로 30초간 task info 받도록 cmd 실행
320         __get_taskinfo(30, 1, 1, __get_tinfo_result_cb, ad);
321
322         return;
323 }
324
325 int main(int argc, char* argv[])
326 {
327         app_data ad;
328         ad.timer = NULL;
329         ad.command = NULL;
330         service_app_lifecycle_callback_s event_callback;
331
332         event_callback.create = service_app_create;
333         event_callback.terminate = service_app_terminate;
334         event_callback.app_control = service_app_control;
335
336         return service_app_main(argc, argv, &event_callback, &ad);
337 }