/* * * 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 "AppTask.h" #include "AppConfig.h" #include "AppEvent.h" #include "ButtonHandler.h" #include "CHIPDeviceManager.h" #include "DeviceCallbacks.h" #include "LEDWidget.h" #include "qrcodegen.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include "init_Matter.h" #include #include #include #include #include namespace { TimerHandle_t sFunctionTimer; // FreeRTOS app sw timer. TaskHandle_t sAppTaskHandle; QueueHandle_t sAppEventQueue; LEDWidget sStatusLED; LEDWidget sLockLED; bool sIsWiFiStationEnabled = false; bool sIsWiFiStationConnected = false; bool sHaveBLEConnections = false; } // namespace using namespace ::chip; using namespace ::chip::Credentials; using namespace ::chip::DeviceManager; using namespace ::chip::DeviceLayer; using namespace ::chip::System; AppTask AppTask::sAppTask; namespace { constexpr EndpointId kNetworkCommissioningEndpointMain = 0; constexpr EndpointId kNetworkCommissioningEndpointSecondary = 0xFFFE; app::Clusters::NetworkCommissioning::Instance sWiFiNetworkCommissioningInstance(kNetworkCommissioningEndpointMain /* Endpoint Id */, &(NetworkCommissioning::ASRWiFiDriver::GetInstance())); } // namespace void NetWorkCommissioningInstInit() { sWiFiNetworkCommissioningInstance.Init(); // We only have network commissioning on endpoint 0. emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, false); } static DeviceCallbacks EchoCallbacks; CHIP_ERROR AppTask::StartAppTask() { sAppEventQueue = xQueueCreate(APP_EVENT_QUEUE_SIZE, sizeof(AppEvent)); if (sAppEventQueue == NULL) { ASR_LOG("Failed to allocate app event queue"); appError(APP_ERROR_EVENT_QUEUE_FAILED); } // Start App task. xTaskCreate(AppTaskMain, APP_TASK_NAME, APP_TASK_STACK_SIZE, 0, 2, &sAppTaskHandle); return (sAppTaskHandle == nullptr) ? APP_ERROR_CREATE_TASK_FAILED : CHIP_NO_ERROR; } CHIP_ERROR AppTask::Init() { ASR_LOG("App Task started"); CHIP_ERROR err = CHIP_NO_ERROR; if (MatterInitializer::Init_Matter_Stack(MATTER_DEVICE_NAME) != CHIP_NO_ERROR) return CHIP_ERROR_INTERNAL; CHIPDeviceManager & deviceMgr = CHIPDeviceManager::GetInstance(); if (deviceMgr.Init(&EchoCallbacks) != CHIP_NO_ERROR) return CHIP_ERROR_INTERNAL; if (MatterInitializer::Init_Matter_Server() != CHIP_NO_ERROR) return CHIP_ERROR_INTERNAL; // 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) { ASR_LOG("funct timer create failed"); appError(APP_ERROR_CREATE_TIMER_FAILED); } NetWorkCommissioningInstInit(); ASR_LOG("Current Software Version: %s", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING); err = BoltLockMgr().Init(); if (err != CHIP_NO_ERROR) { ASR_LOG("BoltLockMgr().Init() failed"); appError(err); } BoltLockMgr().SetCallbacks(ActionInitiated, ActionCompleted); // Initialize LEDs sStatusLED.Init(LIGHT_LED); sStatusLED.Set(0); sLockLED.Init(STATE_LED); sLockLED.Set(!BoltLockMgr().IsUnlocked()); ConfigurationMgr().LogDeviceConfig(); // Print setup info #if CONFIG_NETWORK_LAYER_BLE PrintOnboardingCodes(chip::RendezvousInformationFlag(chip::RendezvousInformationFlag::kBLE)); #else PrintOnboardingCodes(chip::RendezvousInformationFlag(chip::RendezvousInformationFlag::kOnNetwork)); #endif /* CONFIG_NETWORK_LAYER_BLE */ return err; } void AppTask::AppTaskMain(void * pvParameter) { AppEvent event; CHIP_ERROR err = sAppTask.Init(); if (err != CHIP_NO_ERROR) { ASR_LOG("AppTask.Init() failed"); appError(err); } while (true) { BaseType_t eventReceived = xQueueReceive(sAppEventQueue, &event, pdMS_TO_TICKS(10)); if (eventReceived == pdTRUE) { sAppTask.DispatchEvent(&event); } // 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 != Function::kFactoryReset) { if (sIsWiFiStationEnabled && !sIsWiFiStationConnected) { sStatusLED.Blink(950, 50); } else if (sHaveBLEConnections) { sStatusLED.Blink(100, 100); } else { sStatusLED.Blink(50, 950); } } sStatusLED.Animate(); sLockLED.Animate(); } } void AppTask::LockActionEventHandler(AppEvent * event) { bool initiated = false; BoltLockManager::Action action = BoltLockManager::Action::KInvalid; int32_t actor = 0; CHIP_ERROR err = CHIP_NO_ERROR; if (event->Type == AppEvent::kEventType_Lock) { action = static_cast(event->LockEvent.Action); actor = event->LockEvent.Actor; } else if (event->Type == AppEvent::kEventType_Button) { if (BoltLockMgr().IsUnlocked()) { action = BoltLockManager::Action::kLock; } else { action = BoltLockManager::Action::kUnlock; } actor = AppEvent::kEventType_Button; } else { err = APP_ERROR_UNHANDLED_EVENT; } if (err == CHIP_NO_ERROR) { initiated = BoltLockMgr().InitiateAction(actor, action); if (!initiated) { ASR_LOG("Action is already in progress or active."); } } } void AppTask::ButtonEventHandler(uint8_t btnIdx, uint8_t btnAction) { if (btnIdx != SWITCH1_BUTTON && btnIdx != SWITCH2_BUTTON) { return; } AppEvent button_event = {}; button_event.Type = AppEvent::kEventType_Button; button_event.ButtonEvent.ButtonIdx = btnIdx; button_event.ButtonEvent.Action = btnAction; if (btnIdx == SWITCH1_BUTTON) { button_event.Handler = LockActionEventHandler; sAppTask.PostEvent(&button_event); } else if (btnIdx == SWITCH2_BUTTON) { button_event.Handler = FunctionHandler; sAppTask.PostEvent(&button_event); } } void AppTask::TimerEventHandler(TimerHandle_t timer) { AppEvent event; event.Type = AppEvent::kEventType_Timer; event.TimerEvent.Context = (void *) timer; event.Handler = FunctionTimerEventHandler; sAppTask.PostEvent(&event); } void AppTask::FunctionTimerEventHandler(AppEvent * event) { if (event->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 == Function::kStartBleAdv) { ASR_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 = Function::kFactoryReset; // Turn off all LEDs before starting blink to make sure blink is // co-ordinated. sStatusLED.Set(false); sLockLED.Set(false); sStatusLED.Blink(500); sLockLED.Blink(500); } else if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == Function::kFactoryReset) { // Actually trigger Factory Reset sAppTask.mFunction = Function::kNoneSelected; chip::Server::GetInstance().ScheduleFactoryReset(); } } void AppTask::FunctionHandler(AppEvent * event) { if (event->ButtonEvent.ButtonIdx != SWITCH2_BUTTON) { return; } if (event->ButtonEvent.Action == BUTTON_RELEASED) { if (!sAppTask.mFunctionTimerActive && sAppTask.mFunction == Function::kNoneSelected) { sAppTask.StartTimer(FACTORY_RESET_TRIGGER_TIMEOUT); sAppTask.mFunction = Function::kStartBleAdv; } } else { // If the button was released before factory reset got initiated, start Thread Network if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == Function::kStartBleAdv) { sAppTask.CancelTimer(); sAppTask.mFunction = Function::kNoneSelected; } else if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == Function::kFactoryReset) { // 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 = Function::kNoneSelected; ASR_LOG("Factory Reset has been Canceled"); } } } void AppTask::CancelTimer() { if (xTimerStop(sFunctionTimer, 0) == pdFAIL) { ASR_LOG("app timer stop() failed"); appError(APP_ERROR_STOP_TIMER_FAILED); } mFunctionTimerActive = false; } void AppTask::StartTimer(uint32_t aTimeoutInMs) { if (xTimerIsTimerActive(sFunctionTimer)) { ASR_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) { ASR_LOG("app timer start() failed"); appError(APP_ERROR_START_TIMER_FAILED); } mFunctionTimerActive = true; } void AppTask::ActionInitiated(BoltLockManager::Action action, int32_t actor) { // 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 (action == BoltLockManager::Action::kLock) { ASR_LOG("Lock Action has been initiated"); } else if (action == BoltLockManager::Action::kUnlock) { ASR_LOG("Unlock Action has been initiated"); } if (actor == AppEvent::kEventType_Button) { sAppTask.mSyncClusterToButtonAction = true; } sLockLED.Blink(50, 50); } void AppTask::ActionCompleted(BoltLockManager::Action action) { // 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 (action == BoltLockManager::Action::kLock) { ASR_LOG("Lock Action has been completed"); sLockLED.Set(true); } else if (action == BoltLockManager::Action::kUnlock) { ASR_LOG("Unlock Action has been completed"); sLockLED.Set(false); } if (sAppTask.mSyncClusterToButtonAction) { sAppTask.UpdateClusterState(); sAppTask.mSyncClusterToButtonAction = false; } } void AppTask::PostLockActionRequest(int32_t actor, BoltLockManager::Action action) { AppEvent event; event.Type = AppEvent::kEventType_Lock; event.LockEvent.Actor = actor; event.LockEvent.Action = static_cast(action); event.Handler = LockActionEventHandler; PostEvent(&event); } void AppTask::PostEvent(const AppEvent * event) { if (sAppEventQueue != NULL) { if (!xQueueSend(sAppEventQueue, event, 1)) { ASR_LOG("Failed to post event to app task event queue"); } } } void AppTask::DispatchEvent(AppEvent * event) { if (event->Handler) { event->Handler(event); } else { ASR_LOG("Event received with no handler. Dropping event."); } } void AppTask::UpdateCluster(intptr_t context) { uint8_t unlocked = BoltLockMgr().IsUnlocked(); DlLockState newState = unlocked ? DlLockState::kUnlocked : DlLockState::kLocked; OperationSourceEnum source = OperationSourceEnum::kUnspecified; // write the new lock value Protocols::InteractionModel::Status status = DoorLockServer::Instance().SetLockState(1, newState, source) ? Protocols::InteractionModel::Status::Success : Protocols::InteractionModel::Status::Failure; if (status != Protocols::InteractionModel::Status::Success) { ASR_LOG("ERR: updating lock state %x", to_underlying(status)); } } void AppTask::UpdateClusterState(void) { chip::DeviceLayer::PlatformMgr().ScheduleWork(UpdateCluster, reinterpret_cast(nullptr)); }