Skip to content

Commit

Permalink
Implement event support for switch cluster (#13036)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca authored and pull[bot] committed Nov 3, 2023
1 parent 27a39e3 commit 1124999
Show file tree
Hide file tree
Showing 27 changed files with 411 additions and 21 deletions.
1 change: 1 addition & 0 deletions examples/all-clusters-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ set(SRC_DIRS_LIST
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/tv-channel-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/scenes"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/software-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/switch-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/general-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/basic"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/bindings"
Expand Down
1 change: 1 addition & 0 deletions examples/all-clusters-app/mbed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ target_sources(${APP_TARGET} PRIVATE
${APP_CLUSTERS}/on-off-server/on-off-server.cpp
${APP_CLUSTERS}/ota-provider/ota-provider.cpp
${APP_CLUSTERS}/scenes/scenes.cpp
${APP_CLUSTERS}/switch-server/switch-server.cpp
${APP_CLUSTERS}/target-navigator-server/target-navigator-server.cpp
${APP_CLUSTERS}/thermostat-server/thermostat-server.cpp
${APP_CLUSTERS}/thermostat-user-interface-configuration-server/thermostat-user-interface-configuration-server.cpp
Expand Down
1 change: 1 addition & 0 deletions examples/bridge-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ idf_component_register(PRIV_INCLUDE_DIRS
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/thread-network-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/wifi-network-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/software-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/switch-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/general-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/descriptor"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/network-commissioning"
Expand Down
20 changes: 20 additions & 0 deletions examples/lighting-app/lighting-common/lighting-app.zap
Original file line number Diff line number Diff line change
Expand Up @@ -3612,6 +3612,26 @@
}
]
},
{
"name": "Switch",
"code": 59,
"mfgCode": null,
"define": "SWITCH_CLUSTER",
"side": "client",
"enabled": 0,
"commands": [],
"attributes": []
},
{
"name": "Switch",
"code": 59,
"mfgCode": null,
"define": "SWITCH_CLUSTER",
"side": "server",
"enabled": 1,
"commands": [],
"attributes": []
},
{
"name": "AdministratorCommissioning",
"code": 60,
Expand Down
1 change: 1 addition & 0 deletions examples/lighting-app/mbed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ target_sources(${APP_TARGET} PRIVATE
${CHIP_ROOT}/src/app/clusters/thread-network-diagnostics-server/thread-network-diagnostics-server.cpp
${CHIP_ROOT}/src/app/clusters/wifi-network-diagnostics-server/wifi-network-diagnostics-server.cpp
${CHIP_ROOT}/src/app/clusters/software-diagnostics-server/software-diagnostics-server.cpp
${CHIP_ROOT}/src/app/clusters/switch-server/switch-server.cpp
${CHIP_ROOT}/src/app/clusters/general-diagnostics-server/general-diagnostics-server.cpp
${CHIP_ROOT}/src/app/clusters/general-commissioning-server/general-commissioning-server.cpp
${CHIP_ROOT}/src/app/clusters/on-off-server/on-off-server.cpp
Expand Down
1 change: 1 addition & 0 deletions examples/lighting-app/telink/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ target_sources(app PRIVATE
${CHIP_ROOT}/src/app/clusters/thread-network-diagnostics-server/thread-network-diagnostics-server.cpp
${CHIP_ROOT}/src/app/clusters/wifi-network-diagnostics-server/wifi-network-diagnostics-server.cpp
${CHIP_ROOT}/src/app/clusters/software-diagnostics-server/software-diagnostics-server.cpp
${CHIP_ROOT}/src/app/clusters/switch-server/switch-server.cpp
${CHIP_ROOT}/src/app/clusters/general-diagnostics-server/general-diagnostics-server.cpp
${CHIP_ROOT}/src/app/clusters/general-commissioning-server/general-commissioning-server.cpp
${CHIP_ROOT}/src/app/clusters/on-off-server/on-off-server.cpp
Expand Down
2 changes: 2 additions & 0 deletions examples/lock-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ idf_component_register(INCLUDE_DIRS
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/thread-network-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/wifi-network-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/software-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/switch-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/general-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/power-source-configuration-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/user-label-server"
Expand Down Expand Up @@ -141,6 +142,7 @@ idf_component_register(PRIV_INCLUDE_DIRS
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/thread-network-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/wifi-network-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/software-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/switch-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/general-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/network-commissioning"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/on-off-server"
Expand Down
1 change: 1 addition & 0 deletions examples/lock-app/mbed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ target_sources(${APP_TARGET} PRIVATE
${CHIP_ROOT}/src/app/clusters/thread-network-diagnostics-server/thread-network-diagnostics-server.cpp
${CHIP_ROOT}/src/app/clusters/wifi-network-diagnostics-server/wifi-network-diagnostics-server.cpp
${CHIP_ROOT}/src/app/clusters/software-diagnostics-server/software-diagnostics-server.cpp
${CHIP_ROOT}/src/app/clusters/switch-server/switch-server.cpp
${CHIP_ROOT}/src/app/clusters/general-diagnostics-server/general-diagnostics-server.cpp
${CHIP_ROOT}/src/app/clusters/general-commissioning-server/general-commissioning-server.cpp
${CHIP_ROOT}/src/app/clusters/network-commissioning/network-commissioning-ember.cpp
Expand Down
1 change: 1 addition & 0 deletions examples/ota-provider-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ idf_component_register(PRIV_INCLUDE_DIRS
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/thread-network-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/wifi-network-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/software-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/switch-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/general-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/general-commissioning-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/network-commissioning"
Expand Down
1 change: 1 addition & 0 deletions examples/ota-requestor-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ idf_component_register(PRIV_INCLUDE_DIRS
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/thread-network-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/wifi-network-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/software-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/switch-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/general-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/general-commissioning-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/network-commissioning"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ idf_component_register(PRIV_INCLUDE_DIRS
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/thread-network-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/wifi-network-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/software-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/switch-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/general-diagnostics-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/general-commissioning-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/network-commissioning"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include <lib/support/logging/CHIPLogging.h>
#include <platform/CHIPDeviceConfig.h>
#include <platform/ConfigurationManager.h>
#include <platform/internal/DeviceControlServer.h>
#include <platform/DeviceControlServer.h>

using namespace chip;
using namespace chip::app;
Expand Down Expand Up @@ -104,7 +104,7 @@ bool emberAfGeneralCommissioningClusterArmFailSafeCallback(app::CommandHandler *
{
auto expiryLengthSeconds = System::Clock::Seconds16(commandData.expiryLengthSeconds);

CHIP_ERROR err = DeviceLayer::Internal::DeviceControlServer::DeviceControlSvr().ArmFailSafe(expiryLengthSeconds);
CHIP_ERROR err = DeviceLayer::DeviceControlServer::DeviceControlSvr().ArmFailSafe(expiryLengthSeconds);
emberAfSendImmediateDefaultResponse(err == CHIP_NO_ERROR ? EMBER_ZCL_STATUS_SUCCESS : EMBER_ZCL_STATUS_FAILURE);

return true;
Expand All @@ -114,7 +114,7 @@ bool emberAfGeneralCommissioningClusterCommissioningCompleteCallback(
app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath,
const Commands::CommissioningComplete::DecodableType & commandData)
{
CHIP_ERROR err = DeviceLayer::Internal::DeviceControlServer::DeviceControlSvr().CommissioningComplete();
CHIP_ERROR err = DeviceLayer::DeviceControlServer::DeviceControlSvr().CommissioningComplete();
emberAfSendImmediateDefaultResponse(err == CHIP_NO_ERROR ? EMBER_ZCL_STATUS_SUCCESS : EMBER_ZCL_STATUS_FAILURE);

return true;
Expand All @@ -128,8 +128,7 @@ bool emberAfGeneralCommissioningClusterSetRegulatoryConfigCallback(app::CommandH
auto & countryCode = commandData.countryCode;
auto & breadcrumb = commandData.breadcrumb;

CHIP_ERROR err =
DeviceLayer::Internal::DeviceControlServer::DeviceControlSvr().SetRegulatoryConfig(location, countryCode, breadcrumb);
CHIP_ERROR err = DeviceLayer::DeviceControlServer::DeviceControlSvr().SetRegulatoryConfig(location, countryCode, breadcrumb);

emberAfSendImmediateDefaultResponse(err == CHIP_NO_ERROR ? EMBER_ZCL_STATUS_SUCCESS : EMBER_ZCL_STATUS_FAILURE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <lib/support/logging/CHIPLogging.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/ConnectivityManager.h>
#include <platform/internal/DeviceControlServer.h>
#include <platform/DeviceControlServer.h>

#include <app-common/zap-generated/cluster-objects.h>

Expand Down Expand Up @@ -281,7 +281,7 @@ void OnConnectNetworkCommandCallbackInternal(app::CommandHandler * apCommandHand
exit:
if (err == NetworkCommissioningStatus::kSuccess)
{
DeviceLayer::Internal::DeviceControlServer::DeviceControlSvr().ConnectNetworkForOperational(networkID);
DeviceLayer::DeviceControlServer::DeviceControlSvr().ConnectNetworkForOperational(networkID);
}
response.networkingStatus = err;
apCommandHandler->AddResponseData(commandPath, response);
Expand Down
200 changes: 200 additions & 0 deletions src/app/clusters/switch-server/switch-server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/**
*
* Copyright (c) 2021 Project CHIP Authors
*
* 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 <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/cluster-objects.h>
#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/AttributeAccessInterface.h>
#include <app/CommandHandler.h>
#include <app/ConcreteCommandPath.h>
#include <app/EventLogging.h>
#include <app/util/af.h>
#include <app/util/attribute-storage.h>
#include <lib/core/Optional.h>
#include <platform/DeviceControlServer.h>

using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::Switch;
using namespace chip::app::Clusters::Switch::Attributes;
using chip::DeviceLayer::DeviceControlServer;

namespace {

class SwitchDelegate : public DeviceLayer::SwitchDeviceControlDelegate
{
/**
* @brief
* Called when the latching switch is moved to a new position.
*/
void OnSwitchLatched(uint8_t newPosition) override
{
ChipLogProgress(Zcl, "SwitchDelegate: OnSwitchLatched");

for (auto endpointId : EnabledEndpointsWithServerCluster(Switch::Id))
{
// Record SwitchLatched event
EventNumber eventNumber;
Events::SwitchLatched::Type event{ newPosition };

if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber))
{
ChipLogError(Zcl, "SwitchDelegate: Failed to record SwitchLatched event");
}
}
}

/**
* @brief
* Called when the momentary switch starts to be pressed.
*/
void OnInitialPressed(uint8_t newPosition) override
{
ChipLogProgress(Zcl, "SwitchDelegate: OnInitialPressed");

for (auto endpointId : EnabledEndpointsWithServerCluster(Switch::Id))
{
// Record InitialPress event
EventNumber eventNumber;
Events::InitialPress::Type event{ newPosition };

if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber))
{
ChipLogError(Zcl, "SwitchDelegate: Failed to record InitialPress event");
}
}
}

/**
* @brief
* Called when the momentary switch has been pressed for a "long" time.
*/
void OnLongPressed(uint8_t newPosition) override
{
ChipLogProgress(Zcl, "SwitchDelegate: OnLongPressed");

for (auto endpointId : EnabledEndpointsWithServerCluster(Switch::Id))
{
// Record LongPress event
EventNumber eventNumber;
Events::LongPress::Type event{ newPosition };

if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber))
{
ChipLogError(Zcl, "SwitchDelegate: Failed to record LongPress event");
}
}
}

/**
* @brief
* Called when the momentary switch has been released.
*/
void OnShortReleased(uint8_t previousPosition) override
{
ChipLogProgress(Zcl, "SwitchDelegate: OnShortReleased");

for (auto endpointId : EnabledEndpointsWithServerCluster(Switch::Id))
{
// Record ShortRelease event
EventNumber eventNumber;
Events::ShortRelease::Type event{ previousPosition };

if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber))
{
ChipLogError(Zcl, "SwitchDelegate: Failed to record ShortRelease event");
}
}
}

/**
* @brief
* Called when the momentary switch has been released (after debouncing)
* and after having been pressed for a long time.
*/
void OnLongReleased(uint8_t previousPosition) override
{
ChipLogProgress(Zcl, "SwitchDelegate: OnLongReleased");

for (auto endpointId : EnabledEndpointsWithServerCluster(Switch::Id))
{
// Record LongRelease event
EventNumber eventNumber;
Events::LongRelease::Type event{ previousPosition };

if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber))
{
ChipLogError(Zcl, "SwitchDelegate: Failed to record LongRelease event");
}
}
}

/**
* @brief
* Called to indicate how many times the momentary switch has been pressed
* in a multi-press sequence, during that sequence.
*/
void OnMultiPressOngoing(uint8_t newPosition, uint8_t count) override
{
ChipLogProgress(Zcl, "SwitchDelegate: OnMultiPressOngoing");

for (auto endpointId : EnabledEndpointsWithServerCluster(Switch::Id))
{
// Record MultiPressOngoing event
EventNumber eventNumber;
Events::MultiPressOngoing::Type event{ newPosition, count };

if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber))
{
ChipLogError(Zcl, "SwitchDelegate: Failed to record MultiPressOngoing event");
}
}
}

/**
* @brief
* Called to indicate how many times the momentary switch has been pressed
* in a multi-press sequence, after it has been detected that the sequence has ended.
*/
void OnMultiPressComplete(uint8_t newPosition, uint8_t count) override
{
ChipLogProgress(Zcl, "SwitchDelegate: OnMultiPressComplete");

for (auto endpointId : EnabledEndpointsWithServerCluster(Switch::Id))
{
// Record MultiPressComplete event
EventNumber eventNumber;
Events::MultiPressComplete::Type event{ newPosition, count };

if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber))
{
ChipLogError(Zcl, "SwitchDelegate: Failed to record MultiPressComplete event");
}
}
}
};

SwitchDelegate gSwitchDelegate;

} // anonymous namespace

void MatterSwitchPluginServerInitCallback()
{
DeviceControlServer::DeviceControlSvr().SetSwitchDelegate(&gSwitchDelegate);
}
1 change: 0 additions & 1 deletion src/app/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ void MatterElectricalMeasurementPluginServerInitCallback() {}
void MatterOtaSoftwareUpdateRequestorPluginServerInitCallback() {}
void MatterGroupKeyManagementPluginServerInitCallback() {}
void MatterRelativeHumidityMeasurementPluginServerInitCallback() {}
void MatterSwitchPluginServerInitCallback() {}
void MatterIlluminanceMeasurementPluginServerInitCallback() {}
void MatterBinaryInputBasicPluginServerInitCallback() {}
void MatterPressureMeasurementPluginServerInitCallback() {}
Expand Down
2 changes: 1 addition & 1 deletion src/app/zap_cluster_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
'RELATIVE_HUMIDITY_MEASUREMENT_CLUSTER': [],
'SCENES_CLUSTER': ['scenes'],
'SOFTWARE_DIAGNOSTICS_CLUSTER': ['software-diagnostics-server'],
'SWITCH_CLUSTER': [],
'SWITCH_CLUSTER': ['switch-server'],
'TARGET_NAVIGATOR_CLUSTER': ['target-navigator-server'],
'TEMP_MEASUREMENT_CLUSTER': [],
'TEST_CLUSTER': ['test-cluster-server'],
Expand Down
Loading

0 comments on commit 1124999

Please sign in to comment.