From 9310a46148b82b5ade776fb5857d53f852053f33 Mon Sep 17 00:00:00 2001 From: Minchul Lee Date: Fri, 15 May 2015 21:52:57 +0900 Subject: [PATCH] (1) fix leak of crud functions, (2) reinforce sample binaries for crud operations, (3) reinforce delete function Change-Id: Id0d13339d182f0d3ca172191c3847f74bd3d4e69 Signed-off-by: Minchul Lee --- lib/ic-ioty-repr.cpp | 2 +- lib/ic-ioty.cpp | 12 ++- lib/ic-repr.c | 50 ++++++------ lib/ic-repr.h | 15 +++- lib/ic.c | 5 +- lib/include/iotcon.h | 3 + test/crud-test-client.c | 134 +++++++++++++++--------------- test/crud-test-server.c | 212 +++++++++++++++++++++++++++++++++++++----------- 8 files changed, 283 insertions(+), 150 deletions(-) diff --git a/lib/ic-ioty-repr.cpp b/lib/ic-ioty-repr.cpp index 3428d86..d27af9e 100644 --- a/lib/ic-ioty-repr.cpp +++ b/lib/ic-ioty-repr.cpp @@ -87,7 +87,7 @@ OCRepresentation ic_ioty_repr_parse(iotcon_repr_h repr) // TODO: It's better that iotcon_repr_h is changed to // OCRepresentation at once. - char *repr_json = ic_repr_generate_json(repr); + char *repr_json = ic_repr_generate_json(repr, false); try { info.setJSONRepresentation(repr_json); diff --git a/lib/ic-ioty.cpp b/lib/ic-ioty.cpp index b052107..977865b 100644 --- a/lib/ic-ioty.cpp +++ b/lib/ic-ioty.cpp @@ -150,6 +150,7 @@ namespace icIotivityHandler { if (on_get) on_get(options, repr, res, cb_data); + iotcon_repr_free(repr); iotcon_options_free(options); } }; @@ -191,6 +192,7 @@ namespace icIotivityHandler { if (on_put) on_put(options, repr, res, cb_data); + iotcon_repr_free(repr); iotcon_options_free(options); } }; @@ -216,7 +218,7 @@ namespace icIotivityHandler { iotcon_options_h options; iotcon_repr_h repr = NULL; - if (OC_STACK_OK == eCode) + if (OC_STACK_OK == eCode || OC_STACK_RESOURCE_CREATED == eCode) res = IOTCON_ERR_NONE; else res = IOTCON_ERR_IOTIVITY; @@ -232,6 +234,7 @@ namespace icIotivityHandler { if (on_post) on_post(options, repr, res, cb_data); + iotcon_repr_free(repr); iotcon_options_free(options); } }; @@ -254,7 +257,7 @@ namespace icIotivityHandler { int res; iotcon_options_h options; - if (OC_STACK_OK == eCode) + if (OC_STACK_OK == eCode || OC_STACK_RESOURCE_DELETED == eCode) res = IOTCON_ERR_NONE; else res = IOTCON_ERR_IOTIVITY; @@ -309,6 +312,7 @@ namespace icIotivityHandler { if (on_observe) on_observe(options, repr, res, sequenceNumber, cb_data); + iotcon_repr_free(repr); iotcon_options_free(options); } }; @@ -417,7 +421,9 @@ static OCEntityHandlerResult _entity_handler(shared_ptr reque free(request_s.request_type); free(request_s.res_uri); - if (request_s.repr) /* To avoid unnecessary ERR log (repr could be NULL) */ + + /* To avoid unnecessary ERR log (repr could be NULL) */ + if (request_s.repr) iotcon_repr_free(request_s.repr); iotcon_options_free(request_s.header_options); iotcon_query_free(request_s.query); diff --git a/lib/ic-repr.c b/lib/ic-repr.c index 2b3f088..1ebd353 100644 --- a/lib/ic-repr.c +++ b/lib/ic-repr.c @@ -463,7 +463,7 @@ API int iotcon_repr_get_keys_count(iotcon_repr_h repr) return g_hash_table_size(repr->hash_table); } -bool ic_repr_has_value(iotcon_repr_h repr, const char *key) +API bool iotcon_repr_contains(iotcon_repr_h repr, const char *key) { RETV_IF(NULL == repr, false); RETV_IF(NULL == key, false); @@ -618,26 +618,25 @@ JsonObject* _ic_repr_generate_json_foreach(iotcon_repr_h cur_repr, DBG("Representation has \"%s\" key. count(%d)", IOTCON_KEY_RESOURCETYPES, rt_count); JsonArray *rt_array = json_array_new(); - iotcon_repr_get_resource_interfaces(cur_repr, _ic_repr_get_res_type_fn, rt_array); + iotcon_repr_get_resource_types(cur_repr, _ic_repr_get_res_type_fn, rt_array); json_object_set_array_member(prop_obj, IOTCON_KEY_RESOURCETYPES, rt_array); } if (0 < if_count) { DBG("Representation has \"%s\" key. count(%d)", IOTCON_KEY_INTERFACES, if_count); JsonArray *if_array = json_array_new(); - iotcon_repr_get_resource_types(cur_repr, _ic_repr_get_res_interface_fn, if_array); + iotcon_repr_get_resource_interfaces(cur_repr, _ic_repr_get_res_interface_fn, if_array); json_object_set_array_member(prop_obj, IOTCON_KEY_INTERFACES, if_array); } return repr_obj; } -char* ic_repr_generate_json(iotcon_repr_h repr) + +static JsonObject* _ic_repr_generate_json_object(iotcon_repr_h repr) { JsonObject *repr_obj = NULL; - gchar *json_data = NULL; JsonObject *root_obj = NULL; - JsonNode *root_node = NULL; JsonArray *root_array = NULL; GList *children = NULL; unsigned int child_count = 0; @@ -675,9 +674,24 @@ char* ic_repr_generate_json(iotcon_repr_h repr) json_object_set_array_member(root_obj, IOTCON_KEY_OC, root_array); + return root_obj; +} + +char* ic_repr_generate_json(iotcon_repr_h repr, bool set_pretty) +{ + JsonNode *root_node = NULL; + char *json_data = NULL; + + JsonObject *obj = _ic_repr_generate_json_object(repr); + if (NULL == obj) { + ERR("ic_repr_generate_json() Fail"); + return NULL; + } + JsonGenerator *gen = json_generator_new(); + json_generator_set_pretty(gen, set_pretty); root_node = json_node_new(JSON_NODE_OBJECT); - json_node_set_object(root_node, root_obj); + json_node_set_object(root_node, obj); json_generator_set_root(gen, root_node); json_data = json_generator_to_data(gen, NULL); @@ -862,25 +876,7 @@ API void iotcon_repr_free(iotcon_repr_h repr) FN_END; } -API void iotcon_repr_print(iotcon_repr_h repr) +API char* iotcon_repr_generate_json(iotcon_repr_h repr) { - FN_CALL; - gchar *json_data = NULL; - JsonNode *root_node = NULL; - int error_code = IOTCON_ERR_NONE; - - JsonObject *repr_obj = ic_repr_generate_json_repr(repr, &error_code); - if (NULL == repr_obj) { - ERR("ic_repr_generate_json_repr() Fail(NULL == repr_obj)"); - return; - } - - root_node = json_node_new(JSON_NODE_OBJECT); - JsonGenerator *gen = json_generator_new(); - json_generator_set_pretty(gen, TRUE); - json_node_set_object(root_node, repr_obj); - json_generator_set_root(gen, root_node); - - json_data = json_generator_to_data(gen, NULL); - DBG("\n%s", json_data); + return ic_repr_generate_json(repr, TRUE); } diff --git a/lib/ic-repr.h b/lib/ic-repr.h index 9ecf92b..41e0684 100644 --- a/lib/ic-repr.h +++ b/lib/ic-repr.h @@ -29,10 +29,21 @@ int ic_repr_get_value(iotcon_repr_h repr, const char *key, iotcon_value_h *retval); int ic_repr_replace_value(iotcon_repr_h repr, const char *key, iotcon_value_h value); -bool ic_repr_has_value(iotcon_repr_h repr, const char *key); JsonObject* ic_repr_generate_json_repr(iotcon_repr_h repr, int *err_code); -char* ic_repr_generate_json(iotcon_repr_h repr); + +/** + * @ingroup CAPI_IOT_CONNECTIVITY_MODULE + * @brief Generates JSON string from Representation. + * @since_tizen 3.0 + * @remarks The returned string must be released with free() by you. + * + * @param[in] repr The handle to the Representation + * @param[in] set_pretty Whether the generated JSON string should be pretty printed + * + * @return Generated JSON string, otherwise a null pointer if a parse error + */ +char* ic_repr_generate_json(iotcon_repr_h repr, bool set_pretty); iotcon_repr_h ic_repr_parse_json_obj(JsonObject *obj, int *err_code); iotcon_repr_h ic_repr_parse_json(const char *json_string); diff --git a/lib/ic.c b/lib/ic.c index 7424d7f..4c69ace 100644 --- a/lib/ic.c +++ b/lib/ic.c @@ -176,7 +176,10 @@ API void iotcon_response_free(iotcon_response_h resp) RET_IF(NULL == resp); free(data->new_resource_uri); - ic_options_free(resp->header_options); + + /* To avoid unnecessary ERR log (header_options could be NULL) */ + if (resp->header_options) + ic_options_free(resp->header_options); iotcon_repr_free(resp->repr); free(data); } diff --git a/lib/include/iotcon.h b/lib/include/iotcon.h index d14367c..798fb5e 100644 --- a/lib/include/iotcon.h +++ b/lib/include/iotcon.h @@ -191,6 +191,7 @@ iotcon_repr_h iotcon_repr_get_nth_child(iotcon_repr_h parent, int index); GList* iotcon_repr_get_key_list(iotcon_repr_h repr); int iotcon_repr_get_keys_count(iotcon_repr_h repr); +bool iotcon_repr_contains(iotcon_repr_h repr, const char *key); void iotcon_repr_print(iotcon_repr_h repr); @@ -219,6 +220,8 @@ char* iotcon_value_get_str(iotcon_value_h value); iotcon_list_h iotcon_value_get_list(iotcon_value_h value); iotcon_repr_h iotcon_value_get_repr(iotcon_value_h value); +char* iotcon_repr_generate_json(iotcon_repr_h repr); + #ifdef __cplusplus } #endif diff --git a/test/crud-test-client.c b/test/crud-test-client.c index c11c537..90f557a 100644 --- a/test/crud-test-client.c +++ b/test/crud-test-client.c @@ -39,91 +39,83 @@ char* _alloc_str_from_glist(GList *list) return ret_str; } -void _get_res_type_fn(const char *res_type, void *user_data) +void _print_repr_info(iotcon_repr_h repr) { - DBG("resource type : %s", res_type); + if (0 < iotcon_repr_get_keys_count(repr)) + DBG("rep : \n%s", iotcon_repr_generate_json(repr)); } -void _get_res_interface_fn(const char *res_if, void *user_data) +static void _on_delete(const iotcon_options_h header_options, const int e_code, + void *user_data) { - DBG("resource interface : %s", res_if); + RETM_IF(IOTCON_EH_OK != e_code && IOTCON_EH_RESOURCE_DELETED != e_code, + "_on_delete Response error(%d)", e_code); + INFO("DELETE request was successful"); + + /* delete callback operations */ } -void _print_repr_info(iotcon_repr_h repr) +static void _on_post(const iotcon_options_h header_options, iotcon_repr_h recv_repr, + const int e_code, void *user_data) { - const char *uri = iotcon_repr_get_uri(repr); - if (uri) - DBG("uri : %s", uri); + char *created_uri = NULL; + iotcon_resource_s new_door_resource = {0}; - if (0 < iotcon_repr_get_keys_count(repr)) { - DBG("rep :"); - iotcon_repr_print(repr); - } + RETM_IF(IOTCON_EH_OK != e_code, "_on_post Response error(%d)", e_code); + INFO("POST request was successful"); + + _print_repr_info(recv_repr); - iotcon_repr_get_resource_types(repr, _get_res_type_fn, NULL); - iotcon_repr_get_resource_interfaces(repr, _get_res_interface_fn, NULL); + created_uri = iotcon_repr_get_str(recv_repr, "createduri"); + if (created_uri) { + DBG("New resource created : %s", created_uri); + + new_door_resource = iotcon_construct_resource_object(door_resource.resource_host, + created_uri, + true, door_resource.resource_types, door_resource.resource_interfaces); + + iotcon_delete_resource(new_door_resource, _on_delete, NULL); + } } -static void _on_put(const iotcon_options_h header_options, iotcon_repr_h repr, +static void _on_put(const iotcon_options_h header_options, iotcon_repr_h recv_repr, const int e_code, void *user_data) { - FN_CALL; + RETM_IF(IOTCON_EH_OK != e_code, "_on_put Response error(%d)", e_code); + INFO("PUT request was successful"); - if (e_code == IOTCON_ERR_NONE) { - INFO("PUT request was successful"); + _print_repr_info(recv_repr); - DBG("[%s] parent", iotcon_repr_get_uri(repr)); - _print_repr_info(repr); + iotcon_repr_h send_repr = iotcon_repr_new(); + + iotcon_query query_params = iotcon_query_new(); + /* send POST request */ + iotcon_post(door_resource, send_repr, query_params, _on_post, NULL); + + iotcon_repr_free(send_repr); + iotcon_query_free(query_params); - GList *children = iotcon_repr_get_children(repr); - if (children) { - int child_count = iotcon_repr_get_children_count(repr); - int child_index = 0; - for (child_index = 0; child_index < child_count; child_index++) { - DBG("[%s] %dth child", child_index + 1, iotcon_repr_get_uri(repr)); - _print_repr_info(iotcon_repr_get_nth_child(repr, child_index)); - } - } - } - else { - ERR("onPUT Response error(%d)", e_code); - } - /* set the flag for receiving response succesfully */ } -void _on_get(const iotcon_options_h header_options, iotcon_repr_h repr, +static void _on_get(const iotcon_options_h header_options, iotcon_repr_h recv_repr, const int e_code, void *user_data) { - FN_CALL; + RETM_IF(IOTCON_EH_OK != e_code, "_on_get Response error(%d)", e_code); + INFO("GET request was successful"); - if (e_code == IOTCON_ERR_NONE) { - INFO("GET request was successful"); + _print_repr_info(recv_repr); - DBG("[%s] parent", iotcon_repr_get_uri(repr)); - _print_repr_info(repr); + iotcon_repr_h send_repr = iotcon_repr_new(); + iotcon_repr_set_bool(send_repr, "opened", true); - GList *children = iotcon_repr_get_children(repr); - if (children) { - int child_count = iotcon_repr_get_children_count(repr); - int child_index = 0; - for (child_index = 0; child_index < child_count; child_index++) { - DBG("[%s] %dth child", child_index + 1, iotcon_repr_get_uri(repr)); - _print_repr_info(iotcon_repr_get_nth_child(repr, child_index)); - } - } + iotcon_query query_params = iotcon_query_new(); + /* send PUT request */ + iotcon_put(door_resource, send_repr, query_params, _on_put, NULL); - iotcon_repr_h repr = iotcon_repr_new(); - iotcon_repr_set_bool(repr, "opened", true); + iotcon_repr_free(send_repr); + iotcon_query_free(query_params); - iotcon_query query = iotcon_query_new(); - iotcon_put(door_resource, repr, query, _on_put, NULL); - - } - else { - ERR("onGET Response error(%d)", e_code); - } - /* set the flag for receiving response succesfully */ } static void _found_resource(iotcon_resource_s *resource, void *user_data) @@ -139,17 +131,18 @@ static void _found_resource(iotcon_resource_s *resource, void *user_data) INFO("===== resource found ====="); - /* Get the resource URI */ + /* get the resource URI */ resource_uri = iotcon_resource_get_uri(*resource); if (NULL == resource_uri) { ERR("uri is NULL"); return; } - /* Get the resource host address */ + + /* get the resource host address */ resource_host = iotcon_resource_get_host(*resource); DBG("[%s] resource host : %s", resource_uri, resource_host); - /* Get the resource interfaces */ + /* get the resource interfaces */ resource_interfaces = iotcon_resource_get_interfaces(*resource); if (resource_interfaces) { interfaces_str = _alloc_str_from_glist(resource_interfaces); @@ -157,7 +150,7 @@ static void _found_resource(iotcon_resource_s *resource, void *user_data) free(interfaces_str); } - /* Get the resource types */ + /* get the resource types */ resource_types = iotcon_resource_get_types(*resource); if (resource_types) { res_types_str = _alloc_str_from_glist(resource_types); @@ -166,14 +159,14 @@ static void _found_resource(iotcon_resource_s *resource, void *user_data) } if (!strcmp(door_uri, resource_uri)) { + /* copy resource to use elsewhere */ door_resource = iotcon_copy_resource(*resource); - iotcon_query query = iotcon_query_new(); - - /* send GET Request */ - iotcon_get(*resource, query, _on_get, NULL); + iotcon_query query_params = iotcon_query_new(); + /* send GET request */ + iotcon_get(*resource, query_params, _on_get, NULL); - iotcon_query_free(query); + iotcon_query_free(query_params); } } } @@ -184,9 +177,12 @@ int main(int argc, char **argv) GMainLoop *loop; loop = g_main_loop_new(NULL, FALSE); + /* initialize address and port */ iotcon_initialize("0.0.0.0", 0); - iotcon_find_resource("", "coap://224.0.1.187/oc/core?rt=core.door", &_found_resource, NULL); + /* find door typed resources */ + iotcon_find_resource("", "coap://224.0.1.187/oc/core?rt=core.door", &_found_resource, + NULL); g_main_loop_run(loop); g_main_loop_unref(loop); diff --git a/test/crud-test-server.c b/test/crud-test-server.c index 124d0b0..a3b84a9 100644 --- a/test/crud-test-server.c +++ b/test/crud-test-server.c @@ -14,12 +14,12 @@ * limitations under the License. */ +#include +#include #include #include "test-log.h" -const char* const door_uri = "/a/door"; -const char* const door_type = "core.door"; - +/* Door Resource */ typedef struct _door_resource_s { bool state; char *uri; @@ -28,59 +28,165 @@ typedef struct _door_resource_s { } door_resource_s; static door_resource_s my_door; +static bool resource_created = false; +iotcon_resource_h new_door_handle; + +static void _request_handler(const iotcon_request_s *request); + +static iotcon_error_e _set_door_resource() +{ + my_door.state = false; + my_door.type = strdup("core.door"); + if (NULL == my_door.type) { + ERR("strdup(core.door) Fail"); + return IOTCON_ERR_MEMORY; + } + + my_door.uri = strdup("/a/door"); + if (NULL == my_door.uri) { + ERR("strdup(/a/door) Fail"); + return IOTCON_ERR_MEMORY; + } + + return IOTCON_ERR_NONE; +} -static void _set_door_resource(door_resource_s *door_s) +static void _check_door_state() { - door_s->state = false; - door_s->type = strdup(door_type); - door_s->uri = strdup(door_uri); + if (false == my_door.state) + INFO("[Door] closed."); + else + INFO("[Door] opened."); } -static void _entity_handler_door(const iotcon_request_s *request_s) +static iotcon_resource_h _create_door_resource(char *uri, iotcon_interface_e interfaces, + iotcon_resource_property_e properties) { - FN_CALL; + /* register door resource */ + iotcon_resource_h door_handle = iotcon_register_resource(uri, my_door.type, + interfaces, properties, _request_handler); + if (NULL == door_handle) { + ERR("iotcon_register_resource() Fail"); + return NULL; + } + + return door_handle; +} + +static void _send_response(iotcon_response_h response, iotcon_repr_h repr, + iotcon_entity_handler_result_e result) +{ + iotcon_response_set(response, IOTCON_RESP_RESULT, result); + iotcon_response_set(response, IOTCON_RESP_REPRESENTATION, repr); + iotcon_response_set(response, IOTCON_RESP_ERR_CODE, 200); + + /* send Representation to the client */ + iotcon_send_resource_response(response); +} + +static void _request_handler_get(iotcon_response_h response) +{ + iotcon_repr_h resp_repr = NULL; + INFO("GET request"); + + /* create a door Representation */ + resp_repr = iotcon_repr_new(); + iotcon_repr_set_uri(resp_repr, my_door.uri); + iotcon_repr_set_bool(resp_repr, "opened", my_door.state); + + _send_response(response, resp_repr, IOTCON_EH_OK); +} - RET_IF(NULL == request_s); +static void _request_handler_put(const iotcon_request_s *request, + iotcon_response_h response) +{ + iotcon_repr_h req_repr = NULL; + iotcon_repr_h resp_repr = NULL; + INFO("PUT request"); + + req_repr = iotcon_request_get_representation(request); + my_door.state = iotcon_repr_get_bool(req_repr, "opened"); + + _check_door_state(); - char *requestType = request_s->request_type; + resp_repr = iotcon_repr_new(); + iotcon_repr_set_uri(resp_repr, my_door.uri); + iotcon_repr_set_bool(resp_repr, "opened", my_door.state); - int requestFlag = request_s->request_handler_flag; - if (requestFlag & IOTCON_REQUEST_FLAG) { - iotcon_response_h pResponse = iotcon_response_new(request_s->request_handle, - request_s->resource_handle); - if (NULL == pResponse) { - ERR("pResponse is NULL"); + _send_response(response, resp_repr, IOTCON_EH_OK); +} + +static void _request_handler_post(iotcon_response_h response) +{ + iotcon_repr_h resp_repr = NULL; + INFO("POST request"); + + if (false == resource_created) { + new_door_handle = _create_door_resource("/a/door1", IOTCON_INTERFACE_DEFAULT, + (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE)); + if (NULL == new_door_handle) { + ERR("_create_door_resource() Fail"); return; } + resource_created = true; - if (!strcmp("GET", requestType)) { - INFO("GET request"); + /* send information that new resource was created */ + resp_repr = iotcon_repr_new(); + iotcon_repr_set_str(resp_repr, "createduri", "/a/door1"); - iotcon_repr_h door_rep = iotcon_repr_new(); - iotcon_repr_set_uri(door_rep, my_door.uri); - iotcon_repr_set_bool(door_rep, "opened", my_door.state); + _send_response(response, resp_repr, IOTCON_EH_RESOURCE_CREATED); + } - iotcon_response_set(pResponse, IOTCON_RESP_REPRESENTATION, door_rep); - iotcon_response_set(pResponse, IOTCON_RESP_ERR_CODE, 200); - iotcon_response_set(pResponse, IOTCON_RESP_RESULT, IOTCON_EH_OK); - iotcon_send_resource_response(pResponse); +} - } - else if (!strcmp("PUT", requestType)) { - INFO("PUT request"); +static void _request_handler_delete(iotcon_response_h response) +{ + int ret = IOTCON_ERR_NONE; + iotcon_repr_h resp_repr = NULL; + iotcon_entity_handler_result_e result = IOTCON_EH_OK; + INFO("DELETE request"); + + ret = iotcon_unregister_resource(new_door_handle); + resp_repr = iotcon_repr_new(); + if (IOTCON_ERR_NONE == ret) + result = IOTCON_EH_RESOURCE_DELETED; + else + result = IOTCON_EH_ERROR; + + _send_response(response, resp_repr, result); +} - iotcon_repr_h repr = iotcon_request_get_representation(request_s); - my_door.state = iotcon_repr_get_bool(repr, "opened"); - iotcon_repr_h door_rep = iotcon_repr_new(); - iotcon_repr_set_uri(door_rep, my_door.uri); - iotcon_repr_set_bool(door_rep, "opened", my_door.state); +static void _request_handler(const iotcon_request_s *request) +{ + char *request_type = NULL; + int request_flag = IOTCON_INIT_FLAG; + iotcon_response_h response = NULL; + FN_CALL; - iotcon_response_set(pResponse, IOTCON_RESP_REPRESENTATION, door_rep); - iotcon_response_set(pResponse, IOTCON_RESP_ERR_CODE, 200); - iotcon_response_set(pResponse, IOTCON_RESP_RESULT, IOTCON_EH_OK); - iotcon_send_resource_response(pResponse); + RET_IF(NULL == request); + request_type = request->request_type; + request_flag = request->request_handler_flag; + if (request_flag & IOTCON_REQUEST_FLAG) { + response = iotcon_response_new(request->request_handle, request->resource_handle); + if (NULL == response) { + ERR("iotcon_response_new() Fail(NULL == response)"); + return; } + + if (!strcmp("GET", request_type)) + _request_handler_get(response); + + else if (!strcmp("PUT", request_type)) + _request_handler_put(request, response); + + else if (!strcmp("POST", request_type)) + _request_handler_post(response); + + else if (!strcmp("DELETE", request_type)) + _request_handler_delete(response); + + iotcon_response_free(response); } } @@ -88,24 +194,36 @@ int main(int argc, char **argv) { FN_CALL; GMainLoop *loop; - int result = 0; + iotcon_interface_e door_interfaces = IOTCON_INTERFACE_DEFAULT; + iotcon_resource_property_e resource_properties = IOTCON_DISCOVERABLE; + iotcon_resource_h door_handle = NULL; + iotcon_error_e iotcon_error = IOTCON_ERR_NONE; loop = g_main_loop_new(NULL, FALSE); /* initialize address and port */ iotcon_initialize("0.0.0.0", 0); - _set_door_resource(&my_door); + /* set local door resource */ + iotcon_error = _set_door_resource(); + if (IOTCON_ERR_NONE != iotcon_error) { + ERR("_set_door_resource() Fail"); + return -1; + } - /* register door resource */ - iotcon_resource_h door_handle = iotcon_register_resource(door_uri, - door_type, IOTCON_INTERFACE_DEFAULT, - (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), _entity_handler_door); - if (0 != result) { - ERR("register %s resource Fail(%d)", "/a/light", result); - return result; + /* add resource options */ + door_interfaces |= IOTCON_INTERFACE_BATCH; + resource_properties |= IOTCON_OBSERVABLE; + + /* create new door resource */ + door_handle = _create_door_resource("/a/door", door_interfaces, resource_properties); + if (NULL == door_handle) { + ERR("_create_door_resource() Fail"); + return -1; } + _check_door_state(); + g_main_loop_run(loop); g_main_loop_unref(loop); -- 2.7.4