+/*
+ * 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);
+}