Skip to content

Commit

Permalink
tv-casting-app fix CloseCommissioningWindow upon unexpected CDC Commi… (
Browse files Browse the repository at this point in the history
  • Loading branch information
pgregorr-amazon authored Oct 10, 2024
1 parent 86c1c71 commit 80a7636
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import com.R;
import com.matter.casting.core.CastingPlayer;
import com.matter.casting.support.CommissionerDeclaration;
Expand Down Expand Up @@ -208,36 +209,41 @@ public void handle(CommissionerDeclaration cd) {
Log.i(TAG, "CastingPlayer CommissionerDeclaration message received: ");
cd.logDetail();

getActivity()
.runOnUiThread(
() -> {
connectionFragmentStatusTextView.setText(
"CommissionerDeclaration message received from Casting Player: \n\n");
if (cd.getCommissionerPasscode()) {
FragmentActivity activity = getActivity();
// Prevent possible NullPointerException. This callback could be called when
// this Fragment is not attached to its host activity or when the fragment's
// lifecycle is not in a valid state for interacting with the activity.
if (activity != null && !activity.isFinishing()) {
activity.runOnUiThread(
() -> {
connectionFragmentStatusTextView.setText(
"CommissionerDeclaration message received from Casting Player: \n\n");
if (cd.getCommissionerPasscode()) {

displayPasscodeInputDialog(getActivity());
displayPasscodeInputDialog(activity);

connectionFragmentStatusTextView.setText(
"CommissionerDeclaration message received from Casting Player: A passcode is now displayed for the user by the Casting Player. \n\n");
}
if (cd.getCancelPasscode()) {
if (useCommissionerGeneratedPasscode) {
connectionFragmentStatusTextView.setText(
"CommissionerDeclaration message received from Casting Player: A passcode is now displayed for the user by the Casting Player. \n\n");
}
if (cd.getCancelPasscode()) {
if (useCommissionerGeneratedPasscode) {
connectionFragmentStatusTextView.setText(
"CastingPlayer/Commissioner-Generated passcode connection attempt cancelled by the CastingPlayer/Commissioner user. \n\nRoute back to exit. \n\n");
} else {
connectionFragmentStatusTextView.setText(
"Connection attempt cancelled by the CastingPlayer/Commissioner user. \n\nRoute back to exit. \n\n");
}
if (passcodeDialog != null && passcodeDialog.isShowing()) {
passcodeDialog.dismiss();
}
"CastingPlayer/Commissioner-Generated passcode connection attempt cancelled by the CastingPlayer/Commissioner user. \n\nRoute back to exit. \n\n");
} else {
connectionFragmentStatusTextView.setText(
"Connection attempt cancelled by the CastingPlayer/Commissioner user. \n\nRoute back to exit. \n\n");
}
if (cd.getErrorCode() != CommissionerDeclaration.CdError.noError) {
commissionerDeclarationErrorTextView.setText(
"CommissionerDeclaration error from CastingPlayer: "
+ cd.getErrorCode().getDescription());
if (passcodeDialog != null && passcodeDialog.isShowing()) {
passcodeDialog.dismiss();
}
});
}
if (cd.getErrorCode() != CommissionerDeclaration.CdError.noError) {
commissionerDeclarationErrorTextView.setText(
"CommissionerDeclaration error from CastingPlayer: "
+ cd.getErrorCode().getDescription());
}
});
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion examples/tv-casting-app/linux/simple-app-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ void ConnectionHandler(CHIP_ERROR err, matter::casting::core::CastingPlayer * ca
kDesiredEndpointVendorId);
}

ChipLogProgress(AppServer, "simple-app-helper.cpp::ConnectionHandler(): Getting endpoints avaiable for demo interactions");
ChipLogProgress(AppServer, "simple-app-helper.cpp::ConnectionHandler(): Getting endpoints available for demo interactions");
std::vector<matter::casting::memory::Strong<matter::casting::core::Endpoint>> endpoints = castingPlayer->GetEndpoints();
LogEndpointsDetails(endpoints);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,34 @@ CommissionerDeclarationHandler * CommissionerDeclarationHandler::GetInstance()
return sCommissionerDeclarationHandler_;
}

// TODO: In the following PRs. Implement setHandler() for CommissionerDeclaration messages and expose messages to higher layers for
// Linux, Android and iOS.
void CommissionerDeclarationHandler::OnCommissionerDeclarationMessage(
const chip::Transport::PeerAddress & source, chip::Protocols::UserDirectedCommissioning::CommissionerDeclaration cd)
{
ChipLogProgress(AppServer,
"CommissionerDeclarationHandler::OnCommissionerDeclarationMessage(), calling CloseCommissioningWindow()");
// Close the commissioning window. Since we recived a CommissionerDeclaration message from the Commissioner, we know that
// commissioning via AccountLogin cluster failed. We will open a new commissioningWindow prior to sending the next
// IdentificationDeclaration Message to the Commissioner.
chip::Server::GetInstance().GetCommissioningWindowManager().CloseCommissioningWindow();
support::ChipDeviceEventHandler::SetUdcStatus(false);
ChipLogProgress(AppServer, "CommissionerDeclarationHandler::OnCommissionerDeclarationMessage()");

// During UDC with CastingPlayer/Commissioner-Generated Passcode, the Commissioner responds with a CommissionerDeclaration
// message with CommissionerPasscode set to true. The CommissionerPasscode flag indicates that a Passcode is now displayed for
// the user by the CastingPlayer /Commissioner. With this CommissionerDeclaration message, we also know that commissioning via
// AccountLogin cluster has failed. Therefore, we close the commissioning window. We will open a new commissioning window prior
// to sending the next/2nd IdentificationDeclaration message to the Commissioner.
if (cd.GetCommissionerPasscode())
{
ChipLogProgress(AppServer,
"CommissionerDeclarationHandler::OnCommissionerDeclarationMessage(), calling CloseCommissioningWindow()");
// Close the commissioning window.
chip::Server::GetInstance().GetCommissioningWindowManager().CloseCommissioningWindow();
support::ChipDeviceEventHandler::SetUdcStatus(false);
}

// Flag to indicate when the CastingPlayer/Commissioner user has decided to exit the commissioning process.
if (cd.GetCancelPasscode())
{
ChipLogProgress(AppServer,
"CommissionerDeclarationHandler::OnCommissionerDeclarationMessage(), Got CancelPasscode parameter, "
"CastingPlayer/Commissioner user has decided to exit the commissioning attempt. Connection aborted.");
// Close the commissioning window.
chip::Server::GetInstance().GetCommissioningWindowManager().CloseCommissioningWindow();
support::ChipDeviceEventHandler::SetUdcStatus(false);
// Since the CastingPlayer/Commissioner user has decided to exit the commissioning process, we cancel the ongoing
// connection attempt without notifying the CastingPlayer/Commissioner. Therefore the
// shouldSendIdentificationDeclarationMessage flag in the internal StopConnecting() API call is set to false. The
Expand All @@ -65,12 +74,7 @@ void CommissionerDeclarationHandler::OnCommissionerDeclarationMessage(
// and user 2 might be controlling the CastingPlayer/Commissioner TV.
CastingPlayer * targetCastingPlayer = CastingPlayer::GetTargetCastingPlayer();
// Avoid crashing if we recieve this CommissionerDeclaration message when targetCastingPlayer is nullptr.
if (targetCastingPlayer == nullptr)
{
ChipLogError(AppServer,
"CommissionerDeclarationHandler::OnCommissionerDeclarationMessage() targetCastingPlayer is nullptr");
}
else
if (targetCastingPlayer != nullptr)
{
CHIP_ERROR err = targetCastingPlayer->StopConnecting(false);
if (err != CHIP_NO_ERROR)
Expand All @@ -82,6 +86,11 @@ void CommissionerDeclarationHandler::OnCommissionerDeclarationMessage(
err.Format());
}
}
else
{
ChipLogError(AppServer,
"CommissionerDeclarationHandler::OnCommissionerDeclarationMessage() targetCastingPlayer is nullptr");
}
}

if (mCmmissionerDeclarationCallback_)
Expand Down

0 comments on commit 80a7636

Please sign in to comment.