Add configure module 13/179413/7
authorjunkyu han <junkyu.han@samsung.com>
Fri, 18 May 2018 01:23:41 +0000 (10:23 +0900)
committerjunkyu han <junkyu.han@samsung.com>
Mon, 21 May 2018 09:59:11 +0000 (18:59 +0900)
Change-Id: Id6562acd7740cd80a440e7175ed02208f03b1927

daemon/CMakeLists.txt
daemon/include/ttd-cmd-type.h
daemon/include/ttd-config.h [new file with mode: 0644]
daemon/res/ttd.conf [new file with mode: 0644]
daemon/src/tizen-things-daemon.c
daemon/src/ttd-config.c [new file with mode: 0644]
daemon/src/ttd-parse-cmd.c
daemon/src/ttd-worker-handle.c
packaging/tizen-things-daemon.spec

index 2e0a762..f20bd44 100644 (file)
@@ -1,5 +1,6 @@
 SET(CMAKE_VERBOSE_MAKEFILE 0)
 SET(DAEMON tizen-things-daemon)
+SET(TTD_CONF_FILE ttd.conf)
 
 INCLUDE(FindPkgConfig)
 pkg_check_modules(DAEMON_PKGS REQUIRED
@@ -39,3 +40,4 @@ TARGET_INCLUDE_DIRECTORIES(${DAEMON} PUBLIC
 
 # Install
 INSTALL(TARGETS ${DAEMON} DESTINATION ${INSTALL_BIN_DIR})
+INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/res/${TTD_CONF_FILE} DESTINATION ${INSTALL_CONF_DIR})
index 48d678d..b8a4724 100644 (file)
@@ -37,10 +37,15 @@ typedef enum { /* TBD */
 
 typedef enum { /* TBD */
        TTD_CMD_CONFIG_SET,
-       TTD_CMD_CONFIG_UNSET,
        TTD_CMD_CONFIG_MAX,
 } ttd_cmd_config_e;
 
+typedef enum { /* TBD */
+       TTD_CMD_CONFIG_SET_SERVER_URL,
+       TTD_CMD_CONFIG_SET_APP_ID,
+       TTD_CMD_CONFIG_ACTION_MAX,
+} ttd_cmd_config_action_e;
+
 typedef enum {
        TTD_CMD_PACKAGE_INSTALL,
        TTD_CMD_PACKAGE_REMOVE,
diff --git a/daemon/include/ttd-config.h b/daemon/include/ttd-config.h
new file mode 100644 (file)
index 0000000..08eeb61
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * 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__ */
+
diff --git a/daemon/res/ttd.conf b/daemon/res/ttd.conf
new file mode 100644 (file)
index 0000000..784ab66
--- /dev/null
@@ -0,0 +1,4 @@
+[url]
+server=http://apitest.showiot.xyz/api/cmd?&target=test-page-device&owner=test-page&state=created
+[worker]
+appid=
index 8d3665b..40649ec 100644 (file)
 #include <stdlib.h>
 #include <glib.h>
 #include <glib-unix.h>
+
 #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"
@@ -112,6 +114,7 @@ static ttd_task_func __get_task_func(ttd_cmd_data *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:
diff --git a/daemon/src/ttd-config.c b/daemon/src/ttd-config.c
new file mode 100644 (file)
index 0000000..f3d2d3d
--- /dev/null
@@ -0,0 +1,323 @@
+/*
+ * 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
index e9320a0..1336b5e 100644 (file)
@@ -27,6 +27,7 @@
 #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)
@@ -71,23 +72,48 @@ static int __parse_cmd_power(json_object *obj, ttd_cmd_data *cmd)
 
 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)
index c9d5070..498b5ca 100644 (file)
@@ -20,7 +20,7 @@
 #include "ttd-log.h"
 
 #define WORKER_PKG_MGR "org.tizen.package-manager-worker"
-#define WORKER_INFO_SYS "org.tizen.ttsd-worker-system"
+#define WORKER_INFO_SYS "ttsd-worker-system"
 #define WORKER_INFO_TASK "org.tizen.task-worker"
 
 int __worker_launch_info(ttd_cmd_data *c_data)
index ff5c496..cfc9709 100644 (file)
@@ -36,6 +36,7 @@ A Things Service library in Tizen (Development) package.
 %setup -q
 
 %build
+%define _ttd_conf_dir %{TZ_SYS_ETC}/ttd
 
 %ifarch %{arm}
 export CFLAGS="$CFLAGS -DTIZEN_BUILD_TARGET"
@@ -52,6 +53,7 @@ MAJOR_VER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'`
 cmake . -DINSTALL_BIN_DIR=%{_bindir} \
        -DINSTALL_LIB_DIR=%{_libdir} \
        -DINSTALL_INC_DIR=%{_includedir} \
+       -DINSTALL_CONF_DIR=%{_ttd_conf_dir} \
        -DFULL_VER=%{version} \
        -DMAJOR_VER=${MAJOR_VER}
 
@@ -67,6 +69,8 @@ install -m 0644 %SOURCE1 %{buildroot}%{_unitdir}/%{name}.service
 %post
 /sbin/ldconfig
 
+mkdir -p %{_ttd_conf_dir}
+
 %postun -p /sbin/ldconfig
 
 %files
@@ -77,6 +81,7 @@ install -m 0644 %SOURCE1 %{buildroot}%{_unitdir}/%{name}.service
 %{_unitdir}/multi-user.target.wants/%{name}.service
 %{_libdir}/*.so.*
 %{_bindir}/*
+%{_ttd_conf_dir}/*.conf
 
 %files devel
 %{_includedir}/*.h