#include "ttd-log.h"
#include "ttd-cloud-conn-state.h"
-#include "ttd-cmd.h"
-#include "ttd-queue.h"
#include "ttd-conn-mgr.h"
-#include "ttd-task.h"
#include "ttd-config.h"
-#include "ttd-http.h"
-#include "ttd-parse-cmd.h"
#include "ttd-worker-interface.h"
#include "ttd-worker-handle.h"
#include "common-util.h"
-#ifndef SERVER_URL
-/* TODO : remove it after test */
-#define TEST_SERVER_URL "http://test.showiot.xyz/api/cmd?&target=test-page-device&owner=test-page&state=created"
-#define SERVER_URL TEST_SERVER_URL
-#endif
-
#define DEFAULT_DURATION 600
#define CONNECTION_CHECK_INTERVAL 10
typedef struct __ttd_data {
GMainLoop *mainloop;
- GThreadPool *thread_pool;
worker_interface_h worker_itf_h;
bool is_time_set;
guint timer;
int disconnected_limit_count;
} ttd_data;
-static gboolean __daemon_job_handler(gpointer data);
-static gboolean __cloud_try_connect(gpointer data);
-
static long long _get_monotonic_time(void)
{
long long m_time = 0;
return m_time;
}
-static const char *__ttd_get_cloud_url(void)
-{
- const char *url = NULL;
-
- /* TODO : get cloud url */
- url = SERVER_URL;
-
- return url;
-}
-
static gboolean _handle_sigint(gpointer data)
{
ttd_data *d_data = data;
return FALSE;
}
-static void __main_thread_pool_func(gpointer thread_data, gpointer pool_data)
-{
- ttd_task *task = thread_data;
- int ret = 0;
-
- retm_if(!task, "task is NULL");
-
- ret = ttd_task_run(task);
- if (ret)
- _E("Failed to ttd_task_func_run()");
-
- ttd_task_free(task);
-}
-
-static ttd_task_func __get_task_func(ttd_cmd_data *cmd)
-{
- ttd_task_func func = NULL;
- retv_if(!cmd, NULL);
-
- switch (ttd_cmd_get_type(cmd)) {
- case TTD_CMD_TYPE_POWER:
- break;
- case TTD_CMD_TYPE_CONFIG:
- func = ttd_config_write;
- break;
- case TTD_CMD_TYPE_PACKAGE:
- case TTD_CMD_TYPE_INFO:
- func = ttd_worker_launch;
- break;
- case TTD_CMD_TYPE_DIAGNOSIS:
- break;
- case TTD_CMD_TYPE_LOCAL:
- break;
- case TTD_CMD_TYPE_UNKNOWN:
- case TTD_CMD_TYPE_MAX:
- default:
- break;
- }
-
- return func;
-}
-
-static gboolean __daemon_job_handler(gpointer data)
-{
- ttd_data *d_data = data;
-
- ttd_cmd_data *cmd_data = NULL;
- ttd_result_data_s *res_data = NULL;
-
- if (!data) {
- /* Must not reach here */
- _E("data is NULL, daemon will be stop");
- abort();
- return FALSE;
- }
-
- res_data = ttd_queue_timeout_pop(TTD_QUEUE_TYPE_RESULT, 500);
- if (res_data) {
- /* Do something, handles result...jfkdjlfjklajflkdsjfkdslajfkdlsjlk */
- /* pushes new task? or creates new command? */
- }
-
- cmd_data = ttd_queue_timeout_pop(TTD_QUEUE_TYPE_CMD, 500);
- if (cmd_data) {
- ttd_task *new_task = NULL;
- ttd_task_func func = NULL;
- /* Do something, creates task... */
- func = __get_task_func(cmd_data);
- if (!func) {
- _E("task func is not defined - type[%d]",
- ttd_cmd_get_type(cmd_data));
-
- ttd_cmd_free(cmd_data);
- return TRUE;
- }
-
- new_task = ttd_task_new(TTD_TASK_PRIORITY_NORMAL, func,
- cmd_data, (ttd_task_data_free_func)ttd_cmd_free);
-
- g_thread_pool_push(d_data->thread_pool, new_task, NULL);
- }
- return TRUE;
-}
-
-static int __say_hello_to_cloud(void *data)
-{
- int ret = 0;
- char *cmd = NULL;
- long res_code = 0;
-
- ret = ttd_http_get_cloud_cmd(__ttd_get_cloud_url(), &cmd, &res_code);
- retvm_if(ret, -1, "failed to get cmd [%ld]", res_code);
-
- if (cmd) {
- GList *cmd_list = NULL;
- GList *l = NULL;
-
- ttd_parse_json_to_cmd(cmd, &cmd_list);
- for (l = cmd_list; l != NULL; l = l->next) {
- ttd_cmd_data *cmd_data = NULL;
-
- cmd_data = (ttd_cmd_data *)l->data;
- if (cmd_data)
- ttd_queue_push(TTD_QUEUE_TYPE_CMD,
- cmd_data, (ttd_queue_item_free_func)ttd_cmd_free);
- }
- } else
- _D("there is no cmd now");
-
- return 0;
-}
-
-/*********** FIXME *************/
-static gboolean __cloud_try_connect(gpointer data)
-{
- int ret = 0;
- ttd_data *d_data = data;
-
- if (!data) {
- /* Must not reach here */
- _E("data is NULL, daemon will be stop");
- abort();
- }
- ret = __say_hello_to_cloud(data);
-
- if (!ret) {
- ttd_cloud_conn_state_set(TTD_CLOUD_CONN_STATE_CONNECTED);
- g_idle_add(__daemon_job_handler, d_data);
- } else /* endless retry? */
- g_timeout_add_seconds(3, __cloud_try_connect, data);
-
- return FALSE;
-}
-
static gboolean __do_device_register(gpointer data)
{
int registered = 0;
/* TODO : start easy setup procedure here !! */
}
- ttd_cloud_conn_state_set(TTD_CLOUD_CONN_STATE_CONNECTABLE);
- __cloud_try_connect(data); /* ???? */
+ /* TODO : DO somethings!! */
return FALSE;
}
vconf_notify_key_changed(VCONFKEY_SYSTEM_TIME_CHANGED, __system_time_changed, d_data);
- ttd_queue_init();
ttd_conn_mgr_init();
ttd_http_init();
ttd_cloud_conn_state_set(TTD_CLOUD_CONN_STATE_DISCONNECTED);
if (state == TTD_CONN_STATE_CONNECTED)
__set_changed_time(d_data);
- d_data->thread_pool =
- g_thread_pool_new(__main_thread_pool_func, d_data, -1, FALSE, NULL);
- g_thread_pool_set_sort_function(
- d_data->thread_pool, ttd_task_sort_func, NULL);
-
_D("[delay] start-to-ready - %.3lf(ms)",
(double)(_get_monotonic_time() - start)/1000);
g_main_loop_run(d_data->mainloop);
g_main_loop_unref(d_data->mainloop);
- g_thread_pool_free(d_data->thread_pool, FALSE, TRUE);
ttd_worker_interface_fini(d_data->worker_itf_h);
if (d_data->timer)
free(d_data);
d_data = NULL;
- ttd_queue_fini();
ttd_conn_mgr_fini();
ttd_http_fini();