Merge "add command function to upload log"
authorJeonghoon Park <jh1979.park@samsung.com>
Fri, 29 Jun 2018 01:42:01 +0000 (01:42 +0000)
committerGerrit Code Review <gerrit@review.ap-northeast-2.compute.internal>
Fri, 29 Jun 2018 01:42:01 +0000 (01:42 +0000)
daemon/include/ttd-app-data.h
daemon/src/ttd-app-data.c
daemon/src/ttd-app-interface.c
daemon/src/ttd-cmd-mgr.c
lib/things-service/include/things-service.h
lib/things-service/src/things-service.c

index 9b9218b..ab80421 100644 (file)
@@ -21,7 +21,11 @@ typedef struct __ttd_app_data ttd_app_data;
 
 ttd_app_data *ttd_app_data_new(const char *project, const char *data);
 void ttd_app_data_free(ttd_app_data *app_data);
+int ttd_app_data_set_retry_count(
+       ttd_app_data *app_data, unsigned int count);
 const char *ttd_app_data_get_project_name(ttd_app_data *app_data);
 const char *ttd_app_data_get_data(ttd_app_data *app_data);
+int ttd_app_data_get_retry_count(
+       ttd_app_data *app_data, unsigned int *count);
 
 #endif /* __TT_DAEMON_APP_DATA_H__ */
index b92ead8..28a47b6 100644 (file)
@@ -21,6 +21,7 @@
 struct __ttd_app_data {
        char *p_name;
        char *data;
+       unsigned int retry;
 };
 
 ttd_app_data *ttd_app_data_new(const char *project, const char *data)
@@ -49,6 +50,16 @@ void ttd_app_data_free(ttd_app_data *app_data)
        g_free(app_data);
 }
 
+int ttd_app_data_set_retry_count(
+       ttd_app_data *app_data, unsigned int count)
+{
+       retv_if(!app_data, -1);
+
+       app_data->retry = count;
+
+       return 0;
+}
+
 const char *ttd_app_data_get_project_name(ttd_app_data *app_data)
 {
        retv_if(!app_data, NULL);
@@ -60,3 +71,15 @@ const char *ttd_app_data_get_data(ttd_app_data *app_data)
        retv_if(!app_data, NULL);
        return app_data->data;
 }
+
+int ttd_app_data_get_retry_count(
+       ttd_app_data *app_data, unsigned int *count)
+{
+       retv_if(!app_data, -1);
+       retv_if(!count, -1);
+
+       *count = app_data->retry;
+       app_data->retry--;
+
+       return 0;
+}
index 9257f07..53b3582 100644 (file)
 #include "common-app-inf.h"
 #include "ttd-app-data.h"
 
-#define POST_DATA_URL "http://apitest.showiot.xyz/api/data"
-#define __POST_THREAD_RUN 1
-#define __POST_THREAD_STOP 0
-#define __QUEUE_TIME_OUT 500
-
+#define TTD_APP_DATA_RETRY 2
 #define APP_INF_INTROSPECTION \
 "<node>" \
 "  <interface name='"TTD_APP_INF_BUS_INF"'>" \
@@ -125,7 +121,7 @@ __receive_msg(GIOChannel *ch, GIOCondition cond, gpointer user_data)
                char **strv = NULL;
                int i;
 
-               strv = g_strsplit(msg, "|", -1);
+               strv = g_strsplit(msg, TTD_APP_MSG_DELIMITER, -1);
                for (i = 0; strv[i]; i++) {
                        if (strv[i][0] == '\0') {
                                g_free(strv[i]);
@@ -134,10 +130,12 @@ __receive_msg(GIOChannel *ch, GIOCondition cond, gpointer user_data)
                                ttd_app_data *app_data = NULL;
                                _D("strv[%d] : %s", i, strv[i]);
                                app_data = ttd_app_data_new(data->project, strv[i]);
-                               if (app_data)
+                               if (app_data) {
+                                       ttd_app_data_set_retry_count(app_data, TTD_APP_DATA_RETRY);
                                        g_async_queue_push(data->data_queue, app_data);
-                               else
+                               } else {
                                        _E("failed to creat app_data for %s", strv[i]);
+                               }
                                g_free(strv[i]);
                        }
                }
@@ -591,11 +589,18 @@ static gpointer post_thread(gpointer user_data)
 
                ret = ttd_http_appdata_post(ttd_app_data_get_project_name(app_data),
                        ttd_app_data_get_data(app_data), &res_code);
-               if (!ret) {
-                       _E("failed to post data, retry it - code[%ld]", res_code);
-                       g_async_queue_push_front(handle->data_queue, app_data);
-                       g_usleep((gulong)(G_USEC_PER_SEC/2));
-                       /* 0.5 sec sleep before retry it */
+               if (ret) {
+                       unsigned int retry = 0;
+                       ttd_app_data_get_retry_count(app_data, &retry);
+                       _E("failed to post data - code[%ld], retry[%u]", res_code, retry);
+                       if (retry) {
+                               g_async_queue_push_front(handle->data_queue, app_data);
+                               g_usleep((gulong)(G_USEC_PER_SEC/2));
+                               /* 0.5 sec sleep before retry it */
+                       } else {
+                               _D("drop msg : %s", ttd_app_data_get_data(app_data));
+                               ttd_app_data_free(app_data);
+                       }
                        continue;
                }
 
index 0d231ed..a4c719c 100644 (file)
@@ -226,7 +226,7 @@ static gpointer _get_thread(gpointer data)
                        else
                                _D("res_code: %ld", res_code);
 
-                       if (res_code == 200) // HTTP OK - success to get cmd
+                       if (res_code == TTD_HTTP_OK) // HTTP OK - success to get cmd
                                break;
 
                        retry--;
@@ -282,7 +282,7 @@ static gpointer _launch_thread(gpointer data)
                report = ttd_build_json_create_report(cmd_id, cmd_type,
                        TTD_CMD_STATE_RUNNING, 0, "state update", NULL);
                ret = ttd_http_report_post(report, &r_code);
-               if (r_code != 200) {
+               if (r_code != TTD_HTTP_OK) {
                        _E("failed to post report [%ld]", r_code);
                        g_free(report);
                        goto DONE_N_WAIT;
index 792ec5e..5a5ff11 100644 (file)
@@ -31,8 +31,12 @@ int things_service_init(ts_handle *handle, const char *project);
 int things_service_fini(ts_handle handle);
 
 timebased_data *things_service_timebased_data_new(ts_value_type_e type);
+int things_service_timebased_data_reset(
+       timebased_data *tb_data, ts_value_type_e type);
 void things_service_timebased_data_free(timebased_data *tb_data);
+
 int things_service_timebased_data_get_length(timebased_data *tb_data);
+
 int things_service_timebased_data_append_int(
        timebased_data *tb_data, int ivalue);
 int things_service_timebased_data_append_double(
@@ -40,6 +44,6 @@ int things_service_timebased_data_append_double(
 
 int things_service_send_data(ts_handle handle, const char *json_data);
 int things_service_send_timebased_data(
-       ts_handle handle, timebased_data *tb_data);
+       ts_handle handle, timebased_data *tb_data, const char *project_key);
 
 #endif /* __LIB_THINGS_SERVICE_H__ */
index ab45b7c..517b499 100644 (file)
@@ -27,6 +27,8 @@
 #include "common-app-inf.h"
 #include "common-util.h"
 
+#define TEST_DEVICE_ID "test-page-device"
+
 struct _ts_data {
        char *app_id;
        char *token;
@@ -40,140 +42,15 @@ struct _ts_data {
        int accept_thread_running;
 };
 
-struct __timebased_int {
-       gint64 i_time;
-       gint value;
-};
-
-struct __timebased_double {
-       gint64 i_time;
-       gdouble value;
-};
-
 struct __timebased_data {
        ts_value_type_e type;
        unsigned int length;
-       GList *v_list;
+       struct json_object *arr_obj;
 };
 
 static void __quit_n_join_accept_thread(ts_handle handle);
 static int _create_accept_thread(ts_handle handle);
 
-static void __timebase_value_free(gpointer data)
-{
-       g_free(data);
-}
-
-static void
-__add_timebased_int_to_json_object(gpointer data, gpointer user_data)
-{
-       struct __timebased_int *tb_int = data;
-       struct json_object *a_obj = user_data;
-       struct json_object *obj = NULL;
-       struct json_object *value_o = NULL;
-       struct json_object *time_o = NULL;
-
-       ret_if(!tb_int);
-       ret_if(!a_obj);
-
-       obj = json_object_new_object();
-       ret_if(!obj);
-
-       time_o = json_object_new_int64(tb_int->i_time);
-       goto_if(!time_o, ERROR);
-       json_object_object_add(obj, "x", time_o);
-
-       value_o = json_object_new_int64(tb_int->value);
-       goto_if(!value_o, ERROR);
-       json_object_object_add(obj, "y", value_o);
-
-       json_object_array_add(a_obj, obj);
-       return;
-
-ERROR:
-       json_object_put(obj);
-       return;
-}
-
-static void
-__add_timebased_double_to_json_object(gpointer data, gpointer user_data)
-{
-       struct __timebased_double *tb_double = data;
-       struct json_object *a_obj = user_data;
-       struct json_object *obj = NULL;
-       struct json_object *value_o = NULL;
-       struct json_object *time_o = NULL;
-
-       ret_if(!tb_double);
-       ret_if(!a_obj);
-
-       obj = json_object_new_object();
-       ret_if(!obj);
-
-       time_o = json_object_new_int64(tb_double->i_time);
-       goto_if(!time_o, ERROR);
-       json_object_object_add(obj, "x", time_o);
-
-       value_o = json_object_new_double(tb_double->value);
-       goto_if(!value_o, ERROR);
-       json_object_object_add(obj, "y", value_o);
-
-       json_object_array_add(a_obj, obj);
-
-       return;
-
-ERROR:
-       json_object_put(obj);
-       return;
-}
-
-static char *__timebased_data_to_json(timebased_data *tb_data)
-{
-       struct json_object *obj = NULL;
-       struct json_object *a_obj = NULL;
-       char *json_str = NULL;
-
-       retv_if(!tb_data, NULL);
-       retvm_if(!((tb_data->type == TS_VALUE_TYPE_INT) ||
-                       (tb_data->type == TS_VALUE_TYPE_DOUBLE)),
-                       NULL, "invalid type[%d]", tb_data->type);
-
-       obj = json_object_new_object();
-       retv_if(!obj, NULL);
-
-       a_obj = json_object_new_array();
-       goto_if(!a_obj, ERROR);
-       json_object_object_add(obj, "data", a_obj);
-
-       switch (tb_data->type) {
-       case TS_VALUE_TYPE_INT:
-               g_list_foreach(tb_data->v_list,
-                       __add_timebased_int_to_json_object, a_obj);
-               break;
-       case TS_VALUE_TYPE_DOUBLE:
-               g_list_foreach(tb_data->v_list,
-                       __add_timebased_double_to_json_object, a_obj);
-               break;
-       default:
-               _E("invalid type [%d]", tb_data->type);
-               goto ERROR;
-               break;
-       }
-
-       json_str = g_strdup(json_object_to_json_string(obj));
-
-       if (obj)
-               json_object_put(obj);
-
-       return json_str;
-
-ERROR:
-       if (obj)
-               json_object_put(obj);
-
-       return NULL;
-}
-
 static char *_get_socket_addr_name(const char *appID, const char *token)
 {
        return common_make_socket_addr_name(token, appID, (guint)getpid());
@@ -519,27 +396,46 @@ int things_service_send_data(ts_handle handle, const char *json_data)
 
 timebased_data *things_service_timebased_data_new(ts_value_type_e type)
 {
-       timebased_data *t_data = NULL;
+       timebased_data *tb_data = NULL;
 
        retvm_if(!((type == TS_VALUE_TYPE_INT) || (type == TS_VALUE_TYPE_DOUBLE)),
                NULL, "invalid type[%d]", type);
 
-       t_data = g_try_malloc0(sizeof(timebased_data));
-       if (!t_data)
-               return NULL;
+       tb_data = g_try_malloc(sizeof(timebased_data));
+       retv_if(!tb_data, NULL);
+
+       tb_data->type = type;
+       tb_data->length = 0;
+       tb_data->arr_obj = NULL;
+
+       return tb_data;
+}
+
+int things_service_timebased_data_reset(
+       timebased_data *tb_data, ts_value_type_e type)
+{
+       retv_if(!tb_data, -1);
+
+       retvm_if(!((type == TS_VALUE_TYPE_INT) || (type == TS_VALUE_TYPE_DOUBLE)),
+               -1, "invalid type[%d]", type);
 
-       t_data->type = type;
-       t_data->length = 0;
+       tb_data->type = type;
+       tb_data->length = 0;
 
-       return t_data;
+       if (tb_data->arr_obj) {
+               json_object_put(tb_data->arr_obj);
+               tb_data->arr_obj = NULL;
+       }
+       return 0;
 }
 
+
 void things_service_timebased_data_free(timebased_data *tb_data)
 {
        ret_if(!tb_data);
 
-       if (tb_data->v_list)
-               g_list_free_full(tb_data->v_list, __timebase_value_free);
+       if (tb_data->arr_obj)
+               json_object_put(tb_data->arr_obj);
 
        g_free(tb_data);
 }
@@ -554,61 +450,130 @@ int things_service_timebased_data_get_length(timebased_data *tb_data)
 int
 things_service_timebased_data_append_int(timebased_data *tb_data, int ivalue)
 {
-       struct __timebased_int *tb_int = NULL;
+       struct json_object *tmp_o = NULL;
+       struct json_object *time_o = NULL;
+       struct json_object *value_o = NULL;
 
        retv_if(!tb_data, -1);
        retv_if(tb_data->type != TS_VALUE_TYPE_INT, -1);
 
-       tb_int = g_try_malloc0(sizeof(struct __timebased_int));
-       retv_if(!tb_int, -1);
+       if (!tb_data->arr_obj) {
+               struct json_object *obj = NULL;
+               obj = json_object_new_array();
+               retvm_if(!obj, -1, "failed to create json arry object");
+               tb_data->arr_obj = obj;
+       }
+
+       tmp_o = json_object_new_object();
+       retvm_if (!tmp_o, -1, "failed to create json object");
+
+       time_o = json_object_new_int64(common_get_epoch_coarse_time());
+       goto_if(!time_o, ERROR);
+       value_o = json_object_new_int64(ivalue);
+       goto_if(!value_o, ERROR);
 
-       tb_int->i_time = common_get_epoch_coarse_time();
-       tb_int->value = ivalue;
+       json_object_object_add(tmp_o, "x", time_o);
+       json_object_object_add(tmp_o, "y", value_o);
+       json_object_array_add(tb_data->arr_obj, tmp_o);
 
-       tb_data->v_list = g_list_append(tb_data->v_list, tb_int);
        tb_data->length++;
 
        return 0;
+
+ERROR:
+       if (tmp_o)
+               json_object_put(tmp_o);
+
+       if (time_o)
+               json_object_put(time_o);
+
+       return -1;
 }
 
 int things_service_timebased_data_append_double(
        timebased_data *tb_data, double dvalue)
 {
-       struct __timebased_double *tb_double = NULL;
+       struct json_object *tmp_o = NULL;
+       struct json_object *time_o = NULL;
+       struct json_object *value_o = NULL;
 
        retv_if(!tb_data, -1);
        retv_if(tb_data->type != TS_VALUE_TYPE_DOUBLE, -1);
 
-       tb_double = g_try_malloc0(sizeof(struct __timebased_double));
-       retv_if(!tb_double, -1);
+       if (!tb_data->arr_obj) {
+               struct json_object *obj = NULL;
+               obj = json_object_new_array();
+               retvm_if(!obj, -1, "failed to create json arry object");
+               tb_data->arr_obj = obj;
+       }
+
+       tmp_o = json_object_new_object();
+       retvm_if (!tmp_o, -1, "failed to create json object");
+
+       time_o = json_object_new_int64(common_get_epoch_coarse_time());
+       goto_if(!time_o, ERROR);
+       value_o = json_object_new_double(dvalue);
+       goto_if(!value_o, ERROR);
 
-       tb_double->i_time = common_get_epoch_coarse_time();
-       tb_double->value = dvalue;
+       json_object_object_add(tmp_o, "x", time_o);
+       json_object_object_add(tmp_o, "y", value_o);
+       json_object_array_add(tb_data->arr_obj, tmp_o);
 
-       tb_data->v_list = g_list_append(tb_data->v_list, tb_double);
        tb_data->length++;
 
        return 0;
+
+ERROR:
+       if (tmp_o)
+               json_object_put(tmp_o);
+
+       if (time_o)
+               json_object_put(time_o);
+
+       return -1;
 }
 
-int
-things_service_send_timebased_data(ts_handle handle, timebased_data *tb_data)
+int things_service_send_timebased_data(
+       ts_handle handle, timebased_data *tb_data, const char *project_key)
 {
        int ret = 0;
-       char *json_data = NULL;
+       struct json_object *msg_o = NULL;
+       struct json_object *key_o = NULL;
 
        retv_if(!handle, -1);
        retv_if(!tb_data, -1);
+       retv_if(!tb_data->arr_obj, -1);
+       retv_if(!project_key, -1);
 
-       json_data = __timebased_data_to_json(tb_data);
-       ret  = __ts_send_data(handle, json_data);
-       g_free(json_data);
+       msg_o = json_object_new_object();
+       retvm_if(!msg_o, -1, "failed to create json object for message");
 
-       if (!ret) { /* if success to send data, remove all items in tb_data */
-               g_list_free_full(tb_data->v_list, __timebase_value_free);
-               tb_data->v_list = NULL;
+       key_o = json_object_new_string(project_key);
+       if (!key_o) {
+               _E("failed to create json object for project_key");
+               json_object_put(msg_o);
+               return -1;
+       }
+
+       json_object_object_add(msg_o, "key", key_o);
+       json_object_object_add(msg_o, "data", tb_data->arr_obj);
+#ifdef TEST_DEVICE_ID
+/* only for test, SHOULD BE REMOVED after device auth method is applied on cloud */
+       struct json_object *did_o = json_object_new_string(TEST_DEVICE_ID);
+       json_object_object_add(msg_o, "deviceId", did_o);
+#endif /* TEST_DEVICE_ID */
+
+       ret  = __ts_send_data(handle, json_object_to_json_string(msg_o));
+       if (ret) {
+               _E("failed to send data");
+               /* keep appended data */
+               json_object_get(tb_data->arr_obj);
+       } else { /* if success to send data, remove all items in tb_data */
+               tb_data->arr_obj = NULL;
                tb_data->length = 0;
        }
 
+       json_object_put(msg_o);
+
        return ret;
 }