Skip to content

Commit

Permalink
Moved TargetEndpointInfo and TargetVideoPlayerInfo out of tv-casting-…
Browse files Browse the repository at this point in the history
…app/linux/main.cpp (#17826)

Refactored tv-casting-app/linux/main.cpp: Created CastingServer, renamed Casting->CastingUtils
  • Loading branch information
sharadb-amazon authored and pull[bot] committed Feb 7, 2024
1 parent fec3cdd commit 9a2af19
Show file tree
Hide file tree
Showing 12 changed files with 788 additions and 544 deletions.
6 changes: 5 additions & 1 deletion examples/tv-casting-app/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ assert(chip_build_tools)
executable("chip-tv-casting-app") {
sources = [
"${chip_root}/examples/tv-casting-app/tv-casting-common/include/CHIPProjectAppConfig.h",
"Casting.h",
"CastingShellCommands.cpp",
"CastingShellCommands.h",
"CastingUtils.cpp",
"CastingUtils.h",
"main.cpp",
]

Expand All @@ -37,6 +38,9 @@ executable("chip-tv-casting-app") {
"${chip_root}/third_party/inipp",
]

include_dirs =
[ "${chip_root}/examples/tv-casting-app/tv-casting-common/include" ]

cflags = [ "-Wconversion" ]

if (chip_build_libshell) {
Expand Down
13 changes: 8 additions & 5 deletions examples/tv-casting-app/linux/CastingShellCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
*/

#include "CastingShellCommands.h"
#include "Casting.h"
#include "CastingServer.h"
#include "CastingUtils.h"
#include <inttypes.h>
#include <lib/core/CHIPCore.h>
#include <lib/shell/Commands.h>
Expand Down Expand Up @@ -67,7 +68,7 @@ static CHIP_ERROR CastingHandler(int argc, char ** argv)
char * eptr;
chip::NodeId nodeId = (chip::NodeId) strtoull(argv[1], &eptr, 10);
chip::FabricIndex fabricIndex = (chip::FabricIndex) strtol(argv[2], &eptr, 10);
return TargetVideoPlayerInfoInit(nodeId, fabricIndex);
return CastingServer::GetInstance()->TargetVideoPlayerInfoInit(nodeId, fabricIndex);
}
if (strcmp(argv[0], "discover") == 0)
{
Expand Down Expand Up @@ -95,7 +96,7 @@ static CHIP_ERROR CastingHandler(int argc, char ** argv)
}
char * url = argv[1];
char * display = argv[2];
return ContentLauncherLaunchURL(url, display);
return CastingServer::GetInstance()->ContentLauncherLaunchURL(url, display);
}
if (strcmp(argv[0], "access") == 0)
{
Expand All @@ -106,7 +107,7 @@ static CHIP_ERROR CastingHandler(int argc, char ** argv)
}
char * eptr;
chip::NodeId node = (chip::NodeId) strtoull(argv[1], &eptr, 0);
ReadServerClustersForNode(node);
CastingServer::GetInstance()->ReadServerClustersForNode(node);
return CHIP_NO_ERROR;
}
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
Expand All @@ -116,7 +117,9 @@ static CHIP_ERROR CastingHandler(int argc, char ** argv)
chip::Inet::IPAddress commissioner;
chip::Inet::IPAddress::FromString(argv[1], commissioner);
uint16_t port = (uint16_t) strtol(argv[2], &eptr, 10);
return SendUDC(chip::Transport::PeerAddress::UDP(commissioner, port));
PrepareForCommissioning();
return CastingServer::GetInstance()->SendUserDirectedCommissioningRequest(
chip::Transport::PeerAddress::UDP(commissioner, port));
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
return CHIP_ERROR_INVALID_ARGUMENT;
Expand Down
112 changes: 112 additions & 0 deletions examples/tv-casting-app/linux/CastingUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
*
* Copyright (c) 2022 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 "CastingUtils.h"

CHIP_ERROR DiscoverCommissioners()
{
// Send discover commissioners request
ReturnErrorOnFailure(CastingServer::GetInstance()->DiscoverCommissioners());

// Give commissioners some time to respond and then ScheduleWork to initiate commissioning
return DeviceLayer::SystemLayer().StartTimer(
chip::System::Clock::Milliseconds32(kCommissionerDiscoveryTimeoutInMs),
[](System::Layer *, void *) { chip::DeviceLayer::PlatformMgr().ScheduleWork(InitCommissioningFlow); }, nullptr);
}

CHIP_ERROR RequestCommissioning(int index)
{
const Dnssd::DiscoveredNodeData * selectedCommissioner = CastingServer::GetInstance()->GetDiscoveredCommissioner(index);
if (selectedCommissioner == nullptr)
{
ChipLogError(AppServer, "No such commissioner with index %d exists", index);
return CHIP_ERROR_INVALID_ARGUMENT;
}
PrepareForCommissioning(selectedCommissioner);
return CHIP_NO_ERROR;
}

/**
* Enters commissioning mode, opens commissioning window, logs onboarding payload.
* If non-null selectedCommissioner is provided, sends user directed commissioning
* request to the selectedCommissioner and advertises self as commissionable node over DNS-SD
*/
void PrepareForCommissioning(const Dnssd::DiscoveredNodeData * selectedCommissioner)
{
CastingServer::GetInstance()->InitServer();

CastingServer::GetInstance()->OpenBasicCommissioningWindow();

// Display onboarding payload
chip::DeviceLayer::ConfigurationMgr().LogDeviceConfig();

#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
if (selectedCommissioner != nullptr)
{
// Send User Directed commissioning request
// Wait 1 second to allow our commissionee DNS records to publish (needed on Mac)
int32_t expiration = 1;
ReturnOnFailure(DeviceLayer::SystemLayer().StartTimer(System::Clock::Seconds32(expiration), HandleUDCSendExpiration,
(void *) selectedCommissioner));
}
else
{
ChipLogProgress(AppServer, "To run discovery again, enter: cast discover");
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
}

void InitCommissioningFlow(intptr_t commandArg)
{
int commissionerCount = 0;

// Display discovered commissioner TVs to ask user to select one
for (int i = 0; i < CHIP_DEVICE_CONFIG_MAX_DISCOVERED_NODES; i++)
{
const Dnssd::DiscoveredNodeData * commissioner = CastingServer::GetInstance()->GetDiscoveredCommissioner(i);
if (commissioner != nullptr)
{
ChipLogProgress(AppServer, "Discovered Commissioner #%d", commissionerCount++);
commissioner->LogDetail();
}
}

if (commissionerCount > 0)
{
ChipLogProgress(AppServer, "%d commissioner(s) discovered. Select one (by number# above) to request commissioning from: ",
commissionerCount);

ChipLogProgress(AppServer, "Example: cast request 0");
}
else
{
ChipLogError(AppServer, "No commissioner discovered, commissioning must be initiated manually!");
PrepareForCommissioning();
}
}

#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
void HandleUDCSendExpiration(System::Layer * aSystemLayer, void * context)
{
Dnssd::DiscoveredNodeData * selectedCommissioner = (Dnssd::DiscoveredNodeData *) context;

// Send User Directed commissioning request
ReturnOnFailure(CastingServer::GetInstance()->SendUserDirectedCommissioningRequest(chip::Transport::PeerAddress::UDP(
selectedCommissioner->ipAddress[0], selectedCommissioner->port, selectedCommissioner->interfaceId)));
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -16,18 +16,26 @@
* limitations under the License.
*/

/**
* @brief CastingUtils carries several utility functions that can be used
* with Linux implementations of the TV Casting app.
*/
#pragma once

#include <app/server/Dnssd.h>
#include <app/server/Server.h>
#include <lib/core/CHIPError.h>
#include <lib/core/NodeId.h>
#include "CastingServer.h"
#include "TargetEndpointInfo.h"
#include "TargetVideoPlayerInfo.h"

constexpr uint32_t kCommissionerDiscoveryTimeoutInMs = 5 * 1000;

CHIP_ERROR TargetVideoPlayerInfoInit(chip::NodeId nodeId, chip::FabricIndex fabricIndex);
CHIP_ERROR DiscoverCommissioners();

CHIP_ERROR RequestCommissioning(int index);
void ReadServerClustersForNode(chip::NodeId nodeId);
CHIP_ERROR ContentLauncherLaunchURL(const char * contentUrl, const char * contentDisplayStr);

void PrepareForCommissioning(const Dnssd::DiscoveredNodeData * selectedCommissioner = nullptr);

void InitCommissioningFlow(intptr_t commandArg);

#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
CHIP_ERROR SendUDC(chip::Transport::PeerAddress commissioner);
void HandleUDCSendExpiration(System::Layer * aSystemLayer, void * context);
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
Loading

0 comments on commit 9a2af19

Please sign in to comment.