bridged-actions-stub.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. *
  3. * Copyright (c) 2021 Project CHIP Authors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <app-common/zap-generated/cluster-objects.h>
  18. #include <app-common/zap-generated/ids/Attributes.h>
  19. #include <app-common/zap-generated/ids/Clusters.h>
  20. #include <app/AttributeAccessInterface.h>
  21. #include <app/util/attribute-storage.h>
  22. #include <lib/support/CodeUtils.h>
  23. #include <lib/support/logging/CHIPLogging.h>
  24. #include <vector>
  25. #include "Device.h"
  26. #include "main.h"
  27. using namespace chip;
  28. using namespace chip::app;
  29. using namespace chip::app::Clusters;
  30. using namespace chip::app::Clusters::Actions::Attributes;
  31. namespace {
  32. class ActionsAttrAccess : public AttributeAccessInterface
  33. {
  34. public:
  35. // Register for the Actions cluster on all endpoints.
  36. ActionsAttrAccess() : AttributeAccessInterface(Optional<EndpointId>::Missing(), Actions::Id) {}
  37. CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) override;
  38. private:
  39. static constexpr uint16_t ClusterRevision = 1;
  40. CHIP_ERROR ReadActionListAttribute(EndpointId endpoint, AttributeValueEncoder & aEncoder);
  41. CHIP_ERROR ReadEndpointListAttribute(EndpointId endpoint, AttributeValueEncoder & aEncoder);
  42. CHIP_ERROR ReadSetupUrlAttribute(EndpointId endpoint, AttributeValueEncoder & aEncoder);
  43. CHIP_ERROR ReadClusterRevision(EndpointId endpoint, AttributeValueEncoder & aEncoder);
  44. };
  45. constexpr uint16_t ActionsAttrAccess::ClusterRevision;
  46. CHIP_ERROR ActionsAttrAccess::ReadActionListAttribute(EndpointId endpoint, AttributeValueEncoder & aEncoder)
  47. {
  48. CHIP_ERROR err = aEncoder.EncodeList([&endpoint](const auto & encoder) -> CHIP_ERROR {
  49. std::vector<Action *> actionList = GetActionListInfo(endpoint);
  50. for (auto action : actionList)
  51. {
  52. if (action->getIsVisible())
  53. {
  54. Actions::Structs::ActionStruct::Type actionStruct = { action->getActionId(),
  55. CharSpan::fromCharString(action->getName().c_str()),
  56. action->getType(),
  57. action->getEndpointListId(),
  58. action->getSupportedCommands(),
  59. action->getStatus() };
  60. ReturnErrorOnFailure(encoder.Encode(actionStruct));
  61. }
  62. }
  63. return CHIP_NO_ERROR;
  64. });
  65. return err;
  66. }
  67. CHIP_ERROR ActionsAttrAccess::ReadEndpointListAttribute(EndpointId endpoint, AttributeValueEncoder & aEncoder)
  68. {
  69. std::vector<EndpointListInfo> infoList = GetEndpointListInfo(endpoint);
  70. CHIP_ERROR err = aEncoder.EncodeList([&infoList](const auto & encoder) -> CHIP_ERROR {
  71. for (auto info : infoList)
  72. {
  73. Actions::Structs::EndpointListStruct::Type endpointListStruct = {
  74. info.GetEndpointListId(), CharSpan::fromCharString(info.GetName().c_str()), info.GetType(),
  75. DataModel::List<chip::EndpointId>(info.GetEndpointListData(), info.GetEndpointListSize())
  76. };
  77. ReturnErrorOnFailure(encoder.Encode(endpointListStruct));
  78. }
  79. return CHIP_NO_ERROR;
  80. });
  81. return err;
  82. }
  83. CHIP_ERROR ActionsAttrAccess::ReadSetupUrlAttribute(EndpointId endpoint, AttributeValueEncoder & aEncoder)
  84. {
  85. const char SetupUrl[] = "https://example.com";
  86. return aEncoder.Encode(chip::CharSpan::fromCharString(SetupUrl));
  87. }
  88. CHIP_ERROR ActionsAttrAccess::ReadClusterRevision(EndpointId endpoint, AttributeValueEncoder & aEncoder)
  89. {
  90. return aEncoder.Encode(ClusterRevision);
  91. }
  92. ActionsAttrAccess gAttrAccess;
  93. CHIP_ERROR ActionsAttrAccess::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
  94. {
  95. VerifyOrDie(aPath.mClusterId == Actions::Id);
  96. switch (aPath.mAttributeId)
  97. {
  98. case ActionList::Id:
  99. return ReadActionListAttribute(aPath.mEndpointId, aEncoder);
  100. case EndpointLists::Id:
  101. return ReadEndpointListAttribute(aPath.mEndpointId, aEncoder);
  102. case SetupURL::Id:
  103. return ReadSetupUrlAttribute(aPath.mEndpointId, aEncoder);
  104. case ClusterRevision::Id:
  105. return ReadClusterRevision(aPath.mEndpointId, aEncoder);
  106. default:
  107. break;
  108. }
  109. return CHIP_NO_ERROR;
  110. }
  111. } // anonymous namespace
  112. void MatterActionsPluginServerInitCallback()
  113. {
  114. registerAttributeAccessOverride(&gAttrAccess);
  115. }