Skip to content

Commit 57fb2e1

Browse files
[NXP] App button manager rework (#35859)
* [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 <marius.tache@nxp.com> * [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 <marius.tache@nxp.com> * [nxp][examples][mcxw71_k32w1] Rework button manager Update build system files for all impacted applications. Signed-off-by: marius-alex-tache <marius.tache@nxp.com> * [nxp][examples][rw61x] Update empty button registration path Signed-off-by: marius-alex-tache <marius.tache@nxp.com> * [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 <marius.tache@nxp.com> * Restyled by gn * Restyled by prettier-markdown --------- Signed-off-by: marius-alex-tache <marius.tache@nxp.com> Co-authored-by: Restyled.io <commits@restyled.io>
1 parent d404400 commit 57fb2e1

File tree

33 files changed

+837
-508
lines changed

33 files changed

+837
-508
lines changed

examples/all-clusters-app/nxp/rt/rw61x/BUILD.gn

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ rt_executable("all_cluster_app") {
170170
"${common_example_dir}/device_callbacks/source/CommonDeviceCallbacks.cpp",
171171
"${common_example_dir}/device_manager/source/CHIPDeviceManager.cpp",
172172
"${common_example_dir}/icd/source/ICDUtil.cpp",
173-
"${common_example_dir}/matter_button/source/AppMatterButtonEmpty.cpp",
173+
"${common_example_dir}/matter_button/source/ButtonRegistrationEmpty.cpp",
174174
]
175175

176176
deps = [

examples/contact-sensor-app/nxp/common/include/AppEvent.h

+1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ typedef void (*EventHandler)(const AppEvent &);
2424
struct AppEvent
2525
{
2626
EventHandler Handler;
27+
void * extra;
2728
};

examples/contact-sensor-app/nxp/k32w1/BUILD.gn

+6-4
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ mcxw71_k32w1_executable("contact_sensor_app") {
149149
"${common_example_dir}/clusters/source/ZclCallbacks.cpp",
150150
"${common_example_dir}/device_callbacks/source/CommonDeviceCallbacks.cpp",
151151
"${common_example_dir}/device_manager/source/CHIPDeviceManager.cpp",
152+
"${common_example_dir}/matter_button/source/ButtonApp.cpp",
153+
"${common_example_dir}/matter_button/source/ButtonBle.cpp",
154+
"${common_example_dir}/matter_button/source/ButtonManager.cpp",
155+
"${common_example_dir}/matter_button/source/ButtonRegistrationAppAndBle.cpp",
156+
"${common_example_dir}/matter_button/source/ButtonWithTimer.cpp",
152157
"${common_example_dir}/operational_keystore/source/OperationalKeystoreS200.cpp",
153158
"${example_platform_dir}/factory_data/source/AppFactoryDataExample.cpp",
154159
]
@@ -193,10 +198,7 @@ mcxw71_k32w1_executable("contact_sensor_app") {
193198
"${example_platform_dir}/button",
194199
]
195200

196-
sources += [
197-
"${example_platform_dir}/button/ButtonManager.cpp",
198-
"${example_platform_dir}/clusters/Identify.cpp",
199-
]
201+
sources += [ "${example_platform_dir}/clusters/Identify.cpp" ]
200202

201203
if (chip_enable_ota_requestor) {
202204
sources += [ "${example_platform_dir}/ota/OtaUtils.cpp" ]

examples/contact-sensor-app/nxp/mcxw71/BUILD.gn

+6-4
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ mcxw71_k32w1_executable("contact_sensor_app") {
148148
"${common_example_dir}/clusters/source/ZclCallbacks.cpp",
149149
"${common_example_dir}/device_callbacks/source/CommonDeviceCallbacks.cpp",
150150
"${common_example_dir}/device_manager/source/CHIPDeviceManager.cpp",
151+
"${common_example_dir}/matter_button/source/ButtonApp.cpp",
152+
"${common_example_dir}/matter_button/source/ButtonBle.cpp",
153+
"${common_example_dir}/matter_button/source/ButtonManager.cpp",
154+
"${common_example_dir}/matter_button/source/ButtonRegistrationAppAndBle.cpp",
155+
"${common_example_dir}/matter_button/source/ButtonWithTimer.cpp",
151156
"${common_example_dir}/operational_keystore/source/OperationalKeystoreS200.cpp",
152157
"${example_platform_dir}/factory_data/source/AppFactoryDataExample.cpp",
153158
]
@@ -192,10 +197,7 @@ mcxw71_k32w1_executable("contact_sensor_app") {
192197
"${example_platform_dir}/button",
193198
]
194199

195-
sources += [
196-
"${example_platform_dir}/button/ButtonManager.cpp",
197-
"${example_platform_dir}/clusters/Identify.cpp",
198-
]
200+
sources += [ "${example_platform_dir}/clusters/Identify.cpp" ]
199201

200202
if (chip_enable_ota_requestor) {
201203
sources += [ "${example_platform_dir}/ota/OtaUtils.cpp" ]

examples/laundry-washer-app/nxp/rt/rw61x/BUILD.gn

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ rt_executable("laundry-washer") {
171171
"${common_example_dir}/device_callbacks/source/CommonDeviceCallbacks.cpp",
172172
"${common_example_dir}/device_manager/source/CHIPDeviceManager.cpp",
173173
"${common_example_dir}/icd/source/ICDUtil.cpp",
174-
"${common_example_dir}/matter_button/source/AppMatterButtonEmpty.cpp",
174+
"${common_example_dir}/matter_button/source/ButtonRegistrationEmpty.cpp",
175175
]
176176

177177
deps = [

examples/lighting-app/nxp/common/include/AppEvent.h

+1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ typedef void (*EventHandler)(const AppEvent &);
2424
struct AppEvent
2525
{
2626
EventHandler Handler;
27+
void * extra;
2728
};

examples/lighting-app/nxp/k32w1/BUILD.gn

+6-4
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ mcxw71_k32w1_executable("light_app") {
156156
"${common_example_dir}/clusters/source/ZclCallbacks.cpp",
157157
"${common_example_dir}/device_callbacks/source/CommonDeviceCallbacks.cpp",
158158
"${common_example_dir}/device_manager/source/CHIPDeviceManager.cpp",
159+
"${common_example_dir}/matter_button/source/ButtonApp.cpp",
160+
"${common_example_dir}/matter_button/source/ButtonBle.cpp",
161+
"${common_example_dir}/matter_button/source/ButtonManager.cpp",
162+
"${common_example_dir}/matter_button/source/ButtonRegistrationAppAndBle.cpp",
163+
"${common_example_dir}/matter_button/source/ButtonWithTimer.cpp",
159164
"${common_example_dir}/operational_keystore/source/OperationalKeystoreS200.cpp",
160165
"${example_platform_dir}/factory_data/source/AppFactoryDataExample.cpp",
161166
]
@@ -180,10 +185,7 @@ mcxw71_k32w1_executable("light_app") {
180185
"${example_platform_dir}/button",
181186
]
182187

183-
sources += [
184-
"${example_platform_dir}/button/ButtonManager.cpp",
185-
"${example_platform_dir}/clusters/Identify.cpp",
186-
]
188+
sources += [ "${example_platform_dir}/clusters/Identify.cpp" ]
187189

188190
if (chip_enable_ota_requestor) {
189191
sources += [ "${example_platform_dir}/ota/OtaUtils.cpp" ]

examples/lighting-app/nxp/k32w1/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ by running:
220220
The console should have already been installed in the virtual environment. From
221221
the `chip-console`, a user can send specific commands to the device.
222222
223-
For button commands, please run `rpcs.chip.rpc.Button.Event(index)` based on the
224-
table below:
223+
For button commands, please run `rpcs.chip.rpc.Button.Event(idx=index)` based on
224+
the table below:
225225
226226
| index | action |
227227
| ----- | --------------------------------------------- |

examples/lighting-app/nxp/mcxw71/BUILD.gn

+6-4
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ mcxw71_k32w1_executable("light_app") {
156156
"${common_example_dir}/clusters/source/ZclCallbacks.cpp",
157157
"${common_example_dir}/device_callbacks/source/CommonDeviceCallbacks.cpp",
158158
"${common_example_dir}/device_manager/source/CHIPDeviceManager.cpp",
159+
"${common_example_dir}/matter_button/source/ButtonApp.cpp",
160+
"${common_example_dir}/matter_button/source/ButtonBle.cpp",
161+
"${common_example_dir}/matter_button/source/ButtonManager.cpp",
162+
"${common_example_dir}/matter_button/source/ButtonRegistrationAppAndBle.cpp",
163+
"${common_example_dir}/matter_button/source/ButtonWithTimer.cpp",
159164
"${common_example_dir}/operational_keystore/source/OperationalKeystoreS200.cpp",
160165
"${example_platform_dir}/factory_data/source/AppFactoryDataExample.cpp",
161166
]
@@ -183,10 +188,7 @@ mcxw71_k32w1_executable("light_app") {
183188
"${example_platform_dir}/button",
184189
]
185190

186-
sources += [
187-
"${example_platform_dir}/button/ButtonManager.cpp",
188-
"${example_platform_dir}/clusters/Identify.cpp",
189-
]
191+
sources += [ "${example_platform_dir}/clusters/Identify.cpp" ]
190192

191193
if (chip_enable_ota_requestor) {
192194
sources += [ "${example_platform_dir}/ota/OtaUtils.cpp" ]

examples/lock-app/nxp/common/main/include/AppEvent.h

+1
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ struct AppEvent
4848
};
4949

5050
EventHandler Handler;
51+
void * extra;
5152
};

examples/lock-app/nxp/k32w1/BUILD.gn

+6-4
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ mcxw71_k32w1_executable("lock_app") {
154154
sources += [
155155
"${common_example_dir}/app_task/source/AppTaskBase.cpp",
156156
"${common_example_dir}/app_task/source/AppTaskFreeRTOS.cpp",
157+
"${common_example_dir}/matter_button/source/ButtonApp.cpp",
158+
"${common_example_dir}/matter_button/source/ButtonBle.cpp",
159+
"${common_example_dir}/matter_button/source/ButtonManager.cpp",
160+
"${common_example_dir}/matter_button/source/ButtonRegistrationAppAndBle.cpp",
161+
"${common_example_dir}/matter_button/source/ButtonWithTimer.cpp",
157162

158163
#"${common_example_dir}/clusters/source/ZclCallbacks.cpp",
159164
"${common_example_dir}/device_callbacks/source/CommonDeviceCallbacks.cpp",
@@ -221,10 +226,7 @@ mcxw71_k32w1_executable("lock_app") {
221226
"${example_platform_dir}/button",
222227
]
223228

224-
sources += [
225-
"${example_platform_dir}/button/ButtonManager.cpp",
226-
"${example_platform_dir}/clusters/Identify.cpp",
227-
]
229+
sources += [ "${example_platform_dir}/clusters/Identify.cpp" ]
228230

229231
if (chip_enable_ota_requestor) {
230232
sources += [ "${example_platform_dir}/ota/OtaUtils.cpp" ]

examples/lock-app/nxp/mcxw71/BUILD.gn

+6-4
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ mcxw71_k32w1_executable("lock_app") {
154154
sources += [
155155
"${common_example_dir}/app_task/source/AppTaskBase.cpp",
156156
"${common_example_dir}/app_task/source/AppTaskFreeRTOS.cpp",
157+
"${common_example_dir}/matter_button/source/ButtonApp.cpp",
158+
"${common_example_dir}/matter_button/source/ButtonBle.cpp",
159+
"${common_example_dir}/matter_button/source/ButtonManager.cpp",
160+
"${common_example_dir}/matter_button/source/ButtonRegistrationAppAndBle.cpp",
161+
"${common_example_dir}/matter_button/source/ButtonWithTimer.cpp",
157162

158163
#"${common_example_dir}/clusters/source/ZclCallbacks.cpp",
159164
"${common_example_dir}/device_callbacks/source/CommonDeviceCallbacks.cpp",
@@ -221,10 +226,7 @@ mcxw71_k32w1_executable("lock_app") {
221226
"${example_platform_dir}/button",
222227
]
223228

224-
sources += [
225-
"${example_platform_dir}/button/ButtonManager.cpp",
226-
"${example_platform_dir}/clusters/Identify.cpp",
227-
]
229+
sources += [ "${example_platform_dir}/clusters/Identify.cpp" ]
228230

229231
if (chip_enable_ota_requestor) {
230232
sources += [ "${example_platform_dir}/ota/OtaUtils.cpp" ]

examples/platform/nxp/common/app_task/source/AppTaskFreeRTOS.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <app/server/Dnssd.h>
2222
#include <lib/dnssd/Advertiser.h>
2323

24-
#include "AppMatterButton.h"
24+
#include "ButtonRegistration.h"
2525

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

100100
/* Register Matter buttons */
101-
err = AppMatterButton_registerButtons();
101+
err = chip::NXP::App::RegisterButtons();
102102
if (err != CHIP_NO_ERROR)
103103
{
104-
ChipLogError(DeviceLayer, "Error during AppMatterButton_registerButtons");
104+
ChipLogError(DeviceLayer, "Error during button registration");
105105
return err;
106106
}
107107
return err;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#pragma once
20+
21+
#include "fsl_component_button.h"
22+
23+
#include <lib/core/CHIPError.h>
24+
25+
namespace chip::NXP::App {
26+
27+
/**
28+
* @brief This class is an abstraction over an SDK button.
29+
*
30+
*/
31+
class Button
32+
{
33+
public:
34+
using Callback = button_status_t (*)(void * handle, button_callback_message_t * message, void * param);
35+
36+
virtual ~Button() = default;
37+
38+
virtual CHIP_ERROR Init() = 0;
39+
virtual void HandleShortPress() = 0;
40+
virtual void HandleLongPress() = 0;
41+
virtual void HandleDoubleClick() = 0;
42+
43+
/**
44+
* @brief This is an SDK handle for a button.
45+
*
46+
* It should be set in the Init method, based on a newly defined
47+
* handle or an already defined handle owned by the SDK.
48+
*/
49+
button_handle_t handle = nullptr;
50+
};
51+
52+
} // namespace chip::NXP::App
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#pragma once
20+
21+
#include "Button.h"
22+
23+
namespace chip::NXP::App {
24+
/**
25+
* @brief This class implements a custom app button behavior.
26+
*
27+
* | Action | Effect |
28+
* | ------------ | --------------------------------- |
29+
* | Short press | Switch a cluster attribute state |
30+
* | Long press | Schedule a soft reset taking into |
31+
* | | account Matter shutdown mechanism |
32+
* | Double click | Do nothing |
33+
*
34+
*/
35+
class ButtonApp : public Button
36+
{
37+
public:
38+
virtual CHIP_ERROR Init() override;
39+
virtual void HandleShortPress() override;
40+
virtual void HandleLongPress() override;
41+
virtual void HandleDoubleClick() override;
42+
};
43+
44+
} // namespace chip::NXP::App
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#pragma once
20+
21+
#include "ButtonWithTimer.h"
22+
23+
namespace chip::NXP::App {
24+
25+
/**
26+
* @brief This class implements a customized default button behavior.
27+
*
28+
* | Action | Effect |
29+
* | ------------ | -------------------------------------------------- |
30+
* | Short press | If a factory reset is scheduled, cancel it. |
31+
* | | Else if the device is commissioned and a factory |
32+
* | | reset is not scheduled, switch to ICD active mode. |
33+
* | | Otherwise, switch commissioning state. |
34+
* | Long press | Schedule a factory reset |
35+
* | Double click | Toggle SIT mode request through DSLS mechanism |
36+
*
37+
*/
38+
class ButtonBle : public ButtonWithTimer
39+
{
40+
public:
41+
virtual CHIP_ERROR Init() override;
42+
virtual void HandleShortPress() override;
43+
virtual void HandleLongPress() override;
44+
virtual void HandleDoubleClick() override;
45+
virtual void HandleTimerExpire() override;
46+
};
47+
48+
} // namespace chip::NXP::App

0 commit comments

Comments
 (0)