diff --git a/examples/lighting-app/nrfconnect/CMakeLists.txt b/examples/lighting-app/nrfconnect/CMakeLists.txt index b7d2989f640e87..cbc3cb12c685d6 100644 --- a/examples/lighting-app/nrfconnect/CMakeLists.txt +++ b/examples/lighting-app/nrfconnect/CMakeLists.txt @@ -36,6 +36,7 @@ target_sources(app PRIVATE ${LIGHTING_COMMON}/gen/callback-stub.c ${LIGHTING_COMMON}/gen/znet-bookkeeping.c ${NRFCONNECT_COMMON}/util/LEDWidget.cpp + ${NRFCONNECT_COMMON}/util/ThreadUtil.cpp ${CHIP_APP_SERVER}/DataModelHandler.cpp ${CHIP_APP_SERVER}/Server.cpp ${CHIP_APP_SERVER}/QRCodeUtil.cpp diff --git a/examples/lighting-app/nrfconnect/main/AppTask.cpp b/examples/lighting-app/nrfconnect/main/AppTask.cpp index 52e7820136c281..be90e31a9e0bd2 100644 --- a/examples/lighting-app/nrfconnect/main/AppTask.cpp +++ b/examples/lighting-app/nrfconnect/main/AppTask.cpp @@ -24,6 +24,7 @@ #include "LightingManager.h" #include "QRCodeUtil.h" #include "Server.h" +#include "ThreadUtil.h" #include @@ -219,11 +220,11 @@ void AppTask::ButtonEventHandler(uint32_t button_state, uint32_t has_changed) sAppTask.PostEvent(&button_event); } - if (JOINER_BUTTON_MASK & button_state & has_changed) + if (THREAD_START_BUTTON_MASK & button_state & has_changed) { - button_event.ButtonEvent.PinNo = JOINER_BUTTON; + button_event.ButtonEvent.PinNo = THREAD_START_BUTTON; button_event.ButtonEvent.Action = kButtonPushEvent; - button_event.Handler = JoinerHandler; + button_event.Handler = StartThreadHandler; sAppTask.PostEvent(&button_event); } } @@ -307,18 +308,22 @@ void AppTask::FunctionHandler(AppEvent * aEvent) } } -void AppTask::JoinerHandler(AppEvent * aEvent) +void AppTask::StartThreadHandler(AppEvent * aEvent) { - if (aEvent->ButtonEvent.PinNo != JOINER_BUTTON) + if (aEvent->ButtonEvent.PinNo != THREAD_START_BUTTON) return; - CHIP_ERROR error = CHIP_ERROR_NOT_IMPLEMENTED; - #if CHIP_DEVICE_CONFIG_ENABLE_THREAD - error = ThreadStackMgr().JoinerStart(); + if (!chip::DeviceLayer::ConnectivityMgr().IsThreadProvisioned()) + { + StartDefaultThreadNetwork(); + LOG_INF("Device is not commissioned to a Thread network. Starting with the default configuration."); + } + else + { + LOG_INF("Device is commissioned to a Thread network."); + } #endif - - LOG_INF("Thread joiner triggering result: %s", log_strdup(chip::ErrorStr(error))); } void AppTask::CancelTimer() diff --git a/examples/lighting-app/nrfconnect/main/include/AppConfig.h b/examples/lighting-app/nrfconnect/main/include/AppConfig.h index 3d71cb8dd7e136..110d0fa24fa889 100644 --- a/examples/lighting-app/nrfconnect/main/include/AppConfig.h +++ b/examples/lighting-app/nrfconnect/main/include/AppConfig.h @@ -25,8 +25,8 @@ #define LIGHTING_BUTTON_MASK DK_BTN2_MSK #define FUNCTION_BUTTON DK_BTN1 #define FUNCTION_BUTTON_MASK DK_BTN1_MSK -#define JOINER_BUTTON DK_BTN3 -#define JOINER_BUTTON_MASK DK_BTN3_MSK +#define THREAD_START_BUTTON DK_BTN3 +#define THREAD_START_BUTTON_MASK DK_BTN3_MSK #define SYSTEM_STATE_LED DK_LED1 // led0 in device tree #define LIGHTING_GPIO_DEVICE_NAME DT_GPIO_LABEL(DT_ALIAS(led1), gpios) diff --git a/examples/lighting-app/nrfconnect/main/include/AppTask.h b/examples/lighting-app/nrfconnect/main/include/AppTask.h index 4f1a8ec442485c..f7c0065b499356 100644 --- a/examples/lighting-app/nrfconnect/main/include/AppTask.h +++ b/examples/lighting-app/nrfconnect/main/include/AppTask.h @@ -48,7 +48,7 @@ class AppTask static void FunctionTimerEventHandler(AppEvent * aEvent); static void FunctionHandler(AppEvent * aEvent); - static void JoinerHandler(AppEvent * aEvent); + static void StartThreadHandler(AppEvent * aEvent); static void LightingActionEventHandler(AppEvent * aEvent); static void ButtonEventHandler(uint32_t button_state, uint32_t has_changed); diff --git a/examples/lighting-app/nrfconnect/prj.conf b/examples/lighting-app/nrfconnect/prj.conf index 094dfbf26e4c22..33e54eda8769c8 100644 --- a/examples/lighting-app/nrfconnect/prj.conf +++ b/examples/lighting-app/nrfconnect/prj.conf @@ -21,6 +21,12 @@ # Add support for LEDs and buttons on Nordic development kits CONFIG_DK_LIBRARY=y +# Default OpenThread network settings +CONFIG_OPENTHREAD_PANID=4660 +CONFIG_OPENTHREAD_CHANNEL=15 +CONFIG_OPENTHREAD_NETWORK_NAME="OpenThread" +CONFIG_OPENTHREAD_XPANID="11:11:11:11:22:22:22:22" + # Long log strings CONFIG_LOG_STRDUP_MAX_STRING=256 CONFIG_LOG_STRDUP_BUF_COUNT=20 diff --git a/examples/lock-app/nrfconnect/CMakeLists.txt b/examples/lock-app/nrfconnect/CMakeLists.txt index e518de24bb1749..b3d462673728b9 100644 --- a/examples/lock-app/nrfconnect/CMakeLists.txt +++ b/examples/lock-app/nrfconnect/CMakeLists.txt @@ -36,6 +36,7 @@ target_sources(app PRIVATE ${LOCK_COMMON}/gen/callback-stub.c ${LOCK_COMMON}/gen/znet-bookkeeping.c ${NRFCONNECT_COMMON}/util/LEDWidget.cpp + ${NRFCONNECT_COMMON}/util/ThreadUtil.cpp ${CHIP_APP_SERVER}/DataModelHandler.cpp ${CHIP_APP_SERVER}/Server.cpp ${CHIP_APP_SERVER}/QRCodeUtil.cpp diff --git a/examples/lock-app/nrfconnect/main/AppTask.cpp b/examples/lock-app/nrfconnect/main/AppTask.cpp index d81e2a580c7dbc..7a2187c9f98d26 100644 --- a/examples/lock-app/nrfconnect/main/AppTask.cpp +++ b/examples/lock-app/nrfconnect/main/AppTask.cpp @@ -22,6 +22,7 @@ #include "LEDWidget.h" #include "QRCodeUtil.h" #include "Server.h" +#include "ThreadUtil.h" #include @@ -213,11 +214,11 @@ void AppTask::ButtonEventHandler(uint32_t button_state, uint32_t has_changed) sAppTask.PostEvent(&button_event); } - if (JOINER_BUTTON_MASK & button_state & has_changed) + if (THREAD_START_BUTTON_MASK & button_state & has_changed) { - button_event.ButtonEvent.PinNo = JOINER_BUTTON; + button_event.ButtonEvent.PinNo = THREAD_START_BUTTON; button_event.ButtonEvent.Action = BUTTON_PUSH_EVENT; - button_event.Handler = JoinerHandler; + button_event.Handler = StartThreadHandler; sAppTask.PostEvent(&button_event); } } @@ -310,18 +311,22 @@ void AppTask::FunctionHandler(AppEvent * aEvent) } } -void AppTask::JoinerHandler(AppEvent * aEvent) +void AppTask::StartThreadHandler(AppEvent * aEvent) { - if (aEvent->ButtonEvent.PinNo != JOINER_BUTTON) + if (aEvent->ButtonEvent.PinNo != THREAD_START_BUTTON) return; - CHIP_ERROR error = CHIP_ERROR_NOT_IMPLEMENTED; - #if CHIP_DEVICE_CONFIG_ENABLE_THREAD - error = ThreadStackMgr().JoinerStart(); + if (!chip::DeviceLayer::ConnectivityMgr().IsThreadProvisioned()) + { + StartDefaultThreadNetwork(); + LOG_INF("Device is not commissioned to a Thread network. Starting with the default configuration."); + } + else + { + LOG_INF("Device is commissioned to a Thread network."); + } #endif - - LOG_INF("Thread joiner triggering result: %s", log_strdup(chip::ErrorStr(error))); } void AppTask::CancelTimer() diff --git a/examples/lock-app/nrfconnect/main/include/AppConfig.h b/examples/lock-app/nrfconnect/main/include/AppConfig.h index 2018eb7cabb1f9..270d55e0fb18ac 100644 --- a/examples/lock-app/nrfconnect/main/include/AppConfig.h +++ b/examples/lock-app/nrfconnect/main/include/AppConfig.h @@ -25,8 +25,8 @@ #define LOCK_BUTTON_MASK DK_BTN2_MSK #define FUNCTION_BUTTON DK_BTN1 #define FUNCTION_BUTTON_MASK DK_BTN1_MSK -#define JOINER_BUTTON DK_BTN3 -#define JOINER_BUTTON_MASK DK_BTN3_MSK +#define THREAD_START_BUTTON DK_BTN3 +#define THREAD_START_BUTTON_MASK DK_BTN3_MSK #define SYSTEM_STATE_LED DK_LED1 #define LOCK_STATE_LED DK_LED2 diff --git a/examples/lock-app/nrfconnect/main/include/AppTask.h b/examples/lock-app/nrfconnect/main/include/AppTask.h index 5a2085787200c8..6c85259fe0dd3f 100644 --- a/examples/lock-app/nrfconnect/main/include/AppTask.h +++ b/examples/lock-app/nrfconnect/main/include/AppTask.h @@ -47,7 +47,7 @@ class AppTask static void FunctionTimerEventHandler(AppEvent * aEvent); static void FunctionHandler(AppEvent * aEvent); - static void JoinerHandler(AppEvent * aEvent); + static void StartThreadHandler(AppEvent * aEvent); static void LockActionEventHandler(AppEvent * aEvent); static void ButtonEventHandler(uint32_t buttons_state, uint32_t has_changed); diff --git a/examples/lock-app/nrfconnect/prj.conf b/examples/lock-app/nrfconnect/prj.conf index 094dfbf26e4c22..33e54eda8769c8 100644 --- a/examples/lock-app/nrfconnect/prj.conf +++ b/examples/lock-app/nrfconnect/prj.conf @@ -21,6 +21,12 @@ # Add support for LEDs and buttons on Nordic development kits CONFIG_DK_LIBRARY=y +# Default OpenThread network settings +CONFIG_OPENTHREAD_PANID=4660 +CONFIG_OPENTHREAD_CHANNEL=15 +CONFIG_OPENTHREAD_NETWORK_NAME="OpenThread" +CONFIG_OPENTHREAD_XPANID="11:11:11:11:22:22:22:22" + # Long log strings CONFIG_LOG_STRDUP_MAX_STRING=256 CONFIG_LOG_STRDUP_BUF_COUNT=20 diff --git a/examples/platform/nrfconnect/util/ThreadUtil.cpp b/examples/platform/nrfconnect/util/ThreadUtil.cpp new file mode 100644 index 00000000000000..2c9bd9d3251424 --- /dev/null +++ b/examples/platform/nrfconnect/util/ThreadUtil.cpp @@ -0,0 +1,51 @@ +/* + * + * Copyright (c) 2020 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ThreadUtil.h" + +#include +#include + +#include + +#include + +void StartDefaultThreadNetwork(void) +{ + chip::DeviceLayer::Internal::DeviceNetworkInfo deviceNetworkInfo; + memset(&deviceNetworkInfo, 0, sizeof(deviceNetworkInfo)); + + const uint8_t masterKey[chip::DeviceLayer::Internal::kThreadNetworkKeyLength] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, + 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, + 0xcc, 0xdd, 0xee, 0xff }; + const uint8_t threadMeshPrefix[chip::DeviceLayer::Internal::kThreadMeshPrefixLength] = { 0xfd, 0x11, 0x11, 0x11, + 0x11, 0x22, 0x00, 0x00 }; + + memcpy(deviceNetworkInfo.ThreadNetworkName, CONFIG_OPENTHREAD_NETWORK_NAME, strlen(CONFIG_OPENTHREAD_NETWORK_NAME)); + net_bytes_from_str(deviceNetworkInfo.ThreadExtendedPANId, 8, CONFIG_OPENTHREAD_XPANID); + deviceNetworkInfo.FieldPresent.ThreadExtendedPANId = true; + memcpy(deviceNetworkInfo.ThreadNetworkKey, masterKey, sizeof(masterKey)); + deviceNetworkInfo.FieldPresent.ThreadMeshPrefix = true; + memcpy(deviceNetworkInfo.ThreadMeshPrefix, threadMeshPrefix, sizeof(threadMeshPrefix)); + deviceNetworkInfo.ThreadPANId = CONFIG_OPENTHREAD_PANID; + deviceNetworkInfo.ThreadChannel = CONFIG_OPENTHREAD_CHANNEL; + + chip::DeviceLayer::ThreadStackMgr().SetThreadEnabled(false); + chip::DeviceLayer::ThreadStackMgr().SetThreadProvision(deviceNetworkInfo); + chip::DeviceLayer::ThreadStackMgr().SetThreadEnabled(true); +} diff --git a/examples/platform/nrfconnect/util/include/ThreadUtil.h b/examples/platform/nrfconnect/util/include/ThreadUtil.h new file mode 100644 index 00000000000000..d3154c7ffe5e86 --- /dev/null +++ b/examples/platform/nrfconnect/util/include/ThreadUtil.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2020 Project CHIP Authors + * 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 THREAD_UTIL_H +#define THREAD_UTIL_H + +void StartDefaultThreadNetwork(void); + +#endif // THREAD_UTIL_H