Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nrf5-lock] enable Thread joining #1879

Merged
merged 5 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions config/nrf5/nrf5-chip.mk
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ STD_LIBS += \
-lCHIPDataModel \
-lInetLayer \
-lSystemLayer \
-llwip
-llwip \
-lSetupPayload

# Add the appropriate CHIP target as a prerequisite to all application
# compilation targets to ensure that CHIP gets built and its header
Expand All @@ -165,7 +166,9 @@ STD_LINK_PREREQUISITES += \
$(CHIP_OUTPUT_DIR)/lib/libCHIP.a \
$(CHIP_OUTPUT_DIR)/lib/libInetLayer.a \
$(CHIP_OUTPUT_DIR)/lib/libSystemLayer.a \
$(CHIP_OUTPUT_DIR)/lib/liblwip.a
$(CHIP_OUTPUT_DIR)/lib/liblwip.a \
$(CHIP_OUTPUT_DIR)/lib/libSetupPayload.a \



# ==================================================
Expand Down
1 change: 0 additions & 1 deletion examples/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ PRETTY_FILES = \
lock-app/nrf5/main/support/AltPrintf.c \
lock-app/nrf5/main/support/FreeRTOSNewlibLockSupport.c \
lock-app/nrf5/main/support/nRF5Sbrk.c \
lock-app/nrf5/main/support/CXXExceptionStubs.cpp \
$(NULL)

include $(abs_top_nlbuild_autotools_dir)/automake/post.am
3 changes: 3 additions & 0 deletions examples/lock-app/nrf5/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@ INC_DIRS = \
$(PROJECT_ROOT)/main/traits/include \
$(PROJECT_ROOT)/main/schema/include \
$(PROJECT_ROOT)/third_party/printf \
$(CHIP_OUTPUT_DIR)/third_party/openthread/include \
$(CHIP_ROOT)/src/include/ \
$(CHIP_ROOT)/src/lib \
$(CHIP_ROOT)/src/ \
$(CHIP_ROOT)/src/system \
$(CHIP_ROOT)/third_party/openthread/repo/include \
$(NRF5_PLATFORM_DIR)/app/include \
$(NRF5_PLATFORM_DIR)/app/project_include \
$(NRF5_PLATFORM_DIR)/util/include \
Expand Down Expand Up @@ -210,6 +212,7 @@ LIBS = \
-lopenthread-nrf52840-transport \
-lopenthread-nrf52840-softdevice-sdk \
-lnordicsemi-nrf52840-radio-driver-softdevice \
-lSetupPayload \
$(NRF5_SDK_ROOT)/external/openthread/nrf_security/lib/libmbedcrypto_glue.a \
$(NRF5_SDK_ROOT)/external/openthread/nrf_security/lib/libmbedcrypto_glue_cc310.a \
$(NRF5_SDK_ROOT)/external/openthread/nrf_security/lib/libmbedcrypto_glue_vanilla.a \
Expand Down
32 changes: 30 additions & 2 deletions examples/lock-app/nrf5/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
#include "FreeRTOS.h"

#include <platform/CHIPDeviceLayer.h>
#if CHIP_ENABLE_OPENTHREAD
#include <platform/ThreadStackManager.h>
#endif
#include <support/ErrorStr.h>

#define FACTORY_RESET_TRIGGER_TIMEOUT 3000
#define FACTORY_RESET_CANCEL_WINDOW_TIMEOUT 3000
Expand Down Expand Up @@ -102,6 +106,9 @@ int AppTask::Init()
static app_button_cfg_t sButtons[] = {
{ LOCK_BUTTON, APP_BUTTON_ACTIVE_LOW, BUTTON_PULL, ButtonEventHandler },
{ FUNCTION_BUTTON, APP_BUTTON_ACTIVE_LOW, BUTTON_PULL, ButtonEventHandler },
#if CHIP_ENABLE_OPENTHREAD
{ JOIN_BUTTON, APP_BUTTON_ACTIVE_LOW, BUTTON_PULL, ButtonEventHandler },
#endif
};

ret = app_button_init(sButtons, ARRAY_SIZE(sButtons), pdMS_TO_TICKS(FUNCTION_BUTTON_DEBOUNCE_PERIOD_MS));
Expand Down Expand Up @@ -160,7 +167,7 @@ void AppTask::AppTaskMain(void * pvParameter)
ret = sAppTask.Init();
if (ret != NRF_SUCCESS)
{
NRF_LOG_INFO("AppTask.Init() failed");
NRF_LOG_INFO("AppTask.Init() failed: %s", chip::ErrorStr(ret));
APP_ERROR_HANDLER(ret);
}

Expand Down Expand Up @@ -270,9 +277,24 @@ void AppTask::LockActionEventHandler(AppEvent * aEvent)
}
}

#if CHIP_ENABLE_OPENTHREAD
void AppTask::JoinHandler(AppEvent * aEvent)
{
if (aEvent->ButtonEvent.PinNo != JOIN_BUTTON)
return;

CHIP_ERROR error = ThreadStackMgr().JoinerStart();
NRF_LOG_INFO("Thread joiner triggered: %s", chip::ErrorStr(error));
}
#endif

void AppTask::ButtonEventHandler(uint8_t pin_no, uint8_t button_action)
{
if (pin_no != LOCK_BUTTON && pin_no != FUNCTION_BUTTON)
if (pin_no != LOCK_BUTTON
#if CHIP_ENABLE_OPENTHREAD
&& pin_no != JOIN_BUTTON
#endif
&& pin_no != FUNCTION_BUTTON)
{
return;
}
Expand All @@ -290,6 +312,12 @@ void AppTask::ButtonEventHandler(uint8_t pin_no, uint8_t button_action)
{
button_event.Handler = FunctionHandler;
}
#if CHIP_ENABLE_OPENTHREAD
else if (pin_no == JOIN_BUTTON && button_action == APP_BUTTON_RELEASE)
{
button_event.Handler = JoinHandler;
}
#endif

sAppTask.PostEvent(&button_event);
}
Expand Down
1 change: 1 addition & 0 deletions examples/lock-app/nrf5/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class AppTask

static void FunctionTimerEventHandler(AppEvent * aEvent);
static void FunctionHandler(AppEvent * aEvent);
static void JoinHandler(AppEvent * aEvent);
static void LockActionEventHandler(AppEvent * aEvent);

static void ButtonEventHandler(uint8_t pin_no, uint8_t button_action);
Expand Down
3 changes: 2 additions & 1 deletion examples/lock-app/nrf5/main/include/app_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,15 @@
#define BUTTON_ENABLED 1

#define GPIOTE_ENABLED 1
#define GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS 2
#define GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS 3

#define APP_TIMER_CONFIG_OP_QUEUE_SIZE 10

// ---- Lock Example App Config ----

#define LOCK_BUTTON BUTTON_2
#define FUNCTION_BUTTON BUTTON_1
#define JOIN_BUTTON BUTTON_4
#define FUNCTION_BUTTON_DEBOUNCE_PERIOD_MS 50

#define SYSTEM_STATE_LED BSP_LED_0
Expand Down
7 changes: 7 additions & 0 deletions src/include/platform/ThreadStackManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class ThreadStackManager
CHIP_ERROR GetAndLogThreadTopologyFull(void);
CHIP_ERROR GetPrimary802154MACAddress(uint8_t * buf);

CHIP_ERROR JoinerStart(void);

private:
// ===== Members for internal use by the following friends.

Expand Down Expand Up @@ -295,6 +297,11 @@ inline CHIP_ERROR ThreadStackManager::GetPrimary802154MACAddress(uint8_t * buf)
return static_cast<ImplClass *>(this)->_GetPrimary802154MACAddress(buf);
}

inline CHIP_ERROR ThreadStackManager::JoinerStart(void)
{
return static_cast<ImplClass *>(this)->_JoinerStart();
}

} // namespace DeviceLayer
} // namespace chip

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,14 +551,14 @@ CHIP_ERROR GenericConfigurationManagerImpl<ImplClass>::_GetSetupPinCode(uint32_t
CHIP_ERROR err;

err = Impl()->ReadConfigValue(ImplClass::kConfigKey_SetupPinCode, setupPinCode);
#ifdef CHIP_DEVICE_CONFIG_USE_TEST_SETUP_PIN_CODE
if (CHIP_DEVICE_CONFIG_USE_TEST_SETUP_PIN_CODE != 0 && err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
#if defined(CHIP_DEVICE_CONFIG_USE_TEST_SETUP_PIN_CODE) && CHIP_DEVICE_CONFIG_USE_TEST_SETUP_PIN_CODE
if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
{
setupPinCode = CHIP_DEVICE_CONFIG_USE_TEST_SETUP_PIN_CODE;
ChipLogProgress(DeviceLayer, "Pairing code not found; using default: %08u", CHIP_DEVICE_CONFIG_USE_TEST_SETUP_PIN_CODE);
ChipLogProgress(DeviceLayer, "Setup PIN code not found; using default: %08u", CHIP_DEVICE_CONFIG_USE_TEST_SETUP_PIN_CODE);
err = CHIP_NO_ERROR;
}
#endif // CHIP_DEVICE_CONFIG_USE_TEST_SETUP_PIN_CODE
#endif // defined(CHIP_DEVICE_CONFIG_USE_TEST_SETUP_PIN_CODE) && CHIP_DEVICE_CONFIG_USE_TEST_SETUP_PIN_CODE
SuccessOrExit(err);

exit:
Expand All @@ -577,15 +577,15 @@ CHIP_ERROR GenericConfigurationManagerImpl<ImplClass>::_GetSetupDiscriminator(ui
CHIP_ERROR err;

err = Impl()->ReadConfigValue(ImplClass::kConfigKey_SetupDiscriminator, setupDiscriminator);
#ifdef CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR
if (CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR != 0 && err == CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR)
#if defined(CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR) && CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR
if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
{
setupDiscriminator = CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR;
ChipLogProgress(DeviceLayer, "Pairing code not found; using default: %03x",
ChipLogProgress(DeviceLayer, "Setup PIN discriminator not found; using default: %03x",
CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR);
err = CHIP_NO_ERROR;
}
#endif // CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR
#endif // defined(CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR) && CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR
SuccessOrExit(err);

exit:
Expand Down
5 changes: 5 additions & 0 deletions src/platform/Linux/ThreadStackManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ CHIP_ERROR ThreadStackManagerImpl::_GetPrimary802154MACAddress(uint8_t * buf)
return OTBR_TO_CHIP_ERROR(error);
}

CHIP_ERROR ThreadStackManagerImpl::_JoinerStart(void)
{
return CHIP_ERROR_NOT_IMPLEMENTED;
}

// TODO: Implement after we decide on the dbus message loop
extern ThreadStackManager & ThreadStackMgr(void);

Expand Down
2 changes: 2 additions & 0 deletions src/platform/Linux/ThreadStackManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class ThreadStackManagerImpl : public ThreadStackManager

CHIP_ERROR _GetPrimary802154MACAddress(uint8_t * buf);

CHIP_ERROR _JoinerStart(void);

~ThreadStackManagerImpl() = default;

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,17 @@ class GenericThreadStackManagerImpl_OpenThread
bool IsThreadAttachedNoLock(void);
CHIP_ERROR AdjustPollingInterval(void);

CHIP_ERROR _JoinerStart(void);

private:
// ===== Private members for use by this class only.

otInstance * mOTInst;
ConnectivityManager::ThreadPollingConfig mPollingConfig;

static void OnJoinerComplete(otError aError, void * aContext);
void OnJoinerComplete(otError aError);

inline ImplClass * Impl() { return static_cast<ImplClass *>(this); }
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <openthread/cli.h>
#include <openthread/dataset.h>
#include <openthread/dataset_ftd.h>
#include <openthread/joiner.h>
#include <openthread/link.h>
#include <openthread/netdata.h>
#include <openthread/tasklet.h>
Expand Down Expand Up @@ -875,6 +876,72 @@ void GenericThreadStackManagerImpl_OpenThread<ImplClass>::_ErasePersistentInfo(v
Impl()->UnlockThreadStack();
}

template <class ImplClass>
void GenericThreadStackManagerImpl_OpenThread<ImplClass>::OnJoinerComplete(otError aError, void * aContext)
{
static_cast<GenericThreadStackManagerImpl_OpenThread *>(aContext)->OnJoinerComplete(aError);
}

template <class ImplClass>
void GenericThreadStackManagerImpl_OpenThread<ImplClass>::OnJoinerComplete(otError aError)
{
ChipLogProgress(DeviceLayer, "Join Thread network: %s", otThreadErrorToString(aError));

if (aError == OT_ERROR_NONE)
{
otError error = otThreadSetEnabled(mOTInst, true);

ChipLogProgress(DeviceLayer, "Start Thread network: %s", otThreadErrorToString(error));
}
}

template <class ImplClass>
CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_JoinerStart(void)
{
CHIP_ERROR error = CHIP_NO_ERROR;

Impl()->LockThreadStack();
VerifyOrExit(!otDatasetIsCommissioned(mOTInst) && otThreadGetDeviceRole(mOTInst) == OT_DEVICE_ROLE_DISABLED,
error = MapOpenThreadError(OT_ERROR_INVALID_STATE));
VerifyOrExit(otJoinerGetState(mOTInst) == OT_JOINER_STATE_IDLE, error = MapOpenThreadError(OT_ERROR_BUSY));

if (!otIp6IsEnabled(mOTInst))
{
SuccessOrExit(error = MapOpenThreadError(otIp6SetEnabled(mOTInst, true)));
}

{
otJoinerDiscerner discerner;
uint32_t discriminator;

SuccessOrExit(error = ConfigurationMgr().GetSetupDiscriminator(discriminator));
discerner.mLength = 12;
discerner.mValue = discriminator;

ChipLogProgress(DeviceLayer, "Joiner Discerner: %u", discriminator);
otJoinerSetDiscerner(mOTInst, &discerner);
}

{
otJoinerPskd pskd;
uint32_t pincode;

SuccessOrExit(error = ConfigurationMgr().GetSetupPinCode(pincode));
snprintf(pskd.m8, sizeof(pskd.m8) - 1, "%u", pincode);

ChipLogProgress(DeviceLayer, "Joiner PSKd: %u", pincode);
error = MapOpenThreadError(otJoinerStart(mOTInst, pskd.m8, NULL, NULL, NULL, NULL, NULL,
&GenericThreadStackManagerImpl_OpenThread::OnJoinerComplete, this));
}

exit:
Impl()->UnlockThreadStack();

ChipLogProgress(DeviceLayer, "Joiner start: %s", chip::ErrorStr(error));

return error;
}

} // namespace Internal
} // namespace DeviceLayer
} // namespace chip
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread_LwIP<ImplClass>::DoInit(otIn
// Initialize member data.
memset(mAddrAssigned, 0, sizeof(mAddrAssigned));

sThreadNetIf.name[0] = 'o';
sThreadNetIf.name[1] = 't';

// Initialize the base class.
err = GenericThreadStackManagerImpl_OpenThread<ImplClass>::DoInit(otInst);
SuccessOrExit(err);
Expand Down
4 changes: 2 additions & 2 deletions src/platform/nRF5/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@
// ========== Platform-specific Configuration Overrides =========

#ifndef CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE
#define CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE 8192
#define CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE 4096
#endif // CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE

#ifndef CHIP_DEVICE_CONFIG_THREAD_TASK_STACK_SIZE
#define CHIP_DEVICE_CONFIG_THREAD_TASK_STACK_SIZE 8192
#define CHIP_DEVICE_CONFIG_THREAD_TASK_STACK_SIZE 4096
#endif // CHIP_DEVICE_CONFIG_THREAD_TASK_STACK_SIZE

#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_TELEMETRY 0
Expand Down
2 changes: 1 addition & 1 deletion third_party/openthread/repo
Submodule repo updated 58 files
+100 −0 .github/workflows/docker.yml
+1 −4 .github/workflows/simulation.yml
+2 −0 Android.mk
+2 −0 BUILD.gn
+5 −0 etc/cmake/options.cmake
+5 −0 examples/common-switches.mk
+2 −0 script/test
+2 −0 src/core/CMakeLists.txt
+6 −0 src/core/Makefile.am
+0 −1 src/core/api/border_router_api.cpp
+1 −1 src/core/api/ip6_api.cpp
+16 −10 src/core/api/thread_ftd_api.cpp
+4 −0 src/core/backbone_router/bbr_leader.cpp
+125 −0 src/core/backbone_router/bbr_manager.cpp
+99 −0 src/core/backbone_router/bbr_manager.hpp
+1 −1 src/core/coap/coap_message.cpp
+2 −2 src/core/coap/coap_message.hpp
+144 −0 src/core/common/bit_vector.hpp
+12 −0 src/core/common/instance.hpp
+4 −11 src/core/common/settings.cpp
+94 −24 src/core/common/settings.hpp
+4 −0 src/core/config/openthread-core-config-check.h
+10 −0 src/core/config/openthread-core-default-config.h
+1 −1 src/core/mac/data_poll_handler.hpp
+25 −0 src/core/net/ip6_address.cpp
+23 −0 src/core/net/ip6_address.hpp
+6 −3 src/core/net/netif.cpp
+207 −2 src/core/net/netif.hpp
+2 −78 src/core/thread/child_mask.hpp
+35 −25 src/core/thread/mesh_forwarder.cpp
+9 −18 src/core/thread/mle.cpp
+10 −30 src/core/thread/mle_router.cpp
+0 −15 src/core/thread/mle_router.hpp
+3 −3 src/core/thread/mle_types.hpp
+373 −0 src/core/thread/mlr_manager.cpp
+138 −0 src/core/thread/mlr_manager.hpp
+64 −0 src/core/thread/mlr_types.hpp
+10 −0 src/core/thread/router_table.hpp
+4 −0 src/core/thread/thread_netif.cpp
+10 −1 src/core/thread/thread_netif.hpp
+64 −0 src/core/thread/thread_tlvs.hpp
+8 −0 src/core/thread/thread_uri_paths.hpp
+33 −25 src/core/thread/topology.cpp
+160 −24 src/core/thread/topology.hpp
+6 −0 src/core/utils/otns.cpp
+8 −0 src/core/utils/otns.hpp
+3 −1 src/ncp/CMakeLists.txt
+3 −0 tests/scripts/thread-cert/config.py
+19 −1 tests/scripts/thread-cert/network_layer.py
+5 −0 tests/scripts/thread-cert/node.py
+1 −1 tests/scripts/thread-cert/simulator.py
+400 −0 tests/scripts/thread-cert/v1_2_test_multicast_listener_registration.py
+100 −17 tests/unit/test_child.cpp
+57 −6 tests/unit/test_toolchain.cpp
+8 −8 third_party/NordicSemiconductor/libraries/nrf_security/config/nrf-config.h
+529 −354 tools/harness-thci/OpenThread.py
+ tools/harness-thci/OpenThread_BR.png
+288 −0 tools/harness-thci/OpenThread_BR.py