2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Contact: Jin Yoon <jinny.yoon@samsung.com>
5 * Geunsun Lee <gs86.lee@samsung.com>
6 * Eunyoung Lee <ey928.lee@samsung.com>
7 * Junkyu Han <junkyu.han@samsung.com>
8 * Jeonghoon Park <jh1979.park@samsung.com>
10 * Licensed under the Flora License, Version 1.1 (the License);
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://floralicense.org/license/
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an AS IS BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
24 #include <curl/curl.h>
25 #include <glib-object.h>
26 #include <json-glib/json-glib.h>
30 typedef struct _wu_json_handle {
36 static wu_json_handle Json_h = {NULL, false, false};
38 static size_t _response_write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
42 res_size = size*nmemb;
45 _I("POST response : %s", ptr);
46 /* What should we do here, if response body has negative message? */
51 int web_util_noti_init(void)
55 result = curl_global_init(CURL_GLOBAL_DEFAULT);
56 if(result != CURLE_OK) {
57 _E("curl_global_init() failed: %s",
58 curl_easy_strerror(result));
64 void web_util_noti_fini(void)
66 curl_global_cleanup();
70 int web_util_noti_post(const char *resource, const char *json_data)
74 CURLcode response = CURLE_OK;
75 struct curl_slist *headers = NULL;
77 retv_if(resource == NULL,-1);
78 retv_if(json_data == NULL,-1);
80 _I("server : %s", resource);
81 _I("json_data : %s", json_data);
83 curl = curl_easy_init();
86 _E("fail to init curl");
90 headers = curl_slist_append(headers, "Accept: application/json");
91 headers = curl_slist_append(headers, "Content-Type: application/json");
93 curl_easy_setopt(curl, CURLOPT_URL, resource);
94 curl_easy_setopt(curl, CURLOPT_POST, 1L);
95 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
96 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_data);
97 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _response_write_callback);
99 response = curl_easy_perform(curl);
101 if(response != CURLE_OK) {
102 _E("curl_easy_perform() failed: %s",
103 curl_easy_strerror(response));
104 /* What should we do here, if response is kind of errors? */
108 curl_slist_free_all(headers);
109 curl_easy_cleanup(curl);
115 int web_util_json_init(void)
118 g_object_unref(Json_h.builder);
121 Json_h.is_begin = false;
122 Json_h.is_end = false;
123 Json_h.builder = json_builder_new();
124 retv_if(Json_h.builder == NULL, -1);
129 int web_util_json_fini(void)
133 g_object_unref(Json_h.builder);
134 Json_h.builder = NULL;
137 Json_h.is_begin = false;
138 Json_h.is_end = false;
143 int web_util_json_data_array_begin(void)
145 retv_if(Json_h.builder == NULL, -1);
146 retv_if(Json_h.is_begin == true, -1);
147 retv_if(Json_h.is_end == true, -1);
149 Json_h.is_begin = true;
153 SensorsDataList : [ SensorData ]
157 json_builder_begin_object(Json_h.builder);
158 json_builder_set_member_name(Json_h.builder,"SensorDataList");
159 json_builder_begin_array(Json_h.builder);
164 int web_util_json_data_array_end(void)
166 retv_if(Json_h.builder == NULL, -1);
167 retv_if(Json_h.is_begin == false, -1);
168 retv_if(Json_h.is_end == true, -1);
170 json_builder_end_array(Json_h.builder);
171 json_builder_end_object(Json_h.builder);
172 Json_h.is_end = true;
177 int web_util_json_add_sensor_data(const char* sensorpi_id, web_util_sensor_data_s *sensor_data)
179 const char n_id[] = "SensorPiID";
180 const char n_motion[] = "Motion";
181 const char n_flame[] = "Flame";
182 const char n_hum[] = "Humidity";
183 const char n_temp[] = "Temperature";
184 const char n_vib[] = "Vibration";
185 const char n_co2[] = "CO2";
186 const char n_sound[] = "SoundLevel";
187 const char n_e_sensor[] = "SensorEnabled";
188 const char n_hash[] = "Hash";
190 retv_if(Json_h.builder == NULL, -1);
191 retv_if(Json_h.is_begin == false, -1);
192 retv_if(Json_h.is_end == true, -1);
193 retv_if(sensorpi_id == NULL, -1);
194 retv_if(sensor_data == NULL, -1);
206 SensorEnabled: [Motion, ],
211 json_builder_begin_object(Json_h.builder);
213 json_builder_set_member_name(Json_h.builder, n_id);
214 json_builder_add_string_value(Json_h.builder, sensorpi_id);
216 if(sensor_data->enabled_sensor & WEB_UTIL_SENSOR_MOTION) {
217 json_builder_set_member_name(Json_h.builder, n_motion);
218 json_builder_add_int_value(Json_h.builder, sensor_data->motion);
221 if(sensor_data->enabled_sensor & WEB_UTIL_SENSOR_FLAME) {
222 json_builder_set_member_name(Json_h.builder, n_flame);
223 json_builder_add_int_value(Json_h.builder, sensor_data->flame);
226 if(sensor_data->enabled_sensor & WEB_UTIL_SENSOR_HUMIDITY) {
227 json_builder_set_member_name(Json_h.builder, n_hum);
228 json_builder_add_double_value(Json_h.builder, sensor_data->humidity);
231 if(sensor_data->enabled_sensor & WEB_UTIL_SENSOR_TEMPERATURE) {
232 json_builder_set_member_name(Json_h.builder, n_temp);
233 json_builder_add_double_value(Json_h.builder, sensor_data->temperature);
236 if(sensor_data->enabled_sensor & WEB_UTIL_SENSOR_VIB) {
237 json_builder_set_member_name(Json_h.builder, n_vib);
238 json_builder_add_int_value(Json_h.builder, sensor_data->virbration);
241 if(sensor_data->enabled_sensor & WEB_UTIL_SENSOR_CO2) {
242 json_builder_set_member_name(Json_h.builder, n_co2);
243 json_builder_add_double_value(Json_h.builder, sensor_data->co2);
246 if(sensor_data->enabled_sensor & WEB_UTIL_SENSOR_SOUND) {
247 json_builder_set_member_name(Json_h.builder, n_sound);
248 json_builder_add_int_value(Json_h.builder, sensor_data->soundlevel);
251 json_builder_set_member_name(Json_h.builder, n_e_sensor);
252 json_builder_begin_array(Json_h.builder);
254 if(sensor_data->enabled_sensor & WEB_UTIL_SENSOR_MOTION)
255 json_builder_add_string_value(Json_h.builder, n_motion);
257 if(sensor_data->enabled_sensor & WEB_UTIL_SENSOR_FLAME)
258 json_builder_add_string_value(Json_h.builder, n_flame);
260 if(sensor_data->enabled_sensor & WEB_UTIL_SENSOR_HUMIDITY)
261 json_builder_add_string_value(Json_h.builder, n_hum);
263 if(sensor_data->enabled_sensor & WEB_UTIL_SENSOR_TEMPERATURE)
264 json_builder_add_string_value(Json_h.builder, n_temp);
266 if(sensor_data->enabled_sensor & WEB_UTIL_SENSOR_VIB)
267 json_builder_add_string_value(Json_h.builder, n_vib);
269 if(sensor_data->enabled_sensor & WEB_UTIL_SENSOR_CO2)
270 json_builder_add_string_value(Json_h.builder, n_co2);
272 if(sensor_data->enabled_sensor & WEB_UTIL_SENSOR_SOUND)
273 json_builder_add_string_value(Json_h.builder, n_sound);
275 json_builder_end_array(Json_h.builder);
277 if(sensor_data->hash) {
278 json_builder_set_member_name(Json_h.builder, n_hash);
279 json_builder_add_string_value(Json_h.builder, sensor_data->hash);
282 json_builder_end_object(Json_h.builder);
287 char *web_util_get_json_string(void)
289 JsonGenerator *gen = NULL;
290 JsonNode *root = NULL;
293 retv_if(Json_h.builder == NULL, NULL);
294 retv_if(Json_h.is_begin == false, NULL);
295 retv_if(Json_h.is_end == false, NULL);
297 root = json_builder_get_root(Json_h.builder);
298 retv_if(root == NULL, NULL);
300 gen = json_generator_new();
301 goto_if(gen == NULL, error_release_all);
302 json_generator_set_root(gen, root);
304 str = json_generator_to_data(gen, NULL);
310 json_node_free(root);