From c20d2e486c8b9fa454426055bc486efbd0bf8c97 Mon Sep 17 00:00:00 2001 From: Jihoon Jung Date: Mon, 24 Aug 2020 14:40:14 +0900 Subject: [PATCH 02/10] Initial repository commit Change-Id: If37afed608c3e1d63022dde22f24560f3750eee4 Signed-off-by: Jihoon Jung --- CMakeLists.txt | 84 ++++ LICENSE.APLv2 | 204 +++++++++ NOTICE | 3 + README.md | 2 + capi-network-uwb.pc.in | 15 + include/uwb.h | 88 ++++ include/uwb_log.h | 110 +++++ include/uwb_private.h | 73 +++ include/uwb_util.h | 34 ++ packaging/capi-network-uwb.manifest | 5 + packaging/capi-network-uwb.spec | 107 +++++ src/gen.sh | 4 + src/uwb-gdbuslib.xml | 55 +++ src/uwb.c | 885 ++++++++++++++++++++++++++++++++++++ src/uwb_util.c | 73 +++ 15 files changed, 1742 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 LICENSE.APLv2 create mode 100644 NOTICE create mode 100644 README.md create mode 100644 capi-network-uwb.pc.in create mode 100755 include/uwb.h create mode 100755 include/uwb_log.h create mode 100755 include/uwb_private.h create mode 100755 include/uwb_util.h create mode 100644 packaging/capi-network-uwb.manifest create mode 100644 packaging/capi-network-uwb.spec create mode 100755 src/gen.sh create mode 100755 src/uwb-gdbuslib.xml create mode 100755 src/uwb.c create mode 100755 src/uwb_util.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..4425f65 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,84 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +SET(CAPI_UWB "capi-network-uwb") + +PROJECT(${CAPI_UWB} C CXX) + +MESSAGE(" - Defining...configuration variables") +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) + +SET(LIB ${LIB_PATH}) +SET(LIBDIR ${PREFIX}/${LIB_PATH}) + +SET(INC_DIR include) +INCLUDE_DIRECTORIES(${INC_DIR}) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/src) + +MESSAGE(" - Checking...packages dependency") +SET(COMMON_DEPS dlog gio-2.0 glib-2.0 gio-unix-2.0 capi-system-info) +SET(PC_DEPS "") + +MESSAGE(" - Making...build configuration") +INCLUDE(FindPkgConfig) +pkg_check_modules(${CAPI_UWB} REQUIRED ${COMMON_DEPS}) +FOREACH(flag ${${CAPI_UWB}_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) +SET(DEFAULT_CFLAGS "${EXTRA_CFLAGS} -Wall -fPIE -fPIC -Werror -g -fvisibility=hidden") +IF(BUILD_GCOV) + SET(GCOV_C_FLAGS "-fprofile-arcs -ftest-coverage") +ENDIF(BUILD_GCOV) +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${DEFAULT_CFLAGS} ${GCOV_C_FLAGS}") +SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") + +IF("${ARCH}" STREQUAL "arm") + ADD_DEFINITIONS("-DTARGET") +ENDIF("${ARCH}" STREQUAL "arm") + +ADD_DEFINITIONS("-DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"") +ADD_DEFINITIONS("-DTIZEN_DEBUG") + +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie -Wl,--rpath=${LIBDIR}") + +MESSAGE(" - Generating...d-bus code") +FIND_PROGRAM(GDBUS_CODEGEN NAMES gdbus-codegen) +EXEC_PROGRAM(${GDBUS_CODEGEN} ARGS + " \\ + --generate-c-code ${CMAKE_CURRENT_SOURCE_DIR}/src/uwb_gdbus \\ + --interface-prefix org.tizen.uwb. \\ + ${CMAKE_CURRENT_SOURCE_DIR}/src/uwb-gdbuslib.xml \\ + ") + +MESSAGE(" - Building...shared library") +SET(SOURCES src/uwb.c + src/uwb_gdbus.c + src/uwb_util.c) + +ADD_LIBRARY(${CAPI_UWB} SHARED ${SOURCES}) +TARGET_LINK_LIBRARIES(${CAPI_UWB} ${${CAPI_UWB}_LDFLAGS} pthread) + +SET_TARGET_PROPERTIES(${CAPI_UWB} + PROPERTIES + VERSION ${FULLVER} + SOVERSION ${MAJORVER} + CLEAN_DIRECT_OUTPUT 1 +) + +MESSAGE(" - Installing...the library and header files") +INSTALL(TARGETS ${CAPI_UWB} DESTINATION ${LIB}) +INSTALL( + DIRECTORY include/ DESTINATION include + FILES_MATCHING + PATTERN "include/*.h" + ) + +SET(PC_NAME ${CAPI_UWB}) +SET(PC_REQUIRED ${PC_DEPS}) +SET(PC_LDFLAGS -l${CAPI_UWB}) + +CONFIGURE_FILE( + ${CAPI_UWB}.pc.in + ${CMAKE_CURRENT_SOURCE_DIR}/${CAPI_UWB}.pc + @ONLY +) + +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${CAPI_UWB}.pc DESTINATION ${LIB}/pkgconfig) diff --git a/LICENSE.APLv2 b/LICENSE.APLv2 new file mode 100644 index 0000000..9bbfde4 --- /dev/null +++ b/LICENSE.APLv2 @@ -0,0 +1,204 @@ +Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved. + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. + diff --git a/NOTICE b/NOTICE new file mode 100644 index 0000000..ccdad52 --- /dev/null +++ b/NOTICE @@ -0,0 +1,3 @@ +Copyright (c) Samsung Electronics Co., Ltd. All rights reserved. +Except as noted, this software is licensed under Apache License, Version 2. +Please, see the LICENSE file for Apache License terms and conditions. diff --git a/README.md b/README.md new file mode 100644 index 0000000..2aa9bd6 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# uwb +The api for uwb functions diff --git a/capi-network-uwb.pc.in b/capi-network-uwb.pc.in new file mode 100644 index 0000000..c45e5b2 --- /dev/null +++ b/capi-network-uwb.pc.in @@ -0,0 +1,15 @@ + +# Package Information for pkg-config + +prefix=@PREFIX@ +exec_prefix=/usr +libdir=@libdir@ +includedir=/usr/include/ + +Name: @PC_NAME@ +Description: @PACKAGE_DESCRIPTION@ +Version: @VERSION@ +Requires: @PC_REQUIRED@ +Libs: -L${libdir} @PC_LDFLAGS@ +Cflags: -I${includedir} + diff --git a/include/uwb.h b/include/uwb.h new file mode 100755 index 0000000..6619057 --- /dev/null +++ b/include/uwb.h @@ -0,0 +1,88 @@ +/* + * Network Configuration Module + * + * Copyright (c) 2020 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. + * + */ +#ifndef __TIZEN_CAPI_NETWORK_UWB_H__ +#define __TIZEN_CAPI_NETWORK_UWB_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef TIZEN_ERROR_UWB +#define TIZEN_ERROR_UWB -0x02F50000 /**< Base error code */ +#endif + +typedef enum { + UWB_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */ + UWB_ERROR_IO_ERROR = TIZEN_ERROR_IO_ERROR, /**< I/O error */ + UWB_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */ + UWB_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */ + UWB_ERROR_PERMISSION_DENIED = TIZEN_ERROR_PERMISSION_DENIED, /**< Permission denied */ + UWB_ERROR_NOT_SUPPORTED = TIZEN_ERROR_NOT_SUPPORTED, /**< Not supported */ + UWB_ERROR_OPERATION_FAILED = TIZEN_ERROR_UWB | 0x01 +} uwb_error_e; + +typedef void *uwb_network_h; /**< Device handle */ +typedef void *uwb_node_h; +typedef void (*uwb_message_received_cb)(uint64_t node_id, const unsigned char *message, + int message_length, void *user_data); +typedef void (*uwb_position_changed_cb)(uint64_t node_id, int x, int y, int z, + void *user_data); +typedef void (*uwb_network_get_finished_cb)(int result, uwb_network_h *network, void *user_data); +typedef bool (*uwb_network_foreach_remote_node_cb)(uwb_node_h remote_node, void *user_data); + +int uwb_initialize(); +int uwb_deinitialize(); +int uwb_reset(); +int uwb_factory_reset(); +int uwb_set_message_received_cb(uwb_message_received_cb message_received_cb, void *user_data); +int uwb_set_position_changed_cb(uwb_position_changed_cb position_changed_cb, void *user_data); +int uwb_unset_message_received_cb(); +int uwb_unset_position_changed_cb(); +int uwb_get_own_node(uwb_node_h *own_node); +int uwb_network_get(uwb_network_get_finished_cb finished_cb, void *user_data); +int uwb_network_clone(uwb_network_h source, uwb_network_h *target); +int uwb_network_destroy(uwb_network_h network); +int uwb_network_get_pan_id(uwb_network_h network, uint64_t *pan_id); +int uwb_network_get_remote_node_count(uwb_network_h network, int *remote_node_count); +int uwb_network_foreach_remote_node(uwb_network_h network, uwb_network_foreach_remote_node_cb node_cb, void *user_data); +int uwb_node_clone(uwb_node_h source, uwb_node_h *target); +int uwb_node_destroy(uwb_node_h node); +int uwb_node_get_distance(uwb_node_h node, uint64_t *distance); +int uwb_node_get_node_id(uwb_node_h node, uint64_t *node_id); +int uwb_node_get_pan_id(uwb_node_h node, uint64_t *pan_id); +int uwb_node_get_is_remote(uwb_node_h node, bool *is_remote); +int uwb_node_get_position(uwb_node_h node, int *x, int *y, int *z); +int uwb_node_set_position(uwb_node_h node, int x, int y, int z); +int uwb_node_send_message(const unsigned char *message, int len); +int uwb_node_send_message_to(uwb_node_h node, const unsigned char *message, int len); +int uwb_node_get_configuration_int32(uwb_node_h node, char *key, int32_t *value); +int uwb_node_set_configuration_int32(uwb_node_h node, char *key, int32_t value); +int uwb_node_get_configuration_int64(uwb_node_h node, char *key, int64_t *value); +int uwb_node_set_configuration_int64(uwb_node_h node, char *key, int64_t value); +int uwb_node_get_configuration_string(uwb_node_h node, char *key, const char **value); +int uwb_node_set_configuration_string(uwb_node_h node, char *key, const char *value); + +#ifdef __cplusplus +} +#endif + +#endif /* __TIZEN_CAPI_NETWORK_UWB_H__ */ diff --git a/include/uwb_log.h b/include/uwb_log.h new file mode 100755 index 0000000..f20a398 --- /dev/null +++ b/include/uwb_log.h @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2020 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. + * + */ + +#ifndef __UWB_LOG_H__ +#define __UWB_LOG_H__ + +#include + +#define NOTUSED(var) (var = var) + +#define COLOR_BLACK "\033[0;30m" +#define COLOR_RED "\033[0;31m" +#define COLOR_GREEN "\033[0;32m" +#define COLOR_BROWN "\033[0;33m" +#define COLOR_BLUE "\033[0;34m" +#define COLOR_PURPLE "\033[0;35m" +#define COLOR_CYAN "\033[0;36m" +#define COLOR_GRAY "\033[0;37m" +#define COLOR_END "\033[0;m" + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "UWB_CAPI" + +#define _ERR(fmt, ...) \ + do { \ + LOGE(COLOR_RED fmt COLOR_END, ##__VA_ARGS__); \ + } while (0) + +#define _INFO(fmt, ...) \ + do { \ + LOGI(COLOR_GREEN fmt COLOR_END, ##__VA_ARGS__); \ + } while (0) + +#define _WARN(fmt, ...) \ + do { \ + LOGI(COLOR_BROWN fmt COLOR_END, ##__VA_ARGS__); \ + } while (0) + +#define _DBG(fmt, ...) \ + do { \ + LOGD(fmt, ##__VA_ARGS__); \ + } while (0) + +#define _BEGIN() \ + do { \ + LOGD(COLOR_BLUE "BEGIN >>>>" COLOR_END); \ + } while (0) + +#define _END() \ + do { \ + LOGD(COLOR_BLUE "END <<<<" COLOR_END); \ + } while (0) + +#define cond_expr_ret(expr, val) \ + do { \ + if (expr) { \ + _ERR("[precond fail] expr : %s, ret : %d\n", #expr, val); \ + return (val); \ + } \ + } while (0) + +#define cond_ret(val) \ + do { \ + if (val) { \ + _ERR("[precond fail] ret : %d\n", val); \ + return (val); \ + } \ + } while (0) + +#define uwb_check_null_ret_error(name, value, error) \ + do { \ + /* LCOV_EXCL_START */ \ + if (G_UNLIKELY(NULL == (value))) { \ + LOGE("%s is NULL", name); \ + return error; \ + } \ + /* LCOV_EXCL_STOP */ \ + } while (FALSE) + +#define uwb_check_null_ret(name, value) \ + do { \ + /* LCOV_EXCL_START */ \ + if (G_UNLIKELY(NULL == (value))) { \ + LOGE("%s is NULL", name); \ + return; \ + } \ + /* LCOV_EXCL_STOP */ \ + } while (FALSE) + +#define PRT(format, args...) printf("%s:%d() " format, __FUNCTION__, __LINE__, ##args) +#define TC_PRT(format, args...) PRT(format "\n", ##args) + +#endif /* __UWB_LOG_H__ */ diff --git a/include/uwb_private.h b/include/uwb_private.h new file mode 100755 index 0000000..d0e8d2b --- /dev/null +++ b/include/uwb_private.h @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2020 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. + * + */ + +#ifndef __UWB_PRIVATE_H__ +#define __UWB_PRIVATE_H__ + +#include +#include +#include +#include +#include +#include + +#define UWB_FEATURE "http://tizen.org/feature/network.uwb" + +#define CHECK_INPUT_PARAMETER(arg) \ + if (arg == NULL) { \ + _WARN("INVALID_PARAMETER"); \ + return UWB_ERROR_INVALID_PARAMETER; \ + } + +#if 0 +#define CHECK_FEATURE_SUPPORTED(feature_name) \ + { \ + bool uwb_supported = FALSE; \ + if (!system_info_get_platform_bool(feature_name, &uwb_supported)) { \ + if (uwb_supported == FALSE) { \ + _WARN("UWB Manager feature is disabled"); \ + return UWB_ERROR_NOT_SUPPORTED; \ + } \ + } else { \ + _ERR("Error - Feature getting from System Info"); \ + return UWB_ERROR_NOT_SUPPORTED; \ + } \ + } +#else +#define CHECK_FEATURE_SUPPORTED(feature_name) \ + { \ + _WARN("[Feature] Should be check !"); \ + } +#endif + +typedef struct { + uint64_t node_id; + uint16_t pan_id; + bool is_remote; + uint64_t distance; + int x; + int y; + int z; +} uwb_node_s; + +typedef struct { + uint16_t pan_id; + GSList *remote_node_list; + int remote_node_count; +} uwb_network_s; + +#endif /* __UWB_PRIVATE_H__ */ diff --git a/include/uwb_util.h b/include/uwb_util.h new file mode 100755 index 0000000..de78287 --- /dev/null +++ b/include/uwb_util.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2020 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. + * + */ + +#ifndef __UWB_UTIL_H__ +#define __UWB_UTIL_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +uwb_node_s *uwb_get_node_from_variant(GVariant *va); +void uwb_network_clean(uwb_network_s *network); + +#ifdef __cplusplus +} +#endif + +#endif /* __UWB_UTIL_H__ */ diff --git a/packaging/capi-network-uwb.manifest b/packaging/capi-network-uwb.manifest new file mode 100644 index 0000000..a76fdba --- /dev/null +++ b/packaging/capi-network-uwb.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/packaging/capi-network-uwb.spec b/packaging/capi-network-uwb.spec new file mode 100644 index 0000000..55149ca --- /dev/null +++ b/packaging/capi-network-uwb.spec @@ -0,0 +1,107 @@ +Name: capi-network-uwb +Summary: UWB CAPI +Version: 0.1.1 +Release: 0 +Group: Network & Connectivity/Other +License: Apache-2.0 +Source0: %{name}-%{version}.tar.gz +Source1: %{name}.manifest + +BuildRequires: cmake +BuildRequires: pkgconfig(dlog) +BuildRequires: pkgconfig(gio-2.0) +BuildRequires: pkgconfig(glib-2.0) +BuildRequires: pkgconfig(gio-unix-2.0) +BuildRequires: pkgconfig(capi-system-info) + +Requires(post): /sbin/ldconfig +Requires(postun): /sbin/ldconfig + +%description +UWB API library +Group: Network & Connectivity/Other +Requires: %{name} = %{version}-%{release} + +%package devel +Summary: UWB API library (development library) +Group: Network & Connectivity/Other +Requires: %{name} = %{version}-%{release} + +%description devel +This package contains the development files for %{name} API library. + +%if 0%{?gcov:1} +%package gcov +Summary: A UWB gcov Tool +Group: Network & Connectivity/Development + +%description gcov +UWB gcov objects +%endif + +%prep +%setup #-q +chmod g-w %_sourcedir/* +cp %{SOURCE1} ./%{name}.manifest + +%build +CFLAGS=$(echo $CFLAGS | sed 's/-O2/-O0/' | sed 's/-O1/-O0/' | sed 's/-Wp,-D_FORTIFY_SOURCE=2//') +CXXFLAGS=$(echo $CXXFLAGS | sed 's/-O2/-O0/' | sed 's/-O1/-O0/' | sed 's/-Wp,-D_FORTIFY_SOURCE=2//') + +%if 0%{?gcov:1} +export LDFLAGS+=" -lgcov" +%endif + +MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` + +%define NETWORK_FW_DATADIR %{TZ_SYS_GLOBALUSER_DATA}/network +%define DBDIR %{TZ_SYS_GLOBALUSER_DATA}/ + +cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} \ + -DLIB_INSTALL_DIR=%{_libdir} \ + -DBIN_INSTALL_DIR=%{_bindir} \ + -DINCLUDE_INSTALL_DIR=%{_includedir} \ + -DLIB_PATH=%{_lib} \ + -DFULLVER=%{version} \ + -DMAJORVER=${MAJORVER} \ + -DNETWORK_FW_DATADIR=%{NETWORK_FW_DATADIR} \ + -DDBDIR=%{DBDIR} \ + -DBUILD_GCOV=%{?gcov:1}%{!?gcov:0} + +make %{?jobs:-j%jobs} + +%if 0%{?gcov:1} +mkdir -p gcov-obj +find . -name '*.gcno' -exec cp '{}' gcov-obj ';' +%endif + +%install +rm -rf %{buildroot}/BUILD/%{name} +%make_install + +%if 0%{?gcov:1} +mkdir -p %{buildroot}%{_datadir}/gcov/obj +install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj +%endif + +%post -p /sbin/ldconfig + +%postun -p /sbin/ldconfig + + +%files +%manifest %{name}.manifest +%defattr(-,root,root,-) +%license LICENSE.APLv2 +%attr(644,-,-) %{_libdir}/lib%{name}.so.* + +%files devel +%defattr(-,root,root,-) +%attr(644,-,-) %{_libdir}/lib%{name}.so +%{_libdir}/pkgconfig/%{name}.pc +%{_includedir}/*.h + +%if 0%{?gcov:1} +%files gcov +%{_datadir}/gcov/obj/* +%endif diff --git a/src/gen.sh b/src/gen.sh new file mode 100755 index 0000000..c41531f --- /dev/null +++ b/src/gen.sh @@ -0,0 +1,4 @@ +gdbus-codegen --interface-prefix org.tizen.uwb \ + --generate-c-code uwb_gdbus \ + --c-generate-object-manager \ + uwb-gdbuslib.xml \ diff --git a/src/uwb-gdbuslib.xml b/src/uwb-gdbuslib.xml new file mode 100755 index 0000000..4cba98f --- /dev/null +++ b/src/uwb-gdbuslib.xml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/uwb.c b/src/uwb.c new file mode 100755 index 0000000..e9afa22 --- /dev/null +++ b/src/uwb.c @@ -0,0 +1,885 @@ +/* + * Copyright (c) 2020 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. + * + */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#define UWB_DBUS_SERVICE "org.tizen.uwb" /**< For uwb dbus */ +#define UWB_DBUS_MANAGER_PATH "/org/tizen/uwb/manager" /**< For manager dbus */ + + +static struct _uwb_ctx { + Manager *manager_proxy; + uwb_message_received_cb message_received_cb; + void *message_received_user_data; + uwb_position_changed_cb position_changed_cb; + void *position_changed_user_data; + uwb_network_get_finished_cb network_get_finished_cb; + uwb_network_foreach_remote_node_cb foreach_remote_node_cb; +} uwb_ctx; + +static inline void __handle_error(GError *error, int *ret) +{ + if (!error) + return; + + if (NULL == g_strrstr(error->message, "org.tizen.uwb.Error")) { + if (NULL != g_strrstr(error->message, ".AccessDenied")) { + _DBG("Client doesn't have nmdaemon privilege"); + *ret = UWB_ERROR_PERMISSION_DENIED; + } else { + _DBG("DBus failure"); + *ret = UWB_ERROR_OPERATION_FAILED; + } + } else { + if (NULL != g_strrstr(error->message, ".NotPermitted")) { + _DBG("Client doesn't have uwb daemon privilege"); + *ret = UWB_ERROR_PERMISSION_DENIED; + } else if (NULL != g_strrstr(error->message, ".InvalidParameter")) { + _DBG("Invalid Parameter"); + *ret = UWB_ERROR_INVALID_PARAMETER; + } else if (NULL != g_strrstr(error->message, ".OutOfMemory")) { + _DBG("Out of memory"); + *ret = UWB_ERROR_OUT_OF_MEMORY; + } else { + _DBG("Operation Failed"); + *ret = UWB_ERROR_OPERATION_FAILED; + } + } + g_error_free(error); +} + +static void __message_received(GObject *source_object, + guint16 node_id, GVariant *message, gint message_length) +{ + GVariantIter *iter; + unsigned char element; + unsigned char *buf = NULL; + int size = 0; + + g_variant_get(message, "a(y)", &iter); + size = g_variant_iter_n_children(iter); + + buf = (unsigned char *)malloc(sizeof(unsigned char) * size + 1); + for (int i = 0; g_variant_iter_loop(iter, "(y)", &element); i++) + { + buf[i] = element; + } + buf[size] = '\0'; + + g_variant_iter_free(iter); + + if (uwb_ctx.message_received_cb != NULL) { + uwb_ctx.message_received_cb(node_id, buf, message_length, + uwb_ctx.message_received_user_data); + } + + free(buf); +} + +static void __position_changed(GObject *source_object, + guint16 node_id, gint x, gint y, gint z) +{ + if (uwb_ctx.position_changed_cb != NULL) + uwb_ctx.position_changed_cb(node_id, x, y, z, uwb_ctx.position_changed_user_data); +} + +static int manager_proxy_init() +{ + GError *error = NULL; + + uwb_ctx.manager_proxy = manager_proxy_new_for_bus_sync( + G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + UWB_DBUS_SERVICE, + UWB_DBUS_MANAGER_PATH, + NULL, + &error); + if (NULL == uwb_ctx.manager_proxy) { + /* LCOV_EXCL_START */ + if (error != NULL) { + _ERR("Failed to connect to the D-BUS daemon [%s]", error->message); + g_error_free(error); + } + return UWB_ERROR_IO_ERROR; + /* LCOV_EXCL_STOP */ + } + + g_signal_connect(uwb_ctx.manager_proxy, "message-received", + G_CALLBACK(__message_received), NULL); + + g_signal_connect(uwb_ctx.manager_proxy, "position-changed", + G_CALLBACK(__position_changed), NULL); + + return UWB_ERROR_NONE; +} + +static void manager_proxy_deinit() +{ + g_object_unref(uwb_ctx.manager_proxy); +} + +EXPORT_API int uwb_initialize() +{ + int ret = UWB_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + ret = manager_proxy_init(); + + _END(); + + return ret; +} + +EXPORT_API int uwb_deinitialize() +{ + int ret = UWB_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + manager_proxy_deinit(); + + _END(); + + return ret; +} + +EXPORT_API int uwb_reset() +{ + int ret = UWB_ERROR_NONE; + GError *error = NULL; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + if (manager_call_reset_sync(uwb_ctx.manager_proxy, NULL, &error) == FALSE) { + _ERR("manager_call_reset_sync failed : %s", error->message); + __handle_error(error, &ret); + } + + _END(); + + return ret; +} + +EXPORT_API int uwb_factory_reset() +{ + int ret = UWB_ERROR_NONE; + GError *error = NULL; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + if (manager_call_factory_reset_sync(uwb_ctx.manager_proxy, NULL, &error) == FALSE) { + _ERR("manager_call_factory_reset_sync failed : %s", error->message); + __handle_error(error, &ret); + } + + _END(); + + return ret; +} + +EXPORT_API int uwb_set_message_received_cb(uwb_message_received_cb message_received_cb, void *user_data) +{ + int ret = UWB_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_ctx.message_received_cb = message_received_cb; + uwb_ctx.message_received_user_data = user_data; + + _END(); + + return ret; +} + +EXPORT_API int uwb_set_position_changed_cb(uwb_position_changed_cb position_changed_cb, void *user_data) +{ + int ret = UWB_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_ctx.position_changed_cb = position_changed_cb; + uwb_ctx.position_changed_user_data = user_data; + + _END(); + + return ret; +} + +EXPORT_API int uwb_unset_message_received_cb() +{ + int ret = UWB_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_ctx.message_received_cb = NULL; + uwb_ctx.message_received_user_data = NULL; + + _END(); + + return ret; +} + +EXPORT_API int uwb_unset_position_changed_cb() +{ + int ret = UWB_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_ctx.position_changed_cb = NULL; + uwb_ctx.position_changed_user_data = NULL; + + _END(); + + return ret; +} + +EXPORT_API int uwb_get_own_node(uwb_node_h *own_node) +{ + int ret = UWB_ERROR_NONE; + GError *error = NULL; + GVariant *own_node_va = NULL; + uwb_node_s **_own_node = (uwb_node_s **)own_node; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("own_node", own_node, UWB_ERROR_INVALID_PARAMETER); + + if (manager_call_get_own_node_sync(uwb_ctx.manager_proxy, + &own_node_va, NULL, &error) == FALSE) { + __handle_error(error, &ret); + } + + if (ret == UWB_ERROR_NONE && own_node_va != NULL) + *_own_node = uwb_get_node_from_variant(own_node_va); + else + *_own_node = NULL; + + _END(); + + return ret; +} + +static void __network_get_cb(GObject *source_object, GAsyncResult *res, gpointer user_data) +{ + int ret = UWB_ERROR_NONE; + uint16_t pan_id = 0; + GVariant *remote_node_list_va = NULL; + GError *error = NULL; + uwb_network_s *network_s; + + if (manager_call_get_network_info_finish( + MANAGER(source_object), + &pan_id, + &remote_node_list_va, + res, + &error) == FALSE) { + _ERR("manager_call_get_network_info_finish failed : %s", error->message); + __handle_error(error, &ret); + + if (uwb_ctx.network_get_finished_cb != NULL) { + uwb_ctx.network_get_finished_cb(ret, NULL, user_data); + } + + return; + } + + /* create network handle */ + network_s = (uwb_network_s *)malloc(sizeof(uwb_network_s)); + + if (ret == UWB_ERROR_NONE) { + GSList *remote_node_list = NULL; + network_s->pan_id = pan_id; + network_s->remote_node_count = g_variant_n_children(remote_node_list_va); + + if (network_s->remote_node_count > 0) { + GVariantIter *iter = NULL; + GVariant *nodes_va = NULL; + + g_variant_get(remote_node_list_va, "aa{sv}", &iter); + while ((nodes_va = g_variant_iter_next_value(iter)) != NULL) { + uwb_node_s *node; + + node = uwb_get_node_from_variant(nodes_va); + node->is_remote = true; + + _DBG("(%d, %d, %d)", node->x, node->y, node->z); + remote_node_list = g_slist_append(remote_node_list, node); + } + g_variant_iter_free(iter); + } + network_s->remote_node_list = remote_node_list; + + g_variant_unref(remote_node_list_va); + } + + if (uwb_ctx.network_get_finished_cb != NULL) { + uwb_ctx.network_get_finished_cb(ret, (uwb_network_h)network_s, user_data); + } +} + +EXPORT_API int uwb_network_get(uwb_network_get_finished_cb finished_cb, void *user_data) +{ + int ret = UWB_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("finished_cb", finished_cb, UWB_ERROR_INVALID_PARAMETER); + + uwb_ctx.network_get_finished_cb = finished_cb; + + manager_call_get_network_info(uwb_ctx.manager_proxy, NULL, __network_get_cb, user_data); + + _END(); + + return ret; +} + +EXPORT_API int uwb_network_clone(uwb_network_h source, uwb_network_h *target) +{ + int ret = UWB_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + _END(); + + return ret; +} + +EXPORT_API int uwb_network_destroy(uwb_network_h network) +{ + int ret = UWB_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("network", network, UWB_ERROR_INVALID_PARAMETER); + + uwb_network_clean(network); + + _END(); + + return ret; +} + +EXPORT_API int uwb_network_get_pan_id(uwb_network_h network, uint64_t *pan_id) +{ + int ret = UWB_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("network", network, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("pan_id", pan_id, UWB_ERROR_INVALID_PARAMETER); + + *pan_id = ((uwb_network_s *)network)->pan_id; + + _END(); + + return ret; +} + +EXPORT_API int uwb_network_get_remote_node_count(uwb_network_h network, int *remote_node_count) +{ + int ret = UWB_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("network", network, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("remote_node_count", remote_node_count, UWB_ERROR_INVALID_PARAMETER); + + *remote_node_count = ((uwb_network_s *)network)->remote_node_count; + + _END(); + + return ret; +} + +static void __foreach_node(gpointer data, gpointer user_data) +{ + uwb_node_s *node = (uwb_node_s *)data; + _DBG("panid : 0x%X, nodeid : 0x%04llX, distance : %lld", node->pan_id, node->node_id, node->distance); + + if (uwb_ctx.foreach_remote_node_cb != NULL) + uwb_ctx.foreach_remote_node_cb((uwb_node_h)node, user_data); +} + +EXPORT_API int uwb_network_foreach_remote_node(uwb_network_h network, + uwb_network_foreach_remote_node_cb node_cb, void *user_data) +{ + int ret = UWB_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("network", network, UWB_ERROR_INVALID_PARAMETER); + + uwb_network_s *network_s = (uwb_network_s *)network; + if (network_s->remote_node_count == 0) { + _DBG("remote_node_count is 0"); + return ret; + } + + uwb_ctx.foreach_remote_node_cb = node_cb; + g_slist_foreach(network_s->remote_node_list, __foreach_node, user_data); + + _END(); + + return ret; +} + +EXPORT_API int uwb_node_clone(uwb_node_h src, uwb_node_h *dst) +{ + int ret = UWB_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("source", src, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("target", dst, UWB_ERROR_INVALID_PARAMETER); + + uwb_node_s *_src = (uwb_node_s *)src; + *dst = (uwb_node_s *)malloc(sizeof(uwb_node_s)); + uwb_node_s *_dst = *dst; + + _dst->node_id = _src->node_id; + _dst->pan_id = _src->pan_id; + _dst->is_remote = _src->is_remote; + _dst->distance = _src->distance; + _dst->x = _src->x; + _dst->y = _src->y; + _dst->z = _src->z; + + _END(); + + return ret; +} + +EXPORT_API int uwb_node_destroy(uwb_node_h node) +{ + int ret = UWB_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + + free(node); + + _END(); + + return ret; +} + +EXPORT_API int uwb_node_get_distance(uwb_node_h node, uint64_t *distance) +{ + int ret = UWB_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("distance", distance, UWB_ERROR_INVALID_PARAMETER); + + *distance = ((uwb_node_s *)node)->distance; + + _END(); + + return ret; +} + +EXPORT_API int uwb_node_get_node_id(uwb_node_h node, uint64_t *node_id) +{ + int ret = UWB_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("node_id", node_id, UWB_ERROR_INVALID_PARAMETER); + + *node_id = ((uwb_node_s *)node)->node_id; + + _END(); + + return ret; +} + +EXPORT_API int uwb_node_get_pan_id(uwb_node_h node, uint64_t *pan_id) +{ + int ret = UWB_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("pan_id", pan_id, UWB_ERROR_INVALID_PARAMETER); + + *pan_id = ((uwb_node_s *)node)->pan_id; + + _END(); + + return ret; +} + +EXPORT_API int uwb_node_get_is_remote(uwb_node_h node, bool *is_remote) +{ + int ret = UWB_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("is_remote", is_remote, UWB_ERROR_INVALID_PARAMETER); + + *is_remote = ((uwb_node_s *)node)->is_remote; + + _END(); + + return ret; +} + +EXPORT_API int uwb_node_get_position(uwb_node_h node, int *x, int *y, int *z) +{ + int ret = UWB_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("x", x, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("y", y, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("z", z, UWB_ERROR_INVALID_PARAMETER); + + *x = ((uwb_node_s *)node)->x; + *y = ((uwb_node_s *)node)->y; + *z = ((uwb_node_s *)node)->z; + + _END(); + + return ret; +} + +EXPORT_API int uwb_node_set_position(uwb_node_h node, int x, int y, int z) +{ + int ret = UWB_ERROR_NONE; + GError *error = NULL; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + + if (manager_call_set_position_sync(uwb_ctx.manager_proxy, + ((uwb_node_s *)node)->node_id, x, y, z, NULL, &error) == FALSE) { + _ERR("manager_call_set_position_sync failed : %s", error->message); + __handle_error(error, &ret); + } + + _END(); + + return ret; +} + +static GVariant *__data_to_variant(const unsigned char *data, int length) +{ + GVariantBuilder builder; + + g_variant_builder_init(&builder, G_VARIANT_TYPE("a(y)")); + + if (data && length > 0) + { + for(int i = 0; i < length; i++) + g_variant_builder_add(&builder, "(y)", *(data + i)); + } + + return g_variant_builder_end(&builder); +} + +EXPORT_API int uwb_node_send_message(const unsigned char *message, int len) +{ + int ret = UWB_ERROR_NONE; + GVariant *msg = NULL; + GError *error = NULL; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("message", message, UWB_ERROR_INVALID_PARAMETER); + cond_expr_ret(len <= 0, UWB_ERROR_INVALID_PARAMETER); + + msg = __data_to_variant(message, len); + + if (manager_call_send_message_sync(uwb_ctx.manager_proxy, + msg, len, NULL, &error) == FALSE) { + _ERR("manager_call_send_message_sync failed : %s", error->message); + __handle_error(error, &ret); + } + + _END(); + + return ret; +} + +EXPORT_API int uwb_node_send_message_to(uwb_node_h node, const unsigned char *message, int len) +{ + int ret = UWB_ERROR_NONE; + GVariant *msg = NULL; + GError *error = NULL; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("message", message, UWB_ERROR_INVALID_PARAMETER); + cond_expr_ret(len <= 0, UWB_ERROR_INVALID_PARAMETER); + + msg = __data_to_variant(message, len); + + if (manager_call_send_message_to_sync(uwb_ctx.manager_proxy, ((uwb_node_s *)node)->node_id, + msg, len, NULL, &error) == FALSE) { + _ERR("manager_call_send_message_to_sync failed : %s", error->message); + __handle_error(error, &ret); + } + + _END(); + + return ret; +} + +static int __get_configurations(uwb_node_h node, char *key, + const GVariantType *va_type, GVariant **va_out) +{ + int ret = UWB_ERROR_NONE; + GError *error = NULL; + GVariant *va = NULL; + + if (manager_call_get_configurations_sync(uwb_ctx.manager_proxy, + ((uwb_node_s *)node)->node_id, &va, NULL, &error) == FALSE) { + _ERR("__get_configurations failed : %s", error->message); + __handle_error(error, &ret); + } else { + *va_out = g_variant_lookup_value(va, key, va_type); + } + + return ret; +} + +EXPORT_API int uwb_node_get_configuration_int32(uwb_node_h node, char *key, int32_t *value) +{ + int ret = UWB_ERROR_NONE; + GVariant *value_va = NULL; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + + ret = __get_configurations(node, key, G_VARIANT_TYPE_INT32, &value_va); + + if (value_va != NULL) + *value = g_variant_get_int32(value_va); + else + *value = 0; + + _END(); + + return ret; +} + +EXPORT_API int uwb_node_get_configuration_int64(uwb_node_h node, char *key, int64_t *value) +{ + int ret = UWB_ERROR_NONE; + GVariant *value_va = NULL; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + + ret = __get_configurations(node, key, G_VARIANT_TYPE_INT64, &value_va); + + if (value_va != NULL) + *value = g_variant_get_int64(value_va); + else + *value = 0; + + _END(); + + return ret; +} + +EXPORT_API int uwb_node_get_configuration_string(uwb_node_h node, char *key, const char **value) +{ + int ret = UWB_ERROR_NONE; + GVariant *value_va = NULL; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + + ret = __get_configurations(node, key, G_VARIANT_TYPE_STRING, &value_va); + + if (value_va != NULL) + *value = g_variant_get_string(value_va, NULL); + else + *value = 0; + + _END(); + + return ret; +} + +static int __set_configuration(uwb_node_h node, char *key, GVariant *va) +{ + int ret = UWB_ERROR_NONE; + GError *error = NULL; + + if (manager_call_set_configurations_sync(uwb_ctx.manager_proxy, ((uwb_node_s *)node)->node_id, + va, NULL, &error) == FALSE) { + _ERR("__set_configurations : %s", error->message); + __handle_error(error, &ret); + } + + return ret; +} + +static GVariant *__build_configuration(char *key, GVariant *value) +{ + GVariant *va = NULL; + GVariantBuilder *builder; + + builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); + g_variant_builder_open(builder, G_VARIANT_TYPE_VARDICT); + g_variant_builder_add(builder, "{sv}", key, value); + g_variant_builder_close(builder); + va = g_variant_builder_end(builder); + g_variant_builder_unref(builder); + + return va; +} + +EXPORT_API int uwb_node_set_configuration_int32(uwb_node_h node, char *key, int32_t value) +{ + int ret = UWB_ERROR_NONE; + GVariant *configuration = NULL; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + + configuration = __build_configuration(key, g_variant_new_int32(value)); + + ret = __set_configuration(node, key, configuration); + + _END(); + + return ret; +} + +EXPORT_API int uwb_node_set_configuration_int64(uwb_node_h node, char *key, int64_t value) +{ + int ret = UWB_ERROR_NONE; + GVariant *configuration = NULL; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + + configuration = __build_configuration(key, g_variant_new_int64(value)); + + ret = __set_configuration(node, key, configuration); + + _END(); + + return ret; +} + +EXPORT_API int uwb_node_set_configuration_string(uwb_node_h node, char *key, const char *value) +{ + int ret = UWB_ERROR_NONE; + GVariant *configuration = NULL; + + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + + _BEGIN(); + + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + + configuration = __build_configuration(key, g_variant_new_string(value)); + + ret = __set_configuration(node, key, configuration); + + _END(); + + return ret; +} + diff --git a/src/uwb_util.c b/src/uwb_util.c new file mode 100755 index 0000000..ecaae35 --- /dev/null +++ b/src/uwb_util.c @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2020 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. + * + */ +#include +#include +#include +#include +#include + +void _node_free_func(gpointer data) +{ + uwb_node_s *node = (uwb_node_s *)data; + + free(node); + node = NULL; +} + +void uwb_network_clean(uwb_network_s *network) +{ + if (network == NULL) + return; + + g_slist_free_full(network->remote_node_list, _node_free_func); + + g_free(network); + network = NULL; +} + +uwb_node_s *uwb_get_node_from_variant(GVariant *va) +{ + GVariantIter *iter = NULL; + const gchar *key; + GVariant *key_value = NULL; + + uwb_node_s *node = calloc(1, sizeof(uwb_node_s)); + if (NULL == node) { + _ERR("Memory allocation failed"); + return NULL; + } + + g_variant_get(va, "a{sv}", &iter); + while (g_variant_iter_loop(iter, "{sv}", &key, &key_value)) { + if (g_strcmp0(key, "Distance") == 0) + node->distance = g_variant_get_uint64(key_value); + else if (g_strcmp0(key, "NodeID") == 0) + node->node_id = g_variant_get_uint64(key_value); + else if (g_strcmp0(key, "PanID") == 0) + node->pan_id = g_variant_get_uint16(key_value); + else if (g_strcmp0(key, "X") == 0) + node->x = g_variant_get_int32(key_value); + else if (g_strcmp0(key, "Y") == 0) + node->y = g_variant_get_int32(key_value); + else if (g_strcmp0(key, "Z") == 0) + node->z = g_variant_get_int32(key_value); + } + + g_variant_iter_free(iter); + + return node; +} -- 2.7.4 From 3d1bcab5f60545ce05fbf67acf2cff1579ad7a77 Mon Sep 17 00:00:00 2001 From: Jihoon Jung Date: Mon, 24 Aug 2020 19:17:26 +0900 Subject: [PATCH 03/10] Fix build error for 64bit architectures Change-Id: I090d52e3865e9ae5dd1746bfee2e1d2bb003de30 Signed-off-by: Jihoon Jung --- src/uwb.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/uwb.c b/src/uwb.c index e9afa22..4c46893 100755 --- a/src/uwb.c +++ b/src/uwb.c @@ -25,6 +25,7 @@ #include #include #include +#include #define UWB_DBUS_SERVICE "org.tizen.uwb" /**< For uwb dbus */ #define UWB_DBUS_MANAGER_PATH "/org/tizen/uwb/manager" /**< For manager dbus */ @@ -447,7 +448,7 @@ EXPORT_API int uwb_network_get_remote_node_count(uwb_network_h network, int *rem static void __foreach_node(gpointer data, gpointer user_data) { uwb_node_s *node = (uwb_node_s *)data; - _DBG("panid : 0x%X, nodeid : 0x%04llX, distance : %lld", node->pan_id, node->node_id, node->distance); + _DBG("panid : 0x%X, nodeid : 0x%04"PRIX64", distance : %"PRId64"", node->pan_id, node->node_id, node->distance); if (uwb_ctx.foreach_remote_node_cb != NULL) uwb_ctx.foreach_remote_node_cb((uwb_node_h)node, user_data); -- 2.7.4 From 672e3f6d3296e080ea4da88139ed2b814ecad549 Mon Sep 17 00:00:00 2001 From: Yu Date: Thu, 27 Aug 2020 09:08:43 +0900 Subject: [PATCH 04/10] Rearrange folder Change-Id: I34c301ae4dbff30d25e66e12c9b7ddec91be466d Signed-off-by: Yu jiung --- CMakeLists.txt | 109 +++++++++-------------------- capi-network-uwb.pc.in | 15 ---- include/CMakeLists.txt | 21 ++++++ packaging/capi-network-uwb.spec | 78 +++++++++++++-------- pkgconfig/CMakeLists.txt | 30 ++++++++ pkgconfig/capi-network-uwb.pc.in | 11 +++ src/CMakeLists.txt | 43 ++++++++++++ src/gen.sh | 4 -- include/uwb_log.h => src/uwb-log.h | 0 include/uwb_private.h => src/uwb-private.h | 3 +- src/{uwb_util.c => uwb-util.c} | 4 +- include/uwb_util.h => src/uwb-util.h | 2 - src/uwb.c | 8 +-- 13 files changed, 193 insertions(+), 135 deletions(-) delete mode 100644 capi-network-uwb.pc.in create mode 100644 include/CMakeLists.txt create mode 100644 pkgconfig/CMakeLists.txt create mode 100644 pkgconfig/capi-network-uwb.pc.in create mode 100644 src/CMakeLists.txt delete mode 100755 src/gen.sh rename include/uwb_log.h => src/uwb-log.h (100%) rename include/uwb_private.h => src/uwb-private.h (98%) rename src/{uwb_util.c => uwb-util.c} (97%) rename include/uwb_util.h => src/uwb-util.h (97%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4425f65..516c32f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,84 +1,41 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +# Copyright (c) 2020 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. +# +# @file CMakeLists.txt +# + +############################# Check minimum CMake version ##################### + +CMAKE_MINIMUM_REQUIRED(VERSION 2.8.3) SET(CAPI_UWB "capi-network-uwb") +PROJECT(${CAPI_UWB}) -PROJECT(${CAPI_UWB} C CXX) +############################# cmake packages ################################## -MESSAGE(" - Defining...configuration variables") -SET(PREFIX ${CMAKE_INSTALL_PREFIX}) - -SET(LIB ${LIB_PATH}) -SET(LIBDIR ${PREFIX}/${LIB_PATH}) - -SET(INC_DIR include) -INCLUDE_DIRECTORIES(${INC_DIR}) -INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/src) - -MESSAGE(" - Checking...packages dependency") -SET(COMMON_DEPS dlog gio-2.0 glib-2.0 gio-unix-2.0 capi-system-info) -SET(PC_DEPS "") - -MESSAGE(" - Making...build configuration") INCLUDE(FindPkgConfig) -pkg_check_modules(${CAPI_UWB} REQUIRED ${COMMON_DEPS}) -FOREACH(flag ${${CAPI_UWB}_CFLAGS}) - SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") -ENDFOREACH(flag) -SET(DEFAULT_CFLAGS "${EXTRA_CFLAGS} -Wall -fPIE -fPIC -Werror -g -fvisibility=hidden") -IF(BUILD_GCOV) - SET(GCOV_C_FLAGS "-fprofile-arcs -ftest-coverage") -ENDIF(BUILD_GCOV) -SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${DEFAULT_CFLAGS} ${GCOV_C_FLAGS}") -SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") - -IF("${ARCH}" STREQUAL "arm") - ADD_DEFINITIONS("-DTARGET") -ENDIF("${ARCH}" STREQUAL "arm") - -ADD_DEFINITIONS("-DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"") -ADD_DEFINITIONS("-DTIZEN_DEBUG") - -SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie -Wl,--rpath=${LIBDIR}") - -MESSAGE(" - Generating...d-bus code") -FIND_PROGRAM(GDBUS_CODEGEN NAMES gdbus-codegen) -EXEC_PROGRAM(${GDBUS_CODEGEN} ARGS - " \\ - --generate-c-code ${CMAKE_CURRENT_SOURCE_DIR}/src/uwb_gdbus \\ - --interface-prefix org.tizen.uwb. \\ - ${CMAKE_CURRENT_SOURCE_DIR}/src/uwb-gdbuslib.xml \\ - ") - -MESSAGE(" - Building...shared library") -SET(SOURCES src/uwb.c - src/uwb_gdbus.c - src/uwb_util.c) - -ADD_LIBRARY(${CAPI_UWB} SHARED ${SOURCES}) -TARGET_LINK_LIBRARIES(${CAPI_UWB} ${${CAPI_UWB}_LDFLAGS} pthread) - -SET_TARGET_PROPERTIES(${CAPI_UWB} - PROPERTIES - VERSION ${FULLVER} - SOVERSION ${MAJORVER} - CLEAN_DIRECT_OUTPUT 1 -) -MESSAGE(" - Installing...the library and header files") -INSTALL(TARGETS ${CAPI_UWB} DESTINATION ${LIB}) -INSTALL( - DIRECTORY include/ DESTINATION include - FILES_MATCHING - PATTERN "include/*.h" - ) +SET(COMMON_DEPS "dlog glib-2.0 gio-2.0 gio-unix-2.0 capi-system-info") +SET(PC_DEPS "capi-base-common") +SET(TEST_DEPS "glib-2.0") +SET(GTEST_DEPS "gmock") -SET(PC_NAME ${CAPI_UWB}) -SET(PC_REQUIRED ${PC_DEPS}) -SET(PC_LDFLAGS -l${CAPI_UWB}) +SET(TARGET_UWB "capi-network-uwb") +SET(TARGET_UWB_TEST "capi-network-uwb-test") +SET(TARGET_UWB_GTEST "capi-network-uwb-gtest") -CONFIGURE_FILE( - ${CAPI_UWB}.pc.in - ${CMAKE_CURRENT_SOURCE_DIR}/${CAPI_UWB}.pc - @ONLY -) +ADD_DEFINITIONS("-DUSE_DLOG") -INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${CAPI_UWB}.pc DESTINATION ${LIB}/pkgconfig) +ADD_SUBDIRECTORY(include) +ADD_SUBDIRECTORY(pkgconfig) +ADD_SUBDIRECTORY(src) diff --git a/capi-network-uwb.pc.in b/capi-network-uwb.pc.in deleted file mode 100644 index c45e5b2..0000000 --- a/capi-network-uwb.pc.in +++ /dev/null @@ -1,15 +0,0 @@ - -# Package Information for pkg-config - -prefix=@PREFIX@ -exec_prefix=/usr -libdir=@libdir@ -includedir=/usr/include/ - -Name: @PC_NAME@ -Description: @PACKAGE_DESCRIPTION@ -Version: @VERSION@ -Requires: @PC_REQUIRED@ -Libs: -L${libdir} @PC_LDFLAGS@ -Cflags: -I${includedir} - diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt new file mode 100644 index 0000000..55a2db4 --- /dev/null +++ b/include/CMakeLists.txt @@ -0,0 +1,21 @@ +# Copyright (c) 2020 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. +# +# @file CMakeLists.txt +# + +INSTALL(FILES + ${CMAKE_SOURCE_DIR}/include/uwb.h + DESTINATION ${INCLUDE_DIR} + ) diff --git a/packaging/capi-network-uwb.spec b/packaging/capi-network-uwb.spec index 55149ca..48b5eee 100644 --- a/packaging/capi-network-uwb.spec +++ b/packaging/capi-network-uwb.spec @@ -2,7 +2,7 @@ Name: capi-network-uwb Summary: UWB CAPI Version: 0.1.1 Release: 0 -Group: Network & Connectivity/Other +Group: Network & Connectivity/API License: Apache-2.0 Source0: %{name}-%{version}.tar.gz Source1: %{name}.manifest @@ -17,19 +17,31 @@ BuildRequires: pkgconfig(capi-system-info) Requires(post): /sbin/ldconfig Requires(postun): /sbin/ldconfig +%if 0%{?gcov:1} +BuildRequires: lcov +BuildRequires: tar +%endif +BuildRequires: pkgconfig(gmock) + %description UWB API library -Group: Network & Connectivity/Other -Requires: %{name} = %{version}-%{release} %package devel Summary: UWB API library (development library) -Group: Network & Connectivity/Other +Group: Development/Libraries Requires: %{name} = %{version}-%{release} %description devel This package contains the development files for %{name} API library. +%package tests +Summary: Test Application UWB +Group: Network & Connectivity/Testing +Requires: %{name} = %{version}-%{release} + +%description tests +Test Application for UWB Framework + %if 0%{?gcov:1} %package gcov Summary: A UWB gcov Tool @@ -40,48 +52,50 @@ UWB gcov objects %endif %prep -%setup #-q -chmod g-w %_sourcedir/* +%setup -q +chmod 644 %{SOURCE0} cp %{SOURCE1} ./%{name}.manifest %build -CFLAGS=$(echo $CFLAGS | sed 's/-O2/-O0/' | sed 's/-O1/-O0/' | sed 's/-Wp,-D_FORTIFY_SOURCE=2//') -CXXFLAGS=$(echo $CXXFLAGS | sed 's/-O2/-O0/' | sed 's/-O1/-O0/' | sed 's/-Wp,-D_FORTIFY_SOURCE=2//') + +export CFLAGS="$CFLAGS -DTIZEN_DEBUG_ENABLE" +export CXXFLAGS="$CXXFLAGS -DTIZEN_DEBUG_ENABLE" +export FFLAGS="$FFLAGS -DTIZEN_DEBUG_ENABLE" %if 0%{?gcov:1} +export CFLAGS+=" -fprofile-arcs -ftest-coverage" +export CXXFLAGS+=" -fprofile-arcs -ftest-coverage" +export FFLAGS+=" -fprofile-arcs -ftest-coverage" export LDFLAGS+=" -lgcov" %endif MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` -%define NETWORK_FW_DATADIR %{TZ_SYS_GLOBALUSER_DATA}/network -%define DBDIR %{TZ_SYS_GLOBALUSER_DATA}/ +cmake . \ + -DCMAKE_VERBOSE_MAKEFILE=ON \ + -DLIB_DIR:PATH=%{_libdir} \ + -DBIN_DIR:PATH=%{_bindir} \ + -DINCLUDE_DIR:PATH=%{_includedir} \ + -DFULLVER=%{version} \ + -DMAJORVER=${MAJORVER} -cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} \ - -DLIB_INSTALL_DIR=%{_libdir} \ - -DBIN_INSTALL_DIR=%{_bindir} \ - -DINCLUDE_INSTALL_DIR=%{_includedir} \ - -DLIB_PATH=%{_lib} \ - -DFULLVER=%{version} \ - -DMAJORVER=${MAJORVER} \ - -DNETWORK_FW_DATADIR=%{NETWORK_FW_DATADIR} \ - -DDBDIR=%{DBDIR} \ - -DBUILD_GCOV=%{?gcov:1}%{!?gcov:0} +make %{?_smp_mflags} -make %{?jobs:-j%jobs} +%install + +%make_install %if 0%{?gcov:1} -mkdir -p gcov-obj -find . -name '*.gcno' -exec cp '{}' gcov-obj ';' +find .. -name '*.gcno' | tar cf %{name}-gcov.tar -T - +install -d -m 755 %{buildroot}%{_datadir}/gcov/obj +tar xf %{name}-gcov.tar -C %{buildroot}%{_datadir}/gcov/obj %endif -%install -rm -rf %{buildroot}/BUILD/%{name} -%make_install - +%check +#tests/%{name}-gtest %if 0%{?gcov:1} -mkdir -p %{buildroot}%{_datadir}/gcov/obj -install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj +lcov -c --ignore-errors graph --no-external -b . -d . -o %{name}.info +genhtml %{name}.info -o out --legend --show-details %endif %post -p /sbin/ldconfig @@ -91,8 +105,8 @@ install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj %files %manifest %{name}.manifest -%defattr(-,root,root,-) %license LICENSE.APLv2 +%defattr(-,root,root,-) %attr(644,-,-) %{_libdir}/lib%{name}.so.* %files devel @@ -101,6 +115,10 @@ install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj %{_libdir}/pkgconfig/%{name}.pc %{_includedir}/*.h +%files tests +#%{_bindir}/%{name}-test +#%{_bindir}/%{name}-gtest + %if 0%{?gcov:1} %files gcov %{_datadir}/gcov/obj/* diff --git a/pkgconfig/CMakeLists.txt b/pkgconfig/CMakeLists.txt new file mode 100644 index 0000000..44ff5e3 --- /dev/null +++ b/pkgconfig/CMakeLists.txt @@ -0,0 +1,30 @@ +# Copyright (c) 2020 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. +# +# @file CMakeLists.txt +# + +SET(PC_NAME ${CAPI_UWB}) +SET(PC_DESCRIPTION ${PACKAGE_DESCRIPTION}) +SET(PC_VERSION ${FULLVER}) +SET(PC_REQUIRED ${PC_DEPS}) +SET(PC_LDFLAGS -l${CAPI_UWB}) + +CONFIGURE_FILE(${PC_NAME}.pc.in ${PC_NAME}.pc @ONLY) + +INSTALL(FILES + ${CMAKE_BINARY_DIR}/pkgconfig/${PC_NAME}.pc + DESTINATION + ${LIB_DIR}/pkgconfig + ) diff --git a/pkgconfig/capi-network-uwb.pc.in b/pkgconfig/capi-network-uwb.pc.in new file mode 100644 index 0000000..72dcd2b --- /dev/null +++ b/pkgconfig/capi-network-uwb.pc.in @@ -0,0 +1,11 @@ +# Package Information for pkg-config + +libdir=@LIB_DIR@ +includedir=@INCLUDE_DIR@ + +Name: @PC_NAME@ +Description: @PC_DESCRIPTION@ +Version: @PC_VERSION@ +Requires: @PC_REQUIRED@ +Libs: -L${libdir} @PC_LDFLAGS@ +Cflags: -I${includedir} \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..3c99212 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,43 @@ +########################## search for packages ################################ + +PKG_CHECK_MODULES(UWB_DEPS REQUIRED ${COMMON_DEPS}) + +############################# compiler flags ################################## + +SET(EXTRA_FLAGS "-fPIC -Wall -Werror -fvisibility=hidden") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_FLAGS}") +SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread -pie") + +######################## directory configuration ############################ + +INCLUDE_DIRECTORIES(${UWB_DEPS_INCLUDE_DIRS}) +LINK_DIRECTORIES(${UWB_DEPS_LIBRARY_DIRS}) + +INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include) +INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/src) + +MESSAGE(" - Generating...d-bus code") +FIND_PROGRAM(GDBUS_CODEGEN NAMES gdbus-codegen) +EXEC_PROGRAM(${GDBUS_CODEGEN} ARGS + " \\ + --generate-c-code ${CMAKE_SOURCE_DIR}/src/uwb-gdbus \\ + --interface-prefix org.tizen.uwb. \\ + ${CMAKE_SOURCE_DIR}/src/uwb-gdbuslib.xml \\ + ") +# Build +SET(SRCS + uwb.c + uwb-util.c + uwb-gdbus.c +) + +ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS}) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${UWB_DEPS_LIBRARIES}) +SET_TARGET_PROPERTIES(${PROJECT_NAME} + PROPERTIES + VERSION ${FULLVER} + SOVERSION ${MAJORVER} +) + +# Install +INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_DIR}) diff --git a/src/gen.sh b/src/gen.sh deleted file mode 100755 index c41531f..0000000 --- a/src/gen.sh +++ /dev/null @@ -1,4 +0,0 @@ -gdbus-codegen --interface-prefix org.tizen.uwb \ - --generate-c-code uwb_gdbus \ - --c-generate-object-manager \ - uwb-gdbuslib.xml \ diff --git a/include/uwb_log.h b/src/uwb-log.h similarity index 100% rename from include/uwb_log.h rename to src/uwb-log.h diff --git a/include/uwb_private.h b/src/uwb-private.h similarity index 98% rename from include/uwb_private.h rename to src/uwb-private.h index d0e8d2b..b317bb9 100755 --- a/include/uwb_private.h +++ b/src/uwb-private.h @@ -21,8 +21,7 @@ #include #include #include -#include -#include +#include #include #define UWB_FEATURE "http://tizen.org/feature/network.uwb" diff --git a/src/uwb_util.c b/src/uwb-util.c similarity index 97% rename from src/uwb_util.c rename to src/uwb-util.c index ecaae35..5a4a563 100755 --- a/src/uwb_util.c +++ b/src/uwb-util.c @@ -16,8 +16,8 @@ */ #include #include -#include -#include +#include +#include #include void _node_free_func(gpointer data) diff --git a/include/uwb_util.h b/src/uwb-util.h similarity index 97% rename from include/uwb_util.h rename to src/uwb-util.h index de78287..c30859c 100755 --- a/include/uwb_util.h +++ b/src/uwb-util.h @@ -18,8 +18,6 @@ #ifndef __UWB_UTIL_H__ #define __UWB_UTIL_H__ -#include - #ifdef __cplusplus extern "C" { #endif diff --git a/src/uwb.c b/src/uwb.c index 4c46893..b5bac02 100755 --- a/src/uwb.c +++ b/src/uwb.c @@ -21,10 +21,10 @@ #include #include -#include -#include -#include -#include +#include +#include +#include +#include #include #define UWB_DBUS_SERVICE "org.tizen.uwb" /**< For uwb dbus */ -- 2.7.4 From 357fb3edcedb76d596665fbf6e0c55dcf5d03536 Mon Sep 17 00:00:00 2001 From: Yu Date: Thu, 27 Aug 2020 10:38:28 +0900 Subject: [PATCH 05/10] Check initialized before calling DBus method Change-Id: I5cbf2ecfa61c2eef40da9bb2b8a2d0bbb84127c2 Signed-off-by: Yu jiung --- include/uwb.h | 4 +++- src/uwb-private.h | 12 ++++++++++++ src/uwb.c | 18 +++++++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/include/uwb.h b/include/uwb.h index 6619057..c3724e7 100755 --- a/include/uwb.h +++ b/include/uwb.h @@ -37,7 +37,9 @@ typedef enum { UWB_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */ UWB_ERROR_PERMISSION_DENIED = TIZEN_ERROR_PERMISSION_DENIED, /**< Permission denied */ UWB_ERROR_NOT_SUPPORTED = TIZEN_ERROR_NOT_SUPPORTED, /**< Not supported */ - UWB_ERROR_OPERATION_FAILED = TIZEN_ERROR_UWB | 0x01 + UWB_ERROR_OPERATION_FAILED = TIZEN_ERROR_UWB | 0x01, /**< Operation failed */ + UWB_ERROR_ALREADY_INITIALIZED = TIZEN_ERROR_UWB | 0x02, /**< already initialized */ + UWB_ERROR_NOT_INITIALIZED = TIZEN_ERROR_UWB | 0x03, /**< Not initialized */ } uwb_error_e; typedef void *uwb_network_h; /**< Device handle */ diff --git a/src/uwb-private.h b/src/uwb-private.h index b317bb9..117dff3 100755 --- a/src/uwb-private.h +++ b/src/uwb-private.h @@ -26,6 +26,18 @@ #define UWB_FEATURE "http://tizen.org/feature/network.uwb" +#define CHECK_ALREADY_INITIALIZED() \ + if (uwb_ctx.manager_proxy != NULL) { \ + _WARN("Alread initialized"); \ + return UWB_ERROR_ALREADY_INITIALIZED; \ + } + +#define CHECK_NOT_INITIALIZED() \ + if (uwb_ctx.manager_proxy == NULL) { \ + _WARN("Not initialized"); \ + return UWB_ERROR_NOT_INITIALIZED; \ + } + #define CHECK_INPUT_PARAMETER(arg) \ if (arg == NULL) { \ _WARN("INVALID_PARAMETER"); \ diff --git a/src/uwb.c b/src/uwb.c index b5bac02..adf2530 100755 --- a/src/uwb.c +++ b/src/uwb.c @@ -39,7 +39,7 @@ static struct _uwb_ctx { void *position_changed_user_data; uwb_network_get_finished_cb network_get_finished_cb; uwb_network_foreach_remote_node_cb foreach_remote_node_cb; -} uwb_ctx; +} uwb_ctx = {NULL,}; static inline void __handle_error(GError *error, int *ret) { @@ -140,6 +140,7 @@ static int manager_proxy_init() static void manager_proxy_deinit() { g_object_unref(uwb_ctx.manager_proxy); + uwb_ctx.manager_proxy = NULL; } EXPORT_API int uwb_initialize() @@ -147,6 +148,7 @@ EXPORT_API int uwb_initialize() int ret = UWB_ERROR_NONE; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_ALREADY_INITIALIZED(); _BEGIN(); @@ -162,6 +164,7 @@ EXPORT_API int uwb_deinitialize() int ret = UWB_ERROR_NONE; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); _BEGIN(); @@ -178,6 +181,7 @@ EXPORT_API int uwb_reset() GError *error = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); _BEGIN(); @@ -197,6 +201,7 @@ EXPORT_API int uwb_factory_reset() GError *error = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); _BEGIN(); @@ -282,6 +287,7 @@ EXPORT_API int uwb_get_own_node(uwb_node_h *own_node) uwb_node_s **_own_node = (uwb_node_s **)own_node; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); _BEGIN(); @@ -365,6 +371,7 @@ EXPORT_API int uwb_network_get(uwb_network_get_finished_cb finished_cb, void *us int ret = UWB_ERROR_NONE; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); _BEGIN(); @@ -624,6 +631,7 @@ EXPORT_API int uwb_node_set_position(uwb_node_h node, int x, int y, int z) GError *error = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); _BEGIN(); @@ -662,6 +670,7 @@ EXPORT_API int uwb_node_send_message(const unsigned char *message, int len) GError *error = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); _BEGIN(); @@ -688,6 +697,7 @@ EXPORT_API int uwb_node_send_message_to(uwb_node_h node, const unsigned char *me GError *error = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); _BEGIN(); @@ -732,6 +742,7 @@ EXPORT_API int uwb_node_get_configuration_int32(uwb_node_h node, char *key, int3 GVariant *value_va = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); _BEGIN(); @@ -755,6 +766,7 @@ EXPORT_API int uwb_node_get_configuration_int64(uwb_node_h node, char *key, int6 GVariant *value_va = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); _BEGIN(); @@ -778,6 +790,7 @@ EXPORT_API int uwb_node_get_configuration_string(uwb_node_h node, char *key, con GVariant *value_va = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); _BEGIN(); @@ -830,6 +843,7 @@ EXPORT_API int uwb_node_set_configuration_int32(uwb_node_h node, char *key, int3 GVariant *configuration = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); _BEGIN(); @@ -850,6 +864,7 @@ EXPORT_API int uwb_node_set_configuration_int64(uwb_node_h node, char *key, int6 GVariant *configuration = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); _BEGIN(); @@ -870,6 +885,7 @@ EXPORT_API int uwb_node_set_configuration_string(uwb_node_h node, char *key, con GVariant *configuration = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); _BEGIN(); -- 2.7.4 From a257bc182a20446e8235d80d31b2dda7d339d602 Mon Sep 17 00:00:00 2001 From: Yu Date: Thu, 27 Aug 2020 16:35:16 +0900 Subject: [PATCH 06/10] Add test program Change-Id: Id1dfafbdb5fa9bbb406f67463bf26060e39c3a8b Signed-off-by: Yu jiung --- include/uwb.h | 16 +- src/uwb-util.c | 4 +- src/uwb-util.h | 6 +- src/uwb.c | 77 ++++-- tests/CMakeLists.txt | 27 ++ tests/capi-network-uwb-test.c | 597 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 697 insertions(+), 30 deletions(-) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/capi-network-uwb-test.c diff --git a/include/uwb.h b/include/uwb.h index c3724e7..e0edd16 100755 --- a/include/uwb.h +++ b/include/uwb.h @@ -48,19 +48,19 @@ typedef void (*uwb_message_received_cb)(uint64_t node_id, const unsigned char *m int message_length, void *user_data); typedef void (*uwb_position_changed_cb)(uint64_t node_id, int x, int y, int z, void *user_data); -typedef void (*uwb_network_get_finished_cb)(int result, uwb_network_h *network, void *user_data); +typedef void (*uwb_get_network_finished_cb)(int result, uwb_network_h network, void *user_data); typedef bool (*uwb_network_foreach_remote_node_cb)(uwb_node_h remote_node, void *user_data); -int uwb_initialize(); -int uwb_deinitialize(); -int uwb_reset(); -int uwb_factory_reset(); +int uwb_initialize(void); +int uwb_deinitialize(void); +int uwb_reset(void); +int uwb_factory_reset(void); int uwb_set_message_received_cb(uwb_message_received_cb message_received_cb, void *user_data); int uwb_set_position_changed_cb(uwb_position_changed_cb position_changed_cb, void *user_data); -int uwb_unset_message_received_cb(); -int uwb_unset_position_changed_cb(); +int uwb_unset_message_received_cb(void); +int uwb_unset_position_changed_cb(void); int uwb_get_own_node(uwb_node_h *own_node); -int uwb_network_get(uwb_network_get_finished_cb finished_cb, void *user_data); +int uwb_get_network(uwb_get_network_finished_cb finished_cb, void *user_data); int uwb_network_clone(uwb_network_h source, uwb_network_h *target); int uwb_network_destroy(uwb_network_h network); int uwb_network_get_pan_id(uwb_network_h network, uint64_t *pan_id); diff --git a/src/uwb-util.c b/src/uwb-util.c index 5a4a563..96f52d1 100755 --- a/src/uwb-util.c +++ b/src/uwb-util.c @@ -28,7 +28,7 @@ void _node_free_func(gpointer data) node = NULL; } -void uwb_network_clean(uwb_network_s *network) +void uwb_util_destroy_network(uwb_network_s *network) { if (network == NULL) return; @@ -39,7 +39,7 @@ void uwb_network_clean(uwb_network_s *network) network = NULL; } -uwb_node_s *uwb_get_node_from_variant(GVariant *va) +uwb_node_s *uwb_util_get_node_from_variant(GVariant *va) { GVariantIter *iter = NULL; const gchar *key; diff --git a/src/uwb-util.h b/src/uwb-util.h index c30859c..cb07e55 100755 --- a/src/uwb-util.h +++ b/src/uwb-util.h @@ -18,12 +18,14 @@ #ifndef __UWB_UTIL_H__ #define __UWB_UTIL_H__ +#include + #ifdef __cplusplus extern "C" { #endif -uwb_node_s *uwb_get_node_from_variant(GVariant *va); -void uwb_network_clean(uwb_network_s *network); +uwb_node_s *uwb_util_get_node_from_variant(GVariant *va); +void uwb_util_destroy_network(uwb_network_s *network); #ifdef __cplusplus } diff --git a/src/uwb.c b/src/uwb.c index adf2530..1be4838 100755 --- a/src/uwb.c +++ b/src/uwb.c @@ -37,7 +37,7 @@ static struct _uwb_ctx { void *message_received_user_data; uwb_position_changed_cb position_changed_cb; void *position_changed_user_data; - uwb_network_get_finished_cb network_get_finished_cb; + uwb_get_network_finished_cb get_network_finished_cb; uwb_network_foreach_remote_node_cb foreach_remote_node_cb; } uwb_ctx = {NULL,}; @@ -107,7 +107,7 @@ static void __position_changed(GObject *source_object, uwb_ctx.position_changed_cb(node_id, x, y, z, uwb_ctx.position_changed_user_data); } -static int manager_proxy_init() +static int manager_proxy_init(void) { GError *error = NULL; @@ -137,13 +137,13 @@ static int manager_proxy_init() return UWB_ERROR_NONE; } -static void manager_proxy_deinit() +static void manager_proxy_deinit(void) { g_object_unref(uwb_ctx.manager_proxy); uwb_ctx.manager_proxy = NULL; } -EXPORT_API int uwb_initialize() +EXPORT_API int uwb_initialize(void) { int ret = UWB_ERROR_NONE; @@ -159,7 +159,7 @@ EXPORT_API int uwb_initialize() return ret; } -EXPORT_API int uwb_deinitialize() +EXPORT_API int uwb_deinitialize(void) { int ret = UWB_ERROR_NONE; @@ -175,7 +175,7 @@ EXPORT_API int uwb_deinitialize() return ret; } -EXPORT_API int uwb_reset() +EXPORT_API int uwb_reset(void) { int ret = UWB_ERROR_NONE; GError *error = NULL; @@ -195,7 +195,7 @@ EXPORT_API int uwb_reset() return ret; } -EXPORT_API int uwb_factory_reset() +EXPORT_API int uwb_factory_reset(void) { int ret = UWB_ERROR_NONE; GError *error = NULL; @@ -247,7 +247,7 @@ EXPORT_API int uwb_set_position_changed_cb(uwb_position_changed_cb position_chan return ret; } -EXPORT_API int uwb_unset_message_received_cb() +EXPORT_API int uwb_unset_message_received_cb(void) { int ret = UWB_ERROR_NONE; @@ -263,7 +263,7 @@ EXPORT_API int uwb_unset_message_received_cb() return ret; } -EXPORT_API int uwb_unset_position_changed_cb() +EXPORT_API int uwb_unset_position_changed_cb(void) { int ret = UWB_ERROR_NONE; @@ -299,7 +299,7 @@ EXPORT_API int uwb_get_own_node(uwb_node_h *own_node) } if (ret == UWB_ERROR_NONE && own_node_va != NULL) - *_own_node = uwb_get_node_from_variant(own_node_va); + *_own_node = uwb_util_get_node_from_variant(own_node_va); else *_own_node = NULL; @@ -325,8 +325,8 @@ static void __network_get_cb(GObject *source_object, GAsyncResult *res, gpointer _ERR("manager_call_get_network_info_finish failed : %s", error->message); __handle_error(error, &ret); - if (uwb_ctx.network_get_finished_cb != NULL) { - uwb_ctx.network_get_finished_cb(ret, NULL, user_data); + if (uwb_ctx.get_network_finished_cb != NULL) { + uwb_ctx.get_network_finished_cb(ret, NULL, user_data); } return; @@ -348,7 +348,7 @@ static void __network_get_cb(GObject *source_object, GAsyncResult *res, gpointer while ((nodes_va = g_variant_iter_next_value(iter)) != NULL) { uwb_node_s *node; - node = uwb_get_node_from_variant(nodes_va); + node = uwb_util_get_node_from_variant(nodes_va); node->is_remote = true; _DBG("(%d, %d, %d)", node->x, node->y, node->z); @@ -361,12 +361,12 @@ static void __network_get_cb(GObject *source_object, GAsyncResult *res, gpointer g_variant_unref(remote_node_list_va); } - if (uwb_ctx.network_get_finished_cb != NULL) { - uwb_ctx.network_get_finished_cb(ret, (uwb_network_h)network_s, user_data); + if (uwb_ctx.get_network_finished_cb != NULL) { + uwb_ctx.get_network_finished_cb(ret, (uwb_network_h)network_s, user_data); } } -EXPORT_API int uwb_network_get(uwb_network_get_finished_cb finished_cb, void *user_data) +EXPORT_API int uwb_network_get(uwb_get_network_finished_cb finished_cb, void *user_data) { int ret = UWB_ERROR_NONE; @@ -377,7 +377,7 @@ EXPORT_API int uwb_network_get(uwb_network_get_finished_cb finished_cb, void *us uwb_check_null_ret_error("finished_cb", finished_cb, UWB_ERROR_INVALID_PARAMETER); - uwb_ctx.network_get_finished_cb = finished_cb; + uwb_ctx.get_network_finished_cb = finished_cb; manager_call_get_network_info(uwb_ctx.manager_proxy, NULL, __network_get_cb, user_data); @@ -386,16 +386,57 @@ EXPORT_API int uwb_network_get(uwb_network_get_finished_cb finished_cb, void *us return ret; } + +static gpointer __copy_node(gconstpointer src, gpointer data) +{ + if (!src) + return NULL; + + uwb_node_s *src_ptr = (uwb_node_s *)src; + uwb_node_s *dst_ptr = (uwb_node_s *)malloc(sizeof(uwb_node_s)); + if (!dst_ptr) + return NULL; + + dst_ptr->node_id = src_ptr->node_id; + dst_ptr->pan_id = src_ptr->pan_id; + dst_ptr->is_remote = src_ptr->is_remote; + dst_ptr->distance = src_ptr->distance; + dst_ptr->x = src_ptr->x; + dst_ptr->y = src_ptr->y; + dst_ptr->z = src_ptr->z; + + return (gpointer)dst_ptr; +} + EXPORT_API int uwb_network_clone(uwb_network_h source, uwb_network_h *target) { + uwb_network_s *result_network = NULL; + uwb_network_s *src_network = (uwb_network_s *)source; + GSList *remote_node_list = NULL; int ret = UWB_ERROR_NONE; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); _BEGIN(); + uwb_check_null_ret_error("source", source, UWB_ERROR_INVALID_PARAMETER); + + result_network = (uwb_network_s *)malloc(sizeof(uwb_network_s)); + if (result_network == NULL) { + _ERR("malloc failed"); + return UWB_ERROR_OPERATION_FAILED; + } + + result_network->pan_id = src_network->pan_id; + result_network->remote_node_count = src_network->remote_node_count; + remote_node_list = src_network->remote_node_list; + result_network->remote_node_list = g_slist_copy_deep(remote_node_list, + __copy_node, + NULL); + _END(); + *target = result_network; return ret; } @@ -409,7 +450,7 @@ EXPORT_API int uwb_network_destroy(uwb_network_h network) uwb_check_null_ret_error("network", network, UWB_ERROR_INVALID_PARAMETER); - uwb_network_clean(network); + uwb_util_destroy_network(network); _END(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..da32239 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,27 @@ +########################## search for packages ################################ + +PKG_CHECK_MODULES(TARGET_UWB_TEST_REQ_PKGS REQUIRED glib-2.0) + +############################# compiler flags ################################## + +SET(EXTRA_FLAGS "-Wall -Werror") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} ${CFLAGS} -fPIE") +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") + +######################## directory configuration ############################ + +INCLUDE_DIRECTORIES(${TARGET_UWB_TEST_REQ_PKGS_INCLUDE_DIRS}) +LINK_DIRECTORIES(${TARGET_UWB_TEST_REQ_PKGS_LIBRARY_DIRS}) + +INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include) + +# Build +ADD_EXECUTABLE(${TARGET_UWB_TEST} + ${CMAKE_CURRENT_SOURCE_DIR}/capi-network-uwb-test.c +) + +TARGET_LINK_LIBRARIES(${TARGET_UWB_TEST} + ${TARGET_UWB_TEST_REQ_PKGS_LIBRARIES} ${PROJECT_NAME}) + +#Install +INSTALL(TARGETS ${TARGET_UWB_TEST} DESTINATION ${BIN_DIR}) \ No newline at end of file diff --git a/tests/capi-network-uwb-test.c b/tests/capi-network-uwb-test.c new file mode 100644 index 0000000..fad84f8 --- /dev/null +++ b/tests/capi-network-uwb-test.c @@ -0,0 +1,597 @@ +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +#define RESET_COLOR "\e[m" +#define MAKE_RED "\e[31m" +#define MAKE_GREEN "\e[32m" + +#define __FUNC_ENTER__ printf("%s() entering...\n", __func__) +#define __FUNC_EXIT__ printf("%s() leaving...\n", __func__) + +#define RET_IF_LOOP_IS_NULL()\ + do {\ + if (!g_main_loop_p) {\ + printf("Loop was not initialized\n");\ + return;\ + } \ + } while (0) + + +enum { + CMD_QUIT, + CMD_FULL_MENU, + CMD_INITIALIZE, + CMD_DEINITIALIZE, + CMD_RESET, + CMD_FACTORY_RESET, + CMD_GET_OWN_NODE, + CMD_GET_NETWORK, + CMD_SET_POSITION, + CMD_SEND_MESSAGE, + CMD_SEND_MESSAGE_TO, + CMD_GET_CONFIGURATION, + CMD_SET_CONFIGURATION, + + CMD_INVALID, +}; + +char *g_menu_str[] = { + [CMD_QUIT] + = "QUIT", + [CMD_FULL_MENU] + = "FULL_MENU", + [CMD_INITIALIZE] + = "INITIALIZE", + [CMD_DEINITIALIZE] + = "DEINITIALIZE", + [CMD_RESET] + = "RESET", + [CMD_FACTORY_RESET] + = "FACTORY_RESET", + [CMD_GET_OWN_NODE] + = "GET_OWN_NODE", + [CMD_GET_NETWORK] + = "GET_NETWORK", + + [CMD_SET_POSITION] + = "SET_POSITION", + [CMD_SEND_MESSAGE] + = "SEND_MESSAGE", + [CMD_SEND_MESSAGE_TO] + = "SEND_MESSAGE_TO", + [CMD_GET_CONFIGURATION] + = "GET_CONFIGURATION", + [CMD_SET_CONFIGURATION] + = "SET_CONFIGURATION", + + [CMD_INVALID] + = NULL, }; + +static GMainLoop* g_main_loop_p; + +static const char *__print_error(uwb_error_e err_type) +{ + switch (err_type) { + case UWB_ERROR_NONE: + return "NONE"; + case UWB_ERROR_IO_ERROR: + return "IO_ERROR"; + case UWB_ERROR_OUT_OF_MEMORY: + return "OUT_OF_MEMORY"; + case UWB_ERROR_INVALID_PARAMETER: + return "INVALID_PARAMETER"; + case UWB_ERROR_PERMISSION_DENIED: + return "PERMISSION_DENIED"; + case UWB_ERROR_NOT_SUPPORTED: + return "NOT_SUPPORTED"; + case UWB_ERROR_OPERATION_FAILED: + return "OPERATION_FAILED"; + case UWB_ERROR_ALREADY_INITIALIZED: + return "ALREADY_INITIALIZED"; + case UWB_ERROR_NOT_INITIALIZED: + return "NOT_INITIALIZED"; + default: + break; + } + return "UNKNOWN"; +} + +static inline void __print_result(int ret, gchar *function_name) +{ + if (ret == UWB_ERROR_NONE) { + printf(MAKE_GREEN"%s"RESET_COLOR"\n", function_name); + } else { + printf(MAKE_RED"%s : %s ", function_name, __print_error(ret)); + printf(RESET_COLOR"\n"); + } + + printf("%s() result=[%d]\n", function_name, ret); +} + +static char *__cmd_transform(char *str) +{ + int i, j; + int len; + static char static_buffer[255]; + + if (str == NULL) + return ""; + + len = strlen(str); + if (len == 0) + return ""; + + /* lower char */ + /* replance "_" to space */ + for (i = 0, j = 0; i < len; i++, j++) { + + if (str[j] >= 'A' && str[j] <= 'Z') + static_buffer[i] = str[j] + 'a' - 'A'; + else if (str[j] == '_') + static_buffer[i] = ' '; + else + static_buffer[i] = str[j]; + } + static_buffer[j] = '\0'; + + return static_buffer; +} + +static inline void __usage_full() +{ + int i; + printf("Call Test Program\n"); + + for (i = CMD_QUIT; i < CMD_INVALID; i++) { + if (i%3 == 0) + printf("\n"); + printf(" %02d: %-32s ", i, + __cmd_transform(g_menu_str[i])); + } + printf("\n"); +} + +static int __is_digit(const char* str) +{ + int len; + int i; + + if (str == NULL) + return -1; + + if (strlen(str) == 0) + return -1; + + len = strlen(str); + for (i = 0; i < len; i++) { + if (str[i] < '0' || str[i] > '9') + return -2; + } + + return 0; +} + +void test_full_menu(void) +{ + __usage_full(); + + return; +} + +void test_quit(void) +{ + RET_IF_LOOP_IS_NULL(); + + printf("Bye\n"); + g_main_loop_quit(g_main_loop_p); + + return; +} + +void test_init(void) +{ + int ret = 0; + + RET_IF_LOOP_IS_NULL(); + + ret = uwb_initialize(); + __print_result(ret, "uwb_initialize"); + + return; +} + +void test_deinit(void) +{ + int ret = 0; + + RET_IF_LOOP_IS_NULL(); + + ret = uwb_deinitialize(); + __print_result(ret, "uwb_deinitialize"); + + return; +} + +void test_reset(void) +{ + int ret = 0; + + RET_IF_LOOP_IS_NULL(); + + ret = uwb_reset(); + __print_result(ret, "uwb_reset"); + + return; +} + +void test_factory_reset(void) +{ + int ret = 0; + + RET_IF_LOOP_IS_NULL(); + + ret = uwb_factory_reset(); + __print_result(ret, "uwb_factory_reset"); + + return; +} + +bool __print_node_info(uwb_node_h node, void *user_data) +{ + uint64_t distance = 0; + uint64_t node_id = 0; + uint64_t pan_id = 0; + bool is_remote = 0; + int x = 0, y = 0, z = 0; + int ret = 0; + + if (node == NULL) + return true; + + ret = uwb_node_get_distance(node, &distance); + __print_result(ret, "uwb_node_get_distance"); + ret = uwb_node_get_node_id(node, &node_id); + __print_result(ret, "uwb_node_get_node_id"); + ret = uwb_node_get_pan_id(node, &pan_id); + __print_result(ret, "uwb_node_get_pan_id"); + ret = uwb_node_get_is_remote(node, &is_remote); + __print_result(ret, "uwb_node_get_is_remote"); + ret = uwb_node_get_position(node, &x, &y, &z); + __print_result(ret, "uwb_node_get_position"); + + printf("Distance :%llu Node ID: %llu Pan ID: %llu\n", distance, node_id, pan_id); + printf("Position X: %d Y: %d Z: %d\n", x, y, z); + return true; +} + +void test_get_own_node(void) +{ + uwb_node_h own_node = NULL; + int ret = 0; + + RET_IF_LOOP_IS_NULL(); + + ret = uwb_get_own_node(&own_node); + __print_result(ret, "uwb_get_own_node"); + + if (own_node == NULL) + return; + + uwb_node_h cloned_node = NULL; + ret = uwb_node_clone(own_node, &cloned_node); + __print_result(ret, "uwb_node_clone"); + + ret = uwb_node_destroy(own_node); + __print_result(ret, "uwb_node_destroy"); + + if (cloned_node == NULL) + return; + + __print_node_info(cloned_node, NULL); + ret = uwb_node_destroy(cloned_node); + __print_result(ret, "cloned_node"); + + return; +} + +void __print_network_info(uwb_network_h uwb_network) +{ + uint64_t pan_id = 0; + int remote_node_count = 0; + int ret = 0; + + ret = uwb_network_get_pan_id(uwb_network, &pan_id); + __print_result(ret, "uwb_network_get_pan_id"); + ret = uwb_network_get_remote_node_count(uwb_network, &remote_node_count); + __print_result(ret, "uwb_network_get_remote_node_count"); + + printf("Pan ID: %llu remote node count: %d\n", pan_id, remote_node_count); + uwb_network_foreach_remote_node(uwb_network, __print_node_info, NULL); +} + +void __network_finished_cb(int result, uwb_network_h uwb_network, void *user_data) +{ + int ret = 0; + if (result != UWB_ERROR_NONE) + return; + + if (uwb_network == NULL) + return; + + uwb_network_h cloned_network = NULL; + ret = uwb_network_clone(uwb_network, &cloned_network); + __print_result(ret, "uwb_network_clone"); + + ret = uwb_network_destroy(uwb_network); + __print_result(ret, "uwb_network_destroy"); + + if (cloned_network == NULL) + return; + + __print_network_info(cloned_network); + ret = uwb_network_destroy(cloned_network); + __print_result(ret, "uwb_network_destroy"); + +} + +void test_get_network(void) +{ + int ret = 0; + + RET_IF_LOOP_IS_NULL(); + + ret = uwb_get_network(__network_finished_cb, NULL); + __print_result(ret, "uwb_get_network"); + + return; +} + +void test_set_position(void) +{ + uwb_node_h own_node = NULL; + int ret = 0; + int test_x = 10; + int test_y = 10; + int test_z = 10; + + RET_IF_LOOP_IS_NULL(); + ret = uwb_get_own_node(&own_node); + __print_result(ret, "uwb_get_own_node"); + + + ret = uwb_node_set_position(own_node, test_x, test_y, test_z); + __print_result(ret, "uwb_node_set_position"); +} + +void test_send_message(void) +{ + uwb_node_h own_node = NULL; + int ret = 0; + const char test_string[] = {"test string"}; + + ret = uwb_get_own_node(&own_node); + __print_result(ret, "uwb_get_own_node"); + + ret = uwb_node_send_message((const unsigned char *)test_string, strlen(test_string)); + __print_result(ret, "uwb_node_send_message"); +} + +void test_send_message_to(void) +{ + uwb_node_h own_node = NULL; + int ret = 0; + const char test_string[] = {"test string"}; + + ret = uwb_get_own_node(&own_node); + __print_result(ret, "uwb_get_own_node"); + + ret = uwb_node_send_message_to(own_node, (const unsigned char *)test_string, strlen(test_string)); + __print_result(ret, "uwb_node_send_message"); +} + +void test_get_configuration(void) +{ + uwb_node_h own_node = NULL; + int data_type = 0; + char *key; + int32_t int32_data; + int64_t int64_data; + char *string_data = NULL; + int ret = 0; + + RET_IF_LOOP_IS_NULL(); + + printf("Select Data type :\n"); + printf("0. int32_t\n"); + printf("1. int64_t\n"); + printf("2. const char *\n"); + if (scanf("%d", &data_type) < 1) + return; + + printf("Input key :\n"); + if (scanf(" %255ms", &key) < 1) + return; + + ret = uwb_get_own_node(&own_node); + __print_result(ret, "uwb_get_own_node"); + if (!own_node) + goto out; + + if (data_type == 0) { + ret = uwb_node_get_configuration_int32(own_node, key, &int32_data); + __print_result(ret, "uwb_node_get_configuration_int32"); + } else if (data_type == 1) { + ret = uwb_node_get_configuration_int64(own_node, key, &int64_data); + __print_result(ret, "uwb_node_get_configuration_int64"); + } else if (data_type == 2) { + ret = uwb_node_get_configuration_string(own_node, key, &string_data); + __print_result(ret, "uwb_node_get_configuration_string"); + } + +out: + if (key) + free(key); + uwb_node_destroy(own_node); + if (string_data) + free(string_data); +} + +void test_set_configuration(void) +{ + uwb_node_h own_node = NULL; + int data_type = 0; + char *key; + int32_t int32_data; + int64_t int64_data; + char *string_data = NULL; + int ret = 0; + + RET_IF_LOOP_IS_NULL(); + + printf("Select Data type :\n"); + printf("0. int32_t\n"); + printf("1. int64_t\n"); + printf("2. const char *\n"); + if (scanf("%d", &data_type) < 1) + return; + + printf("Input key :\n"); + if (scanf(" %255ms", &key) < 1) + return; + + ret = uwb_get_own_node(&own_node); + __print_result(ret, "uwb_get_own_node"); + if (!own_node) + goto out; + + if (data_type == 0) { + ret = uwb_node_set_configuration_int32(own_node, key, int32_data); + __print_result(ret, "uwb_node_set_configuration_int32"); + } else if (data_type == 1) { + ret = uwb_node_set_configuration_int64(own_node, key, int64_data); + __print_result(ret, "uwb_node_set_configuration_int64"); + } else if (data_type == 2) { + printf("Input string :\n"); + if (scanf(" %255ms", &string_data) < 1) + return; + ret = uwb_node_set_configuration_string(own_node, key, string_data); + __print_result(ret, "uwb_node_set_configuration_string"); + } + +out: + if (key) + free(key); + uwb_node_destroy(own_node); + if (string_data) + free(string_data); +} + + +typedef void (*test_func)(void); +test_func g_menu_func[] = { + [CMD_QUIT] + = test_quit, + [CMD_FULL_MENU] + = test_full_menu, + + [CMD_INITIALIZE] + = test_init, + [CMD_DEINITIALIZE] + = test_deinit, + + [CMD_RESET] + = test_reset, + [CMD_FACTORY_RESET] + = test_factory_reset, + + [CMD_GET_OWN_NODE] + = test_get_own_node, + [CMD_GET_NETWORK] + = test_get_network, + + [CMD_SET_POSITION] + = test_set_position, + [CMD_SEND_MESSAGE] + = test_send_message, + [CMD_SEND_MESSAGE_TO] + = test_send_message_to, + [CMD_GET_CONFIGURATION] + = test_get_configuration, + [CMD_SET_CONFIGURATION] + = test_set_configuration, + [CMD_INVALID] + = NULL, }; + +static void __process_input(const char *input, gpointer user_data) +{ + int cmd = strtol(input, NULL, 0); + + if (__is_digit(input) < 0 || strlen(input) == 0 || errno == ERANGE || errno + == EINVAL) + cmd = CMD_INVALID; + + printf("cmd=[%d]\n", cmd); + if (cmd >= CMD_INVALID || cmd < CMD_QUIT) { + printf("Invalid CMD\n"); + return; + } + g_menu_func[cmd](); +} + +static gboolean __test_terminal_read_std_input(GIOChannel * source, + GIOCondition condition, gpointer user_data) +{ + int fd = 0; + + static char buf[1024]; + int n; + + errno = 0; + n = read(fd, buf, 1024); + if (n == 0) { + printf("Error: read() from stdin returns 0.\n"); + } else if (n < 0) { + char error_buf[100] = {0, }; + strerror_r(errno, error_buf, sizeof(error_buf)); + printf("input: read, err=%s\n", error_buf); + } else { + buf[n - 1] = '\0'; /* remove new line... */ + printf("\n\n"); + /* printf("Read [%d]bytes data: [%s]\n", n, buf); */ + /* printf("Processing it ---------------------\n", n, buf); */ + __process_input(buf, user_data); + } + + return TRUE; +} + +int main(int argc, char **argv) +{ + +#if !GLIB_CHECK_VERSION(2, 36, 0) + g_type_init(); +#endif + + g_main_loop_p = g_main_loop_new(NULL, FALSE); + + int std_input_fd = 0; + GIOChannel *gio2 = g_io_channel_unix_new(std_input_fd); + g_io_add_watch(gio2, G_IO_IN, (GIOFunc) __test_terminal_read_std_input, NULL); + g_io_channel_unref(gio2); + + __usage_full(); + + g_main_loop_run(g_main_loop_p); + + return 0; +} -- 2.7.4 From aef4fdb2df44109a7f8d14c4eda0441900fdabdb Mon Sep 17 00:00:00 2001 From: Yu Date: Fri, 28 Aug 2020 11:01:53 +0900 Subject: [PATCH 07/10] Fix set/get configuration API Change-Id: I4fb0b0ccf154edc21067404fcce0e232edf3939f Signed-off-by: Yu jiung --- CMakeLists.txt | 1 + include/uwb.h | 18 +- packaging/capi-network-uwb.spec | 2 +- src/uwb.c | 533 ++++++++++++++++++++-------------------- tests/capi-network-uwb-test.c | 19 +- 5 files changed, 296 insertions(+), 277 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 516c32f..b430e62 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,3 +39,4 @@ ADD_DEFINITIONS("-DUSE_DLOG") ADD_SUBDIRECTORY(include) ADD_SUBDIRECTORY(pkgconfig) ADD_SUBDIRECTORY(src) +ADD_SUBDIRECTORY(tests) diff --git a/include/uwb.h b/include/uwb.h index e0edd16..3717eb8 100755 --- a/include/uwb.h +++ b/include/uwb.h @@ -59,6 +59,15 @@ int uwb_set_message_received_cb(uwb_message_received_cb message_received_cb, voi int uwb_set_position_changed_cb(uwb_position_changed_cb position_changed_cb, void *user_data); int uwb_unset_message_received_cb(void); int uwb_unset_position_changed_cb(void); +int uwb_node_set_position(uwb_node_h node, int x, int y, int z); +int uwb_node_send_message(const unsigned char *message, int len); +int uwb_node_send_message_to(uwb_node_h node, const unsigned char *message, int len); +int uwb_node_get_configuration_int32(uwb_node_h node, const char *key, int32_t *value); +int uwb_node_set_configuration_int32(uwb_node_h node, const char *key, int32_t value); +int uwb_node_get_configuration_int64(uwb_node_h node, const char *key, int64_t *value); +int uwb_node_set_configuration_int64(uwb_node_h node, const char *key, int64_t value); +int uwb_node_get_configuration_string(uwb_node_h node, const char *key, char **value); +int uwb_node_set_configuration_string(uwb_node_h node, const char *key, const char *value); int uwb_get_own_node(uwb_node_h *own_node); int uwb_get_network(uwb_get_network_finished_cb finished_cb, void *user_data); int uwb_network_clone(uwb_network_h source, uwb_network_h *target); @@ -73,15 +82,6 @@ int uwb_node_get_node_id(uwb_node_h node, uint64_t *node_id); int uwb_node_get_pan_id(uwb_node_h node, uint64_t *pan_id); int uwb_node_get_is_remote(uwb_node_h node, bool *is_remote); int uwb_node_get_position(uwb_node_h node, int *x, int *y, int *z); -int uwb_node_set_position(uwb_node_h node, int x, int y, int z); -int uwb_node_send_message(const unsigned char *message, int len); -int uwb_node_send_message_to(uwb_node_h node, const unsigned char *message, int len); -int uwb_node_get_configuration_int32(uwb_node_h node, char *key, int32_t *value); -int uwb_node_set_configuration_int32(uwb_node_h node, char *key, int32_t value); -int uwb_node_get_configuration_int64(uwb_node_h node, char *key, int64_t *value); -int uwb_node_set_configuration_int64(uwb_node_h node, char *key, int64_t value); -int uwb_node_get_configuration_string(uwb_node_h node, char *key, const char **value); -int uwb_node_set_configuration_string(uwb_node_h node, char *key, const char *value); #ifdef __cplusplus } diff --git a/packaging/capi-network-uwb.spec b/packaging/capi-network-uwb.spec index 48b5eee..25a9794 100644 --- a/packaging/capi-network-uwb.spec +++ b/packaging/capi-network-uwb.spec @@ -116,7 +116,7 @@ genhtml %{name}.info -o out --legend --show-details %{_includedir}/*.h %files tests -#%{_bindir}/%{name}-test +%{_bindir}/%{name}-test #%{_bindir}/%{name}-gtest %if 0%{?gcov:1} diff --git a/src/uwb.c b/src/uwb.c index 1be4838..50851e9 100755 --- a/src/uwb.c +++ b/src/uwb.c @@ -308,219 +308,280 @@ EXPORT_API int uwb_get_own_node(uwb_node_h *own_node) return ret; } -static void __network_get_cb(GObject *source_object, GAsyncResult *res, gpointer user_data) +EXPORT_API int uwb_node_set_position(uwb_node_h node, int x, int y, int z) { int ret = UWB_ERROR_NONE; - uint16_t pan_id = 0; - GVariant *remote_node_list_va = NULL; GError *error = NULL; - uwb_network_s *network_s; - if (manager_call_get_network_info_finish( - MANAGER(source_object), - &pan_id, - &remote_node_list_va, - res, - &error) == FALSE) { - _ERR("manager_call_get_network_info_finish failed : %s", error->message); - __handle_error(error, &ret); + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); - if (uwb_ctx.get_network_finished_cb != NULL) { - uwb_ctx.get_network_finished_cb(ret, NULL, user_data); - } + _BEGIN(); - return; + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + + if (manager_call_set_position_sync(uwb_ctx.manager_proxy, + ((uwb_node_s *)node)->node_id, x, y, z, NULL, &error) == FALSE) { + _ERR("manager_call_set_position_sync failed : %s", error->message); + __handle_error(error, &ret); } - /* create network handle */ - network_s = (uwb_network_s *)malloc(sizeof(uwb_network_s)); + _END(); - if (ret == UWB_ERROR_NONE) { - GSList *remote_node_list = NULL; - network_s->pan_id = pan_id; - network_s->remote_node_count = g_variant_n_children(remote_node_list_va); + return ret; +} - if (network_s->remote_node_count > 0) { - GVariantIter *iter = NULL; - GVariant *nodes_va = NULL; +static GVariant *__data_to_variant(const unsigned char *data, int length) +{ + GVariantBuilder builder; - g_variant_get(remote_node_list_va, "aa{sv}", &iter); - while ((nodes_va = g_variant_iter_next_value(iter)) != NULL) { - uwb_node_s *node; + g_variant_builder_init(&builder, G_VARIANT_TYPE("a(y)")); - node = uwb_util_get_node_from_variant(nodes_va); - node->is_remote = true; + if (data && length > 0) + { + for(int i = 0; i < length; i++) + g_variant_builder_add(&builder, "(y)", *(data + i)); + } - _DBG("(%d, %d, %d)", node->x, node->y, node->z); - remote_node_list = g_slist_append(remote_node_list, node); - } - g_variant_iter_free(iter); - } - network_s->remote_node_list = remote_node_list; + return g_variant_builder_end(&builder); +} - g_variant_unref(remote_node_list_va); - } +EXPORT_API int uwb_node_send_message(const unsigned char *message, int len) +{ + int ret = UWB_ERROR_NONE; + GVariant *msg = NULL; + GError *error = NULL; - if (uwb_ctx.get_network_finished_cb != NULL) { - uwb_ctx.get_network_finished_cb(ret, (uwb_network_h)network_s, user_data); + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); + + _BEGIN(); + + uwb_check_null_ret_error("message", message, UWB_ERROR_INVALID_PARAMETER); + cond_expr_ret(len <= 0, UWB_ERROR_INVALID_PARAMETER); + + msg = __data_to_variant(message, len); + + if (manager_call_send_message_sync(uwb_ctx.manager_proxy, + msg, len, NULL, &error) == FALSE) { + _ERR("manager_call_send_message_sync failed : %s", error->message); + __handle_error(error, &ret); } + + _END(); + + return ret; } -EXPORT_API int uwb_network_get(uwb_get_network_finished_cb finished_cb, void *user_data) +EXPORT_API int uwb_node_send_message_to(uwb_node_h node, const unsigned char *message, int len) { int ret = UWB_ERROR_NONE; + GVariant *msg = NULL; + GError *error = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); CHECK_NOT_INITIALIZED(); _BEGIN(); - uwb_check_null_ret_error("finished_cb", finished_cb, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("message", message, UWB_ERROR_INVALID_PARAMETER); + cond_expr_ret(len <= 0, UWB_ERROR_INVALID_PARAMETER); - uwb_ctx.get_network_finished_cb = finished_cb; + msg = __data_to_variant(message, len); - manager_call_get_network_info(uwb_ctx.manager_proxy, NULL, __network_get_cb, user_data); + if (manager_call_send_message_to_sync(uwb_ctx.manager_proxy, ((uwb_node_s *)node)->node_id, + msg, len, NULL, &error) == FALSE) { + _ERR("manager_call_send_message_to_sync failed : %s", error->message); + __handle_error(error, &ret); + } _END(); return ret; } - -static gpointer __copy_node(gconstpointer src, gpointer data) +static int __get_configurations(uwb_node_h node, const char *key, + const GVariantType *va_type, GVariant **va_out) { - if (!src) - return NULL; - - uwb_node_s *src_ptr = (uwb_node_s *)src; - uwb_node_s *dst_ptr = (uwb_node_s *)malloc(sizeof(uwb_node_s)); - if (!dst_ptr) - return NULL; + int ret = UWB_ERROR_NONE; + GError *error = NULL; + GVariant *va = NULL; - dst_ptr->node_id = src_ptr->node_id; - dst_ptr->pan_id = src_ptr->pan_id; - dst_ptr->is_remote = src_ptr->is_remote; - dst_ptr->distance = src_ptr->distance; - dst_ptr->x = src_ptr->x; - dst_ptr->y = src_ptr->y; - dst_ptr->z = src_ptr->z; + if (manager_call_get_configurations_sync(uwb_ctx.manager_proxy, + ((uwb_node_s *)node)->node_id, &va, NULL, &error) == FALSE) { + _ERR("__get_configurations failed : %s", error->message); + __handle_error(error, &ret); + } else { + *va_out = g_variant_lookup_value(va, key, va_type); + } - return (gpointer)dst_ptr; + return ret; } -EXPORT_API int uwb_network_clone(uwb_network_h source, uwb_network_h *target) +EXPORT_API int uwb_node_get_configuration_int32(uwb_node_h node, const char *key, int32_t *value) { - uwb_network_s *result_network = NULL; - uwb_network_s *src_network = (uwb_network_s *)source; - GSList *remote_node_list = NULL; int ret = UWB_ERROR_NONE; + GVariant *value_va = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); _BEGIN(); - uwb_check_null_ret_error("source", source, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); - result_network = (uwb_network_s *)malloc(sizeof(uwb_network_s)); - if (result_network == NULL) { - _ERR("malloc failed"); - return UWB_ERROR_OPERATION_FAILED; - } + ret = __get_configurations(node, key, G_VARIANT_TYPE_INT32, &value_va); - result_network->pan_id = src_network->pan_id; - result_network->remote_node_count = src_network->remote_node_count; - remote_node_list = src_network->remote_node_list; - result_network->remote_node_list = g_slist_copy_deep(remote_node_list, - __copy_node, - NULL); + if (value_va != NULL) + *value = g_variant_get_int32(value_va); + else + *value = 0; _END(); - *target = result_network; return ret; } -EXPORT_API int uwb_network_destroy(uwb_network_h network) +EXPORT_API int uwb_node_get_configuration_int64(uwb_node_h node, const char *key, int64_t *value) { int ret = UWB_ERROR_NONE; + GVariant *value_va = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); _BEGIN(); - uwb_check_null_ret_error("network", network, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); - uwb_util_destroy_network(network); + ret = __get_configurations(node, key, G_VARIANT_TYPE_INT64, &value_va); + + if (value_va != NULL) + *value = g_variant_get_int64(value_va); + else + *value = 0; _END(); return ret; } -EXPORT_API int uwb_network_get_pan_id(uwb_network_h network, uint64_t *pan_id) +EXPORT_API int uwb_node_get_configuration_string(uwb_node_h node, const char *key, char **value) { int ret = UWB_ERROR_NONE; + GVariant *value_va = NULL; + const char *value_str = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); _BEGIN(); - uwb_check_null_ret_error("network", network, UWB_ERROR_INVALID_PARAMETER); - uwb_check_null_ret_error("pan_id", pan_id, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); - *pan_id = ((uwb_network_s *)network)->pan_id; + ret = __get_configurations(node, key, G_VARIANT_TYPE_STRING, &value_va); + + if (value_va != NULL) + value_str = g_variant_get_string(value_va, NULL); + else + value_str = NULL; _END(); + if (value_str && strlen(value_str) != 0) + *value = strdup(value_str); return ret; } -EXPORT_API int uwb_network_get_remote_node_count(uwb_network_h network, int *remote_node_count) +static int __set_configuration(uwb_node_h node, GVariant *va) +{ + int ret = UWB_ERROR_NONE; + GError *error = NULL; + + if (manager_call_set_configurations_sync(uwb_ctx.manager_proxy, ((uwb_node_s *)node)->node_id, + va, NULL, &error) == FALSE) { + _ERR("__set_configurations : %s", error->message); + __handle_error(error, &ret); + } + + return ret; +} + +static GVariant *__build_configuration(const char *key, GVariant *value) +{ + GVariant *va = NULL; + GVariantBuilder *builder; + + builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); + g_variant_builder_add(builder, "{sv}", key, value); + va = g_variant_new("a{sv}", builder); + g_variant_builder_unref(builder); + + return va; +} + +EXPORT_API int uwb_node_set_configuration_int32(uwb_node_h node, const char *key, int32_t value) { int ret = UWB_ERROR_NONE; + GVariant *configuration = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); _BEGIN(); - uwb_check_null_ret_error("network", network, UWB_ERROR_INVALID_PARAMETER); - uwb_check_null_ret_error("remote_node_count", remote_node_count, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("key", key, UWB_ERROR_INVALID_PARAMETER); - *remote_node_count = ((uwb_network_s *)network)->remote_node_count; + configuration = __build_configuration(key, g_variant_new_int32(value)); + + ret = __set_configuration(node, configuration); _END(); return ret; } -static void __foreach_node(gpointer data, gpointer user_data) +EXPORT_API int uwb_node_set_configuration_int64(uwb_node_h node, const char *key, int64_t value) { - uwb_node_s *node = (uwb_node_s *)data; - _DBG("panid : 0x%X, nodeid : 0x%04"PRIX64", distance : %"PRId64"", node->pan_id, node->node_id, node->distance); + int ret = UWB_ERROR_NONE; + GVariant *configuration = NULL; - if (uwb_ctx.foreach_remote_node_cb != NULL) - uwb_ctx.foreach_remote_node_cb((uwb_node_h)node, user_data); + CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); + + _BEGIN(); + + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("key", key, UWB_ERROR_INVALID_PARAMETER); + + configuration = __build_configuration(key, g_variant_new_int64(value)); + + ret = __set_configuration(node, configuration); + + _END(); + + return ret; } -EXPORT_API int uwb_network_foreach_remote_node(uwb_network_h network, - uwb_network_foreach_remote_node_cb node_cb, void *user_data) +EXPORT_API int uwb_node_set_configuration_string(uwb_node_h node, const char *key, const char *value) { int ret = UWB_ERROR_NONE; + GVariant *configuration = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + CHECK_NOT_INITIALIZED(); _BEGIN(); - uwb_check_null_ret_error("network", network, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("key", key, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("value", value, UWB_ERROR_INVALID_PARAMETER); - uwb_network_s *network_s = (uwb_network_s *)network; - if (network_s->remote_node_count == 0) { - _DBG("remote_node_count is 0"); - return ret; - } + configuration = __build_configuration(key, g_variant_new_string(value)); - uwb_ctx.foreach_remote_node_cb = node_cb; - g_slist_foreach(network_s->remote_node_list, __foreach_node, user_data); + ret = __set_configuration(node, configuration); _END(); @@ -666,278 +727,220 @@ EXPORT_API int uwb_node_get_position(uwb_node_h node, int *x, int *y, int *z) return ret; } -EXPORT_API int uwb_node_set_position(uwb_node_h node, int x, int y, int z) +static void __get_network_cb(GObject *source_object, GAsyncResult *res, gpointer user_data) { int ret = UWB_ERROR_NONE; + uint16_t pan_id = 0; + GVariant *remote_node_list_va = NULL; GError *error = NULL; + uwb_network_s *network_s; - CHECK_FEATURE_SUPPORTED(UWB_FEATURE); - CHECK_NOT_INITIALIZED(); - - _BEGIN(); - - uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); - - if (manager_call_set_position_sync(uwb_ctx.manager_proxy, - ((uwb_node_s *)node)->node_id, x, y, z, NULL, &error) == FALSE) { - _ERR("manager_call_set_position_sync failed : %s", error->message); - __handle_error(error, &ret); - } - - _END(); - - return ret; -} - -static GVariant *__data_to_variant(const unsigned char *data, int length) -{ - GVariantBuilder builder; + if (manager_call_get_network_info_finish( + MANAGER(source_object), + &pan_id, + &remote_node_list_va, + res, + &error) == FALSE) { + _ERR("manager_call_get_network_info_finish failed : %s", error->message); + __handle_error(error, &ret); - g_variant_builder_init(&builder, G_VARIANT_TYPE("a(y)")); + if (uwb_ctx.get_network_finished_cb != NULL) { + uwb_ctx.get_network_finished_cb(ret, NULL, user_data); + } - if (data && length > 0) - { - for(int i = 0; i < length; i++) - g_variant_builder_add(&builder, "(y)", *(data + i)); + return; } - return g_variant_builder_end(&builder); -} + /* create network handle */ + network_s = (uwb_network_s *)malloc(sizeof(uwb_network_s)); -EXPORT_API int uwb_node_send_message(const unsigned char *message, int len) -{ - int ret = UWB_ERROR_NONE; - GVariant *msg = NULL; - GError *error = NULL; + if (ret == UWB_ERROR_NONE) { + GSList *remote_node_list = NULL; + network_s->pan_id = pan_id; + network_s->remote_node_count = g_variant_n_children(remote_node_list_va); - CHECK_FEATURE_SUPPORTED(UWB_FEATURE); - CHECK_NOT_INITIALIZED(); + if (network_s->remote_node_count > 0) { + GVariantIter *iter = NULL; + GVariant *nodes_va = NULL; - _BEGIN(); + g_variant_get(remote_node_list_va, "aa{sv}", &iter); + while ((nodes_va = g_variant_iter_next_value(iter)) != NULL) { + uwb_node_s *node; - uwb_check_null_ret_error("message", message, UWB_ERROR_INVALID_PARAMETER); - cond_expr_ret(len <= 0, UWB_ERROR_INVALID_PARAMETER); + node = uwb_util_get_node_from_variant(nodes_va); + node->is_remote = true; - msg = __data_to_variant(message, len); + _DBG("(%d, %d, %d)", node->x, node->y, node->z); + remote_node_list = g_slist_append(remote_node_list, node); + } + g_variant_iter_free(iter); + } + network_s->remote_node_list = remote_node_list; - if (manager_call_send_message_sync(uwb_ctx.manager_proxy, - msg, len, NULL, &error) == FALSE) { - _ERR("manager_call_send_message_sync failed : %s", error->message); - __handle_error(error, &ret); + g_variant_unref(remote_node_list_va); } - _END(); - - return ret; + if (uwb_ctx.get_network_finished_cb != NULL) { + uwb_ctx.get_network_finished_cb(ret, (uwb_network_h)network_s, user_data); + } } -EXPORT_API int uwb_node_send_message_to(uwb_node_h node, const unsigned char *message, int len) +EXPORT_API int uwb_get_network(uwb_get_network_finished_cb finished_cb, void *user_data) { int ret = UWB_ERROR_NONE; - GVariant *msg = NULL; - GError *error = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); CHECK_NOT_INITIALIZED(); _BEGIN(); - uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); - uwb_check_null_ret_error("message", message, UWB_ERROR_INVALID_PARAMETER); - cond_expr_ret(len <= 0, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("finished_cb", finished_cb, UWB_ERROR_INVALID_PARAMETER); - msg = __data_to_variant(message, len); + uwb_ctx.get_network_finished_cb = finished_cb; - if (manager_call_send_message_to_sync(uwb_ctx.manager_proxy, ((uwb_node_s *)node)->node_id, - msg, len, NULL, &error) == FALSE) { - _ERR("manager_call_send_message_to_sync failed : %s", error->message); - __handle_error(error, &ret); - } + manager_call_get_network_info(uwb_ctx.manager_proxy, NULL, __get_network_cb, user_data); _END(); return ret; } -static int __get_configurations(uwb_node_h node, char *key, - const GVariantType *va_type, GVariant **va_out) +static gpointer __copy_node(gconstpointer src, gpointer data) { - int ret = UWB_ERROR_NONE; - GError *error = NULL; - GVariant *va = NULL; + if (!src) + return NULL; - if (manager_call_get_configurations_sync(uwb_ctx.manager_proxy, - ((uwb_node_s *)node)->node_id, &va, NULL, &error) == FALSE) { - _ERR("__get_configurations failed : %s", error->message); - __handle_error(error, &ret); - } else { - *va_out = g_variant_lookup_value(va, key, va_type); - } + uwb_node_s *src_ptr = (uwb_node_s *)src; + uwb_node_s *dst_ptr = (uwb_node_s *)malloc(sizeof(uwb_node_s)); + if (!dst_ptr) + return NULL; - return ret; + dst_ptr->node_id = src_ptr->node_id; + dst_ptr->pan_id = src_ptr->pan_id; + dst_ptr->is_remote = src_ptr->is_remote; + dst_ptr->distance = src_ptr->distance; + dst_ptr->x = src_ptr->x; + dst_ptr->y = src_ptr->y; + dst_ptr->z = src_ptr->z; + + return (gpointer)dst_ptr; } -EXPORT_API int uwb_node_get_configuration_int32(uwb_node_h node, char *key, int32_t *value) +EXPORT_API int uwb_network_clone(uwb_network_h source, uwb_network_h *target) { + uwb_network_s *result_network = NULL; + uwb_network_s *src_network = (uwb_network_s *)source; + GSList *remote_node_list = NULL; int ret = UWB_ERROR_NONE; - GVariant *value_va = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); - CHECK_NOT_INITIALIZED(); _BEGIN(); - uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("source", source, UWB_ERROR_INVALID_PARAMETER); - ret = __get_configurations(node, key, G_VARIANT_TYPE_INT32, &value_va); + result_network = (uwb_network_s *)malloc(sizeof(uwb_network_s)); + if (result_network == NULL) { + _ERR("malloc failed"); + return UWB_ERROR_OPERATION_FAILED; + } - if (value_va != NULL) - *value = g_variant_get_int32(value_va); - else - *value = 0; + result_network->pan_id = src_network->pan_id; + result_network->remote_node_count = src_network->remote_node_count; + remote_node_list = src_network->remote_node_list; + result_network->remote_node_list = g_slist_copy_deep(remote_node_list, + __copy_node, + NULL); _END(); + *target = result_network; return ret; } -EXPORT_API int uwb_node_get_configuration_int64(uwb_node_h node, char *key, int64_t *value) +EXPORT_API int uwb_network_destroy(uwb_network_h network) { int ret = UWB_ERROR_NONE; - GVariant *value_va = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); - CHECK_NOT_INITIALIZED(); _BEGIN(); - uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); - - ret = __get_configurations(node, key, G_VARIANT_TYPE_INT64, &value_va); + uwb_check_null_ret_error("network", network, UWB_ERROR_INVALID_PARAMETER); - if (value_va != NULL) - *value = g_variant_get_int64(value_va); - else - *value = 0; + uwb_util_destroy_network(network); _END(); return ret; } -EXPORT_API int uwb_node_get_configuration_string(uwb_node_h node, char *key, const char **value) +EXPORT_API int uwb_network_get_pan_id(uwb_network_h network, uint64_t *pan_id) { int ret = UWB_ERROR_NONE; - GVariant *value_va = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); - CHECK_NOT_INITIALIZED(); _BEGIN(); - uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); - - ret = __get_configurations(node, key, G_VARIANT_TYPE_STRING, &value_va); + uwb_check_null_ret_error("network", network, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("pan_id", pan_id, UWB_ERROR_INVALID_PARAMETER); - if (value_va != NULL) - *value = g_variant_get_string(value_va, NULL); - else - *value = 0; + *pan_id = ((uwb_network_s *)network)->pan_id; _END(); return ret; } -static int __set_configuration(uwb_node_h node, char *key, GVariant *va) -{ - int ret = UWB_ERROR_NONE; - GError *error = NULL; - - if (manager_call_set_configurations_sync(uwb_ctx.manager_proxy, ((uwb_node_s *)node)->node_id, - va, NULL, &error) == FALSE) { - _ERR("__set_configurations : %s", error->message); - __handle_error(error, &ret); - } - - return ret; -} - -static GVariant *__build_configuration(char *key, GVariant *value) -{ - GVariant *va = NULL; - GVariantBuilder *builder; - - builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); - g_variant_builder_open(builder, G_VARIANT_TYPE_VARDICT); - g_variant_builder_add(builder, "{sv}", key, value); - g_variant_builder_close(builder); - va = g_variant_builder_end(builder); - g_variant_builder_unref(builder); - - return va; -} - -EXPORT_API int uwb_node_set_configuration_int32(uwb_node_h node, char *key, int32_t value) +EXPORT_API int uwb_network_get_remote_node_count(uwb_network_h network, int *remote_node_count) { int ret = UWB_ERROR_NONE; - GVariant *configuration = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); - CHECK_NOT_INITIALIZED(); _BEGIN(); - uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); - - configuration = __build_configuration(key, g_variant_new_int32(value)); + uwb_check_null_ret_error("network", network, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("remote_node_count", remote_node_count, UWB_ERROR_INVALID_PARAMETER); - ret = __set_configuration(node, key, configuration); + *remote_node_count = ((uwb_network_s *)network)->remote_node_count; _END(); return ret; } -EXPORT_API int uwb_node_set_configuration_int64(uwb_node_h node, char *key, int64_t value) +static void __foreach_node(gpointer data, gpointer user_data) { - int ret = UWB_ERROR_NONE; - GVariant *configuration = NULL; - - CHECK_FEATURE_SUPPORTED(UWB_FEATURE); - CHECK_NOT_INITIALIZED(); - - _BEGIN(); - - uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); - - configuration = __build_configuration(key, g_variant_new_int64(value)); - - ret = __set_configuration(node, key, configuration); - - _END(); + uwb_node_s *node = (uwb_node_s *)data; + _DBG("panid : 0x%X, nodeid : 0x%04"PRIX64", distance : %"PRId64"", node->pan_id, node->node_id, node->distance); - return ret; + if (uwb_ctx.foreach_remote_node_cb != NULL) + uwb_ctx.foreach_remote_node_cb((uwb_node_h)node, user_data); } -EXPORT_API int uwb_node_set_configuration_string(uwb_node_h node, char *key, const char *value) +EXPORT_API int uwb_network_foreach_remote_node(uwb_network_h network, + uwb_network_foreach_remote_node_cb node_cb, void *user_data) { int ret = UWB_ERROR_NONE; - GVariant *configuration = NULL; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); - CHECK_NOT_INITIALIZED(); _BEGIN(); - uwb_check_null_ret_error("node", node, UWB_ERROR_INVALID_PARAMETER); + uwb_check_null_ret_error("network", network, UWB_ERROR_INVALID_PARAMETER); - configuration = __build_configuration(key, g_variant_new_string(value)); + uwb_network_s *network_s = (uwb_network_s *)network; + if (network_s->remote_node_count == 0) { + _DBG("remote_node_count is 0"); + return ret; + } - ret = __set_configuration(node, key, configuration); + uwb_ctx.foreach_remote_node_cb = node_cb; + g_slist_foreach(network_s->remote_node_list, __foreach_node, user_data); _END(); return ret; } - diff --git a/tests/capi-network-uwb-test.c b/tests/capi-network-uwb-test.c index fad84f8..7fbd64d 100644 --- a/tests/capi-network-uwb-test.c +++ b/tests/capi-network-uwb-test.c @@ -430,12 +430,16 @@ void test_get_configuration(void) if (data_type == 0) { ret = uwb_node_get_configuration_int32(own_node, key, &int32_data); __print_result(ret, "uwb_node_get_configuration_int32"); + printf("Value [%d]\n", int32_data); } else if (data_type == 1) { ret = uwb_node_get_configuration_int64(own_node, key, &int64_data); __print_result(ret, "uwb_node_get_configuration_int64"); + printf("Value [%lld]\n", int64_data); } else if (data_type == 2) { ret = uwb_node_get_configuration_string(own_node, key, &string_data); __print_result(ret, "uwb_node_get_configuration_string"); + if (string_data) + printf("Value [%s]\n", string_data); } out: @@ -451,8 +455,8 @@ void test_set_configuration(void) uwb_node_h own_node = NULL; int data_type = 0; char *key; - int32_t int32_data; - int64_t int64_data; + int32_t int32_data = 2020; + int64_t int64_data = 2020; char *string_data = NULL; int ret = 0; @@ -575,6 +579,16 @@ static gboolean __test_terminal_read_std_input(GIOChannel * source, return TRUE; } +static void __glib_log( + const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *msg, + gpointer user_data) +{ + printf("[GLib Err] %s-0x%2.2X: %s\n", + log_domain, log_level, msg); +} + int main(int argc, char **argv) { @@ -582,6 +596,7 @@ int main(int argc, char **argv) g_type_init(); #endif + g_log_set_default_handler(__glib_log, NULL); g_main_loop_p = g_main_loop_new(NULL, FALSE); int std_input_fd = 0; -- 2.7.4 From b6ca152e556bb37c7c6f10ecc539d5e0d7cb2703 Mon Sep 17 00:00:00 2001 From: Jihoon Jung Date: Fri, 28 Aug 2020 15:29:53 +0900 Subject: [PATCH 08/10] Fix Svace issue : #446374 Change-Id: Ie0bac818fb3591d54e2d360c95022dd8a695b7a2 Signed-off-by: Jihoon Jung --- src/uwb.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/uwb.c b/src/uwb.c index adf2530..df50551 100755 --- a/src/uwb.c +++ b/src/uwb.c @@ -80,10 +80,15 @@ static void __message_received(GObject *source_object, unsigned char *buf = NULL; int size = 0; + buf = (unsigned char *)malloc(sizeof(unsigned char) * size + 1); + if (buf == NULL) { + _ERR("buf is NULL"); + return; + } + g_variant_get(message, "a(y)", &iter); size = g_variant_iter_n_children(iter); - buf = (unsigned char *)malloc(sizeof(unsigned char) * size + 1); for (int i = 0; g_variant_iter_loop(iter, "(y)", &element); i++) { buf[i] = element; -- 2.7.4 From c256bec35a28f392fd3b0c5e4dc09976c8399420 Mon Sep 17 00:00:00 2001 From: Jihoon Jung Date: Mon, 31 Aug 2020 08:35:31 +0900 Subject: [PATCH 09/10] Fix Svace issues #446512~446516 Change-Id: I96c1e7456abf89753d6b75808ca3260338e8bc7c Signed-off-by: Jihoon Jung --- tests/capi-network-uwb-test.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/capi-network-uwb-test.c b/tests/capi-network-uwb-test.c index 7fbd64d..e8207bb 100644 --- a/tests/capi-network-uwb-test.c +++ b/tests/capi-network-uwb-test.c @@ -371,6 +371,9 @@ void test_set_position(void) ret = uwb_node_set_position(own_node, test_x, test_y, test_z); __print_result(ret, "uwb_node_set_position"); + + ret = uwb_node_destroy(own_node); + __print_result(ret, "uwb_node_destroy"); } void test_send_message(void) @@ -384,6 +387,9 @@ void test_send_message(void) ret = uwb_node_send_message((const unsigned char *)test_string, strlen(test_string)); __print_result(ret, "uwb_node_send_message"); + + ret = uwb_node_destroy(own_node); + __print_result(ret, "uwb_node_destroy"); } void test_send_message_to(void) @@ -397,6 +403,9 @@ void test_send_message_to(void) ret = uwb_node_send_message_to(own_node, (const unsigned char *)test_string, strlen(test_string)); __print_result(ret, "uwb_node_send_message"); + + ret = uwb_node_destroy(own_node); + __print_result(ret, "uwb_node_destroy"); } void test_get_configuration(void) @@ -486,8 +495,12 @@ void test_set_configuration(void) __print_result(ret, "uwb_node_set_configuration_int64"); } else if (data_type == 2) { printf("Input string :\n"); - if (scanf(" %255ms", &string_data) < 1) + if (scanf(" %255ms", &string_data) < 1) { + ret = uwb_node_destroy(own_node); + __print_result(ret, "uwb_node_destroy"); + return; + } ret = uwb_node_set_configuration_string(own_node, key, string_data); __print_result(ret, "uwb_node_set_configuration_string"); } -- 2.7.4 From 1a498e26da178577f1ed6c565406fa0ee4d40a81 Mon Sep 17 00:00:00 2001 From: Jihoon Jung Date: Tue, 1 Sep 2020 15:48:17 +0900 Subject: [PATCH 10/10] Add gtest for coverage and auto test Change-Id: I82eb9117e1fe4a7831fa591ea62e2acc992b8039 Signed-off-by: Jihoon Jung --- CMakeLists.txt | 5 +- packaging/capi-network-uwb.spec | 7 +- src/CMakeLists.txt | 1 + src/uwb-util.c | 4 + src/uwb.c | 36 ++ tests/CMakeLists.txt | 38 +- tests/capi-network-uwb-gtest.cpp | 872 +++++++++++++++++++++++++++++++++++++++ tests/capi-network-uwb-test.c | 6 +- tests/mocks/uwb_mock.c | 258 ++++++++++++ 9 files changed, 1218 insertions(+), 9 deletions(-) create mode 100755 tests/capi-network-uwb-gtest.cpp create mode 100755 tests/mocks/uwb_mock.c diff --git a/CMakeLists.txt b/CMakeLists.txt index b430e62..24c177f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,12 +27,13 @@ INCLUDE(FindPkgConfig) SET(COMMON_DEPS "dlog glib-2.0 gio-2.0 gio-unix-2.0 capi-system-info") SET(PC_DEPS "capi-base-common") +SET(GTEST_DEPS "capi-base-common dlog capi-system-info glib-2.0 gio-unix-2.0 gmock") SET(TEST_DEPS "glib-2.0") -SET(GTEST_DEPS "gmock") SET(TARGET_UWB "capi-network-uwb") -SET(TARGET_UWB_TEST "capi-network-uwb-test") +SET(TARGET_UWB_MOCK "uwb-mock") SET(TARGET_UWB_GTEST "capi-network-uwb-gtest") +SET(TARGET_UWB_TEST "capi-network-uwb-test") ADD_DEFINITIONS("-DUSE_DLOG") diff --git a/packaging/capi-network-uwb.spec b/packaging/capi-network-uwb.spec index 25a9794..56ae13f 100644 --- a/packaging/capi-network-uwb.spec +++ b/packaging/capi-network-uwb.spec @@ -92,9 +92,10 @@ tar xf %{name}-gcov.tar -C %{buildroot}%{_datadir}/gcov/obj %endif %check -#tests/%{name}-gtest %if 0%{?gcov:1} -lcov -c --ignore-errors graph --no-external -b . -d . -o %{name}.info +LD_PRELOAD=./tests/libuwb-mock.so tests/%{name}-gtest +lcov -c --ignore-errors graph --no-external -b . -d . \ +--exclude "/home/abuild/rpmbuild/BUILD/%{name}-%{version}/src/uwb-gdbus.c" -o %{name}.info genhtml %{name}.info -o out --legend --show-details %endif @@ -117,7 +118,7 @@ genhtml %{name}.info -o out --legend --show-details %files tests %{_bindir}/%{name}-test -#%{_bindir}/%{name}-gtest +%{_bindir}/%{name}-gtest %if 0%{?gcov:1} %files gcov diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3c99212..cc9a8b6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,6 +6,7 @@ PKG_CHECK_MODULES(UWB_DEPS REQUIRED ${COMMON_DEPS}) SET(EXTRA_FLAGS "-fPIC -Wall -Werror -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_FLAGS}") +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_FLAGS}") SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread -pie") ######################## directory configuration ############################ diff --git a/src/uwb-util.c b/src/uwb-util.c index 96f52d1..6a901e7 100755 --- a/src/uwb-util.c +++ b/src/uwb-util.c @@ -20,6 +20,7 @@ #include #include +/* LCOV_EXCL_START */ void _node_free_func(gpointer data) { uwb_node_s *node = (uwb_node_s *)data; @@ -27,6 +28,7 @@ void _node_free_func(gpointer data) free(node); node = NULL; } +/* LCOV_EXCL_STOP */ void uwb_util_destroy_network(uwb_network_s *network) { @@ -47,8 +49,10 @@ uwb_node_s *uwb_util_get_node_from_variant(GVariant *va) uwb_node_s *node = calloc(1, sizeof(uwb_node_s)); if (NULL == node) { + /* LCOV_EXCL_START */ _ERR("Memory allocation failed"); return NULL; + /* LCOV_EXCL_STOP */ } g_variant_get(va, "a{sv}", &iter); diff --git a/src/uwb.c b/src/uwb.c index 752d305..c4bf9ec 100755 --- a/src/uwb.c +++ b/src/uwb.c @@ -41,6 +41,7 @@ static struct _uwb_ctx { uwb_network_foreach_remote_node_cb foreach_remote_node_cb; } uwb_ctx = {NULL,}; +/* LCOV_EXCL_START */ static inline void __handle_error(GError *error, int *ret) { if (!error) @@ -111,6 +112,7 @@ static void __position_changed(GObject *source_object, if (uwb_ctx.position_changed_cb != NULL) uwb_ctx.position_changed_cb(node_id, x, y, z, uwb_ctx.position_changed_user_data); } +/* LCOV_EXCL_STOP */ static int manager_proxy_init(void) { @@ -191,8 +193,10 @@ EXPORT_API int uwb_reset(void) _BEGIN(); if (manager_call_reset_sync(uwb_ctx.manager_proxy, NULL, &error) == FALSE) { + /* LCOV_EXCL_START */ _ERR("manager_call_reset_sync failed : %s", error->message); __handle_error(error, &ret); + /* LCOV_EXCL_STOP */ } _END(); @@ -211,8 +215,10 @@ EXPORT_API int uwb_factory_reset(void) _BEGIN(); if (manager_call_factory_reset_sync(uwb_ctx.manager_proxy, NULL, &error) == FALSE) { + /* LCOV_EXCL_START */ _ERR("manager_call_factory_reset_sync failed : %s", error->message); __handle_error(error, &ret); + /* LCOV_EXCL_STOP */ } _END(); @@ -225,6 +231,7 @@ EXPORT_API int uwb_set_message_received_cb(uwb_message_received_cb message_recei int ret = UWB_ERROR_NONE; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + uwb_check_null_ret_error("message_received_cb", message_received_cb, UWB_ERROR_INVALID_PARAMETER); _BEGIN(); @@ -241,6 +248,7 @@ EXPORT_API int uwb_set_position_changed_cb(uwb_position_changed_cb position_chan int ret = UWB_ERROR_NONE; CHECK_FEATURE_SUPPORTED(UWB_FEATURE); + uwb_check_null_ret_error("position_changed_cb", position_changed_cb, UWB_ERROR_INVALID_PARAMETER); _BEGIN(); @@ -300,13 +308,17 @@ EXPORT_API int uwb_get_own_node(uwb_node_h *own_node) if (manager_call_get_own_node_sync(uwb_ctx.manager_proxy, &own_node_va, NULL, &error) == FALSE) { + /* LCOV_EXCL_START */ __handle_error(error, &ret); + /* LCOV_EXCL_STOP */ } + /* LCOV_EXCL_START */ if (ret == UWB_ERROR_NONE && own_node_va != NULL) *_own_node = uwb_util_get_node_from_variant(own_node_va); else *_own_node = NULL; + /* LCOV_EXCL_STOP */ _END(); @@ -327,8 +339,10 @@ EXPORT_API int uwb_node_set_position(uwb_node_h node, int x, int y, int z) if (manager_call_set_position_sync(uwb_ctx.manager_proxy, ((uwb_node_s *)node)->node_id, x, y, z, NULL, &error) == FALSE) { + /* LCOV_EXCL_START */ _ERR("manager_call_set_position_sync failed : %s", error->message); __handle_error(error, &ret); + /* LCOV_EXCL_STOP */ } _END(); @@ -369,8 +383,10 @@ EXPORT_API int uwb_node_send_message(const unsigned char *message, int len) if (manager_call_send_message_sync(uwb_ctx.manager_proxy, msg, len, NULL, &error) == FALSE) { + /* LCOV_EXCL_START */ _ERR("manager_call_send_message_sync failed : %s", error->message); __handle_error(error, &ret); + /* LCOV_EXCL_STOP */ } _END(); @@ -397,8 +413,10 @@ EXPORT_API int uwb_node_send_message_to(uwb_node_h node, const unsigned char *me if (manager_call_send_message_to_sync(uwb_ctx.manager_proxy, ((uwb_node_s *)node)->node_id, msg, len, NULL, &error) == FALSE) { + /* LCOV_EXCL_START */ _ERR("manager_call_send_message_to_sync failed : %s", error->message); __handle_error(error, &ret); + /* LCOV_EXCL_STOP */ } _END(); @@ -415,8 +433,10 @@ static int __get_configurations(uwb_node_h node, const char *key, if (manager_call_get_configurations_sync(uwb_ctx.manager_proxy, ((uwb_node_s *)node)->node_id, &va, NULL, &error) == FALSE) { + /* LCOV_EXCL_START */ _ERR("__get_configurations failed : %s", error->message); __handle_error(error, &ret); + /* LCOV_EXCL_STOP */ } else { *va_out = g_variant_lookup_value(va, key, va_type); } @@ -438,10 +458,12 @@ EXPORT_API int uwb_node_get_configuration_int32(uwb_node_h node, const char *key ret = __get_configurations(node, key, G_VARIANT_TYPE_INT32, &value_va); + /* LCOV_EXCL_START */ if (value_va != NULL) *value = g_variant_get_int32(value_va); else *value = 0; + /* LCOV_EXCL_STOP */ _END(); @@ -462,10 +484,12 @@ EXPORT_API int uwb_node_get_configuration_int64(uwb_node_h node, const char *key ret = __get_configurations(node, key, G_VARIANT_TYPE_INT64, &value_va); + /* LCOV_EXCL_START */ if (value_va != NULL) *value = g_variant_get_int64(value_va); else *value = 0; + /* LCOV_EXCL_STOP */ _END(); @@ -506,8 +530,10 @@ static int __set_configuration(uwb_node_h node, GVariant *va) if (manager_call_set_configurations_sync(uwb_ctx.manager_proxy, ((uwb_node_s *)node)->node_id, va, NULL, &error) == FALSE) { + /* LCOV_EXCL_START */ _ERR("__set_configurations : %s", error->message); __handle_error(error, &ret); + /* LCOV_EXCL_STOP */ } return ret; @@ -746,6 +772,7 @@ static void __get_network_cb(GObject *source_object, GAsyncResult *res, gpointer &remote_node_list_va, res, &error) == FALSE) { + /* LCOV_EXCL_START */ _ERR("manager_call_get_network_info_finish failed : %s", error->message); __handle_error(error, &ret); @@ -754,6 +781,7 @@ static void __get_network_cb(GObject *source_object, GAsyncResult *res, gpointer } return; + /* LCOV_EXCL_STOP */ } /* create network handle */ @@ -765,6 +793,7 @@ static void __get_network_cb(GObject *source_object, GAsyncResult *res, gpointer network_s->remote_node_count = g_variant_n_children(remote_node_list_va); if (network_s->remote_node_count > 0) { + /* LCOV_EXCL_START */ GVariantIter *iter = NULL; GVariant *nodes_va = NULL; @@ -779,6 +808,7 @@ static void __get_network_cb(GObject *source_object, GAsyncResult *res, gpointer remote_node_list = g_slist_append(remote_node_list, node); } g_variant_iter_free(iter); + /* LCOV_EXCL_STOP */ } network_s->remote_node_list = remote_node_list; @@ -810,6 +840,7 @@ EXPORT_API int uwb_get_network(uwb_get_network_finished_cb finished_cb, void *us return ret; } +/* LCOV_EXCL_START */ static gpointer __copy_node(gconstpointer src, gpointer data) { if (!src) @@ -830,6 +861,7 @@ static gpointer __copy_node(gconstpointer src, gpointer data) return (gpointer)dst_ptr; } +/* LCOV_EXCL_STOP */ EXPORT_API int uwb_network_clone(uwb_network_h source, uwb_network_h *target) { @@ -846,8 +878,10 @@ EXPORT_API int uwb_network_clone(uwb_network_h source, uwb_network_h *target) result_network = (uwb_network_s *)malloc(sizeof(uwb_network_s)); if (result_network == NULL) { + /* LCOV_EXCL_START */ _ERR("malloc failed"); return UWB_ERROR_OPERATION_FAILED; + /* LCOV_EXCL_STOP */ } result_network->pan_id = src_network->pan_id; @@ -916,6 +950,7 @@ EXPORT_API int uwb_network_get_remote_node_count(uwb_network_h network, int *rem return ret; } +/* LCOV_EXCL_START */ static void __foreach_node(gpointer data, gpointer user_data) { uwb_node_s *node = (uwb_node_s *)data; @@ -949,3 +984,4 @@ EXPORT_API int uwb_network_foreach_remote_node(uwb_network_h network, return ret; } +/* LCOV_EXCL_STOP */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index da32239..f7e6703 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,6 +1,42 @@ ########################## search for packages ################################ -PKG_CHECK_MODULES(TARGET_UWB_TEST_REQ_PKGS REQUIRED glib-2.0) +PKG_CHECK_MODULES(TARGET_UWB_GTEST_REQ_PKGS REQUIRED ${GTEST_DEPS}) + +############################# compiler flags ################################## + +SET(EXTRA_FLAGS "-Wall -Werror") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} ${CFLAGS} -fPIE -fPIC") +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CXXFLAGS} ${CXXFLAGS} -fPIE -fPIC") +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") + +######################## directory configuration ############################ + +INCLUDE_DIRECTORIES(${TARGET_UWB_GTEST_REQ_PKGS_INCLUDE_DIRS}) +LINK_DIRECTORIES(${TARGET_UWB_GTEST_REQ_PKGS_LIBRARY_DIRS}) + +INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include) + +SET(TARGET_UWB_MOCK_SRCS + mocks/uwb_mock.c) +SET(TARGET_UWB_MOCK_CFLAGS "${CMAKE_C_FLAGS}" ) +ADD_LIBRARY(${TARGET_UWB_MOCK} SHARED ${TARGET_UWB_MOCK_SRCS}) +SET_TARGET_PROPERTIES(${TARGET_UWB_MOCK} PROPERTIES + COMPILE_FLAGS ${TARGET_UWB_MOCK_CFLAGS} +) + +# Build +ADD_EXECUTABLE(${TARGET_UWB_GTEST} + ${CMAKE_CURRENT_SOURCE_DIR}/capi-network-uwb-gtest.cpp +) + +TARGET_LINK_LIBRARIES(${TARGET_UWB_GTEST} + ${TARGET_UWB_GTEST_REQ_PKGS_LIBRARIES} ${PROJECT_NAME}) + +#Install +INSTALL(PROGRAMS ${TARGET_UWB_GTEST} DESTINATION ${BIN_DIR}) +########################## search for packages ################################ + +PKG_CHECK_MODULES(TARGET_UWB_TEST_REQ_PKGS REQUIRED ${TEST_DEPS}) ############################# compiler flags ################################## diff --git a/tests/capi-network-uwb-gtest.cpp b/tests/capi-network-uwb-gtest.cpp new file mode 100755 index 0000000..69613cd --- /dev/null +++ b/tests/capi-network-uwb-gtest.cpp @@ -0,0 +1,872 @@ +/* + * gtest-uwb.cpp + * + * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jihoon Jung + * + * 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. + * + */ + +/***************************************************************************** + * Standard headers + *****************************************************************************/ + +/***************************************************************************** + * System headers + *****************************************************************************/ +#include +#include +/***************************************************************************** + * Local headers + *****************************************************************************/ +#include + +/***************************************************************************** + * Macros and Typedefs + *****************************************************************************/ +/***************************************************************************** + * Global Variables + *****************************************************************************/ +/***************************************************************************** + * Local Functions Definition + *****************************************************************************/ + +class UwbManagerTest : public ::testing::Test { +protected: + void SetUp() override + { + uwb_initialize(); + } + + void TearDown() override + { + uwb_deinitialize(); + } +}; + +TEST_F(UwbManagerTest, uwb_initialize_Positive) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_deinitialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); + + ret = uwb_initialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbManagerTest, uwb_initialize_Negative) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_initialize(); + ASSERT_EQ(UWB_ERROR_ALREADY_INITIALIZED, ret); +} + +TEST_F(UwbManagerTest, uwb_deinitialize_Positive) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_deinitialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); + + ret = uwb_initialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbManagerTest, uwb_deinitialize_Negative) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_deinitialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); + + ret = uwb_deinitialize(); + ASSERT_EQ(UWB_ERROR_NOT_INITIALIZED, ret); + + ret = uwb_initialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbManagerTest, uwb_reset_Positive) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_reset(); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbManagerTest, uwb_reset_Negative) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_deinitialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); + + ret = uwb_reset(); + EXPECT_EQ(UWB_ERROR_NOT_INITIALIZED, ret); + + ret = uwb_initialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbManagerTest, uwb_factory_reset_Positive) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_factory_reset(); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbManagerTest, uwb_factory_reset_Negative) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_deinitialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); + + ret = uwb_factory_reset(); + EXPECT_EQ(UWB_ERROR_NOT_INITIALIZED, ret); + + ret = uwb_initialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +void __uwb_message_received_cb(uint64_t node_id, const unsigned char *message, + int message_length, void *user_data) +{ + +} + +TEST_F(UwbManagerTest, uwb_set_message_received_cb_Positive) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_set_message_received_cb(__uwb_message_received_cb, NULL); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbManagerTest, uwb_set_message_received_cb_Negative) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_set_message_received_cb(NULL, NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +void __uwb_position_changed_cb(uint64_t node_id, int x, int y, int z, + void *user_data) +{ + +} + +TEST_F(UwbManagerTest, uwb_set_position_changed_cb_Positive) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_set_position_changed_cb(__uwb_position_changed_cb, NULL); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbManagerTest, uwb_set_position_changed_cb_Negative) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_set_position_changed_cb(NULL, NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbManagerTest, uwb_unset_message_received_cb_Positive) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_unset_message_received_cb(); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbManagerTest, uwb_unset_position_changed_cb_Positive) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_unset_position_changed_cb(); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbManagerTest, uwb_get_own_node_Positive) +{ + int ret = UWB_ERROR_NONE; + uwb_node_h own_node = NULL; + + ret = uwb_get_own_node(&own_node); + + if (own_node != NULL) + uwb_node_destroy(own_node); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbManagerTest, uwb_get_own_node_Negative1) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_deinitialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); + + ret = uwb_get_own_node(NULL); + EXPECT_EQ(UWB_ERROR_NOT_INITIALIZED, ret); + + ret = uwb_initialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbManagerTest, uwb_get_own_node_Negative2) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_get_own_node(NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +GMainLoop *tc_loop; +uwb_network_h _network; + +void __uwb_network_get_finished_cb(int result, uwb_network_h network, void *user_data) +{ + uwb_network_clone(network, &_network); + + uwb_network_destroy(network); + + g_main_loop_quit(tc_loop); + tc_loop = NULL; +} + +class UwbNetworkTest : public ::testing::Test { +protected: + void SetUp() override + { + uwb_initialize(); + + tc_loop = g_main_loop_new(NULL, FALSE); + + uwb_get_network(__uwb_network_get_finished_cb, NULL); + + g_main_loop_run(tc_loop); + } + + void TearDown() override + { + _network = NULL; + + uwb_deinitialize(); + } + +}; + +GMainLoop *tc_loop2; + +void __uwb_network_get_finished_cb2(int result, uwb_network_h network, void *user_data) +{ + g_main_loop_quit(tc_loop2); +} + +TEST_F(UwbNetworkTest, uwb_network_get_Positive) +{ + int ret = UWB_ERROR_NONE; + + tc_loop2 = g_main_loop_new(NULL, FALSE); + + ret = uwb_get_network(__uwb_network_get_finished_cb2, NULL); + + g_main_loop_run(tc_loop2); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNetworkTest, uwb_network_get_Negative1) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_deinitialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); + + ret = uwb_get_network(NULL, NULL); + EXPECT_EQ(UWB_ERROR_NOT_INITIALIZED, ret); + + ret = uwb_initialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNetworkTest, uwb_network_get_Negative2) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_get_network(NULL, NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbNetworkTest, uwb_network_clone_Positive) +{ + int ret = UWB_ERROR_NONE; + + uwb_node_h network = NULL; + + ret = uwb_network_clone(_network, &network); + ASSERT_TRUE(network != NULL); + + uwb_network_destroy(network); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNetworkTest, uwb_network_clone_Negative) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_network_clone(NULL, NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbNetworkTest, uwb_network_destroy_Positive) +{ + int ret = UWB_ERROR_NONE; + + uwb_node_h network = NULL; + + uwb_network_clone(_network, &network); + ASSERT_TRUE(network != NULL); + + ret = uwb_network_destroy(network); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNetworkTest, uwb_network_destroy_Negative) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_network_destroy(NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbNetworkTest, uwb_network_get_pan_id_Positive) +{ + int ret = UWB_ERROR_NONE; + uint64_t pan_id; + + ret = uwb_network_get_pan_id(_network, &pan_id); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNetworkTest, uwb_network_get_pan_id_Negative) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_network_get_pan_id(NULL, NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbNetworkTest, uwb_network_get_remote_node_count_Positive) +{ + int ret = UWB_ERROR_NONE; + int remote_node_count = 0; + + ret = uwb_network_get_remote_node_count(_network, &remote_node_count); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNetworkTest, uwb_network_get_remote_node_count_Negative) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_network_get_remote_node_count(NULL, NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +bool __uwb_network_foreach_remote_node_cb(uwb_node_h remote_node, void *user_data) +{ + return true; +} + +TEST_F(UwbNetworkTest, uwb_network_foreach_remote_node_Positive) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_network_foreach_remote_node(_network, __uwb_network_foreach_remote_node_cb, NULL); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNetworkTest, uwb_network_foreach_remote_node_Negative) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_network_foreach_remote_node(NULL, NULL, NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +class UwbNodeTest : public ::testing::Test { +protected: + void SetUp() override + { + uwb_initialize(); + + uwb_get_own_node(&_node); + + strncpy(_key_int32, "TEMP", sizeof(_key_int32)); + strncpy(_key_int64, "NETWORK_TIMEOUT", sizeof(_key_int64)); + strncpy(_key_string, "MODE", sizeof(_key_string)); + } + + void TearDown() override + { + uwb_deinitialize(); + } + + char _key_int32[1024] = {0,}; + char _key_int64[1024] = {0,}; + char _key_string[1024] = {0,}; + uwb_node_h _node; +}; + +TEST_F(UwbNodeTest, uwb_node_clone_Positive) +{ + int ret = UWB_ERROR_NONE; + uwb_node_h node = NULL; + + ret = uwb_node_clone(_node, &node); + ASSERT_TRUE(node != NULL); + + uwb_node_destroy(node); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_clone_Negative) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_node_clone(NULL, NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbNodeTest, uwb_node_destroy_Positive) +{ + int ret = UWB_ERROR_NONE; + uwb_node_h node = NULL; + + uwb_get_own_node(&node); + ASSERT_TRUE(node != NULL); + + ret = uwb_node_destroy(node); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_destroy_Negative) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_node_destroy(NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbNodeTest, uwb_node_get_distance_Positive) +{ + int ret = UWB_ERROR_NONE; + uint64_t distance; + + ret = uwb_node_get_distance(_node, &distance); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_get_distance_Negative) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_node_get_distance(NULL, NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbNodeTest, uwb_node_get_node_id_Positive) +{ + int ret = UWB_ERROR_NONE; + uint64_t node_id = 0; + + ret = uwb_node_get_node_id(_node, &node_id); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_get_node_id_Negative) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_node_get_node_id(NULL, NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbNodeTest, uwb_node_get_pan_id_Positive) +{ + int ret = UWB_ERROR_NONE; + uint64_t pan_id; + + ret = uwb_node_get_pan_id(_node, &pan_id); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_get_pan_id_Negative) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_node_get_pan_id(NULL, NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbNodeTest, uwb_node_get_is_remote_Positive) +{ + int ret = UWB_ERROR_NONE; + bool is_remote = false; + + ret = uwb_node_get_is_remote(_node, &is_remote); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_get_is_remote_Negative) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_node_get_is_remote(NULL, NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbNodeTest, uwb_node_get_position_Positive) +{ + int ret = UWB_ERROR_NONE; + int x = 0; + int y = 0; + int z = 0; + + ret = uwb_node_get_position(_node, &x, &y, &z); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_get_position_Negative) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_node_get_position(NULL, NULL, NULL, NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbNodeTest, uwb_node_set_position_Positive) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_node_set_position(_node, 1, 2, 3); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_set_position_Negative1) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_deinitialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); + + ret = uwb_node_set_position(NULL, 0, 0, 0); + EXPECT_EQ(UWB_ERROR_NOT_INITIALIZED, ret); + + ret = uwb_initialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_set_position_Negative2) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_node_set_position(NULL, 0, 0, 0); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbNodeTest, uwb_node_send_message_Positive) +{ + int ret = UWB_ERROR_NONE; + const unsigned char message[1024] = {0x00, 0x01, 0x02}; + + ret = uwb_node_send_message(message, sizeof(message)); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_send_message_Negative1) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_deinitialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); + + ret = uwb_node_send_message(NULL, 0); + EXPECT_EQ(UWB_ERROR_NOT_INITIALIZED, ret); + + ret = uwb_initialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_send_message_Negative2) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_node_send_message(NULL, 0); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbNodeTest, uwb_node_send_message_to_Positive) +{ + int ret = UWB_ERROR_NONE; + const unsigned char message[1024] = {0x00, 0x01, 0x02}; + + ret = uwb_node_send_message_to(_node, message, sizeof(message)); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_send_message_to_Negative1) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_deinitialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); + + ret = uwb_node_send_message_to(NULL, NULL, 0); + EXPECT_EQ(UWB_ERROR_NOT_INITIALIZED, ret); + + ret = uwb_initialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_send_message_to_Negative2) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_node_send_message_to(NULL, NULL, 0); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbNodeTest, uwb_node_get_configuration_int32_Positive) +{ + int ret = UWB_ERROR_NONE; + + int value; + + ret = uwb_node_get_configuration_int32(_node, _key_int32, &value); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_get_configuration_int32_Negative1) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_deinitialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); + + ret = uwb_node_get_configuration_int32(NULL, NULL, NULL); + EXPECT_EQ(UWB_ERROR_NOT_INITIALIZED, ret); + + ret = uwb_initialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_get_configuration_int32_Negative2) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_node_get_configuration_int32(NULL, NULL, NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbNodeTest, uwb_node_set_configuration_int32_Positive) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_node_set_configuration_int32(_node, _key_int32, 0); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_set_configuration_int32_Negative1) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_deinitialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); + + ret = uwb_node_set_configuration_int32(NULL, NULL, 0); + EXPECT_EQ(UWB_ERROR_NOT_INITIALIZED, ret); + + ret = uwb_initialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_set_configuration_int32_Negative2) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_node_set_configuration_int32(NULL, NULL, 0); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbNodeTest, uwb_node_get_configuration_int64_Positive) +{ + int ret = UWB_ERROR_NONE; + int64_t value = 0; + + ret = uwb_node_get_configuration_int64(_node, _key_int64, &value); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_get_configuration_int64_Negative1) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_deinitialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); + + ret = uwb_node_get_configuration_int64(NULL, NULL, NULL); + EXPECT_EQ(UWB_ERROR_NOT_INITIALIZED, ret); + + ret = uwb_initialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_get_configuration_int64_Negative2) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_node_get_configuration_int64(NULL, NULL, NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbNodeTest, uwb_node_set_configuration_int64_Positive) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_node_set_configuration_int64(_node, _key_int64, 0); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_set_configuration_int64_Negative1) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_deinitialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); + + ret = uwb_node_set_configuration_int64(NULL, NULL, 0); + EXPECT_EQ(UWB_ERROR_NOT_INITIALIZED, ret); + + ret = uwb_initialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_set_configuration_int64_Negative2) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_node_set_configuration_int64(NULL, NULL, 0); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbNodeTest, uwb_node_get_configuration_string_Positive) +{ + int ret = UWB_ERROR_NONE; + char *value = NULL; + + ret = uwb_node_get_configuration_string(_node, _key_string, &value); + + if (value != NULL) + free(value); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_get_configuration_string_Negative1) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_deinitialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); + + ret = uwb_node_get_configuration_string(NULL, NULL, NULL); + EXPECT_EQ(UWB_ERROR_NOT_INITIALIZED, ret); + + ret = uwb_initialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_get_configuration_string_Negative2) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_node_get_configuration_string(NULL, NULL, NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +TEST_F(UwbNodeTest, uwb_node_set_configuration_string_Positive) +{ + int ret = UWB_ERROR_NONE; + char conf_str[1024] = {0,}; + + strncpy(conf_str, "HELLO", sizeof(conf_str)); + + ret = uwb_node_set_configuration_string(_node, _key_string, (const char *)conf_str); + + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_set_configuration_string_Negative1) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_deinitialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); + + ret = uwb_node_set_configuration_string(NULL, NULL, NULL); + EXPECT_EQ(UWB_ERROR_NOT_INITIALIZED, ret); + + ret = uwb_initialize(); + ASSERT_EQ(UWB_ERROR_NONE, ret); +} + +TEST_F(UwbNodeTest, uwb_node_set_configuration_string_Negative2) +{ + int ret = UWB_ERROR_NONE; + + ret = uwb_node_set_configuration_string(NULL, NULL, NULL); + ASSERT_EQ(UWB_ERROR_INVALID_PARAMETER, ret); +} + +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/tests/capi-network-uwb-test.c b/tests/capi-network-uwb-test.c index e8207bb..4c6b47a 100644 --- a/tests/capi-network-uwb-test.c +++ b/tests/capi-network-uwb-test.c @@ -360,9 +360,9 @@ void test_set_position(void) { uwb_node_h own_node = NULL; int ret = 0; - int test_x = 10; - int test_y = 10; - int test_z = 10; + int test_x = 1000; + int test_y = 1000; + int test_z = 1000; RET_IF_LOOP_IS_NULL(); ret = uwb_get_own_node(&own_node); diff --git a/tests/mocks/uwb_mock.c b/tests/mocks/uwb_mock.c new file mode 100755 index 0000000..d1073be --- /dev/null +++ b/tests/mocks/uwb_mock.c @@ -0,0 +1,258 @@ +// +// Copyright (c) 2020 Samsung Electronics Co., Ltd. +// +// 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 +#include +#include +#include + +#include +#include + +#ifndef API +#define API __attribute__ ((visibility("default"))) +#endif + +typedef enum { + LOG_ID_INVALID = -1, + LOG_ID_MAIN, + LOG_ID_RADIO, + LOG_ID_SYSTEM, + LOG_ID_APPS, + LOG_ID_KMSG, + LOG_ID_SYSLOG, + LOG_ID_MAX +} log_id_t; + +API int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + printf("\n"); + + return 0; +} + +API int system_info_get_platform_bool(const char *key, bool *value) +{ + *value = true; + return 0; +} + +API gulong g_signal_connect_data(gpointer instance, + const gchar *detailed_signal, + GCallback c_handler, + gpointer data, + GClosureNotify destroy_data, + GConnectFlags connect_flags) +{ + return 0; +} + +API GVariant *g_variant_new(const gchar *format_string, ...) +{ + return (void *)1234; +} + +API gsize g_variant_iter_init(GVariantIter *iter, GVariant *value) +{ + return 0; +} + +API void g_object_unref(gpointer _object) +{ + return; +} + +API gpointer g_initable_new(GType object_type, GCancellable *cancellable, + GError **error, const gchar *first_property_name, ...) +{ + return (void *)1234; +} + +API GTypeInstance *g_type_check_instance_cast(GTypeInstance *type_instance, + GType iface_type) +{ + return (void *)1234; +} + +API GVariant *g_dbus_proxy_call_sync(GDBusProxy *proxy, const gchar *method_name, + GVariant *parameters, GDBusCallFlags flags, gint timeout_msec, GCancellable *cancellable, + GError **error) +{ + return (void *)1234; +} + +API void g_variant_unref (GVariant *value) +{ + return; +} + +GAsyncReadyCallback _callback; + +gboolean __get_network_info_cb(gpointer user_data) +{ + _callback(NULL, NULL, user_data); + + return FALSE; +} + +API void g_dbus_proxy_call (GDBusProxy *proxy, const gchar *method_name, GVariant *parameters, + GDBusCallFlags flags, gint timeout_msec, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) +{ + _callback = callback; + + if (0 == strncmp(method_name, "GetNetworkInfo", strlen(method_name))) { + g_timeout_add_seconds(1, __get_network_info_cb, user_data); + } + + return; +} + +API GVariant *g_dbus_proxy_call_finish(GDBusProxy *proxy, GAsyncResult *res, GError **error) +{ + return (void *)1234; +} + +static GVariant *__create_dummy() +{ + GVariant *va = NULL; + GVariantBuilder *builder = NULL; + + builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}")); + va = g_variant_builder_end(builder); + g_variant_builder_unref(builder); + + return va; +} + +static GVariant *__create_vardict() +{ + GVariant *va = NULL; + GVariantBuilder *builder = NULL; + builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); + g_variant_builder_add(builder, "{sv}", "Distance", g_variant_new_uint64(1)); + g_variant_builder_add(builder, "{sv}", "NodeID", g_variant_new_uint64(2)); + g_variant_builder_add(builder, "{sv}", "PanID", g_variant_new_uint16(3)); + g_variant_builder_add(builder, "{sv}", "X", g_variant_new_int32(4)); + g_variant_builder_add(builder, "{sv}", "Y", g_variant_new_int32(5)); + g_variant_builder_add(builder, "{sv}", "Z", g_variant_new_int32(6)); + + g_variant_builder_add(builder, "{sv}", "MODE", g_variant_new_string("ANCHOR")); + g_variant_builder_add(builder, "{sv}", "NETWORK_TIMEOUT", g_variant_new_int64(1)); + g_variant_builder_add(builder, "{sv}", "HARDWARE_WAIT_TIME", g_variant_new_int64(1)); + g_variant_builder_add(builder, "{sv}", "PANID", g_variant_new_int64(1)); + + va = g_variant_builder_end(builder); + g_variant_builder_unref(builder); + + return va; +} + +int state; +API gboolean g_variant_iter_loop(GVariantIter *iter, const gchar *format_string, ...) +{ + va_list ap; + const gchar **key = NULL; + char _key[1024] = {0,}; + GVariant **value = NULL; + bool ret = true; + + if (0 != strncmp(format_string, "{sv}", strlen(format_string))) + return false; + + va_start(ap, format_string); + key = va_arg(ap, const gchar **); + value = va_arg(ap, GVariant **); + + switch (state) { + case 0: + strncpy(_key, "Distance", sizeof(_key)); + *value = g_variant_new_uint64(1); + break; + case 1: + strncpy(_key, "NodeID", sizeof(_key)); + *value = g_variant_new_uint64(1); + break; + case 2: + strncpy(_key, "PanID", sizeof(_key)); + *value = g_variant_new_uint16(1); + break; + case 3: + strncpy(_key, "X", sizeof(_key)); + *value = g_variant_new_int32(1); + break; + case 4: + strncpy(_key, "Y", sizeof(_key)); + *value = g_variant_new_int32(1); + break; + case 5: + strncpy(_key, "Z", sizeof(_key)); + *value = g_variant_new_int32(1); + break; + default: + ret = false; + } + + if (ret == true) { + *key = (const gchar *)_key; + state += 1; + } else { + state = 0; + } + + va_end(ap); + + return ret; +} + +API void g_variant_get (GVariant *value, const gchar *format_string, ...) +{ + va_list ap; + + if (0 == strncmp(format_string, "(q@aa{sv})", strlen(format_string))) { + guint16 *out_pan_id; + GVariant **out_remote_node_list = NULL; + va_start(ap, format_string); + + out_pan_id = va_arg(ap, guint16 *); + *out_pan_id = 1; + + out_remote_node_list = va_arg(ap, GVariant **); + *out_remote_node_list = __create_dummy(); + + va_end(ap); + } else if (0 == strncmp(format_string, "(@a{sv})", strlen(format_string))) { + GVariant **out_own_node = NULL; + va_start(ap, format_string); + + out_own_node = va_arg(ap, GVariant **); + *out_own_node = __create_vardict(); + + va_end(ap); + } +} + +API gsize g_variant_n_children(GVariant *value) +{ + return 0; +} + +API void g_variant_iter_free (GVariantIter *iter) +{ + return; +} -- 2.7.4