[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>
diff --git a/examples/all-clusters-app/nxp/rt/rw61x/BUILD.gn b/examples/all-clusters-app/nxp/rt/rw61x/BUILD.gn
index bdc878c..19fa822 100644
--- a/examples/all-clusters-app/nxp/rt/rw61x/BUILD.gn
+++ b/examples/all-clusters-app/nxp/rt/rw61x/BUILD.gn
@@ -170,7 +170,7 @@
"${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 = [
diff --git a/examples/contact-sensor-app/nxp/common/include/AppEvent.h b/examples/contact-sensor-app/nxp/common/include/AppEvent.h
index b292afa..562c0b4 100644
--- a/examples/contact-sensor-app/nxp/common/include/AppEvent.h
+++ b/examples/contact-sensor-app/nxp/common/include/AppEvent.h
@@ -24,4 +24,5 @@
struct AppEvent
{
EventHandler Handler;
+ void * extra;
};
diff --git a/examples/contact-sensor-app/nxp/k32w1/BUILD.gn b/examples/contact-sensor-app/nxp/k32w1/BUILD.gn
index 8e215d4..1b57e5f 100644
--- a/examples/contact-sensor-app/nxp/k32w1/BUILD.gn
+++ b/examples/contact-sensor-app/nxp/k32w1/BUILD.gn
@@ -149,6 +149,11 @@
"${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",
]
@@ -193,10 +198,7 @@
"${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" ]
diff --git a/examples/contact-sensor-app/nxp/mcxw71/BUILD.gn b/examples/contact-sensor-app/nxp/mcxw71/BUILD.gn
index cf22ec5..8bba239 100644
--- a/examples/contact-sensor-app/nxp/mcxw71/BUILD.gn
+++ b/examples/contact-sensor-app/nxp/mcxw71/BUILD.gn
@@ -148,6 +148,11 @@
"${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",
]
@@ -192,10 +197,7 @@
"${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" ]
diff --git a/examples/laundry-washer-app/nxp/rt/rw61x/BUILD.gn b/examples/laundry-washer-app/nxp/rt/rw61x/BUILD.gn
index cb0b7ba..7f22eca 100644
--- a/examples/laundry-washer-app/nxp/rt/rw61x/BUILD.gn
+++ b/examples/laundry-washer-app/nxp/rt/rw61x/BUILD.gn
@@ -171,7 +171,7 @@
"${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 = [
diff --git a/examples/lighting-app/nxp/common/include/AppEvent.h b/examples/lighting-app/nxp/common/include/AppEvent.h
index b292afa..562c0b4 100644
--- a/examples/lighting-app/nxp/common/include/AppEvent.h
+++ b/examples/lighting-app/nxp/common/include/AppEvent.h
@@ -24,4 +24,5 @@
struct AppEvent
{
EventHandler Handler;
+ void * extra;
};
diff --git a/examples/lighting-app/nxp/k32w1/BUILD.gn b/examples/lighting-app/nxp/k32w1/BUILD.gn
index 54030a7..57a1b79 100644
--- a/examples/lighting-app/nxp/k32w1/BUILD.gn
+++ b/examples/lighting-app/nxp/k32w1/BUILD.gn
@@ -156,6 +156,11 @@
"${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",
]
@@ -180,10 +185,7 @@
"${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" ]
diff --git a/examples/lighting-app/nxp/k32w1/README.md b/examples/lighting-app/nxp/k32w1/README.md
index c47449b..d61ab1e 100644
--- a/examples/lighting-app/nxp/k32w1/README.md
+++ b/examples/lighting-app/nxp/k32w1/README.md
@@ -220,8 +220,8 @@
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 |
| ----- | --------------------------------------------- |
diff --git a/examples/lighting-app/nxp/mcxw71/BUILD.gn b/examples/lighting-app/nxp/mcxw71/BUILD.gn
index efd3af6..f7e9c42 100644
--- a/examples/lighting-app/nxp/mcxw71/BUILD.gn
+++ b/examples/lighting-app/nxp/mcxw71/BUILD.gn
@@ -156,6 +156,11 @@
"${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",
]
@@ -183,10 +188,7 @@
"${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" ]
diff --git a/examples/lock-app/nxp/common/main/include/AppEvent.h b/examples/lock-app/nxp/common/main/include/AppEvent.h
index 065078a..06522ef 100644
--- a/examples/lock-app/nxp/common/main/include/AppEvent.h
+++ b/examples/lock-app/nxp/common/main/include/AppEvent.h
@@ -48,4 +48,5 @@
};
EventHandler Handler;
+ void * extra;
};
diff --git a/examples/lock-app/nxp/k32w1/BUILD.gn b/examples/lock-app/nxp/k32w1/BUILD.gn
index 602398e..c10cc05 100644
--- a/examples/lock-app/nxp/k32w1/BUILD.gn
+++ b/examples/lock-app/nxp/k32w1/BUILD.gn
@@ -154,6 +154,11 @@
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",
@@ -221,10 +226,7 @@
"${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" ]
diff --git a/examples/lock-app/nxp/mcxw71/BUILD.gn b/examples/lock-app/nxp/mcxw71/BUILD.gn
index 6d105f1..0c34d3d 100644
--- a/examples/lock-app/nxp/mcxw71/BUILD.gn
+++ b/examples/lock-app/nxp/mcxw71/BUILD.gn
@@ -154,6 +154,11 @@
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",
@@ -221,10 +226,7 @@
"${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" ]
diff --git a/examples/platform/nxp/common/app_task/source/AppTaskFreeRTOS.cpp b/examples/platform/nxp/common/app_task/source/AppTaskFreeRTOS.cpp
index 5b8e3ea..ec394b4 100644
--- a/examples/platform/nxp/common/app_task/source/AppTaskFreeRTOS.cpp
+++ b/examples/platform/nxp/common/app_task/source/AppTaskFreeRTOS.cpp
@@ -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>
@@ -98,10 +98,10 @@
#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;
diff --git a/examples/platform/nxp/common/matter_button/include/AppMatterButton.h b/examples/platform/nxp/common/matter_button/include/AppMatterButton.h
deleted file mode 100644
index dd00a5c..0000000
--- a/examples/platform/nxp/common/matter_button/include/AppMatterButton.h
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- *
- * Copyright (c) 2022 Project CHIP Authors
- * Copyright 2023-2024 NXP
- * 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.
- */
-
-#ifndef _MATTER_BUTTON_H_
-#define _MATTER_BUTTON_H_
-
-#include <lib/core/CHIPError.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-namespace chip {
-namespace NXP {
-namespace App {
-/**
- * API allowing to register matter buttons
- */
-CHIP_ERROR AppMatterButton_registerButtons(void);
-
-} // namespace App
-} // namespace NXP
-} // namespace chip
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* _MATTER_BUTTON_H_ */
diff --git a/examples/platform/nxp/common/matter_button/include/Button.h b/examples/platform/nxp/common/matter_button/include/Button.h
new file mode 100644
index 0000000..7485cf9
--- /dev/null
+++ b/examples/platform/nxp/common/matter_button/include/Button.h
@@ -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
diff --git a/examples/platform/nxp/common/matter_button/include/ButtonApp.h b/examples/platform/nxp/common/matter_button/include/ButtonApp.h
new file mode 100644
index 0000000..3ace28f
--- /dev/null
+++ b/examples/platform/nxp/common/matter_button/include/ButtonApp.h
@@ -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
diff --git a/examples/platform/nxp/common/matter_button/include/ButtonBle.h b/examples/platform/nxp/common/matter_button/include/ButtonBle.h
new file mode 100644
index 0000000..75b35f2
--- /dev/null
+++ b/examples/platform/nxp/common/matter_button/include/ButtonBle.h
@@ -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
diff --git a/examples/platform/nxp/common/matter_button/include/ButtonDefault.h b/examples/platform/nxp/common/matter_button/include/ButtonDefault.h
new file mode 100644
index 0000000..4241f04
--- /dev/null
+++ b/examples/platform/nxp/common/matter_button/include/ButtonDefault.h
@@ -0,0 +1,43 @@
+/*
+ *
+ * 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 default button behavior.
+ *
+ * | Action | Effect |
+ * | ------------ | -------------------------- |
+ * | Short press | Switch commissioning state |
+ * | Long press | Schedule a factory reset |
+ * | Double click | Do nothing |
+ *
+ */
+class ButtonDefault : public Button
+{
+public:
+ virtual CHIP_ERROR Init() override;
+ virtual void HandleShortPress() override;
+ virtual void HandleLongPress() override;
+ virtual void HandleDoubleClick() override;
+};
+
+} // namespace chip::NXP::App
diff --git a/examples/platform/nxp/common/matter_button/include/ButtonManager.h b/examples/platform/nxp/common/matter_button/include/ButtonManager.h
new file mode 100644
index 0000000..44ad191
--- /dev/null
+++ b/examples/platform/nxp/common/matter_button/include/ButtonManager.h
@@ -0,0 +1,58 @@
+/*
+ *
+ * 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"
+
+#include <lib/core/CHIPError.h>
+
+namespace chip::NXP::App {
+
+/**
+ * @brief This class describes a manager over Button operations.
+ *
+ */
+class ButtonManager
+{
+public:
+ static ButtonManager sInstance;
+
+ CHIP_ERROR Init();
+
+ /**
+ * @brief API for registering a Button object.
+ *
+ */
+ CHIP_ERROR RegisterButton(Button & button);
+
+ /**
+ * @brief Generic callback for all registered buttons.
+ *
+ * Event handling is delegated to Button objects.
+ *
+ */
+ static button_status_t HandleCallbacks(void * buttonHandle, button_callback_message_t * message, void * callbackParam);
+};
+
+inline ButtonManager & ButtonMgr()
+{
+ return ButtonManager::sInstance;
+}
+
+} // namespace chip::NXP::App
diff --git a/examples/platform/nxp/common/matter_button/source/AppMatterButtonEmpty.cpp b/examples/platform/nxp/common/matter_button/include/ButtonRegistration.h
similarity index 65%
copy from examples/platform/nxp/common/matter_button/source/AppMatterButtonEmpty.cpp
copy to examples/platform/nxp/common/matter_button/include/ButtonRegistration.h
index c9f602c..c47eeba 100644
--- a/examples/platform/nxp/common/matter_button/source/AppMatterButtonEmpty.cpp
+++ b/examples/platform/nxp/common/matter_button/include/ButtonRegistration.h
@@ -1,7 +1,6 @@
/*
*
- * Copyright (c) 2022 Project CHIP Authors
- * Copyright 2023-2024 NXP
+ * Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,10 +16,17 @@
* limitations under the License.
*/
-#include "AppMatterButton.h"
+#pragma once
-CHIP_ERROR chip::NXP::App::AppMatterButton_registerButtons(void)
-{
- /* Empty content could be re-defined in a dedicated platform AppMatterButton_registerButtons function */
- return CHIP_NO_ERROR;
-}
+#include <lib/core/CHIPError.h>
+
+namespace chip::NXP::App {
+
+/**
+ * @brief API used by app task to register buttons.
+ *
+ * The default implementation will register a single ButtonDefault instance.
+ */
+CHIP_ERROR RegisterButtons();
+
+} // namespace chip::NXP::App
diff --git a/examples/platform/nxp/common/matter_button/include/ButtonWithTimer.h b/examples/platform/nxp/common/matter_button/include/ButtonWithTimer.h
new file mode 100644
index 0000000..6620bae
--- /dev/null
+++ b/examples/platform/nxp/common/matter_button/include/ButtonWithTimer.h
@@ -0,0 +1,50 @@
+/*
+ *
+ * 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"
+#include "FreeRTOS.h"
+#include "timers.h"
+
+namespace chip::NXP::App {
+/**
+ * @brief This class implements a default button with timer behavior.
+ *
+ * This class provides an API for starting/cancelling the timer. It can
+ * be used by derived classes to define their own timer handler behavior.
+ */
+class ButtonWithTimer : public Button
+{
+public:
+ ButtonWithTimer();
+
+ virtual CHIP_ERROR Init() override;
+ virtual void HandleShortPress() override;
+ virtual void HandleLongPress() override;
+ virtual void HandleDoubleClick() override;
+
+ virtual void HandleTimerExpire();
+
+ void CancelTimer();
+ void StartTimer(uint32_t aTimeoutInMs);
+
+ TimerHandle_t timer;
+};
+
+} // namespace chip::NXP::App
diff --git a/examples/platform/nxp/common/matter_button/source/AppMatterButton.cpp b/examples/platform/nxp/common/matter_button/source/AppMatterButton.cpp
deleted file mode 100644
index be5adc9..0000000
--- a/examples/platform/nxp/common/matter_button/source/AppMatterButton.cpp
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- *
- * Copyright (c) 2022 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.
- */
-
-#include "AppMatterButton.h"
-#include "AppTaskBase.h"
-#include "board_comp.h"
-#include "fsl_component_timer_manager.h"
-#include "fwk_platform.h"
-#include <platform/CHIPDeviceLayer.h>
-
-static BUTTON_HANDLE_DEFINE(sdkButtonHandle);
-
-static button_status_t AppMatterButton_ButtonCallback(void * buttonHandle, button_callback_message_t * message,
- void * callbackParam)
-{
- switch (message->event)
- {
- case kBUTTON_EventShortPress:
- case kBUTTON_EventOneClick:
- chip::NXP::App::GetAppTask().SwitchCommissioningStateHandler();
- break;
- case kBUTTON_EventLongPress:
- chip::NXP::App::GetAppTask().FactoryResetHandler();
- break;
- default:
- break;
- }
- return kStatus_BUTTON_Success;
-}
-
-CHIP_ERROR chip::NXP::App::AppMatterButton_registerButtons(void)
-{
- button_config_t buttonConfig;
- button_status_t bStatus;
- CHIP_ERROR err = CHIP_NO_ERROR;
-
- do
- {
- /* Init the Platform Timer Manager */
- if (PLATFORM_InitTimerManager() != 0)
- {
- err = CHIP_ERROR_UNEXPECTED_EVENT;
- ChipLogError(DeviceLayer, "tmr init error");
- break;
- }
-
- /* Init board buttons */
- bStatus = BOARD_InitButton((button_handle_t) sdkButtonHandle);
- if (bStatus != kStatus_BUTTON_Success)
- {
- err = CHIP_ERROR_UNEXPECTED_EVENT;
- ChipLogError(DeviceLayer, "button init error");
- break;
- }
- bStatus = BUTTON_InstallCallback((button_handle_t) sdkButtonHandle, AppMatterButton_ButtonCallback, NULL);
-
- if (bStatus != kStatus_BUTTON_Success)
- {
- err = CHIP_ERROR_UNEXPECTED_EVENT;
- ChipLogError(DeviceLayer, "button init error");
- break;
- }
- } while (0);
-
- return err;
-}
diff --git a/examples/platform/nxp/common/matter_button/source/ButtonApp.cpp b/examples/platform/nxp/common/matter_button/source/ButtonApp.cpp
new file mode 100644
index 0000000..3b03b41
--- /dev/null
+++ b/examples/platform/nxp/common/matter_button/source/ButtonApp.cpp
@@ -0,0 +1,57 @@
+/*
+ *
+ * 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.
+ */
+
+#include "ButtonApp.h"
+#include "AppTaskBase.h"
+
+extern "C" {
+#include "app.h"
+#include "board_comp.h"
+}
+
+CHIP_ERROR chip::NXP::App::ButtonApp::Init()
+{
+ // Button is initialized in otSysInit, when APP_InitServices is called.
+ // Overwrite the handle to reference the SDK handle.
+ handle = &g_buttonHandle[1];
+
+ return CHIP_NO_ERROR;
+}
+
+void chip::NXP::App::ButtonApp::HandleShortPress()
+{
+ chip::DeviceLayer::PlatformMgr().ScheduleWork(
+ [](intptr_t arg) {
+ auto status = chip::NXP::App::GetAppTask().ProcessSetStateClusterHandler();
+ if (status != CHIP_NO_ERROR)
+ {
+ ChipLogProgress(DeviceLayer, "Error when updating cluster attribute");
+ }
+ },
+ (intptr_t) nullptr);
+}
+
+void chip::NXP::App::ButtonApp::HandleLongPress()
+{
+ chip::DeviceLayer::PlatformMgrImpl().CleanReset();
+}
+
+void chip::NXP::App::ButtonApp::HandleDoubleClick()
+{
+ /* Currently not mapped to any action */
+}
diff --git a/examples/platform/nxp/common/matter_button/source/ButtonBle.cpp b/examples/platform/nxp/common/matter_button/source/ButtonBle.cpp
new file mode 100644
index 0000000..447b0b0
--- /dev/null
+++ b/examples/platform/nxp/common/matter_button/source/ButtonBle.cpp
@@ -0,0 +1,142 @@
+/*
+ *
+ * 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.
+ */
+
+#include "ButtonBle.h"
+#include "AppTaskBase.h"
+#include "BLEApplicationManager.h"
+#include "UserInterfaceFeedback.h"
+
+extern "C" {
+#include "app.h"
+#include "board_comp.h"
+}
+
+#include <app/icd/server/ICDServerConfig.h>
+
+/**
+ * @brief Timeout (ms) for factory data reset action.
+ *
+ * During this timeout, the factory reset action can be cancelled by pressing a button.
+ */
+#ifndef BLE_BUTTON_FACTORY_RESET_TIMEOUT_MS
+#define BLE_BUTTON_FACTORY_RESET_TIMEOUT_MS 6000
+#endif
+
+static void BleHandleLongPress(const AppEvent & event)
+{
+ auto & button = *static_cast<chip::NXP::App::ButtonBle *>(event.extra);
+
+ if (xTimerIsTimerActive(button.timer))
+ {
+ button.CancelTimer();
+#if CONFIG_ENABLE_FEEDBACK
+ FeedbackMgr().RestoreState();
+#endif
+ ChipLogProgress(DeviceLayer, "Factory Reset was cancelled!");
+ }
+ else
+ {
+ uint32_t resetTimeout = BLE_BUTTON_FACTORY_RESET_TIMEOUT_MS;
+ ChipLogProgress(DeviceLayer, "Factory Reset Triggered. Push the RESET button within %lu ms to cancel!", resetTimeout);
+
+#if CONFIG_ENABLE_FEEDBACK
+ FeedbackMgr().DisplayOnAction(UserInterfaceFeedback::Action::kFactoryReset);
+#endif
+ button.StartTimer(resetTimeout);
+ }
+}
+
+static void BleHandleShortPress(const AppEvent & event)
+{
+ auto & button = *static_cast<chip::NXP::App::ButtonBle *>(event.extra);
+ if (xTimerIsTimerActive(button.timer))
+ {
+ // If a factory reset is scheduled, pressing the BLE button will cancel it.
+ BleHandleLongPress(event);
+ return;
+ }
+
+#if CHIP_CONFIG_ENABLE_ICD_LIT
+ if (chip::DeviceLayer::ConfigurationMgr().IsFullyProvisioned())
+ {
+ // If the device is commissioned and a factory reset is not scheduled, switch to active mode.
+ chip::DeviceLayer::PlatformMgr().ScheduleWork(
+ [](intptr_t arg) { chip::app::ICDNotifier::GetInstance().NotifyNetworkActivityNotification(); }, 0);
+ return;
+ }
+#endif
+
+ chip::NXP::App::GetAppTask().SwitchCommissioningStateHandler();
+ chip::NXP::App::BleAppMgr().EnableMultipleConnectionsHandler();
+}
+
+CHIP_ERROR chip::NXP::App::ButtonBle::Init()
+{
+ // Button is initialized in otSysInit, when APP_InitServices is called.
+ // Overwrite the handle to reference the SDK handle.
+ handle = &g_buttonHandle[0];
+
+ return CHIP_NO_ERROR;
+}
+
+void chip::NXP::App::ButtonBle::HandleShortPress()
+{
+ AppEvent event;
+ event.Handler = BleHandleShortPress;
+ event.extra = this;
+ chip::NXP::App::GetAppTask().PostEvent(event);
+}
+
+void chip::NXP::App::ButtonBle::HandleLongPress()
+{
+ AppEvent event;
+ event.Handler = BleHandleLongPress;
+ event.extra = this;
+ chip::NXP::App::GetAppTask().PostEvent(event);
+}
+
+void chip::NXP::App::ButtonBle::HandleDoubleClick()
+{
+#if (CHIP_CONFIG_ENABLE_ICD_LIT && CHIP_CONFIG_ENABLE_ICD_DSLS)
+ static bool sitModeRequested = false;
+
+ if (chip::DeviceLayer::ConfigurationMgr().IsFullyProvisioned())
+ {
+ if (!sitModeRequested)
+ {
+ chip::DeviceLayer::PlatformMgr().ScheduleWork(
+ [](intptr_t arg) { chip::app::ICDNotifier::GetInstance().NotifySITModeRequestNotification(); }, 0);
+ sitModeRequested = true;
+ }
+ else
+ {
+ chip::DeviceLayer::PlatformMgr().ScheduleWork(
+ [](intptr_t arg) { chip::app::ICDNotifier::GetInstance().NotifySITModeRequestWithdrawal(); }, 0);
+ sitModeRequested = false;
+ }
+ }
+#endif
+}
+
+void chip::NXP::App::ButtonBle::HandleTimerExpire()
+{
+ ChipLogProgress(DeviceLayer, "Device will factory reset...");
+
+ // Actually trigger Factory Reset
+ chip::Server::GetInstance().ScheduleFactoryReset();
+}
diff --git a/examples/platform/nxp/common/matter_button/source/ButtonDefault.cpp b/examples/platform/nxp/common/matter_button/source/ButtonDefault.cpp
new file mode 100644
index 0000000..526bcd3
--- /dev/null
+++ b/examples/platform/nxp/common/matter_button/source/ButtonDefault.cpp
@@ -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.
+ */
+
+#include "ButtonDefault.h"
+#include "AppTaskBase.h"
+#include "board_comp.h"
+
+BUTTON_HANDLE_DEFINE(sHandle);
+
+CHIP_ERROR chip::NXP::App::ButtonDefault::Init()
+{
+ handle = sHandle;
+
+ auto status = BOARD_InitButton(handle);
+ VerifyOrReturnError(status == kStatus_BUTTON_Success, CHIP_ERROR_UNEXPECTED_EVENT);
+
+ return CHIP_NO_ERROR;
+}
+
+void chip::NXP::App::ButtonDefault::HandleShortPress()
+{
+ chip::NXP::App::GetAppTask().SwitchCommissioningStateHandler();
+}
+
+void chip::NXP::App::ButtonDefault::HandleLongPress()
+{
+ chip::NXP::App::GetAppTask().FactoryResetHandler();
+}
+
+void chip::NXP::App::ButtonDefault::HandleDoubleClick()
+{
+ /* Currently not mapped to any action */
+}
diff --git a/examples/platform/nxp/common/matter_button/source/ButtonManager.cpp b/examples/platform/nxp/common/matter_button/source/ButtonManager.cpp
new file mode 100644
index 0000000..0463373
--- /dev/null
+++ b/examples/platform/nxp/common/matter_button/source/ButtonManager.cpp
@@ -0,0 +1,67 @@
+/*
+ *
+ * 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.
+ */
+
+#include "ButtonManager.h"
+#include "fwk_platform.h"
+
+#include <lib/core/CHIPError.h>
+#include <lib/support/CodeUtils.h>
+
+chip::NXP::App::ButtonManager chip::NXP::App::ButtonManager::sInstance;
+
+CHIP_ERROR chip::NXP::App::ButtonManager::Init()
+{
+ VerifyOrReturnError(PLATFORM_InitTimerManager() >= 0, CHIP_ERROR_UNEXPECTED_EVENT);
+
+ return CHIP_NO_ERROR;
+}
+
+CHIP_ERROR chip::NXP::App::ButtonManager::RegisterButton(Button & button)
+{
+ ReturnErrorOnFailure(button.Init());
+ VerifyOrReturnError(button.handle != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
+
+ auto status = BUTTON_InstallCallback(button.handle, ButtonManager::HandleCallbacks, &button);
+ VerifyOrReturnError(status == kStatus_BUTTON_Success, CHIP_ERROR_UNEXPECTED_EVENT);
+
+ return CHIP_NO_ERROR;
+}
+
+button_status_t chip::NXP::App::ButtonManager::HandleCallbacks(void * buttonHandle, button_callback_message_t * message,
+ void * callbackParam)
+{
+ auto * button = static_cast<Button *>(callbackParam);
+
+ switch (message->event)
+ {
+ case kBUTTON_EventOneClick:
+ case kBUTTON_EventShortPress:
+ button->HandleShortPress();
+ break;
+ case kBUTTON_EventLongPress:
+ button->HandleLongPress();
+ break;
+ case kBUTTON_EventDoubleClick:
+ button->HandleDoubleClick();
+ break;
+ default:
+ break;
+ }
+
+ return kStatus_BUTTON_Success;
+}
diff --git a/examples/platform/nxp/common/matter_button/source/ButtonRegistrationAppAndBle.cpp b/examples/platform/nxp/common/matter_button/source/ButtonRegistrationAppAndBle.cpp
new file mode 100644
index 0000000..45037b2
--- /dev/null
+++ b/examples/platform/nxp/common/matter_button/source/ButtonRegistrationAppAndBle.cpp
@@ -0,0 +1,37 @@
+/*
+ *
+ * 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.
+ */
+
+#include "ButtonManager.h"
+#include "ButtonRegistration.h"
+
+#include "ButtonApp.h"
+#include "ButtonBle.h"
+
+#include <lib/support/CodeUtils.h>
+
+static chip::NXP::App::ButtonApp sAppButton;
+static chip::NXP::App::ButtonBle sBleButton;
+
+CHIP_ERROR chip::NXP::App::RegisterButtons(void)
+{
+ ReturnErrorOnFailure(ButtonMgr().Init());
+ ReturnErrorOnFailure(ButtonMgr().RegisterButton(sBleButton));
+ ReturnErrorOnFailure(ButtonMgr().RegisterButton(sAppButton));
+
+ return CHIP_NO_ERROR;
+}
diff --git a/examples/platform/nxp/common/matter_button/source/AppMatterButtonEmpty.cpp b/examples/platform/nxp/common/matter_button/source/ButtonRegistrationDefault.cpp
similarity index 63%
copy from examples/platform/nxp/common/matter_button/source/AppMatterButtonEmpty.cpp
copy to examples/platform/nxp/common/matter_button/source/ButtonRegistrationDefault.cpp
index c9f602c..3b1bf18 100644
--- a/examples/platform/nxp/common/matter_button/source/AppMatterButtonEmpty.cpp
+++ b/examples/platform/nxp/common/matter_button/source/ButtonRegistrationDefault.cpp
@@ -1,7 +1,6 @@
/*
*
- * Copyright (c) 2022 Project CHIP Authors
- * Copyright 2023-2024 NXP
+ * Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,10 +16,18 @@
* limitations under the License.
*/
-#include "AppMatterButton.h"
+#include "ButtonDefault.h"
+#include "ButtonManager.h"
+#include "ButtonRegistration.h"
-CHIP_ERROR chip::NXP::App::AppMatterButton_registerButtons(void)
+#include <lib/support/CodeUtils.h>
+
+static chip::NXP::App::ButtonDefault sButton;
+
+CHIP_ERROR chip::NXP::App::RegisterButtons()
{
- /* Empty content could be re-defined in a dedicated platform AppMatterButton_registerButtons function */
+ ReturnErrorOnFailure(ButtonMgr().Init());
+ ReturnErrorOnFailure(ButtonMgr().RegisterButton(sButton));
+
return CHIP_NO_ERROR;
}
diff --git a/examples/platform/nxp/common/matter_button/source/AppMatterButtonEmpty.cpp b/examples/platform/nxp/common/matter_button/source/ButtonRegistrationEmpty.cpp
similarity index 76%
rename from examples/platform/nxp/common/matter_button/source/AppMatterButtonEmpty.cpp
rename to examples/platform/nxp/common/matter_button/source/ButtonRegistrationEmpty.cpp
index c9f602c..a8a8c7b 100644
--- a/examples/platform/nxp/common/matter_button/source/AppMatterButtonEmpty.cpp
+++ b/examples/platform/nxp/common/matter_button/source/ButtonRegistrationEmpty.cpp
@@ -1,7 +1,6 @@
/*
*
- * Copyright (c) 2022 Project CHIP Authors
- * Copyright 2023-2024 NXP
+ * Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,10 +16,12 @@
* limitations under the License.
*/
-#include "AppMatterButton.h"
+#include "ButtonRegistration.h"
-CHIP_ERROR chip::NXP::App::AppMatterButton_registerButtons(void)
+#include <lib/support/CodeUtils.h>
+
+CHIP_ERROR chip::NXP::App::RegisterButtons()
{
- /* Empty content could be re-defined in a dedicated platform AppMatterButton_registerButtons function */
+ /* Empty content could be re-defined in a dedicated platform RegisterButtons function */
return CHIP_NO_ERROR;
}
diff --git a/examples/platform/nxp/common/matter_button/source/ButtonWithTimer.cpp b/examples/platform/nxp/common/matter_button/source/ButtonWithTimer.cpp
new file mode 100644
index 0000000..c430132
--- /dev/null
+++ b/examples/platform/nxp/common/matter_button/source/ButtonWithTimer.cpp
@@ -0,0 +1,86 @@
+/*
+ *
+ * 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.
+ */
+
+#include "ButtonWithTimer.h"
+#include "AppTaskBase.h"
+
+#include <lib/support/CodeUtils.h>
+#include <platform/CHIPDeviceLayer.h>
+
+chip::NXP::App::ButtonWithTimer::ButtonWithTimer()
+{
+ // Pass the instance pointer as timer id pointer. The pointer can then
+ // be retrieved inside the callback to delegate the action to the button
+ // instance, through the HandleTimerExpire function.
+ timer = xTimerCreate("FnTmr", 1, false, (void *) this, [](TimerHandle_t xTimer) {
+ ButtonWithTimer * button = static_cast<ButtonWithTimer *>(pvTimerGetTimerID(xTimer));
+ button->HandleTimerExpire();
+ });
+ VerifyOrDie(timer != nullptr);
+}
+
+CHIP_ERROR chip::NXP::App::ButtonWithTimer::Init()
+{
+ /* Currently empty */
+ return CHIP_NO_ERROR;
+}
+
+void chip::NXP::App::ButtonWithTimer::HandleShortPress()
+{
+ /* Currently not mapped to any action */
+}
+
+void chip::NXP::App::ButtonWithTimer::HandleLongPress()
+{
+ /* Currently not mapped to any action */
+}
+
+void chip::NXP::App::ButtonWithTimer::HandleDoubleClick()
+{
+ /* Currently not mapped to any action */
+}
+
+void chip::NXP::App::ButtonWithTimer::HandleTimerExpire()
+{
+ /* Currently empty */
+}
+
+void chip::NXP::App::ButtonWithTimer::CancelTimer()
+{
+ if (xTimerStop(timer, 0) == pdFAIL)
+ {
+ ChipLogProgress(DeviceLayer, "app timer stop() failed");
+ }
+}
+
+void chip::NXP::App::ButtonWithTimer::StartTimer(uint32_t aTimeoutInMs)
+{
+ if (xTimerIsTimerActive(timer))
+ {
+ ChipLogProgress(DeviceLayer, "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(timer, aTimeoutInMs / portTICK_PERIOD_MS, 100) != pdPASS)
+ {
+ ChipLogProgress(DeviceLayer, "app timer start() failed");
+ }
+}
diff --git a/examples/platform/nxp/mcxw71_k32w1/button/ButtonManager.cpp b/examples/platform/nxp/mcxw71_k32w1/button/ButtonManager.cpp
deleted file mode 100644
index c94e6e4..0000000
--- a/examples/platform/nxp/mcxw71_k32w1/button/ButtonManager.cpp
+++ /dev/null
@@ -1,247 +0,0 @@
-/*
- *
- * 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.
- */
-
-#include "ButtonManager.h"
-#include "AppConfig.h"
-#include "AppMatterButton.h"
-#include "AppTask.h"
-#include "BLEApplicationManager.h"
-#include "UserInterfaceFeedback.h"
-
-#include <app-common/zap-generated/attributes/Accessors.h>
-
-extern "C" {
-#include "app.h"
-#include "board_comp.h"
-}
-
-CHIP_ERROR chip::NXP::App::AppMatterButton_registerButtons(void)
-{
- /* Board buttons are initialized in otSysInit, when APP_InitServices is called. */
- button_status_t bStatus;
-
- bStatus = BUTTON_InstallCallback((button_handle_t) g_buttonHandle[0], ButtonManager::BleCallback, NULL);
- VerifyOrReturnError(bStatus == kStatus_BUTTON_Success, CHIP_ERROR_UNEXPECTED_EVENT,
- ChipLogError(DeviceLayer, "button init error"));
-
- bStatus = BUTTON_InstallCallback((button_handle_t) g_buttonHandle[1], ButtonManager::AppActionCallback, NULL);
- VerifyOrReturnError(bStatus == kStatus_BUTTON_Success, CHIP_ERROR_UNEXPECTED_EVENT,
- ChipLogError(DeviceLayer, "button init error"));
-
- ReturnErrorOnFailure(ButtonMgr().Init());
-
- return CHIP_NO_ERROR;
-}
-
-ButtonManager ButtonManager::sInstance;
-
-TimerHandle_t resetTimer;
-
-CHIP_ERROR ButtonManager::Init()
-{
- resetTimer = xTimerCreate("FnTmr", 1, false, (void *) this, [](TimerHandle_t xTimer) {
- AppEvent event;
- event.Handler = FunctionTimerEventHandler;
- chip::NXP::App::GetAppTask().PostEvent(event);
- });
- VerifyOrReturnError(resetTimer != NULL, APP_ERROR_CREATE_TIMER_FAILED);
-
- return CHIP_NO_ERROR;
-}
-
-button_status_t ButtonManager::BleCallback(void * handle, button_callback_message_t * message, void * param)
-{
- AppEvent event;
-
- switch (message->event)
- {
- case kBUTTON_EventOneClick:
- case kBUTTON_EventShortPress:
- event.Handler = ButtonManager::BleHandler;
- break;
- case kBUTTON_EventLongPress:
- event.Handler = ButtonManager::ResetActionEventHandler;
- break;
-
-#if (CHIP_CONFIG_ENABLE_ICD_LIT && CHIP_CONFIG_ENABLE_ICD_DSLS)
- case kBUTTON_EventDoubleClick:
- event.Handler = ButtonManager::DSLSActionEventHandler;
- break;
-#endif
-
- default:
- /* No action required */
- break;
- }
-
- chip::NXP::App::GetAppTask().PostEvent(event);
-
- return kStatus_BUTTON_Success;
-}
-
-button_status_t ButtonManager::AppActionCallback(void * handle, button_callback_message_t * message, void * param)
-{
- AppEvent event;
-
- switch (message->event)
- {
- case kBUTTON_EventOneClick:
- case kBUTTON_EventShortPress:
- event.Handler = ButtonManager::AppActionEventHandler;
- break;
- case kBUTTON_EventLongPress:
- // Soft reset ensures that platform manager shutdown procedure is called.
- event.Handler = ButtonManager::SoftResetHandler;
- break;
- default:
- /* No action required */
- break;
- }
-
- chip::NXP::App::GetAppTask().PostEvent(event);
-
- return kStatus_BUTTON_Success;
-}
-
-void ButtonManager::FunctionTimerEventHandler(const AppEvent & event)
-{
- ChipLogProgress(DeviceLayer, "Device will factory reset...");
-
- // Actually trigger Factory Reset
- chip::Server::GetInstance().ScheduleFactoryReset();
-}
-
-void ButtonManager::ResetActionEventHandler(const AppEvent & event)
-{
- if (xTimerIsTimerActive(resetTimer))
- {
- ButtonMgr().CancelTimer();
-#if CONFIG_ENABLE_FEEDBACK
- FeedbackMgr().RestoreState();
-#endif
- ChipLogProgress(DeviceLayer, "Factory Reset was cancelled!");
- }
- else
- {
- uint32_t resetTimeout = BUTTON_MANAGER_FACTORY_RESET_TIMEOUT_MS;
- ChipLogProgress(DeviceLayer, "Factory Reset Triggered. Push the RESET button within %lu ms to cancel!", resetTimeout);
-
-#if CONFIG_ENABLE_FEEDBACK
- FeedbackMgr().DisplayOnAction(UserInterfaceFeedback::Action::kFactoryReset);
-#endif
- ButtonMgr().StartTimer(resetTimeout);
- }
-}
-
-void ButtonManager::AppActionEventHandler(const AppEvent & event)
-{
- chip::DeviceLayer::PlatformMgr().ScheduleWork(
- [](intptr_t arg) {
- auto status = chip::NXP::App::GetAppTask().ProcessSetStateClusterHandler();
- if (status != CHIP_NO_ERROR)
- {
- ChipLogProgress(DeviceLayer, "Error when updating cluster attribute");
- }
- },
- (intptr_t) nullptr);
-}
-
-void ButtonManager::SoftResetHandler(const AppEvent & event)
-{
- chip::DeviceLayer::PlatformMgrImpl().CleanReset();
-}
-
-#if CHIP_CONFIG_ENABLE_ICD_LIT
-static void UserActiveModeHandler(const AppEvent & event)
-{
- chip::DeviceLayer::PlatformMgr().ScheduleWork(
- [](intptr_t arg) { chip::app::ICDNotifier::GetInstance().NotifyNetworkActivityNotification(); }, 0);
-}
-#endif
-
-void ButtonManager::BleHandler(const AppEvent & event)
-{
- if (xTimerIsTimerActive(resetTimer))
- {
- // If a factory reset is scheduled, pressing the BLE button will cancel it.
- ResetActionEventHandler(event);
- return;
- }
-
-#if CHIP_CONFIG_ENABLE_ICD_LIT
- if (chip::DeviceLayer::ConfigurationMgr().IsFullyProvisioned())
- {
- // If the device is commissioned and a factory reset is not scheduled, switch to active mode.
- UserActiveModeHandler(event);
- return;
- }
-#endif
-
- chip::NXP::App::GetAppTask().SwitchCommissioningStateHandler();
-
- chip::NXP::App::BleAppMgr().EnableMultipleConnectionsHandler();
-}
-
-#if (CHIP_CONFIG_ENABLE_ICD_LIT && CHIP_CONFIG_ENABLE_ICD_DSLS)
-void ButtonManager::DSLSActionEventHandler(const AppEvent & event)
-{
-
- static bool sitModeRequested = false;
-
- if (chip::DeviceLayer::ConfigurationMgr().IsFullyProvisioned())
- {
- if (!sitModeRequested)
- {
- chip::DeviceLayer::PlatformMgr().ScheduleWork(
- [](intptr_t arg) { chip::app::ICDNotifier::GetInstance().NotifySITModeRequestNotification(); }, 0);
- sitModeRequested = true;
- }
- else
- {
- chip::DeviceLayer::PlatformMgr().ScheduleWork(
- [](intptr_t arg) { chip::app::ICDNotifier::GetInstance().NotifySITModeRequestWithdrawal(); }, 0);
- sitModeRequested = false;
- }
- }
-}
-#endif // CHIP_CONFIG_ENABLE_ICD_LIT && CHIP_CONFIG_ENABLE_ICD_DSLS
-
-void ButtonManager::CancelTimer()
-{
- if (xTimerStop(resetTimer, 0) == pdFAIL)
- {
- ChipLogProgress(DeviceLayer, "app timer stop() failed");
- }
-}
-
-void ButtonManager::StartTimer(uint32_t aTimeoutInMs)
-{
- if (xTimerIsTimerActive(resetTimer))
- {
- ChipLogProgress(DeviceLayer, "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(resetTimer, aTimeoutInMs / portTICK_PERIOD_MS, 100) != pdPASS)
- {
- ChipLogProgress(DeviceLayer, "app timer start() failed");
- }
-}
diff --git a/examples/platform/nxp/mcxw71_k32w1/button/ButtonManager.h b/examples/platform/nxp/mcxw71_k32w1/button/ButtonManager.h
deleted file mode 100644
index 4ed6553..0000000
--- a/examples/platform/nxp/mcxw71_k32w1/button/ButtonManager.h
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- *
- * 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 "AppEvent.h"
-
-#include "FreeRTOS.h"
-#include "timers.h"
-
-#include "fsl_component_button.h"
-
-#include <app/icd/server/ICDServerConfig.h>
-#include <lib/core/CHIPError.h>
-
-// Application-defined error codes in the CHIP_ERROR space.
-#define APP_ERROR_CREATE_TIMER_FAILED CHIP_APPLICATION_ERROR(0x01)
-
-/**
- * @brief Timeout (ms) for factory data reset action.
- *
- * During this timeout, the factory reset action can be cancelled by pressing a button.
- */
-#ifndef BUTTON_MANAGER_FACTORY_RESET_TIMEOUT_MS
-#define BUTTON_MANAGER_FACTORY_RESET_TIMEOUT_MS 6000
-#endif
-
-/**
- * @brief This class describes a manager for button callbacks.
- *
- */
-class ButtonManager
-{
-public:
- CHIP_ERROR Init();
-
- // These are the callbacks registered with the buttons. They will delegate actions
- // to other methods based on the button event: short press, long press etc.
- static button_status_t BleCallback(void * handle, button_callback_message_t * message, void * param);
- static button_status_t AppActionCallback(void * handle, button_callback_message_t * message, void * param);
-
-private:
- /**
- * @brief This callback performs a soft reset.
- *
- * This can be used when the user wants to clean reset the device,
- * meaning that Matter is properly shutdown, unlike a RESET button
- * press, where the device resets without calling the shutdown procedure.
- */
- static void SoftResetHandler(const AppEvent & event);
-
- /**
- * @brief This callback toggles between BLE start/stop advertising.
- *
- * It is used during commissioning to ensure a user-intent commissioning flow.
- */
- static void BleHandler(const AppEvent & event);
-
- /**
- * @brief This callback updates the application state.
- *
- * An example of application state would be the lighting manager light LED state.
- * This handler will toggle the light LED state.
- */
- static void AppActionEventHandler(const AppEvent & event);
-
- /**
- * @brief This callback schedules a factory reset.
- *
- * The factory reset is scheduled based on the value of BUTTON_MANAGER_FACTORY_RESET_TIMEOUT_MS.
- * Until the timer expires, the user can cancel the factory reset operation by doing an action.
- * In this reference app, the action would be pressing again the factory reset button.
- */
- static void ResetActionEventHandler(const AppEvent & event);
-
-#if (CHIP_CONFIG_ENABLE_ICD_LIT && CHIP_CONFIG_ENABLE_ICD_DSLS)
- /**
- * @brief This callback schedules a DSLS LIT action (Dynamic SIT LIT Support).
- *
- * It is used when the app requests SIT mode (check spec, "Runtime Operating Mode Switching")
- */
- static void DSLSActionEventHandler(const AppEvent & event);
-#endif /* CHIP_CONFIG_ENABLE_ICD_LIT && CHIP_CONFIG_ENABLE_ICD_DSLS */
-
- /**
- * @brief This callback performs a factory reset.
- *
- * This is the callback registered with the timer scheduled in ResetActionEventHandler.
- * It will schedule a factory reset using the Matter server instance.
- */
- static void FunctionTimerEventHandler(const AppEvent & event);
-
- void CancelTimer();
- void StartTimer(uint32_t aTimeoutInMs);
-
- friend ButtonManager & ButtonMgr();
- static ButtonManager sInstance;
-};
-
-inline ButtonManager & ButtonMgr()
-{
- return ButtonManager::sInstance;
-}
diff --git a/examples/platform/nxp/mcxw71_k32w1/rpc/AppRpc.cpp b/examples/platform/nxp/mcxw71_k32w1/rpc/AppRpc.cpp
index beea840..af303c0 100644
--- a/examples/platform/nxp/mcxw71_k32w1/rpc/AppRpc.cpp
+++ b/examples/platform/nxp/mcxw71_k32w1/rpc/AppRpc.cpp
@@ -16,12 +16,19 @@
*/
#include "AppRpc.h"
+#include "ButtonApp.h"
+#include "ButtonBle.h"
#include "ButtonManager.h"
#include "clock_config.h"
#include "pin_mux.h"
#include "Rpc.h"
+// These instances do not need to be initialized, since the RPC
+// code only simulates the press of a button.
+static chip::NXP::App::ButtonApp sAppButton;
+static chip::NXP::App::ButtonBle sBleButton;
+
CHIP_ERROR chip::NXP::App::Rpc::Init()
{
/* set clock */
@@ -52,19 +59,19 @@
{
case 0:
message->event = kBUTTON_EventShortPress;
- ButtonMgr().BleCallback(nullptr, message, nullptr);
+ ButtonManager::HandleCallbacks(nullptr, message, &sBleButton);
break;
case 1:
message->event = kBUTTON_EventLongPress;
- ButtonMgr().BleCallback(nullptr, message, nullptr);
+ ButtonManager::HandleCallbacks(nullptr, message, &sBleButton);
break;
case 2:
message->event = kBUTTON_EventShortPress;
- ButtonMgr().AppActionCallback(nullptr, message, nullptr);
+ ButtonManager::HandleCallbacks(nullptr, message, &sAppButton);
break;
case 3:
message->event = kBUTTON_EventLongPress;
- ButtonMgr().AppActionCallback(nullptr, message, nullptr);
+ ButtonManager::HandleCallbacks(nullptr, message, &sAppButton);
break;
default:
break;
diff --git a/examples/thermostat/nxp/rt/rw61x/BUILD.gn b/examples/thermostat/nxp/rt/rw61x/BUILD.gn
index f377845..3e46391 100644
--- a/examples/thermostat/nxp/rt/rw61x/BUILD.gn
+++ b/examples/thermostat/nxp/rt/rw61x/BUILD.gn
@@ -194,7 +194,7 @@
"${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 = [