Skip to content

Commit

Permalink
Revert "[nrf fromtree] Remove test mode from examples (project-chip#2…
Browse files Browse the repository at this point in the history
…1676)"

This reverts commit 238a18d.
  • Loading branch information
kkasperczyk-no committed Aug 31, 2022
1 parent 4081f47 commit 9f7d6f5
Show file tree
Hide file tree
Showing 30 changed files with 289 additions and 7 deletions.
16 changes: 16 additions & 0 deletions config/nrfconnect/chip-module/Kconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,22 @@ config NET_L2_OPENTHREAD

if NET_L2_OPENTHREAD

config OPENTHREAD_PANID
int
default 4660

config OPENTHREAD_CHANNEL
int
default 15

config OPENTHREAD_NETWORK_NAME
string
default "OpenThread"

config OPENTHREAD_XPANID
string
default "11:11:11:11:22:22:22:22"

# Increase the default RX stack size
config IEEE802154_NRF5_RX_STACK_SIZE
int
Expand Down
1 change: 1 addition & 0 deletions examples/chef/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ target_sources(app PRIVATE
${CHEF}/common/stubs.cpp
${GEN_DIR}/callback-stub.cpp
${GEN_DIR}/IMClusterCommandHandler.cpp
$<$<BOOL:${CONFIG_NET_L2_OPENTHREAD}>:${NRFCONNECT_COMMON}/util/ThreadUtil.cpp>
)

message(STATUS ${CHEF}/devices/${SAMPLE_NAME}.zap)
Expand Down
3 changes: 2 additions & 1 deletion examples/light-switch-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ target_sources(app PRIVATE
main/BindingHandler.cpp
${GEN_DIR}/light-switch-app/zap-generated/callback-stub.cpp
${GEN_DIR}/light-switch-app/zap-generated/IMClusterCommandHandler.cpp
${NRFCONNECT_COMMON}/util/LEDWidget.cpp)
${NRFCONNECT_COMMON}/util/LEDWidget.cpp
$<$<BOOL:${CONFIG_NET_L2_OPENTHREAD}>:${NRFCONNECT_COMMON}/util/ThreadUtil.cpp>)


if(CONFIG_CHIP_OTA_REQUESTOR)
Expand Down
5 changes: 5 additions & 0 deletions examples/light-switch-app/nrfconnect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ has Thread disabled, and it should be paired with Matter controller and get
configuration from it. Some actions required before establishing full
communication are described below.

The example also comes with a test mode, which allows to start Thread with the
default settings by pressing button manually. However, this mode does not
guarantee that the device will be able to communicate with the Matter controller
and other devices.

The example can be configured to use the secure bootloader and utilize it for
performing over-the-air Device Firmware Upgrade using Bluetooth LE.

Expand Down
3 changes: 3 additions & 0 deletions examples/light-switch-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include "AppConfig.h"
#include "LEDWidget.h"
#include "LightSwitch.h"
#ifdef CONFIG_NET_L2_OPENTHREAD
#include "ThreadUtil.h"
#endif

#include <DeviceInfoProviderImpl.h>
#include <app/clusters/identify-server/identify-server.h>
Expand Down
3 changes: 2 additions & 1 deletion examples/lighting-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ target_sources(app PRIVATE
${GEN_DIR}/lighting-app/zap-generated/callback-stub.cpp
${GEN_DIR}/lighting-app/zap-generated/IMClusterCommandHandler.cpp
${NRFCONNECT_COMMON}/util/LEDWidget.cpp
${NRFCONNECT_COMMON}/util/PWMDevice.cpp)
${NRFCONNECT_COMMON}/util/PWMDevice.cpp
$<$<BOOL:${CONFIG_NET_L2_OPENTHREAD}>:${NRFCONNECT_COMMON}/util/ThreadUtil.cpp>)

chip_configure_data_model(app
INCLUDE_SERVER
Expand Down
8 changes: 8 additions & 0 deletions examples/lighting-app/nrfconnect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ Thread disabled, and it should be paired with Matter controller and get
configuration from it. Some actions required before establishing full
communication are described below.

The example also comes with a test mode, which allows to start Thread with the
default settings by pressing button manually. However, this mode does not
guarantee that the device will be able to communicate with the Matter controller
and other devices.

The example can be configured to use the secure bootloader and utilize it for
performing over-the-air Device Firmware Upgrade using Bluetooth LE.

Expand Down Expand Up @@ -236,6 +241,9 @@ effect.
**Button 2** &mdash; Pressing the button once changes the lighting state to the
opposite one.

**Button 3** &mdash; Pressing the button once starts the Thread networking in
the test mode using the default configuration.

**Button 4** &mdash; Pressing the button once starts the NFC tag emulation and
enables Bluetooth LE advertising for the predefined period of time (15 minutes
by default).
Expand Down
31 changes: 31 additions & 0 deletions examples/lighting-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#include "AppEvent.h"
#include "LEDWidget.h"
#include "PWMDevice.h"
#ifdef CONFIG_NET_L2_OPENTHREAD
#include "ThreadUtil.h"
#endif

#include <DeviceInfoProviderImpl.h>
#include <app-common/zap-generated/attribute-id.h>
Expand Down Expand Up @@ -273,6 +276,16 @@ void AppTask::ButtonEventHandler(uint32_t button_state, uint32_t has_changed)
sAppTask.PostEvent(&button_event);
}

#ifdef CONFIG_NET_L2_OPENTHREAD
if (THREAD_START_BUTTON_MASK & button_state & has_changed)
{
button_event.ButtonEvent.PinNo = THREAD_START_BUTTON;
button_event.ButtonEvent.Action = kButtonPushEvent;
button_event.Handler = StartThreadHandler;
sAppTask.PostEvent(&button_event);
}
#endif

if (BLE_ADVERTISEMENT_START_BUTTON_MASK & button_state & has_changed)
{
button_event.ButtonEvent.PinNo = BLE_ADVERTISEMENT_START_BUTTON;
Expand Down Expand Up @@ -394,6 +407,24 @@ void AppTask::FunctionHandler(AppEvent * aEvent)
}
}

#ifdef CONFIG_NET_L2_OPENTHREAD
void AppTask::StartThreadHandler(AppEvent * aEvent)
{
if (aEvent->ButtonEvent.PinNo != THREAD_START_BUTTON)
return;

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

void AppTask::StartBLEAdvertisementHandler(AppEvent *)
{
if (Server::GetInstance().GetFabricTable().FabricCount() != 0)
Expand Down
2 changes: 2 additions & 0 deletions examples/lighting-app/nrfconnect/main/include/AppConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#define LIGHTING_BUTTON_MASK DK_BTN2_MSK
#define FUNCTION_BUTTON DK_BTN1
#define FUNCTION_BUTTON_MASK DK_BTN1_MSK
#define THREAD_START_BUTTON DK_BTN3
#define THREAD_START_BUTTON_MASK DK_BTN3_MSK
#define BLE_ADVERTISEMENT_START_BUTTON DK_BTN4
#define BLE_ADVERTISEMENT_START_BUTTON_MASK DK_BTN4_MSK

Expand Down
4 changes: 3 additions & 1 deletion examples/lighting-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ class AppTask
static void UpdateLedStateEventHandler(AppEvent * aEvent);
static void FunctionTimerEventHandler(AppEvent * aEvent);
static void FunctionHandler(AppEvent * aEvent);

#ifdef CONFIG_NET_L2_OPENTHREAD
static void StartThreadHandler(AppEvent * aEvent);
#endif
static void LightingActionEventHandler(AppEvent * aEvent);
static void StartBLEAdvertisementHandler(AppEvent * aEvent);

Expand Down
3 changes: 2 additions & 1 deletion examples/lock-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ target_sources(app PRIVATE
main/ZclCallbacks.cpp
${GEN_DIR}/lock-app/zap-generated/callback-stub.cpp
${GEN_DIR}/lock-app/zap-generated/IMClusterCommandHandler.cpp
${NRFCONNECT_COMMON}/util/LEDWidget.cpp)
${NRFCONNECT_COMMON}/util/LEDWidget.cpp
$<$<BOOL:${CONFIG_NET_L2_OPENTHREAD}>:${NRFCONNECT_COMMON}/util/ThreadUtil.cpp>)

chip_configure_data_model(app
INCLUDE_SERVER
Expand Down
8 changes: 8 additions & 0 deletions examples/lock-app/nrfconnect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ Thread disabled, and it should be paired with Matter controller and get
configuration from it. Some actions required before establishing full
communication are described below.

The example also comes with a test mode, which allows to start Thread with the
default settings by pressing button manually. However, this mode does not
guarantee that the device will be able to communicate with the Matter controller
and other devices.

The example can be configured to use the secure bootloader and utilize it for
performing over-the-air Device Firmware Upgrade using Bluetooth LE.

Expand Down Expand Up @@ -222,6 +227,9 @@ states are possible:
**Button 2** &mdash; Pressing the button once changes the lock state to the
opposite one.

**Button 3** &mdash; Pressing the button once starts the Thread networking in
the test mode using the default configuration.

**Button 4** &mdash; Pressing the button once starts the NFC tag emulation and
enables Bluetooth LE advertising for the predefined period of time (15 minutes
by default).
Expand Down
31 changes: 31 additions & 0 deletions examples/lock-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include "AppConfig.h"
#include "BoltLockManager.h"
#include "LEDWidget.h"
#ifdef CONFIG_NET_L2_OPENTHREAD
#include "ThreadUtil.h"
#endif

#include <DeviceInfoProviderImpl.h>
#include <app-common/zap-generated/attribute-id.h>
Expand Down Expand Up @@ -252,6 +255,16 @@ void AppTask::ButtonEventHandler(uint32_t button_state, uint32_t has_changed)
sAppTask.PostEvent(&button_event);
}

#ifdef CONFIG_NET_L2_OPENTHREAD
if (THREAD_START_BUTTON_MASK & button_state & has_changed)
{
button_event.ButtonEvent.PinNo = THREAD_START_BUTTON;
button_event.ButtonEvent.Action = BUTTON_PUSH_EVENT;
button_event.Handler = StartThreadHandler;
sAppTask.PostEvent(&button_event);
}
#endif

if (BLE_ADVERTISEMENT_START_BUTTON_MASK & button_state & has_changed)
{
button_event.ButtonEvent.PinNo = BLE_ADVERTISEMENT_START_BUTTON;
Expand Down Expand Up @@ -368,6 +381,24 @@ void AppTask::FunctionHandler(AppEvent * aEvent)
}
}

#ifdef CONFIG_NET_L2_OPENTHREAD
void AppTask::StartThreadHandler(AppEvent * aEvent)
{
if (aEvent->ButtonEvent.PinNo != THREAD_START_BUTTON)
return;

if (!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

void AppTask::StartBLEAdvertisementHandler(AppEvent *)
{
if (Server::GetInstance().GetFabricTable().FabricCount() != 0)
Expand Down
2 changes: 2 additions & 0 deletions examples/lock-app/nrfconnect/main/include/AppConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#define LOCK_BUTTON_MASK DK_BTN2_MSK
#define FUNCTION_BUTTON DK_BTN1
#define FUNCTION_BUTTON_MASK DK_BTN1_MSK
#define THREAD_START_BUTTON DK_BTN3
#define THREAD_START_BUTTON_MASK DK_BTN3_MSK
#define BLE_ADVERTISEMENT_START_BUTTON DK_BTN4
#define BLE_ADVERTISEMENT_START_BUTTON_MASK DK_BTN4_MSK

Expand Down
3 changes: 3 additions & 0 deletions examples/lock-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class AppTask
static void UpdateLedStateEventHandler(AppEvent * aEvent);
static void FunctionTimerEventHandler(AppEvent * aEvent);
static void FunctionHandler(AppEvent * aEvent);
#ifdef CONFIG_NET_L2_OPENTHREAD
static void StartThreadHandler(AppEvent * aEvent);
#endif
static void LockActionEventHandler(AppEvent * aEvent);
static void StartBLEAdvertisementHandler(AppEvent * aEvent);

Expand Down
50 changes: 50 additions & 0 deletions examples/platform/nrfconnect/util/ThreadUtil.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
*
* 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 <platform/CHIPDeviceLayer.h>

#include <app/server/Dnssd.h>
#include <lib/support/ThreadOperationalDataset.h>

#include <zephyr/zephyr.h>

#include <cstring>

void StartDefaultThreadNetwork(void)
{
chip::Thread::OperationalDataset dataset{};
uint8_t xpanid[8];
constexpr uint8_t masterkey[] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
};
net_bytes_from_str(xpanid, sizeof(xpanid), CONFIG_OPENTHREAD_XPANID);

dataset.SetChannel(CONFIG_OPENTHREAD_CHANNEL);
dataset.SetExtendedPanId(xpanid);
dataset.SetMasterKey(masterkey);
dataset.SetNetworkName(CONFIG_OPENTHREAD_NETWORK_NAME);
dataset.SetPanId(CONFIG_OPENTHREAD_PANID);

chip::DeviceLayer::ThreadStackMgr().SetThreadEnabled(false);
chip::DeviceLayer::ThreadStackMgr().SetThreadProvision(dataset.AsByteSpan());
chip::DeviceLayer::ThreadStackMgr().SetThreadEnabled(true);

chip::app::DnssdServer::Instance().StartServer();
}
20 changes: 20 additions & 0 deletions examples/platform/nrfconnect/util/include/ThreadUtil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.
*/

#pragma once

void StartDefaultThreadNetwork(void);
3 changes: 2 additions & 1 deletion examples/pump-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ target_sources(app PRIVATE
main/ZclCallbacks.cpp
${GEN_DIR}/pump-app/zap-generated/callback-stub.cpp
${GEN_DIR}/pump-app/zap-generated/IMClusterCommandHandler.cpp
${NRFCONNECT_COMMON}/util/LEDWidget.cpp)
${NRFCONNECT_COMMON}/util/LEDWidget.cpp
$<$<BOOL:${CONFIG_NET_L2_OPENTHREAD}>:${NRFCONNECT_COMMON}/util/ThreadUtil.cpp>)

chip_configure_data_model(app
INCLUDE_SERVER
Expand Down
8 changes: 8 additions & 0 deletions examples/pump-app/nrfconnect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ Thread disabled, and it should be paired with Matter controller and get
configuration from it. Some actions required before establishing full
communication are described below.

The example also comes with a test mode, which allows to start Thread with the
default settings by pressing button manually. However, this mode does not
guarantee that the device will be able to communicate with the Matter controller
and other devices.

The example can be configured to use the secure bootloader and utilize it for
performing over-the-air Device Firmware Upgrade using Bluetooth LE.

Expand Down Expand Up @@ -215,6 +220,9 @@ following states are possible:
**Button 2** &mdash; Pressing the button once changes the pump state to the
opposite one.

**Button 3** &mdash; Pressing the button once starts the Thread networking in
the test mode using the default configuration.

**Button 4** &mdash; Pressing the button once starts the NFC tag emulation and
enables Bluetooth LE advertising for the predefined period of time (15 minutes
by default).
Expand Down
Loading

0 comments on commit 9f7d6f5

Please sign in to comment.