Skip to content

Commit 5d31461

Browse files
[Amazon] StopConnect for UDC in Android tv-casting-app
Updated Android & iOS tv-casting-app to send stopConnect command when user exits UDC before confirming passcode Changes 1. Added IsPendingPasscodeFromUser() to CastingPlayer. This function will return true if we are still connecting and pending user action for passcode 2. ConnectionExampleFragment.java onDestroy() calls stopConnect() if user has not confirmed passcode 4. MCConnectionExampleView ConnectingView dissapear calls stopConnect() if user has not confirmed passcode Test 1. Manually verified UDC attempt is successful both Android iOS for following 1. Attempt UDC attempt (both commissionee & commissioner generated passcode) 2. Navigate back to discovery page 3. Start new UDC attempt (both commissionee & commissioner generated passcode) 2. Manually verified UDC attempt is successful both Android iOS for following. UDC will finish in the background. 1. Attempt UDC attempt (both commissionee & commissioner generated passcode) 2. Confirm passcode on TV app or casting app depending on who generated passcode 3. Navigate back before commission fnishes. 3. Manual regression test following * UDC attempt happy path (no navigate back) for both commissinee & commissioner generated passcode
1 parent 9d99ef2 commit 5d31461

File tree

10 files changed

+84
-5
lines changed

10 files changed

+84
-5
lines changed

examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ConnectionExampleFragment.java

+16
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,22 @@ public void handle(CommissionerDeclaration cd) {
262262
});
263263
}
264264

265+
@Override
266+
public void onDestroy() {
267+
super.onDestroy();
268+
269+
// Only stop connection if we are pending passcode confirmation
270+
// We cannot stop connection once continueConnecting() is called
271+
if (targetCastingPlayer.isPendingPasscodeFromUser()) {
272+
MatterError err = targetCastingPlayer.stopConnecting();
273+
if (err.hasError()) {
274+
Log.e(
275+
TAG,
276+
"Going back before connection finishes but stopConnecting() failed due to: " + err);
277+
}
278+
}
279+
}
280+
265281
private void displayPasscodeInputDialog(Context context) {
266282
AlertDialog.Builder builder = new AlertDialog.Builder(context);
267283

examples/tv-casting-app/android/App/app/src/main/jni/com/matter/casting/core/CastingPlayer.java

+6
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,10 @@ MatterError verifyOrEstablishConnection(
152152

153153
/** @brief Sets the internal connection state of this CastingPlayer to "disconnected" */
154154
void disconnect();
155+
156+
/**
157+
* @return true if this CastingPlayer is still pending pass code from user and therefore is not
158+
* ready
159+
*/
160+
boolean isPendingPasscodeFromUser();
155161
}

examples/tv-casting-app/android/App/app/src/main/jni/com/matter/casting/core/MatterCastingPlayer.java

+3
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,7 @@ public MatterError continueConnecting() {
267267

268268
@Override
269269
public native void disconnect();
270+
271+
@Override
272+
public native boolean isPendingPasscodeFromUser();
270273
}

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

+12
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,18 @@ JNI_METHOD(void, disconnect)
165165
castingPlayer->Disconnect();
166166
}
167167

168+
JNI_METHOD(bool, isPendingPasscodeFromUser)
169+
(JNIEnv * env, jobject thiz)
170+
{
171+
chip::DeviceLayer::StackLock lock;
172+
ChipLogProgress(AppServer, "MatterCastingPlayer-JNI::isPendingPasscodeFromUser()");
173+
174+
CastingPlayer * castingPlayer = support::convertCastingPlayerFromJavaToCpp(thiz);
175+
VerifyOrReturnValue(castingPlayer != nullptr, support::convertMatterErrorFromCppToJava(CHIP_ERROR_INVALID_ARGUMENT));
176+
177+
return castingPlayer->IsPendingPasscodeFromUser();
178+
}
179+
168180
JNI_METHOD(jobject, getEndpoints)
169181
(JNIEnv * env, jobject thiz)
170182
{

examples/tv-casting-app/darwin/MatterTvCastingBridge/MatterTvCastingBridge/MCCastingPlayer.h

+5
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@
148148
*/
149149
- (void)disconnect;
150150

151+
/**
152+
* @return true if this CastingPlayer is still pending pass code from user and therefore is not ready
153+
*/
154+
- (bool)isPendingPasscodeFromUser;
155+
151156
- (NSString * _Nonnull)identifier;
152157
- (NSString * _Nonnull)deviceName;
153158
- (uint16_t)vendorId;

examples/tv-casting-app/darwin/MatterTvCastingBridge/MatterTvCastingBridge/MCCastingPlayer.mm

+14
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,20 @@ - (void)disconnect
190190
});
191191
}
192192

193+
- (bool)isPendingPasscodeFromUser
194+
{
195+
ChipLogProgress(AppServer, "MCCastingPlayer.isPendingPasscodeFromUser() called");
196+
VerifyOrReturnValue([[MCCastingApp getSharedInstance] isRunning], false, ChipLogError(AppServer, "MCCastingPlayer.isPendingPasscodeFromUser() MCCastingApp NOT running"));
197+
198+
dispatch_queue_t workQueue = [[MCCastingApp getSharedInstance] getWorkQueue];
199+
200+
__block bool isPending = false;
201+
dispatch_sync(workQueue, ^{
202+
isPending = _cppCastingPlayer->IsPendingPasscodeFromUser();
203+
});
204+
return isPending;
205+
}
206+
193207
- (instancetype _Nonnull)initWithCppCastingPlayer:(matter::casting::memory::Strong<matter::casting::core::CastingPlayer>)cppCastingPlayer
194208
{
195209
if (self = [super init]) {

examples/tv-casting-app/darwin/TvCasting/TvCasting/MCConnectionExampleView.swift

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ struct MCConnectionExampleView: View {
8888
viewModel.connect(selectedCastingPlayer: self.selectedCastingPlayer, useCommissionerGeneratedPasscode: self.useCommissionerGeneratedPasscode)
8989
}
9090
})
91+
.onDisappear(perform: {
92+
viewModel.cancelConnectionAttempt(selectedCastingPlayer: self.selectedCastingPlayer)
93+
})
9194
}
9295
}
9396

examples/tv-casting-app/darwin/TvCasting/TvCasting/MCConnectionExampleViewModel.swift

+17
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ class MCConnectionExampleViewModel: ObservableObject {
4242

4343
@Published var errorCodeDescription: String?
4444

45+
func cancelConnectionAttempt(selectedCastingPlayer: MCCastingPlayer?) {
46+
DispatchQueue.main.async {
47+
// Only stop connection if we are pending passcode confirmation
48+
if selectedCastingPlayer?.isPendingPasscodeFromUser() == true {
49+
self.Log.info("MCConnectionExampleViewModel cancelConnect(). User navigating back from ConnectionView")
50+
let err = selectedCastingPlayer?.stopConnecting()
51+
if err == nil {
52+
self.connectionStatus = "User cancelled the connection attempt with CastingPlayer.stopConnecting()."
53+
self.Log.info("MCConnectionExampleViewModel cancelConnect() MCCastingPlayer.stopConnecting() succeeded.")
54+
} else {
55+
self.connectionStatus = "Cancel connection failed due to: \(String(describing: err))."
56+
self.Log.error("MCConnectionExampleViewModel cancelConnect() MCCastingPlayer.stopConnecting() failed due to: \(err)")
57+
}
58+
}
59+
}
60+
}
61+
4562
func connect(selectedCastingPlayer: MCCastingPlayer?, useCommissionerGeneratedPasscode: Bool) {
4663
self.Log.info("MCConnectionExampleViewModel.connect() useCommissionerGeneratedPasscode: \(String(describing: useCommissionerGeneratedPasscode))")
4764

examples/tv-casting-app/tv-casting-common/core/CastingPlayer.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,6 @@ CHIP_ERROR CastingPlayer::ContinueConnecting()
228228

229229
CHIP_ERROR CastingPlayer::StopConnecting()
230230
{
231-
VerifyOrReturnValue(
232-
mIdOptions.mCommissionerPasscode, CHIP_ERROR_INCORRECT_STATE,
233-
ChipLogError(AppServer,
234-
"CastingPlayer::StopConnecting() mIdOptions.mCommissionerPasscode == false, ContinueConnecting() should only "
235-
"be called when the CastingPlayer/Commissioner-Generated passcode commissioning flow is in progress."););
236231
// Calling the internal StopConnecting() API with the shouldSendIdentificationDeclarationMessage set to true to notify the
237232
// CastingPlayer/Commissioner that the commissioning session was cancelled by the Casting Client/Commissionee user. This will
238233
// result in the Casting Client/Commissionee sending a CancelPasscode IdentificationDeclaration message to the CastingPlayer.

examples/tv-casting-app/tv-casting-common/core/CastingPlayer.h

+8
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ class CastingPlayer : public std::enable_shared_from_this<CastingPlayer>
142142
*/
143143
bool IsConnected() const { return mConnectionState == CASTING_PLAYER_CONNECTED; }
144144

145+
/**
146+
* @return true if this CastingPlayer is still pending pass code from user and therefore is not ready
147+
*/
148+
bool IsPendingPasscodeFromUser() const
149+
{
150+
return mConnectionState == CASTING_PLAYER_CONNECTING && !mIdOptions.mCommissionerPasscodeReady;
151+
}
152+
145153
/**
146154
* @brief Verifies that a connection exists with this CastingPlayer, or triggers a new commissioning session request. If the
147155
* CastingApp does not have the nodeId and fabricIndex of this CastingPlayer cached on disk, this will execute the User Directed

0 commit comments

Comments
 (0)