--- /dev/null
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * 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.
+ */
+
+#ifndef __TT_DAEMON_CONFIG_H__
+#define __TT_DAEMON_CONFIG_H__
+
+#include <stdbool.h>
+#include "ttd-cmd.h"
+
+#define CONFIG_PATH_MAX 1024
+
+int ttd_config_init(void);
+void ttd_config_fini(void);
+int ttd_config_write(void *cmd_data);
+int ttd_config_read_integer(const char *group, const char *key, int *value);
+int ttd_config_read_boolean(const char *group, const char *key, bool *value);
+int ttd_config_read_string(const char *group, const char *key, char **value);
+int ttd_config_write_integer(const char *group, const char *key, int value);
+int ttd_config_write_boolean(const char *group, const char *key, bool value);
+int ttd_config_write_string(const char *group, const char *key, const char *value);
+int ttd_config_remove_group(const char *group);
+int tt_config_remove_key(const char *group, const char *key);
+
+#endif /* __TT_DAEMON_CONFIG_H__ */
+
--- /dev/null
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * 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 <stdlib.h>
+#include <string.h>
+#include <tzplatform_config.h>
+
+#include "ttd-config.h"
+#include "ttd-cmd.h"
+#include "ttd-log.h"
+
+#define CONF_FILE_NAME "ttd.conf"
+#define CONF_PATH tzplatform_mkpath3(TZ_SYS_ETC, "ttd", "ttd.conf")
+
+GKeyFile *gkf = NULL;
+int is_initialized = 0;
+
+int ttd_config_init(void)
+{
+ gkf = g_key_file_new();
+ retv_if(!gkf, -1);
+
+ if (!g_key_file_load_from_file(gkf, CONF_PATH, G_KEY_FILE_NONE, NULL)) {
+ _E("Failed to load conf file");
+ ttd_config_fini();
+ return -1;
+ }
+
+ is_initialized = 1;
+
+ return 0;
+}
+
+void ttd_config_fini(void)
+{
+ if (gkf)
+ g_key_file_free(gkf);
+}
+
+int ttd_config_read_integer(const char *group, const char *key, int *value)
+{
+ GError *error = NULL;
+ int ret = 0;
+
+ retv_if(!group, -1);
+ retv_if(!key, -1);
+
+ if (!is_initialized) {
+ if (ttd_config_init() < 0) {
+ _E("Failed to initialize configure");
+ return -1;
+ }
+ }
+
+ ret = g_key_file_get_integer(gkf, group, key, &error);
+ if (!error) {
+ _E("Failed to get integer[%s]", error->message);
+ g_error_free(error);
+ return -1;
+ }
+
+ *value = ret;
+
+ return 0;
+}
+
+int ttd_config_read_boolean(const char *group, const char *key, bool *value)
+{
+ GError *error = NULL;
+ gboolean ret = FALSE;
+
+ retv_if(!group, -1);
+ retv_if(!key, -1);
+
+ if (!is_initialized) {
+ if (ttd_config_init() < 0) {
+ _E("Failed to initialize configure");
+ return -1;
+ }
+ }
+
+ ret = g_key_file_get_boolean(gkf, group, key, &error);
+ if (!ret) {
+ _E("Failed to get boolean[%s]", error->message);
+ g_error_free(error);
+ return -1;
+ }
+
+ *value = ret;
+
+ return 0;
+}
+
+int ttd_config_read_string(const char *group, const char *key, char **value)
+{
+ GError *error = NULL;
+ char *ret = NULL;
+
+ retv_if(!group, -1);
+ retv_if(!key, -1);
+
+ if (!is_initialized) {
+ if (ttd_config_init() < 0) {
+ _E("Failed to initialize configure");
+ return -1;
+ }
+ }
+
+ ret = g_key_file_get_string(gkf, group, key, &error);
+ if (!ret) {
+ _E("Failed to get string[%s]", error->message);
+ g_error_free(error);
+ return -1;
+ }
+
+ *value = ret;
+
+ return 0;
+}
+
+int ttd_config_write_integer(const char *group, const char *key, int value)
+{
+ retv_if(!group, -1);
+ retv_if(!key, -1);
+ retv_if(!value, -1);
+
+ if (!is_initialized) {
+ if (ttd_config_init() < 0) {
+ _E("Failed to initialize configure");
+ return -1;
+ }
+ }
+
+ g_key_file_set_integer(gkf, group, key, value);
+
+ return 0;
+}
+
+int ttd_config_write_bool(const char *group, const char *key, bool value)
+{
+ retv_if(!group, -1);
+ retv_if(!key, -1);
+ retv_if(!value, -1);
+
+ if (!is_initialized) {
+ if (ttd_config_init() < 0) {
+ _E("Failed to initialize configure");
+ return -1;
+ }
+ }
+
+ g_key_file_set_boolean(gkf, group, key, value);
+
+ return 0;
+}
+
+int ttd_config_write_string(const char *group, const char *key, const char *value)
+{
+ retv_if(!group, -1);
+ retv_if(!key, -1);
+ retv_if(!value, -1);
+
+ if (!is_initialized) {
+ if (ttd_config_init() < 0) {
+ _E("Failed to initialize configure");
+ return -1;
+ }
+ }
+
+ g_key_file_set_string(gkf, group, key, value);
+
+ return 0;
+}
+
+int ttd_config_remove_group(const char *group)
+{
+ gboolean ret = FALSE;
+ GError *error = NULL;
+
+ retv_if(!group, -1);
+
+ if (!is_initialized) {
+ if (ttd_config_init() < 0) {
+ _E("Failed to initialize configure");
+ return -1;
+ }
+ }
+
+ ret = g_key_file_remove_group(gkf, group, &error);
+ if (!ret) {
+ _E("Failed to remove group[%s]--caused by[%s]", group, error->message);
+ g_error_free(error);
+ return -1;
+ }
+
+ return 0;
+}
+
+int ttd_config_remove_key(const char *group, const char *key)
+{
+ gboolean ret = FALSE;
+ GError *error = NULL;
+
+ retv_if(!key, -1);
+
+ if (!is_initialized) {
+ if (ttd_config_init() < 0) {
+ _E("Failed to initialize configure");
+ return -1;
+ }
+ }
+
+ ret = g_key_file_remove_key(gkf, group, key, &error);
+ if (!ret) {
+ _E("Failed to remove group[%s]--caused by[%s]", group, error->message);
+ g_error_free(error);
+ return -1;
+ }
+
+ return 0;
+}
+
+int ttd_config_write(void *cmd_data)
+{
+ ttd_cmd_data *c_data = cmd_data;
+ void *data = NULL;
+ unsigned int data_len = 0;
+ int ret = 0;
+
+ retv_if(!cmd_data, -1);
+
+ ret = ttd_cmd_get_data(c_data, &data, &data_len);
+ retv_if(ret < 0, -1);
+
+ switch (ttd_cmd_get_command(cmd_data))
+ {
+ case TTD_CMD_CONFIG_SET_SERVER_URL:
+ ret = ttd_config_write_string("url", "server", (char *)data);
+ if (ret < 0) {
+ _E("Failed to set server url[%s]", (char *)data);
+ return -1;
+ }
+ break;
+ case TTD_CMD_CONFIG_SET_APP_ID:
+ ret = ttd_config_write_string("worker", "appid", (char *)data);
+ if (ret < 0) {
+ _E("Failed to set app id[%s]", (char *)data);
+ return -1;
+ }
+ break;
+#if 0
+ case TTD_CMD_CONFIG_SET_XXXX_XXXX:
+ int val = __convert_string_to_int((char *)c_data->data);
+ ret = ttd_config_write_integer("", "", val);
+ if (ret < 0) {
+ _E("Failed to set XXX XXX[%d]", val);
+ return -1;
+ }
+ break;
+ case TTD_CMD_CONFIG_SET_XXXX_XXX:
+ bool val = __convert_string_to_bool((char *)c_data->data);
+ ret = ttd_config_write_boolean("", "", val);
+ if (ret < 0) {
+ _E("Failed to set XXX XXX[%d]", val);
+ return -1;
+ }
+ break;
+#endif
+ default:
+ _E("Strange action");
+ return -1;
+ break;
+ }
+
+ return 0;
+}
+
+#if 0
+static int __convert_string_to_int(const char *str);
+static bool __convert_string_to_bool(const char *str);
+
+static int __convert_string_to_int(const char *str)
+{
+ if (!str) {
+ _E("String is NULL!!!!!!!");
+ return -1;
+ }
+
+ return atoi(str);
+}
+
+static bool __convert_string_to_bool(const char *str)
+{
+ if (!str) {
+ _E("String is NULL!!!!!!!");
+ return false;
+ }
+
+ if ((!strncmp(str, "false", strlen("false"))) || (!strncmp(str, "FALSE", strlen("FALSE"))))
+ return false;
+ else if ((!strncmp(str, "true", strlen("true")))
+ || (!strncmp(str, "TRUE", strlen("TRUE"))))
+ return true;
+ else {
+ _E("Strange data!!!!!!");
+ return false;
+ }
+}
+#endif
#define TTD_CMD_KEY_STATE_CREATED "created"
#define TTD_CMD_KEY_CONTENT "content"
#define TTD_CMD_KEY_CONTENT_ACTION "action"
+#define TTD_CMD_KEY_CONTENT_INFO "info"
#define TTD_CMD_KEY_EXTRA "extra"
static int __parse_cmd_get_action(json_object *obj)
static int __parse_cmd_config(json_object *obj, ttd_cmd_data *cmd)
{
- int action = 0;
+ json_object *content_obj = NULL;
+ json_object *info_obj = NULL;
+ void *info_data = NULL;
+ int action = -1;
int ret = 0;
+
action = __parse_cmd_get_action(obj);
retv_if(action < 0, -1);
switch (action) {
- case TTD_CMD_CONFIG_SET:
- case TTD_CMD_CONFIG_UNSET:
+ case TTD_CMD_CONFIG_SET_SERVER_URL:
+ case TTD_CMD_CONFIG_SET_APP_ID:
+ {
ret = ttd_cmd_set_command(cmd, action);
+ if (ret < 0) {
+ _E("Failed to set action");
+ return -1;
+ }
+
+ content_obj = json_object_object_get(obj, TTD_CMD_KEY_CONTENT);
+ retvm_if(!content_obj, -1, "%s", "failed to get content");
+
+ info_obj = json_object_object_get(content_obj, TTD_CMD_KEY_CONTENT_INFO);
+ retvm_if(!info_obj, -1, "failed to get content information");
+
+ info_data = g_strdup(json_object_to_json_string(info_obj));
+ if (info_data)
+ ttd_cmd_set_data(cmd, info_data, strlen(info_data));
+ else {
+ _E("failed to get extra content information");
+ return -1;
+ }
break;
- case TTD_CMD_CONFIG_MAX:
+ }
+ case TTD_CMD_CONFIG_ACTION_MAX:
default:
_E("Unknown action : %d", action);
return -1;
break;
}
- return ret;
+
+ return 0;
}
static int __parse_cmd_package(json_object *obj, ttd_cmd_data *cmd)