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.
31 #include "connectivity.h"
33 #define ULTRASONIC_RESOURCE_TYPE "org.tizen.door"
35 #define URI_PATH_LEN 64
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(iotcon_resource_h res, 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(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_bool(attributes, key, value);
91 goto_if(IOTCON_ERROR_NONE != ret, error);
93 ret = iotcon_representation_set_attributes(representation, attributes);
94 goto_if(IOTCON_ERROR_NONE != ret, error);
96 iotcon_attributes_destroy(attributes);
98 return representation;
101 if (attributes) iotcon_attributes_destroy(attributes);
102 if (representation) iotcon_representation_destroy(representation);
107 static iotcon_representation_h _create_representation_with_int(iotcon_resource_h res, const char *key, int value)
109 iotcon_attributes_h attributes = NULL;
110 iotcon_representation_h representation = NULL;
111 char *uri_path = NULL;
114 ret = iotcon_resource_get_uri_path(res, &uri_path);
115 retv_if(IOTCON_ERROR_NONE != ret, NULL);
117 ret = iotcon_representation_create(&representation);
118 retv_if(IOTCON_ERROR_NONE != ret, NULL);
120 ret = iotcon_attributes_create(&attributes);
121 goto_if(IOTCON_ERROR_NONE != ret, error);
123 ret = iotcon_representation_set_uri_path(representation, uri_path);
124 goto_if(IOTCON_ERROR_NONE != ret, error);
126 ret = iotcon_attributes_add_int(attributes, "opened", value);
127 goto_if(IOTCON_ERROR_NONE != ret, error);
129 ret = iotcon_representation_set_attributes(representation, attributes);
130 goto_if(IOTCON_ERROR_NONE != ret, error);
132 iotcon_attributes_destroy(attributes);
134 return representation;
137 if (attributes) iotcon_attributes_destroy(attributes);
138 if (representation) iotcon_representation_destroy(representation);
143 static iotcon_representation_h _create_representation_with_double(iotcon_resource_h res, const char *key, double value)
145 iotcon_attributes_h attributes = NULL;
146 iotcon_representation_h representation = NULL;
147 char *uri_path = NULL;
150 ret = iotcon_resource_get_uri_path(res, &uri_path);
151 retv_if(IOTCON_ERROR_NONE != ret, NULL);
153 ret = iotcon_representation_create(&representation);
154 retv_if(IOTCON_ERROR_NONE != ret, NULL);
156 ret = iotcon_attributes_create(&attributes);
157 goto_if(IOTCON_ERROR_NONE != ret, error);
159 ret = iotcon_representation_set_uri_path(representation, uri_path);
160 goto_if(IOTCON_ERROR_NONE != ret, error);
162 ret = iotcon_attributes_add_double(attributes, "opened", value);
163 goto_if(IOTCON_ERROR_NONE != ret, error);
165 ret = iotcon_representation_set_attributes(representation, attributes);
166 goto_if(IOTCON_ERROR_NONE != ret, error);
168 iotcon_attributes_destroy(attributes);
170 return representation;
173 if (attributes) iotcon_attributes_destroy(attributes);
174 if (representation) iotcon_representation_destroy(representation);
179 int connectivity_notify_bool(connectivity_resource_s *resource_info, const char *key, bool value)
181 iotcon_representation_h representation;
184 retv_if(!resource_info, -1);
185 retv_if(!resource_info->observers, -1);
187 _D("Notify the value[%d]", value);
189 representation = _create_representation_with_bool(resource_info->res, key, value);
190 retv_if(!representation, -1);
192 ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
193 retv_if(IOTCON_ERROR_NONE != ret, -1);
195 _destroy_representation(representation);
200 int connectivity_notify_int(connectivity_resource_s *resource_info, const char *key, int value)
202 iotcon_representation_h representation;
205 retv_if(!resource_info, -1);
206 retv_if(!resource_info->observers, -1);
208 _D("Notify the value[%d]", value);
210 representation = _create_representation_with_int(resource_info->res, key, value);
211 retv_if(!representation, -1);
213 ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
214 retv_if(IOTCON_ERROR_NONE != ret, -1);
216 _destroy_representation(representation);
221 int connectivity_notify_double(connectivity_resource_s *resource_info, const char *key, double value)
223 iotcon_representation_h representation;
226 retv_if(!resource_info, -1);
227 retv_if(!resource_info->observers, -1);
229 _D("Notify the value[%f]", value);
231 representation = _create_representation_with_double(resource_info->res, key, value);
232 retv_if(!representation, -1);
234 ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
235 retv_if(IOTCON_ERROR_NONE != ret, -1);
237 _destroy_representation(representation);
242 static bool _query_cb(const char *key, const char *value, void *user_data)
244 _D("Key : [%s], Value : [%s]", key, value);
246 return IOTCON_FUNC_CONTINUE;
249 static int _handle_query(iotcon_request_h request)
251 iotcon_query_h query = NULL;
254 ret = iotcon_request_get_query(request, &query);
255 retv_if(IOTCON_ERROR_NONE != ret, -1);
257 if (query) iotcon_query_foreach(query, _query_cb, NULL);
262 static int _handle_request_by_crud_type(iotcon_request_h request, connectivity_resource_s *resource_info)
264 iotcon_request_type_e type;
267 ret = iotcon_request_get_request_type(request, &type);
268 retv_if(IOTCON_ERROR_NONE != ret, -1);
271 case IOTCON_REQUEST_GET:
272 _I("Do not support 'get' query");
274 case IOTCON_REQUEST_PUT:
275 _I("Do not support 'put' query");
277 case IOTCON_REQUEST_POST:
278 _I("Do not support 'post' query");
280 case IOTCON_REQUEST_DELETE:
281 _I("Do not support 'delete' query");
284 _E("Cannot reach here");
288 retv_if(0 != ret, -1);
293 static int _handle_observer(iotcon_request_h request, iotcon_observers_h observers)
295 iotcon_observe_type_e observe_type;
299 ret = iotcon_request_get_observe_type(request, &observe_type);
300 retv_if(IOTCON_ERROR_NONE != ret, -1);
302 if (IOTCON_OBSERVE_REGISTER == observe_type) {
303 ret = iotcon_request_get_observe_id(request, &observe_id);
304 retv_if(IOTCON_ERROR_NONE != ret, -1);
306 _I("Add an observer : %d", observe_id);
308 ret = iotcon_observers_add(observers, observe_id);
309 retv_if(IOTCON_ERROR_NONE != ret, -1);
310 } else if (IOTCON_OBSERVE_DEREGISTER == observe_type) {
311 ret = iotcon_request_get_observe_id(request, &observe_id);
312 retv_if(IOTCON_ERROR_NONE != ret, -1);
314 _I("Remove an observer : %d", observe_id);
316 ret = iotcon_observers_remove(observers, observe_id);
317 retv_if(IOTCON_ERROR_NONE != ret, -1);
323 static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data)
325 connectivity_resource_s *resource_info = user_data;
327 char *host_address = NULL;
331 ret = iotcon_request_get_host_address(request, &host_address);
332 goto_if(IOTCON_ERROR_NONE != ret, error);
334 _D("Host address : %s", host_address);
336 ret = _handle_query(request);
337 goto_if(IOTCON_ERROR_NONE != ret, error);
339 ret = _handle_request_by_crud_type(request, resource_info);
340 goto_if(0 != ret, error);
342 ret = _handle_observer(request, resource_info->observers);
343 goto_if(0 != ret, error);
348 _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
351 static void _copy_file(const char *in_filename, const char *out_filename)
353 char buf[BUFSIZE] = { 0, };
358 ret_if(!in_filename);
359 ret_if(!out_filename);
361 in = fopen(in_filename, "r");
364 out = fopen(out_filename, "w");
365 goto_if(!out, error);
368 while ((nread = fread(buf, 1, sizeof(buf), in)) > 0) {
369 if (fwrite (buf, 1, nread, out) < nread) {
370 _E("critical error to copy a file");
384 int connectivity_init(void)
388 _copy_file(CBOR_FILE_IN_RES, CBOR_FILE_IN_DATA);
390 ret = iotcon_initialize(CBOR_FILE_IN_DATA);
391 retv_if(IOTCON_ERROR_NONE != ret, -1);
393 ret = iotcon_set_device_name(ULTRASONIC_RESOURCE_TYPE);
394 goto_if(IOTCON_ERROR_NONE != ret, error);
399 iotcon_deinitialize();
403 int connectivity_fini(void)
405 iotcon_deinitialize();
409 void connectivity_unset_resource(connectivity_resource_s *resource_info)
411 ret_if(!resource_info);
412 if (resource_info->observers) iotcon_observers_destroy(resource_info->observers);
413 if (resource_info->res) iotcon_resource_destroy(resource_info->res);
417 static int _get_default_uri_path_in_conf(char *buf, int size)
422 in = fopen(CONF_FILE, "r");
425 nread = fread(buf, 1, size, in);
427 _I("No contents in the conf.");
431 if (buf[nread - 1] == '\n')
432 buf[nread - 1] = '\0';
439 int connectivity_set_resource(const char *uri_path, const char *type, connectivity_resource_s **out_resource_info)
441 iotcon_resource_types_h resource_types = NULL;
442 iotcon_resource_interfaces_h ifaces = NULL;
443 connectivity_resource_s *resource_info = NULL;
446 const char *final_uri_path = NULL;
447 char default_uri_path[URI_PATH_LEN] = { 0, };
450 final_uri_path = uri_path;
452 ret = _get_default_uri_path_in_conf(default_uri_path, URI_PATH_LEN);
453 retv_if(ret < 0, -1);
454 final_uri_path = default_uri_path;
457 _D("uri path : [%s]", final_uri_path);
459 resource_info = calloc(1, sizeof(connectivity_resource_s));
460 retv_if(!resource_info, -1);
461 *out_resource_info = resource_info;
463 ret = iotcon_resource_types_create(&resource_types);
464 goto_if(IOTCON_ERROR_NONE != ret, error);
466 ret = iotcon_resource_types_add(resource_types, type);
467 goto_if(IOTCON_ERROR_NONE != ret, error);
469 ret = iotcon_resource_interfaces_create(&ifaces);
470 goto_if(IOTCON_ERROR_NONE != ret, error);
472 ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_DEFAULT);
473 goto_if(IOTCON_ERROR_NONE != ret, error);
475 ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_BATCH);
476 goto_if(IOTCON_ERROR_NONE != ret, error);
479 IOTCON_RESOURCE_DISCOVERABLE |
480 IOTCON_RESOURCE_OBSERVABLE |
481 IOTCON_RESOURCE_SECURE;
483 ret = iotcon_resource_create(final_uri_path,
487 _request_resource_handler,
489 &resource_info->res);
490 goto_if(IOTCON_ERROR_NONE != ret, error);
492 ret = iotcon_observers_create(&resource_info->observers);
493 goto_if(IOTCON_ERROR_NONE != ret, error);
495 iotcon_resource_types_destroy(resource_types);
496 iotcon_resource_interfaces_destroy(ifaces);
501 if (ifaces) iotcon_resource_interfaces_destroy(ifaces);
502 if (resource_types) iotcon_resource_types_destroy(resource_types);
503 if (resource_info->res) iotcon_resource_destroy(resource_info->res);
504 if (resource_info) free(resource_info);