From: Jihoon Jung Date: Mon, 24 Aug 2020 05:40:14 +0000 (+0900) Subject: Initial repository commit X-Git-Tag: submit/tizen/20200824.092644^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c20d2e486c8b9fa454426055bc486efbd0bf8c97;p=platform%2Fcore%2Fapi%2Fuwb.git Initial repository commit Change-Id: If37afed608c3e1d63022dde22f24560f3750eee4 Signed-off-by: Jihoon Jung --- 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; +}