2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
8 * http://floralicense.org/license/
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.
19 #include "ttd-cmd-mgr.h"
21 #include "ttd-cmd-type.h"
22 #include "ttd-cmd-func.h"
24 #include "ttd-parse-cmd.h"
25 #include "ttd-build-json.h"
26 #include "common-util.h"
28 #define CMD_MGR_GET_INTERVAL_SEC (3600 * 3)
29 #define RESULT_WAIT_IN_SEC (10)
30 #define RESULT_WAIT_TIME (RESULT_WAIT_IN_SEC * 1000000)
31 #define RESULT_WAIT_TRY_MAX (10)
33 // #define CMD_FETCH_TEST
35 typedef struct _ttd_result_data_s {
37 ttd_cmd_result_e result;
42 GAsyncQueue *cmd_id_queue;
44 GAsyncQueue *result_queue;
45 const char *current_cmd_id;
48 int get_thread_running;
52 GThread *launch_thread;
55 /* Just a pointer value to make unblocking async queue and exiting thread,
56 * it could be any address as long, as it isn't a valid queue item
58 static const gpointer quit_marker = (gpointer) &ttd_cmd_mgr_init;
61 static struct __cmd_mgr_h *g_handle = NULL;
63 static void __cmd_id_item_free(gpointer data)
68 if (data == quit_marker)
74 static void __result_item_free(gpointer data)
76 ttd_result_data_s *item = data;
81 if (data == quit_marker)
85 _D("free result item[%s]", item->cmd_id);
92 int ttd_cmd_mgr_push_result(const char *id,
93 ttd_cmd_result_e result, const char *data)
95 ttd_result_data_s *result_item = NULL;
96 const char *curr_id = NULL;
98 retvm_if(!g_handle, -1, "cmd mgr is not initialized yet");
99 retvm_if(!id, -1, "cmd id is NULL");
101 g_mutex_lock(&g_handle->mutex);
102 curr_id = g_handle->current_cmd_id;
103 g_mutex_unlock(&g_handle->mutex);
105 retvm_if(!curr_id, -1, "current cmd id is NULL");
107 if (0 != g_strcmp0(id, curr_id)) {
108 _E("invaild cmd id[%s] - current cmd id[%s]", id, curr_id);
112 result_item = g_try_new0(ttd_result_data_s, 1);
114 result_item->cmd_id = g_strdup(id);
115 result_item->result = result;
117 result_item->data = g_strdup(data);
119 g_async_queue_push(g_handle->result_queue, result_item);
124 int ttd_cmd_mgr_get_cmd(void)
126 retvm_if(!g_handle, -1, "cmd mgr is not initialized yet");
127 _D("unblocking get_thread");
128 g_mutex_lock(&g_handle->get_mutex);
129 g_handle->get_by_request = 1;
130 g_cond_broadcast(&g_handle->get_cond);
131 g_mutex_unlock(&g_handle->get_mutex);
132 // wait here to get result of getting cmd or not ???
137 static int _get_cloud_cmd(struct __cmd_mgr_h *handle, long *res_code)
142 GList *cmd_list = NULL;
145 retv_if(!handle, -1);
147 ret = ttd_http_command_get(&cmd, &r_code);
149 _E("failed to get cmd [%ld]", r_code);
158 retvm_if(!cmd, 0, "there is no new cmd now");
160 ret = ttd_parse_json_to_cmd(cmd, &cmd_list);
162 _E("failed to parse cmd");
169 for (l = cmd_list; l != NULL; l = l->next) {
170 ttd_cmd_data *cmd_data = NULL;
171 ttd_cmd_data *item = NULL;
172 const char *cmd_id = NULL;
174 cmd_data = (ttd_cmd_data *)l->data;
175 continue_if(!cmd_data);
177 cmd_id = ttd_cmd_get_id(cmd_data);
179 g_mutex_lock(&handle->mutex);
180 item = g_hash_table_lookup(handle->cmd_hash, cmd_id);
182 _D("cmd[%s] is pushed in cmd queue", cmd_id);
183 g_async_queue_push(handle->cmd_id_queue, g_strdup(cmd_id));
184 g_hash_table_insert(handle->cmd_hash, g_strdup(cmd_id), cmd_data);
186 _D("cmd[%s] is already in cmd queue", cmd_id);
187 ttd_cmd_free(cmd_data);
189 g_mutex_unlock(&handle->mutex);
191 g_list_free(cmd_list);
196 static gpointer _get_thread(gpointer data)
198 struct __cmd_mgr_h *handle = data;
200 g_mutex_lock(&handle->get_mutex);
204 unsigned int retry = 0;
206 until = common_get_monotonic_coarse_time() +
207 CMD_MGR_GET_INTERVAL_SEC * G_TIME_SPAN_SECOND;
208 _D("thread blocked for 3 hours");
209 g_cond_wait_until(&handle->get_cond, &handle->get_mutex, until);
210 _D("thread unblocked");
212 if (!handle->get_thread_running)
215 if (handle->get_by_request)
223 ret = _get_cloud_cmd(handle, &res_code);
225 _E("failed to get cmd - %ld", res_code);
227 _D("res_code: %ld", res_code);
229 if (res_code == TTD_HTTP_OK) // HTTP OK - success to get cmd
234 g_usleep(G_USEC_PER_SEC); // sleep in 1 second before retry
237 handle->get_by_request = 0;
239 g_mutex_unlock(&handle->get_mutex);
240 _D("thread terminated");
244 static gpointer _launch_thread(gpointer data)
246 struct __cmd_mgr_h *handle = data;
250 ttd_cmd_data *cmd_data = NULL;
254 ttd_cmd_launch_func launch_func = NULL;
255 ttd_cmd_type_e cmd_type = TTD_CMD_TYPE_UNKNOWN;
258 // get pop oldest cmd from cmd_id_queue
259 _D("block launch thread");
260 cmd_id = g_async_queue_pop(handle->cmd_id_queue);
262 _D("unblock launch thread");
263 _D("cmd queue is empty");
266 _D("unblock launch thread");
268 if (cmd_id == quit_marker)
271 // get cmd data from cmd_hash
272 g_mutex_lock(&handle->mutex);
273 cmd_data = g_hash_table_lookup(handle->cmd_hash, cmd_id);
274 g_mutex_unlock(&handle->mutex);
276 _E("data for cmd[%s] is not exist", cmd_id);
280 // report 'running' state of the cmd id to cloud
281 cmd_type = ttd_cmd_get_type(cmd_data);
282 report = ttd_build_json_create_report(cmd_id, cmd_type,
283 TTD_CMD_STATE_RUNNING, 0, "state update", NULL);
284 ret = ttd_http_report_post(report, &r_code);
285 if (r_code != TTD_HTTP_OK) {
286 _E("failed to post report [%ld]", r_code);
292 ttd_cmd_set_state(cmd_data, TTD_CMD_STATE_RUNNING);
295 launch_func = ttd_cmd_get_launch_func(cmd_type);
297 _E("cmd[%s] no proper launch function", cmd_id);
298 report = ttd_build_json_create_report(cmd_id, cmd_type,
299 TTD_CMD_STATE_FAILED, 0, "no proper launch function", NULL);
300 ttd_http_report_post(report, NULL);
306 g_mutex_lock(&handle->mutex);
307 g_handle->current_cmd_id = cmd_id;
308 g_mutex_unlock(&handle->mutex);
309 ret = launch_func(cmd_data);
311 _E("cmd[%s] launch failed", cmd_id);
312 report = ttd_build_json_create_report(cmd_id, cmd_type,
313 TTD_CMD_STATE_FAILED, 0, "command launch failed", NULL);
314 ttd_http_report_post(report, NULL);
320 while (result_wait) {
321 ttd_result_data_s *result_item = NULL;
322 static unsigned int count = 0;
324 result_item = g_async_queue_timeout_pop(
325 g_handle->result_queue, RESULT_WAIT_TIME); /* 10 sec */
330 if (count <= RESULT_WAIT_TRY_MAX) /* 10 times */
333 /* Wait 100 sec to receive a result,
334 * IS IT(100 sec) "ENOUGH" to install pkgs or task monitoring ???
336 /* timeout to wait result, report fail */
337 report = ttd_build_json_create_report(cmd_id, cmd_type,
338 TTD_CMD_STATE_FAILED, 0, "timeout to wait result ", NULL);
339 ttd_http_report_post(report, NULL);
347 if (result_item == quit_marker)
350 switch (result_item->result) {
351 case TTD_CMD_RESULT_RUNNING:
352 /* report running and wait more result */
353 report = ttd_build_json_create_report(cmd_id, cmd_type,
354 TTD_CMD_STATE_RUNNING, 0,
355 "report in progress", result_item->data);
356 ttd_http_report_post(report, NULL);
358 case TTD_CMD_RESULT_SUCCESS:
360 report = ttd_build_json_create_report(cmd_id, cmd_type,
361 TTD_CMD_STATE_DONE, 0,
362 "done", result_item->data);
363 ttd_http_report_post(report, NULL);
366 case TTD_CMD_RESULT_FAIL:
368 report = ttd_build_json_create_report(cmd_id, cmd_type,
369 TTD_CMD_STATE_FAILED, 0,
370 "failed to process command", result_item->data);
371 ttd_http_report_post(report, NULL);
374 /* unhandled states */
380 __result_item_free(result_item);
385 g_mutex_lock(&handle->mutex);
386 g_handle->current_cmd_id = NULL;
387 g_hash_table_remove(handle->cmd_hash, cmd_id);
388 g_mutex_unlock(&handle->mutex);
394 _D("get quit_marker");
397 _D("thread terminated");
401 static void __free_cmd_mgr_handle(void)
406 if (g_handle->get_thread) {
407 g_mutex_lock(&g_handle->get_mutex);
408 g_handle->get_thread_running = 0;
409 g_cond_broadcast(&g_handle->get_cond);
410 g_mutex_unlock(&g_handle->get_mutex);
412 g_thread_join(g_handle->get_thread);
414 g_mutex_clear(&g_handle->get_mutex);
415 g_cond_clear(&g_handle->get_cond);
417 if (g_handle->launch_thread) {
418 g_async_queue_push(g_handle->cmd_id_queue, quit_marker);
419 g_async_queue_push(g_handle->result_queue, quit_marker);
421 g_thread_join(g_handle->launch_thread);
424 g_mutex_clear(&g_handle->mutex);
426 if (g_handle->result_queue)
427 g_async_queue_unref(g_handle->result_queue);
429 if (g_handle->cmd_id_queue)
430 g_async_queue_unref(g_handle->cmd_id_queue);
432 if (g_handle->cmd_hash) {
433 g_hash_table_remove_all(g_handle->cmd_hash);
434 g_hash_table_unref(g_handle->cmd_hash);
441 #ifdef CMD_FETCH_TEST
442 static gboolean __get_cmd_first_time(gpointer data)
444 ttd_cmd_mgr_get_cmd();
446 return G_SOURCE_REMOVE;
448 #endif /* CMD_FETCH_TEST */
450 int ttd_cmd_mgr_init(void)
452 GError *error = NULL;
454 retvm_if(g_handle, -1, "cmd mgr already initialized, finalized it first");
456 g_handle = g_try_malloc0(sizeof(struct __cmd_mgr_h));
457 retvm_if(!g_handle, -1, "failed to malloc");
459 g_handle->cmd_id_queue = g_async_queue_new_full(__cmd_id_item_free);
460 goto_if(!g_handle->cmd_id_queue, ERROR_N_EXIT);
462 g_handle->cmd_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
463 g_free, (GDestroyNotify)ttd_cmd_free);
464 goto_if(!g_handle->cmd_hash, ERROR_N_EXIT);
466 g_handle->result_queue = g_async_queue_new_full(__result_item_free);
467 goto_if(!g_handle->result_queue, ERROR_N_EXIT);
469 g_mutex_init(&g_handle->mutex);
471 g_mutex_init(&g_handle->get_mutex);
472 g_cond_init(&g_handle->get_cond);
473 g_handle->get_thread_running = 1;
475 g_handle->get_thread =
476 g_thread_try_new(NULL, (GThreadFunc)_get_thread, g_handle, &error);
477 if (!g_handle->get_thread) {
478 _E("failed to create get thread - %s",
479 error && error->message ? error->message : "Unknown Error");
483 g_handle->launch_thread =
484 g_thread_try_new(NULL, (GThreadFunc)_launch_thread, g_handle, &error);
485 if (!g_handle->launch_thread) {
486 _E("failed to create launch_thread - %s",
487 error && error->message ? error->message : "Unknown Error");
491 #ifdef CMD_FETCH_TEST
492 g_idle_add(__get_cmd_first_time, NULL);
493 #endif /* CMD_FETCH_TEST */
500 __free_cmd_mgr_handle();
504 int ttd_cmd_mgr_fini(void)
506 __free_cmd_mgr_handle();