From 3787f6eab7ad665c7625f68e40c644322f25e21d Mon Sep 17 00:00:00 2001 From: youngman Date: Fri, 13 Nov 2015 14:52:15 +0900 Subject: [PATCH] modify server-side test Change-Id: I2b90ed7014a5721f3e6c7e9dd7ff187f7e014b07 Signed-off-by: youngman --- test/iotcon-test-basic-client.c | 4 +- test/iotcon-test-basic-server.c | 356 +++++++------ test/iotcon-test-repr-client.c | 14 +- test/iotcon-test-repr-server.c | 854 ++++++++++++++++++++------------ 4 files changed, 719 insertions(+), 509 deletions(-) diff --git a/test/iotcon-test-basic-client.c b/test/iotcon-test-basic-client.c index 1eba3da..ee34db4 100644 --- a/test/iotcon-test-basic-client.c +++ b/test/iotcon-test-basic-client.c @@ -570,8 +570,8 @@ static void _found_resource(iotcon_remote_resource_h resource, iotcon_error_e re } /* Set NOTIFY callback */ - ret = iotcon_remote_resource_set_notify_cb(resource_clone, IOTCON_OBSERVE_IGNORE_OUT_OF_ORDER, NULL, - _on_response, NULL); + ret = iotcon_remote_resource_set_notify_cb(resource_clone, + IOTCON_OBSERVE_IGNORE_OUT_OF_ORDER, NULL, _on_response, NULL); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_remote_resource_set_notify_cb() Fail(%d)", ret); device_id_list = g_list_remove(device_id_list, door_resource_device_id); diff --git a/test/iotcon-test-basic-server.c b/test/iotcon-test-basic-server.c index 7891df7..b235ec7 100644 --- a/test/iotcon-test-basic-server.c +++ b/test/iotcon-test-basic-server.c @@ -22,6 +22,10 @@ #include #include "test.h" +#define DOOR_RESOURCE_URI "/door/1" +#define DOOR_RESOURCE_URI2 "/door/2" +#define DOOR_RESOURCE_TYPE "org.tizen.door" + /* Door Resource */ typedef struct _door_resource_s { bool state; @@ -34,11 +38,7 @@ typedef struct _door_resource_s { iotcon_representation_h repr; } door_resource_s; -static bool resource_created; - -#define DOOR_RESOURCE_URI "/door/1" -#define DOOR_RESOURCE_URI2 "/door/2" -#define DOOR_RESOURCE_TYPE "org.tizen.door" +static bool _resource_created; static void _request_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data); @@ -106,8 +106,8 @@ static iotcon_resource_h _create_door_resource(char *uri_path, char *type, int i ret = iotcon_resource_types_add(resource_types, type); if (IOTCON_ERROR_NONE != ret) { - iotcon_resource_types_destroy(resource_types); ERR("iotcon_resource_types_add() Fail(%d)", ret); + iotcon_resource_types_destroy(resource_types); return NULL; } @@ -115,8 +115,8 @@ static iotcon_resource_h _create_door_resource(char *uri_path, char *type, int i ret = iotcon_resource_create(uri_path, resource_types, ifaces, properties, _request_handler, user_data, &handle); if (IOTCON_ERROR_NONE != ret) { - iotcon_resource_types_destroy(resource_types); ERR("iotcon_resource_create() Fail"); + iotcon_resource_types_destroy(resource_types); return NULL; } @@ -125,20 +125,29 @@ static iotcon_resource_h _create_door_resource(char *uri_path, char *type, int i return handle; } -static int _send_response(iotcon_response_h response, iotcon_representation_h repr, +static int _send_response(iotcon_request_h request, iotcon_representation_h repr, iotcon_response_result_e result) { int ret; + iotcon_response_h response; + + ret = iotcon_response_create(request, &response); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_create() Fail(%d)", ret); + return -1; + } ret = iotcon_response_set_result(response, result); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_response_set_result() Fail(%d)", ret); + iotcon_response_destroy(response); return -1; } ret = iotcon_response_set_representation(response, IOTCON_INTERFACE_DEFAULT, repr); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_response_set_representation() Fail(%d)", ret); + iotcon_response_destroy(response); return -1; } @@ -146,141 +155,143 @@ static int _send_response(iotcon_response_h response, iotcon_representation_h re ret = iotcon_response_send(response); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_response_send() Fail(%d)", ret); + iotcon_response_destroy(response); return -1; } + iotcon_response_destroy(response); + return 0; } -static void _request_handler_get(door_resource_s *door, iotcon_response_h response) +static iotcon_representation_h _get_door_representation(door_resource_s *door) { int ret; - iotcon_representation_h resp_repr; - iotcon_state_h resp_state; - INFO("GET request"); + iotcon_state_h state; + iotcon_representation_h repr; /* create a door Representation */ - ret = iotcon_representation_create(&resp_repr); + ret = iotcon_representation_create(&repr); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_representation_create() Fail(%d)", ret); - return; + return NULL; } /* create a door state */ - ret = iotcon_state_create(&resp_state); + ret = iotcon_state_create(&state); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_state_create() Fail(%d)", ret); - iotcon_representation_destroy(resp_repr); - return; + iotcon_representation_destroy(repr); + return NULL; } - ret = iotcon_representation_set_uri_path(resp_repr, door->uri_path); + ret = iotcon_representation_set_uri_path(repr, door->uri_path); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_representation_set_uri_path() Fail(%d)", ret); - iotcon_state_destroy(resp_state); - iotcon_representation_destroy(resp_repr); - return; + iotcon_state_destroy(state); + iotcon_representation_destroy(repr); + return NULL; } - ret = iotcon_state_set_bool(resp_state, "opened", door->state); + ret = iotcon_state_set_bool(state, "opened", door->state); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_state_set_bool() Fail(%d)", ret); - iotcon_state_destroy(resp_state); - iotcon_representation_destroy(resp_repr); - return; + iotcon_state_destroy(state); + iotcon_representation_destroy(repr); + return NULL; } - ret = iotcon_representation_set_state(resp_repr, resp_state); + ret = iotcon_representation_set_state(repr, state); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_representation_set_state() Fail(%d)", ret); - iotcon_state_destroy(resp_state); - iotcon_representation_destroy(resp_repr); - return; + iotcon_state_destroy(state); + iotcon_representation_destroy(repr); + return NULL; } - iotcon_state_destroy(resp_state); + iotcon_state_destroy(state); - ret = _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_OK); - if (0 != ret) + return repr; +} + +static int _request_handler_get(door_resource_s *door, iotcon_request_h request) +{ + int ret; + iotcon_representation_h resp_repr; + INFO("GET request"); + + resp_repr = _get_door_representation(door); + if (NULL == resp_repr) { + ERR("_get_door_representation() Fail"); + return -1; + } + + ret = _send_response(request, resp_repr, IOTCON_RESPONSE_RESULT_OK); + if (0 != ret) { ERR("_send_response() Fail(%d)", ret); + iotcon_representation_destroy(resp_repr); + return -1; + } iotcon_representation_destroy(resp_repr); + + return 0; } -static void _request_handler_put(door_resource_s *door, iotcon_request_h request, - iotcon_response_h response) +static int _set_door_representation(door_resource_s *door, iotcon_representation_h repr) { int ret; bool bval; - iotcon_representation_h req_repr, resp_repr; - iotcon_state_h req_state, resp_state;; - INFO("PUT request"); - - ret = iotcon_request_get_representation(request, &req_repr); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_request_get_representation() Fail(%d)", ret); - return; - } + iotcon_state_h state; - ret = iotcon_representation_get_state(req_repr, &req_state); + ret = iotcon_representation_get_state(repr, &state); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_representation_get_state() Fail(%d)", ret); - return; + return -1; } - ret = iotcon_state_get_bool(req_state, "opened", &bval); + ret = iotcon_state_get_bool(state, "opened", &bval); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_state_get_bool() Fail(%d)", ret); - return; + return -1; } door->state = bval; - _check_door_state(*door); + return 0; +} - ret = iotcon_representation_create(&resp_repr); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_representation_create() Fail(%d)", ret); - return; - } +static int _request_handler_put(door_resource_s *door, iotcon_request_h request) +{ + int ret; + iotcon_representation_h req_repr, resp_repr; + INFO("PUT request"); - ret = iotcon_representation_set_uri_path(resp_repr, door->uri_path); + ret = iotcon_request_get_representation(request, &req_repr); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_representation_set_uri_path() Fail(%d)", ret); - iotcon_representation_destroy(resp_repr); - return; + ERR("iotcon_request_get_representation() Fail(%d)", ret); + return -1; } - ret = iotcon_state_create(&resp_state); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_state_create() Fail(%d)", ret); - iotcon_representation_destroy(resp_repr); - return; + ret = _set_door_representation(door, req_repr); + if (0 != ret) { + ERR("_set_door_representation() Fail(%d)", ret); + return -1; } - ret = iotcon_state_set_bool(resp_state, "opened", door->state); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_state_set_bool() Fail(%d)", ret); - iotcon_state_destroy(resp_state); - iotcon_representation_destroy(resp_repr); - return; - } + _check_door_state(*door); - ret = iotcon_representation_set_state(resp_repr, resp_state); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_representation_set_state() Fail(%d)", ret); - iotcon_state_destroy(resp_state); - iotcon_representation_destroy(resp_repr); - return; + resp_repr = _get_door_representation(door); + if (NULL == resp_repr) { + ERR("_get_door_representation() Fail"); + return -1; } - iotcon_state_destroy(resp_state); - - ret = _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_OK); + ret = _send_response(request, resp_repr, IOTCON_RESPONSE_RESULT_OK); if (0 != ret) { ERR("_send_response() Fail(%d)", ret); iotcon_representation_destroy(resp_repr); - return; + return -1; } /* notify */ @@ -289,15 +300,16 @@ static void _request_handler_put(door_resource_s *door, iotcon_request_h request ERR("iotcon_resource_notify() Fail(%d)", ret); iotcon_representation_destroy(resp_repr); + + return 0; } static gboolean _door_state_changer(gpointer user_data) { int ret; static int i = 0; - door_resource_s *door = user_data; - iotcon_state_h state; iotcon_representation_h repr; + door_resource_s *door = user_data; if ((5 == i++) || NULL == door->observers) return G_SOURCE_REMOVE; @@ -310,39 +322,14 @@ static gboolean _door_state_changer(gpointer user_data) INFO("[Door] opened -> closed"); } - ret = iotcon_representation_create(&repr); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_representation_create() Fail(%d)", ret); - return G_SOURCE_REMOVE; - } - - ret = iotcon_state_create(&state); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_state_create() Fail(%d)", ret); - iotcon_representation_destroy(repr); - return G_SOURCE_REMOVE; - } - - ret = iotcon_state_set_bool(state, "opened", door->state); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_state_set_bool() Fail(%d)", ret); - iotcon_state_destroy(state); - iotcon_representation_destroy(repr); - return G_SOURCE_REMOVE; - } + INFO("NOTIFY!"); - ret = iotcon_representation_set_state(repr, state); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_representation_set_state() Fail(%d)", ret); - iotcon_state_destroy(state); - iotcon_representation_destroy(repr); + repr = _get_door_representation(door); + if (NULL == repr) { + ERR("_get_door_representation() Fail"); return G_SOURCE_REMOVE; } - iotcon_state_destroy(state); - - INFO("NOTIFY!"); - ret = iotcon_resource_notify(door->handle, repr, door->observers); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_resource_notify() Fail(%d)", ret); @@ -354,7 +341,7 @@ static gboolean _door_state_changer(gpointer user_data) return G_SOURCE_CONTINUE; } -static void _request_handler_post(door_resource_s *door, iotcon_response_h response) +static int _request_handler_post(door_resource_s *door, iotcon_request_h request) { int ret; iotcon_state_h resp_state; @@ -362,31 +349,31 @@ static void _request_handler_post(door_resource_s *door, iotcon_response_h respo iotcon_resource_h new_door_handle; INFO("POST request"); - if (resource_created) { - INFO("Resource is already created"); - return; + if (_resource_created) { + ERR("Resource(%s) is already created", DOOR_RESOURCE_URI2); + return -1; } new_door_handle = _create_door_resource(DOOR_RESOURCE_URI2, door->type, IOTCON_INTERFACE_DEFAULT, (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), NULL); if (NULL == new_door_handle) { ERR("_create_door_resource() Fail"); - return; + return -1; } - resource_created = true; + _resource_created = true; /* send information that new resource was created */ ret = iotcon_representation_create(&resp_repr); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_representation_create() Fail(%d)", ret); - return; + return -1; } ret = iotcon_state_create(&resp_state); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_state_create() Fail(%d)", ret); iotcon_representation_destroy(resp_repr); - return; + return -1; } ret = iotcon_state_set_str(resp_state, "createduripath", DOOR_RESOURCE_URI2); @@ -394,7 +381,7 @@ static void _request_handler_post(door_resource_s *door, iotcon_response_h respo ERR("iotcon_state_set_str() Fail(%d)", ret); iotcon_state_destroy(resp_state); iotcon_representation_destroy(resp_repr); - return; + return -1; } ret = iotcon_representation_set_state(resp_repr, resp_state); @@ -402,51 +389,80 @@ static void _request_handler_post(door_resource_s *door, iotcon_response_h respo ERR("iotcon_representation_set_state() Fail(%d)", ret); iotcon_state_destroy(resp_state); iotcon_representation_destroy(resp_repr); - return; + return -1; } iotcon_state_destroy(resp_state); - ret = _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_RESOURCE_CREATED); + ret = _send_response(request, resp_repr, IOTCON_RESPONSE_RESULT_RESOURCE_CREATED); if (0 != ret) { ERR("_send_response() Fail(%d)", ret); iotcon_representation_destroy(resp_repr); - return; + return -1; } iotcon_representation_destroy(resp_repr); - /* add observe */ - g_timeout_add_seconds(5, _door_state_changer, door); + return 0; } -static void _request_handler_delete(iotcon_resource_h resource, - iotcon_response_h response) +static int _request_handler_delete(iotcon_resource_h resource, iotcon_request_h request) { int ret; - iotcon_representation_h resp_repr = NULL; - iotcon_response_result_e result = IOTCON_RESPONSE_RESULT_OK; INFO("DELETE request"); ret = iotcon_resource_destroy(resource); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_unregiser_resource() Fail(%d)", ret); - return; + return -1; } - result = IOTCON_RESPONSE_RESULT_RESOURCE_DELETED; + ret = _send_response(request, NULL, IOTCON_RESPONSE_RESULT_RESOURCE_DELETED); + if (0 != ret) { + ERR("_send_response() Fail(%d)", ret); + return -1; + } - ret = iotcon_representation_create(&resp_repr); + return 0; +} + +static int _request_handler_observe(door_resource_s *door, iotcon_request_h request) +{ + int ret; + int observe_id; + iotcon_observe_action_e observe_action; + + ret = iotcon_request_get_observe_action(request, &observe_action); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_representation_create() Fail(%d)", ret); - return; + ERR("iotcon_request_get_observe_action() Fail(%d)", ret); + return -1; } - ret = _send_response(response, resp_repr, result); - if (0 != ret) - ERR("_send_response() Fail(%d)", ret); + if (IOTCON_OBSERVE_REGISTER == observe_action) { + ret = iotcon_request_get_observe_id(request, &observe_id); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_request_get_observe_id() Fail(%d)", ret); + return -1; + } + ret = iotcon_observers_add(door->observers, observe_id); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_observers_add() Fail(%d)", ret); + return -1; + } + } else if (IOTCON_OBSERVE_DEREGISTER == observe_action) { + ret = iotcon_request_get_observe_id(request, &observe_id); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_request_get_observe_id() Fail(%d)", ret); + return -1; + } + ret = iotcon_observers_remove(door->observers, observe_id); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_observers_remove() Fail(%d)", ret); + return -1; + } + } - iotcon_representation_destroy(resp_repr); + return 0; } static bool _query_cb(const char *key, const char *value, void *user_data) @@ -459,18 +475,17 @@ static bool _query_cb(const char *key, const char *value, void *user_data) static void _request_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data) { - door_resource_s *door; - iotcon_query_h query; - iotcon_response_h response = NULL; - int ret, types, observe_id; - iotcon_observe_action_e observe_action; + int ret, types; char *host_address; + iotcon_query_h query; + door_resource_s *door; RET_IF(NULL == request); ret = iotcon_request_get_host_address(request, &host_address); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_request_get_host_address() Fail(%d)", ret); + _send_response(request, NULL, IOTCON_RESPONSE_RESULT_ERROR); return; } INFO("host_address : %s", host_address); @@ -478,6 +493,7 @@ static void _request_handler(iotcon_resource_h resource, iotcon_request_h reques ret = iotcon_request_get_query(request, &query); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_request_get_query() Fail(%d)", ret); + _send_response(request, NULL, IOTCON_RESPONSE_RESULT_ERROR); return; } if (query) @@ -486,62 +502,31 @@ static void _request_handler(iotcon_resource_h resource, iotcon_request_h reques ret = iotcon_request_get_types(request, &types); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_request_get_types() Fail(%d)", ret); - return; - } - - ret = iotcon_response_create(request, &response); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_response_create() Fail(%d)", ret); + _send_response(request, NULL, IOTCON_RESPONSE_RESULT_ERROR); return; } door = user_data; if (IOTCON_REQUEST_GET & types) - _request_handler_get(door, response); + ret = _request_handler_get(door, request); else if (IOTCON_REQUEST_PUT & types) - _request_handler_put(door, request, response); + ret = _request_handler_put(door, request); else if (IOTCON_REQUEST_POST & types) - _request_handler_post(door, response); + ret = _request_handler_post(door, request); else if (IOTCON_REQUEST_DELETE & types) - _request_handler_delete(resource, response); - - iotcon_response_destroy(response); - - if (IOTCON_REQUEST_OBSERVE & types) { - ret = iotcon_request_get_observe_action(request, &observe_action); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_request_get_observe_action() Fail(%d)", ret); - return; - } + ret = _request_handler_delete(resource, request); - if (IOTCON_OBSERVE_REGISTER == observe_action) { - ret = iotcon_request_get_observe_id(request, &observe_id); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_request_get_observe_id() Fail(%d)", ret); - return; - } - ret = iotcon_observers_add(door->observers, observe_id); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_observers_add() Fail(%d)", ret); - return; - } - } else if (IOTCON_OBSERVE_DEREGISTER == observe_action) { - ret = iotcon_request_get_observe_id(request, &observe_id); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_request_get_observe_id() Fail(%d)", ret); - return; - } - ret = iotcon_observers_remove(door->observers, observe_id); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_observers_remove() Fail(%d)", ret); - return; - } - } + if (0 != ret) { + _send_response(request, NULL, IOTCON_RESPONSE_RESULT_ERROR); + return; } + + if (IOTCON_REQUEST_OBSERVE & types) + _request_handler_observe(door,request); } static gboolean _presence_timer(gpointer user_data) @@ -603,6 +588,9 @@ int main(int argc, char **argv) _check_door_state(my_door); + /* add observe */ + g_timeout_add_seconds(5, _door_state_changer, &my_door); + g_main_loop_run(loop); g_main_loop_unref(loop); diff --git a/test/iotcon-test-repr-client.c b/test/iotcon-test-repr-client.c index 3588b9f..a14e8b1 100644 --- a/test/iotcon-test-repr-client.c +++ b/test/iotcon-test-repr-client.c @@ -25,7 +25,7 @@ static GList *device_id_list; #define ROOM_RESOURCE_TYPE "org.tizen.room" #define ROOM_RESOURCE_URI_PREFIX "/room" #define LIGHT_RESOURCE_URI_PREFIX "/light" -#define SWITCH_RESOURCE_URI_PREFIX "/switch" +#define FAN_RESOURCE_URI_PREFIX "/fan" static bool _get_int_list_cb(int pos, const int value, void *user_data) { @@ -141,20 +141,20 @@ static void _print_repr(iotcon_representation_h recv_repr) } DBG("brightness : %d", int_val); } - } else if (TEST_STR_EQUAL == strncmp(SWITCH_RESOURCE_URI_PREFIX, uri_path, - strlen(SWITCH_RESOURCE_URI_PREFIX))) { + } else if (TEST_STR_EQUAL == strncmp(FAN_RESOURCE_URI_PREFIX, uri_path, + strlen(FAN_RESOURCE_URI_PREFIX))) { ret = iotcon_state_get_keys_count(child_state, &key_count); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_state_get_keys_count() Fail(%d)", ret); continue; } if (key_count) { - ret = iotcon_state_get_bool(child_state, "switch", &bool_val); + ret = iotcon_state_get_bool(child_state, "state", &bool_val); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_state_get_bool() Fail(%d)", ret); continue; } - DBG("switch : %d", bool_val); + DBG("state : %d", bool_val); } } } @@ -228,9 +228,9 @@ static void _on_response_1st(iotcon_remote_resource_h resource, return; } - ret = iotcon_query_add(query_params, "if", "oic.if.b"); + ret = iotcon_query_set_interface(query_params, IOTCON_INTERFACE_BATCH); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_query_add() Fail(%d)", ret); + ERR("iotcon_query_set_interface() Fail(%d)", ret); iotcon_query_destroy(query_params); return; } diff --git a/test/iotcon-test-repr-server.c b/test/iotcon-test-repr-server.c index 2d78ae7..a924150 100644 --- a/test/iotcon-test-repr-server.c +++ b/test/iotcon-test-repr-server.c @@ -20,430 +20,622 @@ #include #include "test.h" -#define LIGHT_RESOURCE_URI "/light/1" -#define LIGHT_RESOURCE_TYPE "org.tizen.light" - #define ROOM_RESOURCE_URI "/room/1" #define ROOM_RESOURCE_TYPE "org.tizen.room" - -#define SWITCH_RESOURCE_URI "/switch/1" - -static int _send_response(iotcon_response_h response, iotcon_representation_h repr, - iotcon_interface_e iface) +#define LIGHT_RESOURCE_URI "/light/1" +#define LIGHT_RESOURCE_TYPE "org.tizen.light" +#define FAN_RESOURCE_URI "/fan/1" +#define FAN_RESOURCE_TYPE "org.tizen.fan" + +/* Light Resource */ +typedef struct _light_resource_s { + int brightness; + char *uri_path; + char *type; + int ifaces; + int properties; + iotcon_resource_h handle; +} light_resource_s; + +/* Fan Resource */ +typedef struct _fan_resource_s { + bool state; + char *uri_path; + char *type; + int ifaces; + int properties; + iotcon_resource_h handle; +} fan_resource_s; + +/* Room Resource */ +typedef struct _room_resource_s { + char *name; + int today_temp[5]; + char *uri_path; + char *type; + int ifaces; + int properties; + iotcon_resource_h handle; + light_resource_s *child_light; + fan_resource_s *child_fan; +} room_resource_s; + +static int _set_room_resource(room_resource_s *room) { - int ret; - - ret = iotcon_response_set_representation(response, iface, repr); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_response_set_representation() Fail(%d)", ret); + room->name = strdup("Michael's Room"); + if (NULL == room->name) { + ERR("strdup() Fail"); return -1; } - ret = iotcon_response_set_result(response, IOTCON_RESPONSE_RESULT_OK); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_response_set_result() Fail(%d)", ret); + room->today_temp[0] = 13; + room->today_temp[1] = 19; + room->today_temp[2] = 24; + room->today_temp[3] = 21; + room->today_temp[4] = 14; + + room->uri_path = strdup(ROOM_RESOURCE_URI); + if (NULL == room->uri_path) { + ERR("strdup(%s) Fail", ROOM_RESOURCE_URI); + free(room->name); return -1; } - /* send Representation to the client */ - ret = iotcon_response_send(response); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_response_send() Fail(%d)", ret); + room->type = strdup(ROOM_RESOURCE_TYPE); + if (NULL == room->type) { + ERR("strdup(%s) Fail", ROOM_RESOURCE_TYPE); + free(room->uri_path); + free(room->name); return -1; } + room->ifaces = IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_BATCH; + room->properties = IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE; + return 0; } -static void _light_request_handler_get(iotcon_response_h response) +static void _free_room_resource(room_resource_s *room) { - int ret; - iotcon_representation_h resp_repr; + free(room->type); + free(room->uri_path); + free(room->name); +} - INFO("GET request - Light"); +static int _set_light_resource(light_resource_s *light) +{ + light->brightness = 50; - /* create a light Representation */ - ret = iotcon_representation_create(&resp_repr); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_representation_create() Fail(%d)", ret); - return; + light->uri_path = strdup(LIGHT_RESOURCE_URI); + if (NULL == light->uri_path) { + ERR("strdup(%s) Fail", LIGHT_RESOURCE_URI); + return -1; } - ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT); - if (0 != ret) - ERR("_send_response() Fail(%d)", ret); + light->type = strdup(LIGHT_RESOURCE_TYPE); + if (NULL == light->type) { + ERR("strdup(%s) Fail", LIGHT_RESOURCE_TYPE); + free(light->uri_path); + return -1; + } + + light->ifaces = IOTCON_INTERFACE_DEFAULT; + light->properties = IOTCON_HIDDEN; - iotcon_representation_destroy(resp_repr); + return 0; } -static void _room_request_handler_get(iotcon_request_h request, - iotcon_response_h response) +static void _free_light_resource(light_resource_s *light) { - int ret; - iotcon_representation_h room_repr, light_repr, switch_repr; - iotcon_state_h room_state, light_state, switch_state; - iotcon_list_h temperature_list; + free(light->type); + free(light->uri_path); +} - iotcon_query_h query; - char *query_str = NULL; +static int _set_fan_resource(fan_resource_s *fan) +{ + fan->state = false; - iotcon_interface_e iface; + fan->uri_path = strdup(FAN_RESOURCE_URI); + if (NULL == fan->uri_path) { + ERR("strdup(%s) Fail", FAN_RESOURCE_URI); + return -1; + } - INFO("GET request - Room"); + fan->type = strdup(FAN_RESOURCE_TYPE); + if (NULL == fan->type) { + ERR("strdup(%s) Fail", FAN_RESOURCE_TYPE); + free(fan->uri_path); + return -1; + } - /* create a room Representation */ - ret = iotcon_representation_create(&room_repr); + fan->ifaces = IOTCON_INTERFACE_DEFAULT; + fan->properties = IOTCON_HIDDEN; + + return 0; +} + +static void _free_fan_resource(fan_resource_s *fan) +{ + free(fan->type); + free(fan->uri_path); +} + +static iotcon_resource_h _create_resource(char *uri_path, char *type, int ifaces, + int properties, iotcon_request_handler_cb cb, void *user_data) +{ + int ret; + iotcon_resource_h handle; + iotcon_resource_types_h resource_types; + + ret = iotcon_resource_types_create(&resource_types); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_representation_create() Fail(%d)", ret); - return; + ERR("iotcon_resource_types_create() Fail(%d)", ret); + return NULL; } - /* create a room state */ - ret = iotcon_state_create(&room_state); + ret = iotcon_resource_types_add(resource_types, type); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_state_create() Fail(%d)", ret); - iotcon_representation_destroy(room_repr); - return; + ERR("iotcon_resource_types_add() Fail(%d)", ret); + iotcon_resource_types_destroy(resource_types); + return NULL; } - ret = iotcon_representation_set_uri_path(room_repr, ROOM_RESOURCE_URI); + ret = iotcon_resource_create(uri_path, resource_types, ifaces, properties, cb, + user_data, &handle); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_representation_set_uri_path() Fail(%d)", ret); - iotcon_state_destroy(room_state); - iotcon_representation_destroy(room_repr); - return; + ERR("iotcon_response_create() Fail(%d)", ret); + iotcon_resource_types_destroy(resource_types); + return NULL; } - ret = iotcon_state_set_str(room_state, "name", "Michael's Room"); + iotcon_resource_types_destroy(resource_types); + + return handle; +} + +static int _send_response(iotcon_request_h request, iotcon_representation_h repr, + iotcon_interface_e iface, iotcon_response_result_e result) +{ + int ret; + iotcon_response_h response; + + ret = iotcon_response_create(request, &response); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_state_set_str() Fail(%d)", ret); - iotcon_state_destroy(room_state); - iotcon_representation_destroy(room_repr); - return; + ERR("iotcon_response_create() Fail(%d)", ret); + return -1; } - /* set null */ - ret = iotcon_state_set_null(room_state, "null value"); + ret = iotcon_response_set_representation(response, iface, repr); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_state_set_null() Fail(%d)", ret); - iotcon_state_destroy(room_state); - iotcon_representation_destroy(room_repr); - return; + ERR("iotcon_response_set_representation() Fail(%d)", ret); + iotcon_response_destroy(response); + return -1; } - ret = iotcon_list_create(IOTCON_TYPE_INT, &temperature_list); + ret = iotcon_response_set_result(response, result); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_list_create() Fail(%d)", ret); - iotcon_state_destroy(room_state); - iotcon_representation_destroy(room_repr); - return; + ERR("iotcon_response_set_result() Fail(%d)", ret); + iotcon_response_destroy(response); + return -1; } - ret = iotcon_list_add_int(temperature_list, 22, -1); + /* send Representation to the client */ + ret = iotcon_response_send(response); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_list_add_int() Fail(%d)", ret); - iotcon_state_destroy(room_state); - iotcon_representation_destroy(room_repr); - return; + ERR("iotcon_response_send() Fail(%d)", ret); + iotcon_response_destroy(response); + return -1; } - ret = iotcon_list_add_int(temperature_list, 23, -1); + iotcon_response_destroy(response); + + return 0; +} + +static iotcon_representation_h _get_light_representation(light_resource_s *light) +{ + int ret; + iotcon_state_h state; + iotcon_representation_h repr; + + /* create a light Representation */ + ret = iotcon_representation_create(&repr); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_list_add_int() Fail(%d)", ret); - iotcon_state_destroy(room_state); - iotcon_representation_destroy(room_repr); - return; + ERR("iotcon_representation_create() Fail(%d)", ret); + return NULL; } - ret = iotcon_list_add_int(temperature_list, 24, -1); + ret = iotcon_representation_set_uri_path(repr, light->uri_path); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_list_add_int() Fail(%d)", ret); - iotcon_state_destroy(room_state); - iotcon_representation_destroy(room_repr); - return; + ERR("iotcon_representation_set_uri_path() Fail(%d)", ret); + iotcon_representation_destroy(repr); + return NULL; } - ret = iotcon_list_add_int(temperature_list, 25, -1); + /* create a light state */ + ret = iotcon_state_create(&state); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_list_add_int() Fail(%d)", ret); - iotcon_state_destroy(room_state); - iotcon_representation_destroy(room_repr); - return; + ERR("iotcon_state_create() Fail(%d)", ret); + iotcon_representation_destroy(repr); + return NULL; } - ret = iotcon_list_add_int(temperature_list, 26, -1); + ret = iotcon_state_set_int(state, "brightness", light->brightness); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_list_add_int() Fail(%d)", ret); - iotcon_state_destroy(room_state); - iotcon_representation_destroy(room_repr); - return; + ERR("iotcon_state_set_int() Fail(%d)", ret); + iotcon_state_destroy(state); + iotcon_representation_destroy(repr); + return NULL; } - ret = iotcon_state_set_list(room_state, "today_temp", temperature_list); + /* Set a light state into light Representation */ + ret = iotcon_representation_set_state(repr, state); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_state_set_list() Fail(%d)", ret); - iotcon_state_destroy(room_state); - iotcon_representation_destroy(room_repr); - return; + ERR("iotcon_representation_set_state() Fail(%d)", ret); + iotcon_state_destroy(state); + iotcon_representation_destroy(repr); + return NULL; } - iotcon_list_destroy(temperature_list); + iotcon_state_destroy(state); - /* Set a room state into room Representation */ - ret = iotcon_representation_set_state(room_repr, room_state); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_representation_set_state() Fail(%d)", ret); - iotcon_state_destroy(room_state); - iotcon_representation_destroy(room_repr); - return; + return repr; +} + +static int _light_request_handler_get(light_resource_s *light, iotcon_request_h request) +{ + int ret; + iotcon_representation_h repr; + + INFO("GET request - Light"); + + repr = _get_light_representation(light); + if (NULL == repr) { + ERR("_get_light_representation() Fail"); + return -1; } - iotcon_state_destroy(room_state); + ret = _send_response(request, repr, IOTCON_INTERFACE_DEFAULT, + IOTCON_RESPONSE_RESULT_OK); + if (0 != ret) { + ERR("_send_response() Fail(%d)", ret); + iotcon_representation_destroy(repr); + return -1; + } - /* create a light Representation */ - ret = iotcon_representation_create(&light_repr); + iotcon_representation_destroy(repr); + + return 0; +} + + +static iotcon_representation_h _get_fan_representation(fan_resource_s *fan) +{ + int ret; + iotcon_state_h state; + iotcon_representation_h repr; + + /* create a fan Representation */ + ret = iotcon_representation_create(&repr); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_representation_create() Fail(%d)", ret); - iotcon_representation_destroy(room_repr); - return; + return NULL; } - ret = iotcon_representation_set_uri_path(light_repr, LIGHT_RESOURCE_URI); + ret = iotcon_representation_set_uri_path(repr, fan->uri_path); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_representation_set_uri_path() Fail(%d)", ret); - iotcon_representation_destroy(light_repr); - iotcon_representation_destroy(room_repr); - return; + iotcon_representation_destroy(repr); + return NULL; } - /* create a light state */ - ret = iotcon_state_create(&light_state); + /* create a fan state */ + ret = iotcon_state_create(&state); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_state_create() Fail(%d)", ret); - iotcon_state_destroy(light_state); - iotcon_representation_destroy(light_repr); - iotcon_representation_destroy(room_repr); - return; + iotcon_representation_destroy(repr); + return NULL; } - ret = iotcon_state_set_int(light_state, "brightness", 50); + ret = iotcon_state_set_bool(state, "state", fan->state); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_state_set_int() Fail(%d)", ret); - iotcon_state_destroy(light_state); - iotcon_representation_destroy(light_repr); - iotcon_representation_destroy(room_repr); - return; + ERR("iotcon_state_set_bool() Fail(%d)", ret); + iotcon_state_destroy(state); + iotcon_representation_destroy(repr); + return NULL; } /* Set a light state into light Representation */ - ret = iotcon_representation_set_state(light_repr, light_state); + ret = iotcon_representation_set_state(repr, state); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_representation_set_state() Fail(%d)", ret); - iotcon_state_destroy(light_state); - iotcon_representation_destroy(light_repr); - iotcon_representation_destroy(room_repr); - return; + iotcon_state_destroy(state); + iotcon_representation_destroy(repr); + return NULL; } - iotcon_state_destroy(light_state); + iotcon_state_destroy(state); - ret = iotcon_representation_append_child(room_repr, light_repr); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_representation_append_child() Fail(%d)", ret); - iotcon_representation_destroy(light_repr); - iotcon_representation_destroy(room_repr); - return; + return repr; +} + +static int _fan_request_handler_get(fan_resource_s *fan, iotcon_request_h request) +{ + int ret; + iotcon_representation_h repr; + + INFO("GET request - Fan"); + + repr = _get_fan_representation(fan); + if (NULL == repr) { + ERR("_get_fan_representation() Fail"); + return -1; } - iotcon_representation_destroy(light_repr); + ret = _send_response(request, repr, IOTCON_INTERFACE_DEFAULT, + IOTCON_RESPONSE_RESULT_OK); + if (0 != ret) { + ERR("_send_response() Fail(%d)", ret); + iotcon_representation_destroy(repr); + return -1; + } + + iotcon_representation_destroy(repr); - /* create a switch Representation */ - ret = iotcon_representation_create(&switch_repr); + return 0; +} + +static iotcon_representation_h _get_room_representation(room_resource_s *room) +{ + int ret; + iotcon_state_h state; + iotcon_list_h today_temp; + iotcon_representation_h repr, light_repr, fan_repr; + + /* create a room Representation */ + ret = iotcon_representation_create(&repr); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_representation_create() Fail(%d)", ret); - iotcon_representation_destroy(room_repr); - return; + return NULL; } - ret = iotcon_representation_set_uri_path(switch_repr, SWITCH_RESOURCE_URI); + ret = iotcon_representation_set_uri_path(repr, room->uri_path); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_representation_set_uri_path() Fail(%d)", ret); - iotcon_representation_destroy(switch_repr); - iotcon_representation_destroy(room_repr); - return; + iotcon_representation_destroy(repr); + return NULL; } - /* create a switch state */ - ret = iotcon_state_create(&switch_state); + /* create a room state */ + ret = iotcon_state_create(&state); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_state_create() Fail(%d)", ret); - iotcon_representation_destroy(switch_repr); - iotcon_representation_destroy(room_repr); - return; + iotcon_representation_destroy(repr); + return NULL; } - ret = iotcon_state_set_bool(switch_state, "switch", false); + ret = iotcon_state_set_str(state, "name", room->name); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_state_set_bool() Fail(%d)", ret); - iotcon_state_destroy(switch_state); - iotcon_representation_destroy(switch_repr); - iotcon_representation_destroy(room_repr); - return; + ERR("iotcon_state_set_str() Fail(%d)", ret); + iotcon_state_destroy(state); + iotcon_representation_destroy(repr); + return NULL; } - /* Set a light state into light Representation */ - ret = iotcon_representation_set_state(switch_repr, switch_state); + /* set null */ + ret = iotcon_state_set_null(state, "null value"); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_representation_set_state() Fail(%d)", ret); - iotcon_state_destroy(switch_state); - iotcon_representation_destroy(switch_repr); - iotcon_representation_destroy(room_repr); - return; + ERR("iotcon_state_set_null() Fail(%d)", ret); + iotcon_state_destroy(state); + iotcon_representation_destroy(repr); + return NULL; } - iotcon_state_destroy(switch_state); - - ret = iotcon_representation_append_child(room_repr, switch_repr); + ret = iotcon_list_create(IOTCON_TYPE_INT, &today_temp); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_representation_append_child() Fail(%d)", ret); - iotcon_representation_destroy(switch_repr); - iotcon_representation_destroy(room_repr); - return; + ERR("iotcon_list_create() Fail(%d)", ret); + iotcon_state_destroy(state); + iotcon_representation_destroy(repr); + return NULL; } - iotcon_representation_destroy(switch_repr); + ret = iotcon_list_add_int(today_temp, room->today_temp[0], -1); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_list_add_int() Fail(%d)", ret); + iotcon_list_destroy(today_temp); + iotcon_state_destroy(state); + iotcon_representation_destroy(repr); + return NULL; + } - ret = iotcon_request_get_query(request, &query); + ret = iotcon_list_add_int(today_temp, room->today_temp[1], -1); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_request_get_query() Fail(%d)", ret); - iotcon_representation_destroy(room_repr); - return; + ERR("iotcon_list_add_int() Fail(%d)", ret); + iotcon_list_destroy(today_temp); + iotcon_state_destroy(state); + iotcon_representation_destroy(repr); + return NULL; } - if (query) { - ret = iotcon_query_lookup(query, "if", &query_str); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_query_lookup() Fail(%d)", ret); - iotcon_representation_destroy(room_repr); - return; - } + ret = iotcon_list_add_int(today_temp, room->today_temp[2], -1); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_list_add_int() Fail(%d)", ret); + iotcon_list_destroy(today_temp); + iotcon_state_destroy(state); + iotcon_representation_destroy(repr); + return NULL; } - if (query_str && (TEST_STR_EQUAL == strcmp("oic.if.b", query_str))) { - DBG("operation for BATCH interface"); - iface = IOTCON_INTERFACE_BATCH; - } else { - DBG("operation for DEFAULT interface"); - iface = IOTCON_INTERFACE_DEFAULT; + ret = iotcon_list_add_int(today_temp, room->today_temp[3], -1); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_list_add_int() Fail(%d)", ret); + iotcon_list_destroy(today_temp); + iotcon_state_destroy(state); + iotcon_representation_destroy(repr); + return NULL; } - ret = _send_response(response, room_repr, iface); - if (0 != ret) - ERR("_send_response() Fail(%d)", ret); + ret = iotcon_list_add_int(today_temp, room->today_temp[4], -1); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_list_add_int() Fail(%d)", ret); + iotcon_list_destroy(today_temp); + iotcon_state_destroy(state); + iotcon_representation_destroy(repr); + return NULL; + } - iotcon_representation_destroy(room_repr); -} + ret = iotcon_state_set_list(state, "today_temp", today_temp); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_state_set_list() Fail(%d)", ret); + iotcon_list_destroy(today_temp); + iotcon_state_destroy(state); + iotcon_representation_destroy(repr); + return NULL; + } -static void _request_handler_put(iotcon_request_h request, iotcon_response_h response) -{ - int ret; - iotcon_representation_h resp_repr; + iotcon_list_destroy(today_temp); - ret = iotcon_representation_create(&resp_repr); + /* Set a room state into room Representation */ + ret = iotcon_representation_set_state(repr, state); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_representation_create() Fail(%d)", ret); - return; + ERR("iotcon_representation_set_state() Fail(%d)", ret); + iotcon_state_destroy(state); + iotcon_representation_destroy(repr); + return NULL; } - INFO("PUT request"); + iotcon_state_destroy(state); - /* do PUT operation */ + light_repr = _get_light_representation(room->child_light); + if (NULL == light_repr) { + ERR("_get_light_representation() fail"); + iotcon_representation_destroy(repr); + return NULL; + } - ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT); - if (0 != ret) - ERR("_send_response() Fail(%d)", ret); + ret = iotcon_representation_append_child(repr, light_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_append_child() Fail(%d)", ret); + iotcon_representation_destroy(light_repr); + iotcon_representation_destroy(repr); + return NULL; + } - iotcon_representation_destroy(resp_repr); -} + iotcon_representation_destroy(light_repr); -static void _request_handler_post(iotcon_response_h response) -{ - int ret; - iotcon_representation_h resp_repr; + fan_repr = _get_fan_representation(room->child_fan); + if (NULL == fan_repr) { + ERR("_get_fan_representation() fail"); + iotcon_representation_destroy(repr); + return NULL; + } - ret = iotcon_representation_create(&resp_repr); + ret = iotcon_representation_append_child(repr, fan_repr); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_representation_create() Fail(%d)", ret); - return; + ERR("iotcon_representation_append_child() Fail(%d)", ret); + iotcon_representation_destroy(fan_repr); + iotcon_representation_destroy(repr); + return NULL; } - INFO("POST request"); - - /* do POST operation */ - - ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT); - if (0 != ret) - ERR("_send_response() Fail(%d)", ret); + iotcon_representation_destroy(fan_repr); - iotcon_representation_destroy(resp_repr); + return repr; } -static void _request_handler_delete(iotcon_response_h response) +static int _room_request_handler_get(room_resource_s *room, iotcon_request_h request) { int ret; - iotcon_representation_h resp_repr; + iotcon_query_h query; + iotcon_representation_h repr; + iotcon_interface_e iface = IOTCON_INTERFACE_DEFAULT; - ret = iotcon_representation_create(&resp_repr); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_representation_create() Fail(%d)", ret); - return; + INFO("GET request - Room"); + + repr = _get_room_representation(room); + if (NULL == repr) { + ERR("_get_room_representation() Fail"); + return -1; } - INFO("DELETE request"); + ret = iotcon_request_get_query(request, &query); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_request_get_query() Fail(%d)", ret); + iotcon_representation_destroy(repr); + return -1; + } - /* do DELETE operation */ + if (query) { + ret = iotcon_query_get_interface(query, &iface); + if (IOTCON_ERROR_NO_DATA == ret) { + iface = IOTCON_INTERFACE_DEFAULT; + } else if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_query_get_interface() Fail(%d)", ret); + iotcon_representation_destroy(repr); + return -1; + } + } - ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT); - if (0 != ret) + ret = _send_response(request, repr, iface, IOTCON_RESPONSE_RESULT_OK); + if (0 != ret) { ERR("_send_response() Fail(%d)", ret); + iotcon_representation_destroy(repr); + return -1; + } - iotcon_representation_destroy(resp_repr); + iotcon_representation_destroy(repr); + + return 0; } static void _light_request_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data) { - int ret; - int types; - iotcon_response_h response; + int ret, types; + light_resource_s *light = user_data; + int iface = IOTCON_INTERFACE_DEFAULT; RET_IF(NULL == request); ret = iotcon_request_get_types(request, &types); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_request_get_types() Fail(%d)", ret); + _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR); return; } - ret = iotcon_response_create(request, &response); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_response_create() Fail"); - return; + if (IOTCON_REQUEST_GET & types) { + ret = _light_request_handler_get(light, request); + if (0 != ret) + _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR); + } else { + _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_FORBIDDEN); } +} - if (IOTCON_REQUEST_GET & types) - _light_request_handler_get(response); - - else if (IOTCON_REQUEST_PUT & types) - _request_handler_put(request, response); +static void _fan_request_handler(iotcon_resource_h resource, iotcon_request_h request, + void *user_data) +{ + int ret, types; + fan_resource_s *fan = user_data; + int iface = IOTCON_INTERFACE_DEFAULT; - else if (IOTCON_REQUEST_POST & types) - _request_handler_post(response); + RET_IF(NULL == request); - else if (IOTCON_REQUEST_DELETE & types) - _request_handler_delete(response); + ret = iotcon_request_get_types(request, &types); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_request_get_types() Fail(%d)", ret); + _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR); + return; + } - iotcon_response_destroy(response); + if (IOTCON_REQUEST_GET & types) { + ret = _fan_request_handler_get(fan, request); + if (0 != ret) + _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR); + } else { + _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_FORBIDDEN); + } } static void _room_request_handler(iotcon_resource_h resource, iotcon_request_h request, @@ -451,35 +643,34 @@ static void _room_request_handler(iotcon_resource_h resource, iotcon_request_h r { FN_CALL; int ret, types; - iotcon_response_h response; + char *host_address; + room_resource_s *room = user_data; + int iface = IOTCON_INTERFACE_DEFAULT; RET_IF(NULL == request); - ret = iotcon_request_get_types(request, &types); + ret = iotcon_request_get_host_address(request, &host_address); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_request_get_types() Fail(%d)", ret); + ERR("iotcon_request_get_host_address() Fail(%d)", ret); + _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR); return; } + INFO("host address : %s", host_address); - ret = iotcon_response_create(request, &response); + ret = iotcon_request_get_types(request, &types); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_response_create() Fail(%d)", ret); + ERR("iotcon_request_get_types() Fail(%d)", ret); + _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR); return; } - if (IOTCON_REQUEST_GET & types) - _room_request_handler_get(request, response); - - else if (IOTCON_REQUEST_PUT & types) - _request_handler_put(request, response); - - else if (IOTCON_REQUEST_POST & types) - _request_handler_post(response); - - else if (IOTCON_REQUEST_DELETE & types) - _request_handler_delete(response); - - iotcon_response_destroy(response); + if (IOTCON_REQUEST_GET & types) { + ret = _room_request_handler_get(room, request); + if (0 != ret) + _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR); + } else { + _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_FORBIDDEN); + } } int main(int argc, char **argv) @@ -487,8 +678,9 @@ int main(int argc, char **argv) FN_CALL; int ret; GMainLoop *loop; - iotcon_resource_h room_handle, light_handle; - iotcon_resource_types_h room_rtypes, light_rtypes; + room_resource_s room = {0}; + light_resource_s light = {0}; + fan_resource_s fan = {0}; loop = g_main_loop_new(NULL, FALSE); @@ -499,68 +691,94 @@ int main(int argc, char **argv) return -1; } - /* register room resource */ - ret = iotcon_resource_types_create(&room_rtypes); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_resource_types_create() Fail(%d)", ret); + /* set resource */ + ret = _set_room_resource(&room); + if (0 != ret) { + ERR("_set_room_resource() Fail(%d)", ret); iotcon_disconnect(); return -1; } - ret = iotcon_resource_types_add(room_rtypes, ROOM_RESOURCE_TYPE); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_resource_types_add(%s) Fail(%d)", ROOM_RESOURCE_TYPE, ret); - iotcon_resource_types_destroy(room_rtypes); + + ret = _set_light_resource(&light); + if (0 != ret) { + ERR("_set_room_resource() Fail(%d)", ret); + _free_room_resource(&room); iotcon_disconnect(); return -1; } - ret = iotcon_resource_create(ROOM_RESOURCE_URI, room_rtypes, - (IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_BATCH), - (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), _room_request_handler, - NULL, &room_handle); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_resource_create() Fail(%d)", ret); - iotcon_resource_types_destroy(room_rtypes); + ret = _set_fan_resource(&fan); + if (0 != ret) { + ERR("_set_room_resource() Fail(%d)", ret); + _free_room_resource(&room); + _free_light_resource(&light); + iotcon_disconnect(); + return -1; + } + + room.child_light = &light; + room.child_fan = &fan; + + /* register room resource */ + room.handle = _create_resource(room.uri_path, room.type, room.ifaces, room.properties, + _room_request_handler, &room); + if (NULL == room.handle) { + ERR("_create_resource() Fail"); + _free_fan_resource(&fan); + _free_light_resource(&light); + _free_room_resource(&room); iotcon_disconnect(); return -1; } - iotcon_resource_types_destroy(room_rtypes); /* register light resource */ - ret = iotcon_resource_types_create(&light_rtypes); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_resource_types_create() Fail(%d)", ret); - iotcon_resource_destroy(room_handle); + light.handle = _create_resource(light.uri_path, light.type, light.ifaces, + light.properties, _light_request_handler, &light); + if (NULL == light.handle) { + ERR("_create_resource() Fail"); + iotcon_resource_destroy(room.handle); + _free_fan_resource(&fan); + _free_light_resource(&light); + _free_room_resource(&room); iotcon_disconnect(); return -1; } - ret = iotcon_resource_types_add(light_rtypes, LIGHT_RESOURCE_TYPE); + + ret = iotcon_resource_bind_child_resource(room.handle, light.handle); if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_resource_types_add(%s) Fail(%d)", LIGHT_RESOURCE_TYPE, ret); - iotcon_resource_types_destroy(light_rtypes); - iotcon_resource_destroy(room_handle); + ERR("iotcon_resource_bind_child_resource() Fail"); + iotcon_resource_destroy(light.handle); + iotcon_resource_destroy(room.handle); + _free_fan_resource(&fan); + _free_light_resource(&light); + _free_room_resource(&room); iotcon_disconnect(); return -1; } - ret = iotcon_resource_create(LIGHT_RESOURCE_URI, light_rtypes, - (IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_BATCH), - (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), _light_request_handler, - NULL, &light_handle); - if (IOTCON_ERROR_NONE != ret) { - ERR("iotcon_resource_create() Fail"); - iotcon_resource_types_destroy(light_rtypes); - iotcon_resource_destroy(room_handle); + /* register fan resource */ + fan.handle = _create_resource(fan.uri_path, fan.type, fan.ifaces, fan.properties, + _fan_request_handler, &fan); + if (NULL == fan.handle) { + ERR("_create_resource() Fail"); + iotcon_resource_destroy(light.handle); + iotcon_resource_destroy(room.handle); + _free_fan_resource(&fan); + _free_light_resource(&light); + _free_room_resource(&room); iotcon_disconnect(); return -1; } - iotcon_resource_types_destroy(light_rtypes); - ret = iotcon_resource_bind_child_resource(room_handle, light_handle); + ret = iotcon_resource_bind_child_resource(room.handle, fan.handle); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_resource_bind_child_resource() Fail"); - iotcon_resource_destroy(light_handle); - iotcon_resource_destroy(room_handle); + iotcon_resource_destroy(fan.handle); + iotcon_resource_destroy(light.handle); + iotcon_resource_destroy(room.handle); + _free_fan_resource(&fan); + _free_light_resource(&light); + _free_room_resource(&room); iotcon_disconnect(); return -1; } @@ -568,8 +786,12 @@ int main(int argc, char **argv) g_main_loop_run(loop); g_main_loop_unref(loop); - iotcon_resource_destroy(light_handle); - iotcon_resource_destroy(room_handle); + iotcon_resource_destroy(fan.handle); + iotcon_resource_destroy(light.handle); + iotcon_resource_destroy(room.handle); + _free_fan_resource(&fan); + _free_light_resource(&light); + _free_room_resource(&room); /* disconnect iotcon */ iotcon_disconnect(); -- 2.34.1