diff options
author | Jooseok Park <jooseok.park@samsung.com> | 2017-04-07 13:23:27 +0900 |
---|---|---|
committer | Jooseok Park <jooseok.park@samsung.com> | 2017-04-07 13:23:27 +0900 |
commit | 1eeaddafa0992935ce7b4330055036422cde33f8 (patch) | |
tree | 460a2d20c70917d68cb08115f20b9b1a8531d9d5 | |
parent | 9743cf2269d1647ba8cd5515a0219275a775c63b (diff) | |
download | iotcon-devel/test_app.tar.gz iotcon-devel/test_app.tar.bz2 iotcon-devel/test_app.zip |
add test_appdevel/test_app
Change-Id: Id8640058fb4eb598586071785e2b876e8cea4693
-rwxr-xr-x | test_app/CMakeLists.txt | 41 | ||||
-rwxr-xr-x | test_app/door.c | 434 | ||||
-rwxr-xr-x | test_app/door.c.orig | 437 | ||||
-rwxr-xr-x | test_app/iotcon-test-provisioning-client.c | 169 | ||||
-rwxr-xr-x | test_app/iotcon-test-simple-client.c | 963 | ||||
-rwxr-xr-x | test_app/iotcon-test-simple-client.c.orig | 963 | ||||
-rwxr-xr-x | test_app/iotcon-test-simple-server.c | 125 | ||||
-rw-r--r-- | test_app/iotcon-test-svr-db-server-all.dat | bin | 0 -> 1017 bytes | |||
-rwxr-xr-x | test_app/light.c | 437 | ||||
-rwxr-xr-x | test_app/menu.c | 374 | ||||
-rwxr-xr-x | test_app/menu.h | 108 |
11 files changed, 4051 insertions, 0 deletions
diff --git a/test_app/CMakeLists.txt b/test_app/CMakeLists.txt new file mode 100755 index 0000000..7f40f5a --- /dev/null +++ b/test_app/CMakeLists.txt @@ -0,0 +1,41 @@ +LINK_DIRECTORIES(${CMAKE_BINARY_DIR}) +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) + +SET(IOTCON_TEST_PROVISIONING_CLIENT "iotcon-test-provisioning-client") +SET(IOTCON_TEST_SIMPLE_CLIENT "iotcon-test-simple-client") +SET(IOTCON_TEST_SIMPLE_SERVER "iotcon-test-simple-server") + +SET(IOTCON_TEST_PROVISIONING_CLIENT_SRCS + iotcon-test-provisioning-client.c + menu.c +) +SET(IOTCON_TEST_SIMPLE_CLIENT_SRCS + iotcon-test-simple-client.c + menu.c +) +SET(IOTCON_TEST_SIMPLE_SERVER_SRCS + iotcon-test-simple-server.c + door.c + light.c + menu.c +) + +SET(IOTCON_TEST_PROVISIONING_DAT "iotcon-test-svr-db-server-all.dat") + +pkg_check_modules(test_pkgs REQUIRED dlog glib-2.0) +INCLUDE_DIRECTORIES(${test_pkgs_INCLUDE_DIRS}) +LINK_DIRECTORIES(${test_pkgs_LIBRARY_DIRS}) + +ADD_EXECUTABLE(${IOTCON_TEST_PROVISIONING_CLIENT} ${IOTCON_TEST_PROVISIONING_CLIENT_SRCS}) +TARGET_LINK_LIBRARIES(${IOTCON_TEST_PROVISIONING_CLIENT} ${test_pkgs_LIBRARIES} ${LIB}) +INSTALL(TARGETS ${IOTCON_TEST_PROVISIONING_CLIENT} DESTINATION ${BIN_INSTALL_DIR}) + +ADD_EXECUTABLE(${IOTCON_TEST_SIMPLE_CLIENT} ${IOTCON_TEST_SIMPLE_CLIENT_SRCS}) +TARGET_LINK_LIBRARIES(${IOTCON_TEST_SIMPLE_CLIENT} ${test_pkgs_LIBRARIES} ${LIB}) +INSTALL(TARGETS ${IOTCON_TEST_SIMPLE_CLIENT} DESTINATION ${BIN_INSTALL_DIR}) + +ADD_EXECUTABLE(${IOTCON_TEST_SIMPLE_SERVER} ${IOTCON_TEST_SIMPLE_SERVER_SRCS}) +TARGET_LINK_LIBRARIES(${IOTCON_TEST_SIMPLE_SERVER} ${test_pkgs_LIBRARIES} ${LIB}) +INSTALL(TARGETS ${IOTCON_TEST_SIMPLE_SERVER} DESTINATION ${BIN_INSTALL_DIR}) + +INSTALL(FILES ${IOTCON_TEST_PROVISIONING_DAT} DESTINATION ${BIN_INSTALL_DIR}) diff --git a/test_app/door.c b/test_app/door.c new file mode 100755 index 0000000..abf5cdb --- /dev/null +++ b/test_app/door.c @@ -0,0 +1,434 @@ +/* + * + * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jooseok Park <jooseok.park@samsung.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/time.h> +#include <unistd.h> +#include <glib.h> +#include <gio/gio.h> +#include "menu.h" +#include <iotcon.h> + +static char data_door_resource_uri[MENU_DATA_SIZE + 1] = "/door/1"; +static char data_door_resource_type[MENU_DATA_SIZE + 1] = "oic.r.door"; +static char data_door_resource_state[MENU_DATA_SIZE + 1] = "0"; + +/* Door Resource */ +typedef struct { + int state; /* attributes */ + char *uri_path; + char *type; + iotcon_resource_interfaces_h ifaces; + uint8_t policies; + iotcon_resource_h handle; + iotcon_observers_h observers; + iotcon_representation_h repr; +} door_resource_s; + +door_resource_s door; +/****************** run **************************************/ +static void _show_door_state() +{ + msgm("[Door] state : %d", door.state); +} +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, repr); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_response_set_representation() Fail(%d)", ret); + iotcon_response_destroy(response); + return -1; + } + + /* send Representation to the client */ + 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 iotcon_representation_h _get_door_representation(door_resource_s *door) +{ + int ret; + iotcon_attributes_h attributes; + iotcon_representation_h repr; + + /* create attributes */ + ret = iotcon_attributes_create(&attributes); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_attributes_create() Fail(%d)", ret); + iotcon_representation_destroy(repr); + return NULL; + } + + ret = iotcon_attributes_add_int(attributes, "DOOR_ATTRIBUTE", door->state); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_attributes_add_int() Fail(%d)", ret); + iotcon_attributes_destroy(attributes); + iotcon_representation_destroy(repr); + return NULL; + } + + /* create representation */ + ret = iotcon_representation_create(&repr); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_representation_create() Fail(%d)", ret); + return NULL; + } + + 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_attributes_destroy(attributes); + iotcon_representation_destroy(repr); + return NULL; + } + + /* set attributes to representation */ + ret = iotcon_representation_set_attributes(repr, attributes); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_representation_set_attributes() Fail(%d)", ret); + iotcon_attributes_destroy(attributes); + iotcon_representation_destroy(repr); + return NULL; + } + + iotcon_attributes_destroy(attributes); + return repr; +} + +static int _request_handler_get(door_resource_s *door, iotcon_request_h request) +{ + int ret; + iotcon_representation_h resp_repr; + DBG("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_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 int _set_door_representation(door_resource_s *door, + iotcon_representation_h repr) +{ + int ret; + int val; + iotcon_attributes_h attributes; + + ret = iotcon_representation_get_attributes(repr, &attributes); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_representation_get_attributes() Fail(%d)", ret); + return -1; + } + + ret = iotcon_attributes_get_int(attributes, "DOOR_ATTRIBUTE", &val); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_attributes_get_int() Fail(%d)", ret); + return -1; + } + + door->state = val; + + return 0; +} +static int _request_handler_put(door_resource_s *door, iotcon_request_h request) +{ + int ret; + iotcon_representation_h req_repr, resp_repr; + DBG("PUT request"); + + ret = iotcon_request_get_representation(request, &req_repr); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_request_get_representation() Fail(%d)", ret); + return -1; + } + + ret = _set_door_representation(door, req_repr); + if (0 != ret) { + err("_set_door_representation() Fail(%d)", ret); + return -1; + } + _show_door_state(); + + 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_OK); + if (0 != ret) { + err("_send_response() Fail(%d)", ret); + iotcon_representation_destroy(resp_repr); + return -1; + } + + /* notify */ + ret = iotcon_resource_notify(door->handle, resp_repr, door->observers, IOTCON_QOS_HIGH); + if (IOTCON_ERROR_NONE != ret) + err("iotcon_resource_notify() Fail(%d)", ret); + + iotcon_representation_destroy(resp_repr); + + return 0; +} +static int _request_handler_post(door_resource_s *door, iotcon_request_h request) +{ + err("POST request is not supported!!\n"); + return -1; +} +static int _request_handler_delete(door_resource_s *door, iotcon_request_h request) +{ + err("DELETE request is not supported!!\n"); + return -1; +} +static void _request_handler(iotcon_resource_h resource, iotcon_request_h request, + void *user_data) +{ + door_resource_s *door = user_data;; + int ret, observe_id; + iotcon_request_type_e type; + iotcon_observe_type_e observe_type; + char *host_address; + + if (NULL == request) + return; + + 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_ERROR); + return; + } + + ret = iotcon_request_get_request_type(request, &type); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_request_get_request_type() Fail(%d)", ret); + _send_response(request, NULL, IOTCON_RESPONSE_ERROR); + return; + } + + DBG("requested host_address : %s", host_address); + + if (IOTCON_REQUEST_GET == type) + ret = _request_handler_get(door, request); + + else if (IOTCON_REQUEST_PUT == type) + ret = _request_handler_put(door, request); + + else if (IOTCON_REQUEST_POST == type) + ret = _request_handler_post(door, request); + + else if (IOTCON_REQUEST_DELETE == type) + ret = _request_handler_delete(door, request); + + if (0 != ret) { + _send_response(request, NULL, IOTCON_RESPONSE_ERROR); + return; + } + + ret = iotcon_request_get_observe_type(request, &observe_type); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_request_get_observe_type() Fail(%d)", ret); + return; + } + + if (IOTCON_OBSERVE_REGISTER == observe_type) { + DBG("observe_type : IOTCON_OBSERVE_REGISTER"); + 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_type) { + DBG("observe_type : IOTCON_OBSERVE_DEREGISTER"); + 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; + } + } +} + +int run_create_door_resource() +{ + int ret; + iotcon_resource_types_h resource_types; + + /* set door resource */ + door.state = (atoi(data_door_resource_state)); + door.uri_path = strdup(data_door_resource_uri); + door.type = strdup(data_door_resource_type); + door.policies = IOTCON_RESOURCE_DISCOVERABLE + | IOTCON_RESOURCE_OBSERVABLE; + + /* create resource interface */ + ret = iotcon_resource_interfaces_create(&door.ifaces); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_resource_interfaces_create() Fail(%d)", ret); + free(door.type); + free(door.uri_path); + return -1; + } + + ret = iotcon_resource_interfaces_add(door.ifaces, IOTCON_INTERFACE_DEFAULT); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_resource_interfaces_add() Fail(%d)", ret); + iotcon_resource_interfaces_destroy(door.ifaces); + free(door.type); + free(door.uri_path); + return -1; + } + + /* create resource type */ + ret = iotcon_resource_types_create(&resource_types); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_resource_types_create() Fail(%d)", ret); + return -1; + } + + ret = iotcon_resource_types_add(resource_types, door.type); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_resource_types_add() Fail(%d)", ret); + iotcon_resource_types_destroy(resource_types); + return -1; + } + + /* create resource observers */ + ret = iotcon_observers_create(&door.observers); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_observers_create() Fail"); + iotcon_resource_interfaces_destroy(door.ifaces); + free(door.type); + free(door.uri_path); + return -1; + } + + /* create(register) resource */ + ret = iotcon_resource_create(door.uri_path, + resource_types, + door.ifaces, + door.policies, + _request_handler, + &door, + &door.handle); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_resource_create() Fail"); + iotcon_resource_types_destroy(resource_types); + return -1; + } + + iotcon_resource_types_destroy(resource_types); + + DBG("resource create success!!"); + + return 0; +} +int run_change_door_resource_state() +{ + int ret; + iotcon_representation_h repr; + door.state = (atoi(data_door_resource_state)); + + _show_door_state(); + + repr = _get_door_representation(&door); + if (NULL == repr) { + err("_get_door_representation() Fail"); + return -1; + } + + ret = iotcon_resource_notify(door.handle, repr, door.observers, IOTCON_QOS_HIGH); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_resource_notify() Fail(%d)", ret); + iotcon_representation_destroy(repr); + return -1; + } + + iotcon_representation_destroy(repr); + return 0; +} + +/****************** sub menu data **************************************/ +static struct menu_data create_door_resource[] = { + {"1", "resource uri", 0, 0, data_door_resource_uri}, + {"2", "resource type", 0, 0, data_door_resource_type}, + {"3", "resource state", 0, 0, data_door_resource_state}, + {"4", "run", 0, run_create_door_resource, NULL}, + {0, 0,}, +}; +static struct menu_data change_door_resource_state[] = { + {"1", "resource state", 0, 0, data_door_resource_state}, + {"2", "run", 0, run_change_door_resource_state, NULL}, + {0, 0,}, +}; +/*********************************************************************************/ +/* Menu */ +struct menu_data menu_door[] = { + {"1", "create door resource", create_door_resource, NULL, NULL}, + {"2", "change door resource state", change_door_resource_state, NULL, NULL}, + {NULL, NULL,}, +}; diff --git a/test_app/door.c.orig b/test_app/door.c.orig new file mode 100755 index 0000000..074358e --- /dev/null +++ b/test_app/door.c.orig @@ -0,0 +1,437 @@ +/* + * + * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jooseok Park <jooseok.park@samsung.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/time.h> +#include <unistd.h> +#include <glib.h> +#include <gio/gio.h> +#include "menu.h" +#include <iotcon.h> + +static char data_door_resource_uri[MENU_DATA_SIZE + 1] = "/door/1"; +static char data_door_resource_type[MENU_DATA_SIZE + 1] = "oic.r.door"; +static char data_door_resource_state[MENU_DATA_SIZE + 1] = "0"; + +/* Door Resource */ +typedef struct { + bool state; /* attributes ( closed:0, opened 1) */ + char *uri_path; + char *type; + iotcon_resource_interfaces_h ifaces; + uint8_t policies; + iotcon_resource_h handle; + iotcon_observers_h observers; + iotcon_representation_h repr; +} door_resource_s; + +door_resource_s door; +/****************** run **************************************/ +static void _show_door_state() +{ + if (false == door.state) + msgm("[Door] closed."); + else + msgm("[Door] opened."); +} +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, repr); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_response_set_representation() Fail(%d)", ret); + iotcon_response_destroy(response); + return -1; + } + + /* send Representation to the client */ + 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 iotcon_representation_h _get_door_representation(door_resource_s *door) +{ + int ret; + iotcon_attributes_h attributes; + iotcon_representation_h repr; + + /* create attributes */ + ret = iotcon_attributes_create(&attributes); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_attributes_create() Fail(%d)", ret); + iotcon_representation_destroy(repr); + return NULL; + } + + ret = iotcon_attributes_add_bool(attributes, "state", door->state); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_attributes_add_bool() Fail(%d)", ret); + iotcon_attributes_destroy(attributes); + iotcon_representation_destroy(repr); + return NULL; + } + + /* create representation */ + ret = iotcon_representation_create(&repr); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_representation_create() Fail(%d)", ret); + return NULL; + } + + 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_attributes_destroy(attributes); + iotcon_representation_destroy(repr); + return NULL; + } + + /* set attributes to representation */ + ret = iotcon_representation_set_attributes(repr, attributes); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_representation_set_attributes() Fail(%d)", ret); + iotcon_attributes_destroy(attributes); + iotcon_representation_destroy(repr); + return NULL; + } + + iotcon_attributes_destroy(attributes); + return repr; +} + +static int _request_handler_get(door_resource_s *door, iotcon_request_h request) +{ + int ret; + iotcon_representation_h resp_repr; + DBG("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_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 int _set_door_representation(door_resource_s *door, + iotcon_representation_h repr) +{ + int ret; + bool bval; + iotcon_attributes_h attributes; + + ret = iotcon_representation_get_attributes(repr, &attributes); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_representation_get_attributes() Fail(%d)", ret); + return -1; + } + + ret = iotcon_attributes_get_bool(attributes, "state", &bval); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_attributes_get_bool() Fail(%d)", ret); + return -1; + } + + door->state = bval; + + return 0; +} +static int _request_handler_put(door_resource_s *door, iotcon_request_h request) +{ + int ret; + iotcon_representation_h req_repr, resp_repr; + DBG("PUT request"); + + ret = iotcon_request_get_representation(request, &req_repr); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_request_get_representation() Fail(%d)", ret); + return -1; + } + + ret = _set_door_representation(door, req_repr); + if (0 != ret) { + err("_set_door_representation() Fail(%d)", ret); + return -1; + } + _show_door_state(); + + 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_OK); + if (0 != ret) { + err("_send_response() Fail(%d)", ret); + iotcon_representation_destroy(resp_repr); + return -1; + } + + /* notify */ + ret = iotcon_resource_notify(door->handle, resp_repr, door->observers, IOTCON_QOS_HIGH); + if (IOTCON_ERROR_NONE != ret) + err("iotcon_resource_notify() Fail(%d)", ret); + + iotcon_representation_destroy(resp_repr); + + return 0; +} +static int _request_handler_post(door_resource_s *door, iotcon_request_h request) +{ + err("POST request is not supported!!\n"); + return -1; +} +static int _request_handler_delete(door_resource_s *door, iotcon_request_h request) +{ + err("DELETE request is not supported!!\n"); + return -1; +} +static void _request_handler(iotcon_resource_h resource, iotcon_request_h request, + void *user_data) +{ + door_resource_s *door = user_data;; + int ret, observe_id; + iotcon_request_type_e type; + iotcon_observe_type_e observe_type; + char *host_address; + + if (NULL == request) + return; + + 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_ERROR); + return; + } + + ret = iotcon_request_get_request_type(request, &type); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_request_get_request_type() Fail(%d)", ret); + _send_response(request, NULL, IOTCON_RESPONSE_ERROR); + return; + } + + DBG("requested host_address : %s", host_address); + + if (IOTCON_REQUEST_GET == type) + ret = _request_handler_get(door, request); + + else if (IOTCON_REQUEST_PUT == type) + ret = _request_handler_put(door, request); + + else if (IOTCON_REQUEST_POST == type) + ret = _request_handler_post(door, request); + + else if (IOTCON_REQUEST_DELETE == type) + ret = _request_handler_delete(door, request); + + if (0 != ret) { + _send_response(request, NULL, IOTCON_RESPONSE_ERROR); + return; + } + + ret = iotcon_request_get_observe_type(request, &observe_type); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_request_get_observe_type() Fail(%d)", ret); + return; + } + + if (IOTCON_OBSERVE_REGISTER == observe_type) { + DBG("observe_type : IOTCON_OBSERVE_REGISTER"); + 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_type) { + DBG("observe_type : IOTCON_OBSERVE_DEREGISTER"); + 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; + } + } +} + +int run_create_door_resource() +{ + int ret; + iotcon_resource_types_h resource_types; + + /* set door resource */ + door.state = (atoi(data_door_resource_state)) ? true : false; + door.uri_path = strdup(data_door_resource_uri); + door.type = strdup(data_door_resource_type); + door.policies = IOTCON_RESOURCE_DISCOVERABLE + | IOTCON_RESOURCE_OBSERVABLE; + + /* create resource interface */ + ret = iotcon_resource_interfaces_create(&door.ifaces); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_resource_interfaces_create() Fail(%d)", ret); + free(door.type); + free(door.uri_path); + return -1; + } + + ret = iotcon_resource_interfaces_add(door.ifaces, IOTCON_INTERFACE_DEFAULT); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_resource_interfaces_add() Fail(%d)", ret); + iotcon_resource_interfaces_destroy(door.ifaces); + free(door.type); + free(door.uri_path); + return -1; + } + + /* create resource type */ + ret = iotcon_resource_types_create(&resource_types); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_resource_types_create() Fail(%d)", ret); + return -1; + } + + ret = iotcon_resource_types_add(resource_types, door.type); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_resource_types_add() Fail(%d)", ret); + iotcon_resource_types_destroy(resource_types); + return -1; + } + + /* create resource observers */ + ret = iotcon_observers_create(&door.observers); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_observers_create() Fail"); + iotcon_resource_interfaces_destroy(door.ifaces); + free(door.type); + free(door.uri_path); + return -1; + } + + /* create(register) resource */ + ret = iotcon_resource_create(door.uri_path, + resource_types, + door.ifaces, + door.policies, + _request_handler, + &door, + &door.handle); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_resource_create() Fail"); + iotcon_resource_types_destroy(resource_types); + return -1; + } + + iotcon_resource_types_destroy(resource_types); + + DBG("resource create success!!"); + + return 0; +} +int run_change_door_resource_state() +{ + int ret; + iotcon_representation_h repr; + door.state = (atoi(data_door_resource_state)) ? true : false; + + _show_door_state(); + + repr = _get_door_representation(&door); + if (NULL == repr) { + err("_get_door_representation() Fail"); + return -1; + } + + ret = iotcon_resource_notify(door.handle, repr, door.observers, IOTCON_QOS_HIGH); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_resource_notify() Fail(%d)", ret); + iotcon_representation_destroy(repr); + return -1; + } + + iotcon_representation_destroy(repr); + return 0; +} + +/****************** sub menu data **************************************/ +static struct menu_data create_door_resource[] = { + {"1", "resource uri", 0, 0, data_door_resource_uri}, + {"2", "resource type", 0, 0, data_door_resource_type}, + {"3", "resource state", 0, 0, data_door_resource_state}, + {"4", "run", 0, run_create_door_resource, NULL}, + {0, 0,}, +}; +static struct menu_data change_door_resource_state[] = { + {"1", "resource state", 0, 0, data_door_resource_state}, + {"2", "run", 0, run_change_door_resource_state, NULL}, + {0, 0,}, +}; +/*********************************************************************************/ +/* Menu */ +struct menu_data menu_door[] = { + {"1", "create door resource", create_door_resource, NULL, NULL}, + {"2", "change door resource state", change_door_resource_state, NULL, NULL}, + {NULL, NULL,}, +}; diff --git a/test_app/iotcon-test-provisioning-client.c b/test_app/iotcon-test-provisioning-client.c new file mode 100755 index 0000000..d49ce7f --- /dev/null +++ b/test_app/iotcon-test-provisioning-client.c @@ -0,0 +1,169 @@ +/* + * + * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jooseok Park <jooseok.park@samsung.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/time.h> +#include <unistd.h> +#include <glib.h> +#include <gio/gio.h> +#include "menu.h" +#include <iotcon.h> +#include <iotcon-internal.h> +#define SVR_DB "/usr/bin/iotcon-test-svr-db-provisioning.dat" +#define PROV_DB "/usr/bin/iotcon-test-provisioning-db.db" + +static char data_provisioning_type[MENU_DATA_SIZE + 1] = "2"; + +/****************** run **************************************/ + + +static bool _provisioning_found_cb(iotcon_provisioning_device_h device, + iotcon_error_e result, void *user_data) +{ + int ret; + bool owned; + char *device_id; + char *host_address; + iotcon_connectivity_type_e connectivity_type; + iotcon_provisioning_oxm_e oxm; + iotcon_provisioning_device_h device_clone; + + if (IOTCON_ERROR_NONE != result) { + ERR("Invalid result (%d)", result); + return IOTCON_FUNC_STOP; + } + + ret = iotcon_provisioning_device_get_id(device, &device_id); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_provisioning_device_get_id() Fail(%d)", ret); + return IOTCON_FUNC_STOP; + } + DBG("device_id:[%s]", device_id); + + ret = iotcon_provisioning_device_clone(device, &device_clone); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_provisioning_device_clone() Fail(%d)", ret); + return IOTCON_FUNC_STOP; + } + + ret = iotcon_provisioning_device_get_connectivity_type(device_clone, &connectivity_type); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_provisioning_device_get_connectivity_type() Fail(%d)", ret); + return IOTCON_FUNC_STOP; + } + DBG("connectivity_type:[%d]", connectivity_type); + + ret = iotcon_provisioning_device_get_host_address(device_clone, &host_address); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_provisioning_device_get_host_address() Fail(%d)", ret); + return IOTCON_FUNC_STOP; + } + DBG("host_address:[%s]", host_address); + + ret = iotcon_provisioning_device_get_oxm(device_clone, &oxm); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_provisioning_device_get_oxm() Fail(%d)", ret); + return IOTCON_FUNC_STOP; + } + DBG("oxm:[%d]", oxm); + + ret = iotcon_provisioning_device_is_owned(device_clone, &owned); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_provisioning_device_is_owned() Fail(%d)", ret); + return IOTCON_FUNC_STOP; + } + DBG("owned:[%d]", owned); + + return IOTCON_FUNC_CONTINUE; +} +static int run_find_provisioining_device() +{ + int ret; + int find_type; + + find_type = atoi(data_provisioning_type); + DBG("find_type:[%d]", find_type); + ret = iotcon_provisioning_find_device(find_type, + _provisioning_found_cb, NULL); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_provisioning_find_device() Fail(%d)", ret); + + + return 0; +} +/****************** sub menu data **************************************/ + +static struct menu_data find_provisioining_device[] = { + {"1", "find type", 0, 0, data_provisioning_type}, + {"2", "run", 0, run_find_provisioining_device, NULL}, + {0, 0,}, +}; + +/*********************************************************************************/ +static struct menu_data menu_main[] = { + {"1", "find provisioining device", find_provisioining_device, NULL, NULL}, + {NULL, NULL,}, +}; +static int _initialize(MManager *mm, struct menu_data *menu) +{ + int ret; + ret = iotcon_provisioning_initialize(SVR_DB, PROV_DB); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_initialize() Fail(%d)", ret); + return -1; + } + return 0; +} + +int main(int arg, char **argv) +{ + GMainLoop *mainloop = NULL; + GIOChannel *channel = g_io_channel_unix_new(STDIN_FILENO); + MManager *manager = NULL; + struct menu_data launch_menu[2] = {{0,},}; + + mainloop = g_main_loop_new(NULL, FALSE); + + DBG("\n******* IoTCon Simple Client Test Application *******"); + + /* Launch Menu */ + launch_menu[0].title = "Simple Client"; + launch_menu[0].key = "1"; + launch_menu[0].sub_menu = menu_main; + launch_menu[0].callback = _initialize; + launch_menu[0].data = NULL; + + /* Menu manager */ + manager = menu_manager_new(launch_menu, mainloop); + menu_manager_run(manager); + + g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL), + on_menu_manager_keyboard, manager); + g_main_loop_run(mainloop); + g_main_loop_unref(mainloop); + + iotcon_deinitialize(); + + DBG("******* Bye bye *******"); + + return 0; +} + diff --git a/test_app/iotcon-test-simple-client.c b/test_app/iotcon-test-simple-client.c new file mode 100755 index 0000000..32d6d22 --- /dev/null +++ b/test_app/iotcon-test-simple-client.c @@ -0,0 +1,963 @@ +/* + * + * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jooseok Park <jooseok.park@samsung.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/time.h> +#include <unistd.h> +#include <glib.h> +#include <gio/gio.h> +#include "menu.h" +#include <iotcon.h> +#include <iotcon-internal.h> +#define SVR_DB "/usr/bin/iotcon-test-svr-db-client-all.dat" /* "/home/owner/apps_rw/iotcon-test-svr-db-client.dat" */ + +static char data_presence_resource_type[MENU_DATA_SIZE + 1] = "oic.r.door"; + +static char data_resource_type[MENU_DATA_SIZE + 1] = "oic.r.door"; +static char data_resource_interface[MENU_DATA_SIZE + 1] = "oic.if.baseline"; +static char data_resource_conn_type[MENU_DATA_SIZE + 1] = "65537"; /* IOTCON_CONNECTIVITY_IP | IOTCON_CONNECTIVITY_PREFER_UDP */ +static char data_resource_state[MENU_DATA_SIZE + 1] = "1"; +static char data_resource_id[MENU_DATA_SIZE + 1] = "0"; + +static iotcon_remote_resource_h resource_clone[100]; +static int resource_clone_idx; +static iotcon_presence_h g_presence_handle; + +/****************** run **************************************/ +static void _on_response(iotcon_remote_resource_h resource, iotcon_error_e err, + iotcon_request_type_e request_type, iotcon_response_h response, void *user_data); + +static void _on_response_delete(iotcon_remote_resource_h resource, + iotcon_response_h response, void *user_data) +{ + int ret; + iotcon_response_result_e response_result; + + ret = iotcon_response_get_result(response, &response_result); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_result() Fail(%d)", ret); + return; + } + + if (IOTCON_RESPONSE_OK != response_result + && IOTCON_RESPONSE_RESOURCE_DELETED != response_result) { + ERR("_on_response_delete Response error(%d)", response_result); + return; + } + INFO("DELETE request was successful"); + + /* delete callback operations */ + + iotcon_remote_resource_destroy(resource); +} + +static void _on_response_post(iotcon_remote_resource_h resource, + iotcon_response_h response, void *user_data) +{ + int ret; + iotcon_attributes_h recv_attributes; + char *host, *created_uri_path; + iotcon_connectivity_type_e connectivity_type; + iotcon_response_result_e response_result; + iotcon_resource_types_h types = NULL; + iotcon_resource_interfaces_h ifaces = NULL; + iotcon_remote_resource_h new_door_resource; + iotcon_representation_h recv_repr = NULL; + + ret = iotcon_response_get_result(response, &response_result); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_result() Fail(%d)", ret); + return; + } + + if (IOTCON_RESPONSE_RESOURCE_CREATED != response_result) { + ERR("_on_response_post Response error(%d)", response_result); + return; + } + INFO("POST request was successful"); + + ret = iotcon_response_get_representation(response, &recv_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_representation() Fail(%d)", ret); + return; + } + + ret = iotcon_representation_get_attributes(recv_repr, &recv_attributes); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_get_attributes() Fail(%d)", ret); + return; + } + + ret = iotcon_attributes_get_str(recv_attributes, "createduripath", &created_uri_path); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_attributes_get_str() Fail(%d)", ret); + return; + } + DBG("New resource created : %s", created_uri_path); + + ret = iotcon_remote_resource_get_host_address(resource, &host); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_host_address() Fail(%d)", ret); + return; + } + + ret = iotcon_remote_resource_get_connectivity_type(resource, &connectivity_type); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_connectivity_type() Fail(%d)", ret); + return; + } + + ret = iotcon_remote_resource_get_types(resource, &types); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_types() Fail(%d)", ret); + return; + } + + ret = iotcon_remote_resource_get_interfaces(resource, &ifaces); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_interfaces() Fail(%d)", ret); + return; + } + + ret = iotcon_remote_resource_create(host, connectivity_type, created_uri_path, + IOTCON_RESOURCE_SECURE, types, ifaces, &new_door_resource); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_create() Fail(%d)", ret); + return; + } + + ret = iotcon_remote_resource_delete(new_door_resource, _on_response, NULL); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_delete() Fail(%d)", ret); + iotcon_remote_resource_destroy(new_door_resource); + return; + } +} + +static void _on_response_put(iotcon_remote_resource_h resource, + iotcon_response_h response, void *user_data) +{ + int ret; + iotcon_response_result_e response_result; + iotcon_representation_h send_repr; + + ret = iotcon_response_get_result(response, &response_result); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_result() Fail(%d)", ret); + return; + } + + if (IOTCON_RESPONSE_RESOURCE_CHANGED != response_result) { + ERR("_on_response_put Response error(%d)", response_result); + return; + } + INFO("PUT request was successful"); + + ret = iotcon_representation_create(&send_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_create() Fail(%d)", ret); + return; + } + + /* send POST request */ + ret = iotcon_remote_resource_post(resource, send_repr, NULL, _on_response, NULL); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_remote_resource_post() Fail(%d)", ret); + + iotcon_representation_destroy(send_repr); +} + +static void _on_response_get(iotcon_remote_resource_h resource, + iotcon_response_h response, void *user_data) +{ + int ret; + int state = 1; + iotcon_response_result_e response_result; + iotcon_representation_h send_repr; + iotcon_representation_h recv_repr; + iotcon_attributes_h send_attributes; + iotcon_attributes_h recv_attributes = NULL; + + ret = iotcon_response_get_result(response, &response_result); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_result() Fail(%d)", ret); + return; + } + + if (IOTCON_RESPONSE_OK != response_result) { + ERR("_on_response_get Response error(%d)", response_result); + return; + } + + /* get the resource host address */ + char *resource_host = NULL; + iotcon_remote_resource_get_host_address(resource, &resource_host); + INFO("resource host : %s", resource_host); + + ret = iotcon_response_get_representation(response, &recv_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_representation() Fail(%d)", ret); + return; + } + + ret = iotcon_representation_get_attributes(recv_repr, &recv_attributes); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_get_attributes() Fail(%d)", ret); + return; + } + + ret = iotcon_attributes_get_int(recv_attributes, "DOOR_ATTRIBUTE", &state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_attributes_get_int() Fail(%d)", ret); + return; + } + + ret = iotcon_representation_create(&send_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_create() Fail(%d)", ret); + return; + } + + ret = iotcon_attributes_create(&send_attributes); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_attributes_create() Fail(%d)", ret); + iotcon_representation_destroy(send_repr); + return; + } + + ret = iotcon_attributes_add_int(send_attributes, "DOOR_ATTRIBUTE",++state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_attributes_add_int() Fail(%d)", ret); + iotcon_attributes_destroy(send_attributes); + iotcon_representation_destroy(send_repr); + return; + } + + ret = iotcon_representation_set_attributes(send_repr, send_attributes); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_set_attributes() Fail(%d)", ret); + iotcon_attributes_destroy(send_attributes); + iotcon_representation_destroy(send_repr); + return; + } + + iotcon_attributes_destroy(send_attributes); + + /* send PUT request */ + ret = iotcon_remote_resource_put(resource, send_repr, NULL, _on_response, NULL); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_remote_resource_put() Fail(%d)", ret); + + iotcon_representation_destroy(send_repr); +} + +static bool _get_res_iface_cb(const char *string, void *user_data) +{ + INFO("resource interface:[%s]", string); + return IOTCON_FUNC_CONTINUE; +} + +static bool _get_res_type_cb(const char *string, void *user_data) +{ + INFO("resource type:[%s]", string); + return IOTCON_FUNC_CONTINUE; +} + +static void _on_response(iotcon_remote_resource_h resource, iotcon_error_e err, + iotcon_request_type_e request_type, iotcon_response_h response, void *user_data) +{ + if (IOTCON_ERROR_NONE != err) { + ERR("_on_response error(%d)", err); + return; + } + INFO("request(%d) was successful", request_type); + + switch (request_type) { + case IOTCON_REQUEST_GET: + _on_response_get(resource, response, user_data); + break; + case IOTCON_REQUEST_PUT: + _on_response_put(resource, response, user_data); + break; + case IOTCON_REQUEST_POST: + _on_response_post(resource, response, user_data); + break; + case IOTCON_REQUEST_DELETE: + _on_response_delete(resource, response, user_data); + break; + default: + ERR("Invalid request type (%d)", request_type); + return; + } +} + +static bool _found_resource(iotcon_remote_resource_h resource, iotcon_error_e result, + void *user_data) +{ + int ret; + char *resource_host; + char *resource_uri_path; + char *resource_device_id; + char *resource_device_name; + iotcon_resource_types_h resource_types; + iotcon_resource_interfaces_h resource_interfaces; + iotcon_connectivity_type_e connectivity_type; + + if (IOTCON_ERROR_NONE != result) { + ERR("Invalid result(%d)", result); + return IOTCON_FUNC_STOP; + } + if (NULL == resource) + return IOTCON_FUNC_CONTINUE; + + /* get the resource URI */ + ret = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_uri_path() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + + INFO("===== [%d] : resource found [%s] =====", resource_clone_idx, resource_uri_path); + + /* get the device unique id. + * this is unique per-server independent on how it was discovered. */ + ret = iotcon_remote_resource_get_device_id(resource, &resource_device_id); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_device_id() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + INFO("resource device id:[%s]", resource_device_id); + + ret = iotcon_remote_resource_get_device_name(resource, &resource_device_name); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_device_name() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + INFO("resource device name:[%s]", resource_device_name); + + /* get the resource host address */ + ret = iotcon_remote_resource_get_host_address(resource, &resource_host); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_host_address() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + INFO("resource host address:[%s]", resource_host); + + ret = iotcon_remote_resource_get_connectivity_type(resource, &connectivity_type); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_connectivity_type() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + + /* get the resource interfaces */ + ret = iotcon_remote_resource_get_interfaces(resource, &resource_interfaces); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_interfaces() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + + ret = iotcon_resource_interfaces_foreach(resource_interfaces, _get_res_iface_cb, + resource_uri_path); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_resource_interfaces_foreach() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + + /* get the resource types */ + ret = iotcon_remote_resource_get_types(resource, &resource_types); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_types() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + + ret = iotcon_resource_types_foreach(resource_types, _get_res_type_cb, + resource_uri_path); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_resource_types_foreach() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + + ret = iotcon_remote_resource_clone(resource, &resource_clone[resource_clone_idx]); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_clone() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + resource_clone_idx++; + + return IOTCON_FUNC_CONTINUE; +} +int run_find_resource() +{ + int ret; + int conn_type; + iotcon_query_h query = NULL; + + if (strlen(data_resource_type) != 0) { + ret = iotcon_query_create(&query); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_query_create() Fail(%d)", ret); + return -1; + } + ret = iotcon_query_set_resource_type(query, data_resource_type); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_query_set_resource_type() Fail(%d)", ret); + iotcon_query_destroy(query); + return -1; + } + } + + if (strlen(data_resource_interface) != 0) { + if (query == NULL) { + ret = iotcon_query_create(&query); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_query_create() Fail(%d)", ret); + return -1; + } + } + ret = iotcon_query_set_interface(query, data_resource_interface); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_query_set_interface() Fail(%d)", ret); + iotcon_query_destroy(query); + return -1; + } + } + conn_type = atoi(data_resource_conn_type); + DBG("conn_type :[%d][0x%x]", conn_type, conn_type); + + ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS, conn_type, query, _found_resource, NULL); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_find_resource() Fail(%d)", ret); + iotcon_deinitialize(); + return -1; + } + if (query) + iotcon_query_destroy(query); + + resource_clone_idx = 0; + return 0; +} + +static void _on_observe_cb(iotcon_remote_resource_h resource, iotcon_error_e err, + int sequence_number, iotcon_response_h response, void *user_data) +{ + int ret; + int state; + iotcon_attributes_h attributes; + iotcon_representation_h repr; + iotcon_response_result_e response_result; + char *resource_uri_path; + + DBG("_on_observe_cb() seq_num:[%d]", sequence_number); + + if (IOTCON_ERROR_NONE != err) { + ERR("_on_observe error(%d)", err); + return; + } + + ret = iotcon_response_get_result(response, &response_result); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_result() Fail(%d)", ret); + return; + } + + if (IOTCON_RESPONSE_OK != response_result) { + ERR("_on_response_observe Response error(%d)", response_result); + return; + } + + ret = iotcon_response_get_representation(response, &repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_representation() Fail(%d)", ret); + return; + } + + ret = iotcon_representation_get_attributes(repr, &attributes); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_get_attributes() Fail(%d)", ret); + return; + } + + ret = iotcon_attributes_get_int(attributes, "DOOR_ATTRIBUTE", &state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_attributes_get_int() Fail(%d)", ret); + return; + } + ret = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_uri_path() Fail(%d)", ret); + return; + } + INFO("[%s] %d", resource_uri_path, state); + +} + +int run_observe_resource() +{ + int ret; + int resource_idx = atoi(data_resource_id); + + DBG("resource_idx:[%d]", resource_idx); + ret = iotcon_remote_resource_observe_register(resource_clone[resource_idx], + IOTCON_OBSERVE_IGNORE_OUT_OF_ORDER, NULL, _on_observe_cb, NULL); + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_observe_register() Fail(%d)", ret); + return -1; + } + + DBG("observe resource success!!"); + return 0; +} + +int run_cancel_observed_resource() +{ + int ret; + + int resource_idx = atoi(data_resource_id); + + DBG("resource_idx:[%d]", resource_idx); + ret = iotcon_remote_resource_observe_deregister(resource_clone[resource_idx]); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_observe_deregister() Fail(%d)", ret); + return -1; + } + + DBG("deregister observed resource success!!"); + return 0; +} +static void _on_get_response_cb(iotcon_remote_resource_h resource, iotcon_error_e err, + iotcon_request_type_e request_type, iotcon_response_h response, void *user_data) +{ + int ret; + int state = 1; + iotcon_response_result_e response_result; + iotcon_representation_h recv_repr; + iotcon_attributes_h recv_attributes = NULL; + char *resource_uri_path = NULL; + + if (IOTCON_ERROR_NONE != err) { + ERR("_on_get_response_cb error(%d)", err); + return; + } + ret = iotcon_response_get_result(response, &response_result); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_result() Fail(%d)", ret); + return; + } + + if (IOTCON_RESPONSE_OK != response_result) { + ERR("_on_response_get Response error(%d)", response_result); + return; + } + + ret = iotcon_response_get_representation(response, &recv_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_representation() Fail(%d)", ret); + return; + } + + ret = iotcon_representation_get_attributes(recv_repr, &recv_attributes); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_get_attributes() Fail(%d)", ret); + return; + } + + ret = iotcon_attributes_get_int(recv_attributes, "DOOR_ATTRIBUTE", &state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_attributes_get_int() Fail(%d)", ret); + return; + } + + ret = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_uri_path() Fail(%d)", ret); + return; + } + INFO("_on_get_response_cb(): [%s] %d", resource_uri_path, (state)); +} +int run_get_resource() +{ + int ret; + int resource_idx = atoi(data_resource_id); + + DBG("resource_idx:[%d]", resource_idx); + ret = iotcon_remote_resource_get(resource_clone[resource_idx], NULL, _on_get_response_cb, NULL); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get() Fail(%d)", ret); + return -1; + } + + return 0; +} + +static void _on_put_response_cb(iotcon_remote_resource_h resource, iotcon_error_e err, + iotcon_request_type_e request_type, iotcon_response_h response, void *user_data) +{ + int ret; + iotcon_response_result_e response_result; + + if (IOTCON_ERROR_NONE != err) { + ERR("_on_get_response_cb error(%d)", err); + return; + } + ret = iotcon_response_get_result(response, &response_result); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_result() Fail(%d)", ret); + return; + } + + if (IOTCON_RESPONSE_RESOURCE_CHANGED != response_result) { + ERR("_on_response_put Response error(%d)", response_result); + return; + } + DBG("PUT request was successful"); +} +int run_put_resource() +{ + int ret; + int state; + iotcon_representation_h send_repr; + iotcon_attributes_h send_attributes; + + DBG("put..."); + + ret = iotcon_representation_create(&send_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_create() Fail(%d)", ret); + return -1; + } + + ret = iotcon_attributes_create(&send_attributes); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_attributes_create() Fail(%d)", ret); + iotcon_representation_destroy(send_repr); + return -1; + } + + state = (atoi(data_resource_state)); + ret = iotcon_attributes_add_int(send_attributes, "DOOR_ATTRIBUTE", state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_attributes_add_int() Fail(%d)", ret); + iotcon_attributes_destroy(send_attributes); + iotcon_representation_destroy(send_repr); + return -1; + } + + ret = iotcon_representation_set_attributes(send_repr, send_attributes); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_set_attributes() Fail(%d)", ret); + iotcon_attributes_destroy(send_attributes); + iotcon_representation_destroy(send_repr); + return -1; + } + + iotcon_attributes_destroy(send_attributes); + + /* send PUT request */ + int resource_idx = atoi(data_resource_id); + + DBG("resource_idx:[%d]", resource_idx); + ret = iotcon_remote_resource_put(resource_clone[resource_idx], send_repr, NULL, _on_put_response_cb, NULL); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_put() Fail(%d)", ret); + return -1; + } + + iotcon_representation_destroy(send_repr); + return 0; +} +static void _presence_cb(iotcon_presence_h presence, iotcon_error_e err, + iotcon_presence_response_h response, void *user_data) +{ + char *host_address; + char *resource_type; + int ret; + iotcon_presence_result_e result; + iotcon_presence_trigger_e trigger; + iotcon_connectivity_type_e connectivity_type; + + DBG("_presence_cb().."); + if (IOTCON_ERROR_NONE != err) { + err("_presence_cb error %d", err); + return; + } + + ret = iotcon_presence_response_get_result(response, &result); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_result() Fail(%d)", ret); + return; + } + INFO("result:[%d] (0:OK, 1: STOPPED, 2: TIMEOUT)", result); + + if (IOTCON_PRESENCE_OK == result) { + ret = iotcon_presence_response_get_trigger(response, &trigger); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_trigger() Fail(%d)", ret); + return; + } + INFO("trigger:[%d] (0:CREATED, 1: UPDATED, 2: DESTROYED)", trigger); + } + + ret = iotcon_presence_response_get_host_address(response, &host_address); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_host_address() Fail(%d)", ret); + return; + } + INFO("host_address:[%s]", host_address); + + ret = iotcon_presence_response_get_connectivity_type(response, &connectivity_type); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_connectivity_type() Fail(%d)", ret); + return; + } + INFO("connectivity_type:[%d]", connectivity_type); + + ret = iotcon_presence_response_get_resource_type(response, &resource_type); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_resource_type() Fail(%d)", ret); + return; + } + INFO("resource_type:[%s]", resource_type); +} + +int run_add_presence_cb() +{ + int ret; + + DBG("data_presence_resource_type [%s]", data_presence_resource_type); + if (strlen(data_presence_resource_type) != 0) { + ret = iotcon_add_presence_cb(NULL, IOTCON_CONNECTIVITY_IP, data_presence_resource_type, + _presence_cb, NULL, &g_presence_handle); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_add_presence_cb() Fail(%d)", ret); + } else { + ret = iotcon_add_presence_cb(NULL, IOTCON_CONNECTIVITY_IP, NULL, + _presence_cb, NULL, &g_presence_handle); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_add_presence_cb() Fail(%d)", ret); + } + return 0; +} +int run_add_presence_cb2() +{ + int ret; + + DBG("data_host_addr [NULL], data_presence_resource_type [%s]", data_presence_resource_type); + ret = iotcon_add_presence_cb(NULL, IOTCON_CONNECTIVITY_IP, data_presence_resource_type, + _presence_cb, NULL, &g_presence_handle); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_add_presence_cb() Fail(%d)", ret); + + return 0; +} +int run_add_presence_cb3() +{ + int ret; + + DBG("data_host_addr [NULL], data_presence_resource_type [NULL]"); + ret = iotcon_add_presence_cb(NULL, IOTCON_CONNECTIVITY_IP, NULL, + _presence_cb, NULL, &g_presence_handle); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_add_presence_cb() Fail(%d)", ret); + + return 0; +} +int run_remove_presence_cb() +{ + int ret; + + ret = iotcon_remove_presence_cb(g_presence_handle); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_remove_presence_cb() Fail(%d)", ret); + + return 0; +} + +static void _unicast_presence_cb(iotcon_presence_h presence, iotcon_error_e err, + iotcon_presence_response_h response, void *user_data) +{ + char *host_address; + char *resource_type; + int ret; + iotcon_presence_result_e result; + iotcon_presence_trigger_e trigger; + iotcon_connectivity_type_e connectivity_type; + + DBG("_unicast_presence_cb().."); + if (IOTCON_ERROR_NONE != err) { + err("_presence_cb error %d", err); + return; + } + + ret = iotcon_presence_response_get_result(response, &result); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_result() Fail(%d)", ret); + return; + } + INFO("result:[%d] (0:OK, 1: STOPPED, 2: TIMEOUT)", result); + + if (IOTCON_PRESENCE_OK == result) { + ret = iotcon_presence_response_get_trigger(response, &trigger); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_trigger() Fail(%d)", ret); + return; + } + INFO("trigger:[%d] (0:CREATED, 1: UPDATED, 2: DESTROYED)", trigger); + } + + ret = iotcon_presence_response_get_host_address(response, &host_address); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_host_address() Fail(%d)", ret); + return; + } + INFO("host_address:[%s]", host_address); + + ret = iotcon_presence_response_get_connectivity_type(response, &connectivity_type); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_connectivity_type() Fail(%d)", ret); + return; + } + INFO("connectivity_type:[%d]", connectivity_type); + + ret = iotcon_presence_response_get_resource_type(response, &resource_type); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_resource_type() Fail(%d)", ret); + return; + } + INFO("resource_type:[%s]", resource_type); +} + +int run_add_unicast_presence_cb() +{ + int ret; + char *resource_host; + iotcon_connectivity_type_e connectivity_type; + iotcon_presence_h presence_handle; + int resource_idx = atoi(data_resource_id); + + DBG("resource_idx:[%d]", resource_idx); + + ret = iotcon_remote_resource_get_host_address(resource_clone[resource_idx], &resource_host); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_host_address() Fail(%d)", ret); + return 0; + } + + ret = iotcon_remote_resource_get_connectivity_type(resource_clone[resource_idx], &connectivity_type); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_connectivity_type() Fail(%d)", ret); + return 0; + } + + DBG("resource_host:[%s], connectivity_type:[%d(0x%x)]", resource_host, connectivity_type, connectivity_type); + ret = iotcon_add_presence_cb(resource_host, connectivity_type, NULL, + _unicast_presence_cb, NULL, &presence_handle); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_add_presence_cb() Fail(%d)", ret); + return 0; + } + + return 0; +} + +/****************** sub menu data **************************************/ +static struct menu_data add_presence_cb[] = { + {"1", "data_presence_resource_type", 0, 0, data_presence_resource_type}, + {"2", "run", 0, run_add_presence_cb, NULL}, + {"5", "run(remove)", 0, run_remove_presence_cb, NULL}, + {0, 0,}, +}; +static struct menu_data find_resource[] = { + {"1", "resource type", 0, 0, data_resource_type}, + {"2", "resource interface", 0, 0, data_resource_interface}, + {"3", "connection type", 0, 0, data_resource_conn_type}, + {"4", "run", 0, run_find_resource, NULL}, + {0, 0,}, +}; + +static struct menu_data put_resource[] = { + {"1", "DOOR_ATTRIBUTE", 0, 0, data_resource_state}, + {"2", "run", 0, run_put_resource, NULL}, + {0, 0,}, +}; + +static struct menu_data manage_resource[] = { + {"1", "resource id", 0, 0, data_resource_id}, + {"2", "observe resource", 0, run_observe_resource, NULL}, + {"3", "cancel observed resource", 0, run_cancel_observed_resource, NULL}, + {"4", "get resource", 0, run_get_resource, NULL}, + {"5", "put resource", put_resource, NULL, NULL}, + {"6", "add unicast presence cb", 0, run_add_unicast_presence_cb, NULL}, + {0, 0,}, +}; + +/*********************************************************************************/ +static struct menu_data menu_main[] = { + {"1", "add multicast presence cb", add_presence_cb, NULL, NULL}, + {"2", "find resource", find_resource, NULL, NULL}, + {"3", "manage resource", manage_resource, NULL, NULL}, + {NULL, NULL,}, +}; +static int _initialize(MManager *mm, struct menu_data *menu) +{ + int ret; + ret = iotcon_initialize(SVR_DB); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_initialize() Fail(%d)", ret); + return -1; + } + return 0; +} + +int main(int arg, char **argv) +{ + GMainLoop *mainloop = NULL; + GIOChannel *channel = g_io_channel_unix_new(STDIN_FILENO); + MManager *manager = NULL; + struct menu_data launch_menu[2] = {{0,},}; + + mainloop = g_main_loop_new(NULL, FALSE); + + DBG("\n******* IoTCon Simple Client Test Application *******"); + + /* Launch Menu */ + launch_menu[0].title = "Simple Client"; + launch_menu[0].key = "1"; + launch_menu[0].sub_menu = menu_main; + launch_menu[0].callback = _initialize; + launch_menu[0].data = NULL; + + /* Menu manager */ + manager = menu_manager_new(launch_menu, mainloop); + menu_manager_run(manager); + + g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL), + on_menu_manager_keyboard, manager); + g_main_loop_run(mainloop); + g_main_loop_unref(mainloop); + + iotcon_deinitialize(); + + DBG("******* Bye bye *******"); + + return 0; +} + diff --git a/test_app/iotcon-test-simple-client.c.orig b/test_app/iotcon-test-simple-client.c.orig new file mode 100755 index 0000000..0c64a8b --- /dev/null +++ b/test_app/iotcon-test-simple-client.c.orig @@ -0,0 +1,963 @@ +/* + * + * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jooseok Park <jooseok.park@samsung.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/time.h> +#include <unistd.h> +#include <glib.h> +#include <gio/gio.h> +#include "menu.h" +#include <iotcon.h> +#include <iotcon-internal.h> +#define SVR_DB "/usr/bin/iotcon-test-svr-db-client-all.dat" /* "/home/owner/apps_rw/iotcon-test-svr-db-client.dat" */ + +static char data_presence_resource_type[MENU_DATA_SIZE + 1] = "oic.r.door"; + +static char data_resource_type[MENU_DATA_SIZE + 1] = "oic.r.door"; +static char data_resource_interface[MENU_DATA_SIZE + 1] = "oic.if.baseline"; +static char data_resource_conn_type[MENU_DATA_SIZE + 1] = "65537"; /* IOTCON_CONNECTIVITY_IP | IOTCON_CONNECTIVITY_PREFER_UDP */ +static char data_resource_state[MENU_DATA_SIZE + 1] = "1"; +static char data_resource_id[MENU_DATA_SIZE + 1] = "0"; + +static iotcon_remote_resource_h resource_clone[100]; +static int resource_clone_idx; +static iotcon_presence_h g_presence_handle; + +/****************** run **************************************/ +static void _on_response(iotcon_remote_resource_h resource, iotcon_error_e err, + iotcon_request_type_e request_type, iotcon_response_h response, void *user_data); + +static void _on_response_delete(iotcon_remote_resource_h resource, + iotcon_response_h response, void *user_data) +{ + int ret; + iotcon_response_result_e response_result; + + ret = iotcon_response_get_result(response, &response_result); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_result() Fail(%d)", ret); + return; + } + + if (IOTCON_RESPONSE_OK != response_result + && IOTCON_RESPONSE_RESOURCE_DELETED != response_result) { + ERR("_on_response_delete Response error(%d)", response_result); + return; + } + INFO("DELETE request was successful"); + + /* delete callback operations */ + + iotcon_remote_resource_destroy(resource); +} + +static void _on_response_post(iotcon_remote_resource_h resource, + iotcon_response_h response, void *user_data) +{ + int ret; + iotcon_attributes_h recv_attributes; + char *host, *created_uri_path; + iotcon_connectivity_type_e connectivity_type; + iotcon_response_result_e response_result; + iotcon_resource_types_h types = NULL; + iotcon_resource_interfaces_h ifaces = NULL; + iotcon_remote_resource_h new_door_resource; + iotcon_representation_h recv_repr = NULL; + + ret = iotcon_response_get_result(response, &response_result); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_result() Fail(%d)", ret); + return; + } + + if (IOTCON_RESPONSE_RESOURCE_CREATED != response_result) { + ERR("_on_response_post Response error(%d)", response_result); + return; + } + INFO("POST request was successful"); + + ret = iotcon_response_get_representation(response, &recv_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_representation() Fail(%d)", ret); + return; + } + + ret = iotcon_representation_get_attributes(recv_repr, &recv_attributes); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_get_attributes() Fail(%d)", ret); + return; + } + + ret = iotcon_attributes_get_str(recv_attributes, "createduripath", &created_uri_path); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_attributes_get_str() Fail(%d)", ret); + return; + } + DBG("New resource created : %s", created_uri_path); + + ret = iotcon_remote_resource_get_host_address(resource, &host); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_host_address() Fail(%d)", ret); + return; + } + + ret = iotcon_remote_resource_get_connectivity_type(resource, &connectivity_type); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_connectivity_type() Fail(%d)", ret); + return; + } + + ret = iotcon_remote_resource_get_types(resource, &types); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_types() Fail(%d)", ret); + return; + } + + ret = iotcon_remote_resource_get_interfaces(resource, &ifaces); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_interfaces() Fail(%d)", ret); + return; + } + + ret = iotcon_remote_resource_create(host, connectivity_type, created_uri_path, + IOTCON_RESOURCE_SECURE, types, ifaces, &new_door_resource); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_create() Fail(%d)", ret); + return; + } + + ret = iotcon_remote_resource_delete(new_door_resource, _on_response, NULL); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_delete() Fail(%d)", ret); + iotcon_remote_resource_destroy(new_door_resource); + return; + } +} + +static void _on_response_put(iotcon_remote_resource_h resource, + iotcon_response_h response, void *user_data) +{ + int ret; + iotcon_response_result_e response_result; + iotcon_representation_h send_repr; + + ret = iotcon_response_get_result(response, &response_result); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_result() Fail(%d)", ret); + return; + } + + if (IOTCON_RESPONSE_RESOURCE_CHANGED != response_result) { + ERR("_on_response_put Response error(%d)", response_result); + return; + } + INFO("PUT request was successful"); + + ret = iotcon_representation_create(&send_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_create() Fail(%d)", ret); + return; + } + + /* send POST request */ + ret = iotcon_remote_resource_post(resource, send_repr, NULL, _on_response, NULL); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_remote_resource_post() Fail(%d)", ret); + + iotcon_representation_destroy(send_repr); +} + +static void _on_response_get(iotcon_remote_resource_h resource, + iotcon_response_h response, void *user_data) +{ + int ret; + bool state = true; + iotcon_response_result_e response_result; + iotcon_representation_h send_repr; + iotcon_representation_h recv_repr; + iotcon_attributes_h send_attributes; + iotcon_attributes_h recv_attributes = NULL; + + ret = iotcon_response_get_result(response, &response_result); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_result() Fail(%d)", ret); + return; + } + + if (IOTCON_RESPONSE_OK != response_result) { + ERR("_on_response_get Response error(%d)", response_result); + return; + } + + /* get the resource host address */ + char *resource_host = NULL; + iotcon_remote_resource_get_host_address(resource, &resource_host); + INFO("resource host : %s", resource_host); + + ret = iotcon_response_get_representation(response, &recv_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_representation() Fail(%d)", ret); + return; + } + + ret = iotcon_representation_get_attributes(recv_repr, &recv_attributes); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_get_attributes() Fail(%d)", ret); + return; + } + + ret = iotcon_attributes_get_bool(recv_attributes, "state", &state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_attributes_get_bool() Fail(%d)", ret); + return; + } + + ret = iotcon_representation_create(&send_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_create() Fail(%d)", ret); + return; + } + + ret = iotcon_attributes_create(&send_attributes); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_attributes_create() Fail(%d)", ret); + iotcon_representation_destroy(send_repr); + return; + } + + ret = iotcon_attributes_add_bool(send_attributes, "state", !state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_attributes_add_bool() Fail(%d)", ret); + iotcon_attributes_destroy(send_attributes); + iotcon_representation_destroy(send_repr); + return; + } + + ret = iotcon_representation_set_attributes(send_repr, send_attributes); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_set_attributes() Fail(%d)", ret); + iotcon_attributes_destroy(send_attributes); + iotcon_representation_destroy(send_repr); + return; + } + + iotcon_attributes_destroy(send_attributes); + + /* send PUT request */ + ret = iotcon_remote_resource_put(resource, send_repr, NULL, _on_response, NULL); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_remote_resource_put() Fail(%d)", ret); + + iotcon_representation_destroy(send_repr); +} + +static bool _get_res_iface_cb(const char *string, void *user_data) +{ + INFO("resource interface:[%s]", string); + return IOTCON_FUNC_CONTINUE; +} + +static bool _get_res_type_cb(const char *string, void *user_data) +{ + INFO("resource type:[%s]", string); + return IOTCON_FUNC_CONTINUE; +} + +static void _on_response(iotcon_remote_resource_h resource, iotcon_error_e err, + iotcon_request_type_e request_type, iotcon_response_h response, void *user_data) +{ + if (IOTCON_ERROR_NONE != err) { + ERR("_on_response error(%d)", err); + return; + } + INFO("request(%d) was successful", request_type); + + switch (request_type) { + case IOTCON_REQUEST_GET: + _on_response_get(resource, response, user_data); + break; + case IOTCON_REQUEST_PUT: + _on_response_put(resource, response, user_data); + break; + case IOTCON_REQUEST_POST: + _on_response_post(resource, response, user_data); + break; + case IOTCON_REQUEST_DELETE: + _on_response_delete(resource, response, user_data); + break; + default: + ERR("Invalid request type (%d)", request_type); + return; + } +} + +static bool _found_resource(iotcon_remote_resource_h resource, iotcon_error_e result, + void *user_data) +{ + int ret; + char *resource_host; + char *resource_uri_path; + char *resource_device_id; + char *resource_device_name; + iotcon_resource_types_h resource_types; + iotcon_resource_interfaces_h resource_interfaces; + iotcon_connectivity_type_e connectivity_type; + + if (IOTCON_ERROR_NONE != result) { + ERR("Invalid result(%d)", result); + return IOTCON_FUNC_STOP; + } + if (NULL == resource) + return IOTCON_FUNC_CONTINUE; + + /* get the resource URI */ + ret = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_uri_path() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + + INFO("===== [%d] : resource found [%s] =====", resource_clone_idx, resource_uri_path); + + /* get the device unique id. + * this is unique per-server independent on how it was discovered. */ + ret = iotcon_remote_resource_get_device_id(resource, &resource_device_id); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_device_id() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + INFO("resource device id:[%s]", resource_device_id); + + ret = iotcon_remote_resource_get_device_name(resource, &resource_device_name); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_device_name() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + INFO("resource device name:[%s]", resource_device_name); + + /* get the resource host address */ + ret = iotcon_remote_resource_get_host_address(resource, &resource_host); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_host_address() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + INFO("resource host address:[%s]", resource_host); + + ret = iotcon_remote_resource_get_connectivity_type(resource, &connectivity_type); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_connectivity_type() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + + /* get the resource interfaces */ + ret = iotcon_remote_resource_get_interfaces(resource, &resource_interfaces); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_interfaces() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + + ret = iotcon_resource_interfaces_foreach(resource_interfaces, _get_res_iface_cb, + resource_uri_path); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_resource_interfaces_foreach() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + + /* get the resource types */ + ret = iotcon_remote_resource_get_types(resource, &resource_types); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_types() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + + ret = iotcon_resource_types_foreach(resource_types, _get_res_type_cb, + resource_uri_path); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_resource_types_foreach() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + + ret = iotcon_remote_resource_clone(resource, &resource_clone[resource_clone_idx]); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_clone() Fail(%d)", ret); + return IOTCON_FUNC_CONTINUE; + } + resource_clone_idx++; + + return IOTCON_FUNC_CONTINUE; +} +int run_find_resource() +{ + int ret; + int conn_type; + iotcon_query_h query = NULL; + + if (strlen(data_resource_type) != 0) { + ret = iotcon_query_create(&query); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_query_create() Fail(%d)", ret); + return -1; + } + ret = iotcon_query_set_resource_type(query, data_resource_type); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_query_set_resource_type() Fail(%d)", ret); + iotcon_query_destroy(query); + return -1; + } + } + + if (strlen(data_resource_interface) != 0) { + if (query == NULL) { + ret = iotcon_query_create(&query); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_query_create() Fail(%d)", ret); + return -1; + } + } + ret = iotcon_query_set_interface(query, data_resource_interface); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_query_set_interface() Fail(%d)", ret); + iotcon_query_destroy(query); + return -1; + } + } + conn_type = atoi(data_resource_conn_type); + DBG("conn_type :[%d][0x%x]", conn_type, conn_type); + + ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS, conn_type, query, _found_resource, NULL); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_find_resource() Fail(%d)", ret); + iotcon_deinitialize(); + return -1; + } + if (query) + iotcon_query_destroy(query); + + resource_clone_idx = 0; + return 0; +} + +static void _on_observe_cb(iotcon_remote_resource_h resource, iotcon_error_e err, + int sequence_number, iotcon_response_h response, void *user_data) +{ + int ret; + bool state; + iotcon_attributes_h attributes; + iotcon_representation_h repr; + iotcon_response_result_e response_result; + char *resource_uri_path; + + DBG("_on_observe_cb() seq_num:[%d]", sequence_number); + + if (IOTCON_ERROR_NONE != err) { + ERR("_on_observe error(%d)", err); + return; + } + + ret = iotcon_response_get_result(response, &response_result); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_result() Fail(%d)", ret); + return; + } + + if (IOTCON_RESPONSE_OK != response_result) { + ERR("_on_response_observe Response error(%d)", response_result); + return; + } + + ret = iotcon_response_get_representation(response, &repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_representation() Fail(%d)", ret); + return; + } + + ret = iotcon_representation_get_attributes(repr, &attributes); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_get_attributes() Fail(%d)", ret); + return; + } + + ret = iotcon_attributes_get_bool(attributes, "state", &state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_attributes_get_bool() Fail(%d)", ret); + return; + } + ret = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_uri_path() Fail(%d)", ret); + return; + } + INFO("[%s] %s", resource_uri_path, (state) ? "opened" : "closed"); + +} + +int run_observe_resource() +{ + int ret; + int resource_idx = atoi(data_resource_id); + + DBG("resource_idx:[%d]", resource_idx); + ret = iotcon_remote_resource_observe_register(resource_clone[resource_idx], + IOTCON_OBSERVE_IGNORE_OUT_OF_ORDER, NULL, _on_observe_cb, NULL); + + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_observe_register() Fail(%d)", ret); + return -1; + } + + DBG("observe resource success!!"); + return 0; +} + +int run_cancel_observed_resource() +{ + int ret; + + int resource_idx = atoi(data_resource_id); + + DBG("resource_idx:[%d]", resource_idx); + ret = iotcon_remote_resource_observe_deregister(resource_clone[resource_idx]); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_observe_deregister() Fail(%d)", ret); + return -1; + } + + DBG("deregister observed resource success!!"); + return 0; +} +static void _on_get_response_cb(iotcon_remote_resource_h resource, iotcon_error_e err, + iotcon_request_type_e request_type, iotcon_response_h response, void *user_data) +{ + int ret; + bool state = true; + iotcon_response_result_e response_result; + iotcon_representation_h recv_repr; + iotcon_attributes_h recv_attributes = NULL; + char *resource_uri_path = NULL; + + if (IOTCON_ERROR_NONE != err) { + ERR("_on_get_response_cb error(%d)", err); + return; + } + ret = iotcon_response_get_result(response, &response_result); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_result() Fail(%d)", ret); + return; + } + + if (IOTCON_RESPONSE_OK != response_result) { + ERR("_on_response_get Response error(%d)", response_result); + return; + } + + ret = iotcon_response_get_representation(response, &recv_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_representation() Fail(%d)", ret); + return; + } + + ret = iotcon_representation_get_attributes(recv_repr, &recv_attributes); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_get_attributes() Fail(%d)", ret); + return; + } + + ret = iotcon_attributes_get_bool(recv_attributes, "state", &state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_attributes_get_bool() Fail(%d)", ret); + return; + } + + ret = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_uri_path() Fail(%d)", ret); + return; + } + INFO("_on_get_response_cb(): [%s] %s", resource_uri_path, (state) ? "opened" : "closed"); +} +int run_get_resource() +{ + int ret; + int resource_idx = atoi(data_resource_id); + + DBG("resource_idx:[%d]", resource_idx); + ret = iotcon_remote_resource_get(resource_clone[resource_idx], NULL, _on_get_response_cb, NULL); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get() Fail(%d)", ret); + return -1; + } + + return 0; +} + +static void _on_put_response_cb(iotcon_remote_resource_h resource, iotcon_error_e err, + iotcon_request_type_e request_type, iotcon_response_h response, void *user_data) +{ + int ret; + iotcon_response_result_e response_result; + + if (IOTCON_ERROR_NONE != err) { + ERR("_on_get_response_cb error(%d)", err); + return; + } + ret = iotcon_response_get_result(response, &response_result); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_response_get_result() Fail(%d)", ret); + return; + } + + if (IOTCON_RESPONSE_RESOURCE_CHANGED != response_result) { + ERR("_on_response_put Response error(%d)", response_result); + return; + } + DBG("PUT request was successful"); +} +int run_put_resource() +{ + int ret; + bool state; + iotcon_representation_h send_repr; + iotcon_attributes_h send_attributes; + + DBG("put..."); + + ret = iotcon_representation_create(&send_repr); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_create() Fail(%d)", ret); + return -1; + } + + ret = iotcon_attributes_create(&send_attributes); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_attributes_create() Fail(%d)", ret); + iotcon_representation_destroy(send_repr); + return -1; + } + + state = (atoi(data_resource_state)) ? true : false; + ret = iotcon_attributes_add_bool(send_attributes, "state", state); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_attributes_add_bool() Fail(%d)", ret); + iotcon_attributes_destroy(send_attributes); + iotcon_representation_destroy(send_repr); + return -1; + } + + ret = iotcon_representation_set_attributes(send_repr, send_attributes); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_representation_set_attributes() Fail(%d)", ret); + iotcon_attributes_destroy(send_attributes); + iotcon_representation_destroy(send_repr); + return -1; + } + + iotcon_attributes_destroy(send_attributes); + + /* send PUT request */ + int resource_idx = atoi(data_resource_id); + + DBG("resource_idx:[%d]", resource_idx); + ret = iotcon_remote_resource_put(resource_clone[resource_idx], send_repr, NULL, _on_put_response_cb, NULL); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_put() Fail(%d)", ret); + return -1; + } + + iotcon_representation_destroy(send_repr); + return 0; +} +static void _presence_cb(iotcon_presence_h presence, iotcon_error_e err, + iotcon_presence_response_h response, void *user_data) +{ + char *host_address; + char *resource_type; + int ret; + iotcon_presence_result_e result; + iotcon_presence_trigger_e trigger; + iotcon_connectivity_type_e connectivity_type; + + DBG("_presence_cb().."); + if (IOTCON_ERROR_NONE != err) { + err("_presence_cb error %d", err); + return; + } + + ret = iotcon_presence_response_get_result(response, &result); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_result() Fail(%d)", ret); + return; + } + INFO("result:[%d] (0:OK, 1: STOPPED, 2: TIMEOUT)", result); + + if (IOTCON_PRESENCE_OK == result) { + ret = iotcon_presence_response_get_trigger(response, &trigger); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_trigger() Fail(%d)", ret); + return; + } + INFO("trigger:[%d] (0:CREATED, 1: UPDATED, 2: DESTROYED)", trigger); + } + + ret = iotcon_presence_response_get_host_address(response, &host_address); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_host_address() Fail(%d)", ret); + return; + } + INFO("host_address:[%s]", host_address); + + ret = iotcon_presence_response_get_connectivity_type(response, &connectivity_type); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_connectivity_type() Fail(%d)", ret); + return; + } + INFO("connectivity_type:[%d]", connectivity_type); + + ret = iotcon_presence_response_get_resource_type(response, &resource_type); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_resource_type() Fail(%d)", ret); + return; + } + INFO("resource_type:[%s]", resource_type); +} + +int run_add_presence_cb() +{ + int ret; + + DBG("data_presence_resource_type [%s]", data_presence_resource_type); + if (strlen(data_presence_resource_type) != 0) { + ret = iotcon_add_presence_cb(NULL, IOTCON_CONNECTIVITY_IP, data_presence_resource_type, + _presence_cb, NULL, &g_presence_handle); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_add_presence_cb() Fail(%d)", ret); + } else { + ret = iotcon_add_presence_cb(NULL, IOTCON_CONNECTIVITY_IP, NULL, + _presence_cb, NULL, &g_presence_handle); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_add_presence_cb() Fail(%d)", ret); + } + return 0; +} +int run_add_presence_cb2() +{ + int ret; + + DBG("data_host_addr [NULL], data_presence_resource_type [%s]", data_presence_resource_type); + ret = iotcon_add_presence_cb(NULL, IOTCON_CONNECTIVITY_IP, data_presence_resource_type, + _presence_cb, NULL, &g_presence_handle); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_add_presence_cb() Fail(%d)", ret); + + return 0; +} +int run_add_presence_cb3() +{ + int ret; + + DBG("data_host_addr [NULL], data_presence_resource_type [NULL]"); + ret = iotcon_add_presence_cb(NULL, IOTCON_CONNECTIVITY_IP, NULL, + _presence_cb, NULL, &g_presence_handle); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_add_presence_cb() Fail(%d)", ret); + + return 0; +} +int run_remove_presence_cb() +{ + int ret; + + ret = iotcon_remove_presence_cb(g_presence_handle); + if (IOTCON_ERROR_NONE != ret) + ERR("iotcon_remove_presence_cb() Fail(%d)", ret); + + return 0; +} + +static void _unicast_presence_cb(iotcon_presence_h presence, iotcon_error_e err, + iotcon_presence_response_h response, void *user_data) +{ + char *host_address; + char *resource_type; + int ret; + iotcon_presence_result_e result; + iotcon_presence_trigger_e trigger; + iotcon_connectivity_type_e connectivity_type; + + DBG("_unicast_presence_cb().."); + if (IOTCON_ERROR_NONE != err) { + err("_presence_cb error %d", err); + return; + } + + ret = iotcon_presence_response_get_result(response, &result); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_result() Fail(%d)", ret); + return; + } + INFO("result:[%d] (0:OK, 1: STOPPED, 2: TIMEOUT)", result); + + if (IOTCON_PRESENCE_OK == result) { + ret = iotcon_presence_response_get_trigger(response, &trigger); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_trigger() Fail(%d)", ret); + return; + } + INFO("trigger:[%d] (0:CREATED, 1: UPDATED, 2: DESTROYED)", trigger); + } + + ret = iotcon_presence_response_get_host_address(response, &host_address); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_host_address() Fail(%d)", ret); + return; + } + INFO("host_address:[%s]", host_address); + + ret = iotcon_presence_response_get_connectivity_type(response, &connectivity_type); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_connectivity_type() Fail(%d)", ret); + return; + } + INFO("connectivity_type:[%d]", connectivity_type); + + ret = iotcon_presence_response_get_resource_type(response, &resource_type); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_presence_response_get_resource_type() Fail(%d)", ret); + return; + } + INFO("resource_type:[%s]", resource_type); +} + +int run_add_unicast_presence_cb() +{ + int ret; + char *resource_host; + iotcon_connectivity_type_e connectivity_type; + iotcon_presence_h presence_handle; + int resource_idx = atoi(data_resource_id); + + DBG("resource_idx:[%d]", resource_idx); + + ret = iotcon_remote_resource_get_host_address(resource_clone[resource_idx], &resource_host); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_host_address() Fail(%d)", ret); + return 0; + } + + ret = iotcon_remote_resource_get_connectivity_type(resource_clone[resource_idx], &connectivity_type); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_remote_resource_get_connectivity_type() Fail(%d)", ret); + return 0; + } + + DBG("resource_host:[%s], connectivity_type:[%d(0x%x)]", resource_host, connectivity_type, connectivity_type); + ret = iotcon_add_presence_cb(resource_host, connectivity_type, NULL, + _unicast_presence_cb, NULL, &presence_handle); + if (IOTCON_ERROR_NONE != ret) { + ERR("iotcon_add_presence_cb() Fail(%d)", ret); + return 0; + } + + return 0; +} + +/****************** sub menu data **************************************/ +static struct menu_data add_presence_cb[] = { + {"1", "data_presence_resource_type", 0, 0, data_presence_resource_type}, + {"2", "run", 0, run_add_presence_cb, NULL}, + {"5", "run(remove)", 0, run_remove_presence_cb, NULL}, + {0, 0,}, +}; +static struct menu_data find_resource[] = { + {"1", "resource type", 0, 0, data_resource_type}, + {"2", "resource interface", 0, 0, data_resource_interface}, + {"3", "connection type", 0, 0, data_resource_conn_type}, + {"4", "run", 0, run_find_resource, NULL}, + {0, 0,}, +}; + +static struct menu_data put_resource[] = { + {"1", "state", 0, 0, data_resource_state}, + {"2", "run", 0, run_put_resource, NULL}, + {0, 0,}, +}; + +static struct menu_data manage_resource[] = { + {"1", "resource id", 0, 0, data_resource_id}, + {"2", "observe resource", 0, run_observe_resource, NULL}, + {"3", "cancel observed resource", 0, run_cancel_observed_resource, NULL}, + {"4", "get resource", 0, run_get_resource, NULL}, + {"5", "put resource", put_resource, NULL, NULL}, + {"6", "add unicast presence cb", 0, run_add_unicast_presence_cb, NULL}, + {0, 0,}, +}; + +/*********************************************************************************/ +static struct menu_data menu_main[] = { + {"1", "add multicast presence cb", add_presence_cb, NULL, NULL}, + {"2", "find resource", find_resource, NULL, NULL}, + {"3", "manage resource", manage_resource, NULL, NULL}, + {NULL, NULL,}, +}; +static int _initialize(MManager *mm, struct menu_data *menu) +{ + int ret; + ret = iotcon_initialize(SVR_DB); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_initialize() Fail(%d)", ret); + return -1; + } + return 0; +} + +int main(int arg, char **argv) +{ + GMainLoop *mainloop = NULL; + GIOChannel *channel = g_io_channel_unix_new(STDIN_FILENO); + MManager *manager = NULL; + struct menu_data launch_menu[2] = {{0,},}; + + mainloop = g_main_loop_new(NULL, FALSE); + + DBG("\n******* IoTCon Simple Client Test Application *******"); + + /* Launch Menu */ + launch_menu[0].title = "Simple Client"; + launch_menu[0].key = "1"; + launch_menu[0].sub_menu = menu_main; + launch_menu[0].callback = _initialize; + launch_menu[0].data = NULL; + + /* Menu manager */ + manager = menu_manager_new(launch_menu, mainloop); + menu_manager_run(manager); + + g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL), + on_menu_manager_keyboard, manager); + g_main_loop_run(mainloop); + g_main_loop_unref(mainloop); + + iotcon_deinitialize(); + + DBG("******* Bye bye *******"); + + return 0; +} + diff --git a/test_app/iotcon-test-simple-server.c b/test_app/iotcon-test-simple-server.c new file mode 100755 index 0000000..b3b0481 --- /dev/null +++ b/test_app/iotcon-test-simple-server.c @@ -0,0 +1,125 @@ +/* + * + * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jooseok Park <jooseok.park@samsung.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/time.h> +#include <unistd.h> +#include <glib.h> +#include <gio/gio.h> +#include "menu.h" +#include <iotcon.h> + +#define SVR_DB "/usr/bin/iotcon-test-svr-db-server-all.dat" /* "/home/owner/apps_rw/iotcon-test-svr-db-server-all.dat" */ +static char data_device_name[MENU_DATA_SIZE + 1] = "simple-server"; +static char data_presence_state[MENU_DATA_SIZE + 1] = "1"; + +extern struct menu_data menu_door[]; +extern struct menu_data menu_light[]; +/****************** run **************************************/ + +int run_set_device_name() +{ + int ret; + ret = iotcon_set_device_name(data_device_name); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_set_device_name() Fail(%d)", ret); + return -1; + } + return 0; +} + +int run_set_presence() +{ + bool state; + state = (atoi(data_presence_state)) ? true : false; + + if (state) + iotcon_start_presence(0); + else + iotcon_stop_presence(); + + return 0; +} + +/**************************************************************/ +/* Sub Menu */ +static struct menu_data set_device_name[] = { + {"1", "device name", 0, 0, data_device_name}, + {"2", "run", 0, run_set_device_name, NULL}, + {0, 0,}, +}; + +static struct menu_data set_presence[] = { + {"1", "presence state", 0, 0, data_presence_state}, + {"2", "run", 0, run_set_presence, NULL}, + {0, 0,}, +}; + +/**************************************************************/ +/* Main Menu */ +static struct menu_data menu_main[] = { + {"1", "set device name", set_device_name, NULL, NULL}, + {"2", "set presence", set_presence, NULL, NULL}, + {"3", "door resource", menu_door, NULL, NULL}, + {NULL, NULL,}, +}; + +static int _initialize(MManager *mm, struct menu_data *menu) +{ + int ret; + ret = iotcon_initialize(SVR_DB); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_initialize() Fail(%d)", ret); + return -1; + } + return 0; +} + +int main(int arg, char **argv) +{ + GMainLoop *mainloop = NULL; + GIOChannel *channel = g_io_channel_unix_new(STDIN_FILENO); + MManager *manager = NULL; + struct menu_data launch_menu[2] = {{0,},}; + + mainloop = g_main_loop_new(NULL, FALSE); + + DBG("\n******* IoTCon Simple Server Test Application *******"); + + /* Launch Menu */ + launch_menu[0].title = "Simple Server"; + launch_menu[0].key = "1"; + launch_menu[0].sub_menu = menu_main; + launch_menu[0].callback = _initialize; + launch_menu[0].data = NULL; + + /* Menu manager */ + manager = menu_manager_new(launch_menu, mainloop); + menu_manager_run(manager); + + g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL), + on_menu_manager_keyboard, manager); + g_main_loop_run(mainloop); + g_main_loop_unref(mainloop); + + DBG("******* Bye bye *******"); + return 0; +} diff --git a/test_app/iotcon-test-svr-db-server-all.dat b/test_app/iotcon-test-svr-db-server-all.dat Binary files differnew file mode 100644 index 0000000..80088b2 --- /dev/null +++ b/test_app/iotcon-test-svr-db-server-all.dat diff --git a/test_app/light.c b/test_app/light.c new file mode 100755 index 0000000..d21ad9b --- /dev/null +++ b/test_app/light.c @@ -0,0 +1,437 @@ +/* + * + * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jooseok Park <jooseok.park@samsung.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/time.h> +#include <unistd.h> +#include <glib.h> +#include <gio/gio.h> +#include "menu.h" +#include <iotcon.h> + +static char data_light_resource_uri[MENU_DATA_SIZE + 1] = "/light/1"; +static char data_light_resource_type[MENU_DATA_SIZE + 1] = "oic.r.light"; +static char data_light_resource_state[MENU_DATA_SIZE + 1] = "0"; + +/* Light Resource */ +typedef struct { + bool state; /* attributes ( closed:0, opened 1) */ + char *uri_path; + char *type; + iotcon_resource_interfaces_h ifaces; + uint8_t policies; + iotcon_resource_h handle; + iotcon_observers_h observers; + iotcon_representation_h repr; +} light_resource_s; + +light_resource_s light; +/****************** run **************************************/ +static void _show_light_state() +{ + if (false == light.state) + INFO("[Light] Off."); + else + INFO("[Light] On."); +} +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, repr); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_response_set_representation() Fail(%d)", ret); + iotcon_response_destroy(response); + return -1; + } + + /* send Representation to the client */ + 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 iotcon_representation_h _get_light_representation(light_resource_s *light) +{ + int ret; + iotcon_attributes_h attributes; + iotcon_representation_h repr; + + /* create attributes */ + ret = iotcon_attributes_create(&attributes); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_attributes_create() Fail(%d)", ret); + iotcon_representation_destroy(repr); + return NULL; + } + + ret = iotcon_attributes_add_bool(attributes, "state", light->state); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_attributes_add_bool() Fail(%d)", ret); + iotcon_attributes_destroy(attributes); + iotcon_representation_destroy(repr); + return NULL; + } + + /* create representation */ + ret = iotcon_representation_create(&repr); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_representation_create() Fail(%d)", ret); + return NULL; + } + + ret = iotcon_representation_set_uri_path(repr, light->uri_path); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_representation_set_uri_path() Fail(%d)", ret); + iotcon_attributes_destroy(attributes); + iotcon_representation_destroy(repr); + return NULL; + } + + /* set attributes to representation */ + ret = iotcon_representation_set_attributes(repr, attributes); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_representation_set_attributes() Fail(%d)", ret); + iotcon_attributes_destroy(attributes); + iotcon_representation_destroy(repr); + return NULL; + } + + iotcon_attributes_destroy(attributes); + return repr; +} + +static int _request_handler_get(light_resource_s *light, iotcon_request_h request) +{ + int ret; + iotcon_representation_h resp_repr; + DBG("GET request"); + + resp_repr = _get_light_representation(light); + if (NULL == resp_repr) { + err("_get_light_representation() Fail"); + return -1; + } + + ret = _send_response(request, resp_repr, IOTCON_RESPONSE_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 int _set_light_representation(light_resource_s *light, + iotcon_representation_h repr) +{ + int ret; + bool bval; + iotcon_attributes_h attributes; + + ret = iotcon_representation_get_attributes(repr, &attributes); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_representation_get_attributes() Fail(%d)", ret); + return -1; + } + + ret = iotcon_attributes_get_bool(attributes, "state", &bval); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_attributes_get_bool() Fail(%d)", ret); + return -1; + } + + light->state = bval; + + return 0; +} +static int _request_handler_put(light_resource_s *light, iotcon_request_h request) +{ + int ret; + iotcon_representation_h req_repr, resp_repr; + DBG("PUT request"); + + ret = iotcon_request_get_representation(request, &req_repr); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_request_get_representation() Fail(%d)", ret); + return -1; + } + + ret = _set_light_representation(light, req_repr); + if (0 != ret) { + err("_set_light_representation() Fail(%d)", ret); + return -1; + } + _show_light_state(); + + resp_repr = _get_light_representation(light); + if (NULL == resp_repr) { + err("_get_light_representation() Fail"); + return -1; + } + + ret = _send_response(request, resp_repr, IOTCON_RESPONSE_OK); + if (0 != ret) { + err("_send_response() Fail(%d)", ret); + iotcon_representation_destroy(resp_repr); + return -1; + } + + /* notify */ + ret = iotcon_resource_notify(light->handle, resp_repr, light->observers, IOTCON_QOS_HIGH); + if (IOTCON_ERROR_NONE != ret) + err("iotcon_resource_notify() Fail(%d)", ret); + + iotcon_representation_destroy(resp_repr); + + return 0; +} +static int _request_handler_post(light_resource_s *light, iotcon_request_h request) +{ + err("POST request is not supported!!\n"); + return -1; +} +static int _request_handler_delete(light_resource_s *light, iotcon_request_h request) +{ + err("DELETE request is not supported!!\n"); + return -1; +} +static void _request_handler(iotcon_resource_h resource, iotcon_request_h request, + void *user_data) +{ + light_resource_s *light = user_data;; + int ret, observe_id; + iotcon_request_type_e type; + iotcon_observe_type_e observe_type; + char *host_address; + + if (NULL == request) + return; + + 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_ERROR); + return; + } + + ret = iotcon_request_get_request_type(request, &type); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_request_get_request_type() Fail(%d)", ret); + _send_response(request, NULL, IOTCON_RESPONSE_ERROR); + return; + } + + DBG("requested host_address : %s", host_address); + + if (IOTCON_REQUEST_GET == type) + ret = _request_handler_get(light, request); + + else if (IOTCON_REQUEST_PUT == type) + ret = _request_handler_put(light, request); + + else if (IOTCON_REQUEST_POST == type) + ret = _request_handler_post(light, request); + + else if (IOTCON_REQUEST_DELETE == type) + ret = _request_handler_delete(light, request); + + if (0 != ret) { + _send_response(request, NULL, IOTCON_RESPONSE_ERROR); + return; + } + + ret = iotcon_request_get_observe_type(request, &observe_type); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_request_get_observe_type() Fail(%d)", ret); + return; + } + + if (IOTCON_OBSERVE_REGISTER == observe_type) { + DBG("observe_type : IOTCON_OBSERVE_REGISTER"); + 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(light->observers, observe_id); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_observers_add() Fail(%d)", ret); + return; + } + } else if (IOTCON_OBSERVE_DEREGISTER == observe_type) { + DBG("observe_type : IOTCON_OBSERVE_DEREGISTER"); + 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(light->observers, observe_id); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_observers_remove() Fail(%d)", ret); + return; + } + } +} + +int run_create_light_resource() +{ + int ret; + iotcon_resource_types_h resource_types; + + /* set light resource */ + light.state = (atoi(data_light_resource_state)) ? true : false; + light.uri_path = strdup(data_light_resource_uri); + light.type = strdup(data_light_resource_type); + light.policies = IOTCON_RESOURCE_DISCOVERABLE + | IOTCON_RESOURCE_OBSERVABLE; + + /* create resource interface */ + ret = iotcon_resource_interfaces_create(&light.ifaces); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_resource_interfaces_create() Fail(%d)", ret); + free(light.type); + free(light.uri_path); + return -1; + } + + ret = iotcon_resource_interfaces_add(light.ifaces, IOTCON_INTERFACE_DEFAULT); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_resource_interfaces_add() Fail(%d)", ret); + iotcon_resource_interfaces_destroy(light.ifaces); + free(light.type); + free(light.uri_path); + return -1; + } + + /* create resource type */ + ret = iotcon_resource_types_create(&resource_types); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_resource_types_create() Fail(%d)", ret); + return -1; + } + + ret = iotcon_resource_types_add(resource_types, light.type); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_resource_types_add() Fail(%d)", ret); + iotcon_resource_types_destroy(resource_types); + return -1; + } + + /* create resource observers */ + ret = iotcon_observers_create(&light.observers); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_observers_create() Fail"); + iotcon_resource_interfaces_destroy(light.ifaces); + free(light.type); + free(light.uri_path); + return -1; + } + + /* create(register) resource */ + ret = iotcon_resource_create(light.uri_path, + resource_types, + light.ifaces, + light.policies, + _request_handler, + &light, + &light.handle); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_resource_create() Fail"); + iotcon_resource_types_destroy(resource_types); + return -1; + } + + iotcon_resource_types_destroy(resource_types); + + DBG("resource create success!!"); + + return 0; +} +int run_change_light_resource_state() +{ + int ret; + iotcon_representation_h repr; + light.state = (atoi(data_light_resource_state)) ? true : false; + + _show_light_state(); + + repr = _get_light_representation(&light); + if (NULL == repr) { + err("_get_light_representation() Fail"); + return -1; + } + + ret = iotcon_resource_notify(light.handle, repr, light.observers, IOTCON_QOS_HIGH); + if (IOTCON_ERROR_NONE != ret) { + err("iotcon_resource_notify() Fail(%d)", ret); + iotcon_representation_destroy(repr); + return -1; + } + + iotcon_representation_destroy(repr); + return 0; +} + +/****************** sub menu data **************************************/ +static struct menu_data create_light_resource[] = { + {"1", "resource uri", 0, 0, data_light_resource_uri}, + {"2", "resource type", 0, 0, data_light_resource_type}, + {"3", "resource state", 0, 0, data_light_resource_state}, + {"4", "run", 0, run_create_light_resource, NULL}, + {0, 0,}, +}; +static struct menu_data change_light_resource_state[] = { + {"1", "resource state", 0, 0, data_light_resource_state}, + {"2", "run", 0, run_change_light_resource_state, NULL}, + {0, 0,}, +}; +/*********************************************************************************/ +/* Menu */ +struct menu_data menu_light[] = { + {"1", "create light resource", create_light_resource, NULL, NULL}, + {"2", "change light resource state", change_light_resource_state, NULL, NULL}, + {NULL, NULL,}, +}; diff --git a/test_app/menu.c b/test_app/menu.c new file mode 100755 index 0000000..a6f579e --- /dev/null +++ b/test_app/menu.c @@ -0,0 +1,374 @@ +/* + * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <glib.h> +#include <asm/unistd.h> +#include <unistd.h> +#include <sys/syscall.h> + +#include "menu.h" + +#define DEFAULT_MENU_MENU "m" +#define DEFAULT_MENU_PREV "p" +#define DEFAULT_MENU_QUIT "q" +#define DEFAULT_MENU_NONE "-" +#define TAB_SPACE " " + +struct menu_manager { + GQueue *stack; + GQueue *title_stack; + + struct menu_data *menu; + + char *buf; + + void *user_data; + GMainLoop *mainloop; +}; + + +char key_buffer[MENU_DATA_SIZE]; +int flag_pid_display = 1; + + +static void _show_prompt(void) +{ + msgn("(%5d) >> ", get_tid()); +} + +static void _show_reserved_menu(void) +{ + msg(ANSI_COLOR_DARKGRAY HR_SINGLE2 ANSI_COLOR_NORMAL); + msg(ANSI_COLOR_DARKGRAY " [ " ANSI_COLOR_NORMAL "%s" ANSI_COLOR_DARKGRAY + " ] " ANSI_COLOR_NORMAL "Previous menu " , DEFAULT_MENU_PREV); + msg(ANSI_COLOR_DARKGRAY " [ " ANSI_COLOR_NORMAL "%s" ANSI_COLOR_DARKGRAY + " ] " ANSI_COLOR_NORMAL "Show Menu " , DEFAULT_MENU_MENU); + msg(ANSI_COLOR_DARKGRAY " [ " ANSI_COLOR_NORMAL "%s" ANSI_COLOR_DARKGRAY + " ] " ANSI_COLOR_NORMAL "Quit " , DEFAULT_MENU_QUIT); +} + +static void _show_input_ok(void) +{ + msg("OK."); +} + +static void _show_menu(MManager *m, struct menu_data menu[]) +{ + int i = 0; + int len = 0; + struct menu_data *item; + char title_buf[256] = { 0, }; + + if (!menu) + return; + + msg(""); + msg(HR_DOUBLE); + + len = g_queue_get_length(m->title_stack); + msgn(ANSI_COLOR_YELLOW " Main"); + if (len > 0) { + for (i = 0; i < len; i++) { + msgn(ANSI_COLOR_NORMAL " >> " ANSI_COLOR_YELLOW "%s", + (char *)g_queue_peek_nth(m->title_stack, i)); + } + } + msg(ANSI_COLOR_NORMAL); + msg(HR_SINGLE); + + hide_pid(); + i = 0; + + while (1) { + item = menu + i; + if (item->key == NULL) + break; + + if (!g_strcmp0(item->key, "-")) { + msgn(" "); + } else if (!g_strcmp0(item->key, "_")) { + msg(ANSI_COLOR_DARKGRAY HR_SINGLE2 ANSI_COLOR_NORMAL); + + if (item->callback) + item->callback(m, item); + + i++; + + continue; + } else if (!g_strcmp0(item->key, "*")) { + msg(" %s", item->title); + if (item->callback) + item->callback(m, item); + } else { + msgn(ANSI_COLOR_DARKGRAY " [" ANSI_COLOR_NORMAL "%3s" + ANSI_COLOR_DARKGRAY "] " ANSI_COLOR_NORMAL, item->key); + } + + memset(title_buf, 0, 256); + if (item->title) { + snprintf(title_buf, MAX_TITLE, "%s", item->title); + + if (strlen(item->title) >= MAX_TITLE) { + title_buf[MAX_TITLE - 2] = '.'; + title_buf[MAX_TITLE - 1] = '.'; + } + } + + if (item->data) { + msg("%s " ANSI_COLOR_LIGHTBLUE "(%s)" ANSI_COLOR_NORMAL, + title_buf, item->data); + } else if (!g_strcmp0(item->key, "*")) { + /* none */ + } else { + msg("%s", title_buf); + } + + if (item->sub_menu) + msg("\e[1A\e[%dC >", (int)POS_MORE); + + i++; + } + + show_pid(); + + _show_reserved_menu(); + + msg(HR_DOUBLE); + + _show_prompt(); +} + +static void _show_item_data_input_msg(struct menu_data *item) +{ + msg(""); + msg(HR_DOUBLE); + msg(" Input [%s] data ", item->title); + msg(HR_SINGLE); + msg(" current = [%s]", item->data); + msgn(" new >> "); +} + +static void _move_menu(MManager *mm, struct menu_data menu[], char *key) +{ + struct menu_data *item; + int i = 0; + + if (!mm->menu) + return; + + if (!g_strcmp0(DEFAULT_MENU_PREV, key)) { + if (g_queue_get_length(mm->stack) > 0) { + mm->menu = g_queue_pop_tail(mm->stack); + g_queue_pop_tail(mm->title_stack); + } + + _show_menu(mm, mm->menu); + mm->buf = key_buffer; + + return; + } else if (!g_strcmp0(DEFAULT_MENU_MENU, key)) { + _show_menu(mm, mm->menu); + return; + } else if (!g_strcmp0(DEFAULT_MENU_QUIT, key)) { + g_main_loop_quit(mm->mainloop); + return; + } else if (!g_strcmp0(DEFAULT_MENU_NONE, key)) { + _show_prompt(); + return; + } + + while (1) { + int ret = RET_SUCCESS; + item = menu + i; + if (item->key == NULL) + break; + + if (!g_strcmp0(item->key, key)) { + if (item->callback) { + ret = item->callback(mm, item); + _show_prompt(); + } + + if (RET_SUCCESS == ret) { + if (item->sub_menu) { + g_queue_push_tail(mm->stack, mm->menu); + g_queue_push_tail(mm->title_stack, (gpointer)item->title); + + mm->menu = item->sub_menu; + _show_menu(mm, mm->menu); + mm->buf = key_buffer; + } + + if (item->data) { + _show_item_data_input_msg(item); + mm->buf = item->data; + } + } + + return; + } + + i++; + } + + _show_prompt(); +} + +MManager *menu_manager_new(struct menu_data items[], GMainLoop *mainloop) +{ + MManager *mm; + + mm = calloc(sizeof(struct menu_manager), 1); + if (!mm) + return NULL; + + mm->stack = g_queue_new(); + mm->title_stack = g_queue_new(); + mm->menu = items; + mm->mainloop = mainloop; + + return mm; +} + +int menu_manager_run(MManager *mm) +{ + _show_menu(mm, mm->menu); + + mm->buf = key_buffer; + + return 0; +} + +int menu_manager_set_user_data(MManager *mm, void *user_data) +{ + if (!mm) + return -1; + + mm->user_data = user_data; + + return 0; +} + +void *menu_manager_ref_user_data(MManager *mm) +{ + if (!mm) + return NULL; + + return mm->user_data; +} + +gboolean on_menu_manager_keyboard(GIOChannel *src, GIOCondition con, + gpointer data) +{ + MManager *mm = data; + char local_buf[MENU_DATA_SIZE + 1] = { 0, }; + + if (fgets(local_buf, MENU_DATA_SIZE, stdin) == NULL) + return TRUE; + + if (strlen(local_buf) > 0) { + if (local_buf[strlen(local_buf) - 1] == '\n') + local_buf[strlen(local_buf) - 1] = '\0'; + } + + if (mm->buf == key_buffer) { + if (strlen(local_buf) < 1) { + _show_prompt(); + return TRUE; + } + + _move_menu(mm, mm->menu, local_buf); + } else { + if (mm->buf) { + memset(mm->buf, 0, MENU_DATA_SIZE); + memcpy(mm->buf, local_buf, MENU_DATA_SIZE); + _show_input_ok(); + } + mm->buf = key_buffer; + _move_menu(mm, mm->menu, (char *)DEFAULT_MENU_MENU); + } + + return TRUE; +} + +pid_t get_tid() +{ + return syscall(__NR_gettid); +} + +void hide_pid() +{ + flag_pid_display = 0; +} + +void show_pid() +{ + flag_pid_display = 1; +} + +int is_pid_show() +{ + return flag_pid_display; +} + +static void _hex_dump(const char *pad, int size, const void *data) +{ + char buf[255] = {0, }; + char hex[4] = {0, }; + int i; + unsigned char *p; + + if (size <= 0) { + msg("%sno data", pad); + return; + } + p = (unsigned char *)data; + + snprintf(buf, 255, "%s%04X: ", pad, 0); + for (i = 0; i < size; i++) { + snprintf(hex, 4, "%02X ", p[i]); + strncat(buf, hex, strlen(hex)); + + if ((i + 1) % 8 == 0) { + if ((i + 1) % 16 == 0) { + msg("%s", buf); + memset(buf, 0, 255); + snprintf(buf, 255, "%s%04X: ", pad, i + 1); + } else { + strncat(buf, TAB_SPACE, strlen(TAB_SPACE)); + } + } + } + + msg("%s", buf); +} + +void menu_print_dump(int data_len, void *data) +{ + if (!data) + return; + + msg(""); + msg(" \tlen=%d", data_len); + _hex_dump(" ", data_len, data); + + msg(""); +} diff --git a/test_app/menu.h b/test_app/menu.h new file mode 100755 index 0000000..74e9685 --- /dev/null +++ b/test_app/menu.h @@ -0,0 +1,108 @@ +/* + * + * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jooseok Park <jooseok.park@samsung.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __MENU_H__ +#define __MENU_H__ + +#define ANSI_COLOR_NORMAL "\e[0m" + +#define ANSI_COLOR_BLACK "\e[0;30m" +#define ANSI_COLOR_RED "\e[0;31m" +#define ANSI_COLOR_GREEN "\e[0;32m" +#define ANSI_COLOR_BROWN "\e[0;33m" +#define ANSI_COLOR_BLUE "\e[0;34m" +#define ANSI_COLOR_MAGENTA "\e[0;35m" +#define ANSI_COLOR_CYAN "\e[0;36m" +#define ANSI_COLOR_LIGHTGRAY "\e[0;37m" + +#define ANSI_COLOR_DARKGRAY "\e[1;30m" +#define ANSI_COLOR_LIGHTRED "\e[1;31m" +#define ANSI_COLOR_LIGHTGREEN "\e[1;32m" +#define ANSI_COLOR_YELLOW "\e[1;33m" +#define ANSI_COLOR_LIGHTBLUE "\e[1;34m" +#define ANSI_COLOR_LIGHTMAGENTA "\e[1;35m" +#define ANSI_COLOR_LIGHTCYAN "\e[1;36m" +#define ANSI_COLOR_WHITE "\e[1;37m" + +#define msg(fmt, args...) do { fprintf(stdout, fmt "\n", ##args); \ + fflush(stdout); } while (0) + +#define err(fmt, args...) do { fprintf(stdout, ANSI_COLOR_RED fmt \ + ANSI_COLOR_NORMAL "\n", ##args); fflush(stdout); } while (0) + +#define msgn(fmt, args...) do { fprintf(stdout, fmt, ##args); \ + fflush(stdout); } while (0) + +/* Bold (green) */ +#define msgb(fmt, args...) do { fprintf(stdout, ANSI_COLOR_LIGHTGREEN fmt \ + ANSI_COLOR_NORMAL "\n", ##args); fflush(stdout); } while (0) + +#define msgm(fmt, args...) do { fprintf(stdout, ANSI_COLOR_LIGHTMAGENTA fmt \ + ANSI_COLOR_NORMAL "\n", ##args); fflush(stdout); } while (0) + +#define MENU_DATA_SIZE 255 + +#define INFO msgm +#define DBG msg +#define ERR err + + +/* + * Horizontal Line - width: 65 + * .12345678901234567890123456789012345678901234567890. + */ +#define HR_SINGLE "----------------------------------------" \ + "-------------------------" +#define HR_DOUBLE "========================================" \ + "=========================" +#define HR_SINGLE2 " ---------------------------------------" \ + "------------------------ " + +#define MAX_WIDTH strlen(HR_SINGLE) +#define MAX_TITLE ((MAX_WIDTH) - 10) +#define POS_MORE ((MAX_WIDTH) - 3) +#define RET_SUCCESS 0 +#define RET_FAILURE -1 + +typedef struct menu_manager MManager; + +struct menu_data { + const char *key; + const char *title; + struct menu_data *sub_menu; + int (*callback)(MManager *mm, struct menu_data *menu); + char *data; +}; + +MManager *menu_manager_new(struct menu_data items[], GMainLoop *mainloop); +int menu_manager_run(MManager *mm); +int menu_manager_set_user_data(MManager *mm, void *user_data); +void *menu_manager_ref_user_data(MManager *mm); + +gboolean on_menu_manager_keyboard(GIOChannel *src, GIOCondition con, + gpointer data); + +pid_t get_tid(); +void hide_pid(); +void show_pid(); +int is_pid_show(); +void menu_print_dump(int data_len, void *data); + +#endif + |