Skip to content

Commit

Permalink
[NXP] App button manager rework (#35859)
Browse files Browse the repository at this point in the history
* [nxp][examples][common] Rework button manager

Create an abstraction over SDK buttons.

If an app needs a custom behavior for a specific button:
- it can implement the Button interface, similar to the ButtonDefault implementation
- it can implement a ButtonWithTimer derived class, to also have access to timer
behavior, such as scheduling a delay for a specific action (e.g. factory reset after 6s)

The ButtonManager is responsible with registering Button instances. The button callback is
common across all buttons because it delegates the proper action handling to the button itself
through the provided Button::Handle* API.

The ButtonRegistration header provides an API that can be used by other components (e.g. AppTask)
to initialize the ButtonManager and register buttons. A default implementation is provided.

Some button examples are implemented:
- ButtonApp
- ButtonBle
- ButtonDefault

Check the corresponding header for a description of actions.

Signed-off-by: marius-alex-tache <[email protected]>

* [nxp][examples][common] Update register buttons API usage

Update AppEvent structure for some apps to take into account
a way to pass extra args to App event processing.

Signed-off-by: marius-alex-tache <[email protected]>

* [nxp][examples][mcxw71_k32w1] Rework button manager

Update build system files for all impacted applications.

Signed-off-by: marius-alex-tache <[email protected]>

* [nxp][examples][rw61x] Update empty button registration path

Signed-off-by: marius-alex-tache <[email protected]>

* [nxp][examples][common] Add double click button action for ButtonBle

Double clicking the ButtonBle will trigger a toggle of SIT mode request
through the DSLS mechanism.

Signed-off-by: marius-alex-tache <[email protected]>

* Restyled by gn

* Restyled by prettier-markdown

---------

Signed-off-by: marius-alex-tache <[email protected]>
Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
marius-alex-tache and restyled-commits authored Oct 1, 2024
1 parent d404400 commit 57fb2e1
Show file tree
Hide file tree
Showing 33 changed files with 837 additions and 508 deletions.
2 changes: 1 addition & 1 deletion examples/all-clusters-app/nxp/rt/rw61x/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ rt_executable("all_cluster_app") {
"${common_example_dir}/device_callbacks/source/CommonDeviceCallbacks.cpp",
"${common_example_dir}/device_manager/source/CHIPDeviceManager.cpp",
"${common_example_dir}/icd/source/ICDUtil.cpp",
"${common_example_dir}/matter_button/source/AppMatterButtonEmpty.cpp",
"${common_example_dir}/matter_button/source/ButtonRegistrationEmpty.cpp",
]

deps = [
Expand Down
1 change: 1 addition & 0 deletions examples/contact-sensor-app/nxp/common/include/AppEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ typedef void (*EventHandler)(const AppEvent &);
struct AppEvent
{
EventHandler Handler;
void * extra;
};
10 changes: 6 additions & 4 deletions examples/contact-sensor-app/nxp/k32w1/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ mcxw71_k32w1_executable("contact_sensor_app") {
"${common_example_dir}/clusters/source/ZclCallbacks.cpp",
"${common_example_dir}/device_callbacks/source/CommonDeviceCallbacks.cpp",
"${common_example_dir}/device_manager/source/CHIPDeviceManager.cpp",
"${common_example_dir}/matter_button/source/ButtonApp.cpp",
"${common_example_dir}/matter_button/source/ButtonBle.cpp",
"${common_example_dir}/matter_button/source/ButtonManager.cpp",
"${common_example_dir}/matter_button/source/ButtonRegistrationAppAndBle.cpp",
"${common_example_dir}/matter_button/source/ButtonWithTimer.cpp",
"${common_example_dir}/operational_keystore/source/OperationalKeystoreS200.cpp",
"${example_platform_dir}/factory_data/source/AppFactoryDataExample.cpp",
]
Expand Down Expand Up @@ -193,10 +198,7 @@ mcxw71_k32w1_executable("contact_sensor_app") {
"${example_platform_dir}/button",
]

sources += [
"${example_platform_dir}/button/ButtonManager.cpp",
"${example_platform_dir}/clusters/Identify.cpp",
]
sources += [ "${example_platform_dir}/clusters/Identify.cpp" ]

if (chip_enable_ota_requestor) {
sources += [ "${example_platform_dir}/ota/OtaUtils.cpp" ]
Expand Down
10 changes: 6 additions & 4 deletions examples/contact-sensor-app/nxp/mcxw71/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ mcxw71_k32w1_executable("contact_sensor_app") {
"${common_example_dir}/clusters/source/ZclCallbacks.cpp",
"${common_example_dir}/device_callbacks/source/CommonDeviceCallbacks.cpp",
"${common_example_dir}/device_manager/source/CHIPDeviceManager.cpp",
"${common_example_dir}/matter_button/source/ButtonApp.cpp",
"${common_example_dir}/matter_button/source/ButtonBle.cpp",
"${common_example_dir}/matter_button/source/ButtonManager.cpp",
"${common_example_dir}/matter_button/source/ButtonRegistrationAppAndBle.cpp",
"${common_example_dir}/matter_button/source/ButtonWithTimer.cpp",
"${common_example_dir}/operational_keystore/source/OperationalKeystoreS200.cpp",
"${example_platform_dir}/factory_data/source/AppFactoryDataExample.cpp",
]
Expand Down Expand Up @@ -192,10 +197,7 @@ mcxw71_k32w1_executable("contact_sensor_app") {
"${example_platform_dir}/button",
]

sources += [
"${example_platform_dir}/button/ButtonManager.cpp",
"${example_platform_dir}/clusters/Identify.cpp",
]
sources += [ "${example_platform_dir}/clusters/Identify.cpp" ]

if (chip_enable_ota_requestor) {
sources += [ "${example_platform_dir}/ota/OtaUtils.cpp" ]
Expand Down
2 changes: 1 addition & 1 deletion examples/laundry-washer-app/nxp/rt/rw61x/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ rt_executable("laundry-washer") {
"${common_example_dir}/device_callbacks/source/CommonDeviceCallbacks.cpp",
"${common_example_dir}/device_manager/source/CHIPDeviceManager.cpp",
"${common_example_dir}/icd/source/ICDUtil.cpp",
"${common_example_dir}/matter_button/source/AppMatterButtonEmpty.cpp",
"${common_example_dir}/matter_button/source/ButtonRegistrationEmpty.cpp",
]

deps = [
Expand Down
1 change: 1 addition & 0 deletions examples/lighting-app/nxp/common/include/AppEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ typedef void (*EventHandler)(const AppEvent &);
struct AppEvent
{
EventHandler Handler;
void * extra;
};
10 changes: 6 additions & 4 deletions examples/lighting-app/nxp/k32w1/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ mcxw71_k32w1_executable("light_app") {
"${common_example_dir}/clusters/source/ZclCallbacks.cpp",
"${common_example_dir}/device_callbacks/source/CommonDeviceCallbacks.cpp",
"${common_example_dir}/device_manager/source/CHIPDeviceManager.cpp",
"${common_example_dir}/matter_button/source/ButtonApp.cpp",
"${common_example_dir}/matter_button/source/ButtonBle.cpp",
"${common_example_dir}/matter_button/source/ButtonManager.cpp",
"${common_example_dir}/matter_button/source/ButtonRegistrationAppAndBle.cpp",
"${common_example_dir}/matter_button/source/ButtonWithTimer.cpp",
"${common_example_dir}/operational_keystore/source/OperationalKeystoreS200.cpp",
"${example_platform_dir}/factory_data/source/AppFactoryDataExample.cpp",
]
Expand All @@ -180,10 +185,7 @@ mcxw71_k32w1_executable("light_app") {
"${example_platform_dir}/button",
]

sources += [
"${example_platform_dir}/button/ButtonManager.cpp",
"${example_platform_dir}/clusters/Identify.cpp",
]
sources += [ "${example_platform_dir}/clusters/Identify.cpp" ]

if (chip_enable_ota_requestor) {
sources += [ "${example_platform_dir}/ota/OtaUtils.cpp" ]
Expand Down
4 changes: 2 additions & 2 deletions examples/lighting-app/nxp/k32w1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ by running:
The console should have already been installed in the virtual environment. From
the `chip-console`, a user can send specific commands to the device.
For button commands, please run `rpcs.chip.rpc.Button.Event(index)` based on the
table below:
For button commands, please run `rpcs.chip.rpc.Button.Event(idx=index)` based on
the table below:
| index | action |
| ----- | --------------------------------------------- |
Expand Down
10 changes: 6 additions & 4 deletions examples/lighting-app/nxp/mcxw71/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ mcxw71_k32w1_executable("light_app") {
"${common_example_dir}/clusters/source/ZclCallbacks.cpp",
"${common_example_dir}/device_callbacks/source/CommonDeviceCallbacks.cpp",
"${common_example_dir}/device_manager/source/CHIPDeviceManager.cpp",
"${common_example_dir}/matter_button/source/ButtonApp.cpp",
"${common_example_dir}/matter_button/source/ButtonBle.cpp",
"${common_example_dir}/matter_button/source/ButtonManager.cpp",
"${common_example_dir}/matter_button/source/ButtonRegistrationAppAndBle.cpp",
"${common_example_dir}/matter_button/source/ButtonWithTimer.cpp",
"${common_example_dir}/operational_keystore/source/OperationalKeystoreS200.cpp",
"${example_platform_dir}/factory_data/source/AppFactoryDataExample.cpp",
]
Expand Down Expand Up @@ -183,10 +188,7 @@ mcxw71_k32w1_executable("light_app") {
"${example_platform_dir}/button",
]

sources += [
"${example_platform_dir}/button/ButtonManager.cpp",
"${example_platform_dir}/clusters/Identify.cpp",
]
sources += [ "${example_platform_dir}/clusters/Identify.cpp" ]

if (chip_enable_ota_requestor) {
sources += [ "${example_platform_dir}/ota/OtaUtils.cpp" ]
Expand Down
1 change: 1 addition & 0 deletions examples/lock-app/nxp/common/main/include/AppEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ struct AppEvent
};

EventHandler Handler;
void * extra;
};
10 changes: 6 additions & 4 deletions examples/lock-app/nxp/k32w1/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ mcxw71_k32w1_executable("lock_app") {
sources += [
"${common_example_dir}/app_task/source/AppTaskBase.cpp",
"${common_example_dir}/app_task/source/AppTaskFreeRTOS.cpp",
"${common_example_dir}/matter_button/source/ButtonApp.cpp",
"${common_example_dir}/matter_button/source/ButtonBle.cpp",
"${common_example_dir}/matter_button/source/ButtonManager.cpp",
"${common_example_dir}/matter_button/source/ButtonRegistrationAppAndBle.cpp",
"${common_example_dir}/matter_button/source/ButtonWithTimer.cpp",

#"${common_example_dir}/clusters/source/ZclCallbacks.cpp",
"${common_example_dir}/device_callbacks/source/CommonDeviceCallbacks.cpp",
Expand Down Expand Up @@ -221,10 +226,7 @@ mcxw71_k32w1_executable("lock_app") {
"${example_platform_dir}/button",
]

sources += [
"${example_platform_dir}/button/ButtonManager.cpp",
"${example_platform_dir}/clusters/Identify.cpp",
]
sources += [ "${example_platform_dir}/clusters/Identify.cpp" ]

if (chip_enable_ota_requestor) {
sources += [ "${example_platform_dir}/ota/OtaUtils.cpp" ]
Expand Down
10 changes: 6 additions & 4 deletions examples/lock-app/nxp/mcxw71/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ mcxw71_k32w1_executable("lock_app") {
sources += [
"${common_example_dir}/app_task/source/AppTaskBase.cpp",
"${common_example_dir}/app_task/source/AppTaskFreeRTOS.cpp",
"${common_example_dir}/matter_button/source/ButtonApp.cpp",
"${common_example_dir}/matter_button/source/ButtonBle.cpp",
"${common_example_dir}/matter_button/source/ButtonManager.cpp",
"${common_example_dir}/matter_button/source/ButtonRegistrationAppAndBle.cpp",
"${common_example_dir}/matter_button/source/ButtonWithTimer.cpp",

#"${common_example_dir}/clusters/source/ZclCallbacks.cpp",
"${common_example_dir}/device_callbacks/source/CommonDeviceCallbacks.cpp",
Expand Down Expand Up @@ -221,10 +226,7 @@ mcxw71_k32w1_executable("lock_app") {
"${example_platform_dir}/button",
]

sources += [
"${example_platform_dir}/button/ButtonManager.cpp",
"${example_platform_dir}/clusters/Identify.cpp",
]
sources += [ "${example_platform_dir}/clusters/Identify.cpp" ]

if (chip_enable_ota_requestor) {
sources += [ "${example_platform_dir}/ota/OtaUtils.cpp" ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <app/server/Dnssd.h>
#include <lib/dnssd/Advertiser.h>

#include "AppMatterButton.h"
#include "ButtonRegistration.h"

#include "CHIPDeviceManager.h"
#include <app/server/Server.h>
Expand Down Expand Up @@ -98,10 +98,10 @@ CHIP_ERROR chip::NXP::App::AppTaskFreeRTOS::AppMatter_Register()
#endif

/* Register Matter buttons */
err = AppMatterButton_registerButtons();
err = chip::NXP::App::RegisterButtons();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Error during AppMatterButton_registerButtons");
ChipLogError(DeviceLayer, "Error during button registration");
return err;
}
return err;
Expand Down
52 changes: 52 additions & 0 deletions examples/platform/nxp/common/matter_button/include/Button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
*
* Copyright (c) 2024 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.
*/

#pragma once

#include "fsl_component_button.h"

#include <lib/core/CHIPError.h>

namespace chip::NXP::App {

/**
* @brief This class is an abstraction over an SDK button.
*
*/
class Button
{
public:
using Callback = button_status_t (*)(void * handle, button_callback_message_t * message, void * param);

virtual ~Button() = default;

virtual CHIP_ERROR Init() = 0;
virtual void HandleShortPress() = 0;
virtual void HandleLongPress() = 0;
virtual void HandleDoubleClick() = 0;

/**
* @brief This is an SDK handle for a button.
*
* It should be set in the Init method, based on a newly defined
* handle or an already defined handle owned by the SDK.
*/
button_handle_t handle = nullptr;
};

} // namespace chip::NXP::App
44 changes: 44 additions & 0 deletions examples/platform/nxp/common/matter_button/include/ButtonApp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
*
* Copyright (c) 2024 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.
*/

#pragma once

#include "Button.h"

namespace chip::NXP::App {
/**
* @brief This class implements a custom app button behavior.
*
* | Action | Effect |
* | ------------ | --------------------------------- |
* | Short press | Switch a cluster attribute state |
* | Long press | Schedule a soft reset taking into |
* | | account Matter shutdown mechanism |
* | Double click | Do nothing |
*
*/
class ButtonApp : public Button
{
public:
virtual CHIP_ERROR Init() override;
virtual void HandleShortPress() override;
virtual void HandleLongPress() override;
virtual void HandleDoubleClick() override;
};

} // namespace chip::NXP::App
48 changes: 48 additions & 0 deletions examples/platform/nxp/common/matter_button/include/ButtonBle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
*
* Copyright (c) 2024 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.
*/

#pragma once

#include "ButtonWithTimer.h"

namespace chip::NXP::App {

/**
* @brief This class implements a customized default button behavior.
*
* | Action | Effect |
* | ------------ | -------------------------------------------------- |
* | Short press | If a factory reset is scheduled, cancel it. |
* | | Else if the device is commissioned and a factory |
* | | reset is not scheduled, switch to ICD active mode. |
* | | Otherwise, switch commissioning state. |
* | Long press | Schedule a factory reset |
* | Double click | Toggle SIT mode request through DSLS mechanism |
*
*/
class ButtonBle : public ButtonWithTimer
{
public:
virtual CHIP_ERROR Init() override;
virtual void HandleShortPress() override;
virtual void HandleLongPress() override;
virtual void HandleDoubleClick() override;
virtual void HandleTimerExpire() override;
};

} // namespace chip::NXP::App
Loading

0 comments on commit 57fb2e1

Please sign in to comment.