#include "log.h"
#include "controller.h"
+#include "json-util.h"
#define TTSD_WORKER_REPORT_COMMAND "ttsd_worker_report_system_information"
+#define COMMAND_ID "id"
+#define TEST 1
+#define SUBMIT_SYNC 0
typedef struct app_data_s {
- FILE *file;
} app_data;
-static int __get_requested_system_info(const char *key, void *data);
-static int __get_all_system_info(void *data);
-static int __get_system_info_int(const char *key, FILE *file);
-static int __get_system_info_bool(const char *key, FILE *file);
-static int __get_system_info_string(const char *key, FILE *file);
-static int __initialize_file_logging(FILE **file);
-static void __finalize_file_logging(FILE *file);
+static int __get_requested_system_info(const char *key);
+static int __get_all_system_info(void);
+static int __get_system_info_int(const char *key);
+static int __get_system_info_bool(const char *key);
+static int __get_system_info_string(const char *key);
static const char *__check_error_reason(int err_code);
-
-static void __make_and_send_data(void)
-{
- _D("Make & Send Data");
- int ret = -1;
- char *data = "jjoggoba is a bad boy";
-
- ret = ttsd_worker_submit_report(data);
- if (ret < 0)
- _E("Failed send operation");
-}
+static void __submit_report_completed_cb(const char *result, void *user_data);
static bool service_app_create(void *data)
{
- app_data *ad = data;
-
- if (__initialize_file_logging(&(ad->file)) < 0)
+ if (json_util_init() < 0) {
+ _E("Failed to initialize Json Object");
return false;
-
- __make_and_send_data();
+ }
return true;
}
{
app_data *ad = data;
- __finalize_file_logging(ad->file);
+ if (json_util_fini() < 0)
+ _E("Failed to finalize Json Object");
free(ad);
}
-static int __get_requested_system_info(const char *key, void *data)
-{
- _D("Get system info[%s]", key);
- int ret = -1;
- app_data *ad = data;
-
- if (key == NULL) {
- _E("Key for system info is null");
- return -1;
- }
-
- if (ad == NULL) {
- _E("app_data is null");
- return -1;
- }
-
- for (int i = 0; i < INFO_KEY_MAX; i++)
- {
- if (!strncmp(key, sys_info[i].key, strlen(key))) {
- switch (sys_info[i].type)
- {
- case KEY_TYPE_INT:
- ret = __get_system_info_int(sys_info[i].key, ad->file);
- break;
- case KEY_TYPE_BOOL:
- ret = __get_system_info_bool(sys_info[i].key, ad->file);
- break;
- case KEY_TYPE_STRING:
- ret = __get_system_info_string(sys_info[i].key, ad->file);
- break;
- default:
- _E("Strange key type");
- break;
- }
- return ret;
- }
- }
- _E("There's no matched key[%s]", key);
-
- return -1;
-}
-
-static int __get_all_system_info(void *data)
-{
- int ret = -1;
- app_data *ad = data;
-
- if (ad == NULL) {
- _E("app_data is null");
- return -1;
- }
-
- for (int i = 0; i < INFO_KEY_MAX; i++)
- {
- switch (sys_info[i].type)
- {
- case KEY_TYPE_INT:
- ret = __get_system_info_int(sys_info[i].key, ad->file);
- break;
- case KEY_TYPE_BOOL:
- ret = __get_system_info_bool(sys_info[i].key, ad->file);
- break;
- case KEY_TYPE_STRING:
- ret = __get_system_info_string(sys_info[i].key, ad->file);
- break;
- default:
- _E("Strange type");
- break;
- }
- }
-
- return ret;
-}
-
static void service_app_control(app_control_h app_control, void *data)
{
/* APP_CONTROL */
int ret = -1;
char *command = NULL;
- app_data *ad = data;
+ char *cmd_id = NULL;
+ char *report = NULL;
+
+ ret = json_util_begin();
+ ret_if(ret < 0);
app_control_get_extra_data(app_control, TTSD_WORKER_REPORT_COMMAND, &command);
+ app_control_get_extra_data(app_control, COMMAND_ID, &cmd_id);
+
+ ret = json_util_add_string(COMMAND_ID, cmd_id);
+ ret_if(ret < 0);
if (command) {
char *ptr;
ptr = strtok(command, ",");
while (ptr != NULL) {
- ret = __get_requested_system_info(ptr, ad);
- if (ret < 0)
+ ret = __get_requested_system_info(ptr);
+ if (ret < 0) {
_E("Failed to get [%s] information", ptr);
+ goto DONE;
+ }
ptr = strtok(NULL, ",");
}
-
- free(command);
} else {
- ret = __get_all_system_info(ad);
- if (ret < 0)
+ ret = __get_all_system_info();
+ if (ret < 0) {
_E("Failed to get system information");
+ goto DONE;
+ }
}
- service_app_exit();
+ ret = json_util_end();
+ goto_if(ret < 0, DONE);
+
+ report = json_util_get_json_string();
+ goto_if(!report, DONE);
+
+DONE:
+ if (command)
+ free(command);
+
+ if (cmd_id)
+ free(cmd_id);
+
+ if (report) {
+#if SUBMIT_SYNC
+ ret = ttsd_worker_submit_report_sync(report);
+ if (ret < 0)
+ _E("Failed to submit report sync");
+
+ free(report);
+ service_app_exit();
+#else
+ ret = ttsd_worker_submit_report(report, __submit_report_completed_cb, NULL);
+ free(report);
+
+ if (ret < 0) {
+ _E("Failed to submit report");
+ service_app_exit();
+ }
+#endif
+ }
}
static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
return ret;
}
-static int __get_system_info_int(const char *key, FILE *file)
+static int __get_system_info_int(const char *key)
{
int ret = SYSTEM_INFO_ERROR_NONE;
int val = -1;
+ retv_if(!key, -1);
+
ret = system_info_get_platform_int(key, &val);
if (ret != SYSTEM_INFO_ERROR_NONE) {
_E("Failed to get System Info [%s] -- [%s]", key, __check_error_reason(ret));
return -1;
- } else {
- _D("%s : %d", key, val);
- fprintf(file, "%s : %d\n", key, val);
}
+ _D("%s : %d", key, val);
+
+ ret = json_util_add_int(key, val);
- return 0;
+ return ret;
}
-static int __get_system_info_bool(const char *key, FILE *file)
+static int __get_system_info_bool(const char *key)
{
int ret = SYSTEM_INFO_ERROR_NONE;
bool val = false;
+ retv_if(!key, -1);
+
ret = system_info_get_platform_bool(key, &val);
if (ret != SYSTEM_INFO_ERROR_NONE) {
_E("Failed to get System Info [%s] -- [%s]", key, __check_error_reason(ret));
return -1;
- } else {
- _D("%s : %s", key, val ? "TRUE" : "FALSE");
- fprintf(file, "%s : %s\n", key, val ? "TRUE" : "FALSE");
}
+ _D("%s : %s", key, val ? "TRUE" : "FALSE");
- return 0;
+ ret = json_util_add_boolean(key, val);
+
+ return ret;
}
-static int __get_system_info_string(const char *key, FILE *file)
+static int __get_system_info_string(const char *key)
{
int ret = SYSTEM_INFO_ERROR_NONE;
char *val = NULL;
ret = system_info_get_platform_string(key, &val);
if (ret != SYSTEM_INFO_ERROR_NONE) {
_E("Failed to get System Info [%s] -- [%s]", key, __check_error_reason(ret));
- } else {
- _D("%s : %s", key, val);
- fprintf(file, "%s : %s\n", key, val);
}
+ _D("%s : %s", key, val);
- free(val);
-
- return 0;
-}
-
-static int __initialize_file_logging(FILE **file)
-{
- if((*(file) = fopen("/tmp/info.txt", "w+")) == NULL) {
- _E("Failed to open log file");
- return -1;
- }
+ ret = json_util_add_boolean(key, val);
- return 0;
-}
+ free(val);
-static void __finalize_file_logging(FILE *file)
-{
- if (file)
- fclose(file);
+ return ret;
}
static const char *__check_error_reason(int err_code)
return reason;
}
+static int __get_requested_system_info(const char *key)
+{
+ int ret = -1;
+
+ if (key == NULL) {
+ _E("Key for system info is null");
+ return -1;
+ }
+
+ for (int i = 0; i < INFO_KEY_MAX; i++)
+ {
+ if (!strncmp(key, sys_info[i].key, strlen(key))) {
+ switch (sys_info[i].type)
+ {
+ case KEY_TYPE_INT:
+ ret = __get_system_info_int(sys_info[i].key);
+ break;
+ case KEY_TYPE_BOOL:
+ ret = __get_system_info_bool(sys_info[i].key);
+ break;
+ case KEY_TYPE_STRING:
+ ret = __get_system_info_string(sys_info[i].key);
+ break;
+ default:
+ _E("Strange key type");
+ break;
+ }
+ return ret;
+ }
+ }
+ _E("There's no matched key[%s]", key);
+
+ return -1;
+}
+
+static int __get_all_system_info(void)
+{
+ int ret = -1;
+
+#if TEST
+ for (int i = 0; i < 5; i++)
+#else
+ for (int i = 0; i < INFO_KEY_MAX; i++)
+#endif
+ {
+ switch (sys_info[i].type)
+ {
+ case KEY_TYPE_INT:
+ ret = __get_system_info_int(sys_info[i].key);
+ break;
+ case KEY_TYPE_BOOL:
+ ret = __get_system_info_bool(sys_info[i].key);
+ break;
+ case KEY_TYPE_STRING:
+ ret = __get_system_info_string(sys_info[i].key);
+ break;
+ default:
+ _E("Strange type");
+ break;
+ }
+ }
+
+ return ret;
+}
+
+static void __submit_report_completed_cb(const char *result, void *user_data)
+{
+ char *pre_report = user_data;
+
+ _D("Submit result[%s]", result);
+
+ free(pre_report);
+
+ service_app_exit();
+}
--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Contact: Junkyu Han <junkyu.han@samsung.com>
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <glib.h>
+#include <stdbool.h>
+#include <json-glib/json-glib.h>
+
+#include "json-util.h"
+#include "log.h"
+
+typedef struct _json_handle {
+ JsonBuilder *builder;
+ bool is_begin;
+ bool is_end;
+} json_handle;
+
+static json_handle Json_h = {NULL, false, false};
+
+int json_util_init(void)
+{
+ if (Json_h.builder)
+ g_object_unref(Json_h.builder);
+
+ Json_h.is_begin = false;
+ Json_h.is_end = false;
+ Json_h.builder = json_builder_new();
+ retv_if(Json_h.builder == NULL, -1);
+
+ return 0;
+}
+
+int json_util_fini(void)
+{
+
+ if (Json_h.builder) {
+ g_object_unref(Json_h.builder);
+ Json_h.builder = NULL;
+ }
+
+ Json_h.is_begin = false;
+ Json_h.is_end = false;
+
+ return 0;
+}
+
+int json_util_begin(void)
+{
+ retv_if(Json_h.builder == NULL, -1);
+ retv_if(Json_h.is_begin == true, -1);
+ retv_if(Json_h.is_end == true, -1);
+
+ Json_h.is_begin = true;
+
+ json_builder_begin_object(Json_h.builder);
+
+ return 0;
+}
+
+int json_util_end(void)
+{
+ retv_if(Json_h.builder == NULL, -1);
+ retv_if(Json_h.is_begin == false, -1);
+ retv_if(Json_h.is_end == true, -1);
+
+ json_builder_end_object(Json_h.builder);
+ Json_h.is_end = true;
+
+ return 0;
+}
+
+int json_util_add_int(const char* key, long long int value)
+{
+ retv_if(!key, -1);
+
+ if (Json_h.builder == NULL) {
+ _E("Handle for json is not initialized, call json_util_init() first");
+ return -1;
+ }
+
+ if (Json_h.is_begin == false || Json_h.is_end == true) {
+ _E("Json object has not prepared, call json_util_begin() first");
+ return -1;
+ }
+
+ json_builder_set_member_name(Json_h.builder, key);
+ json_builder_add_int_value(Json_h.builder, value);
+
+ return 0;
+}
+
+int json_util_add_boolean(const char* key, bool value)
+{
+ retv_if(!key, -1);
+
+ if (Json_h.builder == NULL) {
+ _E("Handle for json is not initialized, call json_util_init() first");
+ return -1;
+ }
+
+ if (Json_h.is_begin == false || Json_h.is_end == true) {
+ _E("Json object has not prepared, call json_util_begin() first");
+ return -1;
+ }
+
+ json_builder_set_member_name(Json_h.builder, key);
+ json_builder_add_boolean_value(Json_h.builder, value);
+
+ return 0;
+}
+
+int json_util_add_string(const char* key, const char *value)
+{
+ retv_if(!key, -1);
+
+ if (Json_h.builder == NULL) {
+ _E("Handle for json is not initialized, call json_util_init() first");
+ return -1;
+ }
+
+ if (Json_h.is_begin == false || Json_h.is_end == true) {
+ _E("Json object has not prepared, call json_util_begin() first");
+ return -1;
+ }
+
+ json_builder_set_member_name(Json_h.builder, key);
+ json_builder_add_string_value(Json_h.builder, value);
+
+ return 0;
+}
+
+char *json_util_get_json_string(void)
+{
+ JsonGenerator *gen = NULL;
+ JsonNode *root = NULL;
+ char *str = NULL;
+
+ retv_if(!Json_h.builder, NULL);
+ retv_if(Json_h.is_begin == false, NULL);
+ retv_if(Json_h.is_end == false, NULL);
+
+ root = json_builder_get_root(Json_h.builder);
+ retv_if(!root, NULL);
+
+ gen = json_generator_new();
+ goto_if(!gen, DONE);
+
+ json_generator_set_root(gen, root);
+
+ str = json_generator_to_data(gen, NULL);
+
+DONE:
+ if (gen)
+ g_object_unref(gen);
+
+ if (root)
+ json_node_free(root);
+
+ return str;
+}