Skip to content
This repository has been archived by the owner on Oct 27, 2022. It is now read-only.

Commit

Permalink
Pivot to event-based evaluation of provisioning state.
Browse files Browse the repository at this point in the history
  • Loading branch information
turon committed Oct 25, 2019
1 parent e420765 commit d178b9b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 22 deletions.
1 change: 0 additions & 1 deletion main/include/OpenThreadConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,4 @@

#define OPENTHREAD_CONFIG_NORDIC_BLE_CFG_TAG NRF_BLE_CFG_TAG


#endif // OPENTHREAD_PLATFORM_CONFIG_H
4 changes: 3 additions & 1 deletion main/include/ble_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@
// ---- BLE Config ----
#define NRF_BLE_CFG_TAG 1

#define WEAVE_DEVICE_CONFIG_WOBLE_SINGLE_CONNECTION 1

#endif // BLE_CONFIG_H

#endif // BLE_CONFIG_H
70 changes: 51 additions & 19 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ extern "C" {

#include <AppTask.h>

#define APP_DEBUG_DISABLE_WOBLE_WHEN_THREAD_PROVISIONED 0

using namespace ::nl;
using namespace ::nl::Inet;
using namespace ::nl::Weave;
Expand Down Expand Up @@ -139,6 +141,37 @@ extern "C" void JLINK_MONITOR_OnPoll(void)

#endif // JLINK_MMD

/**
* Updates the application state based on the provisioning state.
* If unprovisioned, enables WoBLE for pairing.
* Once fully provisioned, disables WoBLE and enables Thread (ToBLE).
*/
static void EvaluateAppProvisioningState()
{
bool isFullyProvisioned = ConnectivityMgr().IsThreadProvisioned();

#if !APP_DEBUG_DISABLE_WOBLE_WHEN_THREAD_PROVISIONED
isFullyProvisioned = (isFullyProvisioned
&& ConfigurationMgr().IsMemberOfFabric()
&& ConfigurationMgr().IsServiceProvisioned()
&& ConfigurationMgr().IsPairedToAccount());
#endif

ConnectivityMgr().SetBLEAdvertisingEnabled(!isFullyProvisioned);
ConnectivityMgr().SetThreadMode(isFullyProvisioned ?
ConnectivityManager::kThreadMode_Enabled :
ConnectivityManager::kThreadMode_Disabled);

if (isFullyProvisioned)
{
NRF_LOG_INFO("WoBLE pairing is disabled, as device is provisioned.");
}
else
{
NRF_LOG_INFO("WoBLE pairing is enabled.");
}
}

// ================================================================================
// Main Code
// ================================================================================
Expand Down Expand Up @@ -301,19 +334,8 @@ int main(void)
}

#if OPENTHREAD_CONFIG_ENABLE_TOBLE
// Disable Weave Pairing if Thread network has already been provisioned.
// TODO: Change this to only disable WoBLE when *full provisioning* has completed,
// i.e. ConfigurationManager.IsPairedToAccount()
if (ConnectivityMgr().IsThreadProvisioned())
{
ConnectivityMgr().SetBLEAdvertisingEnabled(false);
NRF_LOG_INFO("WoBLE pairing is disabled, as Thread is provisioned");
}
else
EvaluateAppProvisioningState();
#endif // OPENTHREAD_CONFIG_ENABLE_TOBLE
{
NRF_LOG_INFO("WoBLE pairing is enabled");
}

NRF_LOG_INFO("Starting Weave task");

Expand Down Expand Up @@ -365,12 +387,22 @@ int main(void)
* NOTE: This function runs on the Weave event loop task.
*/
void DeviceEventHandler(const WeaveDeviceEvent * event, intptr_t arg)
{
if (event->Type == DeviceEventType::kSessionEstablished &&
event->SessionEstablished.IsCommissioner)
{
switch (event->Type)
{
// Disable advertising when Commissioner connected over WoBLE
// so ToBLE connectivity checks can occur.
ConnectivityMgr().SetBLEAdvertisingEnabled(false);
}
case DeviceEventType::kThreadStateChange:
if (!event->ThreadStateChange.RoleChanged)
{
break;
}

// else fall-through

case DeviceEventType::kServiceProvisioningChange:
case DeviceEventType::kFabricMembershipChange:
case DeviceEventType::kAccountPairingChange:
{
EvaluateAppProvisioningState();
}
};
}

0 comments on commit d178b9b

Please sign in to comment.