blob: b7de3c24492917bd68fa17642cf990fd2c64e9b7 [file] [log] [blame]
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2019 Google LLC.
* 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 "AppTask.h"
#include "AppEvent.h"
#include "DataModelHandler.h"
#include "LEDWidget.h"
#include "Server.h"
#include "app_button.h"
#include "app_config.h"
#include "app_timer.h"
#include "boards.h"
#include "nrf_log.h"
#include "FreeRTOS.h"
#include <platform/CHIPDeviceLayer.h>
#if CHIP_ENABLE_OPENTHREAD
#include <platform/OpenThread/OpenThreadUtils.h>
#include <platform/ThreadStackManager.h>
#include <platform/internal/DeviceNetworkInfo.h>
#include <platform/nRF5/ThreadStackManagerImpl.h>
#endif
#include <support/ErrorStr.h>
#include <system/SystemClock.h>
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>
#define FACTORY_RESET_TRIGGER_TIMEOUT 3000
#define FACTORY_RESET_CANCEL_WINDOW_TIMEOUT 3000
#define APP_TASK_STACK_SIZE (4096)
#define APP_TASK_PRIORITY 2
#define APP_EVENT_QUEUE_SIZE 10
#define EXAMPLE_VENDOR_ID 0xabcd
APP_TIMER_DEF(sFunctionTimer);
static SemaphoreHandle_t sCHIPEventLock;
static TaskHandle_t sAppTaskHandle;
static QueueHandle_t sAppEventQueue;
static LEDWidget sStatusLED;
static LEDWidget sLockLED;
static LEDWidget sUnusedLED;
static LEDWidget sUnusedLED_1;
static LEDWidget sLockStatusLED;
static bool sIsThreadProvisioned = false;
static bool sIsThreadEnabled = false;
static bool sIsThreadAttached = false;
static bool sIsPairedToAccount = false;
static bool sHaveBLEConnections = false;
static bool sHaveServiceConnectivity = false;
using namespace ::chip::DeviceLayer;
AppTask AppTask::sAppTask;
int AppTask::StartAppTask()
{
ret_code_t ret = NRF_SUCCESS;
sAppEventQueue = xQueueCreate(APP_EVENT_QUEUE_SIZE, sizeof(AppEvent));
if (sAppEventQueue == NULL)
{
NRF_LOG_INFO("Failed to allocate app event queue");
ret = NRF_ERROR_NULL;
APP_ERROR_HANDLER(ret);
}
// Start App task.
if (xTaskCreate(AppTaskMain, "APP", APP_TASK_STACK_SIZE / sizeof(StackType_t), NULL, APP_TASK_PRIORITY, &sAppTaskHandle) !=
pdPASS)
{
ret = NRF_ERROR_NULL;
}
return ret;
}
int AppTask::Init()
{
ret_code_t ret;
// Initialize LEDs
sStatusLED.Init(SYSTEM_STATE_LED);
sLockLED.Init(LOCK_STATE_LED);
sLockLED.Set(!BoltLockMgr().IsUnlocked());
sLockStatusLED.Init(LOCK_STATE_LED_GPIO);
sLockStatusLED.Set(!BoltLockMgr().IsUnlocked());
sUnusedLED.Init(BSP_LED_2);
sUnusedLED_1.Init(BSP_LED_3);
// Initialize buttons
static app_button_cfg_t sButtons[] = {
{ LOCK_BUTTON, APP_BUTTON_ACTIVE_LOW, BUTTON_PULL, ButtonEventHandler },
{ FUNCTION_BUTTON, APP_BUTTON_ACTIVE_LOW, BUTTON_PULL, ButtonEventHandler },
#if CHIP_ENABLE_OPENTHREAD
{ JOIN_BUTTON, APP_BUTTON_ACTIVE_LOW, BUTTON_PULL, ButtonEventHandler },
#endif
};
ret = app_button_init(sButtons, ARRAY_SIZE(sButtons), pdMS_TO_TICKS(FUNCTION_BUTTON_DEBOUNCE_PERIOD_MS));
if (ret != NRF_SUCCESS)
{
NRF_LOG_INFO("app_button_init() failed");
APP_ERROR_HANDLER(ret);
}
ret = app_button_enable();
if (ret != NRF_SUCCESS)
{
NRF_LOG_INFO("app_button_enable() failed");
APP_ERROR_HANDLER(ret);
}
// Initialize Timer for Function Selection
ret = app_timer_init();
if (ret != NRF_SUCCESS)
{
NRF_LOG_INFO("app_timer_init() failed");
APP_ERROR_HANDLER(ret);
}
ret = app_timer_create(&sFunctionTimer, APP_TIMER_MODE_SINGLE_SHOT, TimerEventHandler);
if (ret != NRF_SUCCESS)
{
NRF_LOG_INFO("app_timer_create() failed");
APP_ERROR_HANDLER(ret);
}
ret = BoltLockMgr().Init();
if (ret != NRF_SUCCESS)
{
NRF_LOG_INFO("BoltLockMgr().Init() failed");
APP_ERROR_HANDLER(ret);
}
BoltLockMgr().SetCallbacks(ActionInitiated, ActionCompleted);
sCHIPEventLock = xSemaphoreCreateMutex();
if (sCHIPEventLock == NULL)
{
NRF_LOG_INFO("xSemaphoreCreateMutex() failed");
APP_ERROR_HANDLER(NRF_ERROR_NULL);
}
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::SetupPayload payload;
uint32_t setUpPINCode = 0;
uint16_t setUpDiscriminator = 0;
err = ConfigurationMgr().GetSetupPinCode(setUpPINCode);
if (err != CHIP_NO_ERROR)
{
NRF_LOG_INFO("ConfigurationMgr().GetSetupPinCode() failed: %s", chip::ErrorStr(err));
}
err = ConfigurationMgr().GetSetupDiscriminator(setUpDiscriminator);
if (err != CHIP_NO_ERROR)
{
NRF_LOG_INFO("ConfigurationMgr().GetSetupDiscriminator() failed: %s", chip::ErrorStr(err));
}
payload.version = 1;
payload.vendorID = EXAMPLE_VENDOR_ID;
payload.productID = 1;
payload.setUpPINCode = setUpPINCode;
payload.discriminator = setUpDiscriminator;
chip::QRCodeSetupPayloadGenerator generator(payload);
// TODO: Usage of STL will significantly increase the image size, this should be changed to more efficient method for
// generating payload
std::string result;
err = generator.payloadBase41Representation(result);
if (err != CHIP_NO_ERROR)
{
NRF_LOG_ERROR("Failed to generate QR Code");
}
NRF_LOG_INFO("SetupPINCode: [%" PRIu32 "]", setUpPINCode);
// There might be whitespace in setup QRCode, add brackets to make it clearer.
NRF_LOG_INFO("SetupQRCode: [%s]", result.c_str());
}
return ret;
}
void AppTask::HandleBLEConnectionOpened(chip::Ble::BLEEndPoint * endPoint)
{
ChipLogProgress(DeviceLayer, "AppTask: Connection opened");
GetAppTask().mBLEEndPoint = endPoint;
endPoint->OnMessageReceived = AppTask::HandleBLEMessageReceived;
endPoint->OnConnectionClosed = AppTask::HandleBLEConnectionClosed;
}
void AppTask::HandleBLEConnectionClosed(chip::Ble::BLEEndPoint * endPoint, BLE_ERROR err)
{
ChipLogProgress(DeviceLayer, "AppTask: Connection closed");
GetAppTask().mBLEEndPoint = nullptr;
}
void AppTask::HandleBLEMessageReceived(chip::Ble::BLEEndPoint * endPoint, chip::System::PacketBuffer * buffer)
{
#if CHIP_ENABLE_OPENTHREAD
uint16_t bufferLen = buffer->DataLength();
uint8_t * data = buffer->Start();
chip::DeviceLayer::Internal::DeviceNetworkInfo networkInfo;
ChipLogProgress(DeviceLayer, "AppTask: Receive message size %u", bufferLen);
memcpy(networkInfo.ThreadNetworkName, data, sizeof(networkInfo.ThreadNetworkName));
data += sizeof(networkInfo.ThreadNetworkName);
memcpy(networkInfo.ThreadExtendedPANId, data, sizeof(networkInfo.ThreadExtendedPANId));
data += sizeof(networkInfo.ThreadExtendedPANId);
memcpy(networkInfo.ThreadMeshPrefix, data, sizeof(networkInfo.ThreadMeshPrefix));
data += sizeof(networkInfo.ThreadMeshPrefix);
memcpy(networkInfo.ThreadNetworkKey, data, sizeof(networkInfo.ThreadNetworkKey));
data += sizeof(networkInfo.ThreadNetworkKey);
memcpy(networkInfo.ThreadPSKc, data, sizeof(networkInfo.ThreadPSKc));
data += sizeof(networkInfo.ThreadPSKc);
networkInfo.ThreadPANId = data[0] | (data[1] << 8);
data += sizeof(networkInfo.ThreadPANId);
networkInfo.ThreadChannel = data[0];
data += sizeof(networkInfo.ThreadChannel);
networkInfo.FieldPresent.ThreadExtendedPANId = *data;
data++;
networkInfo.FieldPresent.ThreadMeshPrefix = *data;
data++;
networkInfo.FieldPresent.ThreadPSKc = *data;
data++;
networkInfo.NetworkId = 0;
networkInfo.FieldPresent.NetworkId = true;
ThreadStackMgr().SetThreadEnabled(false);
ThreadStackMgr().SetThreadProvision(networkInfo);
ThreadStackMgr().SetThreadEnabled(true);
#endif
endPoint->Close();
chip::System::PacketBuffer::Free(buffer);
}
void AppTask::AppTaskMain(void * pvParameter)
{
ret_code_t ret;
AppEvent event;
uint64_t mLastChangeTimeUS = 0;
ret = sAppTask.Init();
if (ret != NRF_SUCCESS)
{
NRF_LOG_INFO("AppTask.Init() failed: %s", chip::ErrorStr(ret));
APP_ERROR_HANDLER(ret);
}
chip::DeviceLayer::ConnectivityMgr().AddCHIPoBLEConnectionHandler(&AppTask::HandleBLEConnectionOpened);
SetDeviceName("LockDemo._chip._udp.local.");
while (true)
{
BaseType_t eventReceived = xQueueReceive(sAppEventQueue, &event, pdMS_TO_TICKS(10));
while (eventReceived == pdTRUE)
{
sAppTask.DispatchEvent(&event);
eventReceived = xQueueReceive(sAppEventQueue, &event, 0);
}
// Collect connectivity and configuration state from the CHIP stack. Because the
// CHIP event loop is being run in a separate task, the stack must be locked
// while these values are queried. However we use a non-blocking lock request
// (TryLockChipStack()) to avoid blocking other UI activities when the CHIP
// task is busy (e.g. with a long crypto operation).
if (PlatformMgr().TryLockChipStack())
{
sIsThreadProvisioned = ConnectivityMgr().IsThreadProvisioned();
sIsThreadEnabled = ConnectivityMgr().IsThreadEnabled();
sIsThreadAttached = ConnectivityMgr().IsThreadAttached();
sHaveBLEConnections = (ConnectivityMgr().NumBLEConnections() != 0);
sHaveServiceConnectivity = ConnectivityMgr().HaveServiceConnectivity();
PlatformMgr().UnlockChipStack();
}
// Consider the system to be "fully connected" if it has service
// connectivity and it is able to interact with the service on a regular basis.
bool isFullyConnected = sHaveServiceConnectivity;
// Update the status LED if factory reset has not been initiated.
//
// 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 (sAppTask.mFunction != kFunction_FactoryReset)
{
if (isFullyConnected)
{
sStatusLED.Set(true);
}
else if (sIsThreadProvisioned && sIsThreadEnabled && sIsPairedToAccount && (!sIsThreadAttached || !isFullyConnected))
{
sStatusLED.Blink(950, 50);
}
else if (sHaveBLEConnections)
{
sStatusLED.Blink(100, 100);
}
else
{
sStatusLED.Blink(50, 950);
}
}
sStatusLED.Animate();
sLockLED.Animate();
sUnusedLED.Animate();
sUnusedLED_1.Animate();
sLockStatusLED.Set(!BoltLockMgr().IsUnlocked());
sLockStatusLED.Animate();
uint64_t nowUS = chip::System::Platform::Layer::GetClock_Monotonic();
uint64_t nextChangeTimeUS = mLastChangeTimeUS + 5 * 1000 * 1000UL;
if (nowUS > nextChangeTimeUS)
{
PublishService();
mLastChangeTimeUS = nowUS;
}
}
}
void AppTask::LockActionEventHandler(AppEvent * aEvent)
{
bool initiated = false;
BoltLockManager::Action_t action;
int32_t actor = 0;
ret_code_t ret = NRF_SUCCESS;
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;
}
}
else
{
ret = NRF_ERROR_NULL;
}
if (ret == NRF_SUCCESS)
{
initiated = BoltLockMgr().InitiateAction(actor, action);
if (!initiated)
{
NRF_LOG_INFO("Action is already in progress or active.");
}
}
}
#if CHIP_ENABLE_OPENTHREAD
void AppTask::JoinHandler(AppEvent * aEvent)
{
if (aEvent->ButtonEvent.PinNo != JOIN_BUTTON)
return;
CHIP_ERROR error = ThreadStackMgr().JoinerStart();
NRF_LOG_INFO("Thread joiner triggered: %s", chip::ErrorStr(error));
}
#endif
void AppTask::ButtonEventHandler(uint8_t pin_no, uint8_t button_action)
{
if (pin_no != LOCK_BUTTON
#if CHIP_ENABLE_OPENTHREAD
&& pin_no != JOIN_BUTTON
#endif
&& pin_no != FUNCTION_BUTTON)
{
return;
}
AppEvent button_event = {};
button_event.Type = AppEvent::kEventType_Button;
button_event.ButtonEvent.PinNo = pin_no;
button_event.ButtonEvent.Action = button_action;
if (pin_no == LOCK_BUTTON && button_action == APP_BUTTON_PUSH)
{
button_event.Handler = LockActionEventHandler;
}
else if (pin_no == FUNCTION_BUTTON)
{
button_event.Handler = FunctionHandler;
}
#if CHIP_ENABLE_OPENTHREAD
else if (pin_no == JOIN_BUTTON && button_action == APP_BUTTON_RELEASE)
{
button_event.Handler = JoinHandler;
}
#endif
else
{
return;
}
sAppTask.PostEvent(&button_event);
}
void AppTask::TimerEventHandler(void * p_context)
{
AppEvent event;
event.Type = AppEvent::kEventType_Timer;
event.TimerEvent.Context = p_context;
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 FACTORY_RESET_TRIGGER_TIMEOUT, initiate factory reset
if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_SoftwareUpdate)
{
NRF_LOG_INFO("Factory Reset Triggered. Release button within %ums to cancel.", FACTORY_RESET_TRIGGER_TIMEOUT);
// 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.
sStatusLED.Set(false);
sLockLED.Set(false);
sUnusedLED_1.Set(false);
sUnusedLED.Set(false);
sStatusLED.Blink(500);
sLockLED.Blink(500);
sUnusedLED.Blink(500);
sUnusedLED_1.Blink(500);
}
else if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_FactoryReset)
{
// Actually trigger Factory Reset
sAppTask.mFunction = kFunction_NoneSelected;
ConfigurationMgr().InitiateFactoryReset();
}
}
void AppTask::FunctionHandler(AppEvent * aEvent)
{
if (aEvent->ButtonEvent.PinNo != FUNCTION_BUTTON)
return;
// To trigger software update: press the FUNCTION_BUTTON button briefly (< FACTORY_RESET_TRIGGER_TIMEOUT)
// To initiate factory reset: press the 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 FUNCTION_BUTTON once all LEDs start blinking within the
// FACTORY_RESET_CANCEL_WINDOW_TIMEOUT
if (aEvent->ButtonEvent.Action == APP_BUTTON_PUSH)
{
if (!sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_NoneSelected)
{
sAppTask.StartTimer(FACTORY_RESET_TRIGGER_TIMEOUT);
sAppTask.mFunction = kFunction_SoftwareUpdate;
}
}
else
{
// If the button was released before factory reset got initiated, trigger a software update.
if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_SoftwareUpdate)
{
sAppTask.CancelTimer();
sAppTask.mFunction = kFunction_NoneSelected;
NRF_LOG_INFO("Software update is not implemented");
}
else if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_FactoryReset)
{
sUnusedLED.Set(false);
sUnusedLED_1.Set(false);
// Set lock status LED back to show state of lock.
sLockLED.Set(!BoltLockMgr().IsUnlocked());
sAppTask.CancelTimer();
// Change the function to none selected since factory reset has been canceled.
sAppTask.mFunction = kFunction_NoneSelected;
NRF_LOG_INFO("Factory Reset has been Canceled");
}
}
}
void AppTask::CancelTimer()
{
ret_code_t ret;
ret = app_timer_stop(sFunctionTimer);
if (ret != NRF_SUCCESS)
{
NRF_LOG_INFO("app_timer_stop() failed");
APP_ERROR_HANDLER(ret);
}
mFunctionTimerActive = false;
}
void AppTask::StartTimer(uint32_t aTimeoutInMs)
{
ret_code_t ret;
ret = app_timer_start(sFunctionTimer, pdMS_TO_TICKS(aTimeoutInMs), this);
if (ret != NRF_SUCCESS)
{
NRF_LOG_INFO("app_timer_start() failed");
APP_ERROR_HANDLER(ret);
}
mFunctionTimerActive = true;
}
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)
{
NRF_LOG_INFO("Lock Action has been initiated")
}
else if (aAction == BoltLockManager::UNLOCK_ACTION)
{
NRF_LOG_INFO("Unlock Action has been initiated")
}
sLockLED.Blink(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)
{
NRF_LOG_INFO("Lock Action has been completed")
sLockLED.Set(true);
}
else if (aAction == BoltLockManager::UNLOCK_ACTION)
{
NRF_LOG_INFO("Unlock Action has been completed")
sLockLED.Set(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))
{
NRF_LOG_INFO("Failed to post event to app task event queue");
}
}
}
void AppTask::DispatchEvent(AppEvent * aEvent)
{
if (aEvent->Handler)
{
aEvent->Handler(aEvent);
}
else
{
NRF_LOG_INFO("Event received with no handler. Dropping event.");
}
}