From 66d25a169f820750a03f26109000672983f68b25 Mon Sep 17 00:00:00 2001 From: Jiacheng Guo Date: Fri, 5 Jun 2020 16:30:52 +0800 Subject: [PATCH 1/9] [linux] add ThreadStackManager for Linux Device layer --- .gitmodules | 6 + configure.ac | 47 ++ repos.conf | 46 +- scripts/setup/linux/install_packages.sh | 1 + src/include/platform/PlatformManager.h | 2 + src/include/platform/ThreadStackManager.h | 2 +- .../platform/internal/DeviceNetworkInfo.h | 74 ++++ src/platform/Linux/ThreadStackManagerImpl.cpp | 410 ++++++++++++++++++ src/platform/Linux/ThreadStackManagerImpl.h | 102 +++++ src/platform/Makefile.am | 12 + src/platform/tests/Makefile.am | 27 ++ src/platform/tests/TestThreadStackMgr.cpp | 73 ++++ src/platform/tests/TestThreadStackMgr.h | 29 ++ .../tests/TestThreadStackMgrDriver.cpp | 23 + third_party/Makefile.am | 4 +- third_party/ot-br-posix/Makefile.am | 48 ++ third_party/ot-br-posix/repo | 1 + 17 files changed, 904 insertions(+), 3 deletions(-) mode change 120000 => 100644 repos.conf create mode 100644 src/include/platform/internal/DeviceNetworkInfo.h create mode 100644 src/platform/Linux/ThreadStackManagerImpl.cpp create mode 100644 src/platform/Linux/ThreadStackManagerImpl.h create mode 100644 src/platform/tests/TestThreadStackMgr.cpp create mode 100644 src/platform/tests/TestThreadStackMgr.h create mode 100644 src/platform/tests/TestThreadStackMgrDriver.cpp create mode 100644 third_party/ot-br-posix/Makefile.am create mode 160000 third_party/ot-br-posix/repo diff --git a/.gitmodules b/.gitmodules index 1e806bf8ada70d..27e4c8cf215889 100644 --- a/.gitmodules +++ b/.gitmodules @@ -52,3 +52,9 @@ branch = master ignore = dirty commit = 5f0e36eeb236a507631e9c822e43f742b7b6738a +[submodule "third_party/ot-br-posix/repo"] + path = third_party/ot-br-posix/repo + url = https://github.com/openthread/ot-br-posix.git + branch = master + ignore = dirty + commit = 38e91052d1fb12a55c4e34073f9e399471650b4b diff --git a/configure.ac b/configure.ac index ea193f50e50b3b..ce19078c6a8f47 100644 --- a/configure.ac +++ b/configure.ac @@ -1784,6 +1784,52 @@ if test "${nl_cv_build_tests}" = "yes"; then fi fi +# +# otbr-client +# + +AC_ARG_ENABLE(otbr-client, + [AS_HELP_STRING([--enable-otbr-client], [Enable OpenThread Linux client])], + [ + case "${enableval}" in + + no|yes) + enable_otbr_client=${enableval} + ;; + + *) + AC_MSG_ERROR([Invalid value ${enableval} for --enable-java]) + ;; + + esac + ], + [ enable_otbr_client="no" ] +) + +AC_MSG_NOTICE("enable_otbr_client=${enable_otbr_client}") + +if test "${enable_otbr_client}" = "yes"; then + maybe_otbr_client_dirstem="ot-br-posix" + otbr_client_dirstem="third_party/${maybe_otbr_client_dirstem}/repo" + + AC_MSG_NOTICE([attempting to create internal ${otbrclient_dirstem}]) + + ${MAKE-make} --no-print-directory -C ${srcdir} -f Makefile-bootstrap ${otbr_client_dirstem} + + if test $? -ne 0; then + AC_MSG_ERROR([failed to create ${otbr_client_dirstem}. Please check your network connection or the correctness of 'repos.conf']) + fi + + # otbrclient requires dbus + PKG_CHECK_MODULES(DBUS, dbus-1 >= 1.4) + AC_SUBST([DBUS_CFLAGS]) + AC_SUBST([DBUS_LIBS]) +else + maybe_otbr_client_dirstem= +fi + +AC_SUBST(OTBR_CLIENT_SUBDIRS, [${maybe_otbr_client_dirstem}]) +AM_CONDITIONAL([CHIP_WITH_OTBR_CLIENT], [test "${enable_otbr_client}" != "no"]) # # Nlunit-test @@ -2147,6 +2193,7 @@ examples/Makefile third_party/Makefile third_party/lwip/Makefile third_party/mbedtls/Makefile +third_party/ot-br-posix/Makefile src/Makefile src/include/Makefile src/app/Makefile diff --git a/repos.conf b/repos.conf deleted file mode 120000 index 7837f9c220680c..00000000000000 --- a/repos.conf +++ /dev/null @@ -1 +0,0 @@ -.gitmodules \ No newline at end of file diff --git a/repos.conf b/repos.conf new file mode 100644 index 00000000000000..fecf19b5b20739 --- /dev/null +++ b/repos.conf @@ -0,0 +1,45 @@ +[submodule "nlassert"] + path = third_party/nlassert/repo + url = https://github.com/nestlabs/nlassert.git + branch = master + update = none +[submodule "nlfaultinjection"] + path = third_party/nlfaultinjection/repo + url = https://github.com/nestlabs/nlfaultinjection.git + branch = master + update = none +[submodule "nlio"] + path = third_party/nlio/repo + url = https://github.com/nestlabs/nlio.git + branch = master + update = none +[submodule "nlunit-test"] + path = third_party/nlunit-test/repo + url = https://github.com/nestlabs/nlunit-test.git + branch = master + update = none +[submodule "mbedtls"] + path = third_party/mbedtls/repo + url = https://github.com/ARMmbed/mbedtls.git + branch = mbedtls-2.18 + update = none +[submodule "qrcode"] + path = examples/common/QRCode/repo + url = https://github.com/nayuki/QR-Code-generator.git + branch = master + update = none +[submodule "m5stack-tft"] + path = examples/common/m5stack-tft/repo + url = https://github.com/jeremyjh/ESP32_TFT_library.git + branch = master + update = none +[submodule "openthread"] + path = third_party/openthread/repo + url = https://github.com/openthread/openthread.git + branch = master + update = none +[submodule "ot-br-posix"] + path = third_party/ot-br-posix/repo + url = https://github.com/openthread/ot-br-posix.git + branch = master + update = none diff --git a/scripts/setup/linux/install_packages.sh b/scripts/setup/linux/install_packages.sh index 5936510eb3ae01..993623c1f63478 100755 --- a/scripts/setup/linux/install_packages.sh +++ b/scripts/setup/linux/install_packages.sh @@ -38,6 +38,7 @@ apt-get install -fy \ libssl-dev \ unzip \ wget \ + libdbus-1-dev \ libmbedtls-dev if [[ ! -f 'ci-cache-persistent/openssl/open_ssl_1.1.1f_installed' ]]; then diff --git a/src/include/platform/PlatformManager.h b/src/include/platform/PlatformManager.h index f453e181d9d588..8b3ccd68b1eb17 100644 --- a/src/include/platform/PlatformManager.h +++ b/src/include/platform/PlatformManager.h @@ -46,6 +46,7 @@ class PlatformManagerImpl; class ConnectivityManagerImpl; class ConfigurationManagerImpl; class TraitManager; +class ThreadStackManagerImpl; class TimeSyncManager; namespace Internal { class FabricProvisioningServer; @@ -97,6 +98,7 @@ class PlatformManager friend class ConnectivityManagerImpl; friend class ConfigurationManagerImpl; friend class TraitManager; + friend class ThreadStackManagerImpl; friend class TimeSyncManager; friend class Internal::FabricProvisioningServer; friend class Internal::ServiceProvisioningServer; diff --git a/src/include/platform/ThreadStackManager.h b/src/include/platform/ThreadStackManager.h index cd2c2ec919ad5d..7a69d9e56672db 100644 --- a/src/include/platform/ThreadStackManager.h +++ b/src/include/platform/ThreadStackManager.h @@ -67,7 +67,7 @@ class ThreadStackManager void LockThreadStack(void); bool TryLockThreadStack(void); void UnlockThreadStack(void); - bool HaveRouteToAddress(const IPAddress & destAddr); + bool HaveRouteToAddress(const Inet::IPAddress & destAddr); CHIP_ERROR GetAndLogThreadStatsCounters(void); CHIP_ERROR GetAndLogThreadTopologyMinimal(void); CHIP_ERROR GetAndLogThreadTopologyFull(void); diff --git a/src/include/platform/internal/DeviceNetworkInfo.h b/src/include/platform/internal/DeviceNetworkInfo.h new file mode 100644 index 00000000000000..18ac9bba810ff7 --- /dev/null +++ b/src/include/platform/internal/DeviceNetworkInfo.h @@ -0,0 +1,74 @@ +/* + * + * Copyright (c) 2020 Project CHIP Authors + * Copyright (c) 2018 Nest Labs, Inc. + * 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 DEVICE_NETWORK_INFO_H +#define DEVICE_NETWORK_INFO_H + +#include + +namespace chip { +namespace DeviceLayer { +namespace Internal { + +/** + * Ids for well-known network provision types. + */ +enum +{ + kThreadNetworkId = 1, + kWiFiStationNetworkId = 2, + kMaxThreadNetworkNameLength = 16, + kThreadExtendedPANIdLength = 8, + kThreadMeshPrefixLength = 8, + kThreadNetworkKeyLength = 16, + kThreadPSKcLength = 16, + kThreadChannel_NotSpecified = UINT8_MAX, + kThreadPANId_NotSpecified = UINT16_MAX, +}; + +class DeviceNetworkInfo +{ +public: + // ---- Thread-specific Fields ---- + char ThreadNetworkName[kMaxThreadNetworkNameLength + 1]; + /**< The Thread network name as a NULL-terminated string. */ + uint8_t ThreadExtendedPANId[kThreadExtendedPANIdLength]; + /**< The Thread extended PAN ID. */ + uint8_t ThreadMeshPrefix[kThreadMeshPrefixLength]; + /**< The Thread mesh prefix. */ + uint8_t ThreadNetworkKey[kThreadNetworkKeyLength]; + /**< The Thread master network key (NOT NULL-terminated). */ + uint8_t ThreadPSKc[kThreadPSKcLength]; + /**< The Thread pre-shared commissioner key (NOT NULL-terminated). */ + uint16_t ThreadPANId; /**< The 16-bit Thread PAN ID, or kThreadPANId_NotSpecified */ + uint8_t ThreadChannel; /**< The Thread channel (currently [11..26]), or kThreadChannel_NotSpecified */ + + struct + { + bool ThreadExtendedPANId : 1; + bool ThreadMeshPrefix : 1; + bool ThreadPSKc : 1; + } FieldPresent; +}; + +} // namespace Internal +} // namespace DeviceLayer +} // namespace chip + +#endif // DEVICE_NETWORK_INFO_H diff --git a/src/platform/Linux/ThreadStackManagerImpl.cpp b/src/platform/Linux/ThreadStackManagerImpl.cpp new file mode 100644 index 00000000000000..5da95da0759c2b --- /dev/null +++ b/src/platform/Linux/ThreadStackManagerImpl.cpp @@ -0,0 +1,410 @@ +/* + * + * Copyright (c) 2020 Project CHIP Authors + * + * 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 "platform/internal/CHIPDeviceLayerInternal.h" +#include "platform/internal/DeviceNetworkInfo.h" + +#include "platform/PlatformManager.h" +#include "platform/ThreadStackManager.h" +#include "support/CodeUtils.h" +#include "support/logging/CHIPLogging.h" + +#include "dbus/client/thread_api_dbus.hpp" + +using chip::DeviceLayer::Internal::DeviceNetworkInfo; +using otbr::DBus::ClientError; +using otbr::DBus::DeviceRole; +using otbr::DBus::IpCounters; +using otbr::DBus::LinkModeConfig; +using otbr::DBus::MacCounters; +using otbr::DBus::NeighborInfo; + +#ifndef CHIP_CONFIG_OTBR_CLIENT_ERROR_MIN +#define CHIP_CONFIG_OTBR_CLIENT_ERROR_MIN 8000000 +#endif + +#define OTBR_TO_CHIP_ERROR(x) \ + (x == ClientError::ERROR_NONE ? CHIP_NO_ERROR : _CHIP_ERROR(CHIP_CONFIG_OTBR_CLIENT_ERROR_MIN + static_cast(x))) + +#define LogClientError(error) \ + do \ + { \ + if (error != ClientError::ERROR_NONE) \ + { \ + ChipLogError(DeviceLayer, __FILE__ " %d: Otbr ClientError %d", __LINE__, static_cast(error)); \ + } \ + } while (0) + +namespace chip { +namespace DeviceLayer { + +ThreadStackManagerImpl::ThreadStackManagerImpl(DBusConnection *aConnection) : + mThreadApi(nullptr), mConnection(aConnection), mNetworkInfo(), mAttached(false) {} + +CHIP_ERROR ThreadStackManagerImpl::_InitThreadStack() +{ + ClientError error; + DeviceRole role; + + VerifyOrExit(mConnection != nullptr, error = ClientError::ERROR_DBUS); + mThreadApi = std::unique_ptr(new otbr::DBus::ThreadApiDBus(mConnection)); + mThreadApi->AddDeviceRoleHandler([this](DeviceRole newRole) { this->_ThreadDevcieRoleChangedHandler(newRole); }); + + SuccessOrExit(error = mThreadApi->GetDeviceRole(role)); + _ThreadDevcieRoleChangedHandler(role); + mAttached = (role != DeviceRole::OTBR_DEVICE_ROLE_DETACHED && role != DeviceRole::OTBR_DEVICE_ROLE_DISABLED); +exit: + LogClientError(error); + return OTBR_TO_CHIP_ERROR(error); +} + +void ThreadStackManagerImpl::_ThreadDevcieRoleChangedHandler(DeviceRole role) +{ + bool attached = (role != DeviceRole::OTBR_DEVICE_ROLE_DETACHED && role != DeviceRole::OTBR_DEVICE_ROLE_DISABLED); + ChipDeviceEvent event = ChipDeviceEvent{}; + + if (attached != mAttached) + { + event.Type = DeviceEventType::kThreadConnectivityChange; + event.ThreadConnectivityChange.Result = + attached ? ConnectivityChange::kConnectivity_Established : ConnectivityChange::kConnectivity_Lost; + PlatformMgr().PostEvent(&event); + } + + event.Type = DeviceEventType::kThreadStateChange; + event.ThreadStateChange.RoleChanged = true; + PlatformMgr().PostEvent(&event); +} + +CHIP_ERROR ThreadStackManagerImpl::_ProcessThreadActivity() +{ + return CHIP_NO_ERROR; +} + +static bool RouteMatch(const otbr::DBus::Ip6Prefix & prefix, const Inet::IPAddress & addr) +{ + bool match = true; + const uint8_t * prefixBuffer; + const uint8_t * addrBuffer; + uint8_t wholeBytes = prefix.mLength / CHAR_BIT; + uint8_t pendingBits = prefix.mLength % CHAR_BIT; + + VerifyOrExit(addr.IsIPv6(), match = false); + VerifyOrExit(prefix.mLength > 0, match = false); + + prefixBuffer = static_cast(&prefix.mPrefix[0]); + addrBuffer = reinterpret_cast(&addr.Addr); + VerifyOrExit(memcmp(addrBuffer, prefixBuffer, wholeBytes) == 0, match = false); + if (pendingBits) + { + uint8_t mask = static_cast(((UINT8_MAX >> pendingBits) << (CHAR_BIT - pendingBits))); + + VerifyOrExit((addrBuffer[wholeBytes] & mask) == (addrBuffer[wholeBytes] & mask), match = false); + } + VerifyOrExit(memcmp(addrBuffer, prefixBuffer, wholeBytes) == 0, match = false); + +exit: + return match; +} + +bool ThreadStackManagerImpl::_HaveRouteToAddress(const Inet::IPAddress & destAddr) +{ + std::vector routes; + bool match = false; + + VerifyOrExit(mThreadApi->GetExternalRoutes(routes) == ClientError::ERROR_NONE, match = false); + VerifyOrExit(_IsThreadAttached(), match = false); + VerifyOrExit(destAddr.IsIPv6LinkLocal(), match = true); + for (const auto & route : routes) + { + VerifyOrExit(!(match = RouteMatch(route.mPrefix, destAddr)), ); + } + +exit: + return match; +} + +void ThreadStackManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) +{ + (void) event; + // The otbr-agent processes the Thread state handling by itself so there + // isn't much to do in the Chip stack. +} + +CHIP_ERROR ThreadStackManagerImpl::_SetThreadProvision(const Internal::DeviceNetworkInfo & netInfo) +{ + mNetworkInfo = netInfo; + + return CHIP_NO_ERROR; +} + +CHIP_ERROR ThreadStackManagerImpl::_GetThreadProvision(Internal::DeviceNetworkInfo & netInfo, bool includeCredentials) +{ + netInfo = mNetworkInfo; + + if (!includeCredentials) + { + memset(&netInfo.ThreadNetworkKey, 0, sizeof(netInfo.ThreadNetworkKey)); + memset(&netInfo.ThreadPSKc, 0, sizeof(netInfo.ThreadPSKc)); + netInfo.FieldPresent.ThreadPSKc = false; + } + + return CHIP_NO_ERROR; +} + +bool ThreadStackManagerImpl::_IsThreadProvisioned() +{ + return mNetworkInfo.ThreadNetworkName[0] != '\0'; +} + +void ThreadStackManagerImpl::_ClearThreadProvision() +{ + mNetworkInfo = Internal::DeviceNetworkInfo{}; +} + +bool ThreadStackManagerImpl::_IsThreadEnabled() +{ + bool enabled = false; + DeviceRole role; + ClientError error; + + SuccessOrExit(error = mThreadApi->GetDeviceRole(role)); + enabled = (role != DeviceRole::OTBR_DEVICE_ROLE_DISABLED); +exit: + LogClientError(error); + return enabled; +} + +bool ThreadStackManagerImpl::_IsThreadAttached() +{ + return mAttached; +} + +CHIP_ERROR ThreadStackManagerImpl::_SetThreadEnabled(bool val) +{ + ClientError error = ClientError::ERROR_NONE; + + if (val) + { + std::vector masterkey(std::begin(mNetworkInfo.ThreadNetworkKey), std::end(mNetworkInfo.ThreadNetworkKey)); + std::vector pskc; + uint64_t extPanId = UINT64_MAX; + uint32_t channelMask = UINT32_MAX; + + if (mNetworkInfo.FieldPresent.ThreadExtendedPANId) + { + extPanId = 0; + for (size_t i = 0; i < extPanId; i++) + { + extPanId <<= CHAR_BIT; + extPanId |= mNetworkInfo.ThreadExtendedPANId[i]; + } + } + if (mNetworkInfo.FieldPresent.ThreadPSKc) + { + pskc = std::vector(std::begin(mNetworkInfo.ThreadPSKc), std::end(mNetworkInfo.ThreadPSKc)); + } + if (mNetworkInfo.ThreadChannel != Internal::kThreadChannel_NotSpecified) + { + channelMask = 1 << mNetworkInfo.ThreadChannel; + } + + if (mNetworkInfo.FieldPresent.ThreadMeshPrefix) + { + std::array prefix; + + std::copy(std::begin(mNetworkInfo.ThreadMeshPrefix), std::end(mNetworkInfo.ThreadMeshPrefix), std::begin(prefix)); + SuccessOrExit(error = mThreadApi->SetMeshLocalPrefix(prefix)); + } + + mThreadApi->Attach(mNetworkInfo.ThreadNetworkName, mNetworkInfo.ThreadPANId, extPanId, masterkey, pskc, channelMask, + [](ClientError result) { ChipLogProgress(DeviceLayer, "Thread attach result %d", result); }); + } + else + { + mThreadApi->Reset(); + } +exit: + LogClientError(error); + return OTBR_TO_CHIP_ERROR(error); +} + +ConnectivityManager::ThreadDeviceType ThreadStackManagerImpl::_GetThreadDeviceType() +{ + ClientError error; + DeviceRole role; + LinkModeConfig linkMode; + ConnectivityManager::ThreadDeviceType type = ConnectivityManager::ThreadDeviceType::kThreadDeviceType_NotSupported; + + SuccessOrExit(error = mThreadApi->GetDeviceRole(role)); + + switch (role) + { + case DeviceRole::OTBR_DEVICE_ROLE_DISABLED: + case DeviceRole::OTBR_DEVICE_ROLE_DETACHED: + break; + case DeviceRole::OTBR_DEVICE_ROLE_CHILD: + SuccessOrExit(error = mThreadApi->GetLinkMode(linkMode)); + if (!linkMode.mRxOnWhenIdle) + { + type = ConnectivityManager::ThreadDeviceType::kThreadDeviceType_SleepyEndDevice; + } + else + { + type = linkMode.mDeviceType ? ConnectivityManager::ThreadDeviceType::kThreadDeviceType_FullEndDevice + : ConnectivityManager::ThreadDeviceType::kThreadDeviceType_MinimalEndDevice; + } + case DeviceRole::OTBR_DEVICE_ROLE_ROUTER: + case DeviceRole::OTBR_DEVICE_ROLE_LEADER: + type = ConnectivityManager::ThreadDeviceType::kThreadDeviceType_Router; + default: + break; + } + +exit: + LogClientError(error); + return type; +} + +CHIP_ERROR ThreadStackManagerImpl::_SetThreadDeviceType(ConnectivityManager::ThreadDeviceType deviceType) +{ + LinkModeConfig linkMode{ true, true, true, true }; + ClientError error = ClientError::ERROR_NONE; + + if (deviceType == ConnectivityManager::ThreadDeviceType::kThreadDeviceType_MinimalEndDevice) + { + linkMode.mNetworkData = false; + } + else if (deviceType == ConnectivityManager::ThreadDeviceType::kThreadDeviceType_SleepyEndDevice) + { + linkMode.mRxOnWhenIdle = false; + linkMode.mNetworkData = false; + } + + if (!linkMode.mNetworkData) + { + error = mThreadApi->SetLinkMode(linkMode); + } + + LogClientError(error); + return OTBR_TO_CHIP_ERROR(error); +} + +void ThreadStackManagerImpl::_GetThreadPollingConfig(ConnectivityManager::ThreadPollingConfig & pollingConfig) +{ + (void) pollingConfig; + + ChipLogError(DeviceLayer, "Polling config is not supported on linux"); +} + +CHIP_ERROR ThreadStackManagerImpl::_SetThreadPollingConfig(const ConnectivityManager::ThreadPollingConfig & pollingConfig) +{ + (void) pollingConfig; + + ChipLogError(DeviceLayer, "Polling config is not supported on linux"); + return CHIP_ERROR_NOT_IMPLEMENTED; +} + +bool ThreadStackManagerImpl::_HaveMeshConnectivity() +{ + DeviceRole role; + ClientError error; + bool hasConnectivity = false; + std::vector neighbors; + + SuccessOrExit(error = mThreadApi->GetDeviceRole(role)); + VerifyOrExit(role != DeviceRole::OTBR_DEVICE_ROLE_DETACHED && role != DeviceRole::OTBR_DEVICE_ROLE_DISABLED, ); + if (role == DeviceRole::OTBR_DEVICE_ROLE_CHILD || role == DeviceRole::OTBR_DEVICE_ROLE_ROUTER) + { + hasConnectivity = true; + } + + SuccessOrExit(error = mThreadApi->GetNeighborTable(neighbors)); + for (const auto & neighbor : neighbors) + { + if (!neighbor.mIsChild) + { + hasConnectivity = true; + break; + } + } + +exit: + LogClientError(error); + return hasConnectivity; +} + +void ThreadStackManagerImpl::_OnMessageLayerActivityChanged(bool messageLayerIsActive) +{ + (void) messageLayerIsActive; +} + +void ThreadStackManagerImpl::_OnCHIPoBLEAdvertisingStart() {} + +void ThreadStackManagerImpl::_OnCHIPoBLEAdvertisingStop() {} + +CHIP_ERROR ThreadStackManagerImpl::_GetAndLogThreadStatsCounters() +{ + // TODO: implement after we decide on the profiling protocol + return CHIP_ERROR_NOT_IMPLEMENTED; +} + +CHIP_ERROR ThreadStackManagerImpl::_GetAndLogThreadTopologyMinimal() +{ + // TODO: implement after we decide on the profiling protocol + return CHIP_ERROR_NOT_IMPLEMENTED; +} + +CHIP_ERROR ThreadStackManagerImpl::_GetAndLogThreadTopologyFull() +{ + // TODO: implement after we decide on the profiling protocol + return CHIP_ERROR_NOT_IMPLEMENTED; +} + +CHIP_ERROR ThreadStackManagerImpl::_GetPrimary802154MACAddress(uint8_t * buf) +{ + uint64_t extAddr; + ClientError error; + SuccessOrExit(error = mThreadApi->GetExtendedAddress(extAddr)); + + for (size_t i = 0; i < sizeof(extAddr); i++) + { + buf[sizeof(uint64_t) - i - 1] = (extAddr & UINT8_MAX); + extAddr >>= CHAR_BIT; + } + +exit: + LogClientError(error); + return OTBR_TO_CHIP_ERROR(error); +} + +void ThreadStackManagerImpl::_FactoryReset() +{ + mThreadApi->FactoryReset(nullptr); +} + +// TODO: Implement after we decide on the dbus message loop +extern ThreadStackManager & ThreadStackMgr(void); + +// TODO: Implement after we decide on the dbus message loop +extern ThreadStackManagerImpl & ThreadStackMgrImpl(void); + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/Linux/ThreadStackManagerImpl.h b/src/platform/Linux/ThreadStackManagerImpl.h new file mode 100644 index 00000000000000..da0e48a4bccc71 --- /dev/null +++ b/src/platform/Linux/ThreadStackManagerImpl.h @@ -0,0 +1,102 @@ +/* + * + * Copyright (c) 2020 Project CHIP Authors + * + * 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 CHIP_PLATFORM_LINUX_THREAD_STACK_MANAGER_IMPL_H +#define CHIP_PLATFORM_LINUX_THREAD_STACK_MANAGER_IMPL_H + +#include + +#include "platform/internal/CHIPDeviceLayerInternal.h" + +#include "dbus/client/thread_api_dbus.hpp" +#include "platform/internal/DeviceNetworkInfo.h" + +namespace chip { +namespace DeviceLayer { + +class ThreadStackManagerImpl : public ThreadStackManager +{ +public: + ThreadStackManagerImpl(DBusConnection *aConnection); + + CHIP_ERROR _InitThreadStack(); + CHIP_ERROR _ProcessThreadActivity(); + + CHIP_ERROR _StartThreadTask() { return CHIP_NO_ERROR; } // Intentionally left blank + CHIP_ERROR _LockThreadStack() { return CHIP_NO_ERROR; } // Intentionally left blank + CHIP_ERROR _TryLockThreadStack() { return CHIP_NO_ERROR; } // Intentionally left blank + CHIP_ERROR _UnlockThreadStack() { return CHIP_NO_ERROR; } // Intentionally left blank + + bool _HaveRouteToAddress(const Inet::IPAddress & destAddr); + + void _OnPlatformEvent(const ChipDeviceEvent * event); + + CHIP_ERROR _GetThreadProvision(Internal::DeviceNetworkInfo & netInfo, bool includeCredentials); + + CHIP_ERROR _SetThreadProvision(const Internal::DeviceNetworkInfo & netInfo); + + void _ClearThreadProvision(); + + bool _IsThreadProvisioned(); + + bool _IsThreadEnabled(); + + bool _IsThreadAttached(); + + CHIP_ERROR _SetThreadEnabled(bool val); + + ConnectivityManager::ThreadDeviceType _GetThreadDeviceType(); + + CHIP_ERROR _SetThreadDeviceType(ConnectivityManager::ThreadDeviceType deviceType); + + void _GetThreadPollingConfig(ConnectivityManager::ThreadPollingConfig & pollingConfig); + + CHIP_ERROR _SetThreadPollingConfig(const ConnectivityManager::ThreadPollingConfig & pollingConfig); + + bool _HaveMeshConnectivity(); + + void _OnMessageLayerActivityChanged(bool messageLayerIsActive); + + void _OnCHIPoBLEAdvertisingStart(); + + void _OnCHIPoBLEAdvertisingStop(); + + CHIP_ERROR _GetAndLogThreadStatsCounters(); + + CHIP_ERROR _GetAndLogThreadTopologyMinimal(); + + CHIP_ERROR _GetAndLogThreadTopologyFull(); + + CHIP_ERROR _GetPrimary802154MACAddress(uint8_t * buf); + + void _FactoryReset(); + + ~ThreadStackManagerImpl() = default; + +private: + void _ThreadDevcieRoleChangedHandler(otbr::DBus::DeviceRole role); + + std::unique_ptr mThreadApi; + DBusConnection * mConnection; + Internal::DeviceNetworkInfo mNetworkInfo; + bool mAttached; +}; + +} // namespace DeviceLayer +} // namespace chip + +#endif // CHIP_PLATFORM_LINUX_THREAD_STACK_MANAGER_IMPL_H diff --git a/src/platform/Makefile.am b/src/platform/Makefile.am index 9812eb1f58c985..1efcd1063be484 100644 --- a/src/platform/Makefile.am +++ b/src/platform/Makefile.am @@ -142,6 +142,18 @@ libDeviceLayer_a_SOURCES += \ Linux/SystemTimeSupport.cpp \ $(NULL) +if CHIP_WITH_OTBR_CLIENT +libDeviceLayer_a_SOURCES += \ + Linux/ThreadStackManagerImpl.cpp \ + $(NULL) +endif + +libDeviceLayer_a_CPPFLAGS += \ + -I$(top_srcdir)/third_party/ot-br-posix/repo/include \ + -I$(top_srcdir)/third_party/ot-br-posix/repo/src \ + $(DBUS_CFLAGS) \ + $(NULL) + endif # CHIP_DEVICE_LAYER_TARGET_LINUX if CHIP_DEVICE_LAYER_TARGET_NRF5 diff --git a/src/platform/tests/Makefile.am b/src/platform/tests/Makefile.am index f03b7df3eccff0..b2b0fdfdef1e82 100644 --- a/src/platform/tests/Makefile.am +++ b/src/platform/tests/Makefile.am @@ -68,6 +68,17 @@ AM_CPPFLAGS = \ $(NLUNIT_TEST_CPPFLAGS) \ $(NULL) +if CHIP_DEVICE_LAYER_TARGET_LINUX +if CHIP_WITH_OTBR_CLIENT +AM_CPPFLAGS += \ + $(DBUS_CFLAGS) \ + -I$(top_srcdir)/third_party/ot-br-posix/repo/include \ + -I$(top_srcdir)/third_party/ot-br-posix/repo/src \ + $(NULL) +libPlatformTests_a_SOURCES += TestThreadStackMgr.cpp +endif +endif + CHIP_LDADD = \ $(top_builddir)/src/platform/libDeviceLayer.a \ $(top_builddir)/src/inet/libInetLayer.a \ @@ -75,6 +86,13 @@ CHIP_LDADD = \ $(top_builddir)/src/lib/support/libSupportLayer.a \ $(NULL) +if CHIP_DEVICE_LAYER_TARGET_LINUX +CHIP_LDADD += \ + $(DBUS_LIBS) \ + $(top_builddir)/third_party/ot-br-posix/libotbrclient.a \ + $(NULL) +endif + COMMON_LDADD = \ libPlatformTests.a \ $(CHIP_LDADD) \ @@ -93,6 +111,15 @@ check_PROGRAMS = \ TestConfigurationMgr \ $(NULL) +if CHIP_DEVICE_LAYER_TARGET_LINUX +if CHIP_WITH_OTBR_CLIENT +check_PROGRAMS += TestThreadStackMgr + +TestThreadStackMgr_LDADD = $(COMMON_LDADD) +TestThreadStackMgr_SOURCES = TestThreadStackMgrDriver.cpp +endif +endif + # TODO Add TestConfigurationMgr to check_PROGRAMS when dependent core profile are merged # Test applications and scripts that should be built and run when the diff --git a/src/platform/tests/TestThreadStackMgr.cpp b/src/platform/tests/TestThreadStackMgr.cpp new file mode 100644 index 00000000000000..981ae67d015389 --- /dev/null +++ b/src/platform/tests/TestThreadStackMgr.cpp @@ -0,0 +1,73 @@ +/* + * + * Copyright (c) 2020 Project CHIP Authors + * + * 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 "platform/internal/CHIPDeviceLayerInternal.h" + +#include "platform/ThreadStackManager.h" +#include "platform/PlatformManager.h" + + +#if CHIP_DEVICE_LAYER_TARGET == LINUX +struct DBusConnectionDeleter +{ + void operator()(DBusConnection *aConnection) { dbus_connection_unref(aConnection); } +}; + +using UniqueDBusConnection = std::unique_ptr; +#endif + +int TestThreadStackManager(void) +{ +#if CHIP_DEVICE_LAYER_TARGET == LINUX + DBusError error; + UniqueDBusConnection connection; + + dbus_error_init(&error); + connection = UniqueDBusConnection(dbus_bus_get(DBUS_BUS_SYSTEM, &error)); + assert(dbus_bus_register(connection.get(), &error) == true); + chip::DeviceLayer::ThreadStackManagerImpl impl(connection.get()); +#else + chip::DeviceLayer::ThreadStackManagerImpl impl; +#endif // CHIP_DEVICE_LAYER_TARGET == LINUX + chip::DeviceLayer::Internal::DeviceNetworkInfo info; + uint16_t masterKey[16] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; + + strncpy(info.ThreadNetworkName, "CHIP-TEST", sizeof(info.ThreadNetworkName)); + info.ThreadChannel = UINT8_MAX; + info.ThreadPANId = 0x3455; + info.FieldPresent.ThreadExtendedPANId = false; + info.FieldPresent.ThreadMeshPrefix = false; + info.FieldPresent.ThreadPSKc = false; + memcpy(&info.ThreadNetworkKey, &masterKey, sizeof(masterKey)); + + chip::DeviceLayer::PlatformMgrImpl().InitChipStack(); + + impl.InitThreadStack(); + impl.StartThreadTask(); + impl._SetThreadProvision(info); + impl._SetThreadEnabled(true); + + printf("Start Thread task done\n"); + + //chip::DeviceLayer::PlatformMgrImpl().RunEventLoop(); + + return 0; +} diff --git a/src/platform/tests/TestThreadStackMgr.h b/src/platform/tests/TestThreadStackMgr.h new file mode 100644 index 00000000000000..9e3bdfa4b51f6f --- /dev/null +++ b/src/platform/tests/TestThreadStackMgr.h @@ -0,0 +1,29 @@ +/* + * + * Copyright (c) 2020 Project CHIP Authors + * + * 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 + * This file declares test entry point for CHIP Platform Manager code unit tests. + * + */ + +#ifndef CHIP_TEST_THREAD_STACK_MGR_H +#define CHIP_TEST_THREAD_STACK_MGR_H + +int TestThreadStackManager(void); + +#endif // CHIP_TEST_THREAD_STACK_MGR_H diff --git a/src/platform/tests/TestThreadStackMgrDriver.cpp b/src/platform/tests/TestThreadStackMgrDriver.cpp new file mode 100644 index 00000000000000..5e4cbc581c2bae --- /dev/null +++ b/src/platform/tests/TestThreadStackMgrDriver.cpp @@ -0,0 +1,23 @@ +/* + * + * Copyright (c) 2020 Project CHIP Authors + * + * 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 "TestThreadStackMgr.h" + +int main() +{ + TestThreadStackManager(); +} diff --git a/third_party/Makefile.am b/third_party/Makefile.am index 20c70980fba9fd..46a91706d04666 100644 --- a/third_party/Makefile.am +++ b/third_party/Makefile.am @@ -33,6 +33,7 @@ EXTRA_DIST = \ DIST_SUBDIRS = \ lwip \ mbedtls \ + ot-br-posix \ $(NULL) # _SUBDIRS are @@ -42,7 +43,7 @@ DIST_SUBDIRS = \ # of the 'distclean' target. Consequently, we conditionally include # them in DIST_SUBDIRS on invocation of 'distclean-recursive' -distclean-recursive: DIST_SUBDIRS += $(NLASSERT_SUBDIRS) $(NLFAULTINJECTION_SUBDIRS) $(NLIO_SUBDIRS) $(NLUNIT_TEST_SUBDIRS) $(MBEDTLS_SUBDIRS) +distclean-recursive: DIST_SUBDIRS += $(NLASSERT_SUBDIRS) $(NLFAULTINJECTION_SUBDIRS) $(NLIO_SUBDIRS) $(NLUNIT_TEST_SUBDIRS) $(MBEDTLS_SUBDIRS) $(OTBR_CLIENT_SUBDIRS) # Always build (e.g. for 'make all') these subdirectories. # @@ -56,6 +57,7 @@ SUBDIRS = \ $(NLIO_SUBDIRS) \ $(NLUNIT_TEST_SUBDIRS) \ $(MBEDTLS_SUBDIRS) \ + $(OTBR_CLIENT_SUBDIRS) \ $(NULL) include $(abs_top_nlbuild_autotools_dir)/automake/post.am diff --git a/third_party/ot-br-posix/Makefile.am b/third_party/ot-br-posix/Makefile.am new file mode 100644 index 00000000000000..16903fbdc637b1 --- /dev/null +++ b/third_party/ot-br-posix/Makefile.am @@ -0,0 +1,48 @@ +# +# Copyright (c) 2020 Project CHIP Authors +# +# 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. + + +# +# Description: +# This file is the GNU automake template for the CHIP in-package, +# otbr clientlibrary. +# +# + +include $(abs_top_nlbuild_autotools_dir)/automake/pre.am + +if CHIP_DEVICE_LAYER_TARGET_LINUX +if CHIP_WITH_OTBR_CLIENT + +lib_LIBRARIES = libotbrclient.a + +nodist_libotbrclient_a_SOURCES = \ + repo/src/dbus/client/client_error.cpp \ + repo/src/dbus/client/thread_api_dbus.cpp \ + repo/src/dbus/common/error.cpp \ + repo/src/dbus/common/dbus_message_helper.cpp \ + repo/src/dbus/common/dbus_message_helper_openthread.cpp \ + $(NULL) + +libotbrclient_a_CPPFLAGS = \ + -I$(top_srcdir)/third_party/ot-br-posix/repo/include \ + -I$(top_srcdir)/third_party/ot-br-posix/repo/src \ + $(DBUS_CFLAGS) \ + $(NULL) + +endif # CHIP_WITH_OTBR_CLIENT +endif # CHIP_DEVICE_LAYER_TARGET_LINUX + +include $(abs_top_nlbuild_autotools_dir)/automake/post.am diff --git a/third_party/ot-br-posix/repo b/third_party/ot-br-posix/repo new file mode 160000 index 00000000000000..77a91561a411de --- /dev/null +++ b/third_party/ot-br-posix/repo @@ -0,0 +1 @@ +Subproject commit 77a91561a411ded8c117c710f365213ec6a34ee2 From f5f255262e90cb7c7fdc02d6b4674e14445f26b8 Mon Sep 17 00:00:00 2001 From: Jiacheng Guo Date: Tue, 30 Jun 2020 18:24:00 +0800 Subject: [PATCH 2/9] new configure logic --- config/nrf5/nrf5-chip.mk | 2 +- configure.ac | 81 ++++------ examples/lock-app/nrf5/Makefile | 2 +- examples/lock-app/nrf5/main/main.cpp | 150 +++++++++++++++++- src/platform/Linux/ThreadStackManagerImpl.cpp | 7 +- src/platform/Linux/ThreadStackManagerImpl.h | 4 +- src/platform/Makefile.am | 6 +- src/platform/nRF5/CHIPDevicePlatformConfig.h | 2 +- src/platform/tests/Makefile.am | 6 +- src/platform/tests/TestThreadStackMgr.cpp | 13 +- third_party/ot-br-posix/Makefile.am | 4 +- 11 files changed, 201 insertions(+), 76 deletions(-) diff --git a/config/nrf5/nrf5-chip.mk b/config/nrf5/nrf5-chip.mk index bf291bcb612f85..d4a75b9763e4b2 100644 --- a/config/nrf5/nrf5-chip.mk +++ b/config/nrf5/nrf5-chip.mk @@ -109,7 +109,7 @@ CHIP_CONFIGURE_OPTIONS = \ --with-chip-ble-project-includes=$(CHIP_PROJECT_CONFIG) \ --with-chip-warm-project-includes=$(CHIP_PROJECT_CONFIG) \ --with-chip-device-project-includes=$(CHIP_PROJECT_CONFIG) \ - --with-openthread=$(NRF5_SDK_ROOT)/external/openthread \ + --enable-openthread \ --disable-ipv4 \ --disable-tests \ --disable-tools \ diff --git a/configure.ac b/configure.ac index ce19078c6a8f47..6d69325bfb6517 100644 --- a/configure.ac +++ b/configure.ac @@ -904,36 +904,6 @@ NL_ENABLE_DOCS([auto],[NO]) AM_CONDITIONAL(CHIP_BUILD_DOCS, [test "${nl_cv_build_docs}" = "yes"]) -# -# OpenThread -# - -NL_WITH_OPTIONAL_INTERNAL_PACKAGE( - [OpenThread], - [OPENTHREAD], - [openthread], - [], - [ - AC_MSG_NOTICE([No internal OpenThread support yet!]) - with_openthread=no - ], - [ - # Check for required OpenThread headers. - AC_CHECK_HEADERS([openthread/dataset.h] [openthread/dataset_ftd.h] [openthread/error.h] [openthread/icmp6.h] [openthread/instance.h] [openthread/ip6.h] [openthread/link.h] [openthread/message.h] [openthread/netdata.h] [openthread/tasklet.h] [openthread/thread.h], - [], - [ - AC_MSG_ERROR(The nlio header "$ac_header" is required but cannot be found.) - ]) - ] -) -AM_CONDITIONAL([CHIP_ENABLE_OPENTHREAD], [test "${with_openthread}" != "no"]) -if test "${with_openthread}" != "no"; then - CHIP_ENABLE_OPENTHREAD=1 -else - CHIP_ENABLE_OPENTHREAD=0 -fi -AC_DEFINE_UNQUOTED([CHIP_ENABLE_OPENTHREAD],[${CHIP_ENABLE_OPENTHREAD}],[Define to 1 if you want to enable OpenThread.]) - # # Network Technology Layer # @@ -1785,16 +1755,16 @@ if test "${nl_cv_build_tests}" = "yes"; then fi # -# otbr-client +# openthread # -AC_ARG_ENABLE(otbr-client, - [AS_HELP_STRING([--enable-otbr-client], [Enable OpenThread Linux client])], +AC_ARG_ENABLE(openthread, + [AS_HELP_STRING([--enable-openthread], [Enable OpenThread])], [ case "${enableval}" in no|yes) - enable_otbr_client=${enableval} + enable_openthread=${enableval} ;; *) @@ -1803,33 +1773,42 @@ AC_ARG_ENABLE(otbr-client, esac ], - [ enable_otbr_client="no" ] + [ enable_openthread="no" ] ) -AC_MSG_NOTICE("enable_otbr_client=${enable_otbr_client}") +AC_MSG_NOTICE("enable_openthread=${enable_openthread}") -if test "${enable_otbr_client}" = "yes"; then - maybe_otbr_client_dirstem="ot-br-posix" - otbr_client_dirstem="third_party/${maybe_otbr_client_dirstem}/repo" +if test "${enable_openthread}" = "yes"; then + if test "${with_device_layer}" = "linux"; then + maybe_ot_br_client_dirstem="ot-br-posix" + ot_br_client_dirstem="third_party/${maybe_ot_br_client_dirstem}/repo" - AC_MSG_NOTICE([attempting to create internal ${otbrclient_dirstem}]) + AC_MSG_NOTICE([attempting to create internal ${ot_brclient_dirstem}]) + ${MAKE-make} --no-print-directory -C ${srcdir} -f Makefile-bootstrap ${ot_br_client_dirstem} - ${MAKE-make} --no-print-directory -C ${srcdir} -f Makefile-bootstrap ${otbr_client_dirstem} + if test $? -ne 0; then + AC_MSG_ERROR([failed to create ${ot_br_client_dirstem}. Please check your network connection or the correctness of 'repos.conf']) + fi - if test $? -ne 0; then - AC_MSG_ERROR([failed to create ${otbr_client_dirstem}. Please check your network connection or the correctness of 'repos.conf']) - fi + PKG_CHECK_MODULES([DBUS], dbus-1 >= 1.4) + AC_SUBST([DBUS_CFLAGS]) + AC_SUBST([DBUS_LIBS]) + AC_SUBST(OTBR_CLIENT_SUBDIRS, [${maybe_ot_br_client_dirstem}]) + else + openthread_dirstem="third_party/openthread/repo" - # otbrclient requires dbus - PKG_CHECK_MODULES(DBUS, dbus-1 >= 1.4) - AC_SUBST([DBUS_CFLAGS]) - AC_SUBST([DBUS_LIBS]) + AC_MSG_NOTICE([attempting to create internal ${openthread_dirstem}]) + ${MAKE-make} --no-print-directory -C ${srcdir} -f Makefile-bootstrap ${openthread_dirstem} + if test $? -ne 0; then + AC_MSG_ERROR([failed to create ${ot_br_client_dirstem}. Please check your network connection or the correctness of 'repos.conf']) + fi + fi else - maybe_otbr_client_dirstem= + maybe_ot_br_client_dirstem= fi -AC_SUBST(OTBR_CLIENT_SUBDIRS, [${maybe_otbr_client_dirstem}]) -AM_CONDITIONAL([CHIP_WITH_OTBR_CLIENT], [test "${enable_otbr_client}" != "no"]) +AM_CONDITIONAL([CHIP_WITH_OT_BR_CLIENT], [test "${enable_openthread}" != "no" && test "${with_device_layer}" == "linux"]) +AM_CONDITIONAL([CHIP_WITH_OPENTHREAD], [test "${enable_openthread}" != "no" && test "${with_device_layer}" != "linux"]) # # Nlunit-test diff --git a/examples/lock-app/nrf5/Makefile b/examples/lock-app/nrf5/Makefile index 0396de152b5361..95a07d415cf263 100644 --- a/examples/lock-app/nrf5/Makefile +++ b/examples/lock-app/nrf5/Makefile @@ -183,7 +183,7 @@ INC_DIRS = \ DEFINES = \ BOARD_PCA10056 \ BSP_DEFINES_ONLY \ - CHIP_ENABLE_OPENTHREAD=1 \ + CHIP_WITH_OPENTHREAD=1 \ CONFIG_GPIO_AS_PINRESET \ ENABLE_FEM \ FLOAT_ABI_HARD \ diff --git a/examples/lock-app/nrf5/main/main.cpp b/examples/lock-app/nrf5/main/main.cpp index 20c847262ab0d6..44e8e590517182 100644 --- a/examples/lock-app/nrf5/main/main.cpp +++ b/examples/lock-app/nrf5/main/main.cpp @@ -33,7 +33,7 @@ #include "nrf_crypto.h" #endif #include "mem_manager.h" -#if CHIP_ENABLE_OPENTHREAD +#if CHIP_WITH_OPENTHREAD extern "C" { #include "multiprotocol_802154_config.h" #include "nrf_802154.h" @@ -41,7 +41,7 @@ extern "C" { #include "nrf_cc310_platform_mutex.h" #include } -#endif // CHIP_ENABLE_OPENTHREAD +#endif // CHIP_WITH_OPENTHREAD #if NRF_LOG_ENABLED #include "nrf_log_backend_uart.h" @@ -51,7 +51,21 @@ extern "C" { #include "chipinit.h" #include +#if CHIP_WITH_OPENTHREAD +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include +#include +#endif // CHIP_WITH_OPENTHREAD using namespace ::chip; using namespace ::chip::Inet; @@ -92,7 +106,7 @@ uint32_t LogTimestamp(void) static void OnSoCEvent(uint32_t sys_evt, void * p_context) { -#if CHIP_ENABLE_OPENTHREAD +#if CHIP_WITH_OPENTHREAD otSysSoftdeviceSocEvtHandler(sys_evt); #endif UNUSED_PARAMETER(p_context); @@ -200,12 +214,142 @@ int main(void) #endif // defined(SOFTDEVICE_PRESENT) && SOFTDEVICE_PRESENT +<<<<<<< HEAD ret = ChipInit(); if (ret != NRF_SUCCESS) +||||||| parent of d9660a4... new configure logic + NRF_LOG_INFO("Init CHIP stack"); + ret = PlatformMgr().InitChipStack(); + if (ret != CHIP_NO_ERROR) + { + NRF_LOG_INFO("PlatformMgr().InitChipStack() failed"); + APP_ERROR_HANDLER(ret); + } + +#if CHIP_ENABLE_OPENTHREAD + NRF_LOG_INFO("Initializing OpenThread stack"); + + mbedtls_platform_set_calloc_free(calloc, free); + nrf_cc310_platform_abort_init(); + nrf_cc310_platform_mutex_init(); + mbedtls_platform_setup(NULL); + otHeapSetCAllocFree(calloc, free); + + otSysInit(0, NULL); + + // Configure multiprotocol to work with BLE. + { + uint32_t retval = multiprotocol_802154_mode_set(MULTIPROTOCOL_802154_MODE_FAST_SWITCHING_TIMES); + + if (retval != NRF_SUCCESS) + { + NRF_LOG_INFO("multiprotocol 15.4 failed"); + APP_ERROR_HANDLER(CHIP_ERROR_INTERNAL); + } + } + + ret = ThreadStackMgr().InitThreadStack(); + if (ret != CHIP_NO_ERROR) + { + NRF_LOG_INFO("ThreadStackMgr().InitThreadStack() failed"); + APP_ERROR_HANDLER(ret); + } + + // Configure device to operate as a Thread sleepy end-device. + ret = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router); + if (ret != CHIP_NO_ERROR) + { + NRF_LOG_INFO("ConnectivityMgr().SetThreadDeviceType() failed"); + APP_ERROR_HANDLER(ret); + } +#endif // CHIP_ENABLE_OPENTHREAD + + NRF_LOG_INFO("Starting CHIP task"); + ret = PlatformMgr().StartEventLoopTask(); + if (ret != CHIP_NO_ERROR) + { + NRF_LOG_INFO("PlatformMgr().StartEventLoopTask() failed"); + APP_ERROR_HANDLER(ret); + } + +#if CHIP_ENABLE_OPENTHREAD + NRF_LOG_INFO("Starting OpenThread task"); + + // Start OpenThread task + ret = ThreadStackMgrImpl().StartThreadTask(); + if (ret != CHIP_NO_ERROR) +======= + NRF_LOG_INFO("Init CHIP stack"); + ret = PlatformMgr().InitChipStack(); + if (ret != CHIP_NO_ERROR) + { + NRF_LOG_INFO("PlatformMgr().InitChipStack() failed"); + APP_ERROR_HANDLER(ret); + } + +#if CHIP_WITH_OPENTHREAD + NRF_LOG_INFO("Initializing OpenThread stack"); + + mbedtls_platform_set_calloc_free(calloc, free); + nrf_cc310_platform_abort_init(); + nrf_cc310_platform_mutex_init(); + mbedtls_platform_setup(NULL); + otHeapSetCAllocFree(calloc, free); + + otSysInit(0, NULL); + + // Configure multiprotocol to work with BLE. + { + uint32_t retval = multiprotocol_802154_mode_set(MULTIPROTOCOL_802154_MODE_FAST_SWITCHING_TIMES); + + if (retval != NRF_SUCCESS) + { + NRF_LOG_INFO("multiprotocol 15.4 failed"); + APP_ERROR_HANDLER(CHIP_ERROR_INTERNAL); + } + } + + ret = ThreadStackMgr().InitThreadStack(); + if (ret != CHIP_NO_ERROR) + { + NRF_LOG_INFO("ThreadStackMgr().InitThreadStack() failed"); + APP_ERROR_HANDLER(ret); + } + + // Configure device to operate as a Thread sleepy end-device. + ret = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router); + if (ret != CHIP_NO_ERROR) + { + NRF_LOG_INFO("ConnectivityMgr().SetThreadDeviceType() failed"); + APP_ERROR_HANDLER(ret); + } +#endif // CHIP_WITH_OPENTHREAD + + NRF_LOG_INFO("Starting CHIP task"); + ret = PlatformMgr().StartEventLoopTask(); + if (ret != CHIP_NO_ERROR) + { + NRF_LOG_INFO("PlatformMgr().StartEventLoopTask() failed"); + APP_ERROR_HANDLER(ret); + } + +#if CHIP_WITH_OPENTHREAD + NRF_LOG_INFO("Starting OpenThread task"); + + // Start OpenThread task + ret = ThreadStackMgrImpl().StartThreadTask(); + if (ret != CHIP_NO_ERROR) +>>>>>>> d9660a4... new configure logic { NRF_LOG_INFO("ChipInit() failed"); APP_ERROR_HANDLER(ret); } +<<<<<<< HEAD +||||||| parent of d9660a4... new configure logic +#endif // CHIP_ENABLE_OPENTHREAD +======= +#endif // CHIP_WITH_OPENTHREAD +>>>>>>> d9660a4... new configure logic ret = GetAppTask().StartAppTask(); if (ret != NRF_SUCCESS) diff --git a/src/platform/Linux/ThreadStackManagerImpl.cpp b/src/platform/Linux/ThreadStackManagerImpl.cpp index 5da95da0759c2b..356316c4c9c2f5 100644 --- a/src/platform/Linux/ThreadStackManagerImpl.cpp +++ b/src/platform/Linux/ThreadStackManagerImpl.cpp @@ -55,8 +55,9 @@ using otbr::DBus::NeighborInfo; namespace chip { namespace DeviceLayer { -ThreadStackManagerImpl::ThreadStackManagerImpl(DBusConnection *aConnection) : - mThreadApi(nullptr), mConnection(aConnection), mNetworkInfo(), mAttached(false) {} +ThreadStackManagerImpl::ThreadStackManagerImpl(DBusConnection * aConnection) : + mThreadApi(nullptr), mConnection(aConnection), mNetworkInfo(), mAttached(false) +{} CHIP_ERROR ThreadStackManagerImpl::_InitThreadStack() { @@ -174,7 +175,7 @@ bool ThreadStackManagerImpl::_IsThreadProvisioned() return mNetworkInfo.ThreadNetworkName[0] != '\0'; } -void ThreadStackManagerImpl::_ClearThreadProvision() +void ThreadStackManagerImpl::_ErasePersistentInfo() { mNetworkInfo = Internal::DeviceNetworkInfo{}; } diff --git a/src/platform/Linux/ThreadStackManagerImpl.h b/src/platform/Linux/ThreadStackManagerImpl.h index da0e48a4bccc71..d5ac792525cc6c 100644 --- a/src/platform/Linux/ThreadStackManagerImpl.h +++ b/src/platform/Linux/ThreadStackManagerImpl.h @@ -31,7 +31,7 @@ namespace DeviceLayer { class ThreadStackManagerImpl : public ThreadStackManager { public: - ThreadStackManagerImpl(DBusConnection *aConnection); + ThreadStackManagerImpl(DBusConnection * aConnection); CHIP_ERROR _InitThreadStack(); CHIP_ERROR _ProcessThreadActivity(); @@ -49,7 +49,7 @@ class ThreadStackManagerImpl : public ThreadStackManager CHIP_ERROR _SetThreadProvision(const Internal::DeviceNetworkInfo & netInfo); - void _ClearThreadProvision(); + void _ErasePersistentInfo(); bool _IsThreadProvisioned(); diff --git a/src/platform/Makefile.am b/src/platform/Makefile.am index 1efcd1063be484..3e29f26f645e51 100644 --- a/src/platform/Makefile.am +++ b/src/platform/Makefile.am @@ -116,7 +116,7 @@ libDeviceLayer_a_SOURCES = \ Globals.cpp \ $(NULL) -if CHIP_ENABLE_OPENTHREAD +if CHIP_WITH_OPENTHREAD libDeviceLayer_a_SOURCES += \ OpenThread/OpenThreadUtils.cpp \ $(NULL) @@ -142,7 +142,7 @@ libDeviceLayer_a_SOURCES += \ Linux/SystemTimeSupport.cpp \ $(NULL) -if CHIP_WITH_OTBR_CLIENT +if CHIP_WITH_OT_BR_CLIENT libDeviceLayer_a_SOURCES += \ Linux/ThreadStackManagerImpl.cpp \ $(NULL) @@ -169,7 +169,7 @@ libDeviceLayer_a_SOURCES += \ FreeRTOS/SystemTimeSupport.cpp \ $(NULL) -if CHIP_ENABLE_OPENTHREAD +if CHIP_WITH_OPENTHREAD libDeviceLayer_a_SOURCES += \ nRF5/ThreadStackManagerImpl.cpp \ $(NULL) diff --git a/src/platform/nRF5/CHIPDevicePlatformConfig.h b/src/platform/nRF5/CHIPDevicePlatformConfig.h index 72121484ad5526..7855fb34ace3b7 100644 --- a/src/platform/nRF5/CHIPDevicePlatformConfig.h +++ b/src/platform/nRF5/CHIPDevicePlatformConfig.h @@ -36,7 +36,7 @@ #define CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION 0 #define CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP 0 -#if CHIP_ENABLE_OPENTHREAD +#if CHIP_WITH_OPENTHREAD #define CHIP_DEVICE_CONFIG_ENABLE_THREAD 1 #endif diff --git a/src/platform/tests/Makefile.am b/src/platform/tests/Makefile.am index b2b0fdfdef1e82..f01d2502bcc57f 100644 --- a/src/platform/tests/Makefile.am +++ b/src/platform/tests/Makefile.am @@ -69,7 +69,7 @@ AM_CPPFLAGS = \ $(NULL) if CHIP_DEVICE_LAYER_TARGET_LINUX -if CHIP_WITH_OTBR_CLIENT +if CHIP_WITH_OT_BR_CLIENT AM_CPPFLAGS += \ $(DBUS_CFLAGS) \ -I$(top_srcdir)/third_party/ot-br-posix/repo/include \ @@ -87,11 +87,13 @@ CHIP_LDADD = \ $(NULL) if CHIP_DEVICE_LAYER_TARGET_LINUX +if CHIP_WITH_OT_BR_CLIENT CHIP_LDADD += \ $(DBUS_LIBS) \ $(top_builddir)/third_party/ot-br-posix/libotbrclient.a \ $(NULL) endif +endif COMMON_LDADD = \ libPlatformTests.a \ @@ -112,7 +114,7 @@ check_PROGRAMS = \ $(NULL) if CHIP_DEVICE_LAYER_TARGET_LINUX -if CHIP_WITH_OTBR_CLIENT +if CHIP_WITH_OT_BR_CLIENT check_PROGRAMS += TestThreadStackMgr TestThreadStackMgr_LDADD = $(COMMON_LDADD) diff --git a/src/platform/tests/TestThreadStackMgr.cpp b/src/platform/tests/TestThreadStackMgr.cpp index 981ae67d015389..0d68affcb368c7 100644 --- a/src/platform/tests/TestThreadStackMgr.cpp +++ b/src/platform/tests/TestThreadStackMgr.cpp @@ -16,19 +16,18 @@ */ #include -#include #include +#include #include "platform/internal/CHIPDeviceLayerInternal.h" -#include "platform/ThreadStackManager.h" #include "platform/PlatformManager.h" - +#include "platform/ThreadStackManager.h" #if CHIP_DEVICE_LAYER_TARGET == LINUX struct DBusConnectionDeleter { - void operator()(DBusConnection *aConnection) { dbus_connection_unref(aConnection); } + void operator()(DBusConnection * aConnection) { dbus_connection_unref(aConnection); } }; using UniqueDBusConnection = std::unique_ptr; @@ -37,8 +36,8 @@ using UniqueDBusConnection = std::unique_ptr Date: Wed, 1 Jul 2020 18:30:08 +0800 Subject: [PATCH 3/9] add role check in tests --- src/platform/Linux/ThreadStackManagerImpl.cpp | 1 + src/platform/tests/TestThreadStackMgr.cpp | 30 +++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/platform/Linux/ThreadStackManagerImpl.cpp b/src/platform/Linux/ThreadStackManagerImpl.cpp index 356316c4c9c2f5..3432a249b0cf6b 100644 --- a/src/platform/Linux/ThreadStackManagerImpl.cpp +++ b/src/platform/Linux/ThreadStackManagerImpl.cpp @@ -276,6 +276,7 @@ ConnectivityManager::ThreadDeviceType ThreadStackManagerImpl::_GetThreadDeviceTy case DeviceRole::OTBR_DEVICE_ROLE_LEADER: type = ConnectivityManager::ThreadDeviceType::kThreadDeviceType_Router; default: + ChipLogError(DeviceLayer, "Unknown Thread role: %d", static_cast(role)); break; } diff --git a/src/platform/tests/TestThreadStackMgr.cpp b/src/platform/tests/TestThreadStackMgr.cpp index 0d68affcb368c7..26f4dcf2f973c8 100644 --- a/src/platform/tests/TestThreadStackMgr.cpp +++ b/src/platform/tests/TestThreadStackMgr.cpp @@ -25,6 +25,8 @@ #include "platform/ThreadStackManager.h" #if CHIP_DEVICE_LAYER_TARGET == LINUX +#include + struct DBusConnectionDeleter { void operator()(DBusConnection * aConnection) { dbus_connection_unref(aConnection); } @@ -33,6 +35,18 @@ struct DBusConnectionDeleter using UniqueDBusConnection = std::unique_ptr; #endif +void EventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg) +{ + (void) arg; + if (event->Type == chip::DeviceLayer::DeviceEventType::kThreadConnectivityChange) + { + if (event->ThreadConnectivityChange.Result == chip::DeviceLayer::ConnectivityChange::kConnectivity_Established) + { + exit(0); + } + } +} + int TestThreadStackManager(void) { #if CHIP_DEVICE_LAYER_TARGET == LINUX @@ -58,6 +72,7 @@ int TestThreadStackManager(void) memcpy(&info.ThreadNetworkKey, &masterKey, sizeof(masterKey)); chip::DeviceLayer::PlatformMgrImpl().InitChipStack(); + chip::DeviceLayer::PlatformMgrImpl().AddEventHandler(EventHandler, 0); impl.InitThreadStack(); impl.StartThreadTask(); @@ -66,7 +81,18 @@ int TestThreadStackManager(void) printf("Start Thread task done\n"); - // chip::DeviceLayer::PlatformMgrImpl().RunEventLoop(); + // FIXME: Remove the dbus message loop after integration into PlatforManager +#if CHIP_DEVICE_LAYER_TARGET == LINUX + DBusConnection * dispatchConnection = connection.get(); + std::thread t([dispatchConnection]() { + while (true) + { + dbus_connection_read_write_dispatch(dispatchConnection, -1); + } + }); +#endif + + chip::DeviceLayer::PlatformMgrImpl().RunEventLoop(); - return 0; + return -1; } From bb4ea8251ee07a14fd0181c160bdd9cbcfe69140 Mon Sep 17 00:00:00 2001 From: Jiacheng Guo Date: Fri, 3 Jul 2020 14:38:45 +0800 Subject: [PATCH 4/9] Revert "new configure logic" This reverts commit d9660a40183181bcdc868f79800d17f05caa68ff. --- config/nrf5/nrf5-chip.mk | 2 +- configure.ac | 81 ++++++++++++------- examples/lock-app/nrf5/Makefile | 2 +- examples/lock-app/nrf5/main/main.cpp | 22 +++-- src/platform/Linux/ThreadStackManagerImpl.cpp | 7 +- src/platform/Linux/ThreadStackManagerImpl.h | 4 +- src/platform/Makefile.am | 6 +- src/platform/nRF5/CHIPDevicePlatformConfig.h | 2 +- src/platform/tests/Makefile.am | 6 +- third_party/ot-br-posix/Makefile.am | 4 +- 10 files changed, 80 insertions(+), 56 deletions(-) diff --git a/config/nrf5/nrf5-chip.mk b/config/nrf5/nrf5-chip.mk index d4a75b9763e4b2..bf291bcb612f85 100644 --- a/config/nrf5/nrf5-chip.mk +++ b/config/nrf5/nrf5-chip.mk @@ -109,7 +109,7 @@ CHIP_CONFIGURE_OPTIONS = \ --with-chip-ble-project-includes=$(CHIP_PROJECT_CONFIG) \ --with-chip-warm-project-includes=$(CHIP_PROJECT_CONFIG) \ --with-chip-device-project-includes=$(CHIP_PROJECT_CONFIG) \ - --enable-openthread \ + --with-openthread=$(NRF5_SDK_ROOT)/external/openthread \ --disable-ipv4 \ --disable-tests \ --disable-tools \ diff --git a/configure.ac b/configure.ac index 6d69325bfb6517..ce19078c6a8f47 100644 --- a/configure.ac +++ b/configure.ac @@ -904,6 +904,36 @@ NL_ENABLE_DOCS([auto],[NO]) AM_CONDITIONAL(CHIP_BUILD_DOCS, [test "${nl_cv_build_docs}" = "yes"]) +# +# OpenThread +# + +NL_WITH_OPTIONAL_INTERNAL_PACKAGE( + [OpenThread], + [OPENTHREAD], + [openthread], + [], + [ + AC_MSG_NOTICE([No internal OpenThread support yet!]) + with_openthread=no + ], + [ + # Check for required OpenThread headers. + AC_CHECK_HEADERS([openthread/dataset.h] [openthread/dataset_ftd.h] [openthread/error.h] [openthread/icmp6.h] [openthread/instance.h] [openthread/ip6.h] [openthread/link.h] [openthread/message.h] [openthread/netdata.h] [openthread/tasklet.h] [openthread/thread.h], + [], + [ + AC_MSG_ERROR(The nlio header "$ac_header" is required but cannot be found.) + ]) + ] +) +AM_CONDITIONAL([CHIP_ENABLE_OPENTHREAD], [test "${with_openthread}" != "no"]) +if test "${with_openthread}" != "no"; then + CHIP_ENABLE_OPENTHREAD=1 +else + CHIP_ENABLE_OPENTHREAD=0 +fi +AC_DEFINE_UNQUOTED([CHIP_ENABLE_OPENTHREAD],[${CHIP_ENABLE_OPENTHREAD}],[Define to 1 if you want to enable OpenThread.]) + # # Network Technology Layer # @@ -1755,16 +1785,16 @@ if test "${nl_cv_build_tests}" = "yes"; then fi # -# openthread +# otbr-client # -AC_ARG_ENABLE(openthread, - [AS_HELP_STRING([--enable-openthread], [Enable OpenThread])], +AC_ARG_ENABLE(otbr-client, + [AS_HELP_STRING([--enable-otbr-client], [Enable OpenThread Linux client])], [ case "${enableval}" in no|yes) - enable_openthread=${enableval} + enable_otbr_client=${enableval} ;; *) @@ -1773,42 +1803,33 @@ AC_ARG_ENABLE(openthread, esac ], - [ enable_openthread="no" ] + [ enable_otbr_client="no" ] ) -AC_MSG_NOTICE("enable_openthread=${enable_openthread}") +AC_MSG_NOTICE("enable_otbr_client=${enable_otbr_client}") -if test "${enable_openthread}" = "yes"; then - if test "${with_device_layer}" = "linux"; then - maybe_ot_br_client_dirstem="ot-br-posix" - ot_br_client_dirstem="third_party/${maybe_ot_br_client_dirstem}/repo" +if test "${enable_otbr_client}" = "yes"; then + maybe_otbr_client_dirstem="ot-br-posix" + otbr_client_dirstem="third_party/${maybe_otbr_client_dirstem}/repo" - AC_MSG_NOTICE([attempting to create internal ${ot_brclient_dirstem}]) - ${MAKE-make} --no-print-directory -C ${srcdir} -f Makefile-bootstrap ${ot_br_client_dirstem} + AC_MSG_NOTICE([attempting to create internal ${otbrclient_dirstem}]) - if test $? -ne 0; then - AC_MSG_ERROR([failed to create ${ot_br_client_dirstem}. Please check your network connection or the correctness of 'repos.conf']) - fi + ${MAKE-make} --no-print-directory -C ${srcdir} -f Makefile-bootstrap ${otbr_client_dirstem} - PKG_CHECK_MODULES([DBUS], dbus-1 >= 1.4) - AC_SUBST([DBUS_CFLAGS]) - AC_SUBST([DBUS_LIBS]) - AC_SUBST(OTBR_CLIENT_SUBDIRS, [${maybe_ot_br_client_dirstem}]) - else - openthread_dirstem="third_party/openthread/repo" - - AC_MSG_NOTICE([attempting to create internal ${openthread_dirstem}]) - ${MAKE-make} --no-print-directory -C ${srcdir} -f Makefile-bootstrap ${openthread_dirstem} - if test $? -ne 0; then - AC_MSG_ERROR([failed to create ${ot_br_client_dirstem}. Please check your network connection or the correctness of 'repos.conf']) - fi + if test $? -ne 0; then + AC_MSG_ERROR([failed to create ${otbr_client_dirstem}. Please check your network connection or the correctness of 'repos.conf']) fi + + # otbrclient requires dbus + PKG_CHECK_MODULES(DBUS, dbus-1 >= 1.4) + AC_SUBST([DBUS_CFLAGS]) + AC_SUBST([DBUS_LIBS]) else - maybe_ot_br_client_dirstem= + maybe_otbr_client_dirstem= fi -AM_CONDITIONAL([CHIP_WITH_OT_BR_CLIENT], [test "${enable_openthread}" != "no" && test "${with_device_layer}" == "linux"]) -AM_CONDITIONAL([CHIP_WITH_OPENTHREAD], [test "${enable_openthread}" != "no" && test "${with_device_layer}" != "linux"]) +AC_SUBST(OTBR_CLIENT_SUBDIRS, [${maybe_otbr_client_dirstem}]) +AM_CONDITIONAL([CHIP_WITH_OTBR_CLIENT], [test "${enable_otbr_client}" != "no"]) # # Nlunit-test diff --git a/examples/lock-app/nrf5/Makefile b/examples/lock-app/nrf5/Makefile index 95a07d415cf263..0396de152b5361 100644 --- a/examples/lock-app/nrf5/Makefile +++ b/examples/lock-app/nrf5/Makefile @@ -183,7 +183,7 @@ INC_DIRS = \ DEFINES = \ BOARD_PCA10056 \ BSP_DEFINES_ONLY \ - CHIP_WITH_OPENTHREAD=1 \ + CHIP_ENABLE_OPENTHREAD=1 \ CONFIG_GPIO_AS_PINRESET \ ENABLE_FEM \ FLOAT_ABI_HARD \ diff --git a/examples/lock-app/nrf5/main/main.cpp b/examples/lock-app/nrf5/main/main.cpp index 44e8e590517182..8ca01ee90c73fd 100644 --- a/examples/lock-app/nrf5/main/main.cpp +++ b/examples/lock-app/nrf5/main/main.cpp @@ -33,7 +33,7 @@ #include "nrf_crypto.h" #endif #include "mem_manager.h" -#if CHIP_WITH_OPENTHREAD +#if CHIP_ENABLE_OPENTHREAD extern "C" { #include "multiprotocol_802154_config.h" #include "nrf_802154.h" @@ -41,7 +41,7 @@ extern "C" { #include "nrf_cc310_platform_mutex.h" #include } -#endif // CHIP_WITH_OPENTHREAD +#endif // CHIP_ENABLE_OPENTHREAD #if NRF_LOG_ENABLED #include "nrf_log_backend_uart.h" @@ -51,7 +51,7 @@ extern "C" { #include "chipinit.h" #include -#if CHIP_WITH_OPENTHREAD +#if CHIP_ENABLE_OPENTHREAD #include #include #include @@ -65,7 +65,7 @@ extern "C" { #include #include #include -#endif // CHIP_WITH_OPENTHREAD +#endif // CHIP_ENABLE_OPENTHREAD using namespace ::chip; using namespace ::chip::Inet; @@ -106,7 +106,7 @@ uint32_t LogTimestamp(void) static void OnSoCEvent(uint32_t sys_evt, void * p_context) { -#if CHIP_WITH_OPENTHREAD +#if CHIP_ENABLE_OPENTHREAD otSysSoftdeviceSocEvtHandler(sys_evt); #endif UNUSED_PARAMETER(p_context); @@ -287,7 +287,7 @@ int main(void) APP_ERROR_HANDLER(ret); } -#if CHIP_WITH_OPENTHREAD +#if CHIP_ENABLE_OPENTHREAD NRF_LOG_INFO("Initializing OpenThread stack"); mbedtls_platform_set_calloc_free(calloc, free); @@ -323,7 +323,7 @@ int main(void) NRF_LOG_INFO("ConnectivityMgr().SetThreadDeviceType() failed"); APP_ERROR_HANDLER(ret); } -#endif // CHIP_WITH_OPENTHREAD +#endif // CHIP_ENABLE_OPENTHREAD NRF_LOG_INFO("Starting CHIP task"); ret = PlatformMgr().StartEventLoopTask(); @@ -333,7 +333,7 @@ int main(void) APP_ERROR_HANDLER(ret); } -#if CHIP_WITH_OPENTHREAD +#if CHIP_ENABLE_OPENTHREAD NRF_LOG_INFO("Starting OpenThread task"); // Start OpenThread task @@ -345,11 +345,17 @@ int main(void) APP_ERROR_HANDLER(ret); } <<<<<<< HEAD +<<<<<<< HEAD ||||||| parent of d9660a4... new configure logic #endif // CHIP_ENABLE_OPENTHREAD ======= #endif // CHIP_WITH_OPENTHREAD >>>>>>> d9660a4... new configure logic +||||||| parent of 062c503... Revert "new configure logic" +#endif // CHIP_WITH_OPENTHREAD +======= +#endif // CHIP_ENABLE_OPENTHREAD +>>>>>>> 062c503... Revert "new configure logic" ret = GetAppTask().StartAppTask(); if (ret != NRF_SUCCESS) diff --git a/src/platform/Linux/ThreadStackManagerImpl.cpp b/src/platform/Linux/ThreadStackManagerImpl.cpp index 3432a249b0cf6b..f1325c1407e269 100644 --- a/src/platform/Linux/ThreadStackManagerImpl.cpp +++ b/src/platform/Linux/ThreadStackManagerImpl.cpp @@ -55,9 +55,8 @@ using otbr::DBus::NeighborInfo; namespace chip { namespace DeviceLayer { -ThreadStackManagerImpl::ThreadStackManagerImpl(DBusConnection * aConnection) : - mThreadApi(nullptr), mConnection(aConnection), mNetworkInfo(), mAttached(false) -{} +ThreadStackManagerImpl::ThreadStackManagerImpl(DBusConnection *aConnection) : + mThreadApi(nullptr), mConnection(aConnection), mNetworkInfo(), mAttached(false) {} CHIP_ERROR ThreadStackManagerImpl::_InitThreadStack() { @@ -175,7 +174,7 @@ bool ThreadStackManagerImpl::_IsThreadProvisioned() return mNetworkInfo.ThreadNetworkName[0] != '\0'; } -void ThreadStackManagerImpl::_ErasePersistentInfo() +void ThreadStackManagerImpl::_ClearThreadProvision() { mNetworkInfo = Internal::DeviceNetworkInfo{}; } diff --git a/src/platform/Linux/ThreadStackManagerImpl.h b/src/platform/Linux/ThreadStackManagerImpl.h index d5ac792525cc6c..da0e48a4bccc71 100644 --- a/src/platform/Linux/ThreadStackManagerImpl.h +++ b/src/platform/Linux/ThreadStackManagerImpl.h @@ -31,7 +31,7 @@ namespace DeviceLayer { class ThreadStackManagerImpl : public ThreadStackManager { public: - ThreadStackManagerImpl(DBusConnection * aConnection); + ThreadStackManagerImpl(DBusConnection *aConnection); CHIP_ERROR _InitThreadStack(); CHIP_ERROR _ProcessThreadActivity(); @@ -49,7 +49,7 @@ class ThreadStackManagerImpl : public ThreadStackManager CHIP_ERROR _SetThreadProvision(const Internal::DeviceNetworkInfo & netInfo); - void _ErasePersistentInfo(); + void _ClearThreadProvision(); bool _IsThreadProvisioned(); diff --git a/src/platform/Makefile.am b/src/platform/Makefile.am index 3e29f26f645e51..1efcd1063be484 100644 --- a/src/platform/Makefile.am +++ b/src/platform/Makefile.am @@ -116,7 +116,7 @@ libDeviceLayer_a_SOURCES = \ Globals.cpp \ $(NULL) -if CHIP_WITH_OPENTHREAD +if CHIP_ENABLE_OPENTHREAD libDeviceLayer_a_SOURCES += \ OpenThread/OpenThreadUtils.cpp \ $(NULL) @@ -142,7 +142,7 @@ libDeviceLayer_a_SOURCES += \ Linux/SystemTimeSupport.cpp \ $(NULL) -if CHIP_WITH_OT_BR_CLIENT +if CHIP_WITH_OTBR_CLIENT libDeviceLayer_a_SOURCES += \ Linux/ThreadStackManagerImpl.cpp \ $(NULL) @@ -169,7 +169,7 @@ libDeviceLayer_a_SOURCES += \ FreeRTOS/SystemTimeSupport.cpp \ $(NULL) -if CHIP_WITH_OPENTHREAD +if CHIP_ENABLE_OPENTHREAD libDeviceLayer_a_SOURCES += \ nRF5/ThreadStackManagerImpl.cpp \ $(NULL) diff --git a/src/platform/nRF5/CHIPDevicePlatformConfig.h b/src/platform/nRF5/CHIPDevicePlatformConfig.h index 7855fb34ace3b7..72121484ad5526 100644 --- a/src/platform/nRF5/CHIPDevicePlatformConfig.h +++ b/src/platform/nRF5/CHIPDevicePlatformConfig.h @@ -36,7 +36,7 @@ #define CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION 0 #define CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP 0 -#if CHIP_WITH_OPENTHREAD +#if CHIP_ENABLE_OPENTHREAD #define CHIP_DEVICE_CONFIG_ENABLE_THREAD 1 #endif diff --git a/src/platform/tests/Makefile.am b/src/platform/tests/Makefile.am index f01d2502bcc57f..b2b0fdfdef1e82 100644 --- a/src/platform/tests/Makefile.am +++ b/src/platform/tests/Makefile.am @@ -69,7 +69,7 @@ AM_CPPFLAGS = \ $(NULL) if CHIP_DEVICE_LAYER_TARGET_LINUX -if CHIP_WITH_OT_BR_CLIENT +if CHIP_WITH_OTBR_CLIENT AM_CPPFLAGS += \ $(DBUS_CFLAGS) \ -I$(top_srcdir)/third_party/ot-br-posix/repo/include \ @@ -87,13 +87,11 @@ CHIP_LDADD = \ $(NULL) if CHIP_DEVICE_LAYER_TARGET_LINUX -if CHIP_WITH_OT_BR_CLIENT CHIP_LDADD += \ $(DBUS_LIBS) \ $(top_builddir)/third_party/ot-br-posix/libotbrclient.a \ $(NULL) endif -endif COMMON_LDADD = \ libPlatformTests.a \ @@ -114,7 +112,7 @@ check_PROGRAMS = \ $(NULL) if CHIP_DEVICE_LAYER_TARGET_LINUX -if CHIP_WITH_OT_BR_CLIENT +if CHIP_WITH_OTBR_CLIENT check_PROGRAMS += TestThreadStackMgr TestThreadStackMgr_LDADD = $(COMMON_LDADD) diff --git a/third_party/ot-br-posix/Makefile.am b/third_party/ot-br-posix/Makefile.am index e3bfb84ee294bd..16903fbdc637b1 100644 --- a/third_party/ot-br-posix/Makefile.am +++ b/third_party/ot-br-posix/Makefile.am @@ -24,7 +24,7 @@ include $(abs_top_nlbuild_autotools_dir)/automake/pre.am if CHIP_DEVICE_LAYER_TARGET_LINUX -if CHIP_WITH_OT_BR_CLIENT +if CHIP_WITH_OTBR_CLIENT lib_LIBRARIES = libotbrclient.a @@ -42,7 +42,7 @@ libotbrclient_a_CPPFLAGS = \ $(DBUS_CFLAGS) \ $(NULL) -endif # CHIP_WITH_OT_BR_CLIENT +endif # CHIP_WITH_OTBR_CLIENT endif # CHIP_DEVICE_LAYER_TARGET_LINUX include $(abs_top_nlbuild_autotools_dir)/automake/post.am From c84335af3182959f81b0f7b796c76d200224912f Mon Sep 17 00:00:00 2001 From: Jiacheng Guo Date: Fri, 3 Jul 2020 17:57:52 +0800 Subject: [PATCH 5/9] fixes --- configure.ac | 53 ++++--- examples/lock-app/nrf5/main/main.cpp | 150 ------------------ .../platform/internal/DeviceNetworkInfo.h | 20 +-- src/platform/Linux/ThreadStackManagerImpl.cpp | 7 +- src/platform/Linux/ThreadStackManagerImpl.h | 4 +- src/platform/Makefile.am | 9 +- src/platform/tests/Makefile.am | 16 +- third_party/Makefile.am | 2 +- third_party/ot-br-posix/Makefile.am | 13 +- 9 files changed, 63 insertions(+), 211 deletions(-) diff --git a/configure.ac b/configure.ac index ce19078c6a8f47..2030799ec66e98 100644 --- a/configure.ac +++ b/configure.ac @@ -1788,48 +1788,51 @@ fi # otbr-client # -AC_ARG_ENABLE(otbr-client, - [AS_HELP_STRING([--enable-otbr-client], [Enable OpenThread Linux client])], +NL_WITH_OPTIONAL_INTERNAL_PACKAGE( + [ot_br_posix], + [OT_BR_POSIX], + [ot_br_posix], + [-lot_br_client], [ - case "${enableval}" in - - no|yes) - enable_otbr_client=${enableval} - ;; - - *) - AC_MSG_ERROR([Invalid value ${enableval} for --enable-java]) - ;; - - esac + OT_BR_POSIX_CPPFLAGS="-I\${abs_top_srcdir}/third_party/ot-br-posix/repo/include/ -I\${abs_top_srcdir}/third_party/ot-br-posix/repo/src/" + OT_BR_POSIX_LDFLAGS="-L${ac_pwd}/third_party/ot-br-posix/" + OT_BR_POSIX_LIBS="-lot_br_client" ], - [ enable_otbr_client="no" ] + [ + # Check for required nlfaultinjection headers. + AC_LANG_PUSH([C++]) + AC_CHECK_HEADERS([dbus/client/thread_api_dbus.hpp], + [], + [ + AC_MSG_ERROR(The OpenThread border router header "$ac_header" is required but cannot be found.) + ]) + AC_LANG_POP([C++]) + ] ) -AC_MSG_NOTICE("enable_otbr_client=${enable_otbr_client}") +AC_MSG_NOTICE("nl_with_ot_br_posix=${nl_with_ot_br_posix}") -if test "${enable_otbr_client}" = "yes"; then - maybe_otbr_client_dirstem="ot-br-posix" - otbr_client_dirstem="third_party/${maybe_otbr_client_dirstem}/repo" +if test "${nl_with_ot_br_posix}" = "internal" && test "${with_device_layer}" = "linux"; then + maybe_ot_br_posix_dirstem="ot-br-posix" + ot_br_posix_dirstem="third_party/${maybe_ot_br_posix_dirstem}/repo" - AC_MSG_NOTICE([attempting to create internal ${otbrclient_dirstem}]) + AC_MSG_NOTICE([attempting to create internal ${ot_br_posix_dirstem}]) - ${MAKE-make} --no-print-directory -C ${srcdir} -f Makefile-bootstrap ${otbr_client_dirstem} + ${MAKE-make} --no-print-directory -C ${srcdir} -f Makefile-bootstrap ${ot_br_posix_dirstem} if test $? -ne 0; then - AC_MSG_ERROR([failed to create ${otbr_client_dirstem}. Please check your network connection or the correctness of 'repos.conf']) + AC_MSG_ERROR([failed to create ${ot_br_posix_dirstem}. Please check your network connection or the correctness of 'repos.conf']) fi - # otbrclient requires dbus PKG_CHECK_MODULES(DBUS, dbus-1 >= 1.4) AC_SUBST([DBUS_CFLAGS]) AC_SUBST([DBUS_LIBS]) else - maybe_otbr_client_dirstem= + maybe_ot_br_posix_dirstem= fi -AC_SUBST(OTBR_CLIENT_SUBDIRS, [${maybe_otbr_client_dirstem}]) -AM_CONDITIONAL([CHIP_WITH_OTBR_CLIENT], [test "${enable_otbr_client}" != "no"]) +AC_SUBST(OT_BR_POSIX_SUBDIRS, [${maybe_ot_br_posix_dirstem}]) +AM_CONDITIONAL([CHIP_WITH_OT_BR_POSIX], [test "${nl_with_ot_br_posix}" != "no"]) # # Nlunit-test diff --git a/examples/lock-app/nrf5/main/main.cpp b/examples/lock-app/nrf5/main/main.cpp index 8ca01ee90c73fd..20c847262ab0d6 100644 --- a/examples/lock-app/nrf5/main/main.cpp +++ b/examples/lock-app/nrf5/main/main.cpp @@ -51,21 +51,7 @@ extern "C" { #include "chipinit.h" #include -#if CHIP_ENABLE_OPENTHREAD -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include -#include -#endif // CHIP_ENABLE_OPENTHREAD using namespace ::chip; using namespace ::chip::Inet; @@ -214,148 +200,12 @@ int main(void) #endif // defined(SOFTDEVICE_PRESENT) && SOFTDEVICE_PRESENT -<<<<<<< HEAD ret = ChipInit(); if (ret != NRF_SUCCESS) -||||||| parent of d9660a4... new configure logic - NRF_LOG_INFO("Init CHIP stack"); - ret = PlatformMgr().InitChipStack(); - if (ret != CHIP_NO_ERROR) - { - NRF_LOG_INFO("PlatformMgr().InitChipStack() failed"); - APP_ERROR_HANDLER(ret); - } - -#if CHIP_ENABLE_OPENTHREAD - NRF_LOG_INFO("Initializing OpenThread stack"); - - mbedtls_platform_set_calloc_free(calloc, free); - nrf_cc310_platform_abort_init(); - nrf_cc310_platform_mutex_init(); - mbedtls_platform_setup(NULL); - otHeapSetCAllocFree(calloc, free); - - otSysInit(0, NULL); - - // Configure multiprotocol to work with BLE. - { - uint32_t retval = multiprotocol_802154_mode_set(MULTIPROTOCOL_802154_MODE_FAST_SWITCHING_TIMES); - - if (retval != NRF_SUCCESS) - { - NRF_LOG_INFO("multiprotocol 15.4 failed"); - APP_ERROR_HANDLER(CHIP_ERROR_INTERNAL); - } - } - - ret = ThreadStackMgr().InitThreadStack(); - if (ret != CHIP_NO_ERROR) - { - NRF_LOG_INFO("ThreadStackMgr().InitThreadStack() failed"); - APP_ERROR_HANDLER(ret); - } - - // Configure device to operate as a Thread sleepy end-device. - ret = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router); - if (ret != CHIP_NO_ERROR) - { - NRF_LOG_INFO("ConnectivityMgr().SetThreadDeviceType() failed"); - APP_ERROR_HANDLER(ret); - } -#endif // CHIP_ENABLE_OPENTHREAD - - NRF_LOG_INFO("Starting CHIP task"); - ret = PlatformMgr().StartEventLoopTask(); - if (ret != CHIP_NO_ERROR) - { - NRF_LOG_INFO("PlatformMgr().StartEventLoopTask() failed"); - APP_ERROR_HANDLER(ret); - } - -#if CHIP_ENABLE_OPENTHREAD - NRF_LOG_INFO("Starting OpenThread task"); - - // Start OpenThread task - ret = ThreadStackMgrImpl().StartThreadTask(); - if (ret != CHIP_NO_ERROR) -======= - NRF_LOG_INFO("Init CHIP stack"); - ret = PlatformMgr().InitChipStack(); - if (ret != CHIP_NO_ERROR) - { - NRF_LOG_INFO("PlatformMgr().InitChipStack() failed"); - APP_ERROR_HANDLER(ret); - } - -#if CHIP_ENABLE_OPENTHREAD - NRF_LOG_INFO("Initializing OpenThread stack"); - - mbedtls_platform_set_calloc_free(calloc, free); - nrf_cc310_platform_abort_init(); - nrf_cc310_platform_mutex_init(); - mbedtls_platform_setup(NULL); - otHeapSetCAllocFree(calloc, free); - - otSysInit(0, NULL); - - // Configure multiprotocol to work with BLE. - { - uint32_t retval = multiprotocol_802154_mode_set(MULTIPROTOCOL_802154_MODE_FAST_SWITCHING_TIMES); - - if (retval != NRF_SUCCESS) - { - NRF_LOG_INFO("multiprotocol 15.4 failed"); - APP_ERROR_HANDLER(CHIP_ERROR_INTERNAL); - } - } - - ret = ThreadStackMgr().InitThreadStack(); - if (ret != CHIP_NO_ERROR) - { - NRF_LOG_INFO("ThreadStackMgr().InitThreadStack() failed"); - APP_ERROR_HANDLER(ret); - } - - // Configure device to operate as a Thread sleepy end-device. - ret = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router); - if (ret != CHIP_NO_ERROR) - { - NRF_LOG_INFO("ConnectivityMgr().SetThreadDeviceType() failed"); - APP_ERROR_HANDLER(ret); - } -#endif // CHIP_ENABLE_OPENTHREAD - - NRF_LOG_INFO("Starting CHIP task"); - ret = PlatformMgr().StartEventLoopTask(); - if (ret != CHIP_NO_ERROR) - { - NRF_LOG_INFO("PlatformMgr().StartEventLoopTask() failed"); - APP_ERROR_HANDLER(ret); - } - -#if CHIP_ENABLE_OPENTHREAD - NRF_LOG_INFO("Starting OpenThread task"); - - // Start OpenThread task - ret = ThreadStackMgrImpl().StartThreadTask(); - if (ret != CHIP_NO_ERROR) ->>>>>>> d9660a4... new configure logic { NRF_LOG_INFO("ChipInit() failed"); APP_ERROR_HANDLER(ret); } -<<<<<<< HEAD -<<<<<<< HEAD -||||||| parent of d9660a4... new configure logic -#endif // CHIP_ENABLE_OPENTHREAD -======= -#endif // CHIP_WITH_OPENTHREAD ->>>>>>> d9660a4... new configure logic -||||||| parent of 062c503... Revert "new configure logic" -#endif // CHIP_WITH_OPENTHREAD -======= -#endif // CHIP_ENABLE_OPENTHREAD ->>>>>>> 062c503... Revert "new configure logic" ret = GetAppTask().StartAppTask(); if (ret != NRF_SUCCESS) diff --git a/src/include/platform/internal/DeviceNetworkInfo.h b/src/include/platform/internal/DeviceNetworkInfo.h index 18ac9bba810ff7..b0ede19da4fd25 100644 --- a/src/include/platform/internal/DeviceNetworkInfo.h +++ b/src/include/platform/internal/DeviceNetworkInfo.h @@ -20,6 +20,7 @@ #ifndef DEVICE_NETWORK_INFO_H #define DEVICE_NETWORK_INFO_H +#include #include namespace chip { @@ -29,18 +30,13 @@ namespace Internal { /** * Ids for well-known network provision types. */ -enum -{ - kThreadNetworkId = 1, - kWiFiStationNetworkId = 2, - kMaxThreadNetworkNameLength = 16, - kThreadExtendedPANIdLength = 8, - kThreadMeshPrefixLength = 8, - kThreadNetworkKeyLength = 16, - kThreadPSKcLength = 16, - kThreadChannel_NotSpecified = UINT8_MAX, - kThreadPANId_NotSpecified = UINT16_MAX, -}; +constexpr size_t kMaxThreadNetworkNameLength = 16; +constexpr size_t kThreadExtendedPANIdLength = 8; +constexpr size_t kThreadMeshPrefixLength = 8; +constexpr size_t kThreadNetworkKeyLength = 16; +constexpr size_t kThreadPSKcLength = 16; +constexpr size_t kThreadChannel_NotSpecified = UINT8_MAX; +constexpr size_t kThreadPANId_NotSpecified = UINT16_MAX; class DeviceNetworkInfo { diff --git a/src/platform/Linux/ThreadStackManagerImpl.cpp b/src/platform/Linux/ThreadStackManagerImpl.cpp index f1325c1407e269..3432a249b0cf6b 100644 --- a/src/platform/Linux/ThreadStackManagerImpl.cpp +++ b/src/platform/Linux/ThreadStackManagerImpl.cpp @@ -55,8 +55,9 @@ using otbr::DBus::NeighborInfo; namespace chip { namespace DeviceLayer { -ThreadStackManagerImpl::ThreadStackManagerImpl(DBusConnection *aConnection) : - mThreadApi(nullptr), mConnection(aConnection), mNetworkInfo(), mAttached(false) {} +ThreadStackManagerImpl::ThreadStackManagerImpl(DBusConnection * aConnection) : + mThreadApi(nullptr), mConnection(aConnection), mNetworkInfo(), mAttached(false) +{} CHIP_ERROR ThreadStackManagerImpl::_InitThreadStack() { @@ -174,7 +175,7 @@ bool ThreadStackManagerImpl::_IsThreadProvisioned() return mNetworkInfo.ThreadNetworkName[0] != '\0'; } -void ThreadStackManagerImpl::_ClearThreadProvision() +void ThreadStackManagerImpl::_ErasePersistentInfo() { mNetworkInfo = Internal::DeviceNetworkInfo{}; } diff --git a/src/platform/Linux/ThreadStackManagerImpl.h b/src/platform/Linux/ThreadStackManagerImpl.h index da0e48a4bccc71..d5ac792525cc6c 100644 --- a/src/platform/Linux/ThreadStackManagerImpl.h +++ b/src/platform/Linux/ThreadStackManagerImpl.h @@ -31,7 +31,7 @@ namespace DeviceLayer { class ThreadStackManagerImpl : public ThreadStackManager { public: - ThreadStackManagerImpl(DBusConnection *aConnection); + ThreadStackManagerImpl(DBusConnection * aConnection); CHIP_ERROR _InitThreadStack(); CHIP_ERROR _ProcessThreadActivity(); @@ -49,7 +49,7 @@ class ThreadStackManagerImpl : public ThreadStackManager CHIP_ERROR _SetThreadProvision(const Internal::DeviceNetworkInfo & netInfo); - void _ClearThreadProvision(); + void _ErasePersistentInfo(); bool _IsThreadProvisioned(); diff --git a/src/platform/Makefile.am b/src/platform/Makefile.am index 1efcd1063be484..0392e885722a4b 100644 --- a/src/platform/Makefile.am +++ b/src/platform/Makefile.am @@ -142,17 +142,16 @@ libDeviceLayer_a_SOURCES += \ Linux/SystemTimeSupport.cpp \ $(NULL) -if CHIP_WITH_OTBR_CLIENT +if CHIP_WITH_OT_BR_POSIX libDeviceLayer_a_SOURCES += \ Linux/ThreadStackManagerImpl.cpp \ $(NULL) -endif libDeviceLayer_a_CPPFLAGS += \ - -I$(top_srcdir)/third_party/ot-br-posix/repo/include \ - -I$(top_srcdir)/third_party/ot-br-posix/repo/src \ - $(DBUS_CFLAGS) \ + $(OT_BR_POSIX_CPPFLAGS) \ + $(DBUS_CFLAGS) \ $(NULL) +endif endif # CHIP_DEVICE_LAYER_TARGET_LINUX diff --git a/src/platform/tests/Makefile.am b/src/platform/tests/Makefile.am index b2b0fdfdef1e82..02cae2222929cc 100644 --- a/src/platform/tests/Makefile.am +++ b/src/platform/tests/Makefile.am @@ -69,11 +69,10 @@ AM_CPPFLAGS = \ $(NULL) if CHIP_DEVICE_LAYER_TARGET_LINUX -if CHIP_WITH_OTBR_CLIENT +if CHIP_WITH_OT_BR_POSIX AM_CPPFLAGS += \ $(DBUS_CFLAGS) \ - -I$(top_srcdir)/third_party/ot-br-posix/repo/include \ - -I$(top_srcdir)/third_party/ot-br-posix/repo/src \ + $(OT_BR_POSIX_CPPFLAGS) \ $(NULL) libPlatformTests_a_SOURCES += TestThreadStackMgr.cpp endif @@ -87,11 +86,13 @@ CHIP_LDADD = \ $(NULL) if CHIP_DEVICE_LAYER_TARGET_LINUX +if CHIP_WITH_OT_BR_POSIX CHIP_LDADD += \ $(DBUS_LIBS) \ - $(top_builddir)/third_party/ot-br-posix/libotbrclient.a \ + $(OT_BR_POSIX_LDFLAGS) $(OT_BR_POSIX_LIBS) \ $(NULL) endif +endif COMMON_LDADD = \ libPlatformTests.a \ @@ -112,7 +113,7 @@ check_PROGRAMS = \ $(NULL) if CHIP_DEVICE_LAYER_TARGET_LINUX -if CHIP_WITH_OTBR_CLIENT +if CHIP_WITH_OT_BR_POSIX check_PROGRAMS += TestThreadStackMgr TestThreadStackMgr_LDADD = $(COMMON_LDADD) @@ -125,8 +126,11 @@ endif # Test applications and scripts that should be built and run when the # 'check' target is run. +# TestThreadStackMgr requires additional daemon to be launched TESTS = \ - $(check_PROGRAMS) \ + TestPlatformTime \ + TestPlatformMgr \ + TestConfigurationMgr \ $(NULL) # The additional environment variables and their values that will be diff --git a/third_party/Makefile.am b/third_party/Makefile.am index 46a91706d04666..4acb87b13342da 100644 --- a/third_party/Makefile.am +++ b/third_party/Makefile.am @@ -57,7 +57,7 @@ SUBDIRS = \ $(NLIO_SUBDIRS) \ $(NLUNIT_TEST_SUBDIRS) \ $(MBEDTLS_SUBDIRS) \ - $(OTBR_CLIENT_SUBDIRS) \ + $(OT_BR_POSIX_SUBDIRS) \ $(NULL) include $(abs_top_nlbuild_autotools_dir)/automake/post.am diff --git a/third_party/ot-br-posix/Makefile.am b/third_party/ot-br-posix/Makefile.am index 16903fbdc637b1..50b075f10627ed 100644 --- a/third_party/ot-br-posix/Makefile.am +++ b/third_party/ot-br-posix/Makefile.am @@ -24,11 +24,11 @@ include $(abs_top_nlbuild_autotools_dir)/automake/pre.am if CHIP_DEVICE_LAYER_TARGET_LINUX -if CHIP_WITH_OTBR_CLIENT +if CHIP_WITH_OT_BR_POSIX -lib_LIBRARIES = libotbrclient.a +lib_LIBRARIES = libot_br_client.a -nodist_libotbrclient_a_SOURCES = \ +nodist_libot_br_client_a_SOURCES = \ repo/src/dbus/client/client_error.cpp \ repo/src/dbus/client/thread_api_dbus.cpp \ repo/src/dbus/common/error.cpp \ @@ -36,13 +36,12 @@ nodist_libotbrclient_a_SOURCES = \ repo/src/dbus/common/dbus_message_helper_openthread.cpp \ $(NULL) -libotbrclient_a_CPPFLAGS = \ - -I$(top_srcdir)/third_party/ot-br-posix/repo/include \ - -I$(top_srcdir)/third_party/ot-br-posix/repo/src \ +libot_br_client_a_CPPFLAGS = \ + $(OT_BR_POSIX_CPPFLAGS) \ $(DBUS_CFLAGS) \ $(NULL) -endif # CHIP_WITH_OTBR_CLIENT +endif # CHIP_WITH_OT_BR_POSIX endif # CHIP_DEVICE_LAYER_TARGET_LINUX include $(abs_top_nlbuild_autotools_dir)/automake/post.am From 39b1c637f6c77572960e4b7ef4332d3a4dba5ec9 Mon Sep 17 00:00:00 2001 From: Jiacheng Guo Date: Fri, 3 Jul 2020 18:34:01 +0800 Subject: [PATCH 6/9] fix ci --- .github/workflows/build.yaml | 5 ++++- .gitmodules | 8 ++++---- integrations/docker/images/chip-build/Dockerfile | 1 + 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 2ac1805483b637..b03bf5976ba8c5 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -32,7 +32,10 @@ jobs: case $BUILD_TYPE in "main") export BOOTSTRAP_ARGUMENTS="";; "clang") export BOOTSTRAP_ARGUMENTS="CC=clang-9 CXX=clang++-9";; - "linux-embedded") export BOOTSTRAP_ARGUMENTS="--with-device-layer=linux";; + "linux-embedded") + apt-get update + apt-get install -y libdbus-1-dev + export BOOTSTRAP_ARGUMENTS="--with-device-layer=linux";; "mbedtls") export BOOTSTRAP_ARGUMENTS="--with-crypto=mbedtls";; *) ;; esac diff --git a/.gitmodules b/.gitmodules index 27e4c8cf215889..220f560519b95e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -53,8 +53,8 @@ ignore = dirty commit = 5f0e36eeb236a507631e9c822e43f742b7b6738a [submodule "third_party/ot-br-posix/repo"] - path = third_party/ot-br-posix/repo - url = https://github.com/openthread/ot-br-posix.git + path = third_party/ot-br-posix/repo + url = https://github.com/openthread/ot-br-posix.git branch = master - ignore = dirty - commit = 38e91052d1fb12a55c4e34073f9e399471650b4b + ignore = dirty + commit = 38e91052d1fb12a55c4e34073f9e399471650b4b diff --git a/integrations/docker/images/chip-build/Dockerfile b/integrations/docker/images/chip-build/Dockerfile index 1086e3bb343b29..9c15c3d9fff9b7 100644 --- a/integrations/docker/images/chip-build/Dockerfile +++ b/integrations/docker/images/chip-build/Dockerfile @@ -40,6 +40,7 @@ RUN set -x \ libglib2.0-dev \ libpixman-1-dev \ ninja-build \ + libdbus-1-dev \ && rm -rf /var/lib/apt/lists/ \ && : # last line From 6185d269c0ed76e9b59531091f6dd65425ce1c0e Mon Sep 17 00:00:00 2001 From: Jiacheng Guo Date: Mon, 6 Jul 2020 16:56:33 +0800 Subject: [PATCH 7/9] fix comments --- src/include/platform/internal/DeviceNetworkInfo.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/include/platform/internal/DeviceNetworkInfo.h b/src/include/platform/internal/DeviceNetworkInfo.h index b0ede19da4fd25..4b7449a59f76bc 100644 --- a/src/include/platform/internal/DeviceNetworkInfo.h +++ b/src/include/platform/internal/DeviceNetworkInfo.h @@ -28,7 +28,7 @@ namespace DeviceLayer { namespace Internal { /** - * Ids for well-known network provision types. + * Constants for common network metadata entries */ constexpr size_t kMaxThreadNetworkNameLength = 16; constexpr size_t kThreadExtendedPANIdLength = 8; From 4d6498678c5324110b2ed826520bee389aee2939 Mon Sep 17 00:00:00 2001 From: Jiacheng Guo Date: Tue, 7 Jul 2020 22:32:34 +0800 Subject: [PATCH 8/9] use updated docker image --- .github/workflows/build.yaml | 7 ++----- integrations/docker/images/chip-build/Dockerfile | 1 - 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index b03bf5976ba8c5..e2db605873f16d 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -20,7 +20,7 @@ jobs: runs-on: ubuntu-latest container: - image: "connectedhomeip/chip-build-openssl:0.2.14" + image: "connectedhomeip/chip-build-openssl:0.2.18" volumes: - "/tmp/log_output:/tmp/test_logs" @@ -32,10 +32,7 @@ jobs: case $BUILD_TYPE in "main") export BOOTSTRAP_ARGUMENTS="";; "clang") export BOOTSTRAP_ARGUMENTS="CC=clang-9 CXX=clang++-9";; - "linux-embedded") - apt-get update - apt-get install -y libdbus-1-dev - export BOOTSTRAP_ARGUMENTS="--with-device-layer=linux";; + "linux-embedded") export BOOTSTRAP_ARGUMENTS="--with-device-layer=linux";; "mbedtls") export BOOTSTRAP_ARGUMENTS="--with-crypto=mbedtls";; *) ;; esac diff --git a/integrations/docker/images/chip-build/Dockerfile b/integrations/docker/images/chip-build/Dockerfile index 9c15c3d9fff9b7..1086e3bb343b29 100644 --- a/integrations/docker/images/chip-build/Dockerfile +++ b/integrations/docker/images/chip-build/Dockerfile @@ -40,7 +40,6 @@ RUN set -x \ libglib2.0-dev \ libpixman-1-dev \ ninja-build \ - libdbus-1-dev \ && rm -rf /var/lib/apt/lists/ \ && : # last line From b50abccd7b64be0805ed5fe9016969b5e3c1bd38 Mon Sep 17 00:00:00 2001 From: Jiacheng Guo Date: Wed, 8 Jul 2020 11:43:18 +0800 Subject: [PATCH 9/9] move dbus event loop to implementation --- src/platform/Linux/ThreadStackManagerImpl.cpp | 22 +++++++++++++++---- src/platform/Linux/ThreadStackManagerImpl.h | 13 +++++++++-- src/platform/tests/TestThreadStackMgr.cpp | 21 ------------------ 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/platform/Linux/ThreadStackManagerImpl.cpp b/src/platform/Linux/ThreadStackManagerImpl.cpp index 3432a249b0cf6b..97de23f8b43acd 100644 --- a/src/platform/Linux/ThreadStackManagerImpl.cpp +++ b/src/platform/Linux/ThreadStackManagerImpl.cpp @@ -55,23 +55,37 @@ using otbr::DBus::NeighborInfo; namespace chip { namespace DeviceLayer { -ThreadStackManagerImpl::ThreadStackManagerImpl(DBusConnection * aConnection) : - mThreadApi(nullptr), mConnection(aConnection), mNetworkInfo(), mAttached(false) -{} +ThreadStackManagerImpl::ThreadStackManagerImpl() : mThreadApi(nullptr), mConnection(nullptr), mNetworkInfo(), mAttached(false) {} CHIP_ERROR ThreadStackManagerImpl::_InitThreadStack() { ClientError error; DeviceRole role; + DBusError dbusError; + DBusConnection * dispatchConnection; + + dbus_error_init(&dbusError); + mConnection = UniqueDBusConnection(dbus_bus_get(DBUS_BUS_SYSTEM, &dbusError)); + VerifyOrExit(dbus_bus_register(mConnection.get(), &dbusError), error = ClientError::ERROR_DBUS); VerifyOrExit(mConnection != nullptr, error = ClientError::ERROR_DBUS); - mThreadApi = std::unique_ptr(new otbr::DBus::ThreadApiDBus(mConnection)); + mThreadApi = std::unique_ptr(new otbr::DBus::ThreadApiDBus(mConnection.get())); mThreadApi->AddDeviceRoleHandler([this](DeviceRole newRole) { this->_ThreadDevcieRoleChangedHandler(newRole); }); SuccessOrExit(error = mThreadApi->GetDeviceRole(role)); _ThreadDevcieRoleChangedHandler(role); mAttached = (role != DeviceRole::OTBR_DEVICE_ROLE_DETACHED && role != DeviceRole::OTBR_DEVICE_ROLE_DISABLED); + + dispatchConnection = mConnection.get(); + mDBusEventLoop = std::thread([dispatchConnection]() { + while (true) + { + dbus_connection_read_write_dispatch(dispatchConnection, -1); + } + }); + mDBusEventLoop.detach(); exit: + dbus_error_free(&dbusError); LogClientError(error); return OTBR_TO_CHIP_ERROR(error); } diff --git a/src/platform/Linux/ThreadStackManagerImpl.h b/src/platform/Linux/ThreadStackManagerImpl.h index d5ac792525cc6c..92f7ba832485c7 100644 --- a/src/platform/Linux/ThreadStackManagerImpl.h +++ b/src/platform/Linux/ThreadStackManagerImpl.h @@ -19,6 +19,7 @@ #define CHIP_PLATFORM_LINUX_THREAD_STACK_MANAGER_IMPL_H #include +#include #include "platform/internal/CHIPDeviceLayerInternal.h" @@ -31,7 +32,7 @@ namespace DeviceLayer { class ThreadStackManagerImpl : public ThreadStackManager { public: - ThreadStackManagerImpl(DBusConnection * aConnection); + ThreadStackManagerImpl(); CHIP_ERROR _InitThreadStack(); CHIP_ERROR _ProcessThreadActivity(); @@ -88,12 +89,20 @@ class ThreadStackManagerImpl : public ThreadStackManager ~ThreadStackManagerImpl() = default; private: + struct DBusConnectionDeleter + { + void operator()(DBusConnection * aConnection) { dbus_connection_unref(aConnection); } + }; + + using UniqueDBusConnection = std::unique_ptr; + void _ThreadDevcieRoleChangedHandler(otbr::DBus::DeviceRole role); std::unique_ptr mThreadApi; - DBusConnection * mConnection; + UniqueDBusConnection mConnection; Internal::DeviceNetworkInfo mNetworkInfo; bool mAttached; + std::thread mDBusEventLoop; }; } // namespace DeviceLayer diff --git a/src/platform/tests/TestThreadStackMgr.cpp b/src/platform/tests/TestThreadStackMgr.cpp index 26f4dcf2f973c8..99040e94394357 100644 --- a/src/platform/tests/TestThreadStackMgr.cpp +++ b/src/platform/tests/TestThreadStackMgr.cpp @@ -49,17 +49,7 @@ void EventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg int TestThreadStackManager(void) { -#if CHIP_DEVICE_LAYER_TARGET == LINUX - DBusError error; - UniqueDBusConnection connection; - - dbus_error_init(&error); - connection = UniqueDBusConnection(dbus_bus_get(DBUS_BUS_SYSTEM, &error)); - assert(dbus_bus_register(connection.get(), &error) == true); - chip::DeviceLayer::ThreadStackManagerImpl impl(connection.get()); -#else chip::DeviceLayer::ThreadStackManagerImpl impl; -#endif // CHIP_DEVICE_LAYER_TARGET == LINUX chip::DeviceLayer::Internal::DeviceNetworkInfo info; uint16_t masterKey[16] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; @@ -81,17 +71,6 @@ int TestThreadStackManager(void) printf("Start Thread task done\n"); - // FIXME: Remove the dbus message loop after integration into PlatforManager -#if CHIP_DEVICE_LAYER_TARGET == LINUX - DBusConnection * dispatchConnection = connection.get(); - std::thread t([dispatchConnection]() { - while (true) - { - dbus_connection_read_write_dispatch(dispatchConnection, -1); - } - }); -#endif - chip::DeviceLayer::PlatformMgrImpl().RunEventLoop(); return -1;