blob: 8c1599ecf58cfbfd7a2a9172a3d00c741c77c780 [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 "AppConfig.h"
#include "AppEvent.h"
#include "ButtonHandler.h"
#include "DataModelHandler.h"
#include "LEDWidget.h"
#include "QRCodeUtil.h"
#include "Server.h"
#include "Service.h"
#include "attribute-storage.h"
#include "gen/attribute-id.h"
#include "gen/attribute-type.h"
#include "gen/cluster-id.h"
#include "lcd.h"
#include "qrcodegen.h"
#include <assert.h>
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>
#include <platform/CHIPDeviceLayer.h>
#if CHIP_ENABLE_OPENTHREAD
#include <platform/EFR32/ThreadStackManagerImpl.h>
#include <platform/OpenThread/OpenThreadUtils.h>
#include <platform/ThreadStackManager.h>
#endif
#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 0xcafe
TimerHandle_t sFunctionTimer; // FreeRTOS app sw timer.
static TaskHandle_t sAppTaskHandle;
static QueueHandle_t sAppEventQueue;
static LEDWidget sStatusLED;
static LEDWidget sLightLED;
static bool sIsThreadProvisioned = false;
static bool sIsThreadEnabled = false;
static bool sHaveBLEConnections = false;
static bool sHaveServiceConnectivity = false;
using namespace chip::TLV;
using namespace ::chip::DeviceLayer;
AppTask AppTask::sAppTask;
int AppTask::StartAppTask()
{
int err = CHIP_ERROR_MAX;
sAppEventQueue = xQueueCreate(APP_EVENT_QUEUE_SIZE, sizeof(AppEvent));
if (sAppEventQueue == NULL)
{
EFR32_LOG("Failed to allocate app event queue");
appError(err);
}
// Start App task.
if (xTaskCreate(AppTaskMain, "APP", APP_TASK_STACK_SIZE / sizeof(StackType_t), NULL, 1, &sAppTaskHandle) == pdPASS)
{
err = CHIP_NO_ERROR;
}
return err;
}
int AppTask::Init()
{
CHIP_ERROR err = CHIP_NO_ERROR;
// Init ZCL Data Model
InitServer();
// Initialise WSTK buttons PB0 and PB1 (including debounce).
ButtonHandler::Init();
// Create FreeRTOS sw timer for Function Selection.
sFunctionTimer = xTimerCreate("FnTmr", // Just a text name, not used by the RTOS kernel
1, // == default timer period (mS)
false, // no timer reload (==one-shot)
(void *) this, // init timer id = app task obj context
TimerEventHandler // timer callback handler
);
if (sFunctionTimer == NULL)
{
EFR32_LOG("funct timer create failed");
appError(err);
}
EFR32_LOG("Current Firmware Version: %s", CHIP_DEVICE_CONFIG_DEVICE_FIRMWARE_REVISION);
err = LightMgr().Init();
if (err != CHIP_NO_ERROR)
{
EFR32_LOG("LightMgr().Init() failed");
appError(err);
}
LightMgr().SetCallbacks(ActionInitiated, ActionCompleted);
// Initialize LEDs
LEDWidget::InitGpio();
sStatusLED.Init(SYSTEM_STATE_LED);
sLightLED.Init(LIGHT_LED);
sLightLED.Set(LightMgr().IsLightOn());
UpdateClusterState();
ConfigurationMgr().LogDeviceConfig();
// Print setup info on LCD if available
#ifdef DISPLAY_ENABLED
std::string QRCode;
if (GetQRCode(QRCode, chip::RendezvousInformationFlags::kBLE) == CHIP_NO_ERROR)
{
LCDWriteQRCode((uint8_t *) QRCode.c_str());
}
else
{
EFR32_LOG("Getting QR code failed!");
}
#else
PrintQRCode(chip::RendezvousInformationFlags::kBLE);
#endif
return err;
}
void AppTask::AppTaskMain(void * pvParameter)
{
int err;
AppEvent event;
uint64_t mLastChangeTimeUS = 0;
err = sAppTask.Init();
if (err != CHIP_NO_ERROR)
{
EFR32_LOG("AppTask.Init() failed");
appError(err);
}
EFR32_LOG("App Task started");
SetDeviceName("EFR32LightingDemo._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();
sHaveBLEConnections = (ConnectivityMgr().NumBLEConnections() != 0);
sHaveServiceConnectivity = ConnectivityMgr().HaveServiceConnectivity();
PlatformMgr().UnlockChipStack();
}
// 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)
{
// Consider the system to be "fully connected" if it has service
// connectivity
if (sHaveServiceConnectivity)
{
sStatusLED.Set(true);
}
else if (sIsThreadProvisioned && sIsThreadEnabled)
{
sStatusLED.Blink(950, 50);
}
else if (sHaveBLEConnections)
{
sStatusLED.Blink(100, 100);
}
else
{
sStatusLED.Blink(50, 950);
}
}
sStatusLED.Animate();
sLightLED.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::LightActionEventHandler(AppEvent * aEvent)
{
bool initiated = false;
LightingManager::Action_t action;
int32_t actor;
int err = CHIP_NO_ERROR;
if (aEvent->Type == AppEvent::kEventType_Light)
{
action = static_cast<LightingManager::Action_t>(aEvent->LightEvent.Action);
actor = aEvent->LightEvent.Actor;
}
else if (aEvent->Type == AppEvent::kEventType_Button)
{
if (LightMgr().IsLightOn())
{
action = LightingManager::OFF_ACTION;
}
else
{
action = LightingManager::ON_ACTION;
}
actor = AppEvent::kEventType_Button;
}
else
{
err = CHIP_ERROR_MAX;
}
if (err == CHIP_NO_ERROR)
{
initiated = LightMgr().InitiateAction(actor, action);
if (!initiated)
{
EFR32_LOG("Action is already in progress or active.");
}
}
}
void AppTask::ButtonEventHandler(uint8_t btnIdx, uint8_t btnAction)
{
if (btnIdx != APP_LIGHT_SWITCH && btnIdx != APP_FUNCTION_BUTTON)
{
return;
}
AppEvent button_event = {};
button_event.Type = AppEvent::kEventType_Button;
button_event.ButtonEvent.ButtonIdx = btnIdx;
button_event.ButtonEvent.Action = btnAction;
if (btnIdx == APP_LIGHT_SWITCH && btnAction == APP_BUTTON_PRESSED)
{
button_event.Handler = LightActionEventHandler;
sAppTask.PostEvent(&button_event);
}
else if (btnIdx == APP_FUNCTION_BUTTON)
{
button_event.Handler = FunctionHandler;
sAppTask.PostEvent(&button_event);
}
}
void AppTask::TimerEventHandler(TimerHandle_t xTimer)
{
AppEvent event;
event.Type = AppEvent::kEventType_Timer;
event.TimerEvent.Context = (void *) xTimer;
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_StartThread)
{
EFR32_LOG("Factory Reset Triggered. Release button within %ums to cancel.", FACTORY_RESET_CANCEL_WINDOW_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);
sLightLED.Set(false);
sStatusLED.Blink(500);
sLightLED.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.ButtonIdx != APP_FUNCTION_BUTTON)
{
return;
}
// To trigger software update: press the APP_FUNCTION_BUTTON button briefly (<
// FACTORY_RESET_TRIGGER_TIMEOUT) 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 == APP_BUTTON_PRESSED)
{
if (!sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_NoneSelected)
{
sAppTask.StartTimer(FACTORY_RESET_TRIGGER_TIMEOUT);
sAppTask.mFunction = kFunction_StartThread;
}
}
else
{
// If the button was released before factory reset got initiated, start Thread Network
if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_StartThread)
{
sAppTask.CancelTimer();
sAppTask.mFunction = kFunction_NoneSelected;
#if CHIP_ENABLE_OPENTHREAD
if (!chip::DeviceLayer::ConnectivityMgr().IsThreadProvisioned())
{
StartDefaultThreadNetwork();
EFR32_LOG("Device is not commissioned to a Thread network. Starting with the default configuration.");
}
else
{
EFR32_LOG("Device is commissioned to a Thread network.");
}
#else
EFR32_LOG("Thread is not defined.");
#endif
}
else if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_FactoryReset)
{
// Set Light status LED back to show state of light.
sLightLED.Set(LightMgr().IsLightOn());
sAppTask.CancelTimer();
// Change the function to none selected since factory reset has been
// canceled.
sAppTask.mFunction = kFunction_NoneSelected;
EFR32_LOG("Factory Reset has been Canceled");
}
}
}
void AppTask::CancelTimer()
{
if (xTimerStop(sFunctionTimer, 0) == pdFAIL)
{
EFR32_LOG("app timer stop() failed");
appError(CHIP_ERROR_MAX);
}
mFunctionTimerActive = false;
}
void AppTask::StartTimer(uint32_t aTimeoutInMs)
{
if (xTimerIsTimerActive(sFunctionTimer))
{
EFR32_LOG("app timer already started!");
CancelTimer();
}
// timer is not active, change its period to required value (== restart).
// FreeRTOS- Block for a maximum of 100 ticks if the change period command
// cannot immediately be sent to the timer command queue.
if (xTimerChangePeriod(sFunctionTimer, aTimeoutInMs / portTICK_PERIOD_MS, 100) != pdPASS)
{
EFR32_LOG("app timer start() failed");
appError(CHIP_ERROR_MAX);
}
mFunctionTimerActive = true;
}
void AppTask::ActionInitiated(LightingManager::Action_t aAction, int32_t aActor)
{
// Action initiated, update the light led
if (aAction == LightingManager::ON_ACTION)
{
EFR32_LOG("Turning light ON")
sLightLED.Set(true);
}
else if (aAction == LightingManager::OFF_ACTION)
{
EFR32_LOG("Turning light OFF")
sLightLED.Set(false);
}
if (aActor == AppEvent::kEventType_Button)
{
sAppTask.mSyncClusterToButtonAction = true;
}
}
void AppTask::ActionCompleted(LightingManager::Action_t aAction)
{
// action has been completed bon the light
if (aAction == LightingManager::ON_ACTION)
{
EFR32_LOG("Light ON")
}
else if (aAction == LightingManager::OFF_ACTION)
{
EFR32_LOG("Light OFF")
}
if (sAppTask.mSyncClusterToButtonAction)
{
UpdateClusterState();
sAppTask.mSyncClusterToButtonAction = false;
}
}
void AppTask::PostLightActionRequest(int32_t aActor, LightingManager::Action_t aAction)
{
AppEvent event;
event.Type = AppEvent::kEventType_Light;
event.LightEvent.Actor = aActor;
event.LightEvent.Action = aAction;
event.Handler = LightActionEventHandler;
PostEvent(&event);
}
void AppTask::PostEvent(const AppEvent * aEvent)
{
if (sAppEventQueue != NULL)
{
if (!xQueueSend(sAppEventQueue, aEvent, 1))
{
EFR32_LOG("Failed to post event to app task event queue");
}
}
}
void AppTask::DispatchEvent(AppEvent * aEvent)
{
if (aEvent->Handler)
{
aEvent->Handler(aEvent);
}
else
{
EFR32_LOG("Event received with no handler. Dropping event.");
}
}
void AppTask::UpdateClusterState(void)
{
uint8_t newValue = LightMgr().IsLightOn();
// write the new on/off value
EmberAfStatus status = emberAfWriteAttribute(1, ZCL_ON_OFF_CLUSTER_ID, ZCL_ON_OFF_ATTRIBUTE_ID, CLUSTER_MASK_SERVER,
(uint8_t *) &newValue, ZCL_BOOLEAN_ATTRIBUTE_TYPE);
if (status != EMBER_ZCL_STATUS_SUCCESS)
{
EFR32_LOG("ERR: updating on/off %x", status);
}
}