From 1278274fd2bac044a30ce903a88213bf2499581b Mon Sep 17 00:00:00 2001 From: sreeramIfx <118491113+sreeramIfx@users.noreply.github.com> Date: Fri, 9 Dec 2022 08:43:58 -0800 Subject: [PATCH] [Infineon] Integrate Door Lock Alarm Event (#23852) * [Infineon] Integrate Door Lock Alarm Event Fixes #23942 * Restyled by clang-format * Update examples/lock-app/infineon/cyw30739/src/ButtonHandler.cpp Co-authored-by: Boris Zbarsky Co-authored-by: Restyled.io Co-authored-by: Boris Zbarsky --- .../infineon/cyw30739/include/LockManager.h | 1 + .../infineon/cyw30739/src/ButtonHandler.cpp | 49 ++++++++++-------- .../infineon/cyw30739/src/LockManager.cpp | 10 +++- .../infineon/psoc6/include/AppConfig.h | 3 +- .../infineon/psoc6/include/ButtonHandler.h | 4 +- .../psoc6/include/CHIPProjectConfig.h | 8 +++ .../lock-app/infineon/psoc6/src/AppTask.cpp | 25 +++++++-- .../infineon/psoc6/src/ButtonHandler.cpp | 51 ++++++++++++------- 8 files changed, 102 insertions(+), 49 deletions(-) diff --git a/examples/lock-app/infineon/cyw30739/include/LockManager.h b/examples/lock-app/infineon/cyw30739/include/LockManager.h index 4bf738182d47b9..8f4cdf658d7375 100644 --- a/examples/lock-app/infineon/cyw30739/include/LockManager.h +++ b/examples/lock-app/infineon/cyw30739/include/LockManager.h @@ -119,6 +119,7 @@ class LockManager { LOCK_ACTION = 0, UNLOCK_ACTION, + LOCK_JAMMED, INVALID_ACTION } Action; diff --git a/examples/lock-app/infineon/cyw30739/src/ButtonHandler.cpp b/examples/lock-app/infineon/cyw30739/src/ButtonHandler.cpp index 51279a8c80c104..40233323890c96 100644 --- a/examples/lock-app/infineon/cyw30739/src/ButtonHandler.cpp +++ b/examples/lock-app/infineon/cyw30739/src/ButtonHandler.cpp @@ -56,14 +56,14 @@ wiced_result_t app_button_init(void) memset(app_buttons, 0, (sizeof(button_manager_button_t) * APP_MAX_BUTTON_DEF)); app_button_configurations[ON_OFF_BUTTON].button = PLATFORM_BUTTON_1; - app_button_configurations[ON_OFF_BUTTON].button_event_mask = BUTTON_CLICK_EVENT; + app_button_configurations[ON_OFF_BUTTON].button_event_mask = (BUTTON_CLICK_EVENT | BUTTON_LONG_DURATION_EVENT); app_buttons[ON_OFF_BUTTON].configuration = &app_button_configurations[ON_OFF_BUTTON]; result = wiced_button_manager_init(&app_button_manager, &app_button_manager_configuration, app_buttons, 1); if (result != WICED_SUCCESS) { - printf("button_manager_init failed (%d)\n", result); + ChipLogProgress(Zcl, "button_manager_init failed (%d)\n", result); } return result; } @@ -71,33 +71,38 @@ wiced_result_t app_button_init(void) void app_button_event_handler(const button_manager_button_t * button_mgr, button_manager_event_t event, button_manager_button_state_t state) { - // printf("app_button_event_handler. button=%d, event=%d, state=%d\n", button_mgr[ON_OFF_BUTTON].configuration->button, event, - // state); bool initiated = false; LockManager::Action_t action; - CHIP_ERROR err = CHIP_NO_ERROR; - if (button_mgr[0].configuration->button == PLATFORM_BUTTON_1 && event == BUTTON_CLICK_EVENT && state == BUTTON_STATE_RELEASED) + + ChipLogProgress(Zcl, "app_button_event_handler. button=%d, event=%d, state=%d\n", + button_mgr[ON_OFF_BUTTON].configuration->button, event, state); + + /* This app is interested in PLATFORM_BUTTON_1 only */ + if (button_mgr[0].configuration->button != PLATFORM_BUTTON_1) + return; + + /* This callback is invoked both for held and released state, we want to process on the released event to avoid duplication */ + if (state != BUTTON_STATE_RELEASED) + return; + + /* single click to Lock/Unlock + long press to generate Jammed event */ + if (event == BUTTON_CLICK_EVENT) { - if (LockMgr().NextState() == true) - { - action = LockManager::LOCK_ACTION; - } - else - { - action = LockManager::UNLOCK_ACTION; - } + action = (LockMgr().NextState() == true) ? LockManager::LOCK_ACTION : LockManager::UNLOCK_ACTION; } - else + else if (event == BUTTON_LONG_DURATION_EVENT) { - err = CHIP_ERROR_UNEXPECTED_EVENT; + action = LockManager::LOCK_JAMMED; } - if (err == CHIP_NO_ERROR) + else { - initiated = LockMgr().InitiateAction(AppEvent::kEventType_Button, action); + return; + } - if (!initiated) - { - printf("Action is already in progress or active."); - } + initiated = LockMgr().InitiateAction(AppEvent::kEventType_Button, action); + if (!initiated) + { + ChipLogProgress(Zcl, "Action is already in progress or active."); } } diff --git a/examples/lock-app/infineon/cyw30739/src/LockManager.cpp b/examples/lock-app/infineon/cyw30739/src/LockManager.cpp index d434e94924c3a4..598e454f2e4d1a 100644 --- a/examples/lock-app/infineon/cyw30739/src/LockManager.cpp +++ b/examples/lock-app/infineon/cyw30739/src/LockManager.cpp @@ -181,6 +181,15 @@ bool LockManager::InitiateAction(int32_t aActor, Action_t aAction) bool action_initiated = false; State_t new_state; + if (aAction == LOCK_JAMMED) + { + ChipLogProgress(Zcl, "Sending a lock jammed event"); + + /* Generating Door Lock Jammed event */ + DoorLockServer::Instance().SendLockAlarmEvent(1, DlAlarmCode::kLockJammed); + return true; + } + // Initiate Lock/Unlock Action only when the previous one is complete. if (mState == kState_LockCompleted && aAction == UNLOCK_ACTION) { @@ -197,7 +206,6 @@ bool LockManager::InitiateAction(int32_t aActor, Action_t aAction) if (action_initiated) { - StartTimer(ACTUATOR_MOVEMENT_PERIOS_MS); // Since the timer started successfully, update the state and trigger callback diff --git a/examples/lock-app/infineon/psoc6/include/AppConfig.h b/examples/lock-app/infineon/psoc6/include/AppConfig.h index f20ca007a5a3fc..41ff70e1f94b71 100644 --- a/examples/lock-app/infineon/psoc6/include/AppConfig.h +++ b/examples/lock-app/infineon/psoc6/include/AppConfig.h @@ -31,10 +31,11 @@ #define APP_LOCK_BUTTON CYBSP_USER_BTN1 #define APP_FUNCTION_BUTTON CYBSP_USER_BTN2 -#define APP_BUTTON_MIN_ASSERT_TIME_MS 30 +#define APP_BUTTON_MIN_ASSERT_TIME_MS 500 #define APP_BUTTON_PRESSED 0 #define APP_BUTTON_RELEASED 1 +#define APP_BUTTON_LONG_PRESS 2 #define SYSTEM_STATE_LED CYBSP_USER_LED1 #define LOCK_STATE_LED CYBSP_USER_LED2 diff --git a/examples/lock-app/infineon/psoc6/include/ButtonHandler.h b/examples/lock-app/infineon/psoc6/include/ButtonHandler.h index 3820e162780640..db2f94feb3fc35 100644 --- a/examples/lock-app/infineon/psoc6/include/ButtonHandler.h +++ b/examples/lock-app/infineon/psoc6/include/ButtonHandler.h @@ -33,7 +33,7 @@ class ButtonHandler private: static void GpioInit(void); - static void lock_button_callback(void * handler_arg, cyhal_gpio_event_t event); - static void func_button_callback(void * handler_arg, cyhal_gpio_event_t event); + static void LockButtonCallback(void * handler_arg, cyhal_gpio_event_t event); + static void FuncButtonCallback(void * handler_arg, cyhal_gpio_event_t event); static void TimerCallback(TimerHandle_t xTimer); }; diff --git a/examples/lock-app/infineon/psoc6/include/CHIPProjectConfig.h b/examples/lock-app/infineon/psoc6/include/CHIPProjectConfig.h index f24b7b73241926..b069e13284c07d 100644 --- a/examples/lock-app/infineon/psoc6/include/CHIPProjectConfig.h +++ b/examples/lock-app/infineon/psoc6/include/CHIPProjectConfig.h @@ -89,6 +89,14 @@ */ #define CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE 1 +/** + * CHIP_DEVICE_CONFIG_ENABLE_CHIP_TIME_SERVICE_TIME_SYNC + * + * Enables synchronizing the device's real time clock with a remote Chip Time service + * using the Chip Time Sync protocol. + */ +#define CHIP_DEVICE_CONFIG_ENABLE_CHIP_TIME_SERVICE_TIME_SYNC 0 + /** * CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER * diff --git a/examples/lock-app/infineon/psoc6/src/AppTask.cpp b/examples/lock-app/infineon/psoc6/src/AppTask.cpp index db2725170740f8..a06f1ca687dca0 100644 --- a/examples/lock-app/infineon/psoc6/src/AppTask.cpp +++ b/examples/lock-app/infineon/psoc6/src/AppTask.cpp @@ -399,16 +399,31 @@ void AppTask::LockActionEventHandler(AppEvent * event) } case AppEvent::kEventType_Button: { - if (LockMgr().NextState() == true) + + P6_LOG("%s [Action: %d]", __FUNCTION__, event->ButtonEvent.Action); + + if (event->ButtonEvent.Action == APP_BUTTON_LONG_PRESS) { - action = LockManager::LOCK_ACTION; + P6_LOG("Sending a lock jammed event"); + + /* Generating Door Lock Jammed event */ + DoorLockServer::Instance().SendLockAlarmEvent(1 /* Endpoint Id */, DlAlarmCode::kLockJammed); + + return; } else { - action = LockManager::UNLOCK_ACTION; - } + if (LockMgr().NextState() == true) + { + action = LockManager::LOCK_ACTION; + } + else + { + action = LockManager::UNLOCK_ACTION; + } - actor = AppEvent::kEventType_Button; + actor = AppEvent::kEventType_Button; + } break; } diff --git a/examples/lock-app/infineon/psoc6/src/ButtonHandler.cpp b/examples/lock-app/infineon/psoc6/src/ButtonHandler.cpp index 2eea4c760a20f9..84aaac16ae3d41 100644 --- a/examples/lock-app/infineon/psoc6/src/ButtonHandler.cpp +++ b/examples/lock-app/infineon/psoc6/src/ButtonHandler.cpp @@ -32,6 +32,7 @@ TimerHandle_t buttonTimers[kButtonCount]; // FreeRTOS timers used for debouncing void ButtonHandler::Init(void) { GpioInit(); + // Create FreeRTOS sw timers for debouncing buttons. for (uint8_t i = 0; i < kButtonCount; i++) { @@ -48,36 +49,42 @@ void ButtonHandler::Init(void) void ButtonHandler::GpioInit(void) { cy_rslt_t result = CY_RSLT_SUCCESS; + static cyhal_gpio_callback_data_t lockButtonCbData; + static cyhal_gpio_callback_data_t funcButtonCbData; + // Set up button GPIOs to input with pullups. result = cyhal_gpio_init(APP_LOCK_BUTTON, CYHAL_GPIO_DIR_INPUT, CYHAL_GPIO_DRIVE_PULLUP, CYBSP_BTN_OFF); if (result != CY_RSLT_SUCCESS) { printf(" cyhal_gpio_init failed for APP_LOCK_BUTTON\r\n"); } + result = cyhal_gpio_init(APP_FUNCTION_BUTTON, CYHAL_GPIO_DIR_INPUT, CYHAL_GPIO_DRIVE_PULLUP, CYBSP_BTN_OFF); if (result != CY_RSLT_SUCCESS) { printf(" cyhal_gpio_init failed for APP_FUNCTION_BUTTON\r\n"); } + /* Configure GPIO interrupt. */ - static cyhal_gpio_callback_data_t lock_button_cbdata; - static cyhal_gpio_callback_data_t func_button_cbdata; - lock_button_cbdata.callback = lock_button_callback; - lock_button_cbdata.callback_arg = NULL; - cyhal_gpio_register_callback(APP_LOCK_BUTTON, &lock_button_cbdata); - func_button_cbdata.callback = func_button_callback; - func_button_cbdata.callback_arg = NULL; - cyhal_gpio_register_callback(APP_FUNCTION_BUTTON, &func_button_cbdata); + lockButtonCbData.callback = LockButtonCallback; + lockButtonCbData.callback_arg = NULL; + cyhal_gpio_register_callback(APP_LOCK_BUTTON, &lockButtonCbData); + + funcButtonCbData.callback = FuncButtonCallback; + funcButtonCbData.callback_arg = NULL; + cyhal_gpio_register_callback(APP_FUNCTION_BUTTON, &funcButtonCbData); + cyhal_gpio_enable_event(APP_LOCK_BUTTON, CYHAL_GPIO_IRQ_FALL, GPIO_INTERRUPT_PRIORITY, true); cyhal_gpio_enable_event(APP_FUNCTION_BUTTON, CYHAL_GPIO_IRQ_FALL, GPIO_INTERRUPT_PRIORITY, true); } -void ButtonHandler::lock_button_callback(void * handler_arg, cyhal_gpio_event_t event) + +void ButtonHandler::LockButtonCallback(void * handler_arg, cyhal_gpio_event_t event) { portBASE_TYPE taskWoken = pdFALSE; xTimerStartFromISR(buttonTimers[APP_LOCK_BUTTON_IDX], &taskWoken); } -void ButtonHandler::func_button_callback(void * handler_arg, cyhal_gpio_event_t event) +void ButtonHandler::FuncButtonCallback(void * handler_arg, cyhal_gpio_event_t event) { portBASE_TYPE taskWoken = pdFALSE; xTimerStartFromISR(buttonTimers[APP_FUNCTION_BUTTON_IDX], &taskWoken); @@ -85,21 +92,29 @@ void ButtonHandler::func_button_callback(void * handler_arg, cyhal_gpio_event_t void ButtonHandler::TimerCallback(TimerHandle_t xTimer) { - // Get the button index of the expired timer and call button event helper. - uint32_t timerId; - uint8_t buttonevent; + uint32_t timerId = 0; + uint8_t buttonevent = 0; + + /* Get the button index of the expired timer and call button event helper. */ timerId = (uint32_t) pvTimerGetTimerID(xTimer); if (timerId) { buttonevent = cyhal_gpio_read(APP_FUNCTION_BUTTON); + if (buttonevent) + { + GetAppTask().ButtonEventHandler(timerId, APP_BUTTON_PRESSED); + } } else { buttonevent = cyhal_gpio_read(APP_LOCK_BUTTON); - } - - if (!buttonevent) - { - GetAppTask().ButtonEventHandler(timerId, APP_BUTTON_PRESSED); + if (!buttonevent) + { + GetAppTask().ButtonEventHandler(timerId, APP_BUTTON_LONG_PRESS); + } + else + { + GetAppTask().ButtonEventHandler(timerId, APP_BUTTON_PRESSED); + } } }