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>
9 * Licensed under the Flora License, Version 1.1 (the License);
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://floralicense.org/license/
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an AS IS BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
29 #include "connectivity.h"
31 #define ULTRASONIC_RESOURCE_TYPE "org.tizen.door"
34 static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data);
36 static int _send_response(iotcon_request_h request, iotcon_representation_h representation, iotcon_response_result_e result)
39 iotcon_response_h response;
41 ret = iotcon_response_create(request, &response);
42 retv_if(IOTCON_ERROR_NONE != ret, -1);
44 ret = iotcon_response_set_result(response, result);
45 goto_if(IOTCON_ERROR_NONE != ret, error);
47 ret = iotcon_response_set_representation(response, representation);
48 goto_if(IOTCON_ERROR_NONE != ret, error);
50 ret = iotcon_response_send(response);
51 goto_if(IOTCON_ERROR_NONE != ret, error);
53 iotcon_response_destroy(response);
58 iotcon_response_destroy(response);
62 static void _destroy_representation(iotcon_representation_h representation)
64 ret_if(!representation);
65 iotcon_representation_destroy(representation);
68 static iotcon_representation_h _create_representation_with_bool(iotcon_resource_h res, const char *key, bool value)
70 iotcon_attributes_h attributes = NULL;
71 iotcon_representation_h representation = NULL;
72 char *uri_path = NULL;
75 ret = iotcon_resource_get_uri_path(res, &uri_path);
76 retv_if(IOTCON_ERROR_NONE != ret, NULL);
78 ret = iotcon_representation_create(&representation);
79 retv_if(IOTCON_ERROR_NONE != ret, NULL);
81 ret = iotcon_attributes_create(&attributes);
82 goto_if(IOTCON_ERROR_NONE != ret, error);
84 ret = iotcon_representation_set_uri_path(representation, uri_path);
85 goto_if(IOTCON_ERROR_NONE != ret, error);
87 ret = iotcon_attributes_add_bool(attributes, key, value);
88 goto_if(IOTCON_ERROR_NONE != ret, error);
90 ret = iotcon_representation_set_attributes(representation, attributes);
91 goto_if(IOTCON_ERROR_NONE != ret, error);
93 iotcon_attributes_destroy(attributes);
95 return representation;
98 if (attributes) iotcon_attributes_destroy(attributes);
99 if (representation) iotcon_representation_destroy(representation);
104 static iotcon_representation_h _create_representation_with_int(iotcon_resource_h res, const char *key, int value)
106 iotcon_attributes_h attributes = NULL;
107 iotcon_representation_h representation = NULL;
108 char *uri_path = NULL;
111 ret = iotcon_resource_get_uri_path(res, &uri_path);
112 retv_if(IOTCON_ERROR_NONE != ret, NULL);
114 ret = iotcon_representation_create(&representation);
115 retv_if(IOTCON_ERROR_NONE != ret, NULL);
117 ret = iotcon_attributes_create(&attributes);
118 goto_if(IOTCON_ERROR_NONE != ret, error);
120 ret = iotcon_representation_set_uri_path(representation, uri_path);
121 goto_if(IOTCON_ERROR_NONE != ret, error);
123 ret = iotcon_attributes_add_int(attributes, "opened", value);
124 goto_if(IOTCON_ERROR_NONE != ret, error);
126 ret = iotcon_representation_set_attributes(representation, attributes);
127 goto_if(IOTCON_ERROR_NONE != ret, error);
129 iotcon_attributes_destroy(attributes);
131 return representation;
134 if (attributes) iotcon_attributes_destroy(attributes);
135 if (representation) iotcon_representation_destroy(representation);
140 static iotcon_representation_h _create_representation_with_double(iotcon_resource_h res, const char *key, double value)
142 iotcon_attributes_h attributes = NULL;
143 iotcon_representation_h representation = NULL;
144 char *uri_path = NULL;
147 ret = iotcon_resource_get_uri_path(res, &uri_path);
148 retv_if(IOTCON_ERROR_NONE != ret, NULL);
150 ret = iotcon_representation_create(&representation);
151 retv_if(IOTCON_ERROR_NONE != ret, NULL);
153 ret = iotcon_attributes_create(&attributes);
154 goto_if(IOTCON_ERROR_NONE != ret, error);
156 ret = iotcon_representation_set_uri_path(representation, uri_path);
157 goto_if(IOTCON_ERROR_NONE != ret, error);
159 ret = iotcon_attributes_add_double(attributes, "opened", value);
160 goto_if(IOTCON_ERROR_NONE != ret, error);
162 ret = iotcon_representation_set_attributes(representation, attributes);
163 goto_if(IOTCON_ERROR_NONE != ret, error);
165 iotcon_attributes_destroy(attributes);
167 return representation;
170 if (attributes) iotcon_attributes_destroy(attributes);
171 if (representation) iotcon_representation_destroy(representation);
176 int connectivity_notify_bool(connectivity_resource_s *resource_info, const char *key, bool value)
178 iotcon_representation_h representation;
181 retv_if(!resource_info, -1);
182 retv_if(!resource_info->observers, -1);
184 _D("Notify the value[%d]", value);
186 representation = _create_representation_with_bool(resource_info->res, key, value);
187 retv_if(!representation, -1);
189 ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
190 retv_if(IOTCON_ERROR_NONE != ret, -1);
192 _destroy_representation(representation);
197 int connectivity_notify_int(connectivity_resource_s *resource_info, const char *key, int value)
199 iotcon_representation_h representation;
202 retv_if(!resource_info, -1);
203 retv_if(!resource_info->observers, -1);
205 _D("Notify the value[%d]", value);
207 representation = _create_representation_with_int(resource_info->res, key, value);
208 retv_if(!representation, -1);
210 ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
211 retv_if(IOTCON_ERROR_NONE != ret, -1);
213 _destroy_representation(representation);
218 int connectivity_notify_double(connectivity_resource_s *resource_info, const char *key, double value)
220 iotcon_representation_h representation;
223 retv_if(!resource_info, -1);
224 retv_if(!resource_info->observers, -1);
226 _D("Notify the value[%f]", value);
228 representation = _create_representation_with_double(resource_info->res, key, value);
229 retv_if(!representation, -1);
231 ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
232 retv_if(IOTCON_ERROR_NONE != ret, -1);
234 _destroy_representation(representation);
239 static bool _query_cb(const char *key, const char *value, void *user_data)
241 _D("Key : [%s], Value : [%s]", key, value);
243 return IOTCON_FUNC_CONTINUE;
246 static int _handle_query(iotcon_request_h request)
248 iotcon_query_h query = NULL;
251 ret = iotcon_request_get_query(request, &query);
252 retv_if(IOTCON_ERROR_NONE != ret, -1);
254 if (query) iotcon_query_foreach(query, _query_cb, NULL);
259 static int _handle_request_by_crud_type(iotcon_request_h request, connectivity_resource_s *resource_info)
261 iotcon_request_type_e type;
264 ret = iotcon_request_get_request_type(request, &type);
265 retv_if(IOTCON_ERROR_NONE != ret, -1);
268 case IOTCON_REQUEST_GET:
269 _I("Do not support 'get' query");
271 case IOTCON_REQUEST_PUT:
272 _I("Do not support 'put' query");
274 case IOTCON_REQUEST_POST:
275 _I("Do not support 'post' query");
277 case IOTCON_REQUEST_DELETE:
278 _I("Do not support 'delete' query");
281 _E("Cannot reach here");
285 retv_if(0 != ret, -1);
290 static int _handle_observer(iotcon_request_h request, iotcon_observers_h observers)
292 iotcon_observe_type_e observe_type;
296 ret = iotcon_request_get_observe_type(request, &observe_type);
297 retv_if(IOTCON_ERROR_NONE != ret, -1);
299 if (IOTCON_OBSERVE_REGISTER == observe_type) {
300 ret = iotcon_request_get_observe_id(request, &observe_id);
301 retv_if(IOTCON_ERROR_NONE != ret, -1);
303 _I("Add an observer : %d", observe_id);
305 ret = iotcon_observers_add(observers, observe_id);
306 retv_if(IOTCON_ERROR_NONE != ret, -1);
307 } else if (IOTCON_OBSERVE_DEREGISTER == observe_type) {
308 ret = iotcon_request_get_observe_id(request, &observe_id);
309 retv_if(IOTCON_ERROR_NONE != ret, -1);
311 _I("Remove an observer : %d", observe_id);
313 ret = iotcon_observers_remove(observers, observe_id);
314 retv_if(IOTCON_ERROR_NONE != ret, -1);
320 static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data)
322 connectivity_resource_s *resource_info = user_data;
324 char *host_address = NULL;
328 ret = iotcon_request_get_host_address(request, &host_address);
329 goto_if(IOTCON_ERROR_NONE != ret, error);
331 _D("Host address : %s", host_address);
333 ret = _handle_query(request);
334 goto_if(IOTCON_ERROR_NONE != ret, error);
336 ret = _handle_request_by_crud_type(request, resource_info);
337 goto_if(0 != ret, error);
339 ret = _handle_observer(request, resource_info->observers);
340 goto_if(0 != ret, error);
345 _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
348 static void _copy_file(const char *in_filename, const char *out_filename)
350 char buf[BUFSIZE] = { 0, };
355 ret_if(!in_filename);
356 ret_if(!out_filename);
358 in = fopen(in_filename, "r");
361 out = fopen(out_filename, "w");
362 goto_if(!out, error);
365 while ((nread = fread(buf, 1, sizeof(buf), in)) > 0) {
366 if (fwrite (buf, 1, nread, out) < nread) {
367 _E("critical error to copy a file");
381 int connectivity_init(void)
385 _copy_file(CBOR_FILE_IN_RES, CBOR_FILE_IN_DATA);
387 ret = iotcon_initialize(CBOR_FILE_IN_DATA);
388 retv_if(IOTCON_ERROR_NONE != ret, -1);
390 ret = iotcon_set_device_name(ULTRASONIC_RESOURCE_TYPE);
391 goto_if(IOTCON_ERROR_NONE != ret, error);
396 iotcon_deinitialize();
400 int connectivity_fini(void)
402 iotcon_deinitialize();
406 void connectivity_unset_resource(connectivity_resource_s *resource_info)
408 ret_if(!resource_info);
409 if (resource_info->observers) iotcon_observers_destroy(resource_info->observers);
410 if (resource_info->res) iotcon_resource_destroy(resource_info->res);
414 int connectivity_set_resource(const char *uri_path, const char *type, connectivity_resource_s **out_resource_info)
416 iotcon_resource_types_h resource_types = NULL;
417 iotcon_resource_interfaces_h ifaces = NULL;
418 connectivity_resource_s *resource_info = NULL;
422 resource_info = calloc(1, sizeof(connectivity_resource_s));
423 retv_if(!resource_info, -1);
424 *out_resource_info = resource_info;
426 ret = iotcon_resource_types_create(&resource_types);
427 goto_if(IOTCON_ERROR_NONE != ret, error);
429 ret = iotcon_resource_types_add(resource_types, type);
430 goto_if(IOTCON_ERROR_NONE != ret, error);
432 ret = iotcon_resource_interfaces_create(&ifaces);
433 goto_if(IOTCON_ERROR_NONE != ret, error);
435 ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_DEFAULT);
436 goto_if(IOTCON_ERROR_NONE != ret, error);
438 ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_BATCH);
439 goto_if(IOTCON_ERROR_NONE != ret, error);
442 IOTCON_RESOURCE_DISCOVERABLE |
443 IOTCON_RESOURCE_OBSERVABLE |
444 IOTCON_RESOURCE_SECURE;
446 ret = iotcon_resource_create(uri_path,
450 _request_resource_handler,
452 &resource_info->res);
453 goto_if(IOTCON_ERROR_NONE != ret, error);
455 ret = iotcon_observers_create(&resource_info->observers);
456 goto_if(IOTCON_ERROR_NONE != ret, error);
458 iotcon_resource_types_destroy(resource_types);
459 iotcon_resource_interfaces_destroy(ifaces);
464 if (ifaces) iotcon_resource_interfaces_destroy(ifaces);
465 if (resource_types) iotcon_resource_types_destroy(resource_types);
466 if (resource_info->res) iotcon_resource_destroy(resource_info->res);
467 if (resource_info) free(resource_info);