190e9119db551e11fc513ff8e3f46cdbcca6a721
[platform/core/iot/iotcon.git] / test / iotcon-test-iface-server.c
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdlib.h>
18 #include <glib.h>
19
20 #include <iotcon.h>
21 #include "test.h"
22
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"
29
30 /* Light Resource */
31 typedef struct _light_resource_s {
32         int brightness;
33         char *uri_path;
34         char *type;
35         int ifaces;
36         int properties;
37         iotcon_resource_h handle;
38 } light_resource_s;
39
40 /* Fan Resource */
41 typedef struct _fan_resource_s {
42         bool state;
43         char *uri_path;
44         char *type;
45         int ifaces;
46         int properties;
47         iotcon_resource_h handle;
48 } fan_resource_s;
49
50 /* Room Resource */
51 typedef struct _room_resource_s {
52         char *name;
53         int today_temp[5];
54         char *uri_path;
55         char *type;
56         int ifaces;
57         int properties;
58         iotcon_resource_h handle;
59         light_resource_s *child_light;
60         fan_resource_s *child_fan;
61 } room_resource_s;
62
63 static int _set_room_resource(room_resource_s *room)
64 {
65         room->name = strdup("Michael's Room");
66         if (NULL == room->name) {
67                 ERR("strdup() Fail");
68                 return -1;
69         }
70
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;
76
77         room->uri_path = strdup(ROOM_RESOURCE_URI);
78         if (NULL == room->uri_path) {
79                 ERR("strdup(%s) Fail", ROOM_RESOURCE_URI);
80                 free(room->name);
81                 return -1;
82         }
83
84         room->type = strdup(ROOM_RESOURCE_TYPE);
85         if (NULL == room->type) {
86                 ERR("strdup(%s) Fail", ROOM_RESOURCE_TYPE);
87                 free(room->uri_path);
88                 free(room->name);
89                 return -1;
90         }
91
92         room->ifaces = IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_BATCH;
93         room->properties = IOTCON_RESOURCE_DISCOVERABLE | IOTCON_RESOURCE_OBSERVABLE;
94
95         return 0;
96 }
97
98 static void _free_room_resource(room_resource_s *room)
99 {
100         free(room->type);
101         free(room->uri_path);
102         free(room->name);
103 }
104
105 static int _set_light_resource(light_resource_s *light)
106 {
107         light->brightness = 50;
108
109         light->uri_path = strdup(LIGHT_RESOURCE_URI);
110         if (NULL == light->uri_path) {
111                 ERR("strdup(%s) Fail", LIGHT_RESOURCE_URI);
112                 return -1;
113         }
114
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);
119                 return -1;
120         }
121
122         light->ifaces = IOTCON_INTERFACE_DEFAULT;
123         light->properties = IOTCON_RESOURCE_NO_PROPERTY;
124
125         return 0;
126 }
127
128 static void _free_light_resource(light_resource_s *light)
129 {
130         free(light->type);
131         free(light->uri_path);
132 }
133
134 static int _set_fan_resource(fan_resource_s *fan)
135 {
136         fan->state = false;
137
138         fan->uri_path = strdup(FAN_RESOURCE_URI);
139         if (NULL == fan->uri_path) {
140                 ERR("strdup(%s) Fail", FAN_RESOURCE_URI);
141                 return -1;
142         }
143
144         fan->type = strdup(FAN_RESOURCE_TYPE);
145         if (NULL == fan->type) {
146                 ERR("strdup(%s) Fail", FAN_RESOURCE_TYPE);
147                 free(fan->uri_path);
148                 return -1;
149         }
150
151         fan->ifaces = IOTCON_INTERFACE_DEFAULT;
152         fan->properties = IOTCON_RESOURCE_NO_PROPERTY;
153
154         return 0;
155 }
156
157 static void _free_fan_resource(fan_resource_s *fan)
158 {
159         free(fan->type);
160         free(fan->uri_path);
161 }
162
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)
165 {
166         int ret;
167         iotcon_resource_h handle;
168         iotcon_resource_types_h resource_types;
169
170         ret = iotcon_resource_types_create(&resource_types);
171         if (IOTCON_ERROR_NONE != ret) {
172                 ERR("iotcon_resource_types_create() Fail(%d)", ret);
173                 return NULL;
174         }
175
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);
180                 return NULL;
181         }
182
183         ret = iotcon_resource_create(uri_path, resource_types, ifaces, properties, cb,
184                         user_data, &handle);
185         if (IOTCON_ERROR_NONE != ret) {
186                 ERR("iotcon_response_create() Fail(%d)", ret);
187                 iotcon_resource_types_destroy(resource_types);
188                 return NULL;
189         }
190
191         iotcon_resource_types_destroy(resource_types);
192
193         return handle;
194 }
195
196 static int _send_response(iotcon_request_h request, iotcon_representation_h repr,
197                 iotcon_interface_e iface, iotcon_response_result_e result)
198 {
199         int ret;
200         iotcon_response_h response;
201
202         ret = iotcon_response_create(request, &response);
203         if (IOTCON_ERROR_NONE != ret) {
204                 ERR("iotcon_response_create() Fail(%d)", ret);
205                 return -1;
206         }
207
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);
212                 return -1;
213         }
214
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);
219                 return -1;
220         }
221
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);
227                 return -1;
228         }
229
230         iotcon_response_destroy(response);
231
232         return 0;
233 }
234
235 static iotcon_representation_h _get_light_representation(light_resource_s *light)
236 {
237         int ret;
238         iotcon_state_h state;
239         iotcon_representation_h repr;
240
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);
245                 return NULL;
246         }
247
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);
252                 return NULL;
253         }
254
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);
260                 return NULL;
261         }
262
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);
268                 return NULL;
269         }
270
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);
277                 return NULL;
278         }
279
280         iotcon_state_destroy(state);
281
282         return repr;
283 }
284
285 static int _light_request_handler_get(light_resource_s *light, iotcon_request_h request)
286 {
287         int ret;
288         iotcon_representation_h repr;
289
290         INFO("GET request - Light");
291
292         repr = _get_light_representation(light);
293         if (NULL == repr) {
294                 ERR("_get_light_representation() Fail");
295                 return -1;
296         }
297
298         ret = _send_response(request, repr, IOTCON_INTERFACE_DEFAULT,
299                         IOTCON_RESPONSE_OK);
300         if (0 != ret) {
301                 ERR("_send_response() Fail(%d)", ret);
302                 iotcon_representation_destroy(repr);
303                 return -1;
304         }
305
306         iotcon_representation_destroy(repr);
307
308         return 0;
309 }
310
311
312 static iotcon_representation_h _get_fan_representation(fan_resource_s *fan)
313 {
314         int ret;
315         iotcon_state_h state;
316         iotcon_representation_h repr;
317
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);
322                 return NULL;
323         }
324
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);
329                 return NULL;
330         }
331
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);
337                 return NULL;
338         }
339
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);
345                 return NULL;
346         }
347
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);
354                 return NULL;
355         }
356
357         iotcon_state_destroy(state);
358
359         return repr;
360 }
361
362 static int _fan_request_handler_get(fan_resource_s *fan, iotcon_request_h request)
363 {
364         int ret;
365         iotcon_representation_h repr;
366
367         INFO("GET request - Fan");
368
369         repr = _get_fan_representation(fan);
370         if (NULL == repr) {
371                 ERR("_get_fan_representation() Fail");
372                 return -1;
373         }
374
375         ret = _send_response(request, repr, IOTCON_INTERFACE_DEFAULT,
376                         IOTCON_RESPONSE_OK);
377         if (0 != ret) {
378                 ERR("_send_response() Fail(%d)", ret);
379                 iotcon_representation_destroy(repr);
380                 return -1;
381         }
382
383         iotcon_representation_destroy(repr);
384
385         return 0;
386 }
387
388 static iotcon_representation_h _get_room_representation(room_resource_s *room)
389 {
390         int ret;
391         iotcon_state_h state;
392         iotcon_list_h today_temp;
393         iotcon_representation_h repr, light_repr, fan_repr;
394
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);
399                 return NULL;
400         }
401
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);
406                 return NULL;
407         }
408
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);
414                 return NULL;
415         }
416
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);
422                 return NULL;
423         }
424
425         /* set null */
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);
431                 return NULL;
432         }
433
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);
439                 return NULL;
440         }
441
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);
448                 return NULL;
449         }
450
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);
457                 return NULL;
458         }
459
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);
466                 return NULL;
467         }
468
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);
475                 return NULL;
476         }
477
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);
484                 return NULL;
485         }
486
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);
493                 return NULL;
494         }
495
496         iotcon_list_destroy(today_temp);
497
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);
504                 return NULL;
505         }
506
507         iotcon_state_destroy(state);
508
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);
513                 return NULL;
514         }
515
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);
521                 return NULL;
522         }
523
524         iotcon_representation_destroy(light_repr);
525
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);
530                 return NULL;
531         }
532
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);
538                 return NULL;
539         }
540
541         iotcon_representation_destroy(fan_repr);
542
543         return repr;
544 }
545
546 static int _room_request_handler_get(room_resource_s *room, iotcon_request_h request)
547 {
548         int ret;
549         iotcon_query_h query;
550         iotcon_representation_h repr;
551         iotcon_interface_e iface = IOTCON_INTERFACE_DEFAULT;
552
553         INFO("GET request - Room");
554
555         repr = _get_room_representation(room);
556         if (NULL == repr) {
557                 ERR("_get_room_representation() Fail");
558                 return -1;
559         }
560
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);
565                 return -1;
566         }
567
568         if (query) {
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);
575                         return -1;
576                 }
577         }
578
579         ret = _send_response(request, repr, iface, IOTCON_RESPONSE_OK);
580         if (0 != ret) {
581                 ERR("_send_response() Fail(%d)", ret);
582                 iotcon_representation_destroy(repr);
583                 return -1;
584         }
585
586         iotcon_representation_destroy(repr);
587
588         return 0;
589 }
590
591 static void _light_request_handler(iotcon_resource_h resource, iotcon_request_h request,
592                 void *user_data)
593 {
594         int ret;
595         iotcon_request_type_e type;
596         light_resource_s *light = user_data;
597         int iface = IOTCON_INTERFACE_DEFAULT;
598
599         RET_IF(NULL == request);
600
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);
605                 return;
606         }
607
608         if (IOTCON_REQUEST_GET == type) {
609                 ret = _light_request_handler_get(light, request);
610                 if (0 != ret)
611                         _send_response(request, NULL, iface, IOTCON_RESPONSE_ERROR);
612         } else {
613                 _send_response(request, NULL, iface, IOTCON_RESPONSE_FORBIDDEN);
614         }
615 }
616
617 static void _fan_request_handler(iotcon_resource_h resource, iotcon_request_h request,
618                 void *user_data)
619 {
620         int ret;
621         iotcon_request_type_e type;
622         fan_resource_s *fan = user_data;
623         int iface = IOTCON_INTERFACE_DEFAULT;
624
625         RET_IF(NULL == request);
626
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);
631                 return;
632         }
633
634         if (IOTCON_REQUEST_GET == type) {
635                 ret = _fan_request_handler_get(fan, request);
636                 if (0 != ret)
637                         _send_response(request, NULL, iface, IOTCON_RESPONSE_ERROR);
638         } else {
639                 _send_response(request, NULL, iface, IOTCON_RESPONSE_FORBIDDEN);
640         }
641 }
642
643 static void _room_request_handler(iotcon_resource_h resource, iotcon_request_h request,
644                 void *user_data)
645 {
646         FN_CALL;
647         int ret;
648         iotcon_request_type_e type;
649         char *host_address;
650         room_resource_s *room = user_data;
651         int iface = IOTCON_INTERFACE_DEFAULT;
652
653         RET_IF(NULL == request);
654
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);
659                 return;
660         }
661         INFO("host address : %s", host_address);
662
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);
667                 return;
668         }
669
670         if (IOTCON_REQUEST_GET == type) {
671                 ret = _room_request_handler_get(room, request);
672                 if (0 != ret)
673                         _send_response(request, NULL, iface, IOTCON_RESPONSE_ERROR);
674         } else {
675                 _send_response(request, NULL, iface, IOTCON_RESPONSE_FORBIDDEN);
676         }
677 }
678
679 int main(int argc, char **argv)
680 {
681         FN_CALL;
682         int ret;
683         GMainLoop *loop;
684         room_resource_s room = {0};
685         light_resource_s light = {0};
686         fan_resource_s fan = {0};
687
688         loop = g_main_loop_new(NULL, FALSE);
689
690         /* connect iotcon */
691         ret = iotcon_connect();
692         if (IOTCON_ERROR_NONE != ret) {
693                 ERR("iotcon_connect() Fail(%d)", ret);
694                 return -1;
695         }
696
697         /* set resource */
698         ret = _set_room_resource(&room);
699         if (0 != ret) {
700                 ERR("_set_room_resource() Fail(%d)", ret);
701                 iotcon_disconnect();
702                 return -1;
703         }
704
705         ret = _set_light_resource(&light);
706         if (0 != ret) {
707                 ERR("_set_room_resource() Fail(%d)", ret);
708                 _free_room_resource(&room);
709                 iotcon_disconnect();
710                 return -1;
711         }
712
713         ret = _set_fan_resource(&fan);
714         if (0 != ret) {
715                 ERR("_set_room_resource() Fail(%d)", ret);
716                 _free_room_resource(&room);
717                 _free_light_resource(&light);
718                 iotcon_disconnect();
719                 return -1;
720         }
721
722         room.child_light = &light;
723         room.child_fan = &fan;
724
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);
733                 iotcon_disconnect();
734                 return -1;
735         }
736
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);
746                 iotcon_disconnect();
747                 return -1;
748         }
749
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);
758                 iotcon_disconnect();
759                 return -1;
760         }
761
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);
772                 iotcon_disconnect();
773                 return -1;
774         }
775
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);
785                 iotcon_disconnect();
786                 return -1;
787         }
788
789         g_main_loop_run(loop);
790         g_main_loop_unref(loop);
791
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);
798
799         /* disconnect iotcon */
800         iotcon_disconnect();
801
802         return 0;
803 }