Skip to content

Commit eff5773

Browse files
tv-casting-app: Updating the context we pass to FindOrEstablishSession
1 parent 4a1b82e commit eff5773

File tree

5 files changed

+101
-33
lines changed

5 files changed

+101
-33
lines changed

examples/tv-casting-app/android/App/app/src/main/jni/cpp/TvCastingApp-JNI.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ JNI_METHOD(jboolean, verifyOrEstablishConnection)
209209
[](CHIP_ERROR err) { TvCastingAppJNIMgr().getOnConnectionFailureHandler(true).Handle(err); },
210210
[](TargetEndpointInfo * endpoint) { TvCastingAppJNIMgr().getOnNewOrUpdatedEndpointHandler(true).Handle(endpoint); });
211211
VerifyOrExit(CHIP_NO_ERROR == err,
212-
ChipLogError(AppServer, "CastingServer::OpenBasicCommissioningWindow failed: %" CHIP_ERROR_FORMAT, err.Format()));
212+
ChipLogError(AppServer, "CastingServer::verifyOrEstablishConnection failed: %" CHIP_ERROR_FORMAT, err.Format()));
213213

214214
exit:
215215
return (err == CHIP_NO_ERROR);

examples/tv-casting-app/tv-casting-common/include/CastingServer.h

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class CastingServer
9797
std::function<void(CHIP_ERROR)> onConnectionFailure,
9898
std::function<void(TargetEndpointInfo *)> onNewOrUpdatedEndpoint);
9999

100+
void LogCachedVideoPlayers();
100101
CHIP_ERROR PurgeVideoPlayerCache();
101102

102103
/**

examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h

+74-22
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,46 @@
2525

2626
constexpr size_t kMaxNumberOfEndpoints = 5;
2727

28+
class TargetVideoPlayerInfo;
29+
class VideoPlayerConnectionContext
30+
{
31+
public:
32+
VideoPlayerConnectionContext(TargetVideoPlayerInfo * targetVideoPlayerInfo, chip::OnDeviceConnected handleDeviceConnected,
33+
chip::OnDeviceConnectionFailure handleConnectionFailure,
34+
std::function<void(TargetVideoPlayerInfo *)> onConnectionSuccess,
35+
std::function<void(CHIP_ERROR)> onConnectionFailure)
36+
{
37+
mTargetVideoPlayerInfo = targetVideoPlayerInfo;
38+
mOnConnectedCallback = new chip::Callback::Callback<chip::OnDeviceConnected>(handleDeviceConnected, this);
39+
mOnConnectionFailureCallback = new chip::Callback::Callback<chip::OnDeviceConnectionFailure>(handleConnectionFailure, this);
40+
mOnConnectionSuccessClientCallback = onConnectionSuccess;
41+
mOnConnectionFailureClientCallback = onConnectionFailure;
42+
}
43+
44+
~VideoPlayerConnectionContext()
45+
{
46+
if (mOnConnectedCallback != nullptr)
47+
{
48+
delete mOnConnectedCallback;
49+
}
50+
51+
if (mOnConnectionFailureCallback != nullptr)
52+
{
53+
delete mOnConnectionFailureCallback;
54+
}
55+
}
56+
57+
TargetVideoPlayerInfo * mTargetVideoPlayerInfo;
58+
chip::Callback::Callback<chip::OnDeviceConnected> * mOnConnectedCallback = nullptr;
59+
chip::Callback::Callback<chip::OnDeviceConnectionFailure> * mOnConnectionFailureCallback = nullptr;
60+
std::function<void(TargetVideoPlayerInfo *)> mOnConnectionSuccessClientCallback = {};
61+
std::function<void(CHIP_ERROR)> mOnConnectionFailureClientCallback = {};
62+
};
63+
2864
class TargetVideoPlayerInfo
2965
{
3066
public:
31-
TargetVideoPlayerInfo() :
32-
mOnConnectedCallback(HandleDeviceConnected, this), mOnConnectionFailureCallback(HandleDeviceConnectionFailure, this)
33-
{}
67+
TargetVideoPlayerInfo() {}
3468

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

@@ -49,9 +83,9 @@ class TargetVideoPlayerInfo
4983

5084
chip::OperationalDeviceProxy * GetOperationalDeviceProxy()
5185
{
52-
if (mDeviceProxy.ConnectionReady())
86+
if (mDeviceProxy != nullptr && mDeviceProxy->ConnectionReady())
5387
{
54-
return &mDeviceProxy;
88+
return mDeviceProxy;
5589
}
5690
return nullptr;
5791
}
@@ -73,19 +107,33 @@ class TargetVideoPlayerInfo
73107
static void HandleDeviceConnected(void * context, chip::Messaging::ExchangeManager & exchangeMgr,
74108
const chip::SessionHandle & sessionHandle)
75109
{
76-
TargetVideoPlayerInfo * _this = static_cast<TargetVideoPlayerInfo *>(context);
77-
_this->mDeviceProxy = chip::OperationalDeviceProxy(&exchangeMgr, sessionHandle);
78-
_this->mInitialized = true;
110+
ChipLogProgress(AppServer, "tmplog: HandleDeviceConnected called");
111+
VideoPlayerConnectionContext * connectionContext = static_cast<VideoPlayerConnectionContext *>(context);
112+
if (connectionContext == nullptr || connectionContext->mTargetVideoPlayerInfo == nullptr)
113+
{
114+
ChipLogError(AppServer, "HandleDeviceConnected called with null context or null context.targetVideoPlayerInfo");
115+
return;
116+
}
117+
if (connectionContext->mTargetVideoPlayerInfo->mDeviceProxy != nullptr)
118+
{
119+
ChipLogProgress(AppServer, "HandleDeviceConnected deleting mDeviceProxy");
120+
delete connectionContext->mTargetVideoPlayerInfo->mDeviceProxy;
121+
ChipLogProgress(AppServer, "HandleDeviceConnected deleted mDeviceProxy");
122+
}
123+
connectionContext->mTargetVideoPlayerInfo->mDeviceProxy = new chip::OperationalDeviceProxy(&exchangeMgr, sessionHandle);
124+
connectionContext->mTargetVideoPlayerInfo->mInitialized = true;
79125
ChipLogProgress(AppServer,
80126
"HandleDeviceConnected created an instance of OperationalDeviceProxy for nodeId: 0x" ChipLogFormatX64
81127
", fabricIndex: %d",
82-
ChipLogValueX64(_this->GetNodeId()), _this->GetFabricIndex());
128+
ChipLogValueX64(connectionContext->mTargetVideoPlayerInfo->GetNodeId()),
129+
connectionContext->mTargetVideoPlayerInfo->GetFabricIndex());
83130

84-
if (_this->mOnConnectionSuccessClientCallback)
131+
if (connectionContext->mOnConnectionSuccessClientCallback)
85132
{
86133
ChipLogProgress(AppServer, "HandleDeviceConnected calling mOnConnectionSuccessClientCallback");
87-
_this->mOnConnectionSuccessClientCallback(_this);
134+
connectionContext->mOnConnectionSuccessClientCallback(connectionContext->mTargetVideoPlayerInfo);
88135
}
136+
delete connectionContext;
89137
}
90138

91139
static void HandleDeviceConnectionFailure(void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error)
@@ -94,31 +142,35 @@ class TargetVideoPlayerInfo
94142
"HandleDeviceConnectionFailure called for peerId.nodeId: 0x" ChipLogFormatX64
95143
", peer.fabricIndex: %d with error: %" CHIP_ERROR_FORMAT,
96144
ChipLogValueX64(peerId.GetNodeId()), peerId.GetFabricIndex(), error.Format());
97-
TargetVideoPlayerInfo * _this = static_cast<TargetVideoPlayerInfo *>(context);
98-
_this->mDeviceProxy = chip::OperationalDeviceProxy();
99-
if (_this->mOnConnectionFailureClientCallback)
145+
VideoPlayerConnectionContext * connectionContext = static_cast<VideoPlayerConnectionContext *>(context);
146+
if (connectionContext == nullptr || connectionContext->mTargetVideoPlayerInfo == nullptr)
147+
{
148+
ChipLogError(AppServer, "HandleDeviceConnectionFailure called with null context");
149+
return;
150+
}
151+
if (connectionContext->mTargetVideoPlayerInfo->mDeviceProxy != nullptr)
152+
{
153+
delete connectionContext->mTargetVideoPlayerInfo->mDeviceProxy;
154+
}
155+
connectionContext->mTargetVideoPlayerInfo->mDeviceProxy = new chip::OperationalDeviceProxy();
156+
if (connectionContext->mOnConnectionFailureClientCallback)
100157
{
101158
ChipLogProgress(AppServer, "HandleDeviceConnectionFailure calling mOnConnectionFailureClientCallback");
102-
_this->mOnConnectionFailureClientCallback(error);
159+
connectionContext->mOnConnectionFailureClientCallback(error);
103160
}
161+
delete connectionContext;
104162
}
105163

106164
TargetEndpointInfo mEndpoints[kMaxNumberOfEndpoints];
107165
chip::NodeId mNodeId;
108166
chip::FabricIndex mFabricIndex;
109-
chip::OperationalDeviceProxy mDeviceProxy;
167+
chip::OperationalDeviceProxy * mDeviceProxy = nullptr;
110168
uint16_t mVendorId = 0;
111169
uint16_t mProductId = 0;
112170
chip::DeviceTypeId mDeviceType = 0;
113171
char mDeviceName[chip::Dnssd::kMaxDeviceNameLen + 1] = {};
114172
char mHostName[chip::Dnssd::kHostNameMaxLength + 1] = {};
115173
size_t mNumIPs = 0; // number of valid IP addresses
116174
chip::Inet::IPAddress mIpAddress[chip::Dnssd::CommonResolutionData::kMaxIPAddresses];
117-
118-
chip::Callback::Callback<chip::OnDeviceConnected> mOnConnectedCallback;
119-
chip::Callback::Callback<chip::OnDeviceConnectionFailure> mOnConnectionFailureCallback;
120-
std::function<void(TargetVideoPlayerInfo *)> mOnConnectionSuccessClientCallback;
121-
std::function<void(CHIP_ERROR)> mOnConnectionFailureClientCallback;
122-
123175
bool mInitialized = false;
124176
};

examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,22 @@ TargetVideoPlayerInfo * CastingServer::ReadCachedTargetVideoPlayerInfos()
295295
return mCachedTargetVideoPlayerInfo;
296296
}
297297

298+
void CastingServer::LogCachedVideoPlayers()
299+
{
300+
ChipLogProgress(AppServer, "CastingServer:LogCachedVideoPlayers dumping any/all cached video players.");
301+
for (size_t i = 0; i < kMaxCachedVideoPlayers && mCachedTargetVideoPlayerInfo[i].IsInitialized(); i++)
302+
{
303+
mCachedTargetVideoPlayerInfo[i].PrintInfo();
304+
}
305+
}
306+
298307
CHIP_ERROR CastingServer::VerifyOrEstablishConnection(TargetVideoPlayerInfo & targetVideoPlayerInfo,
299308
std::function<void(TargetVideoPlayerInfo *)> onConnectionSuccess,
300309
std::function<void(CHIP_ERROR)> onConnectionFailure,
301310
std::function<void(TargetEndpointInfo *)> onNewOrUpdatedEndpoint)
302311
{
312+
LogCachedVideoPlayers();
313+
303314
if (!targetVideoPlayerInfo.IsInitialized())
304315
{
305316
return CHIP_ERROR_INVALID_ARGUMENT;
@@ -316,7 +327,8 @@ CHIP_ERROR CastingServer::VerifyOrEstablishConnection(TargetVideoPlayerInfo & ta
316327
prevDeviceProxy->Disconnect();
317328
}
318329

319-
return targetVideoPlayerInfo.FindOrEstablishCASESession(
330+
CastingServer::GetInstance()->mActiveTargetVideoPlayerInfo = targetVideoPlayerInfo;
331+
return CastingServer::GetInstance()->mActiveTargetVideoPlayerInfo.FindOrEstablishCASESession(
320332
[](TargetVideoPlayerInfo * videoPlayer) {
321333
ChipLogProgress(AppServer, "CastingServer::OnConnectionSuccess lambda called");
322334
CastingServer::GetInstance()->mActiveTargetVideoPlayerInfo = *videoPlayer;

examples/tv-casting-app/tv-casting-common/src/TargetVideoPlayerInfo.cpp

+12-9
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ CHIP_ERROR TargetVideoPlayerInfo::Initialize(NodeId nodeId, FabricIndex fabricIn
4545
memset(mDeviceName, '\0', sizeof(mDeviceName));
4646
if (deviceName != nullptr)
4747
{
48-
chip::Platform::CopyString(mDeviceName, chip::Dnssd::kMaxDeviceNameLen + 1, deviceName);
48+
chip::Platform::CopyString(mDeviceName, chip::Dnssd::kMaxDeviceNameLen, deviceName);
4949
}
5050

5151
memset(mHostName, '\0', sizeof(mHostName));
5252
if (hostName != nullptr)
5353
{
54-
chip::Platform::CopyString(mHostName, chip::Dnssd::kHostNameMaxLength + 1, hostName);
54+
chip::Platform::CopyString(mHostName, chip::Dnssd::kHostNameMaxLength, hostName);
5555
}
5656

5757
for (auto & endpointInfo : mEndpoints)
@@ -71,11 +71,14 @@ CHIP_ERROR TargetVideoPlayerInfo::Initialize(NodeId nodeId, FabricIndex fabricIn
7171
CHIP_ERROR TargetVideoPlayerInfo::FindOrEstablishCASESession(std::function<void(TargetVideoPlayerInfo *)> onConnectionSuccess,
7272
std::function<void(CHIP_ERROR)> onConnectionFailure)
7373
{
74-
mOnConnectionSuccessClientCallback = onConnectionSuccess;
75-
mOnConnectionFailureClientCallback = onConnectionFailure;
76-
Server * server = &(chip::Server::GetInstance());
77-
server->GetCASESessionManager()->FindOrEstablishSession(ScopedNodeId(mNodeId, mFabricIndex), &mOnConnectedCallback,
78-
&mOnConnectionFailureCallback);
74+
ChipLogProgress(AppServer, "TargetVideoPlayerInfo::FindOrEstablishCASESession called");
75+
76+
VideoPlayerConnectionContext * connectionContext = new VideoPlayerConnectionContext(
77+
this, HandleDeviceConnected, HandleDeviceConnectionFailure, onConnectionSuccess, onConnectionFailure);
78+
Server * server = &(chip::Server::GetInstance());
79+
server->GetCASESessionManager()->FindOrEstablishSession(ScopedNodeId(mNodeId, mFabricIndex),
80+
connectionContext->mOnConnectedCallback,
81+
connectionContext->mOnConnectionFailureCallback);
7982
return CHIP_NO_ERROR;
8083
}
8184

@@ -128,8 +131,8 @@ bool TargetVideoPlayerInfo::HasEndpoint(EndpointId endpointId)
128131

129132
void TargetVideoPlayerInfo::PrintInfo()
130133
{
131-
ChipLogProgress(NotSpecified, " TargetVideoPlayerInfo nodeId=0x" ChipLogFormatX64 " fabric index=%d", ChipLogValueX64(mNodeId),
132-
mFabricIndex);
134+
ChipLogProgress(NotSpecified, " TargetVideoPlayerInfo deviceName=%s nodeId=0x" ChipLogFormatX64 " fabric index=%d", mDeviceName,
135+
ChipLogValueX64(mNodeId), mFabricIndex);
133136
for (auto & endpointInfo : mEndpoints)
134137
{
135138
if (endpointInfo.IsInitialized())

0 commit comments

Comments
 (0)