From 58dafeeb3b40fe5cc0c2139c10c01e70fb7a36d5 Mon Sep 17 00:00:00 2001 From: Kedar Sovani Date: Tue, 21 Jul 2020 09:31:13 +0530 Subject: [PATCH] Temporary commit: Cluster DummyEcho --- src/lib/datamodel/BUILD.gn | 1 + src/lib/datamodel/Cluster.h | 2 +- src/lib/datamodel/ClusterDummyEcho.h | 113 +++++++++++++++++++++++++++ src/lib/datamodel/ClusterOnOff.h | 10 +-- src/lib/datamodel/ClusterServer.cpp | 2 +- src/lib/datamodel/ClusterServer.h | 2 +- 6 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 src/lib/datamodel/ClusterDummyEcho.h diff --git a/src/lib/datamodel/BUILD.gn b/src/lib/datamodel/BUILD.gn index 3561308aa1f6d4..5f70242f7b1005 100644 --- a/src/lib/datamodel/BUILD.gn +++ b/src/lib/datamodel/BUILD.gn @@ -21,6 +21,7 @@ static_library("datamodel") { "Attribute.h", "Cluster.h", "ClusterBasic.h", + "ClusterDummyEcho.h", "ClusterOnOff.h", "ClusterServer.cpp", "ClusterServer.h", diff --git a/src/lib/datamodel/Cluster.h b/src/lib/datamodel/Cluster.h index 2a29d35c5e032e..dd7907a1dd0112 100644 --- a/src/lib/datamodel/Cluster.h +++ b/src/lib/datamodel/Cluster.h @@ -76,7 +76,7 @@ class Cluster : public Deque * * @param cmd the command that needs to be processed */ - virtual CHIP_ERROR HandleCommand(const Command & cmd) + virtual CHIP_ERROR HandleCommand(Command & cmd) { /* Do nothing */ return CHIP_NO_ERROR; diff --git a/src/lib/datamodel/ClusterDummyEcho.h b/src/lib/datamodel/ClusterDummyEcho.h new file mode 100644 index 00000000000000..56fce86e312f16 --- /dev/null +++ b/src/lib/datamodel/ClusterDummyEcho.h @@ -0,0 +1,113 @@ +/* + * + * Copyright (c) 2020 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. + */ + +/** + * @file + * This file contains definitions for a dummy CHIP Cluster 'Echo' that can be used for + * end-to-end testing. + * + */ + +#ifndef CHIPCLUSTER_ECHO_H_ +#define CHIPCLUSTER_ECHO_H_ + +#include + +namespace chip { +namespace DataModel { + +/* Cluster ID */ +static const ClusterId_t kClusterIdDummyEcho = 0xfc00; + +/* Attribute IDs */ +static const AttributeId_t kAttributeIdDummyEcho = 0x0000; + +/* Command IDs */ +static const CommandId_t kDummyEchoCmdIdEchoRequest = 0x00; +static const CommandId_t kDummyEchoCmdIdEchoResponse = 0x00; + +static inline void ClusterDummyEchoGenerateCommand(Command & cmd, CommandId_t cmdId, uint16_t endpointId) +{ + cmd.mEndpointId = endpointId; + cmd.mType = kCmdTypeCluster; + cmd.mClusterId = kClusterIdDummyEcho; + cmd.mId = cmdId; + cmd.mDirection = cmdId == kDummyEchoCmdIdEchoRequest ? kCmdDirectionClientToServer : kCmdDirectionServerToClient; + cmd.StartEncode(); + cmd.EndEncode(); +} + +static inline void ClusterDummyEchoEncodeEchoRequest(Command & cmd, uint16_t endpointId) +{ + ClusterDummyEchoGenerateCommand(cmd, kDummyEchoCmdIdEchoRequest, endpointId); +} + +static inline void ClusterDummyEchoEncodeEchoResponse(Command & cmd, uint16_t endpointId) +{ + ClusterDummyEchoGenerateCommand(cmd, kDummyEchoCmdIdEchoResponse, endpointId); +}; + +/** + * @brief + * This class implements the DummyEcho cluster as defined in the CHIP specification. + */ +class ClusterDummyEcho : public Cluster +{ +private: + /* Just a placeholder attribute that is never assigned to */ + AttributeSimple mDummyEcho; + +public: + ClusterDummyEcho() : Cluster(kClusterIdDummyEcho), mDummyEcho(kAttributeIdDummyEcho) + { + AddAttribute(&mDummyEcho); + } + + virtual CHIP_ERROR HandleCommandEchoRequest(Command & cmd) + { + /* TODO The endpoint is fake here */ + ClusterDummyEchoEncodeEchoResponse(cmd, 1); + return CHIP_NO_ERROR; + } + + /** + * @brief + * Handle commands for the Cluster DummyEcho. This is already handled in the ClusterDummyEcho + * class. Applications may choose to override this handling if required. + * + * @param cmd the command to handle + * + * @return CHIP_NO_ERROR on success or a failure-specific error code otherwise + */ + virtual CHIP_ERROR HandleCommand(Command & cmd) + { + switch (cmd.mId) + { + case kDummyEchoCmdIdEchoRequest: + return HandleCommandEchoRequest(cmd); + default: + /* Unsupported */ + return CHIP_ERROR_INTERNAL; + } + return CHIP_ERROR_INTERNAL; + } +}; + +} // namespace DataModel +} // namespace chip + +#endif /* CHIPCLUSTER_ECHO_H_ */ diff --git a/src/lib/datamodel/ClusterOnOff.h b/src/lib/datamodel/ClusterOnOff.h index 71804a7e916dc7..d8d7995eaa6a52 100644 --- a/src/lib/datamodel/ClusterOnOff.h +++ b/src/lib/datamodel/ClusterOnOff.h @@ -120,7 +120,7 @@ class ClusterOnOff : public Cluster * * @return CHIP_NO_ERROR on success or a failure-specific error code otherwise */ - virtual CHIP_ERROR HandleCommand(const Command & cmd) + virtual CHIP_ERROR HandleCommand(Command & cmd) { switch (cmd.mId) { @@ -138,7 +138,7 @@ class ClusterOnOff : public Cluster } }; -static inline void GenerateCommand(Command * cmd, CommandId_t cmdId, uint16_t endpointId) +static inline void ClusterOnOffGenerateCommand(Command * cmd, CommandId_t cmdId, uint16_t endpointId) { cmd->mType = kCmdTypeCluster; cmd->mId = cmdId; @@ -152,17 +152,17 @@ static inline void GenerateCommand(Command * cmd, CommandId_t cmdId, uint16_t en static inline void ClusterOnOffEncodeOn(Command * cmd, uint16_t endpointId) { - GenerateCommand(cmd, kOnOffCmdIdOn, endpointId); + ClusterOnOffGenerateCommand(cmd, kOnOffCmdIdOn, endpointId); } static inline void ClusterOnOffEncodeOff(Command * cmd, uint16_t endpointId) { - GenerateCommand(cmd, kOnOffCmdIdOff, endpointId); + ClusterOnOffGenerateCommand(cmd, kOnOffCmdIdOff, endpointId); } static inline void ClusterOnOffEncodeToggle(Command * cmd, uint16_t endpointId) { - GenerateCommand(cmd, kOnOffCmdIdToggle, endpointId); + ClusterOnOffGenerateCommand(cmd, kOnOffCmdIdToggle, endpointId); } } // namespace DataModel diff --git a/src/lib/datamodel/ClusterServer.cpp b/src/lib/datamodel/ClusterServer.cpp index 1a0012cde4e0a5..7af632698638c0 100644 --- a/src/lib/datamodel/ClusterServer.cpp +++ b/src/lib/datamodel/ClusterServer.cpp @@ -26,7 +26,7 @@ namespace chip { namespace DataModel { -CHIP_ERROR ClusterServer::HandleCommand(const Command & cmd) +CHIP_ERROR ClusterServer::HandleCommand(Command & cmd) { if (cmd.mDirection != kCmdDirectionClientToServer) { diff --git a/src/lib/datamodel/ClusterServer.h b/src/lib/datamodel/ClusterServer.h index 426852884a69b8..791ef9cdf0ddd7 100644 --- a/src/lib/datamodel/ClusterServer.h +++ b/src/lib/datamodel/ClusterServer.h @@ -184,7 +184,7 @@ class ClusterServer * * @return CHIP_NO_ERROR on success or a failure-specific error code otherwise */ - CHIP_ERROR HandleCommand(const Command & cmd); + CHIP_ERROR HandleCommand(Command & cmd); }; } // namespace DataModel