/* * * Copyright (c) 2021 Project CHIP Authors * All rights reserved. * * 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 "ContentLauncherManager.h" #include #include #include #include using namespace chip::app; using namespace chip::app::Clusters; using namespace chip::app::DataModel; using namespace chip::app::Clusters::ContentLauncher; ContentLauncherManager::ContentLauncherManager(std::list acceptHeaderList, uint32_t supportedStreamingProtocols) { mAcceptHeaderList = acceptHeaderList; mSupportedStreamingProtocols = supportedStreamingProtocols; // Add dummy content for test cases ContentEntry entry1; entry1.mName = "TV Show Example"; ParameterType parameter1; parameter1.type = ParameterEnum::kActor; parameter1.value = chip::CharSpan::fromCharString("Gaby sHoffman"); ParameterType parameter2; parameter2.type = ParameterEnum::kChannel; parameter2.value = chip::CharSpan::fromCharString("PBS"); ParameterType parameter3; parameter3.type = ParameterEnum::kCharacter; parameter3.value = chip::CharSpan::fromCharString("Snow White"); ParameterType parameter4; parameter4.type = ParameterEnum::kDirector; parameter4.value = chip::CharSpan::fromCharString("Spike Lee"); ParameterType parameter5; parameter5.type = ParameterEnum::kFranchise; parameter5.value = chip::CharSpan::fromCharString("Star Wars"); ParameterType parameter6; parameter6.type = ParameterEnum::kGenre; parameter6.value = chip::CharSpan::fromCharString("Horror"); ParameterType parameter7; parameter7.type = ParameterEnum::kPopularity; parameter7.value = chip::CharSpan::fromCharString("Popularity"); ParameterType parameter8; parameter8.type = ParameterEnum::kProvider; parameter8.value = chip::CharSpan::fromCharString("Netflix"); entry1.mSearchFields.push_back(parameter1); entry1.mSearchFields.push_back(parameter2); entry1.mSearchFields.push_back(parameter3); entry1.mSearchFields.push_back(parameter4); entry1.mSearchFields.push_back(parameter5); entry1.mSearchFields.push_back(parameter6); entry1.mSearchFields.push_back(parameter7); entry1.mSearchFields.push_back(parameter8); mContentList.push_back(entry1); ContentEntry entry2; entry2.mName = "Sports Example"; ParameterType parameter21; parameter21.type = ParameterEnum::kEvent; parameter21.value = chip::CharSpan::fromCharString("Football games"); ParameterType parameter22; parameter22.type = ParameterEnum::kLeague; parameter22.value = chip::CharSpan::fromCharString("NCAA"); ParameterType parameter23; parameter23.type = ParameterEnum::kSport; parameter23.value = chip::CharSpan::fromCharString("football"); ParameterType parameter24; parameter24.type = ParameterEnum::kSportsTeam; parameter24.value = chip::CharSpan::fromCharString("Arsenel"); ParameterType parameter25; parameter25.type = ParameterEnum::kType; parameter25.value = chip::CharSpan::fromCharString("TVSeries"); entry2.mSearchFields.push_back(parameter21); entry2.mSearchFields.push_back(parameter22); entry2.mSearchFields.push_back(parameter23); entry2.mSearchFields.push_back(parameter24); entry2.mSearchFields.push_back(parameter25); mContentList.push_back(entry2); } void ContentLauncherManager::HandleLaunchContent(CommandResponseHelper & helper, const DecodableList & parameterList, bool autoplay, const CharSpan & data, const chip::Optional playbackPreferences, bool useCurrentContext) { ChipLogProgress(Zcl, "ContentLauncherManager::HandleLaunchContent"); std::string dataString(data.data(), data.size()); ChipLogProgress(Zcl, "ContentLauncherManager::HandleLaunchContent TEST CASE autoplay=%d data=%s ", (autoplay ? 1 : 0), dataString.c_str()); // TODO: Add playbackPreferences as part of search query // TODO: Add useCurrentContext as part of search query bool foundMatch = false; for (auto const & contentEntry : this->mContentList) { auto iter = parameterList.begin(); while (iter.Next()) { auto & parameterType = iter.GetValue(); for (auto const & parameter : contentEntry.mSearchFields) { if (parameter.type == parameterType.type) { std::string val1(parameter.value.data(), parameter.value.size()); std::string val2(parameterType.value.data(), parameterType.value.size()); if (strcmp(val1.c_str(), val2.c_str()) == 0) { ChipLogProgress(Zcl, " TEST CASE found match=%s type=%d", contentEntry.mName.c_str(), static_cast(parameter.type)); foundMatch = true; } } } } } if (!foundMatch) { ChipLogProgress(Zcl, " TEST CASE did not find a match"); } LaunchResponseType response; // TODO: Insert code here response.data = chip::MakeOptional(CharSpan::fromCharString("exampleData")); response.status = ContentLauncher::StatusEnum::kSuccess; helper.Success(response); } void ContentLauncherManager::HandleLaunchUrl(CommandResponseHelper & helper, const CharSpan & contentUrl, const CharSpan & displayString, const BrandingInformationType & brandingInformation) { ChipLogProgress(Zcl, "ContentLauncherManager::HandleLaunchUrl"); std::string contentUrlString(contentUrl.data(), contentUrl.size()); std::string displayStringString(displayString.data(), displayString.size()); std::string providerNameString(brandingInformation.providerName.data(), brandingInformation.providerName.size()); ChipLogProgress( Zcl, "ContentLauncherManager::HandleLaunchUrl TEST CASE ContentURL=%s DisplayString=%s BrandingInformation.ProviderName=%s", contentUrlString.c_str(), displayStringString.c_str(), providerNameString.c_str()); // TODO: Insert code here LaunchResponseType response; response.data = chip::MakeOptional(CharSpan::fromCharString("exampleData")); response.status = ContentLauncher::StatusEnum::kSuccess; // Handle test cases if (contentUrlString == "https://badurl") { response.status = ContentLauncher::StatusEnum::kURLNotAvailable; } else if (contentUrlString == "https://csa-iot.org/badauth") { response.status = ContentLauncher::StatusEnum::kAuthFailed; } helper.Success(response); } CHIP_ERROR ContentLauncherManager::HandleGetAcceptHeaderList(AttributeValueEncoder & aEncoder) { ChipLogProgress(Zcl, "ContentLauncherManager::HandleGetAcceptHeaderList"); return aEncoder.EncodeList([this](const auto & encoder) -> CHIP_ERROR { for (std::string & entry : mAcceptHeaderList) { CharSpan data = CharSpan::fromCharString(entry.c_str()); ReturnErrorOnFailure(encoder.Encode(data)); } return CHIP_NO_ERROR; }); } uint32_t ContentLauncherManager::HandleGetSupportedStreamingProtocols() { ChipLogProgress(Zcl, "ContentLauncherManager::HandleGetSupportedStreamingProtocols"); return mSupportedStreamingProtocols; } uint32_t ContentLauncherManager::GetFeatureMap(chip::EndpointId endpoint) { if (endpoint >= MATTER_DM_CONTENT_LAUNCHER_CLUSTER_SERVER_ENDPOINT_COUNT) { return kEndpointFeatureMap; } uint32_t featureMap = 0; Attributes::FeatureMap::Get(endpoint, &featureMap); return featureMap; } uint16_t ContentLauncherManager::GetClusterRevision(chip::EndpointId endpoint) { if (endpoint >= MATTER_DM_CONTENT_LAUNCHER_CLUSTER_SERVER_ENDPOINT_COUNT) { return kClusterRevision; } uint16_t clusterRevision = 0; bool success = (Attributes::ClusterRevision::Get(endpoint, &clusterRevision) == chip::Protocols::InteractionModel::Status::Success); if (!success) { ChipLogError(Zcl, "ContentLauncherManager::GetClusterRevision error reading cluster revision"); } return clusterRevision; }