diff --git a/examples/all-clusters-app/esp32/main/AppTask.cpp b/examples/all-clusters-app/esp32/main/AppTask.cpp index 51ee806898cd10..a027a4dedbf1a8 100644 --- a/examples/all-clusters-app/esp32/main/AppTask.cpp +++ b/examples/all-clusters-app/esp32/main/AppTask.cpp @@ -27,7 +27,9 @@ #include "esp_spi_flash.h" #include "freertos/FreeRTOS.h" #include +#include +#define FACTORY_RESET_CANCEL_WINDOW_TIMEOUT 3000 #define APP_TASK_NAME "APP" #define APP_EVENT_QUEUE_SIZE 10 #define APP_TASK_STACK_SIZE (3072) @@ -35,6 +37,7 @@ static const char * TAG = "app-task"; namespace { +TimerHandle_t sFunctionTimer; // FreeRTOS app sw timer QueueHandle_t sAppEventQueue; TaskHandle_t sAppTaskHandle; @@ -58,6 +61,108 @@ CHIP_ERROR AppTask::StartAppTask() return (xReturned == pdPASS) ? CHIP_NO_ERROR : APP_ERROR_CREATE_TASK_FAILED; } +void AppTask::TimerEventHandler(TimerHandle_t xTimer) +{ + AppEvent event; + event.mType = AppEvent::kEventType_Timer; + event.mTimerEvent.mContext = (void *) xTimer; + event.mHandler = FunctionTimerEventHandler; + sAppTask.PostEvent(&event); +} + + +void AppTask::FunctionTimerEventHandler(AppEvent * aEvent) +{ + if (aEvent->mType != 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_StartBleAdv) + { + //ESP_LOGI(TAG, "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); + //sLockLED.Set(false); + //sStatusLED.Blink(500); + //sLockLED.Blink(500); + } + else if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_FactoryReset) + { + // Actually trigger Factory Reset + sAppTask.mFunction = kFunction_NoneSelected; + chip::Server::GetInstance().ScheduleFactoryReset(); + } +} + +void AppTask::CancelTimer() +{ + if (xTimerStop(sFunctionTimer, 0) == pdFAIL) + { + ESP_LOGI(TAG, "app timer stop() failed"); + return; + } + mFunctionTimerActive = false; +} +void AppTask::StartTimer(uint32_t aTimeoutInMs) +{ + if (xTimerIsTimerActive(sFunctionTimer)) + { + ESP_LOGI(TAG, "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) + { + ESP_LOGI(TAG, "app timer start() failed"); + return; + } + 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) + { + ESP_LOGI(TAG, "Lock Action has been initiated"); + } + else if (aAction == BoltLockManager::UNLOCK_ACTION) + { + ESP_LOGI(TAG, "Unlock Action has been initiated"); + } + if (aActor == AppEvent::kEventType_Button) + { + sAppTask.mSyncClusterToButtonAction = true; + } + //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) + { + ESP_LOGI(TAG, "Lock Action has been completed"); + //sLockLED.Set(true); + } + else if (aAction == BoltLockManager::UNLOCK_ACTION) + { + ESP_LOGI(TAG, "Unlock Action has been completed"); + //sLockLED.Set(false); + } +} + + CHIP_ERROR AppTask::Init() { /* Print chip information */ @@ -69,7 +174,18 @@ CHIP_ERROR AppTask::Init() ESP_LOGI(TAG, "%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024), (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external"); - CHIP_ERROR err = CHIP_NO_ERROR; + // 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 + ); + + CHIP_ERROR err = BoltLockMgr().InitLockState(); + + BoltLockMgr().SetCallbacks(ActionInitiated, ActionCompleted); + statusLED1.Init(STATUS_LED_GPIO_NUM); // Our second LED doesn't map to any physical LEDs so far, just to virtual // "LED"s on devices with screens. diff --git a/examples/all-clusters-app/esp32/main/CMakeLists.txt b/examples/all-clusters-app/esp32/main/CMakeLists.txt index ee4b0639094d63..a4bf656784a9df 100644 --- a/examples/all-clusters-app/esp32/main/CMakeLists.txt +++ b/examples/all-clusters-app/esp32/main/CMakeLists.txt @@ -20,7 +20,6 @@ set(PRIV_INCLUDE_DIRS_LIST "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/all-clusters-app" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/all-clusters-app/all-clusters-common/include" "${CMAKE_CURRENT_LIST_DIR}/include" - "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/lock-app/linux/include" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/third_party/nlfaultinjection/repo/include" @@ -30,12 +29,12 @@ set(SRC_DIRS_LIST "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/all-clusters-app/zap-generated" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated/attributes" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated" - "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/lock-app/linux/src" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/lock" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/server" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/util" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/reporting" @@ -95,7 +94,7 @@ set(SRC_DIRS_LIST ) set(EXCLUDE_SRCS_LIST - "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/lock-app/linux/src/LockAppCommandDelegate.cpp" + # "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/lock-app/esp32/main/LockAppCommandDelegate.cpp" ) if (CONFIG_ENABLE_PW_RPC) diff --git a/examples/all-clusters-app/esp32/main/include/AppTask.h b/examples/all-clusters-app/esp32/main/include/AppTask.h index 3dd8b0bf121a5c..d905e5e90b4186 100644 --- a/examples/all-clusters-app/esp32/main/include/AppTask.h +++ b/examples/all-clusters-app/esp32/main/include/AppTask.h @@ -18,8 +18,9 @@ */ #pragma once -#include "AppEvent.h" +#include #include +#include // Application-defined error codes in the CHIP_ERROR space. #define APP_ERROR_EVENT_QUEUE_FAILED CHIP_APPLICATION_ERROR(0x01) @@ -28,6 +29,7 @@ #define APP_ERROR_CREATE_TIMER_FAILED CHIP_APPLICATION_ERROR(0x04) #define APP_ERROR_START_TIMER_FAILED CHIP_APPLICATION_ERROR(0x05) #define APP_ERROR_STOP_TIMER_FAILED CHIP_APPLICATION_ERROR(0x06) +#define APP_ERROR_ALLOCATION_FAILED CHIP_APPLICATION_ERROR(0x07) class AppTask { @@ -40,7 +42,30 @@ class AppTask private: CHIP_ERROR Init(); + + static void ActionInitiated(BoltLockManager::Action_t aAction, int32_t aActor); + static void ActionCompleted(BoltLockManager::Action_t aAction); + + void StartTimer(uint32_t aTimeoutMs); + void CancelTimer(void); + + static void FunctionTimerEventHandler(AppEvent * aEvent); + static void TimerEventHandler(TimerHandle_t xTimer); + void DispatchEvent(AppEvent * event); + + enum Function_t + { + kFunction_NoneSelected = 0, + kFunction_SoftwareUpdate = 0, + kFunction_StartBleAdv = 1, + kFunction_FactoryReset = 2, + kFunction_Invalid + } Function; + Function_t mFunction; + bool mFunctionTimerActive; + bool mSyncClusterToButtonAction; + static AppTask sAppTask; friend AppTask & GetAppTask(void); }; diff --git a/examples/lock-app/esp32/main/AppTask.cpp b/examples/lock-app/esp32/main/AppTask.cpp index 75c5fc2d7e6504..05c2ef27350066 100644 --- a/examples/lock-app/esp32/main/AppTask.cpp +++ b/examples/lock-app/esp32/main/AppTask.cpp @@ -16,14 +16,14 @@ */ #include "AppTask.h" -#include "AppConfig.h" -#include "AppEvent.h" +#include +#include #include "Button.h" #include "LEDWidget.h" #include "esp_log.h" #include #include -#include +//#include #include #include #include @@ -61,7 +61,7 @@ StackType_t appStack[APP_TASK_STACK_SIZE / sizeof(StackType_t)]; using namespace ::chip::DeviceLayer; using namespace ::chip::System; -using namespace ESP32DoorLock::LockInitParams; +//using namespace ESP32DoorLock::LockInitParams; AppTask AppTask::sAppTask; @@ -81,85 +81,14 @@ CHIP_ERROR AppTask::StartAppTask() CHIP_ERROR AppTask::Init() { - // Create FreeRTOS sw timer for Function Selection. + // 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 ); - - // Initial lock state - chip::app::DataModel::Nullable state; - chip::EndpointId endpointId{ 1 }; - chip::DeviceLayer::PlatformMgr().LockChipStack(); - chip::app::Clusters::DoorLock::Attributes::LockState::Get(endpointId, state); - - uint8_t numberOfCredentialsPerUser = 0; - if (!DoorLockServer::Instance().GetNumberOfCredentialsSupportedPerUser(endpointId, numberOfCredentialsPerUser)) - { - ChipLogError(Zcl, - "Unable to get number of credentials supported per user when initializing lock endpoint, defaulting to 5 " - "[endpointId=%d]", - endpointId); - numberOfCredentialsPerUser = 5; - } - - uint16_t numberOfUsers = 0; - if (!DoorLockServer::Instance().GetNumberOfUserSupported(endpointId, numberOfUsers)) - { - ChipLogError(Zcl, - "Unable to get number of supported users when initializing lock endpoint, defaulting to 10 [endpointId=%d]", - endpointId); - numberOfUsers = 10; - } - - uint8_t numberOfWeekdaySchedulesPerUser = 0; - if (!DoorLockServer::Instance().GetNumberOfWeekDaySchedulesPerUserSupported(endpointId, numberOfWeekdaySchedulesPerUser)) - { - ChipLogError( - Zcl, - "Unable to get number of supported weekday schedules when initializing lock endpoint, defaulting to 10 [endpointId=%d]", - endpointId); - numberOfWeekdaySchedulesPerUser = 10; - } - - uint8_t numberOfYeardaySchedulesPerUser = 0; - if (!DoorLockServer::Instance().GetNumberOfYearDaySchedulesPerUserSupported(endpointId, numberOfYeardaySchedulesPerUser)) - { - ChipLogError( - Zcl, - "Unable to get number of supported yearday schedules when initializing lock endpoint, defaulting to 10 [endpointId=%d]", - endpointId); - numberOfYeardaySchedulesPerUser = 10; - } - - uint8_t numberOfHolidaySchedules = 0; - if (!DoorLockServer::Instance().GetNumberOfHolidaySchedulesSupported(endpointId, numberOfHolidaySchedules)) - { - ChipLogError( - Zcl, - "Unable to get number of supported holiday schedules when initializing lock endpoint, defaulting to 10 [endpointId=%d]", - endpointId); - numberOfHolidaySchedules = 10; - } - - chip::DeviceLayer::PlatformMgr().UnlockChipStack(); - - CHIP_ERROR err = BoltLockMgr().Init(state, - ParamBuilder() - .SetNumberOfUsers(numberOfUsers) - .SetNumberOfCredentialsPerUser(numberOfCredentialsPerUser) - .SetNumberOfWeekdaySchedulesPerUser(numberOfWeekdaySchedulesPerUser) - .SetNumberOfYeardaySchedulesPerUser(numberOfYeardaySchedulesPerUser) - .SetNumberOfHolidaySchedules(numberOfHolidaySchedules) - .GetLockParam()); - - if (err != CHIP_NO_ERROR) - { - ESP_LOGI(TAG, "BoltLockMgr().Init() failed"); - return err; - } + CHIP_ERROR err = BoltLockMgr().InitLockState(); BoltLockMgr().SetCallbacks(ActionInitiated, ActionCompleted); @@ -269,12 +198,12 @@ void AppTask::LockActionEventHandler(AppEvent * aEvent) int32_t actor; CHIP_ERROR err = CHIP_NO_ERROR; - if (aEvent->Type == AppEvent::kEventType_Lock) + if (aEvent->mType == AppEvent::kEventType_Lock) { - action = static_cast(aEvent->LockEvent.Action); - actor = aEvent->LockEvent.Actor; + action = static_cast(aEvent->mLockEvent.mAction); + actor = aEvent->mLockEvent.mActor; } - else if (aEvent->Type == AppEvent::kEventType_Button) + else if (aEvent->mType == AppEvent::kEventType_Button) { if (BoltLockMgr().IsUnlocked()) { @@ -309,19 +238,19 @@ void AppTask::ButtonEventHandler(uint8_t btnIdx, uint8_t btnAction) return; } - AppEvent button_event = {}; - button_event.Type = AppEvent::kEventType_Button; - button_event.ButtonEvent.PinNo = btnIdx; - button_event.ButtonEvent.Action = btnAction; + AppEvent button_event = {}; + button_event.mType = AppEvent::kEventType_Button; + button_event.mButtonEvent.mPinNo = btnIdx; + button_event.mButtonEvent.mAction = btnAction; if (btnIdx == APP_LOCK_BUTTON && btnAction == APP_BUTTON_PRESSED) { - button_event.Handler = LockActionEventHandler; + button_event.mHandler = LockActionEventHandler; sAppTask.PostEvent(&button_event); } else if (btnIdx == APP_FUNCTION_BUTTON) { - button_event.Handler = FunctionHandler; + button_event.mHandler = FunctionHandler; sAppTask.PostEvent(&button_event); } } @@ -329,15 +258,15 @@ void AppTask::ButtonEventHandler(uint8_t btnIdx, uint8_t btnAction) void AppTask::TimerEventHandler(TimerHandle_t xTimer) { AppEvent event; - event.Type = AppEvent::kEventType_Timer; - event.TimerEvent.Context = (void *) xTimer; - event.Handler = FunctionTimerEventHandler; + event.mType = AppEvent::kEventType_Timer; + event.mTimerEvent.mContext = (void *) xTimer; + event.mHandler = FunctionTimerEventHandler; sAppTask.PostEvent(&event); } void AppTask::FunctionTimerEventHandler(AppEvent * aEvent) { - if (aEvent->Type != AppEvent::kEventType_Timer) + if (aEvent->mType != AppEvent::kEventType_Timer) { return; } @@ -372,7 +301,7 @@ void AppTask::FunctionTimerEventHandler(AppEvent * aEvent) void AppTask::FunctionHandler(AppEvent * aEvent) { - if (aEvent->ButtonEvent.PinNo != APP_FUNCTION_BUTTON) + if (aEvent->mButtonEvent.mPinNo != APP_FUNCTION_BUTTON) { return; } @@ -384,7 +313,7 @@ void AppTask::FunctionHandler(AppEvent * aEvent) // 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 (aEvent->mButtonEvent.mAction == APP_BUTTON_PRESSED) { if (!sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_NoneSelected) { @@ -499,10 +428,10 @@ void AppTask::ActionCompleted(BoltLockManager::Action_t aAction) 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; + event.mType = AppEvent::kEventType_Lock; + event.mLockEvent.mActor = aActor; + event.mLockEvent.mAction = aAction; + event.mHandler = LockActionEventHandler; PostEvent(&event); } @@ -519,9 +448,9 @@ void AppTask::PostEvent(const AppEvent * aEvent) void AppTask::DispatchEvent(AppEvent * aEvent) { - if (aEvent->Handler) + if (aEvent->mHandler) { - aEvent->Handler(aEvent); + aEvent->mHandler(aEvent); } else { diff --git a/examples/lock-app/esp32/main/Button.cpp b/examples/lock-app/esp32/main/Button.cpp index 698888440ecb65..301e62f92d7e08 100644 --- a/examples/lock-app/esp32/main/Button.cpp +++ b/examples/lock-app/esp32/main/Button.cpp @@ -18,7 +18,7 @@ #include "Button.h" #include "AppTask.h" -#include "AppConfig.h" +//#include esp_err_t Button::Init(gpio_num_t gpioNum, uint16_t debouncePeriod) { diff --git a/examples/lock-app/esp32/main/CMakeLists.txt b/examples/lock-app/esp32/main/CMakeLists.txt index ea528144100f5d..7588a5a2a98a38 100644 --- a/examples/lock-app/esp32/main/CMakeLists.txt +++ b/examples/lock-app/esp32/main/CMakeLists.txt @@ -23,6 +23,7 @@ idf_component_register(INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/pw_sys_io/public" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/lock" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed" @@ -147,7 +148,6 @@ idf_component_register(PRIV_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/lock-app/" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers" "${CMAKE_CURRENT_LIST_DIR}/include" - "${IDF_PATH}/components/freertos/include/freertos" SRC_DIRS "${CMAKE_CURRENT_LIST_DIR}" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated/attributes" @@ -187,6 +187,7 @@ idf_component_register(PRIV_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/door-lock-server" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/identify-server" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/groups-server" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/lock" PRIV_REQUIRES chip QRCode bt) add_dependencies(${COMPONENT_LIB} app-codegen) diff --git a/examples/lock-app/esp32/main/DeviceCallbacks.cpp b/examples/lock-app/esp32/main/DeviceCallbacks.cpp index 0c271d8a6cbb76..486ed5fb2ff57b 100644 --- a/examples/lock-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/lock-app/esp32/main/DeviceCallbacks.cpp @@ -24,8 +24,8 @@ **/ #include "DeviceCallbacks.h" -#include "AppConfig.h" -#include "BoltLockManager.h" +#include +#include #include #include diff --git a/examples/lock-app/esp32/main/include/AppEvent.h b/examples/lock-app/esp32/main/include/AppEvent.h deleted file mode 100644 index 783d29ca6feb5d..00000000000000 --- a/examples/lock-app/esp32/main/include/AppEvent.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * - * Copyright (c) 2020 Project CHIP Authors - * - * 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. - */ - -#pragma once - -struct AppEvent; -typedef void (*EventHandler)(AppEvent *); - -struct AppEvent -{ - enum AppEventTypes - { - kEventType_Button = 0, - kEventType_Timer, - kEventType_Lock, - kEventType_Install, - }; - - uint16_t Type; - - union - { - struct - { - uint8_t PinNo; - uint8_t Action; - } ButtonEvent; - struct - { - void * Context; - } TimerEvent; - struct - { - uint8_t Action; - int32_t Actor; - } LockEvent; - }; - - EventHandler Handler; -}; diff --git a/examples/lock-app/esp32/main/include/AppTask.h b/examples/lock-app/esp32/main/include/AppTask.h index 10fcb678d6efd2..3dba306c50f0bd 100644 --- a/examples/lock-app/esp32/main/include/AppTask.h +++ b/examples/lock-app/esp32/main/include/AppTask.h @@ -20,8 +20,8 @@ #include #include -#include "AppEvent.h" -#include "BoltLockManager.h" +#include +#include #include "freertos/FreeRTOS.h" #include @@ -37,6 +37,7 @@ #define APP_ERROR_STOP_TIMER_FAILED CHIP_APPLICATION_ERROR(0x06) #define APP_ERROR_ALLOCATION_FAILED CHIP_APPLICATION_ERROR(0x07) + class AppTask { diff --git a/examples/lock-app/esp32/main/include/Button.h b/examples/lock-app/esp32/main/include/Button.h index 53ae87884b2865..d7ea047d52132c 100644 --- a/examples/lock-app/esp32/main/include/Button.h +++ b/examples/lock-app/esp32/main/include/Button.h @@ -18,8 +18,7 @@ #pragma once #include - -#include "AppConfig.h" +#include "driver/gpio.h" #include "AppTask.h" #include "freertos/FreeRTOS.h" diff --git a/examples/lock-app/esp32/main/include/AppConfig.h b/examples/platform/esp32/lock/AppConfig.h similarity index 98% rename from examples/lock-app/esp32/main/include/AppConfig.h rename to examples/platform/esp32/lock/AppConfig.h index ec883599d56627..e90d9a16336725 100644 --- a/examples/lock-app/esp32/main/include/AppConfig.h +++ b/examples/platform/esp32/lock/AppConfig.h @@ -17,8 +17,6 @@ #pragma once -#include "driver/gpio.h" - // ---- Lock Example App Config ---- #define APP_TASK_NAME "LOCK-APP" diff --git a/examples/all-clusters-app/esp32/main/include/AppEvent.h b/examples/platform/esp32/lock/AppEvent.h similarity index 90% rename from examples/all-clusters-app/esp32/main/include/AppEvent.h rename to examples/platform/esp32/lock/AppEvent.h index 26b97ab86d5018..114b228b078f01 100644 --- a/examples/all-clusters-app/esp32/main/include/AppEvent.h +++ b/examples/platform/esp32/lock/AppEvent.h @@ -29,6 +29,7 @@ struct AppEvent kEventType_Button = 0, kEventType_Timer, kEventType_Light, + kEventType_Lock, kEventType_Install, }; @@ -45,6 +46,11 @@ struct AppEvent { void * mContext; } mTimerEvent; + struct + { + uint8_t mAction; + int32_t mActor; + } mLockEvent; }; EventHandler mHandler; diff --git a/examples/lock-app/esp32/main/BoltLockManager.cpp b/examples/platform/esp32/lock/BoltLockManager.cpp similarity index 89% rename from examples/lock-app/esp32/main/BoltLockManager.cpp rename to examples/platform/esp32/lock/BoltLockManager.cpp index 7692be83ed45f6..9aecf05bb6e6d9 100644 --- a/examples/lock-app/esp32/main/BoltLockManager.cpp +++ b/examples/platform/esp32/lock/BoltLockManager.cpp @@ -19,7 +19,6 @@ #include "AppConfig.h" #include "AppTask.h" -#include #include #include #include @@ -269,22 +268,22 @@ void BoltLockManager::TimerEventHandler(TimerHandle_t xTimer) // once sLockTimer expires. Post an event to apptask queue with the actual handler // so that the event can be handled in the context of the apptask. AppEvent event; - event.Type = AppEvent::kEventType_Timer; - event.TimerEvent.Context = lock; + event.mType = AppEvent::kEventType_Timer; + event.mTimerEvent.mContext = lock; if (lock->mAutoLockTimerArmed) { - event.Handler = AutoReLockTimerEventHandler; + event.mHandler = AutoReLockTimerEventHandler; } else { - event.Handler = ActuatorMovementTimerEventHandler; + event.mHandler = ActuatorMovementTimerEventHandler; } GetAppTask().PostEvent(&event); } void BoltLockManager::AutoReLockTimerEventHandler(AppEvent * aEvent) { - BoltLockManager * lock = static_cast(aEvent->TimerEvent.Context); + BoltLockManager * lock = static_cast(aEvent->mTimerEvent.mContext); int32_t actor = 0; // Make sure auto lock timer is still armed. @@ -304,7 +303,7 @@ void BoltLockManager::ActuatorMovementTimerEventHandler(AppEvent * aEvent) { Action_t actionCompleted = INVALID_ACTION; - BoltLockManager * lock = static_cast(aEvent->TimerEvent.Context); + BoltLockManager * lock = static_cast(aEvent->mTimerEvent.mContext); if (lock->mState == kState_LockInitiated) { @@ -758,3 +757,80 @@ bool BoltLockManager::setLockState(chip::EndpointId endpointId, DlLockState lock err = DlOperationError::kInvalidCredential; return false; } + +CHIP_ERROR BoltLockManager::InitLockState() +{ + + // Initial lock state + chip::app::DataModel::Nullable state; + chip::EndpointId endpointId{ 1 }; + chip::DeviceLayer::PlatformMgr().LockChipStack(); + chip::app::Clusters::DoorLock::Attributes::LockState::Get(endpointId, state); + + uint8_t numberOfCredentialsPerUser = 0; + if (!DoorLockServer::Instance().GetNumberOfCredentialsSupportedPerUser(endpointId, numberOfCredentialsPerUser)) + { + ChipLogError(Zcl, + "Unable to get number of credentials supported per user when initializing lock endpoint, defaulting to 5 " + "[endpointId=%d]", + endpointId); + numberOfCredentialsPerUser = 5; + } + + uint16_t numberOfUsers = 0; + if (!DoorLockServer::Instance().GetNumberOfUserSupported(endpointId, numberOfUsers)) + { + ChipLogError(Zcl, + "Unable to get number of supported users when initializing lock endpoint, defaulting to 10 [endpointId=%d]", + endpointId); + numberOfUsers = 10; + } + + uint8_t numberOfWeekdaySchedulesPerUser = 0; + if (!DoorLockServer::Instance().GetNumberOfWeekDaySchedulesPerUserSupported(endpointId, numberOfWeekdaySchedulesPerUser)) + { + ChipLogError( + Zcl, + "Unable to get number of supported weekday schedules when initializing lock endpoint, defaulting to 10 [endpointId=%d]", + endpointId); + numberOfWeekdaySchedulesPerUser = 10; + } + + uint8_t numberOfYeardaySchedulesPerUser = 0; + if (!DoorLockServer::Instance().GetNumberOfYearDaySchedulesPerUserSupported(endpointId, numberOfYeardaySchedulesPerUser)) + { + ChipLogError( + Zcl, + "Unable to get number of supported yearday schedules when initializing lock endpoint, defaulting to 10 [endpointId=%d]", + endpointId); + numberOfYeardaySchedulesPerUser = 10; + } + + uint8_t numberOfHolidaySchedules = 0; + if (!DoorLockServer::Instance().GetNumberOfHolidaySchedulesSupported(endpointId, numberOfHolidaySchedules)) + { + ChipLogError( + Zcl, + "Unable to get number of supported holiday schedules when initializing lock endpoint, defaulting to 10 [endpointId=%d]", + endpointId); + numberOfHolidaySchedules = 10; + } + + chip::DeviceLayer::PlatformMgr().UnlockChipStack(); + + CHIP_ERROR err = BoltLockMgr().Init(state, + ParamBuilder() + .SetNumberOfUsers(numberOfUsers) + .SetNumberOfCredentialsPerUser(numberOfCredentialsPerUser) + .SetNumberOfWeekdaySchedulesPerUser(numberOfWeekdaySchedulesPerUser) + .SetNumberOfYeardaySchedulesPerUser(numberOfYeardaySchedulesPerUser) + .SetNumberOfHolidaySchedules(numberOfHolidaySchedules) + .GetLockParam()); + if (err != CHIP_NO_ERROR) + { + ESP_LOGI(TAG, "BoltLockMgr().Init() failed"); + return err; + } + + return err; +} diff --git a/examples/lock-app/esp32/main/include/BoltLockManager.h b/examples/platform/esp32/lock/BoltLockManager.h similarity index 98% rename from examples/lock-app/esp32/main/include/BoltLockManager.h rename to examples/platform/esp32/lock/BoltLockManager.h index 805facf375b08a..ff01d75e15f2f2 100644 --- a/examples/lock-app/esp32/main/include/BoltLockManager.h +++ b/examples/platform/esp32/lock/BoltLockManager.h @@ -23,8 +23,8 @@ #include "AppEvent.h" -#include "FreeRTOS.h" -#include "timers.h" // provides FreeRTOS timer support +#include "freertos/FreeRTOS.h" +#include "freertos/timers.h" // provides FreeRTOS timer support #include @@ -130,6 +130,7 @@ class BoltLockManager kState_UnlockCompleted, } State; + CHIP_ERROR InitLockState(); CHIP_ERROR Init(chip::app::DataModel::Nullable state, ESP32DoorLock::LockInitParams::LockParam lockParam); bool IsUnlocked(); diff --git a/examples/lock-app/esp32/main/ZclCallbacks.cpp b/examples/platform/esp32/lock/ZclCallbacks.cpp similarity index 99% rename from examples/lock-app/esp32/main/ZclCallbacks.cpp rename to examples/platform/esp32/lock/ZclCallbacks.cpp index 43be6d76bb7824..b77a66e615f143 100644 --- a/examples/lock-app/esp32/main/ZclCallbacks.cpp +++ b/examples/platform/esp32/lock/ZclCallbacks.cpp @@ -20,7 +20,7 @@ * This file implements the handler for data model messages. */ -#include "AppConfig.h" +//#include "AppConfig.h" #include "BoltLockManager.h" #include