update report format
[apps/native/tizen-things-daemon.git] / daemon / src / ttd-build-json.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.1 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <glib.h>
18 #include <json.h>
19 #include "ttd-log.h"
20
21
22 static inline char *
23 __json_object_to_str(const char *key, struct json_object *value_obj)
24 {
25         char *json_string = NULL;
26         struct json_object *main_object = NULL;
27
28         retv_if(!key, NULL);
29         retv_if(!value_obj, NULL);
30
31         main_object = json_object_new_object();
32         json_object_object_add(main_object, key, value_obj);
33
34         json_string = g_strdup(json_object_to_json_string(main_object));
35         json_object_put(main_object);
36
37         return json_string;
38 }
39 /*
40 report format
41 {
42         "commandID" : "ID-xxxx"(string),
43         "type" : 0(int, optional),
44         "commandState" : 0(int),
45         "content" : {
46                 "code" : 0(int),
47                 "desc" : "xxxx"(string, optional),
48                 "data" : "xxxx"(json object, optional)
49         }(json object)
50 }
51 */
52 char *ttd_build_json_create_report(
53         const char *id,
54         int type,
55         int state,
56         int code,
57         const char *desc,
58         const char *json_data)
59 {
60         char *json_report = NULL;
61         struct json_object *main_object = NULL;
62         struct json_object *content_object = NULL;
63         struct json_object *tmp_object = NULL;
64
65         retv_if(!id, NULL);
66         retv_if(!state, NULL);
67
68         main_object = json_object_new_object();
69
70         tmp_object = json_object_new_string(id);
71         json_object_object_add(main_object, "commandId", tmp_object);
72
73         tmp_object =  json_object_new_int(type);
74         json_object_object_add(main_object, "type", tmp_object);
75
76         tmp_object =  json_object_new_int(state);
77         json_object_object_add(main_object, "state", tmp_object);
78
79         content_object = json_object_new_object();
80
81         tmp_object =  json_object_new_int(code);
82         json_object_object_add(content_object, "code", tmp_object);
83
84         tmp_object =  json_object_new_string(desc);
85         json_object_object_add(content_object, "desc", tmp_object);
86
87         if (json_data) {
88                 tmp_object = json_tokener_parse(json_data);
89                 if (!tmp_object) {
90                         _W("failed to parse report data - %s", json_data);
91                         tmp_object = json_object_new_string(json_data);
92                 }
93                 json_object_object_add(content_object, "data", tmp_object);
94         }
95
96         json_object_object_add(main_object, "content", content_object);
97
98         json_report = g_strdup(json_object_to_json_string(main_object));
99         json_object_put(main_object);
100
101         return json_report;
102 }
103
104 char *ttd_build_json_str_from_string(const char *key, const char *value)
105 {
106         struct json_object *value_obj = NULL;
107
108         retv_if(!value, NULL);
109
110         value_obj = json_object_new_string(value);
111         return __json_object_to_str(key, value_obj);
112 }
113
114 char *ttd_build_json_str_from_integer(const char *key, long long int value)
115 {
116         struct json_object *value_obj = NULL;
117
118         value_obj = json_object_new_int(value);
119         return __json_object_to_str(key, value_obj);
120 }
121
122 char *ttd_build_json_str_from_boolean(const char *key, int b_value)
123 {
124         struct json_object *value_obj = NULL;
125
126         value_obj = json_object_new_boolean(b_value);
127         return __json_object_to_str(key, value_obj);
128 }
129
130 char *ttd_build_json_str_from_double(const char *key, double value)
131 {
132         struct json_object *value_obj = NULL;
133
134         value_obj = json_object_new_double(value);
135         return __json_object_to_str(key, value_obj);
136 }