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"
34 #define CBOR_FILE_IN_RES "/home/owner/apps_rw/org.tizen.position-finder-server/res/iotcon-test-svr-db-server.dat"
35 #define CBOR_FILE_IN_DATA "/home/owner/apps_rw/org.tizen.position-finder-server/data/iotcon-test-svr-db-server.dat"
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(iotcon_resource_h res, 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(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_bool(attributes, key, value);
92 goto_if(IOTCON_ERROR_NONE != ret, error);
94 ret = iotcon_representation_set_attributes(representation, attributes);
95 goto_if(IOTCON_ERROR_NONE != ret, error);
97 iotcon_attributes_destroy(attributes);
99 return representation;
102 if (attributes) iotcon_attributes_destroy(attributes);
103 if (representation) iotcon_representation_destroy(representation);
108 static iotcon_representation_h _create_representation_with_int(iotcon_resource_h res, const char *key, int value)
110 iotcon_attributes_h attributes = NULL;
111 iotcon_representation_h representation = NULL;
112 char *uri_path = NULL;
115 ret = iotcon_resource_get_uri_path(res, &uri_path);
116 retv_if(IOTCON_ERROR_NONE != ret, NULL);
118 ret = iotcon_representation_create(&representation);
119 retv_if(IOTCON_ERROR_NONE != ret, NULL);
121 ret = iotcon_attributes_create(&attributes);
122 goto_if(IOTCON_ERROR_NONE != ret, error);
124 ret = iotcon_representation_set_uri_path(representation, uri_path);
125 goto_if(IOTCON_ERROR_NONE != ret, error);
127 ret = iotcon_attributes_add_int(attributes, "opened", value);
128 goto_if(IOTCON_ERROR_NONE != ret, error);
130 ret = iotcon_representation_set_attributes(representation, attributes);
131 goto_if(IOTCON_ERROR_NONE != ret, error);
133 iotcon_attributes_destroy(attributes);
135 return representation;
138 if (attributes) iotcon_attributes_destroy(attributes);
139 if (representation) iotcon_representation_destroy(representation);
144 static iotcon_representation_h _create_representation_with_double(iotcon_resource_h res, const char *key, double value)
146 iotcon_attributes_h attributes = NULL;
147 iotcon_representation_h representation = NULL;
148 char *uri_path = NULL;
151 ret = iotcon_resource_get_uri_path(res, &uri_path);
152 retv_if(IOTCON_ERROR_NONE != ret, NULL);
154 ret = iotcon_representation_create(&representation);
155 retv_if(IOTCON_ERROR_NONE != ret, NULL);
157 ret = iotcon_attributes_create(&attributes);
158 goto_if(IOTCON_ERROR_NONE != ret, error);
160 ret = iotcon_representation_set_uri_path(representation, uri_path);
161 goto_if(IOTCON_ERROR_NONE != ret, error);
163 ret = iotcon_attributes_add_double(attributes, "opened", value);
164 goto_if(IOTCON_ERROR_NONE != ret, error);
166 ret = iotcon_representation_set_attributes(representation, attributes);
167 goto_if(IOTCON_ERROR_NONE != ret, error);
169 iotcon_attributes_destroy(attributes);
171 return representation;
174 if (attributes) iotcon_attributes_destroy(attributes);
175 if (representation) iotcon_representation_destroy(representation);
180 int connectivity_notify_bool(connectivity_resource_s *resource_info, const char *key, bool value)
182 iotcon_representation_h representation;
185 retv_if(!resource_info, -1);
186 retv_if(!resource_info->observers, -1);
188 _D("Notify the value[%d]", value);
190 representation = _create_representation_with_bool(resource_info->res, key, value);
191 retv_if(!representation, -1);
193 ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
194 retv_if(IOTCON_ERROR_NONE != ret, -1);
196 _destroy_representation(representation);
201 int connectivity_notify_int(connectivity_resource_s *resource_info, const char *key, int value)
203 iotcon_representation_h representation;
206 retv_if(!resource_info, -1);
207 retv_if(!resource_info->observers, -1);
209 _D("Notify the value[%d]", value);
211 representation = _create_representation_with_int(resource_info->res, key, value);
212 retv_if(!representation, -1);
214 ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
215 retv_if(IOTCON_ERROR_NONE != ret, -1);
217 _destroy_representation(representation);
222 int connectivity_notify_double(connectivity_resource_s *resource_info, const char *key, double value)
224 iotcon_representation_h representation;
227 retv_if(!resource_info, -1);
228 retv_if(!resource_info->observers, -1);
230 _D("Notify the value[%f]", value);
232 representation = _create_representation_with_double(resource_info->res, key, value);
233 retv_if(!representation, -1);
235 ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
236 retv_if(IOTCON_ERROR_NONE != ret, -1);
238 _destroy_representation(representation);
243 static bool _query_cb(const char *key, const char *value, void *user_data)
245 _D("Key : [%s], Value : [%s]", key, value);
247 return IOTCON_FUNC_CONTINUE;
250 static int _handle_query(iotcon_request_h request)
252 iotcon_query_h query = NULL;
255 ret = iotcon_request_get_query(request, &query);
256 retv_if(IOTCON_ERROR_NONE != ret, -1);
258 if (query) iotcon_query_foreach(query, _query_cb, NULL);
263 static int _handle_request_by_crud_type(iotcon_request_h request, connectivity_resource_s *resource_info)
265 iotcon_request_type_e type;
268 ret = iotcon_request_get_request_type(request, &type);
269 retv_if(IOTCON_ERROR_NONE != ret, -1);
272 case IOTCON_REQUEST_GET:
273 _I("Do not support 'get' query");
275 case IOTCON_REQUEST_PUT:
276 _I("Do not support 'put' query");
278 case IOTCON_REQUEST_POST:
279 _I("Do not support 'post' query");
281 case IOTCON_REQUEST_DELETE:
282 _I("Do not support 'delete' query");
285 _E("Cannot reach here");
289 retv_if(0 != ret, -1);
294 static int _handle_observer(iotcon_request_h request, iotcon_observers_h observers)
296 iotcon_observe_type_e observe_type;
300 ret = iotcon_request_get_observe_type(request, &observe_type);
301 retv_if(IOTCON_ERROR_NONE != ret, -1);
303 if (IOTCON_OBSERVE_REGISTER == observe_type) {
304 ret = iotcon_request_get_observe_id(request, &observe_id);
305 retv_if(IOTCON_ERROR_NONE != ret, -1);
307 _I("Add an observer : %d", observe_id);
309 ret = iotcon_observers_add(observers, observe_id);
310 retv_if(IOTCON_ERROR_NONE != ret, -1);
311 } else if (IOTCON_OBSERVE_DEREGISTER == observe_type) {
312 ret = iotcon_request_get_observe_id(request, &observe_id);
313 retv_if(IOTCON_ERROR_NONE != ret, -1);
315 _I("Remove an observer : %d", observe_id);
317 ret = iotcon_observers_remove(observers, observe_id);
318 retv_if(IOTCON_ERROR_NONE != ret, -1);
324 static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data)
326 connectivity_resource_s *resource_info = user_data;
328 char *host_address = NULL;
332 ret = iotcon_request_get_host_address(request, &host_address);
333 goto_if(IOTCON_ERROR_NONE != ret, error);
335 _D("Host address : %s", host_address);
337 ret = _handle_query(request);
338 goto_if(IOTCON_ERROR_NONE != ret, error);
340 ret = _handle_request_by_crud_type(request, resource_info);
341 goto_if(0 != ret, error);
343 ret = _handle_observer(request, resource_info->observers);
344 goto_if(0 != ret, error);
349 _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
352 static void _copy_file(const char *in_filename, const char *out_filename)
354 char buf[BUFSIZE] = { 0, };
359 ret_if(!in_filename);
360 ret_if(!out_filename);
362 in = fopen(in_filename, "r");
365 out = fopen(out_filename, "w");
366 goto_if(!out, error);
369 while ((nread = fread(buf, 1, sizeof(buf), in)) > 0) {
370 if (fwrite (buf, 1, nread, out) < nread) {
371 _E("critical error to copy a file");
385 int connectivity_init(void)
389 _copy_file(CBOR_FILE_IN_RES, CBOR_FILE_IN_DATA);
391 ret = iotcon_initialize(CBOR_FILE_IN_DATA);
392 retv_if(IOTCON_ERROR_NONE != ret, -1);
394 ret = iotcon_set_device_name(ULTRASONIC_RESOURCE_TYPE);
395 goto_if(IOTCON_ERROR_NONE != ret, error);
400 iotcon_deinitialize();
404 int connectivity_fini(void)
406 iotcon_deinitialize();
410 void connectivity_unset_resource(connectivity_resource_s *resource_info)
412 ret_if(!resource_info);
413 if (resource_info->observers) iotcon_observers_destroy(resource_info->observers);
414 if (resource_info->res) iotcon_resource_destroy(resource_info->res);
418 int connectivity_set_resource(const char *uri_path, const char *type, connectivity_resource_s **out_resource_info)
420 iotcon_resource_types_h resource_types = NULL;
421 iotcon_resource_interfaces_h ifaces = NULL;
422 connectivity_resource_s *resource_info = NULL;
426 resource_info = calloc(1, sizeof(connectivity_resource_s));
427 retv_if(!resource_info, -1);
428 *out_resource_info = resource_info;
430 ret = iotcon_resource_types_create(&resource_types);
431 goto_if(IOTCON_ERROR_NONE != ret, error);
433 ret = iotcon_resource_types_add(resource_types, type);
434 goto_if(IOTCON_ERROR_NONE != ret, error);
436 ret = iotcon_resource_interfaces_create(&ifaces);
437 goto_if(IOTCON_ERROR_NONE != ret, error);
439 ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_DEFAULT);
440 goto_if(IOTCON_ERROR_NONE != ret, error);
442 ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_BATCH);
443 goto_if(IOTCON_ERROR_NONE != ret, error);
446 IOTCON_RESOURCE_DISCOVERABLE |
447 IOTCON_RESOURCE_OBSERVABLE |
448 IOTCON_RESOURCE_SECURE;
450 ret = iotcon_resource_create(uri_path,
454 _request_resource_handler,
456 &resource_info->res);
457 goto_if(IOTCON_ERROR_NONE != ret, error);
459 ret = iotcon_observers_create(&resource_info->observers);
460 goto_if(IOTCON_ERROR_NONE != ret, error);
462 iotcon_resource_types_destroy(resource_types);
463 iotcon_resource_interfaces_destroy(ifaces);
468 if (ifaces) iotcon_resource_interfaces_destroy(ifaces);
469 if (resource_types) iotcon_resource_types_destroy(resource_types);
470 if (resource_info->res) iotcon_resource_destroy(resource_info->res);
471 if (resource_info) free(resource_info);