Skip to content

Commit

Permalink
* Schedule initialisation of Server to not mix with OpenThread Platfo…
Browse files Browse the repository at this point in the history
…rmEvents

* Only open commissioning window when no fabric exists (lighting-app)
  • Loading branch information
tima-q committed Jul 19, 2022
1 parent ea78432 commit 308e7a5
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 51 deletions.
5 changes: 3 additions & 2 deletions examples/lighting-app/qpg/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ class AppTask
friend AppTask & GetAppTask(void);

CHIP_ERROR Init();
static void InitServer(intptr_t arg);
static void OpenCommissioning(intptr_t arg);

static void ActionInitiated(LightingManager::Action_t aAction);
static void ActionCompleted(LightingManager::Action_t aAction);

void CancelTimer(void);

void DispatchEvent(AppEvent * event);

static void FunctionTimerEventHandler(AppEvent * aEvent);
Expand All @@ -63,6 +63,7 @@ class AppTask
static void TimerEventHandler(chip::System::Layer * aLayer, void * aAppState);

void StartTimer(uint32_t aTimeoutMs);
void CancelTimer(void);

enum Function_t
{
Expand Down
66 changes: 40 additions & 26 deletions examples/lighting-app/qpg/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,28 +208,8 @@ CHIP_ERROR AppTask::StartAppTask()
return CHIP_NO_ERROR;
}

CHIP_ERROR AppTask::Init()
void AppTask::InitServer(intptr_t arg)
{
CHIP_ERROR err = CHIP_NO_ERROR;

ChipLogProgress(NotSpecified, "Current Software Version: %s", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING);

err = LightingMgr().Init();
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "LightingMgr().Init() failed");
return err;
}
LightingMgr().SetCallbacks(ActionInitiated, ActionCompleted);

// Subscribe with our button callback to the qvCHIP button handler.
qvIO_SetBtnCallback(ButtonEventHandler);

#if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
chip::app::DnssdServer::Instance().SetExtendedDiscoveryTimeoutSecs(extDiscTimeoutSecs);
#endif

// Init ZCL Data Model
static chip::CommonCaseDeviceServerInitParams initParams;
(void) initParams.InitializeStaticResourcesBeforeServerInit();

Expand All @@ -243,18 +223,52 @@ CHIP_ERROR AppTask::Init()
initParams.endpointNativeParams = static_cast<void *>(&nativeParams);
chip::Server::GetInstance().Init(initParams);

#if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
chip::app::DnssdServer::Instance().SetExtendedDiscoveryTimeoutSecs(extDiscTimeoutSecs);
#endif

// Open commissioning after boot if no fabric was available
if (chip::Server::GetInstance().GetFabricTable().FabricCount() == 0)
{
PlatformMgr().ScheduleWork(OpenCommissioning, 0);
}
}

void AppTask::OpenCommissioning(intptr_t arg)
{
// Enable BLE advertisements
chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow();
ChipLogProgress(NotSpecified, "BLE advertising started. Waiting for Pairing.");
}

CHIP_ERROR AppTask::Init()
{
CHIP_ERROR err = CHIP_NO_ERROR;

ChipLogProgress(NotSpecified, "Current Software Version: %s", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING);

// Init ZCL Data Model and start server
PlatformMgr().ScheduleWork(InitServer, 0);

// Initialize device attestation config
SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider());

UpdateClusterState();
// Setup light
err = LightingMgr().Init();
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "LightingMgr().Init() failed");
return err;
}
LightingMgr().SetCallbacks(ActionInitiated, ActionCompleted);

// Setup button handler
qvIO_SetBtnCallback(ButtonEventHandler);

// Log device configuration
ConfigurationMgr().LogDeviceConfig();
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));

// Enable BLE advertisements
chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow();
ChipLogProgress(NotSpecified, "BLE advertising started. Waiting for Pairing.");

return err;
}

Expand Down
1 change: 1 addition & 0 deletions examples/lock-app/qpg/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class AppTask
friend AppTask & GetAppTask(void);

CHIP_ERROR Init();
static void InitServer(intptr_t arg);

static void ActionInitiated(BoltLockManager::Action_t aAction, int32_t aActor);
static void ActionCompleted(BoltLockManager::Action_t aAction);
Expand Down
50 changes: 27 additions & 23 deletions examples/lock-app/qpg/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,38 @@ CHIP_ERROR AppTask::StartAppTask()
return CHIP_NO_ERROR;
}

void AppTask::InitServer(intptr_t arg)
{
static chip::CommonCaseDeviceServerInitParams initParams;
(void) initParams.InitializeStaticResourcesBeforeServerInit();

gExampleDeviceInfoProvider.SetStorageDelegate(initParams.persistentStorageDelegate);
chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);

chip::Inet::EndPointStateOpenThread::OpenThreadEndpointInitParam nativeParams;
nativeParams.lockCb = LockOpenThreadTask;
nativeParams.unlockCb = UnlockOpenThreadTask;
nativeParams.openThreadInstancePtr = chip::DeviceLayer::ThreadStackMgrImpl().OTInstance();
initParams.endpointNativeParams = static_cast<void *>(&nativeParams);
chip::Server::GetInstance().Init(initParams);

#if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
chip::app::DnssdServer::Instance().SetExtendedDiscoveryTimeoutSecs(extDiscTimeoutSecs);
#endif
}
CHIP_ERROR AppTask::Init()
{
CHIP_ERROR err = CHIP_NO_ERROR;

ChipLogProgress(NotSpecified, "Current Software Version: %s", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING);

// Init ZCL Data Model and start server
PlatformMgr().ScheduleWork(InitServer, 0);

// Initialize device attestation config
SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider());

// Setup Bolt
err = BoltLockMgr().Init();
if (err != CHIP_NO_ERROR)
{
Expand All @@ -122,32 +148,11 @@ CHIP_ERROR AppTask::Init()
}
BoltLockMgr().SetCallbacks(ActionInitiated, ActionCompleted);

// Subscribe with our button callback to the qvCHIP button handler.
// Setup button handler
qvIO_SetBtnCallback(ButtonEventHandler);

qvIO_LedSet(LOCK_STATE_LED, !BoltLockMgr().IsUnlocked());

#if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
chip::app::DnssdServer::Instance().SetExtendedDiscoveryTimeoutSecs(extDiscTimeoutSecs);
#endif

// Init ZCL Data Model
static chip::CommonCaseDeviceServerInitParams initParams;
(void) initParams.InitializeStaticResourcesBeforeServerInit();

gExampleDeviceInfoProvider.SetStorageDelegate(initParams.persistentStorageDelegate);
chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);

chip::Inet::EndPointStateOpenThread::OpenThreadEndpointInitParam nativeParams;
nativeParams.lockCb = LockOpenThreadTask;
nativeParams.unlockCb = UnlockOpenThreadTask;
nativeParams.openThreadInstancePtr = chip::DeviceLayer::ThreadStackMgrImpl().OTInstance();
initParams.endpointNativeParams = static_cast<void *>(&nativeParams);
chip::Server::GetInstance().Init(initParams);

// Initialize device attestation config
SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider());

UpdateClusterState();

ConfigurationMgr().LogDeviceConfig();
Expand All @@ -164,7 +169,6 @@ void AppTask::AppTaskMain(void * pvParameter)
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "AppTask.Init() failed: %" CHIP_ERROR_FORMAT, err.Format());
return;
}

ChipLogProgress(NotSpecified, "App Task started");
Expand Down

0 comments on commit 308e7a5

Please sign in to comment.