Skip to content

Commit

Permalink
Connection context: no build
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Oct 10, 2023
1 parent 13712ef commit 7a68fda
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
15 changes: 13 additions & 2 deletions examples/tv-casting-app/tv-casting-common/core/CastingPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,18 @@ CastingPlayer * CastingPlayer::mTargetCastingPlayer = nullptr;

void CastingPlayer::Connect(ConnectCallback onCompleted, const chip::System::Clock::Seconds16 commissioningWindowTimeout)
{
chip::Inet::IPAddress * ipAddressToUse = GetIpAddressForUDCRequest();
CHIP_ERROR err = ipAddressToUse != nullptr ? CHIP_NO_ERROR : CHIP_ERROR_INCORRECT_STATE;
ChipLogProgress(AppServer, "CastingPlayer::Connect called");

chip::Inet::IPAddress * ipAddressToUse = nullptr;

CHIP_ERROR err = CHIP_NO_ERROR;
err = (mConnectionState != CASTING_PLAYER_CONNECTING ? CHIP_NO_ERROR : CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mConnectionState != CASTING_PLAYER_CONNECTING,
ChipLogError(AppServer, "CastingPlayer::Connect called while already connecting to this CastingPlayer"));
mConnectionState = CASTING_PLAYER_CONNECTING;

ipAddressToUse = GetIpAddressForUDCRequest();
err = ipAddressToUse != nullptr ? CHIP_NO_ERROR : CHIP_ERROR_INCORRECT_STATE;
VerifyOrExit(ipAddressToUse != nullptr, ChipLogError(AppServer, "No IP Address found to send UDC request to"));

SuccessOrExit(err = support::ChipDeviceEventHandler::SetUdcStatus(true));
Expand All @@ -45,6 +55,7 @@ void CastingPlayer::Connect(ConnectCallback onCompleted, const chip::System::Clo
if (err != CHIP_NO_ERROR)
{
support::ChipDeviceEventHandler::SetUdcStatus(false);
mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
ChipLogError(AppServer, "CastingPlayer::Connect failed with %" CHIP_ERROR_FORMAT, err.Format());
}
}
Expand Down
44 changes: 42 additions & 2 deletions examples/tv-casting-app/tv-casting-common/core/CastingPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,46 @@ class CastingPlayerAttributes
uint32_t deviceType;
};

/**
* @brief Represents CastingPlayer ConnectionState.
*
*/
enum ConnectionState
{
CASTING_PLAYER_NOT_CONNECTED,
CASTING_PLAYER_CONNECTING,
CASTING_PLAYER_CONNECTED,
};

class ConnectionContext
{
public:
ConnectionContext(CastingPlayer * castingPlayer, chip::OnDeviceConnected handleDeviceConnected,
chip::OnDeviceConnectionFailure handleConnectionFailure)
{
mTargetVideoPlayerInfo = targetVideoPlayerInfo;
mOnConnectedCallback = new chip::Callback::Callback<chip::OnDeviceConnected>(handleDeviceConnected, this);
mOnConnectionFailureCallback = new chip::Callback::Callback<chip::OnDeviceConnectionFailure>(handleConnectionFailure, this);
}

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

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

CastingPlayer * mCastingPlayer;
chip::Callback::Callback<chip::OnDeviceConnected> * mOnConnectedCallback = nullptr;
chip::Callback::Callback<chip::OnDeviceConnectionFailure> * mOnConnectionFailureCallback = nullptr;
};

/**
* @brief CastingPlayer represents a Matter commissioner that is able to play media to a physical
* output or to a display screen which is part of the device.
Expand All @@ -63,7 +103,7 @@ class CastingPlayer : public std::enable_shared_from_this<CastingPlayer>
{
private:
// std::vector<memory::Strong<Endpoint>> endpoints;
bool mConnected = false;
ConnectionState mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
CastingPlayerAttributes mAttributes;
static CastingPlayer * mTargetCastingPlayer;
ConnectCallback mOnCompleted;
Expand Down Expand Up @@ -113,7 +153,7 @@ class CastingPlayer : public std::enable_shared_from_this<CastingPlayer>
/**
* @return true if this CastingPlayer is connected to the CastingApp
*/
bool IsConnected() const { return mConnected; }
bool IsConnected() const { return mConnectionState == CASTING_PLAYER_CONNECTED; }

void Connect(ConnectCallback onCompleted,
const chip::System::Clock::Seconds16 commissioningWindowTimeout = kCommissioningWindowTimeout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ void ChipDeviceEventHandler::Handle(const chip::DeviceLayer::ChipDeviceEvent * e
{
ChipLogProgress(AppServer, "ChipDeviceEventHandler::Handle will connect with nodeId=0x" ChipLogFormatX64 " fabricIndex=%d",
ChipLogValueX64(targetPeerNodeId), targetFabricIndex);
ConnectionContext * connectionContext = new ConnectionContext(CastingPlayer::GetTargetCastingPlayer(),
CastingPlayer::HandleDeviceConnected, CastingPlayer::HandleDeviceConnectionFailure);
chip::Server::GetInstance().GetCASESessionManager()->FindOrEstablishSession(ScopedNodeId(mNodeId, mFabricIndex),
connectionContext->mOnConnectedCallback,
connectionContext->mOnConnectionFailureCallback);

// TODO
/*CHIP_ERROR err = CastingServer::GetInstance()->GetActiveTargetVideoPlayer()->Initialize(
targetPeerNodeId, targetFabricIndex, CastingServer::GetInstance()->mOnConnectionSuccessClientCallback,
Expand Down

0 comments on commit 7a68fda

Please sign in to comment.