AppContentLauncherManager.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. *
  3. * Copyright (c) 2021 Project CHIP Authors
  4. * All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "AppContentLauncherManager.h"
  19. #include "../../java/ContentAppAttributeDelegate.h"
  20. #include <app/util/config.h>
  21. #include <json/json.h>
  22. using namespace std;
  23. using namespace chip::app;
  24. using namespace chip::app::Clusters;
  25. using namespace chip::app::DataModel;
  26. using namespace chip::app::Clusters::ContentLauncher;
  27. using ContentAppAttributeDelegate = chip::AppPlatform::ContentAppAttributeDelegate;
  28. AppContentLauncherManager::AppContentLauncherManager(ContentAppAttributeDelegate * attributeDelegate,
  29. list<std::string> acceptHeaderList, uint32_t supportedStreamingProtocols) :
  30. mAttributeDelegate(attributeDelegate)
  31. {
  32. mAcceptHeaderList = acceptHeaderList;
  33. mSupportedStreamingProtocols = supportedStreamingProtocols;
  34. }
  35. void AppContentLauncherManager::HandleLaunchContent(CommandResponseHelper<LaunchResponseType> & helper,
  36. const DecodableList<ParameterType> & parameterList, bool autoplay,
  37. const CharSpan & data)
  38. {
  39. ChipLogProgress(Zcl, "AppContentLauncherManager::HandleLaunchContent for endpoint %d", mEndpointId);
  40. string dataString(data.data(), data.size());
  41. ChipLogProgress(Zcl, " AutoPlay=%s", (autoplay ? "true" : "false"));
  42. bool foundMatch = false;
  43. auto iter = parameterList.begin();
  44. while (iter.Next())
  45. {
  46. auto & parameterType = iter.GetValue();
  47. ChipLogProgress(Zcl, " TEST CASE found match=Example TV Show type=%d", static_cast<uint16_t>(parameterType.type));
  48. foundMatch = true;
  49. }
  50. if (!foundMatch)
  51. {
  52. ChipLogProgress(Zcl, " TEST CASE did not find a match");
  53. }
  54. LaunchResponseType response;
  55. // TODO: Insert code here
  56. response.data = chip::MakeOptional(CharSpan::fromCharString("exampleData"));
  57. response.status = ContentLauncher::ContentLaunchStatusEnum::kSuccess;
  58. helper.Success(response);
  59. }
  60. void AppContentLauncherManager::HandleLaunchUrl(CommandResponseHelper<LaunchResponseType> & helper, const CharSpan & contentUrl,
  61. const CharSpan & displayString, const BrandingInformationType & brandingInformation)
  62. {
  63. ChipLogProgress(Zcl, "AppContentLauncherManager::HandleLaunchUrl");
  64. string contentUrlString(contentUrl.data(), contentUrl.size());
  65. string displayStringString(displayString.data(), displayString.size());
  66. // TODO: Insert code here
  67. LaunchResponseType response;
  68. response.data = chip::MakeOptional(CharSpan::fromCharString("Success"));
  69. response.status = ContentLauncher::ContentLaunchStatusEnum::kSuccess;
  70. helper.Success(response);
  71. }
  72. CHIP_ERROR AppContentLauncherManager::HandleGetAcceptHeaderList(AttributeValueEncoder & aEncoder)
  73. {
  74. ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetAcceptHeaderList");
  75. chip::app::ConcreteReadAttributePath aPath(mEndpointId, chip::app::Clusters::ContentLauncher::Id,
  76. chip::app::Clusters::ContentLauncher::Attributes::AcceptHeader::Id);
  77. std::string resStr = mAttributeDelegate->Read(aPath);
  78. ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetAcceptHeaderList response %s", resStr.c_str());
  79. if (resStr.length() != 0)
  80. {
  81. Json::Reader reader;
  82. Json::Value value;
  83. if (reader.parse(resStr, value))
  84. {
  85. std::string attrId = to_string(chip::app::Clusters::ContentLauncher::Attributes::AcceptHeader::Id);
  86. ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetAcceptHeaderList response parsing done. reading attr %s",
  87. attrId.c_str());
  88. if (value[attrId].isArray())
  89. {
  90. mAcceptHeaderList.clear();
  91. for (Json::Value & entry : value[attrId])
  92. {
  93. if (entry.isString())
  94. {
  95. mAcceptHeaderList.push_back(entry.asString());
  96. }
  97. }
  98. }
  99. }
  100. }
  101. return aEncoder.EncodeList([this](const auto & encoder) -> CHIP_ERROR {
  102. for (std::string & entry : mAcceptHeaderList)
  103. {
  104. CharSpan data = CharSpan::fromCharString(entry.c_str());
  105. ReturnErrorOnFailure(encoder.Encode(data));
  106. }
  107. return CHIP_NO_ERROR;
  108. });
  109. }
  110. uint32_t AppContentLauncherManager::HandleGetSupportedStreamingProtocols()
  111. {
  112. ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetSupportedStreamingProtocols");
  113. chip::app::ConcreteReadAttributePath aPath(mEndpointId, chip::app::Clusters::ContentLauncher::Id,
  114. chip::app::Clusters::ContentLauncher::Attributes::SupportedStreamingProtocols::Id);
  115. std::string resStr = mAttributeDelegate->Read(aPath);
  116. ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetSupportedStreamingProtocols response %s", resStr.c_str());
  117. if (resStr.length() == 0)
  118. {
  119. return mSupportedStreamingProtocols;
  120. }
  121. Json::Reader reader;
  122. Json::Value value;
  123. if (!reader.parse(resStr, value))
  124. {
  125. return mSupportedStreamingProtocols;
  126. }
  127. std::string attrId = to_string(chip::app::Clusters::ContentLauncher::Attributes::SupportedStreamingProtocols::Id);
  128. ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetSupportedStreamingProtocols response parsing done. reading attr %s",
  129. attrId.c_str());
  130. if (!value[attrId].empty() && value[attrId].isInt())
  131. {
  132. uint32_t supportedStreamingProtocols = static_cast<uint32_t>(value[attrId].asInt());
  133. mSupportedStreamingProtocols = supportedStreamingProtocols;
  134. }
  135. return mSupportedStreamingProtocols;
  136. }
  137. uint32_t AppContentLauncherManager::GetFeatureMap(chip::EndpointId endpoint)
  138. {
  139. if (endpoint >= EMBER_AF_CONTENT_LAUNCHER_CLUSTER_SERVER_ENDPOINT_COUNT)
  140. {
  141. return mDynamicEndpointFeatureMap;
  142. }
  143. uint32_t featureMap = 0;
  144. Attributes::FeatureMap::Get(endpoint, &featureMap);
  145. return featureMap;
  146. }