add build json module 34/181234/1
authorJeonghoon Park <jh1979.park@samsung.com>
Mon, 11 Jun 2018 06:48:53 +0000 (15:48 +0900)
committerJeonghoon Park <jh1979.park@samsung.com>
Mon, 11 Jun 2018 06:48:53 +0000 (15:48 +0900)
Change-Id: I19b46a02e8b19eb12f127c1cb796b56d97129a07

daemon/include/ttd-build-json.h [new file with mode: 0644]
daemon/src/ttd-build-json.c [new file with mode: 0644]

diff --git a/daemon/include/ttd-build-json.h b/daemon/include/ttd-build-json.h
new file mode 100644 (file)
index 0000000..d3193e3
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * 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_BUILD_JSON_H__
+#define __TT_DAEMON_BUILD_JSON_H__
+
+char *ttd_build_json_create_report(
+       const char *id,
+       int type,
+       int state,
+       int code,
+       const char *desc,
+       const char *data);
+
+char *ttd_build_json_str_from_string(const char *key, const char *value);
+char *ttd_build_json_str_from_integer(const char *key, long long int value);
+char *ttd_build_json_str_from_boolean(const char *key, int b_value);
+char *ttd_build_json_str_from_double(const char *key, double value);
+
+#endif /* __TT_DAEMON_BUILD_JSON_H__ */
diff --git a/daemon/src/ttd-build-json.c b/daemon/src/ttd-build-json.c
new file mode 100644 (file)
index 0000000..6776e6a
--- /dev/null
@@ -0,0 +1,132 @@
+/*
+ * 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 <json.h>
+#include "ttd-log.h"
+
+
+static inline char *
+__json_object_to_str(const char *key, struct json_object *value_obj)
+{
+       char *json_string = NULL;
+       struct json_object *main_object = NULL;
+
+       retv_if(!key, NULL);
+       retv_if(!value_obj, NULL);
+
+       main_object = json_object_new_object();
+       json_object_object_add(main_object, key, value_obj);
+
+       json_string = g_strdup(json_object_to_json_string(main_object));
+       json_object_put(main_object);
+
+       return json_string;
+}
+/*
+report format
+{
+       "commandID" : "ID-xxxx"(string),
+       "type" : 0(int, optional),
+       "commandState" : 0(int),
+       "content" : {
+               "code" : 0(int),
+               "desc" : "xxxx"(string, optional),
+               "data" : "xxxx"(string, optional)
+       }(json object)
+}
+*/
+char *ttd_build_json_create_report(
+       const char *id,
+       int type,
+       int state,
+       int code,
+       const char *desc,
+       const char *json_data)
+{
+       char *json_report = NULL;
+       struct json_object *main_object = NULL;
+       struct json_object *content_object = NULL;
+       struct json_object *tmp_object = NULL;
+
+       retv_if(!id, NULL);
+       retv_if(!state, NULL);
+
+       main_object = json_object_new_object();
+
+       tmp_object = json_object_new_string(id);
+       json_object_object_add(main_object, "commandID", tmp_object);
+
+       tmp_object =  json_object_new_int(type);
+       json_object_object_add(main_object, "type", tmp_object);
+
+       tmp_object =  json_object_new_int(state);
+       json_object_object_add(main_object, "commandState", tmp_object);
+
+       content_object = json_object_new_object();
+
+       tmp_object =  json_object_new_int(code);
+       json_object_object_add(content_object, "code", tmp_object);
+
+       tmp_object =  json_object_new_string(desc);
+       json_object_object_add(content_object, "desc", tmp_object);
+
+       if (json_data) {
+               tmp_object = json_object_new_string(json_data);
+               json_object_object_add(content_object, "data", tmp_object);
+       }
+
+       json_object_object_add(main_object, "content", content_object);
+
+       json_report = g_strdup(json_object_to_json_string(main_object));
+       json_object_put(main_object);
+
+       return json_report;
+}
+
+char *ttd_build_json_str_from_string(const char *key, const char *value)
+{
+       struct json_object *value_obj = NULL;
+
+       retv_if(!value, NULL);
+
+       value_obj = json_object_new_string(value);
+       return __json_object_to_str(key, value_obj);
+}
+
+char *ttd_build_json_str_from_integer(const char *key, long long int value)
+{
+       struct json_object *value_obj = NULL;
+
+       value_obj = json_object_new_int(value);
+       return __json_object_to_str(key, value_obj);
+}
+
+char *ttd_build_json_str_from_boolean(const char *key, int b_value)
+{
+       struct json_object *value_obj = NULL;
+
+       value_obj = json_object_new_boolean(b_value);
+       return __json_object_to_str(key, value_obj);
+}
+
+char *ttd_build_json_str_from_double(const char *key, double value)
+{
+       struct json_object *value_obj = NULL;
+
+       value_obj = json_object_new_double(value);
+       return __json_object_to_str(key, value_obj);
+}