Skip to content

Commit

Permalink
esp32/all-clusters-app's uses linux door-lock replace it with esp32 d…
Browse files Browse the repository at this point in the history
…oor-lock support
  • Loading branch information
jadhavrohit924 committed Jan 6, 2023
1 parent 71a5df5 commit 166baea
Show file tree
Hide file tree
Showing 15 changed files with 275 additions and 178 deletions.
118 changes: 117 additions & 1 deletion examples/all-clusters-app/esp32/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@
#include "esp_spi_flash.h"
#include "freertos/FreeRTOS.h"
#include <app/server/OnboardingCodesUtil.h>
#include <lock/BoltLockManager.h>

#define FACTORY_RESET_CANCEL_WINDOW_TIMEOUT 3000
#define APP_TASK_NAME "APP"
#define APP_EVENT_QUEUE_SIZE 10
#define APP_TASK_STACK_SIZE (3072)

static const char * TAG = "app-task";

namespace {
TimerHandle_t sFunctionTimer; // FreeRTOS app sw timer

QueueHandle_t sAppEventQueue;
TaskHandle_t sAppTaskHandle;
Expand All @@ -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 */
Expand All @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions examples/all-clusters-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 26 additions & 1 deletion examples/all-clusters-app/esp32/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
*/

#pragma once
#include "AppEvent.h"
#include <lock/AppEvent.h>
#include <platform/CHIPDeviceLayer.h>
#include <lock/BoltLockManager.h>

// Application-defined error codes in the CHIP_ERROR space.
#define APP_ERROR_EVENT_QUEUE_FAILED CHIP_APPLICATION_ERROR(0x01)
Expand All @@ -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
{
Expand All @@ -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);
};
Expand Down
Loading

0 comments on commit 166baea

Please sign in to comment.