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.
27 #include <app_common.h>
30 #include "connectivity.h"
32 #define ULTRASONIC_RESOURCE_TYPE "org.tizen.door"
34 #define URI_PATH_LEN 64
35 #define URI_PATH "/door/1"
38 static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data);
40 static int _send_response(iotcon_request_h request, iotcon_representation_h representation, iotcon_response_result_e result)
43 iotcon_response_h response;
45 ret = iotcon_response_create(request, &response);
46 retv_if(IOTCON_ERROR_NONE != ret, -1);
48 ret = iotcon_response_set_result(response, result);
49 goto_if(IOTCON_ERROR_NONE != ret, error);
51 ret = iotcon_response_set_representation(response, representation);
52 goto_if(IOTCON_ERROR_NONE != ret, error);
54 ret = iotcon_response_send(response);
55 goto_if(IOTCON_ERROR_NONE != ret, error);
57 iotcon_response_destroy(response);
62 iotcon_response_destroy(response);
66 static void _destroy_representation(iotcon_representation_h representation)
68 ret_if(!representation);
69 iotcon_representation_destroy(representation);
72 static iotcon_representation_h _create_representation_with_bool(connectivity_resource_s *resource_info, const char *key, bool value)
74 iotcon_attributes_h attributes = NULL;
75 iotcon_representation_h representation = NULL;
76 char *uri_path = NULL;
79 ret = iotcon_resource_get_uri_path(resource_info->res, &uri_path);
80 retv_if(IOTCON_ERROR_NONE != ret, NULL);
82 ret = iotcon_representation_create(&representation);
83 retv_if(IOTCON_ERROR_NONE != ret, NULL);
85 ret = iotcon_attributes_create(&attributes);
86 goto_if(IOTCON_ERROR_NONE != ret, error);
88 ret = iotcon_representation_set_uri_path(representation, uri_path);
89 goto_if(IOTCON_ERROR_NONE != ret, error);
91 ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
92 goto_if(IOTCON_ERROR_NONE != ret, error);
94 ret = iotcon_attributes_add_bool(attributes, key, value);
95 goto_if(IOTCON_ERROR_NONE != ret, error);
97 ret = iotcon_representation_set_attributes(representation, attributes);
98 goto_if(IOTCON_ERROR_NONE != ret, error);
100 iotcon_attributes_destroy(attributes);
102 return representation;
105 if (attributes) iotcon_attributes_destroy(attributes);
106 if (representation) iotcon_representation_destroy(representation);
111 static iotcon_representation_h _create_representation_with_int(connectivity_resource_s *resource_info, const char *key, int value)
113 iotcon_attributes_h attributes = NULL;
114 iotcon_representation_h representation = NULL;
115 char *uri_path = NULL;
118 ret = iotcon_resource_get_uri_path(resource_info->res, &uri_path);
119 retv_if(IOTCON_ERROR_NONE != ret, NULL);
121 ret = iotcon_representation_create(&representation);
122 retv_if(IOTCON_ERROR_NONE != ret, NULL);
124 ret = iotcon_attributes_create(&attributes);
125 goto_if(IOTCON_ERROR_NONE != ret, error);
127 ret = iotcon_representation_set_uri_path(representation, uri_path);
128 goto_if(IOTCON_ERROR_NONE != ret, error);
130 ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
131 goto_if(IOTCON_ERROR_NONE != ret, error);
133 ret = iotcon_attributes_add_int(attributes, key, value);
134 goto_if(IOTCON_ERROR_NONE != ret, error);
136 ret = iotcon_representation_set_attributes(representation, attributes);
137 goto_if(IOTCON_ERROR_NONE != ret, error);
139 iotcon_attributes_destroy(attributes);
141 return representation;
144 if (attributes) iotcon_attributes_destroy(attributes);
145 if (representation) iotcon_representation_destroy(representation);
150 static iotcon_representation_h _create_representation_with_double(connectivity_resource_s *resource_info, const char *key, double value)
152 iotcon_attributes_h attributes = NULL;
153 iotcon_representation_h representation = NULL;
154 char *uri_path = NULL;
157 ret = iotcon_resource_get_uri_path(resource_info->res, &uri_path);
158 retv_if(IOTCON_ERROR_NONE != ret, NULL);
160 ret = iotcon_representation_create(&representation);
161 retv_if(IOTCON_ERROR_NONE != ret, NULL);
163 ret = iotcon_attributes_create(&attributes);
164 goto_if(IOTCON_ERROR_NONE != ret, error);
166 ret = iotcon_representation_set_uri_path(representation, uri_path);
167 goto_if(IOTCON_ERROR_NONE != ret, error);
169 ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
170 goto_if(IOTCON_ERROR_NONE != ret, error);
172 ret = iotcon_attributes_add_double(attributes, key, value);
173 goto_if(IOTCON_ERROR_NONE != ret, error);
175 ret = iotcon_representation_set_attributes(representation, attributes);
176 goto_if(IOTCON_ERROR_NONE != ret, error);
178 iotcon_attributes_destroy(attributes);
180 return representation;
183 if (attributes) iotcon_attributes_destroy(attributes);
184 if (representation) iotcon_representation_destroy(representation);
189 static void _print_iotcon_error(int err_no)
192 case IOTCON_ERROR_NOT_SUPPORTED:
193 _E("IOTCON_ERROR_NOT_SUPPORTED");
195 case IOTCON_ERROR_PERMISSION_DENIED:
196 _E("IOTCON_ERROR_PERMISSION_DENIED");
198 case IOTCON_ERROR_INVALID_PARAMETER:
199 _E("IOTCON_ERROR_INVALID_PARAMETER");
202 _E("Error : [%d]", err_no);
207 int connectivity_notify_bool(connectivity_resource_s *resource_info, const char *key, bool value)
209 iotcon_representation_h representation;
212 retv_if(!resource_info, -1);
213 retv_if(!resource_info->observers, -1);
215 _D("Notify the value[%d]", value);
217 representation = _create_representation_with_bool(resource_info, key, value);
218 retv_if(!representation, -1);
220 ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
221 if (IOTCON_ERROR_NONE != ret) {
222 _I("There are some troubles for notifying value[%d]", ret);
223 _print_iotcon_error(ret);
227 _destroy_representation(representation);
232 int connectivity_notify_int(connectivity_resource_s *resource_info, const char *key, int value)
234 iotcon_representation_h representation;
237 retv_if(!resource_info, -1);
238 retv_if(!resource_info->observers, -1);
240 _D("Notify the value[%d]", value);
242 representation = _create_representation_with_int(resource_info, key, value);
243 retv_if(!representation, -1);
245 ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
246 if (IOTCON_ERROR_NONE != ret) {
247 _I("There are some troubles for notifying value[%d]", ret);
248 _print_iotcon_error(ret);
252 _destroy_representation(representation);
257 int connectivity_notify_double(connectivity_resource_s *resource_info, const char *key, double value)
259 iotcon_representation_h representation;
262 retv_if(!resource_info, -1);
263 retv_if(!resource_info->observers, -1);
265 _D("Notify the value[%f]", value);
267 representation = _create_representation_with_double(resource_info, key, value);
268 retv_if(!representation, -1);
270 ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
271 if (IOTCON_ERROR_NONE != ret) {
272 _I("There are some troubles for notifying value[%d]", ret);
273 _print_iotcon_error(ret);
277 _destroy_representation(representation);
282 static bool _query_cb(const char *key, const char *value, void *user_data)
284 _D("Key : [%s], Value : [%s]", key, value);
286 return IOTCON_FUNC_CONTINUE;
289 static int _handle_query(iotcon_request_h request)
291 iotcon_query_h query = NULL;
294 ret = iotcon_request_get_query(request, &query);
295 retv_if(IOTCON_ERROR_NONE != ret, -1);
297 if (query) iotcon_query_foreach(query, _query_cb, NULL);
302 static int _handle_request_by_crud_type(iotcon_request_h request, connectivity_resource_s *resource_info)
304 iotcon_request_type_e type;
307 ret = iotcon_request_get_request_type(request, &type);
308 retv_if(IOTCON_ERROR_NONE != ret, -1);
311 case IOTCON_REQUEST_GET:
312 _I("Do not support 'get' query");
314 case IOTCON_REQUEST_PUT:
315 _I("Do not support 'put' query");
317 case IOTCON_REQUEST_POST:
318 _I("Do not support 'post' query");
320 case IOTCON_REQUEST_DELETE:
321 _I("Do not support 'delete' query");
324 _E("Cannot reach here");
328 retv_if(0 != ret, -1);
333 static int _handle_observer(iotcon_request_h request, iotcon_observers_h observers)
335 iotcon_observe_type_e observe_type;
339 ret = iotcon_request_get_observe_type(request, &observe_type);
340 retv_if(IOTCON_ERROR_NONE != ret, -1);
342 if (IOTCON_OBSERVE_REGISTER == observe_type) {
343 ret = iotcon_request_get_observe_id(request, &observe_id);
344 retv_if(IOTCON_ERROR_NONE != ret, -1);
346 _I("Add an observer : %d", observe_id);
348 ret = iotcon_observers_add(observers, observe_id);
349 retv_if(IOTCON_ERROR_NONE != ret, -1);
350 } else if (IOTCON_OBSERVE_DEREGISTER == observe_type) {
351 ret = iotcon_request_get_observe_id(request, &observe_id);
352 retv_if(IOTCON_ERROR_NONE != ret, -1);
354 _I("Remove an observer : %d", observe_id);
356 ret = iotcon_observers_remove(observers, observe_id);
357 retv_if(IOTCON_ERROR_NONE != ret, -1);
363 static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data)
365 connectivity_resource_s *resource_info = user_data;
367 char *host_address = NULL;
371 ret = iotcon_request_get_host_address(request, &host_address);
372 goto_if(IOTCON_ERROR_NONE != ret, error);
374 _D("Host address : %s", host_address);
376 ret = _handle_query(request);
377 goto_if(IOTCON_ERROR_NONE != ret, error);
379 ret = _handle_request_by_crud_type(request, resource_info);
380 goto_if(0 != ret, error);
382 ret = _handle_observer(request, resource_info->observers);
383 goto_if(0 != ret, error);
388 _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
391 int connectivity_init(void)
394 char buf[PATH_MAX] = {0,};
397 prefix = app_get_resource_path();
398 retv_if(!prefix, -1);
399 snprintf(buf, sizeof(buf)-1, "%s%s", prefix, "iotcon-test-svr-db-server.dat");
402 ret = iotcon_initialize(buf);
403 retv_if(IOTCON_ERROR_NONE != ret, -1);
405 ret = iotcon_set_device_name(ULTRASONIC_RESOURCE_TYPE);
406 goto_if(IOTCON_ERROR_NONE != ret, error);
411 iotcon_deinitialize();
415 int connectivity_fini(void)
417 iotcon_deinitialize();
421 void connectivity_unset_resource(connectivity_resource_s *resource_info)
423 ret_if(!resource_info);
424 if (resource_info->observers) iotcon_observers_destroy(resource_info->observers);
425 if (resource_info->res) iotcon_resource_destroy(resource_info->res);
426 if (resource_info->path) free(resource_info->path);
430 int connectivity_set_resource(const char *path, const char *type, connectivity_resource_s **out_resource_info)
432 iotcon_resource_types_h resource_types = NULL;
433 iotcon_resource_interfaces_h ifaces = NULL;
434 connectivity_resource_s *resource_info = NULL;
440 retv_if(!out_resource_info, -1);
442 resource_info = calloc(1, sizeof(connectivity_resource_s));
443 retv_if(!resource_info, -1);
445 resource_info->path = strdup(path);
446 goto_if(!resource_info->path, error);
448 _D("Path : [%s]", resource_info->path);
450 ret = iotcon_resource_types_create(&resource_types);
451 goto_if(IOTCON_ERROR_NONE != ret, error);
453 ret = iotcon_resource_types_add(resource_types, type);
454 goto_if(IOTCON_ERROR_NONE != ret, error);
456 ret = iotcon_resource_interfaces_create(&ifaces);
457 goto_if(IOTCON_ERROR_NONE != ret, error);
459 ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_DEFAULT);
460 goto_if(IOTCON_ERROR_NONE != ret, error);
462 ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_BATCH);
463 goto_if(IOTCON_ERROR_NONE != ret, error);
466 IOTCON_RESOURCE_DISCOVERABLE |
467 IOTCON_RESOURCE_OBSERVABLE |
468 IOTCON_RESOURCE_SECURE;
470 ret = iotcon_resource_create(URI_PATH,
474 _request_resource_handler,
476 &resource_info->res);
477 goto_if(IOTCON_ERROR_NONE != ret, error);
479 ret = iotcon_observers_create(&resource_info->observers);
480 goto_if(IOTCON_ERROR_NONE != ret, error);
482 iotcon_resource_types_destroy(resource_types);
483 iotcon_resource_interfaces_destroy(ifaces);
484 *out_resource_info = resource_info;
489 if (ifaces) iotcon_resource_interfaces_destroy(ifaces);
490 if (resource_types) iotcon_resource_types_destroy(resource_types);
491 if (resource_info->res) iotcon_resource_destroy(resource_info->res);
492 if (resource_info->path) free(resource_info->path);
493 if (resource_info) free(resource_info);