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"
33 #define URI_PATH_LEN 64
34 #define URI_PATH "/door/1"
37 static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data);
39 static int _send_response(iotcon_request_h request, iotcon_representation_h representation, iotcon_response_result_e result)
42 iotcon_response_h response;
44 ret = iotcon_response_create(request, &response);
45 retv_if(IOTCON_ERROR_NONE != ret, -1);
47 ret = iotcon_response_set_result(response, result);
48 goto_if(IOTCON_ERROR_NONE != ret, error);
50 ret = iotcon_response_set_representation(response, representation);
51 goto_if(IOTCON_ERROR_NONE != ret, error);
53 ret = iotcon_response_send(response);
54 goto_if(IOTCON_ERROR_NONE != ret, error);
56 iotcon_response_destroy(response);
61 iotcon_response_destroy(response);
65 static void _destroy_representation(iotcon_representation_h representation)
67 ret_if(!representation);
68 iotcon_representation_destroy(representation);
71 static iotcon_representation_h _create_representation_with_bool(connectivity_resource_s *resource_info, const char *key, bool value)
73 iotcon_attributes_h attributes = NULL;
74 iotcon_representation_h representation = NULL;
75 char *uri_path = NULL;
78 ret = iotcon_resource_get_uri_path(resource_info->res, &uri_path);
79 retv_if(IOTCON_ERROR_NONE != ret, NULL);
81 ret = iotcon_representation_create(&representation);
82 retv_if(IOTCON_ERROR_NONE != ret, NULL);
84 ret = iotcon_attributes_create(&attributes);
85 goto_if(IOTCON_ERROR_NONE != ret, error);
87 ret = iotcon_representation_set_uri_path(representation, uri_path);
88 goto_if(IOTCON_ERROR_NONE != ret, error);
90 ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
91 goto_if(IOTCON_ERROR_NONE != ret, error);
93 ret = iotcon_attributes_add_bool(attributes, key, value);
94 goto_if(IOTCON_ERROR_NONE != ret, error);
96 ret = iotcon_representation_set_attributes(representation, attributes);
97 goto_if(IOTCON_ERROR_NONE != ret, error);
99 iotcon_attributes_destroy(attributes);
101 return representation;
104 if (attributes) iotcon_attributes_destroy(attributes);
105 if (representation) iotcon_representation_destroy(representation);
110 static iotcon_representation_h _create_representation_with_int(connectivity_resource_s *resource_info, const char *key, int value)
112 iotcon_attributes_h attributes = NULL;
113 iotcon_representation_h representation = NULL;
114 char *uri_path = NULL;
117 ret = iotcon_resource_get_uri_path(resource_info->res, &uri_path);
118 retv_if(IOTCON_ERROR_NONE != ret, NULL);
120 ret = iotcon_representation_create(&representation);
121 retv_if(IOTCON_ERROR_NONE != ret, NULL);
123 ret = iotcon_attributes_create(&attributes);
124 goto_if(IOTCON_ERROR_NONE != ret, error);
126 ret = iotcon_representation_set_uri_path(representation, uri_path);
127 goto_if(IOTCON_ERROR_NONE != ret, error);
129 ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
130 goto_if(IOTCON_ERROR_NONE != ret, error);
132 ret = iotcon_attributes_add_int(attributes, key, value);
133 goto_if(IOTCON_ERROR_NONE != ret, error);
135 ret = iotcon_representation_set_attributes(representation, attributes);
136 goto_if(IOTCON_ERROR_NONE != ret, error);
138 iotcon_attributes_destroy(attributes);
140 return representation;
143 if (attributes) iotcon_attributes_destroy(attributes);
144 if (representation) iotcon_representation_destroy(representation);
149 static iotcon_representation_h _create_representation_with_double(connectivity_resource_s *resource_info, const char *key, double value)
151 iotcon_attributes_h attributes = NULL;
152 iotcon_representation_h representation = NULL;
153 char *uri_path = NULL;
156 ret = iotcon_resource_get_uri_path(resource_info->res, &uri_path);
157 retv_if(IOTCON_ERROR_NONE != ret, NULL);
159 ret = iotcon_representation_create(&representation);
160 retv_if(IOTCON_ERROR_NONE != ret, NULL);
162 ret = iotcon_attributes_create(&attributes);
163 goto_if(IOTCON_ERROR_NONE != ret, error);
165 ret = iotcon_representation_set_uri_path(representation, uri_path);
166 goto_if(IOTCON_ERROR_NONE != ret, error);
168 ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
169 goto_if(IOTCON_ERROR_NONE != ret, error);
171 ret = iotcon_attributes_add_double(attributes, key, value);
172 goto_if(IOTCON_ERROR_NONE != ret, error);
174 ret = iotcon_representation_set_attributes(representation, attributes);
175 goto_if(IOTCON_ERROR_NONE != ret, error);
177 iotcon_attributes_destroy(attributes);
179 return representation;
182 if (attributes) iotcon_attributes_destroy(attributes);
183 if (representation) iotcon_representation_destroy(representation);
188 static void _print_iotcon_error(int err_no)
191 case IOTCON_ERROR_NOT_SUPPORTED:
192 _E("IOTCON_ERROR_NOT_SUPPORTED");
194 case IOTCON_ERROR_PERMISSION_DENIED:
195 _E("IOTCON_ERROR_PERMISSION_DENIED");
197 case IOTCON_ERROR_INVALID_PARAMETER:
198 _E("IOTCON_ERROR_INVALID_PARAMETER");
201 _E("Error : [%d]", err_no);
206 int connectivity_notify_bool(connectivity_resource_s *resource_info, const char *key, bool value)
208 iotcon_representation_h representation;
211 retv_if(!resource_info, -1);
212 retv_if(!resource_info->observers, -1);
214 _D("Notify the value[%d]", value);
216 representation = _create_representation_with_bool(resource_info, key, value);
217 retv_if(!representation, -1);
219 ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_LOW);
220 if (IOTCON_ERROR_NONE != ret) {
221 _I("There are some troubles for notifying value[%d]", ret);
222 _print_iotcon_error(ret);
226 _destroy_representation(representation);
231 int connectivity_notify_int(connectivity_resource_s *resource_info, const char *key, int value)
233 iotcon_representation_h representation;
236 retv_if(!resource_info, -1);
237 retv_if(!resource_info->observers, -1);
239 _D("Notify the value[%d]", value);
241 representation = _create_representation_with_int(resource_info, key, value);
242 retv_if(!representation, -1);
244 ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_LOW);
245 if (IOTCON_ERROR_NONE != ret) {
246 _I("There are some troubles for notifying value[%d]", ret);
247 _print_iotcon_error(ret);
251 _destroy_representation(representation);
256 int connectivity_notify_double(connectivity_resource_s *resource_info, const char *key, double value)
258 iotcon_representation_h representation;
261 retv_if(!resource_info, -1);
262 retv_if(!resource_info->observers, -1);
264 _D("Notify the value[%f]", value);
266 representation = _create_representation_with_double(resource_info, key, value);
267 retv_if(!representation, -1);
269 ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_LOW);
270 if (IOTCON_ERROR_NONE != ret) {
271 _I("There are some troubles for notifying value[%d]", ret);
272 _print_iotcon_error(ret);
276 _destroy_representation(representation);
281 static bool _query_cb(const char *key, const char *value, void *user_data)
283 _D("Key : [%s], Value : [%s]", key, value);
285 return IOTCON_FUNC_CONTINUE;
288 static int _handle_query(iotcon_request_h request)
290 iotcon_query_h query = NULL;
293 ret = iotcon_request_get_query(request, &query);
294 retv_if(IOTCON_ERROR_NONE != ret, -1);
296 if (query) iotcon_query_foreach(query, _query_cb, NULL);
301 static int _handle_request_by_crud_type(iotcon_request_h request, connectivity_resource_s *resource_info)
303 iotcon_request_type_e type;
306 ret = iotcon_request_get_request_type(request, &type);
307 retv_if(IOTCON_ERROR_NONE != ret, -1);
310 case IOTCON_REQUEST_GET:
311 _I("Do not support 'get' query");
313 case IOTCON_REQUEST_PUT:
314 _I("Do not support 'put' query");
316 case IOTCON_REQUEST_POST:
317 _I("Do not support 'post' query");
319 case IOTCON_REQUEST_DELETE:
320 _I("Do not support 'delete' query");
323 _E("Cannot reach here");
327 retv_if(0 != ret, -1);
332 static int _handle_observer(iotcon_request_h request, iotcon_observers_h observers)
334 iotcon_observe_type_e observe_type;
338 ret = iotcon_request_get_observe_type(request, &observe_type);
339 retv_if(IOTCON_ERROR_NONE != ret, -1);
341 if (IOTCON_OBSERVE_REGISTER == observe_type) {
342 ret = iotcon_request_get_observe_id(request, &observe_id);
343 retv_if(IOTCON_ERROR_NONE != ret, -1);
345 _I("Add an observer : %d", observe_id);
347 ret = iotcon_observers_add(observers, observe_id);
348 retv_if(IOTCON_ERROR_NONE != ret, -1);
349 } else if (IOTCON_OBSERVE_DEREGISTER == observe_type) {
350 ret = iotcon_request_get_observe_id(request, &observe_id);
351 retv_if(IOTCON_ERROR_NONE != ret, -1);
353 _I("Remove an observer : %d", observe_id);
355 ret = iotcon_observers_remove(observers, observe_id);
356 retv_if(IOTCON_ERROR_NONE != ret, -1);
362 static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data)
364 connectivity_resource_s *resource_info = user_data;
366 char *host_address = NULL;
370 ret = iotcon_request_get_host_address(request, &host_address);
371 goto_if(IOTCON_ERROR_NONE != ret, error);
373 _D("Host address : %s", host_address);
375 ret = _handle_query(request);
376 goto_if(IOTCON_ERROR_NONE != ret, error);
378 ret = _handle_request_by_crud_type(request, resource_info);
379 goto_if(0 != ret, error);
381 ret = _handle_observer(request, resource_info->observers);
382 goto_if(0 != ret, error);
387 _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
390 static void _copy_file(const char *in_filename, const char *out_filename)
392 char buf[BUFSIZE] = { 0, };
397 ret_if(!in_filename);
398 ret_if(!out_filename);
400 in = fopen(in_filename, "r");
403 out = fopen(out_filename, "w");
404 goto_if(!out, error);
407 while ((nread = fread(buf, 1, sizeof(buf), in)) > 0) {
408 if (fwrite (buf, 1, nread, out) < nread) {
409 _E("critical error to copy a file");
423 int connectivity_init(void)
427 _copy_file(CBOR_FILE_IN_RES, CBOR_FILE_IN_DATA);
429 ret = iotcon_initialize(CBOR_FILE_IN_DATA);
430 retv_if(IOTCON_ERROR_NONE != ret, -1);
432 ret = iotcon_set_device_name(ULTRASONIC_RESOURCE_TYPE);
433 goto_if(IOTCON_ERROR_NONE != ret, error);
438 iotcon_deinitialize();
442 int connectivity_fini(void)
444 iotcon_deinitialize();
448 void connectivity_unset_resource(connectivity_resource_s *resource_info)
450 ret_if(!resource_info);
451 if (resource_info->observers) iotcon_observers_destroy(resource_info->observers);
452 if (resource_info->res) iotcon_resource_destroy(resource_info->res);
453 if (resource_info->path) free(resource_info->path);
457 static int _get_default_path_in_conf(char *buf, int size)
462 in = fopen(CONF_FILE, "r");
465 nread = fread(buf, 1, size, in);
467 _I("No contents in the conf.");
471 if (buf[nread - 1] == '\n')
472 buf[nread - 1] = '\0';
479 int connectivity_set_resource(const char *path, const char *type, connectivity_resource_s **out_resource_info)
481 iotcon_resource_types_h resource_types = NULL;
482 iotcon_resource_interfaces_h ifaces = NULL;
483 connectivity_resource_s *resource_info = NULL;
486 char default_path[URI_PATH_LEN] = { 0, };
488 resource_info = calloc(1, sizeof(connectivity_resource_s));
489 retv_if(!resource_info, -1);
492 resource_info->path = strdup(path);
494 ret = _get_default_path_in_conf(default_path, URI_PATH_LEN);
495 retv_if(ret < 0, -1);
496 resource_info->path = strdup(default_path);
498 goto_if(!resource_info->path, error);
500 _D("Path : [%s]", resource_info->path);
502 ret = iotcon_resource_types_create(&resource_types);
503 goto_if(IOTCON_ERROR_NONE != ret, error);
505 ret = iotcon_resource_types_add(resource_types, type);
506 goto_if(IOTCON_ERROR_NONE != ret, error);
508 ret = iotcon_resource_interfaces_create(&ifaces);
509 goto_if(IOTCON_ERROR_NONE != ret, error);
511 ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_DEFAULT);
512 goto_if(IOTCON_ERROR_NONE != ret, error);
514 ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_BATCH);
515 goto_if(IOTCON_ERROR_NONE != ret, error);
518 IOTCON_RESOURCE_DISCOVERABLE |
519 IOTCON_RESOURCE_OBSERVABLE |
520 IOTCON_RESOURCE_SECURE;
522 ret = iotcon_resource_create(URI_PATH,
526 _request_resource_handler,
528 &resource_info->res);
529 goto_if(IOTCON_ERROR_NONE != ret, error);
531 ret = iotcon_observers_create(&resource_info->observers);
532 goto_if(IOTCON_ERROR_NONE != ret, error);
534 iotcon_resource_types_destroy(resource_types);
535 iotcon_resource_interfaces_destroy(ifaces);
536 *out_resource_info = resource_info;
541 if (ifaces) iotcon_resource_interfaces_destroy(ifaces);
542 if (resource_types) iotcon_resource_types_destroy(resource_types);
543 if (resource_info->res) iotcon_resource_destroy(resource_info->res);
544 if (resource_info->path) free(resource_info->path);
545 if (resource_info) free(resource_info);