2 * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include "connectivity.h"
26 #define ULTRASONIC_RESOURCE_1_URI "/door/1"
27 #define ULTRASONIC_RESOURCE_2_URI "/door/2"
28 #define ULTRASONIC_RESOURCE_TYPE "org.tizen.door"
30 struct connectivity_resource {
31 iotcon_resource_h res;
32 iotcon_observers_h observers;
34 static bool _resource_created;
35 static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data);
37 static int _send_response(iotcon_request_h request, iotcon_representation_h representation, iotcon_response_result_e result)
40 iotcon_response_h response;
42 ret = iotcon_response_create(request, &response);
43 retv_if(IOTCON_ERROR_NONE != ret, -1);
45 ret = iotcon_response_set_result(response, result);
46 goto_if(IOTCON_ERROR_NONE != ret, error);
48 ret = iotcon_response_set_representation(response, representation);
49 goto_if(IOTCON_ERROR_NONE != ret, error);
51 ret = iotcon_response_send(response);
52 goto_if(IOTCON_ERROR_NONE != ret, error);
54 iotcon_response_destroy(response);
59 iotcon_response_destroy(response);
63 static void _destroy_representation(iotcon_representation_h representation)
65 ret_if(!representation);
66 iotcon_representation_destroy(representation);
69 static iotcon_representation_h _create_representation_with_attribute(iotcon_resource_h res, bool value)
71 iotcon_attributes_h attributes = NULL;
72 iotcon_representation_h representation = NULL;
73 char *uri_path = NULL;
76 ret = iotcon_resource_get_uri_path(res, &uri_path);
77 retv_if(IOTCON_ERROR_NONE != ret, NULL);
79 ret = iotcon_representation_create(&representation);
80 retv_if(IOTCON_ERROR_NONE != ret, NULL);
82 ret = iotcon_attributes_create(&attributes);
83 goto_if(IOTCON_ERROR_NONE != ret, error);
85 ret = iotcon_representation_set_uri_path(representation, uri_path);
86 goto_if(IOTCON_ERROR_NONE != ret, error);
88 ret = iotcon_attributes_add_bool(attributes, "opened", value);
89 goto_if(IOTCON_ERROR_NONE != ret, error);
91 ret = iotcon_representation_set_attributes(representation, attributes);
92 goto_if(IOTCON_ERROR_NONE != ret, error);
94 iotcon_attributes_destroy(attributes);
96 return representation;
99 if (attributes) iotcon_attributes_destroy(attributes);
100 if (representation) iotcon_representation_destroy(representation);
105 static int _handle_get_request(iotcon_resource_h res, iotcon_request_h request)
107 iotcon_representation_h representation;
111 /* FIXME : We need to check the value of sensors */
112 representation = _create_representation_with_attribute(res, (bool)value);
113 retv_if(!representation, -1);
115 ret = _send_response(request, representation, IOTCON_RESPONSE_OK);
116 goto_if(0 != ret, error);
118 _destroy_representation(representation);
123 _destroy_representation(representation);
127 static int _get_value_from_representation(iotcon_representation_h representation, bool *value)
129 iotcon_attributes_h attributes;
132 ret = iotcon_representation_get_attributes(representation, &attributes);
133 retv_if(IOTCON_ERROR_NONE != ret, -1);
135 ret = iotcon_attributes_get_bool(attributes, "opened", value);
136 retv_if(IOTCON_ERROR_NONE != ret, -1);
141 static int _set_value_into_thing(iotcon_representation_h representation, bool value)
143 /* FIXME : We need to set the value into the thing */
147 static int _handle_put_request(connectivity_resource_s *resource_info, iotcon_request_h request)
149 iotcon_representation_h req_repr, resp_repr;
155 ret = iotcon_request_get_representation(request, &req_repr);
156 retv_if(IOTCON_ERROR_NONE != ret, -1);
158 ret = _get_value_from_representation(req_repr, &value);
159 retv_if(0 != ret, -1);
161 ret = _set_value_into_thing(req_repr, value);
162 retv_if(0 != ret, -1);
164 resp_repr = _create_representation_with_attribute(resource_info->res, (bool)value);
165 retv_if(NULL == resp_repr, -1);
167 ret = _send_response(request, resp_repr, IOTCON_RESPONSE_OK);
168 goto_if(0 != ret, error);
170 ret = iotcon_resource_notify(resource_info->res, resp_repr, resource_info->observers, IOTCON_QOS_HIGH);
171 goto_if(IOTCON_ERROR_NONE != ret, error);
173 _destroy_representation(resp_repr);
178 _destroy_representation(resp_repr);
182 int connectivity_notify(connectivity_resource_s *user_data, int value)
184 iotcon_representation_h representation;
185 connectivity_resource_s *resource_info = user_data;
188 retv_if(!resource_info, -1);
189 retv_if(!resource_info->observers, -1);
191 _D("Notify the value[%d]", value);
193 representation = _create_representation_with_attribute(resource_info->res, (bool)value);
194 retv_if(!representation, -1);
196 ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
197 retv_if(IOTCON_ERROR_NONE != ret, -1);
199 _destroy_representation(representation);
204 static int _handle_post_request(connectivity_resource_s *resource_info, iotcon_request_h request)
206 iotcon_attributes_h resp_attributes = NULL;
207 iotcon_representation_h resp_repr = NULL;
208 connectivity_resource_s *new_resource_info = NULL;
213 if (_resource_created) {
214 _E("Resource(%s) is already created", ULTRASONIC_RESOURCE_2_URI);
218 new_resource_info = calloc(1, sizeof(connectivity_resource_s));
219 retv_if(!new_resource_info, -1);
221 ret = connectivity_create_resource(ULTRASONIC_RESOURCE_2_URI, ULTRASONIC_RESOURCE_TYPE, &new_resource_info);
222 retv_if(0 != ret, -1);
224 _resource_created = true;
226 ret = iotcon_representation_create(&resp_repr);
227 retv_if(IOTCON_ERROR_NONE != ret, -1);
229 ret = iotcon_attributes_create(&resp_attributes);
230 goto_if(IOTCON_ERROR_NONE != ret, error);
232 ret = iotcon_attributes_add_str(resp_attributes, "createduripath", ULTRASONIC_RESOURCE_2_URI);
233 goto_if(IOTCON_ERROR_NONE != ret, error);
235 ret = iotcon_representation_set_attributes(resp_repr, resp_attributes);
236 goto_if(IOTCON_ERROR_NONE != ret, error);
238 iotcon_attributes_destroy(resp_attributes);
240 ret = _send_response(request, resp_repr, IOTCON_RESPONSE_RESOURCE_CREATED);
241 goto_if(0 != ret, error);
243 iotcon_representation_destroy(resp_repr);
248 if (resp_attributes) iotcon_attributes_destroy(resp_attributes);
249 iotcon_representation_destroy(resp_repr);
253 static int _handle_delete_request(iotcon_resource_h resource, iotcon_request_h request)
257 _D("DELETE request");
259 ret = iotcon_resource_destroy(resource);
260 retv_if(IOTCON_ERROR_NONE != ret, -1);
262 ret = _send_response(request, NULL, IOTCON_RESPONSE_RESOURCE_DELETED);
263 retv_if(0 != ret, -1);
268 static bool _query_cb(const char *key, const char *value, void *user_data)
270 _D("Key : [%s], Value : [%s]", key, value);
272 return IOTCON_FUNC_CONTINUE;
275 static int _handle_query(iotcon_request_h request)
277 iotcon_query_h query = NULL;
280 ret = iotcon_request_get_query(request, &query);
281 retv_if(IOTCON_ERROR_NONE != ret, -1);
283 if (query) iotcon_query_foreach(query, _query_cb, NULL);
288 static int _handle_request_by_crud_type(iotcon_request_h request, connectivity_resource_s *resource_info)
290 iotcon_request_type_e type;
293 ret = iotcon_request_get_request_type(request, &type);
294 retv_if(IOTCON_ERROR_NONE != ret, -1);
297 case IOTCON_REQUEST_GET:
298 ret = _handle_get_request(resource_info->res, request);
300 case IOTCON_REQUEST_PUT:
301 ret = _handle_put_request(resource_info, request);
303 case IOTCON_REQUEST_POST:
304 ret = _handle_post_request(resource_info, request);
306 case IOTCON_REQUEST_DELETE:
307 ret = _handle_delete_request(resource_info->res, request);
310 _E("Cannot reach here");
314 retv_if(0 != ret, -1);
319 static int _handle_observer(iotcon_request_h request, iotcon_observers_h observers)
321 iotcon_observe_type_e observe_type;
325 ret = iotcon_request_get_observe_type(request, &observe_type);
326 retv_if(IOTCON_ERROR_NONE != ret, -1);
328 if (IOTCON_OBSERVE_REGISTER == observe_type) {
329 ret = iotcon_request_get_observe_id(request, &observe_id);
330 retv_if(IOTCON_ERROR_NONE != ret, -1);
332 ret = iotcon_observers_add(observers, observe_id);
333 retv_if(IOTCON_ERROR_NONE != ret, -1);
334 } else if (IOTCON_OBSERVE_DEREGISTER == observe_type) {
335 ret = iotcon_request_get_observe_id(request, &observe_id);
336 retv_if(IOTCON_ERROR_NONE != ret, -1);
338 ret = iotcon_observers_remove(observers, observe_id);
339 retv_if(IOTCON_ERROR_NONE != ret, -1);
345 static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data)
347 connectivity_resource_s *resource_info = user_data;
349 char *host_address = NULL;
353 ret = iotcon_request_get_host_address(request, &host_address);
354 goto_if(IOTCON_ERROR_NONE != ret, error);
356 _D("Host address : %s", host_address);
358 ret = _handle_query(request);
359 goto_if(IOTCON_ERROR_NONE != ret, error);
361 ret = _handle_request_by_crud_type(request, resource_info);
362 goto_if(0 != ret, error);
364 ret = _handle_observer(request, resource_info->observers);
365 goto_if(0 != ret, error);
370 _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
373 /* device_name : "iotcon-test-basic-server" */
374 int connectivity_init(const char *device_name)
378 ret = iotcon_initialize("../res/iotcon-test-svr-db-server.dat");
379 retv_if(IOTCON_ERROR_NONE != ret, -1);
381 ret = iotcon_set_device_name(device_name);
382 goto_if(IOTCON_ERROR_NONE != ret, error);
384 ret = iotcon_start_presence(10);
385 goto_if(IOTCON_ERROR_NONE != ret, error);
390 iotcon_deinitialize();
394 int connectivity_fini(void)
396 iotcon_deinitialize();
400 void connectivity_destroy_resource(connectivity_resource_s *resource_info)
402 ret_if(!resource_info);
403 if (resource_info->observers) iotcon_observers_destroy(resource_info->observers);
404 if (resource_info->res) iotcon_resource_destroy(resource_info->res);
408 int connectivity_create_resource(const char *uri_path, const char *type, connectivity_resource_s **out_resource_info)
410 iotcon_resource_types_h resource_types = NULL;
411 iotcon_resource_interfaces_h ifaces = NULL;
412 connectivity_resource_s *resource_info = NULL;
416 resource_info = calloc(1, sizeof(connectivity_resource_s));
417 retv_if(!resource_info, -1);
418 *out_resource_info = resource_info;
420 ret = iotcon_resource_types_create(&resource_types);
421 goto_if(IOTCON_ERROR_NONE != ret, error);
423 ret = iotcon_resource_types_add(resource_types, type);
424 goto_if(IOTCON_ERROR_NONE != ret, error);
426 ret = iotcon_resource_interfaces_create(&ifaces);
427 goto_if(IOTCON_ERROR_NONE != ret, error);
429 ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_DEFAULT);
430 goto_if(IOTCON_ERROR_NONE != ret, error);
432 ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_BATCH);
433 goto_if(IOTCON_ERROR_NONE != ret, error);
436 IOTCON_RESOURCE_DISCOVERABLE |
437 IOTCON_RESOURCE_OBSERVABLE |
438 IOTCON_RESOURCE_SECURE;
440 ret = iotcon_resource_create(uri_path,
444 _request_resource_handler,
446 &resource_info->res);
447 goto_if(IOTCON_ERROR_NONE != ret, error);
449 ret = iotcon_observers_create(&resource_info->observers);
450 goto_if(IOTCON_ERROR_NONE != ret, error);
452 iotcon_resource_types_destroy(resource_types);
453 iotcon_resource_interfaces_destroy(ifaces);
458 if (ifaces) iotcon_resource_interfaces_destroy(ifaces);
459 if (resource_types) iotcon_resource_types_destroy(resource_types);
460 if (resource_info->res) iotcon_resource_destroy(resource_info->res);
461 if (resource_info) free(resource_info);