TargetNavigatorManager.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "TargetNavigatorManager.h"
  18. #include <json/json.h>
  19. using namespace std;
  20. using namespace chip::app;
  21. using namespace chip::app::Clusters::TargetNavigator;
  22. using ContentAppAttributeDelegate = chip::AppPlatform::ContentAppAttributeDelegate;
  23. TargetNavigatorManager::TargetNavigatorManager(ContentAppAttributeDelegate * attributeDelegate, std::list<std::string> targets,
  24. uint8_t currentTarget) :
  25. mAttributeDelegate(attributeDelegate)
  26. {
  27. mTargets = targets;
  28. mCurrentTarget = currentTarget;
  29. }
  30. CHIP_ERROR TargetNavigatorManager::HandleGetTargetList(AttributeValueEncoder & aEncoder)
  31. {
  32. ChipLogProgress(Zcl, "TargetNavigatorManager::HandleNavigateTarget");
  33. if (mAttributeDelegate != nullptr)
  34. {
  35. chip::app::ConcreteReadAttributePath aPath(mEndpointId, chip::app::Clusters::TargetNavigator::Id,
  36. chip::app::Clusters::TargetNavigator::Attributes::TargetList::Id);
  37. std::string resStr = mAttributeDelegate->Read(aPath);
  38. ChipLogProgress(Zcl, "TargetNavigatorManager::HandleNavigateTarget response %s", resStr.c_str());
  39. if (resStr.length() != 0)
  40. {
  41. Json::Reader reader;
  42. Json::Value value;
  43. if (reader.parse(resStr, value))
  44. {
  45. std::string attrId = to_string(chip::app::Clusters::TargetNavigator::Attributes::TargetList::Id);
  46. ChipLogProgress(Zcl, "TargetNavigatorManager::HandleNavigateTarget response parsing done. reading attr %s",
  47. attrId.c_str());
  48. if (value[attrId].isArray())
  49. {
  50. return aEncoder.EncodeList([&](const auto & encoder) -> CHIP_ERROR {
  51. int i = 0;
  52. std::string targetId = to_string(static_cast<uint32_t>(
  53. chip::app::Clusters::TargetNavigator::Structs::TargetInfoStruct::Fields::kIdentifier));
  54. std::string targetName = to_string(
  55. static_cast<uint32_t>(chip::app::Clusters::TargetNavigator::Structs::TargetInfoStruct::Fields::kName));
  56. for (Json::Value & entry : value[attrId])
  57. {
  58. if (!entry[targetId].isUInt() || !entry[targetName].isString() || entry[targetId].asUInt() > 255)
  59. {
  60. // invalid target ID. Ignore.
  61. ChipLogError(Zcl, "TargetNavigatorManager::HandleNavigateTarget invalid target ignored");
  62. i++;
  63. continue;
  64. }
  65. Structs::TargetInfoStruct::Type outputInfo;
  66. outputInfo.identifier = static_cast<uint8_t>(entry[targetId].asUInt());
  67. outputInfo.name = CharSpan::fromCharString(entry[targetName].asCString());
  68. ReturnErrorOnFailure(encoder.Encode(outputInfo));
  69. i++;
  70. }
  71. return CHIP_NO_ERROR;
  72. });
  73. }
  74. }
  75. }
  76. }
  77. // NOTE: the ids for each target start at 1 so that we can reserve 0 as "no current target"
  78. return aEncoder.EncodeList([this](const auto & encoder) -> CHIP_ERROR {
  79. int i = 0;
  80. for (std::string & entry : mTargets)
  81. {
  82. Structs::TargetInfoStruct::Type outputInfo;
  83. outputInfo.identifier = static_cast<uint8_t>(i + 1);
  84. outputInfo.name = CharSpan::fromCharString(entry.c_str());
  85. ReturnErrorOnFailure(encoder.Encode(outputInfo));
  86. i++;
  87. }
  88. return CHIP_NO_ERROR;
  89. });
  90. }
  91. uint8_t TargetNavigatorManager::HandleGetCurrentTarget()
  92. {
  93. ChipLogProgress(Zcl, "TargetNavigatorManager::HandleGetCurrentTarget");
  94. if (mAttributeDelegate != nullptr)
  95. {
  96. chip::app::ConcreteReadAttributePath aPath(mEndpointId, chip::app::Clusters::TargetNavigator::Id,
  97. chip::app::Clusters::TargetNavigator::Attributes::TargetList::Id);
  98. std::string resStr = mAttributeDelegate->Read(aPath);
  99. ChipLogProgress(Zcl, "TargetNavigatorManager::HandleGetCurrentTarget response %s", resStr.c_str());
  100. if (resStr.length() != 0)
  101. {
  102. Json::Reader reader;
  103. Json::Value value;
  104. if (reader.parse(resStr, value))
  105. {
  106. std::string attrId = to_string(chip::app::Clusters::TargetNavigator::Attributes::CurrentTarget::Id);
  107. ChipLogProgress(Zcl, "TargetNavigatorManager::HandleGetCurrentTarget response parsing done. reading attr %s",
  108. attrId.c_str());
  109. if (value[attrId].isUInt() && value[attrId].asUInt() < 256)
  110. {
  111. return static_cast<uint8_t>(value[attrId].asUInt());
  112. }
  113. }
  114. }
  115. }
  116. return mCurrentTarget;
  117. }
  118. void TargetNavigatorManager::HandleNavigateTarget(CommandResponseHelper<NavigateTargetResponseType> & helper,
  119. const uint64_t & target, const CharSpan & data)
  120. {
  121. NavigateTargetResponseType response;
  122. if (target == kNoCurrentTarget || target > mTargets.size())
  123. {
  124. response.data = chip::MakeOptional(CharSpan::fromCharString("error"));
  125. response.status = TargetNavigatorStatusEnum::kTargetNotFound;
  126. helper.Success(response);
  127. return;
  128. }
  129. mCurrentTarget = static_cast<uint8_t>(target);
  130. response.data = chip::MakeOptional(CharSpan::fromCharString("data response"));
  131. response.status = TargetNavigatorStatusEnum::kSuccess;
  132. helper.Success(response);
  133. }