Skip to content

Commit

Permalink
Fixing style issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pgregorr-amazon committed Aug 2, 2024
1 parent 79faacc commit 125d6c6
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public class ConnectionExampleFragment extends Fragment {
private TextView connectionFragmentStatusTextView;
private Button connectionFragmentNextButton;

// Get a singleton instance of the MatterCastingPlayerDiscovery. Which can be used to call stopDiscovery() during connection.
// Get a singleton instance of the MatterCastingPlayerDiscovery. Which can be used to call
// stopDiscovery() during connection.
static final CastingPlayerDiscovery matterCastingPlayerDiscovery =
MatterCastingPlayerDiscovery.getInstance();

Expand Down Expand Up @@ -369,15 +370,20 @@ public void onClick(DialogInterface dialog, int which) {
}

private void stopDiscovery() {
Log.i(TAG, "ConnectionExampleFragment stopDiscovery() called, calling MatterCastingPlayerDiscovery.stopDiscovery()");
Log.i(
TAG,
"ConnectionExampleFragment stopDiscovery() called, calling MatterCastingPlayerDiscovery.stopDiscovery()");

MatterError err = matterCastingPlayerDiscovery.stopDiscovery();
if (err.hasError()) {
Log.e(
TAG,
"ConnectionExampleFragment stopDiscovery() MatterCastingPlayerDiscovery.stopDiscovery() called, err Stop: " + err);
"ConnectionExampleFragment stopDiscovery() MatterCastingPlayerDiscovery.stopDiscovery() called, err Stop: "
+ err);
} else {
Log.d(TAG, "ConnectionExampleFragment stopDiscovery() MatterCastingPlayerDiscovery.stopDiscovery() success");
Log.d(
TAG,
"ConnectionExampleFragment stopDiscovery() MatterCastingPlayerDiscovery.stopDiscovery() success");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public class DiscoveryExampleFragment extends Fragment {
public void onAdded(CastingPlayer castingPlayer) {
Log.i(
TAG,
"DiscoveryExampleFragment onAdded() Discovered CastingPlayer deviceId: " + castingPlayer.getDeviceId());
"DiscoveryExampleFragment onAdded() Discovered CastingPlayer deviceId: "
+ castingPlayer.getDeviceId());
// Display CastingPlayer info on the screen
new Handler(Looper.getMainLooper())
.post(
Expand Down Expand Up @@ -216,7 +217,8 @@ public void onResume() {
public void onPause() {
super.onPause();
Log.i(TAG, "DiscoveryExampleFragment onPause() called, calling stopDiscovery()");
// Stop discovery when leaving the fragment, for example, while displaying the ConnectionExampleFragment.
// Stop discovery when leaving the fragment, for example, while displaying the
// ConnectionExampleFragment.
stopDiscovery();
}

Expand Down
32 changes: 22 additions & 10 deletions examples/tv-casting-app/tv-casting-common/core/CastingPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ void CastingPlayer::VerifyOrEstablishConnection(ConnectionCallbacks connectionCa
nullptr,
[](void * context, chip::Messaging::ExchangeManager & exchangeMgr, const chip::SessionHandle & sessionHandle) {
ChipLogProgress(AppServer,
"CastingPlayer::VerifyOrEstablishConnection() FindOrEstablishSession Connection to CastingPlayer successful");
"CastingPlayer::VerifyOrEstablishConnection() FindOrEstablishSession Connection to "
"CastingPlayer successful");
CastingPlayer::GetTargetCastingPlayer()->mConnectionState = CASTING_PLAYER_CONNECTED;

// this async call will Load all the endpoints with their respective attributes into the TargetCastingPlayer
Expand All @@ -131,7 +132,9 @@ void CastingPlayer::VerifyOrEstablishConnection(ConnectionCallbacks connectionCa
support::EndpointListLoader::GetInstance()->Load();
},
[](void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error) {
ChipLogError(AppServer, "CastingPlayer::VerifyOrEstablishConnection() FindOrEstablishSession Connection to CastingPlayer failed");
ChipLogError(AppServer,
"CastingPlayer::VerifyOrEstablishConnection() FindOrEstablishSession Connection to "
"CastingPlayer failed");
CastingPlayer::GetTargetCastingPlayer()->mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
CHIP_ERROR e = support::CastingStore::GetInstance()->Delete(*CastingPlayer::GetTargetCastingPlayer());
if (e != CHIP_NO_ERROR)
Expand Down Expand Up @@ -197,7 +200,7 @@ CHIP_ERROR CastingPlayer::ContinueConnecting()
"CastingPlayer::ContinueConnecting() mIdOptions.mCommissionerPasscode == false, ContinueConnecting() should "
"only be called when the CastingPlayer/Commissioner-Generated passcode commissioning flow is in progress."););

CHIP_ERROR err = CHIP_NO_ERROR;
CHIP_ERROR err = CHIP_NO_ERROR;
mTargetCastingPlayer = weak_from_this();

ChipLogProgress(AppServer, "CastingPlayer::ContinueConnecting() calling OpenBasicCommissioningWindow()");
Expand Down Expand Up @@ -288,7 +291,7 @@ void CastingPlayer::resetState(CHIP_ERROR err)
void CastingPlayer::Disconnect()
{
ChipLogProgress(AppServer, "CastingPlayer::Disconnect()");
mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
mTargetCastingPlayer.reset();
CastingPlayerDiscovery::GetInstance()->ClearCastingPlayersInternal();
}
Expand Down Expand Up @@ -487,10 +490,14 @@ ConnectionContext::ConnectionContext(void * clientContext, core::CastingPlayer *
[](void * context, chip::Messaging::ExchangeManager & exchangeMgr, const chip::SessionHandle & sessionHandle) {
ChipLogProgress(AppServer, "CastingPlayer::ConnectionContext() Device Connection success callback called");
ConnectionContext * connectionContext = static_cast<ConnectionContext *>(context);
VerifyOrReturn(connectionContext != nullptr && connectionContext->mTargetCastingPlayer != nullptr,
ChipLogError(AppServer, "CastingPlayer::ConnectionContext() Invalid ConnectionContext received in DeviceConnection success callback"));
VerifyOrReturn(
connectionContext != nullptr && connectionContext->mTargetCastingPlayer != nullptr,
ChipLogError(
AppServer,
"CastingPlayer::ConnectionContext() Invalid ConnectionContext received in DeviceConnection success callback"));

ChipLogProgress(AppServer, "CastingPlayer::ConnectionContext() calling mConnectionState = core::CASTING_PLAYER_CONNECTED");
ChipLogProgress(AppServer,
"CastingPlayer::ConnectionContext() calling mConnectionState = core::CASTING_PLAYER_CONNECTED");
connectionContext->mTargetCastingPlayer->mConnectionState = core::CASTING_PLAYER_CONNECTED;
ChipLogProgress(AppServer, "CastingPlayer::ConnectionContext() calling mOnDeviceConnectedFn");
connectionContext->mOnDeviceConnectedFn(connectionContext->mClientContext, exchangeMgr, sessionHandle);
Expand All @@ -501,10 +508,15 @@ ConnectionContext::ConnectionContext(void * clientContext, core::CastingPlayer *

mOnConnectionFailureCallback = new chip::Callback::Callback<chip::OnDeviceConnectionFailure>(
[](void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error) {
ChipLogError(AppServer, "CastingPlayer::ConnectionContext() Device Connection failure callback called with %" CHIP_ERROR_FORMAT, error.Format());
ChipLogError(AppServer,
"CastingPlayer::ConnectionContext() Device Connection failure callback called with %" CHIP_ERROR_FORMAT,
error.Format());
ConnectionContext * connectionContext = static_cast<ConnectionContext *>(context);
VerifyOrReturn(connectionContext != nullptr && connectionContext->mTargetCastingPlayer != nullptr,
ChipLogError(AppServer, "CastingPlayer::ConnectionContext() Invalid ConnectionContext received in DeviceConnection failure callback"));
VerifyOrReturn(
connectionContext != nullptr && connectionContext->mTargetCastingPlayer != nullptr,
ChipLogError(
AppServer,
"CastingPlayer::ConnectionContext() Invalid ConnectionContext received in DeviceConnection failure callback"));
connectionContext->mTargetCastingPlayer->mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
connectionContext->mOnDeviceConnectionFailureFn(connectionContext->mClientContext, peerId, error);
delete connectionContext;
Expand Down
25 changes: 16 additions & 9 deletions examples/tv-casting-app/tv-casting-common/core/CastingPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,25 @@ class CastingPlayer : public std::enable_shared_from_this<CastingPlayer>
/**
* @brief Get the CastingPlayer object targeted currently (may not be connected)
*/
static CastingPlayer * GetTargetCastingPlayer() {
static CastingPlayer * GetTargetCastingPlayer()
{
ChipLogProgress(AppServer, "CastingPlayer::GetTargetCastingPlayer() called");
std::shared_ptr<CastingPlayer> sharedPtr = mTargetCastingPlayer.lock();
CastingPlayer* rawPtr = nullptr;
if (sharedPtr) {
CastingPlayer * rawPtr = nullptr;
if (sharedPtr)
{
rawPtr = sharedPtr.get();
ChipLogProgress(AppServer, "CastingPlayer::GetTargetCastingPlayer() Got rawPtr from mTargetCastingPlayer, sharedPtr reference count: %lu", sharedPtr.use_count());
ChipLogProgress(
AppServer,
"CastingPlayer::GetTargetCastingPlayer() Got rawPtr from mTargetCastingPlayer, sharedPtr reference count: %lu",
sharedPtr.use_count());
sharedPtr.reset();
} else {
ChipLogError(AppServer, "CastingPlayer::GetTargetCastingPlayer() The shared pointer observed by mTargetCastingPlayer has expired (become nullptr)");
}
else
{
ChipLogError(AppServer,
"CastingPlayer::GetTargetCastingPlayer() The shared pointer observed by mTargetCastingPlayer has expired "
"(become nullptr)");
}
return rawPtr;
}
Expand Down Expand Up @@ -272,9 +281,7 @@ class CastingPlayer : public std::enable_shared_from_this<CastingPlayer>
/**
* @brief Return the current state of the CastingPlayer
*/
ConnectionState GetConnectionState() const {
return mConnectionState;
}
ConnectionState GetConnectionState() const { return mConnectionState; }

private:
std::vector<memory::Strong<Endpoint>> mEndpoints;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ CHIP_ERROR CastingPlayerDiscovery::StartDiscovery(uint32_t deviceTypeFilter)

CHIP_ERROR CastingPlayerDiscovery::StopDiscovery()
{
ChipLogProgress(Discovery, "CastingPlayerDiscovery::StopDiscovery() mCastingPlayers: %lu, mCastingPlayersInternal: %lu", mCastingPlayers.size(), mCastingPlayersInternal.size());
ChipLogProgress(Discovery, "CastingPlayerDiscovery::StopDiscovery() mCastingPlayers: %lu, mCastingPlayersInternal: %lu",
mCastingPlayers.size(), mCastingPlayersInternal.size());
VerifyOrReturnError(mState == DISCOVERY_RUNNING, CHIP_ERROR_INCORRECT_STATE);
ReturnErrorOnFailure(mCommissionableNodeController.StopDiscovery());

// Clear mCastingPlayersInternal of disconnected CastingPlayers
ClearDisconnectedCastingPlayersInternal();
for (const auto& player : mCastingPlayers)
for (const auto & player : mCastingPlayers)
{
mCastingPlayersInternal.push_back(player);
}
Expand All @@ -82,21 +83,24 @@ CHIP_ERROR CastingPlayerDiscovery::StopDiscovery()

void CastingPlayerDiscovery::ClearDisconnectedCastingPlayersInternal()
{
ChipLogProgress(Discovery, "CastingPlayerDiscovery::ClearDisconnectedCastingPlayersInternal() mCastingPlayersInternal: %lu", mCastingPlayersInternal.size());
ChipLogProgress(Discovery, "CastingPlayerDiscovery::ClearDisconnectedCastingPlayersInternal() mCastingPlayersInternal: %lu",
mCastingPlayersInternal.size());
// Only clear the CastingPlayers in mCastingPlayersInternal with ConnectionState == CASTING_PLAYER_NOT_CONNECTED
ClearDisconnectedCastingPlayers(mCastingPlayersInternal);
}

void CastingPlayerDiscovery::ClearDisconnectedCastingPlayers(std::vector<std::shared_ptr<CastingPlayer>>& castingPlayers)
void CastingPlayerDiscovery::ClearDisconnectedCastingPlayers(std::vector<std::shared_ptr<CastingPlayer>> & castingPlayers)
{
ChipLogProgress(Discovery, "CastingPlayerDiscovery::ClearDisconnectedCastingPlayers() called");
for (auto it = castingPlayers.begin(); it != castingPlayers.end();)
{
auto& player = *it;
auto & player = *it;
if (player->GetConnectionState() == CASTING_PLAYER_NOT_CONNECTED)
{
ChipLogProgress(Discovery, "CastingPlayerDiscovery::ClearDisconnectedCastingPlayers() Removing disconnected CastingPlayer: %s with reference count: %lu",
player->GetDeviceName(), player.use_count());
ChipLogProgress(Discovery,
"CastingPlayerDiscovery::ClearDisconnectedCastingPlayers() Removing disconnected CastingPlayer: %s "
"with reference count: %lu",
player->GetDeviceName(), player.use_count());
it = castingPlayers.erase(it);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class CastingPlayerDiscovery
*
* @param castingPlayers vector of CastingPlayers to clear
*/
void ClearDisconnectedCastingPlayers(std::vector<std::shared_ptr<CastingPlayer>>& castingPlayers);
void ClearDisconnectedCastingPlayers(std::vector<std::shared_ptr<CastingPlayer>> & castingPlayers);

/**
* @brief Clear all CastingPlayers in mCastingPlayersInternal
Expand All @@ -113,7 +113,8 @@ class CastingPlayerDiscovery

public:
static CastingPlayerDiscovery * GetInstance();
~CastingPlayerDiscovery() {
~CastingPlayerDiscovery()
{
mCastingPlayers.clear();
mCastingPlayersInternal.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ CastingStore * CastingStore::GetInstance()

CHIP_ERROR CastingStore::AddOrUpdate(core::CastingPlayer castingPlayer)
{
ChipLogProgress(AppServer, "CastingStore::AddOrUpdate() called with CastingPlayer deviceName: %s", castingPlayer.GetDeviceName());
ChipLogProgress(AppServer, "CastingStore::AddOrUpdate() called with CastingPlayer deviceName: %s",
castingPlayer.GetDeviceName());

// Read cache of CastingPlayers
std::vector<core::CastingPlayer> castingPlayers = ReadAll();
Expand Down Expand Up @@ -445,7 +446,8 @@ std::vector<core::CastingPlayer> CastingStore::ReadAll()
VerifyOrReturnValue(err == CHIP_NO_ERROR, std::vector<core::CastingPlayer>(),
ChipLogError(AppServer, "TLVReader.ExitContainer failed %" CHIP_ERROR_FORMAT, err.Format()));

ChipLogProgress(AppServer, "CastingStore::ReadAll() CastingPlayers size: %lu", static_cast<unsigned long>(castingPlayers.size()));
ChipLogProgress(AppServer, "CastingStore::ReadAll() CastingPlayers size: %lu",
static_cast<unsigned long>(castingPlayers.size()));
return castingPlayers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void ChipDeviceEventHandler::HandleFailSafeTimerExpired()
sUdcInProgress = false;
CastingPlayer::GetTargetCastingPlayer()->mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
CastingPlayer::GetTargetCastingPlayer()->mOnCompleted(CHIP_ERROR_TIMEOUT, nullptr);
CastingPlayer::GetTargetCastingPlayer()->mOnCompleted = nullptr;
CastingPlayer::GetTargetCastingPlayer()->mOnCompleted = nullptr;
CastingPlayer::GetTargetCastingPlayer()->mTargetCastingPlayer.reset();
return;
}
Expand Down

0 comments on commit 125d6c6

Please sign in to comment.