2 * Copyright (c) 2015 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.
23 #define ROOM_RESOURCE_URI "/room/1"
24 #define ROOM_RESOURCE_TYPE "org.tizen.room"
25 #define LIGHT_RESOURCE_URI "/light/1"
26 #define LIGHT_RESOURCE_TYPE "org.tizen.light"
27 #define FAN_RESOURCE_URI "/fan/1"
28 #define FAN_RESOURCE_TYPE "org.tizen.fan"
31 typedef struct _light_resource_s {
37 iotcon_resource_h handle;
41 typedef struct _fan_resource_s {
47 iotcon_resource_h handle;
51 typedef struct _room_resource_s {
58 iotcon_resource_h handle;
59 light_resource_s *child_light;
60 fan_resource_s *child_fan;
63 static int _set_room_resource(room_resource_s *room)
65 room->name = strdup("Michael's Room");
66 if (NULL == room->name) {
71 room->today_temp[0] = 13;
72 room->today_temp[1] = 19;
73 room->today_temp[2] = 24;
74 room->today_temp[3] = 21;
75 room->today_temp[4] = 14;
77 room->uri_path = strdup(ROOM_RESOURCE_URI);
78 if (NULL == room->uri_path) {
79 ERR("strdup(%s) Fail", ROOM_RESOURCE_URI);
84 room->type = strdup(ROOM_RESOURCE_TYPE);
85 if (NULL == room->type) {
86 ERR("strdup(%s) Fail", ROOM_RESOURCE_TYPE);
92 room->ifaces = IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_BATCH;
93 room->properties = IOTCON_RESOURCE_DISCOVERABLE | IOTCON_RESOURCE_OBSERVABLE;
98 static void _free_room_resource(room_resource_s *room)
101 free(room->uri_path);
105 static int _set_light_resource(light_resource_s *light)
107 light->brightness = 50;
109 light->uri_path = strdup(LIGHT_RESOURCE_URI);
110 if (NULL == light->uri_path) {
111 ERR("strdup(%s) Fail", LIGHT_RESOURCE_URI);
115 light->type = strdup(LIGHT_RESOURCE_TYPE);
116 if (NULL == light->type) {
117 ERR("strdup(%s) Fail", LIGHT_RESOURCE_TYPE);
118 free(light->uri_path);
122 light->ifaces = IOTCON_INTERFACE_DEFAULT;
123 light->properties = IOTCON_RESOURCE_NO_PROPERTY;
128 static void _free_light_resource(light_resource_s *light)
131 free(light->uri_path);
134 static int _set_fan_resource(fan_resource_s *fan)
138 fan->uri_path = strdup(FAN_RESOURCE_URI);
139 if (NULL == fan->uri_path) {
140 ERR("strdup(%s) Fail", FAN_RESOURCE_URI);
144 fan->type = strdup(FAN_RESOURCE_TYPE);
145 if (NULL == fan->type) {
146 ERR("strdup(%s) Fail", FAN_RESOURCE_TYPE);
151 fan->ifaces = IOTCON_INTERFACE_DEFAULT;
152 fan->properties = IOTCON_RESOURCE_NO_PROPERTY;
157 static void _free_fan_resource(fan_resource_s *fan)
163 static iotcon_resource_h _create_resource(char *uri_path, char *type, int ifaces,
164 int properties, iotcon_request_handler_cb cb, void *user_data)
167 iotcon_resource_h handle;
168 iotcon_resource_types_h resource_types;
170 ret = iotcon_resource_types_create(&resource_types);
171 if (IOTCON_ERROR_NONE != ret) {
172 ERR("iotcon_resource_types_create() Fail(%d)", ret);
176 ret = iotcon_resource_types_add(resource_types, type);
177 if (IOTCON_ERROR_NONE != ret) {
178 ERR("iotcon_resource_types_add() Fail(%d)", ret);
179 iotcon_resource_types_destroy(resource_types);
183 ret = iotcon_resource_create(uri_path, resource_types, ifaces, properties, cb,
185 if (IOTCON_ERROR_NONE != ret) {
186 ERR("iotcon_response_create() Fail(%d)", ret);
187 iotcon_resource_types_destroy(resource_types);
191 iotcon_resource_types_destroy(resource_types);
196 static int _send_response(iotcon_request_h request, iotcon_representation_h repr,
197 iotcon_interface_e iface, iotcon_response_result_e result)
200 iotcon_response_h response;
202 ret = iotcon_response_create(request, &response);
203 if (IOTCON_ERROR_NONE != ret) {
204 ERR("iotcon_response_create() Fail(%d)", ret);
208 ret = iotcon_response_set_representation(response, iface, repr);
209 if (IOTCON_ERROR_NONE != ret) {
210 ERR("iotcon_response_set_representation() Fail(%d)", ret);
211 iotcon_response_destroy(response);
215 ret = iotcon_response_set_result(response, result);
216 if (IOTCON_ERROR_NONE != ret) {
217 ERR("iotcon_response_set_result() Fail(%d)", ret);
218 iotcon_response_destroy(response);
222 /* send Representation to the client */
223 ret = iotcon_response_send(response);
224 if (IOTCON_ERROR_NONE != ret) {
225 ERR("iotcon_response_send() Fail(%d)", ret);
226 iotcon_response_destroy(response);
230 iotcon_response_destroy(response);
235 static iotcon_representation_h _get_light_representation(light_resource_s *light)
238 iotcon_state_h state;
239 iotcon_representation_h repr;
241 /* create a light Representation */
242 ret = iotcon_representation_create(&repr);
243 if (IOTCON_ERROR_NONE != ret) {
244 ERR("iotcon_representation_create() Fail(%d)", ret);
248 ret = iotcon_representation_set_uri_path(repr, light->uri_path);
249 if (IOTCON_ERROR_NONE != ret) {
250 ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
251 iotcon_representation_destroy(repr);
255 /* create a light state */
256 ret = iotcon_state_create(&state);
257 if (IOTCON_ERROR_NONE != ret) {
258 ERR("iotcon_state_create() Fail(%d)", ret);
259 iotcon_representation_destroy(repr);
263 ret = iotcon_state_set_int(state, "brightness", light->brightness);
264 if (IOTCON_ERROR_NONE != ret) {
265 ERR("iotcon_state_set_int() Fail(%d)", ret);
266 iotcon_state_destroy(state);
267 iotcon_representation_destroy(repr);
271 /* Set a light state into light Representation */
272 ret = iotcon_representation_set_state(repr, state);
273 if (IOTCON_ERROR_NONE != ret) {
274 ERR("iotcon_representation_set_state() Fail(%d)", ret);
275 iotcon_state_destroy(state);
276 iotcon_representation_destroy(repr);
280 iotcon_state_destroy(state);
285 static int _light_request_handler_get(light_resource_s *light, iotcon_request_h request)
288 iotcon_representation_h repr;
290 INFO("GET request - Light");
292 repr = _get_light_representation(light);
294 ERR("_get_light_representation() Fail");
298 ret = _send_response(request, repr, IOTCON_INTERFACE_DEFAULT,
301 ERR("_send_response() Fail(%d)", ret);
302 iotcon_representation_destroy(repr);
306 iotcon_representation_destroy(repr);
312 static iotcon_representation_h _get_fan_representation(fan_resource_s *fan)
315 iotcon_state_h state;
316 iotcon_representation_h repr;
318 /* create a fan Representation */
319 ret = iotcon_representation_create(&repr);
320 if (IOTCON_ERROR_NONE != ret) {
321 ERR("iotcon_representation_create() Fail(%d)", ret);
325 ret = iotcon_representation_set_uri_path(repr, fan->uri_path);
326 if (IOTCON_ERROR_NONE != ret) {
327 ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
328 iotcon_representation_destroy(repr);
332 /* create a fan state */
333 ret = iotcon_state_create(&state);
334 if (IOTCON_ERROR_NONE != ret) {
335 ERR("iotcon_state_create() Fail(%d)", ret);
336 iotcon_representation_destroy(repr);
340 ret = iotcon_state_set_bool(state, "state", fan->state);
341 if (IOTCON_ERROR_NONE != ret) {
342 ERR("iotcon_state_set_bool() Fail(%d)", ret);
343 iotcon_state_destroy(state);
344 iotcon_representation_destroy(repr);
348 /* Set a light state into light Representation */
349 ret = iotcon_representation_set_state(repr, state);
350 if (IOTCON_ERROR_NONE != ret) {
351 ERR("iotcon_representation_set_state() Fail(%d)", ret);
352 iotcon_state_destroy(state);
353 iotcon_representation_destroy(repr);
357 iotcon_state_destroy(state);
362 static int _fan_request_handler_get(fan_resource_s *fan, iotcon_request_h request)
365 iotcon_representation_h repr;
367 INFO("GET request - Fan");
369 repr = _get_fan_representation(fan);
371 ERR("_get_fan_representation() Fail");
375 ret = _send_response(request, repr, IOTCON_INTERFACE_DEFAULT,
378 ERR("_send_response() Fail(%d)", ret);
379 iotcon_representation_destroy(repr);
383 iotcon_representation_destroy(repr);
388 static iotcon_representation_h _get_room_representation(room_resource_s *room)
391 iotcon_state_h state;
392 iotcon_list_h today_temp;
393 iotcon_representation_h repr, light_repr, fan_repr;
395 /* create a room Representation */
396 ret = iotcon_representation_create(&repr);
397 if (IOTCON_ERROR_NONE != ret) {
398 ERR("iotcon_representation_create() Fail(%d)", ret);
402 ret = iotcon_representation_set_uri_path(repr, room->uri_path);
403 if (IOTCON_ERROR_NONE != ret) {
404 ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
405 iotcon_representation_destroy(repr);
409 /* create a room state */
410 ret = iotcon_state_create(&state);
411 if (IOTCON_ERROR_NONE != ret) {
412 ERR("iotcon_state_create() Fail(%d)", ret);
413 iotcon_representation_destroy(repr);
417 ret = iotcon_state_set_str(state, "name", room->name);
418 if (IOTCON_ERROR_NONE != ret) {
419 ERR("iotcon_state_set_str() Fail(%d)", ret);
420 iotcon_state_destroy(state);
421 iotcon_representation_destroy(repr);
426 ret = iotcon_state_set_null(state, "null value");
427 if (IOTCON_ERROR_NONE != ret) {
428 ERR("iotcon_state_set_null() Fail(%d)", ret);
429 iotcon_state_destroy(state);
430 iotcon_representation_destroy(repr);
434 ret = iotcon_list_create(IOTCON_TYPE_INT, &today_temp);
435 if (IOTCON_ERROR_NONE != ret) {
436 ERR("iotcon_list_create() Fail(%d)", ret);
437 iotcon_state_destroy(state);
438 iotcon_representation_destroy(repr);
442 ret = iotcon_list_add_int(today_temp, room->today_temp[0], -1);
443 if (IOTCON_ERROR_NONE != ret) {
444 ERR("iotcon_list_add_int() Fail(%d)", ret);
445 iotcon_list_destroy(today_temp);
446 iotcon_state_destroy(state);
447 iotcon_representation_destroy(repr);
451 ret = iotcon_list_add_int(today_temp, room->today_temp[1], -1);
452 if (IOTCON_ERROR_NONE != ret) {
453 ERR("iotcon_list_add_int() Fail(%d)", ret);
454 iotcon_list_destroy(today_temp);
455 iotcon_state_destroy(state);
456 iotcon_representation_destroy(repr);
460 ret = iotcon_list_add_int(today_temp, room->today_temp[2], -1);
461 if (IOTCON_ERROR_NONE != ret) {
462 ERR("iotcon_list_add_int() Fail(%d)", ret);
463 iotcon_list_destroy(today_temp);
464 iotcon_state_destroy(state);
465 iotcon_representation_destroy(repr);
469 ret = iotcon_list_add_int(today_temp, room->today_temp[3], -1);
470 if (IOTCON_ERROR_NONE != ret) {
471 ERR("iotcon_list_add_int() Fail(%d)", ret);
472 iotcon_list_destroy(today_temp);
473 iotcon_state_destroy(state);
474 iotcon_representation_destroy(repr);
478 ret = iotcon_list_add_int(today_temp, room->today_temp[4], -1);
479 if (IOTCON_ERROR_NONE != ret) {
480 ERR("iotcon_list_add_int() Fail(%d)", ret);
481 iotcon_list_destroy(today_temp);
482 iotcon_state_destroy(state);
483 iotcon_representation_destroy(repr);
487 ret = iotcon_state_set_list(state, "today_temp", today_temp);
488 if (IOTCON_ERROR_NONE != ret) {
489 ERR("iotcon_state_set_list() Fail(%d)", ret);
490 iotcon_list_destroy(today_temp);
491 iotcon_state_destroy(state);
492 iotcon_representation_destroy(repr);
496 iotcon_list_destroy(today_temp);
498 /* Set a room state into room Representation */
499 ret = iotcon_representation_set_state(repr, state);
500 if (IOTCON_ERROR_NONE != ret) {
501 ERR("iotcon_representation_set_state() Fail(%d)", ret);
502 iotcon_state_destroy(state);
503 iotcon_representation_destroy(repr);
507 iotcon_state_destroy(state);
509 light_repr = _get_light_representation(room->child_light);
510 if (NULL == light_repr) {
511 ERR("_get_light_representation() fail");
512 iotcon_representation_destroy(repr);
516 ret = iotcon_representation_append_child(repr, light_repr);
517 if (IOTCON_ERROR_NONE != ret) {
518 ERR("iotcon_representation_append_child() Fail(%d)", ret);
519 iotcon_representation_destroy(light_repr);
520 iotcon_representation_destroy(repr);
524 iotcon_representation_destroy(light_repr);
526 fan_repr = _get_fan_representation(room->child_fan);
527 if (NULL == fan_repr) {
528 ERR("_get_fan_representation() fail");
529 iotcon_representation_destroy(repr);
533 ret = iotcon_representation_append_child(repr, fan_repr);
534 if (IOTCON_ERROR_NONE != ret) {
535 ERR("iotcon_representation_append_child() Fail(%d)", ret);
536 iotcon_representation_destroy(fan_repr);
537 iotcon_representation_destroy(repr);
541 iotcon_representation_destroy(fan_repr);
546 static int _room_request_handler_get(room_resource_s *room, iotcon_request_h request)
549 iotcon_query_h query;
550 iotcon_representation_h repr;
551 iotcon_interface_e iface = IOTCON_INTERFACE_DEFAULT;
553 INFO("GET request - Room");
555 repr = _get_room_representation(room);
557 ERR("_get_room_representation() Fail");
561 ret = iotcon_request_get_query(request, &query);
562 if (IOTCON_ERROR_NONE != ret) {
563 ERR("iotcon_request_get_query() Fail(%d)", ret);
564 iotcon_representation_destroy(repr);
569 ret = iotcon_query_get_interface(query, &iface);
570 if (IOTCON_ERROR_NO_DATA == ret) {
571 iface = IOTCON_INTERFACE_DEFAULT;
572 } else if (IOTCON_ERROR_NONE != ret) {
573 ERR("iotcon_query_get_interface() Fail(%d)", ret);
574 iotcon_representation_destroy(repr);
579 ret = _send_response(request, repr, iface, IOTCON_RESPONSE_OK);
581 ERR("_send_response() Fail(%d)", ret);
582 iotcon_representation_destroy(repr);
586 iotcon_representation_destroy(repr);
591 static void _light_request_handler(iotcon_resource_h resource, iotcon_request_h request,
595 iotcon_request_type_e type;
596 light_resource_s *light = user_data;
597 int iface = IOTCON_INTERFACE_DEFAULT;
599 RET_IF(NULL == request);
601 ret = iotcon_request_get_request_type(request, &type);
602 if (IOTCON_ERROR_NONE != ret) {
603 ERR("iotcon_request_get_types() Fail(%d)", ret);
604 _send_response(request, NULL, iface, IOTCON_RESPONSE_ERROR);
608 if (IOTCON_REQUEST_GET == type) {
609 ret = _light_request_handler_get(light, request);
611 _send_response(request, NULL, iface, IOTCON_RESPONSE_ERROR);
613 _send_response(request, NULL, iface, IOTCON_RESPONSE_FORBIDDEN);
617 static void _fan_request_handler(iotcon_resource_h resource, iotcon_request_h request,
621 iotcon_request_type_e type;
622 fan_resource_s *fan = user_data;
623 int iface = IOTCON_INTERFACE_DEFAULT;
625 RET_IF(NULL == request);
627 ret = iotcon_request_get_request_type(request, &type);
628 if (IOTCON_ERROR_NONE != ret) {
629 ERR("iotcon_request_get_types() Fail(%d)", ret);
630 _send_response(request, NULL, iface, IOTCON_RESPONSE_ERROR);
634 if (IOTCON_REQUEST_GET == type) {
635 ret = _fan_request_handler_get(fan, request);
637 _send_response(request, NULL, iface, IOTCON_RESPONSE_ERROR);
639 _send_response(request, NULL, iface, IOTCON_RESPONSE_FORBIDDEN);
643 static void _room_request_handler(iotcon_resource_h resource, iotcon_request_h request,
648 iotcon_request_type_e type;
650 room_resource_s *room = user_data;
651 int iface = IOTCON_INTERFACE_DEFAULT;
653 RET_IF(NULL == request);
655 ret = iotcon_request_get_host_address(request, &host_address);
656 if (IOTCON_ERROR_NONE != ret) {
657 ERR("iotcon_request_get_host_address() Fail(%d)", ret);
658 _send_response(request, NULL, iface, IOTCON_RESPONSE_ERROR);
661 INFO("host address : %s", host_address);
663 ret = iotcon_request_get_request_type(request, &type);
664 if (IOTCON_ERROR_NONE != ret) {
665 ERR("iotcon_request_get_types() Fail(%d)", ret);
666 _send_response(request, NULL, iface, IOTCON_RESPONSE_ERROR);
670 if (IOTCON_REQUEST_GET == type) {
671 ret = _room_request_handler_get(room, request);
673 _send_response(request, NULL, iface, IOTCON_RESPONSE_ERROR);
675 _send_response(request, NULL, iface, IOTCON_RESPONSE_FORBIDDEN);
679 int main(int argc, char **argv)
684 room_resource_s room = {0};
685 light_resource_s light = {0};
686 fan_resource_s fan = {0};
688 loop = g_main_loop_new(NULL, FALSE);
691 ret = iotcon_connect();
692 if (IOTCON_ERROR_NONE != ret) {
693 ERR("iotcon_connect() Fail(%d)", ret);
698 ret = _set_room_resource(&room);
700 ERR("_set_room_resource() Fail(%d)", ret);
705 ret = _set_light_resource(&light);
707 ERR("_set_room_resource() Fail(%d)", ret);
708 _free_room_resource(&room);
713 ret = _set_fan_resource(&fan);
715 ERR("_set_room_resource() Fail(%d)", ret);
716 _free_room_resource(&room);
717 _free_light_resource(&light);
722 room.child_light = &light;
723 room.child_fan = &fan;
725 /* register room resource */
726 room.handle = _create_resource(room.uri_path, room.type, room.ifaces, room.properties,
727 _room_request_handler, &room);
728 if (NULL == room.handle) {
729 ERR("_create_resource() Fail");
730 _free_fan_resource(&fan);
731 _free_light_resource(&light);
732 _free_room_resource(&room);
737 /* register light resource */
738 light.handle = _create_resource(light.uri_path, light.type, light.ifaces,
739 light.properties, _light_request_handler, &light);
740 if (NULL == light.handle) {
741 ERR("_create_resource() Fail");
742 iotcon_resource_destroy(room.handle);
743 _free_fan_resource(&fan);
744 _free_light_resource(&light);
745 _free_room_resource(&room);
750 ret = iotcon_resource_bind_child_resource(room.handle, light.handle);
751 if (IOTCON_ERROR_NONE != ret) {
752 ERR("iotcon_resource_bind_child_resource() Fail");
753 iotcon_resource_destroy(light.handle);
754 iotcon_resource_destroy(room.handle);
755 _free_fan_resource(&fan);
756 _free_light_resource(&light);
757 _free_room_resource(&room);
762 /* register fan resource */
763 fan.handle = _create_resource(fan.uri_path, fan.type, fan.ifaces, fan.properties,
764 _fan_request_handler, &fan);
765 if (NULL == fan.handle) {
766 ERR("_create_resource() Fail");
767 iotcon_resource_destroy(light.handle);
768 iotcon_resource_destroy(room.handle);
769 _free_fan_resource(&fan);
770 _free_light_resource(&light);
771 _free_room_resource(&room);
776 ret = iotcon_resource_bind_child_resource(room.handle, fan.handle);
777 if (IOTCON_ERROR_NONE != ret) {
778 ERR("iotcon_resource_bind_child_resource() Fail");
779 iotcon_resource_destroy(fan.handle);
780 iotcon_resource_destroy(light.handle);
781 iotcon_resource_destroy(room.handle);
782 _free_fan_resource(&fan);
783 _free_light_resource(&light);
784 _free_room_resource(&room);
789 g_main_loop_run(loop);
790 g_main_loop_unref(loop);
792 iotcon_resource_destroy(fan.handle);
793 iotcon_resource_destroy(light.handle);
794 iotcon_resource_destroy(room.handle);
795 _free_fan_resource(&fan);
796 _free_light_resource(&light);
797 _free_room_resource(&room);
799 /* disconnect iotcon */