2 * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
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 Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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_1_URI "/door/1"
32 #define ULTRASONIC_RESOURCE_2_URI "/door/2"
33 #define ULTRASONIC_RESOURCE_TYPE "org.tizen.door"
35 struct connectivity_resource {
36 iotcon_resource_h res;
37 iotcon_observers_h observers;
39 static bool _resource_created;
40 static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data);
42 static int _send_response(iotcon_request_h request, iotcon_representation_h representation, iotcon_response_result_e result)
45 iotcon_response_h response;
47 ret = iotcon_response_create(request, &response);
48 retv_if(IOTCON_ERROR_NONE != ret, -1);
50 ret = iotcon_response_set_result(response, result);
51 goto_if(IOTCON_ERROR_NONE != ret, error);
53 ret = iotcon_response_set_representation(response, representation);
54 goto_if(IOTCON_ERROR_NONE != ret, error);
56 ret = iotcon_response_send(response);
57 goto_if(IOTCON_ERROR_NONE != ret, error);
59 iotcon_response_destroy(response);
64 iotcon_response_destroy(response);
68 static void _destroy_representation(iotcon_representation_h representation)
70 ret_if(!representation);
71 iotcon_representation_destroy(representation);
74 static iotcon_representation_h _create_representation_with_attribute(iotcon_resource_h res, bool value)
76 iotcon_attributes_h attributes = NULL;
77 iotcon_representation_h representation = NULL;
78 char *uri_path = NULL;
81 ret = iotcon_resource_get_uri_path(res, &uri_path);
82 retv_if(IOTCON_ERROR_NONE != ret, NULL);
84 ret = iotcon_representation_create(&representation);
85 retv_if(IOTCON_ERROR_NONE != ret, NULL);
87 ret = iotcon_attributes_create(&attributes);
88 goto_if(IOTCON_ERROR_NONE != ret, error);
90 ret = iotcon_representation_set_uri_path(representation, uri_path);
91 goto_if(IOTCON_ERROR_NONE != ret, error);
93 ret = iotcon_attributes_add_bool(attributes, "opened", 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 int _handle_get_request(iotcon_resource_h res, iotcon_request_h request)
112 iotcon_representation_h representation;
116 /* FIXME : We need to check the value of sensors */
117 representation = _create_representation_with_attribute(res, (bool)value);
118 retv_if(!representation, -1);
120 ret = _send_response(request, representation, IOTCON_RESPONSE_OK);
121 goto_if(0 != ret, error);
123 _destroy_representation(representation);
128 _destroy_representation(representation);
132 static int _get_value_from_representation(iotcon_representation_h representation, bool *value)
134 iotcon_attributes_h attributes;
137 ret = iotcon_representation_get_attributes(representation, &attributes);
138 retv_if(IOTCON_ERROR_NONE != ret, -1);
140 ret = iotcon_attributes_get_bool(attributes, "opened", value);
141 retv_if(IOTCON_ERROR_NONE != ret, -1);
146 static int _set_value_into_thing(iotcon_representation_h representation, bool value)
148 /* FIXME : We need to set the value into the thing */
152 static int _handle_put_request(connectivity_resource_s *resource_info, iotcon_request_h request)
154 iotcon_representation_h req_repr, resp_repr;
160 ret = iotcon_request_get_representation(request, &req_repr);
161 retv_if(IOTCON_ERROR_NONE != ret, -1);
163 ret = _get_value_from_representation(req_repr, &value);
164 retv_if(0 != ret, -1);
166 ret = _set_value_into_thing(req_repr, value);
167 retv_if(0 != ret, -1);
169 resp_repr = _create_representation_with_attribute(resource_info->res, (bool)value);
170 retv_if(NULL == resp_repr, -1);
172 ret = _send_response(request, resp_repr, IOTCON_RESPONSE_OK);
173 goto_if(0 != ret, error);
175 ret = iotcon_resource_notify(resource_info->res, resp_repr, resource_info->observers, IOTCON_QOS_HIGH);
176 goto_if(IOTCON_ERROR_NONE != ret, error);
178 _destroy_representation(resp_repr);
183 _destroy_representation(resp_repr);
187 int connectivity_notify(connectivity_resource_s *user_data, int value)
189 iotcon_representation_h representation;
190 connectivity_resource_s *resource_info = user_data;
193 retv_if(!resource_info, -1);
194 retv_if(!resource_info->observers, -1);
196 _D("Notify the value[%d]", value);
198 representation = _create_representation_with_attribute(resource_info->res, (bool)value);
199 retv_if(!representation, -1);
201 ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
202 retv_if(IOTCON_ERROR_NONE != ret, -1);
204 _destroy_representation(representation);
209 static int _handle_post_request(connectivity_resource_s *resource_info, iotcon_request_h request)
211 iotcon_attributes_h resp_attributes = NULL;
212 iotcon_representation_h resp_repr = NULL;
213 connectivity_resource_s *new_resource_info = NULL;
218 if (_resource_created) {
219 _E("Resource(%s) is already created", ULTRASONIC_RESOURCE_2_URI);
223 new_resource_info = calloc(1, sizeof(connectivity_resource_s));
224 retv_if(!new_resource_info, -1);
226 ret = connectivity_create_resource(ULTRASONIC_RESOURCE_2_URI, ULTRASONIC_RESOURCE_TYPE, &new_resource_info);
227 retv_if(0 != ret, -1);
229 _resource_created = true;
231 ret = iotcon_representation_create(&resp_repr);
232 retv_if(IOTCON_ERROR_NONE != ret, -1);
234 ret = iotcon_attributes_create(&resp_attributes);
235 goto_if(IOTCON_ERROR_NONE != ret, error);
237 ret = iotcon_attributes_add_str(resp_attributes, "createduripath", ULTRASONIC_RESOURCE_2_URI);
238 goto_if(IOTCON_ERROR_NONE != ret, error);
240 ret = iotcon_representation_set_attributes(resp_repr, resp_attributes);
241 goto_if(IOTCON_ERROR_NONE != ret, error);
243 iotcon_attributes_destroy(resp_attributes);
245 ret = _send_response(request, resp_repr, IOTCON_RESPONSE_RESOURCE_CREATED);
246 goto_if(0 != ret, error);
248 iotcon_representation_destroy(resp_repr);
253 if (resp_attributes) iotcon_attributes_destroy(resp_attributes);
254 iotcon_representation_destroy(resp_repr);
258 static int _handle_delete_request(iotcon_resource_h resource, iotcon_request_h request)
262 _D("DELETE request");
264 ret = iotcon_resource_destroy(resource);
265 retv_if(IOTCON_ERROR_NONE != ret, -1);
267 ret = _send_response(request, NULL, IOTCON_RESPONSE_RESOURCE_DELETED);
268 retv_if(0 != ret, -1);
273 static bool _query_cb(const char *key, const char *value, void *user_data)
275 _D("Key : [%s], Value : [%s]", key, value);
277 return IOTCON_FUNC_CONTINUE;
280 static int _handle_query(iotcon_request_h request)
282 iotcon_query_h query = NULL;
285 ret = iotcon_request_get_query(request, &query);
286 retv_if(IOTCON_ERROR_NONE != ret, -1);
288 if (query) iotcon_query_foreach(query, _query_cb, NULL);
293 static int _handle_request_by_crud_type(iotcon_request_h request, connectivity_resource_s *resource_info)
295 iotcon_request_type_e type;
298 ret = iotcon_request_get_request_type(request, &type);
299 retv_if(IOTCON_ERROR_NONE != ret, -1);
302 case IOTCON_REQUEST_GET:
303 ret = _handle_get_request(resource_info->res, request);
305 case IOTCON_REQUEST_PUT:
306 ret = _handle_put_request(resource_info, request);
308 case IOTCON_REQUEST_POST:
309 ret = _handle_post_request(resource_info, request);
311 case IOTCON_REQUEST_DELETE:
312 ret = _handle_delete_request(resource_info->res, request);
315 _E("Cannot reach here");
319 retv_if(0 != ret, -1);
324 static int _handle_observer(iotcon_request_h request, iotcon_observers_h observers)
326 iotcon_observe_type_e observe_type;
330 ret = iotcon_request_get_observe_type(request, &observe_type);
331 retv_if(IOTCON_ERROR_NONE != ret, -1);
333 if (IOTCON_OBSERVE_REGISTER == observe_type) {
334 ret = iotcon_request_get_observe_id(request, &observe_id);
335 retv_if(IOTCON_ERROR_NONE != ret, -1);
337 ret = iotcon_observers_add(observers, observe_id);
338 retv_if(IOTCON_ERROR_NONE != ret, -1);
339 } else if (IOTCON_OBSERVE_DEREGISTER == observe_type) {
340 ret = iotcon_request_get_observe_id(request, &observe_id);
341 retv_if(IOTCON_ERROR_NONE != ret, -1);
343 ret = iotcon_observers_remove(observers, observe_id);
344 retv_if(IOTCON_ERROR_NONE != ret, -1);
350 static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data)
352 connectivity_resource_s *resource_info = user_data;
354 char *host_address = NULL;
358 ret = iotcon_request_get_host_address(request, &host_address);
359 goto_if(IOTCON_ERROR_NONE != ret, error);
361 _D("Host address : %s", host_address);
363 ret = _handle_query(request);
364 goto_if(IOTCON_ERROR_NONE != ret, error);
366 ret = _handle_request_by_crud_type(request, resource_info);
367 goto_if(0 != ret, error);
369 ret = _handle_observer(request, resource_info->observers);
370 goto_if(0 != ret, error);
375 _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
378 /* device_name : "iotcon-test-basic-server" */
379 int connectivity_init(const char *device_name)
383 ret = iotcon_initialize("../res/iotcon-test-svr-db-server.dat");
384 retv_if(IOTCON_ERROR_NONE != ret, -1);
386 ret = iotcon_set_device_name(device_name);
387 goto_if(IOTCON_ERROR_NONE != ret, error);
389 ret = iotcon_start_presence(10);
390 goto_if(IOTCON_ERROR_NONE != ret, error);
395 iotcon_deinitialize();
399 int connectivity_fini(void)
401 iotcon_deinitialize();
405 void connectivity_destroy_resource(connectivity_resource_s *resource_info)
407 ret_if(!resource_info);
408 if (resource_info->observers) iotcon_observers_destroy(resource_info->observers);
409 if (resource_info->res) iotcon_resource_destroy(resource_info->res);
413 int connectivity_create_resource(const char *uri_path, const char *type, connectivity_resource_s **out_resource_info)
415 iotcon_resource_types_h resource_types = NULL;
416 iotcon_resource_interfaces_h ifaces = NULL;
417 connectivity_resource_s *resource_info = NULL;
421 resource_info = calloc(1, sizeof(connectivity_resource_s));
422 retv_if(!resource_info, -1);
423 *out_resource_info = resource_info;
425 ret = iotcon_resource_types_create(&resource_types);
426 goto_if(IOTCON_ERROR_NONE != ret, error);
428 ret = iotcon_resource_types_add(resource_types, type);
429 goto_if(IOTCON_ERROR_NONE != ret, error);
431 ret = iotcon_resource_interfaces_create(&ifaces);
432 goto_if(IOTCON_ERROR_NONE != ret, error);
434 ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_DEFAULT);
435 goto_if(IOTCON_ERROR_NONE != ret, error);
437 ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_BATCH);
438 goto_if(IOTCON_ERROR_NONE != ret, error);
441 IOTCON_RESOURCE_DISCOVERABLE |
442 IOTCON_RESOURCE_OBSERVABLE |
443 IOTCON_RESOURCE_SECURE;
445 ret = iotcon_resource_create(uri_path,
449 _request_resource_handler,
451 &resource_info->res);
452 goto_if(IOTCON_ERROR_NONE != ret, error);
454 ret = iotcon_observers_create(&resource_info->observers);
455 goto_if(IOTCON_ERROR_NONE != ret, error);
457 iotcon_resource_types_destroy(resource_types);
458 iotcon_resource_interfaces_destroy(ifaces);
463 if (ifaces) iotcon_resource_interfaces_destroy(ifaces);
464 if (resource_types) iotcon_resource_types_destroy(resource_types);
465 if (resource_info->res) iotcon_resource_destroy(resource_info->res);
466 if (resource_info) free(resource_info);