Skip to content

Commit

Permalink
tv-casting-app: Updating the context we pass to FindOrEstablishSession
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Mar 1, 2023
1 parent 4a1b82e commit eff5773
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ JNI_METHOD(jboolean, verifyOrEstablishConnection)
[](CHIP_ERROR err) { TvCastingAppJNIMgr().getOnConnectionFailureHandler(true).Handle(err); },
[](TargetEndpointInfo * endpoint) { TvCastingAppJNIMgr().getOnNewOrUpdatedEndpointHandler(true).Handle(endpoint); });
VerifyOrExit(CHIP_NO_ERROR == err,
ChipLogError(AppServer, "CastingServer::OpenBasicCommissioningWindow failed: %" CHIP_ERROR_FORMAT, err.Format()));
ChipLogError(AppServer, "CastingServer::verifyOrEstablishConnection failed: %" CHIP_ERROR_FORMAT, err.Format()));

exit:
return (err == CHIP_NO_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class CastingServer
std::function<void(CHIP_ERROR)> onConnectionFailure,
std::function<void(TargetEndpointInfo *)> onNewOrUpdatedEndpoint);

void LogCachedVideoPlayers();
CHIP_ERROR PurgeVideoPlayerCache();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,46 @@

constexpr size_t kMaxNumberOfEndpoints = 5;

class TargetVideoPlayerInfo;
class VideoPlayerConnectionContext
{
public:
VideoPlayerConnectionContext(TargetVideoPlayerInfo * targetVideoPlayerInfo, chip::OnDeviceConnected handleDeviceConnected,
chip::OnDeviceConnectionFailure handleConnectionFailure,
std::function<void(TargetVideoPlayerInfo *)> onConnectionSuccess,
std::function<void(CHIP_ERROR)> onConnectionFailure)
{
mTargetVideoPlayerInfo = targetVideoPlayerInfo;
mOnConnectedCallback = new chip::Callback::Callback<chip::OnDeviceConnected>(handleDeviceConnected, this);
mOnConnectionFailureCallback = new chip::Callback::Callback<chip::OnDeviceConnectionFailure>(handleConnectionFailure, this);
mOnConnectionSuccessClientCallback = onConnectionSuccess;
mOnConnectionFailureClientCallback = onConnectionFailure;
}

~VideoPlayerConnectionContext()
{
if (mOnConnectedCallback != nullptr)
{
delete mOnConnectedCallback;
}

if (mOnConnectionFailureCallback != nullptr)
{
delete mOnConnectionFailureCallback;
}
}

TargetVideoPlayerInfo * mTargetVideoPlayerInfo;
chip::Callback::Callback<chip::OnDeviceConnected> * mOnConnectedCallback = nullptr;
chip::Callback::Callback<chip::OnDeviceConnectionFailure> * mOnConnectionFailureCallback = nullptr;
std::function<void(TargetVideoPlayerInfo *)> mOnConnectionSuccessClientCallback = {};
std::function<void(CHIP_ERROR)> mOnConnectionFailureClientCallback = {};
};

class TargetVideoPlayerInfo
{
public:
TargetVideoPlayerInfo() :
mOnConnectedCallback(HandleDeviceConnected, this), mOnConnectionFailureCallback(HandleDeviceConnectionFailure, this)
{}
TargetVideoPlayerInfo() {}

bool operator==(const TargetVideoPlayerInfo & other) { return this->mNodeId == other.mNodeId; }

Expand All @@ -49,9 +83,9 @@ class TargetVideoPlayerInfo

chip::OperationalDeviceProxy * GetOperationalDeviceProxy()
{
if (mDeviceProxy.ConnectionReady())
if (mDeviceProxy != nullptr && mDeviceProxy->ConnectionReady())
{
return &mDeviceProxy;
return mDeviceProxy;
}
return nullptr;
}
Expand All @@ -73,19 +107,33 @@ class TargetVideoPlayerInfo
static void HandleDeviceConnected(void * context, chip::Messaging::ExchangeManager & exchangeMgr,
const chip::SessionHandle & sessionHandle)
{
TargetVideoPlayerInfo * _this = static_cast<TargetVideoPlayerInfo *>(context);
_this->mDeviceProxy = chip::OperationalDeviceProxy(&exchangeMgr, sessionHandle);
_this->mInitialized = true;
ChipLogProgress(AppServer, "tmplog: HandleDeviceConnected called");
VideoPlayerConnectionContext * connectionContext = static_cast<VideoPlayerConnectionContext *>(context);
if (connectionContext == nullptr || connectionContext->mTargetVideoPlayerInfo == nullptr)
{
ChipLogError(AppServer, "HandleDeviceConnected called with null context or null context.targetVideoPlayerInfo");
return;
}
if (connectionContext->mTargetVideoPlayerInfo->mDeviceProxy != nullptr)
{
ChipLogProgress(AppServer, "HandleDeviceConnected deleting mDeviceProxy");
delete connectionContext->mTargetVideoPlayerInfo->mDeviceProxy;
ChipLogProgress(AppServer, "HandleDeviceConnected deleted mDeviceProxy");
}
connectionContext->mTargetVideoPlayerInfo->mDeviceProxy = new chip::OperationalDeviceProxy(&exchangeMgr, sessionHandle);
connectionContext->mTargetVideoPlayerInfo->mInitialized = true;
ChipLogProgress(AppServer,
"HandleDeviceConnected created an instance of OperationalDeviceProxy for nodeId: 0x" ChipLogFormatX64
", fabricIndex: %d",
ChipLogValueX64(_this->GetNodeId()), _this->GetFabricIndex());
ChipLogValueX64(connectionContext->mTargetVideoPlayerInfo->GetNodeId()),
connectionContext->mTargetVideoPlayerInfo->GetFabricIndex());

if (_this->mOnConnectionSuccessClientCallback)
if (connectionContext->mOnConnectionSuccessClientCallback)
{
ChipLogProgress(AppServer, "HandleDeviceConnected calling mOnConnectionSuccessClientCallback");
_this->mOnConnectionSuccessClientCallback(_this);
connectionContext->mOnConnectionSuccessClientCallback(connectionContext->mTargetVideoPlayerInfo);
}
delete connectionContext;
}

static void HandleDeviceConnectionFailure(void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error)
Expand All @@ -94,31 +142,35 @@ class TargetVideoPlayerInfo
"HandleDeviceConnectionFailure called for peerId.nodeId: 0x" ChipLogFormatX64
", peer.fabricIndex: %d with error: %" CHIP_ERROR_FORMAT,
ChipLogValueX64(peerId.GetNodeId()), peerId.GetFabricIndex(), error.Format());
TargetVideoPlayerInfo * _this = static_cast<TargetVideoPlayerInfo *>(context);
_this->mDeviceProxy = chip::OperationalDeviceProxy();
if (_this->mOnConnectionFailureClientCallback)
VideoPlayerConnectionContext * connectionContext = static_cast<VideoPlayerConnectionContext *>(context);
if (connectionContext == nullptr || connectionContext->mTargetVideoPlayerInfo == nullptr)
{
ChipLogError(AppServer, "HandleDeviceConnectionFailure called with null context");
return;
}
if (connectionContext->mTargetVideoPlayerInfo->mDeviceProxy != nullptr)
{
delete connectionContext->mTargetVideoPlayerInfo->mDeviceProxy;
}
connectionContext->mTargetVideoPlayerInfo->mDeviceProxy = new chip::OperationalDeviceProxy();
if (connectionContext->mOnConnectionFailureClientCallback)
{
ChipLogProgress(AppServer, "HandleDeviceConnectionFailure calling mOnConnectionFailureClientCallback");
_this->mOnConnectionFailureClientCallback(error);
connectionContext->mOnConnectionFailureClientCallback(error);
}
delete connectionContext;
}

TargetEndpointInfo mEndpoints[kMaxNumberOfEndpoints];
chip::NodeId mNodeId;
chip::FabricIndex mFabricIndex;
chip::OperationalDeviceProxy mDeviceProxy;
chip::OperationalDeviceProxy * mDeviceProxy = nullptr;
uint16_t mVendorId = 0;
uint16_t mProductId = 0;
chip::DeviceTypeId mDeviceType = 0;
char mDeviceName[chip::Dnssd::kMaxDeviceNameLen + 1] = {};
char mHostName[chip::Dnssd::kHostNameMaxLength + 1] = {};
size_t mNumIPs = 0; // number of valid IP addresses
chip::Inet::IPAddress mIpAddress[chip::Dnssd::CommonResolutionData::kMaxIPAddresses];

chip::Callback::Callback<chip::OnDeviceConnected> mOnConnectedCallback;
chip::Callback::Callback<chip::OnDeviceConnectionFailure> mOnConnectionFailureCallback;
std::function<void(TargetVideoPlayerInfo *)> mOnConnectionSuccessClientCallback;
std::function<void(CHIP_ERROR)> mOnConnectionFailureClientCallback;

bool mInitialized = false;
};
14 changes: 13 additions & 1 deletion examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,22 @@ TargetVideoPlayerInfo * CastingServer::ReadCachedTargetVideoPlayerInfos()
return mCachedTargetVideoPlayerInfo;
}

void CastingServer::LogCachedVideoPlayers()
{
ChipLogProgress(AppServer, "CastingServer:LogCachedVideoPlayers dumping any/all cached video players.");
for (size_t i = 0; i < kMaxCachedVideoPlayers && mCachedTargetVideoPlayerInfo[i].IsInitialized(); i++)
{
mCachedTargetVideoPlayerInfo[i].PrintInfo();
}
}

CHIP_ERROR CastingServer::VerifyOrEstablishConnection(TargetVideoPlayerInfo & targetVideoPlayerInfo,
std::function<void(TargetVideoPlayerInfo *)> onConnectionSuccess,
std::function<void(CHIP_ERROR)> onConnectionFailure,
std::function<void(TargetEndpointInfo *)> onNewOrUpdatedEndpoint)
{
LogCachedVideoPlayers();

if (!targetVideoPlayerInfo.IsInitialized())
{
return CHIP_ERROR_INVALID_ARGUMENT;
Expand All @@ -316,7 +327,8 @@ CHIP_ERROR CastingServer::VerifyOrEstablishConnection(TargetVideoPlayerInfo & ta
prevDeviceProxy->Disconnect();
}

return targetVideoPlayerInfo.FindOrEstablishCASESession(
CastingServer::GetInstance()->mActiveTargetVideoPlayerInfo = targetVideoPlayerInfo;
return CastingServer::GetInstance()->mActiveTargetVideoPlayerInfo.FindOrEstablishCASESession(
[](TargetVideoPlayerInfo * videoPlayer) {
ChipLogProgress(AppServer, "CastingServer::OnConnectionSuccess lambda called");
CastingServer::GetInstance()->mActiveTargetVideoPlayerInfo = *videoPlayer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ CHIP_ERROR TargetVideoPlayerInfo::Initialize(NodeId nodeId, FabricIndex fabricIn
memset(mDeviceName, '\0', sizeof(mDeviceName));
if (deviceName != nullptr)
{
chip::Platform::CopyString(mDeviceName, chip::Dnssd::kMaxDeviceNameLen + 1, deviceName);
chip::Platform::CopyString(mDeviceName, chip::Dnssd::kMaxDeviceNameLen, deviceName);
}

memset(mHostName, '\0', sizeof(mHostName));
if (hostName != nullptr)
{
chip::Platform::CopyString(mHostName, chip::Dnssd::kHostNameMaxLength + 1, hostName);
chip::Platform::CopyString(mHostName, chip::Dnssd::kHostNameMaxLength, hostName);
}

for (auto & endpointInfo : mEndpoints)
Expand All @@ -71,11 +71,14 @@ CHIP_ERROR TargetVideoPlayerInfo::Initialize(NodeId nodeId, FabricIndex fabricIn
CHIP_ERROR TargetVideoPlayerInfo::FindOrEstablishCASESession(std::function<void(TargetVideoPlayerInfo *)> onConnectionSuccess,
std::function<void(CHIP_ERROR)> onConnectionFailure)
{
mOnConnectionSuccessClientCallback = onConnectionSuccess;
mOnConnectionFailureClientCallback = onConnectionFailure;
Server * server = &(chip::Server::GetInstance());
server->GetCASESessionManager()->FindOrEstablishSession(ScopedNodeId(mNodeId, mFabricIndex), &mOnConnectedCallback,
&mOnConnectionFailureCallback);
ChipLogProgress(AppServer, "TargetVideoPlayerInfo::FindOrEstablishCASESession called");

VideoPlayerConnectionContext * connectionContext = new VideoPlayerConnectionContext(
this, HandleDeviceConnected, HandleDeviceConnectionFailure, onConnectionSuccess, onConnectionFailure);
Server * server = &(chip::Server::GetInstance());
server->GetCASESessionManager()->FindOrEstablishSession(ScopedNodeId(mNodeId, mFabricIndex),
connectionContext->mOnConnectedCallback,
connectionContext->mOnConnectionFailureCallback);
return CHIP_NO_ERROR;
}

Expand Down Expand Up @@ -128,8 +131,8 @@ bool TargetVideoPlayerInfo::HasEndpoint(EndpointId endpointId)

void TargetVideoPlayerInfo::PrintInfo()
{
ChipLogProgress(NotSpecified, " TargetVideoPlayerInfo nodeId=0x" ChipLogFormatX64 " fabric index=%d", ChipLogValueX64(mNodeId),
mFabricIndex);
ChipLogProgress(NotSpecified, " TargetVideoPlayerInfo deviceName=%s nodeId=0x" ChipLogFormatX64 " fabric index=%d", mDeviceName,
ChipLogValueX64(mNodeId), mFabricIndex);
for (auto & endpointInfo : mEndpoints)
{
if (endpointInfo.IsInitialized())
Expand Down

0 comments on commit eff5773

Please sign in to comment.