Skip to content

Commit

Permalink
Added mechanism for leaving the last fabric to the Lock example.
Browse files Browse the repository at this point in the history
For testing...
  • Loading branch information
ArekBalysNordic committed Sep 21, 2023
1 parent bfa9b4c commit a345a16
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 1 deletion.
16 changes: 16 additions & 0 deletions config/nrfconnect/chip-module/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ config CHIP_OTA_REQUESTOR
imply STREAM_FLASH
imply STREAM_FLASH_ERASE

config CHIP_LAST_FABRIC_REMOVED_REBOOT
bool "Reboot when the last fabric is removed"
default y
help
When the last fabric is removed this config allows to reboot the device
automatically.

config CHIP_LAST_FABRIC_REMOVED_PAIRING_AUTOSTART
bool "Turn on Bluetooth LE advertising when the last fabric is removed"
default y if !CHIP_LAST_FABRIC_REMOVED_REBOOT
depends on !CHIP_LAST_FABRIC_REMOVED_REBOOT
help
After removing of the last fabric the device will start Bluetooth LE
advertising automatically if the reboot has not been requested by
CHIP_LAST_FABRIC_REMOVED_REBOOT config.

config CHIP_OTA_REQUESTOR_BUFFER_SIZE
int "OTA Requestor image buffer size"
default 1024
Expand Down
2 changes: 1 addition & 1 deletion config/nrfconnect/chip-module/Kconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ endif # BOARD_NRF7002DK_NRF5340_CPUAPP

# Enable extended discovery
config CHIP_EXTENDED_DISCOVERY
default y
default n

config NVS_LOOKUP_CACHE
default y
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 @@ -469,6 +469,27 @@ void AppTask::StartBLEAdvertisementHandler(const AppEvent &)
}
}

void AppTask::LastFabricRemovedHandler(const AppEvent & event)
{
sHaveBLEConnections = false;

#ifdef CONFIG_CHIP_LAST_FABRIC_REMOVED_REBOOT
chip::Server::GetInstance().ScheduleFactoryReset();
#else
/* Erase Matter data */
PersistedStorage::KeyValueStoreMgrImpl().DoFactoryReset();
/* Erase Thread credentials and disconnect */
ConnectivityMgr().ErasePersistentInfo();
#ifdef CONFIG_CHIP_LAST_FABRIC_REMOVED_PAIRING_AUTOSTART
/* Start the New BLE advertising */
StartBLEAdvertisementHandler(event);
#endif
#endif

/* Update the LED status */
UpdateStatusLED();
}

void AppTask::UpdateLedStateEventHandler(const AppEvent & event)
{
if (event.Type == AppEventType::UpdateLedState)
Expand Down Expand Up @@ -543,6 +564,16 @@ void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */
InitBasicOTARequestor();
#endif // CONFIG_CHIP_OTA_REQUESTOR
break;
case DeviceEventType::kThreadSrpUpdateEvent:
if (Server::GetInstance().GetFabricTable().FabricCount() == 0 && sIsNetworkProvisioned && sIsNetworkEnabled)
{
LOG_INF("\n\nLast fabric EVENT\n\n");
AppEvent event;
event.Type = AppEventType::LastFabricRemove;
event.Handler = LastFabricRemovedHandler;
PostEvent(event);
}
break;
case DeviceEventType::kThreadStateChange:
sIsNetworkProvisioned = ConnectivityMgr().IsThreadProvisioned();
sIsNetworkEnabled = ConnectivityMgr().IsThreadEnabled();
Expand Down
1 change: 1 addition & 0 deletions examples/lock-app/nrfconnect/main/include/AppEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ enum class AppEventType : uint8_t
UpdateLedState,
IdentifyStart,
IdentifyStop,
LastFabricRemove
};

enum class FunctionEvent : uint8_t
Expand Down
1 change: 1 addition & 0 deletions examples/lock-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class AppTask
static void LockActionEventHandler(const AppEvent & event);
static void StartBLEAdvertisementHandler(const AppEvent & event);
static void UpdateLedStateEventHandler(const AppEvent & event);
static void LastFabricRemovedHandler(const AppEvent & event);

static void ChipEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg);
static void ButtonEventHandler(uint32_t buttonState, uint32_t hasChanged);
Expand Down
6 changes: 6 additions & 0 deletions src/include/platform/CHIPDeviceEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,12 @@ enum PublicEventTypes
* An application event occured that should wake up the system/device
*/
kAppWakeUpEvent,

/**
* Signals that current session has been ended
*
*/
kThreadSrpUpdateEvent,
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,14 @@ void GenericThreadStackManagerImpl_OpenThread<ImplClass>::OnSrpClientNotificatio
otService = next;
} while (otService);
}

DeviceLayer::ChipDeviceEvent event;
event.Type = DeviceLayer::DeviceEventType::PublicEventTypes::kThreadSrpUpdateEvent;
CHIP_ERROR status = DeviceLayer::PlatformMgr().PostEvent(&event);
if (status != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Failed to post SRP update event: %" CHIP_ERROR_FORMAT, status.Format());
}
break;
}
case OT_ERROR_PARSE:
Expand Down

0 comments on commit a345a16

Please sign in to comment.