blob: 3306379bbe41da9bbea1346a5e49a1b0c1dcfa4d [file] [log] [blame]
/*
*
* Copyright (c) 2021 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 "qvIO.h"
#include "AppConfig.h"
#include "AppEvent.h"
#include "AppTask.h"
#include "ota.h"
#include <app/server/OnboardingCodesUtil.h>
#include <app-common/zap-generated/attribute-id.h>
#include <app-common/zap-generated/attribute-type.h>
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app/server/Dnssd.h>
#include <app/server/Server.h>
#include <app/util/attribute-storage.h>
#include <credentials/DeviceAttestationCredsProvider.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>
#include <inet/EndPointStateOpenThread.h>
#include <DeviceInfoProviderImpl.h>
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>
using namespace ::chip;
using namespace ::chip::app;
using namespace chip::TLV;
using namespace chip::Credentials;
using namespace chip::DeviceLayer;
#include <platform/CHIPDeviceLayer.h>
#define FACTORY_RESET_TRIGGER_TIMEOUT 3000
#define FACTORY_RESET_CANCEL_WINDOW_TIMEOUT 3000
#define OTA_START_TRIGGER_TIMEOUT 1500
#define APP_TASK_STACK_SIZE (2 * 1024)
#define APP_TASK_PRIORITY 2
#define APP_EVENT_QUEUE_SIZE 10
#define QPG_LOCK_ENDPOINT_ID (1)
namespace {
TaskHandle_t sAppTaskHandle;
QueueHandle_t sAppEventQueue;
bool sIsThreadProvisioned = false;
bool sIsThreadEnabled = false;
bool sHaveBLEConnections = false;
uint8_t sAppEventQueueBuffer[APP_EVENT_QUEUE_SIZE * sizeof(AppEvent)];
StaticQueue_t sAppEventQueueStruct;
StackType_t appStack[APP_TASK_STACK_SIZE / sizeof(StackType_t)];
StaticTask_t appTaskStruct;
chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider;
} // namespace
AppTask AppTask::sAppTask;
namespace {
constexpr int extDiscTimeoutSecs = 20;
}
void LockOpenThreadTask(void)
{
chip::DeviceLayer::ThreadStackMgr().LockThreadStack();
}
void UnlockOpenThreadTask(void)
{
chip::DeviceLayer::ThreadStackMgr().UnlockThreadStack();
}
CHIP_ERROR AppTask::StartAppTask()
{
sAppEventQueue = xQueueCreateStatic(APP_EVENT_QUEUE_SIZE, sizeof(AppEvent), sAppEventQueueBuffer, &sAppEventQueueStruct);
if (sAppEventQueue == nullptr)
{
ChipLogError(NotSpecified, "Failed to allocate app event queue");
return CHIP_ERROR_NO_MEMORY;
}
// Start App task.
sAppTaskHandle = xTaskCreateStatic(AppTaskMain, APP_TASK_NAME, ArraySize(appStack), nullptr, 1, appStack, &appTaskStruct);
if (sAppTaskHandle == nullptr)
{
return CHIP_ERROR_NO_MEMORY;
}
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;
PlatformMgr().AddEventHandler(MatterEventHandler, 0);
ChipLogProgress(NotSpecified, "Current Software Version: %s", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING);
// Init ZCL Data Model and start server
PlatformMgr().ScheduleWork(InitServer, 0);
ReturnErrorOnFailure(mFactoryDataProvider.Init());
SetDeviceInstanceInfoProvider(&mFactoryDataProvider);
SetCommissionableDataProvider(&mFactoryDataProvider);
SetDeviceAttestationCredentialsProvider(&mFactoryDataProvider);
// Setup Bolt
err = BoltLockMgr().Init();
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "BoltLockMgr().Init() failed");
return err;
}
BoltLockMgr().SetCallbacks(ActionInitiated, ActionCompleted);
// Setup button handler
qvIO_SetBtnCallback(ButtonEventHandler);
qvIO_LedSet(LOCK_STATE_LED, !BoltLockMgr().IsUnlocked());
UpdateClusterState();
ConfigurationMgr().LogDeviceConfig();
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));
UpdateLEDs();
return err;
}
void AppTask::AppTaskMain(void * pvParameter)
{
AppEvent event;
while (true)
{
BaseType_t eventReceived = xQueueReceive(sAppEventQueue, &event, portMAX_DELAY);
while (eventReceived == pdTRUE)
{
sAppTask.DispatchEvent(&event);
eventReceived = xQueueReceive(sAppEventQueue, &event, 0);
}
}
}
void AppTask::LockActionEventHandler(AppEvent * aEvent)
{
bool initiated = false;
BoltLockManager::Action_t action;
int32_t actor;
CHIP_ERROR err = CHIP_NO_ERROR;
if (aEvent->Type == AppEvent::kEventType_Lock)
{
action = static_cast<BoltLockManager::Action_t>(aEvent->LockEvent.Action);
actor = aEvent->LockEvent.Actor;
}
else if (aEvent->Type == AppEvent::kEventType_Button)
{
if (BoltLockMgr().IsUnlocked())
{
action = BoltLockManager::LOCK_ACTION;
}
else
{
action = BoltLockManager::UNLOCK_ACTION;
}
actor = AppEvent::kEventType_Button;
}
else
{
err = CHIP_ERROR_INTERNAL;
}
if (err == CHIP_NO_ERROR)
{
initiated = BoltLockMgr().InitiateAction(actor, action);
if (!initiated)
{
ChipLogProgress(NotSpecified, "Action is already in progress or active.");
}
}
}
void AppTask::ButtonEventHandler(uint8_t btnIdx, bool btnPressed)
{
if (btnIdx != APP_LOCK_BUTTON && btnIdx != APP_FUNCTION_BUTTON)
{
return;
}
AppEvent button_event = {};
button_event.Type = AppEvent::kEventType_Button;
button_event.ButtonEvent.ButtonIdx = btnIdx;
button_event.ButtonEvent.Action = btnPressed;
if (btnIdx == APP_LOCK_BUTTON && btnPressed == true)
{
button_event.Handler = LockActionEventHandler;
}
else if (btnIdx == APP_FUNCTION_BUTTON)
{
// Hand off to Functionality handler - depends on duration of press
button_event.Handler = FunctionHandler;
}
else
{
return;
}
sAppTask.PostEvent(&button_event);
}
void AppTask::TimerEventHandler(chip::System::Layer * aLayer, void * aAppState)
{
AppEvent event;
event.Type = AppEvent::kEventType_Timer;
event.TimerEvent.Context = aAppState;
event.Handler = FunctionTimerEventHandler;
sAppTask.PostEvent(&event);
}
void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
{
if (aEvent->Type != AppEvent::kEventType_Timer)
{
return;
}
// If we reached here, the button was held past OTA_START_TRIGGER_TIMEOUT,
// initiate OTA update
if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_StartBleAdv)
{
ChipLogProgress(NotSpecified, "[BTN] Release button now to start Software Updater");
ChipLogProgress(NotSpecified, "[BTN] Hold to trigger Factory Reset");
sAppTask.mFunction = kFunction_SoftwareUpdate;
sAppTask.StartTimer(FACTORY_RESET_TRIGGER_TIMEOUT - OTA_START_TRIGGER_TIMEOUT);
}
else if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_SoftwareUpdate)
{
ChipLogProgress(NotSpecified, "[BTN] Factory Reset selected. Release within %us to cancel.",
FACTORY_RESET_CANCEL_WINDOW_TIMEOUT / 1000);
// Start timer for FACTORY_RESET_CANCEL_WINDOW_TIMEOUT to allow user to
// cancel, if required.
sAppTask.StartTimer(FACTORY_RESET_CANCEL_WINDOW_TIMEOUT);
sAppTask.mFunction = kFunction_FactoryReset;
// Turn off all LEDs before starting blink to make sure blink is
// co-ordinated.
qvIO_LedSet(SYSTEM_STATE_LED, false);
qvIO_LedSet(LOCK_STATE_LED, false);
qvIO_LedBlink(SYSTEM_STATE_LED, 500, 500);
qvIO_LedBlink(LOCK_STATE_LED, 500, 500);
}
else if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_FactoryReset)
{
// Actually trigger Factory Reset
sAppTask.mFunction = kFunction_NoneSelected;
chip::Server::GetInstance().ScheduleFactoryReset();
}
}
void AppTask::FunctionHandler(AppEvent * aEvent)
{
if (aEvent->ButtonEvent.ButtonIdx != APP_FUNCTION_BUTTON)
{
return;
}
// To trigger BLE advertising: press the APP_FUNCTION_BUTTON button briefly (<
// OTA_START_TRIGGER_TIMEOUT). To trigger software update: press the button
// between 1.5sec and 3sec. To initiate factory reset: press the
// APP_FUNCTION_BUTTON for FACTORY_RESET_TRIGGER_TIMEOUT +
// FACTORY_RESET_CANCEL_WINDOW_TIMEOUT All LEDs start blinking after
// FACTORY_RESET_TRIGGER_TIMEOUT to signal factory reset has been initiated.
// To cancel factory reset: release the APP_FUNCTION_BUTTON once all LEDs
// start blinking within the FACTORY_RESET_CANCEL_WINDOW_TIMEOUT
if (aEvent->ButtonEvent.Action == true)
{
if (!sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_NoneSelected)
{
ChipLogProgress(NotSpecified, "[BTN] Hold to select function:");
ChipLogProgress(NotSpecified, "[BTN] - Trigger BLE adv (0-1.5s)");
ChipLogProgress(NotSpecified, "[BTN] - Trigger OTA (1.5-3s)");
ChipLogProgress(NotSpecified, "[BTN] - Factory Reset (>6s)");
sAppTask.StartTimer(OTA_START_TRIGGER_TIMEOUT);
sAppTask.mFunction = kFunction_StartBleAdv;
}
}
else
{
// If the button was released before 1.5sec, trigger BLE advertising.
if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_StartBleAdv)
{
sAppTask.CancelTimer();
sAppTask.mFunction = kFunction_NoneSelected;
if (ConnectivityMgr().IsBLEAdvertisingEnabled())
{
ChipLogProgress(NotSpecified, "BLE advertising already in progress.");
}
else
{
if (!ConnectivityMgr().IsThreadProvisioned())
{
// Enable BLE advertisements and pairing window
if (chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow() == CHIP_NO_ERROR)
{
ChipLogProgress(NotSpecified, "BLE advertising started. Waiting for Pairing.");
}
}
else
{
ChipLogError(NotSpecified, "Network is already provisioned, BLE advertisement not enabled");
}
}
}
else if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_SoftwareUpdate)
{
sAppTask.CancelTimer();
sAppTask.mFunction = kFunction_NoneSelected;
ChipLogProgress(NotSpecified, "[BTN] Triggering OTA Query");
TriggerOTAQuery();
}
else if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_FactoryReset)
{
// Set lock status LED back to show state of lock.
qvIO_LedSet(LOCK_STATE_LED, !BoltLockMgr().IsUnlocked());
sAppTask.CancelTimer();
// Change the function to none selected since factory reset has been
// canceled.
sAppTask.mFunction = kFunction_NoneSelected;
ChipLogProgress(NotSpecified, "[BTN] Factory Reset has been Canceled");
}
}
}
void AppTask::CancelTimer()
{
chip::DeviceLayer::SystemLayer().CancelTimer(TimerEventHandler, this);
mFunctionTimerActive = false;
}
void AppTask::StartTimer(uint32_t aTimeoutInMs)
{
CHIP_ERROR err;
chip::DeviceLayer::SystemLayer().CancelTimer(TimerEventHandler, this);
err = chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Milliseconds32(aTimeoutInMs), TimerEventHandler, this);
SuccessOrExit(err);
mFunctionTimerActive = true;
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "StartTimer failed %s: ", chip::ErrorStr(err));
}
}
void AppTask::ActionInitiated(BoltLockManager::Action_t aAction, int32_t aActor)
{
// If the action has been initiated by the lock, update the bolt lock trait
// and start flashing the LEDs rapidly to indicate action initiation.
if (aAction == BoltLockManager::LOCK_ACTION)
{
ChipLogProgress(NotSpecified, "Lock Action has been initiated");
}
else if (aAction == BoltLockManager::UNLOCK_ACTION)
{
ChipLogProgress(NotSpecified, "Unlock Action has been initiated");
}
if (aActor == AppEvent::kEventType_Button)
{
sAppTask.mSyncClusterToButtonAction = true;
}
qvIO_LedBlink(LOCK_STATE_LED, 50, 50);
}
void AppTask::ActionCompleted(BoltLockManager::Action_t aAction)
{
// if the action has been completed by the lock, update the bolt lock trait.
// Turn on the lock LED if in a LOCKED state OR
// Turn off the lock LED if in an UNLOCKED state.
if (aAction == BoltLockManager::LOCK_ACTION)
{
ChipLogProgress(NotSpecified, "Lock Action has been completed");
qvIO_LedSet(LOCK_STATE_LED, true);
}
else if (aAction == BoltLockManager::UNLOCK_ACTION)
{
ChipLogProgress(NotSpecified, "Unlock Action has been completed");
qvIO_LedSet(LOCK_STATE_LED, false);
}
if (sAppTask.mSyncClusterToButtonAction)
{
sAppTask.UpdateClusterState();
sAppTask.mSyncClusterToButtonAction = false;
}
}
void AppTask::PostLockActionRequest(int32_t aActor, BoltLockManager::Action_t aAction)
{
AppEvent event;
event.Type = AppEvent::kEventType_Lock;
event.LockEvent.Actor = aActor;
event.LockEvent.Action = aAction;
event.Handler = LockActionEventHandler;
PostEvent(&event);
}
void AppTask::PostEvent(const AppEvent * aEvent)
{
if (sAppEventQueue != NULL)
{
if (!xQueueSend(sAppEventQueue, aEvent, 1))
{
ChipLogError(NotSpecified, "Failed to post event to app task event queue");
}
}
else
{
ChipLogError(NotSpecified, "Event Queue is NULL should never happen");
}
}
void AppTask::DispatchEvent(AppEvent * aEvent)
{
if (aEvent->Handler)
{
aEvent->Handler(aEvent);
}
else
{
ChipLogError(NotSpecified, "Event received with no handler. Dropping event.");
}
}
/**
* Update cluster status after application level changes
*/
void AppTask::UpdateClusterState(void)
{
using namespace chip::app::Clusters;
auto newValue = BoltLockMgr().IsUnlocked() ? DoorLock::DlLockState::kUnlocked : DoorLock::DlLockState::kLocked;
ChipLogProgress(NotSpecified, "UpdateClusterState");
EmberAfStatus status = DoorLock::Attributes::LockState::Set(DOOR_LOCK_SERVER_ENDPOINT, newValue);
if (status != EMBER_ZCL_STATUS_SUCCESS)
{
ChipLogError(NotSpecified, "ERR: updating DoorLock %x", status);
}
}
void AppTask::UpdateLEDs(void)
{
// If system has "full connectivity", keep the LED On constantly.
//
// If thread and service provisioned, but not attached to the thread network
// yet OR no connectivity to the service OR subscriptions are not fully
// established THEN blink the LED Off for a short period of time.
//
// If the system has ble connection(s) uptill the stage above, THEN blink
// the LEDs at an even rate of 100ms.
//
// Otherwise, blink the LED ON for a very short time.
if (sIsThreadProvisioned && sIsThreadEnabled)
{
qvIO_LedBlink(SYSTEM_STATE_LED, 950, 50);
}
else if (sHaveBLEConnections)
{
qvIO_LedBlink(SYSTEM_STATE_LED, 100, 100);
}
else
{
qvIO_LedBlink(SYSTEM_STATE_LED, 50, 950);
}
}
void AppTask::MatterEventHandler(const ChipDeviceEvent * event, intptr_t)
{
switch (event->Type)
{
case DeviceEventType::kServiceProvisioningChange: {
sIsThreadProvisioned = event->ServiceProvisioningChange.IsServiceProvisioned;
UpdateLEDs();
break;
}
case DeviceEventType::kThreadConnectivityChange: {
sIsThreadEnabled = (event->ThreadConnectivityChange.Result == kConnectivity_Established);
UpdateLEDs();
break;
}
case DeviceEventType::kCHIPoBLEConnectionEstablished: {
sHaveBLEConnections = true;
UpdateLEDs();
break;
}
case DeviceEventType::kCHIPoBLEConnectionClosed: {
sHaveBLEConnections = false;
UpdateLEDs();
break;
}
default:
break;
}
}