2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <service_app.h>
22 #include "iot-device-manage.h"
23 #include "iot-device-manage-type.h"
26 typedef struct _app_data {
28 idm_command_h command;
31 static void __get_taskinfo(int duration, int period, unsigned long id, idm_result_cb result_cb_func, void *data);
33 static char *__make_taskinfo_request(int duration, int period)
35 char *request_json = NULL;
37 request_json = g_strdup_printf("{ \"type\" : %d, \"duration\" : %d, \"period\" : %d }", 1, duration, period);
42 static char *__make_log_archive_request(char *log_path_arr[], const char *archive_file)
44 char *request_json = NULL;
45 char *log_path_str = NULL;
47 log_path_str = g_strjoinv("\", \"", log_path_arr);
49 request_json = g_strdup_printf("{ \"logPath\" : [ \"%s\" ], \"archiveFile\" : \"%s\" }", log_path_str, archive_file);
54 // Make temporary log directories and files using app_get_data_path() API
55 static char *__make_temp_logs(const char *file_name)
57 char *data_path = NULL;
58 char *file_path = NULL;
61 data_path = app_get_data_path();
66 file_path = g_strdup_printf("%s/%s", data_path, file_name);
69 fp = fopen(file_path, "w");
82 static void __reboot_result_callback(idm_command_h command, idm_result_e result, idm_reason_e reason, const char *detail, void *data)
86 _D("result : %d, reason : %d, detail : %s", result, reason, detail ? detail : "None");
88 if (result != IDM_RESULT_RUNNING) {
89 idm_command_free(command);
94 static void __reboot(void *data)
97 idm_command_h command = NULL;
100 ret = idm_command_new(&command);
102 _E("failed to idm_command_new");
105 ad->command = command;
107 ret = idm_command_set_power_cmd(command, IDM_CMD_POWER_REBOOT, NULL);
109 _E("failed to idm_command_set_power_cmd");
110 idm_command_free(command);
115 ret = idm_command_run(command, __reboot_result_callback, ad);
117 _E("failed to idm_command_run");
118 idm_command_free(command);
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)
127 idm_command_get_id(command, &id);
129 _D("[%d] result : %d, reason : %d, detail : %s", id, result, reason, detail ? detail : "None");
131 if (result != IDM_RESULT_RUNNING) {
133 idm_command_free(command);
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)
141 idm_command_h command = NULL;
142 char *request = NULL;
145 idm_command_new(&command);
148 idm_command_set_id(command, id);
149 ad->command = command;
151 request = __make_log_archive_request(log_path_arr, archive_file);
153 _E("failed to make request");
154 idm_command_free(command);
159 ret = idm_command_set_diagnosis_cmd(command, IDM_CMD_DIAGNOSIS_GET_LOG_ARCHIVE, request);
165 _E("failed to idm_command_set_info_cmd");
166 idm_command_free(command);
171 ret = idm_command_run(command, result_cb_func, ad);
174 _E("failed to idm_command_run");
175 idm_command_free(command);
181 static Eina_Bool __command_stop(void *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;
189 idm_command_stop(ad->command);
190 idm_command_free(ad->command);
195 log_path_arr[0] = __make_temp_logs("test1.log");
196 log_path_arr[1] = __make_temp_logs("test2.log");
198 archive_file = g_strdup_printf("%s/%s", data_path, "log_archive.zip");
201 __get_log_archive(log_path_arr, archive_file, 3, __get_log_archive_result_cb, ad);
203 g_free(log_path_arr[0]);
204 g_free(log_path_arr[1]);
205 g_free(archive_file);
210 return ECORE_CALLBACK_CANCEL;
213 static void __get_tinfo_result_cb(idm_command_h command, idm_result_e result, idm_reason_e reason, const char *detail, void *data)
216 idm_command_get_id(command, &id);
218 _D("[%d] result : %d, reason : %d, detail : %s", id, result, reason, detail ? detail : "None");
220 if (result != IDM_RESULT_RUNNING) {
222 idm_command_free(command);
226 // 2. 2초 간격으로 1분간 task info 받도록 cmd 실행하고, 30초 뒤에 cmd stop 호출
227 __get_taskinfo(60, 2, 2, __get_tinfo_result_cb, ad);
230 ecore_timer_del(ad->timer);
231 ad->timer = ecore_timer_add(30.0, __command_stop, ad);
236 static void __get_taskinfo(int duration, int period, unsigned long id, idm_result_cb result_cb_func, void *data)
239 idm_command_h command = NULL;
240 char *request = NULL;
243 idm_command_new(&command);
246 idm_command_set_id(command, id);
247 ad->command = command;
249 request = __make_taskinfo_request(duration, period);
251 _E("failed to make request");
252 idm_command_free(command);
257 ret = idm_command_set_info_cmd(command, IDM_CMD_INFO_GET_TASKINFO, request);
263 _E("failed to idm_command_set_info_cmd");
264 idm_command_free(command);
269 ret = idm_command_run(command, result_cb_func, ad);
271 _E("failed to idm_command_run");
272 idm_command_free(command);
278 static bool service_app_create(void *data)
284 static void service_app_terminate(void *data)
290 idm_command_stop(ad->command);
291 idm_command_free(ad->command);
296 ecore_timer_del(ad->timer);
303 static void service_app_control(app_control_h app_control, void *data)
309 idm_command_stop(ad->command);
310 idm_command_free(ad->command);
315 ecore_timer_del(ad->timer);
319 // 1. 1초 간격으로 30초간 task info 받도록 cmd 실행
320 __get_taskinfo(30, 1, 1, __get_tinfo_result_cb, ad);
325 int main(int argc, char* argv[])
330 service_app_lifecycle_callback_s event_callback;
332 event_callback.create = service_app_create;
333 event_callback.terminate = service_app_terminate;
334 event_callback.app_control = service_app_control;
336 return service_app_main(argc, argv, &event_callback, &ad);