Skip to content

Commit

Permalink
Linux tv-casting-app v1.3 Commissioner-Generated passcode flow (#33479)
Browse files Browse the repository at this point in the history
* Linux tv-casting-app v1.3 Commissioner-Generated passcode flow

* Fixing style issues

* Addressed comments by andy31415 and tcarmelveilleux

* Fixing style issue

* Addressed comments by andy31415 and chrisdecenzo
  • Loading branch information
pgregorr-amazon authored and pull[bot] committed Oct 3, 2024
1 parent a73c24f commit 2415408
Show file tree
Hide file tree
Showing 17 changed files with 631 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "core/CastingApp.h" // from tv-casting-common
#include "core/CastingPlayer.h" // from tv-casting-common
#include "core/CastingPlayerDiscovery.h" // from tv-casting-common
#include "core/ConnectionCallbacks.h" // from tv-casting-common

#include <app/clusters/bindings/BindingManager.h>
#include <app/server/Server.h>
Expand Down Expand Up @@ -92,24 +93,36 @@ JNI_METHOD(jobject, verifyOrEstablishConnection)
MatterCastingPlayerJNIMgr().mConnectionSuccessHandler.SetUp(env, jSuccessCallback);
MatterCastingPlayerJNIMgr().mConnectionFailureHandler.SetUp(env, jFailureCallback);

// TODO: In the following PRs. Add optional CommissionerDeclarationHandler callback parameter.
castingPlayer->VerifyOrEstablishConnection(
[](CHIP_ERROR err, CastingPlayer * playerPtr) {
ChipLogProgress(AppServer, "MatterCastingPlayer-JNI::verifyOrEstablishConnection() ConnectCallback called");
if (err == CHIP_NO_ERROR)
{
ChipLogProgress(AppServer, "MatterCastingPlayer-JNI:: Connected to Casting Player with device ID: %s",
playerPtr->GetId());
MatterCastingPlayerJNIMgr().mConnectionSuccessHandler.Handle(nullptr);
}
else
{
ChipLogError(AppServer, "MatterCastingPlayer-JNI:: ConnectCallback, connection error: %" CHIP_ERROR_FORMAT,
err.Format());
MatterCastingPlayerJNIMgr().mConnectionFailureHandler.Handle(err);
}
},
static_cast<unsigned long long int>(commissioningWindowTimeoutSec), idOptions);
auto connectCallback = [](CHIP_ERROR err, CastingPlayer * playerPtr) {
ChipLogProgress(AppServer, "MatterCastingPlayer-JNI::verifyOrEstablishConnection() ConnectCallback()");
if (err == CHIP_NO_ERROR)
{
ChipLogProgress(AppServer,
"MatterCastingPlayer-JNI::verifyOrEstablishConnection() ConnectCallback() Connected to Casting Player "
"with device ID: %s",
playerPtr->GetId());
// The Java jSuccessCallback is expecting a Void v callback parameter which translates to a nullptr. When calling the
// Java method from C++ via JNI, passing nullptr is equivalent to passing a Void object in Java.
MatterCastingPlayerJNIMgr().mConnectionSuccessHandler.Handle(nullptr);
}
else
{
ChipLogError(
AppServer,
"MatterCastingPlayer-JNI::verifyOrEstablishConnection() ConnectCallback() Connection error: %" CHIP_ERROR_FORMAT,
err.Format());
MatterCastingPlayerJNIMgr().mConnectionFailureHandler.Handle(err);
}
};

// TODO: In the following PRs. Add optional CommissionerDeclarationHandler callback parameter for the Commissioner-Generated
// passcode commissioning flow.
matter::casting::core::ConnectionCallbacks connectionCallbacks;
connectionCallbacks.mOnConnectionComplete = connectCallback;

// TODO: Verify why commissioningWindowTimeoutSec is a "unsigned long long int" type. Seems too big.
castingPlayer->VerifyOrEstablishConnection(connectionCallbacks,
static_cast<unsigned long long int>(commissioningWindowTimeoutSec), idOptions);
return support::convertMatterErrorFromCppToJava(CHIP_NO_ERROR);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#import "MCEndpoint_Internal.h"
#import "MCErrorUtils.h"

#import "core/CastingPlayer.h"
#import "core/CastingPlayer.h" // from tv-casting-common
#import "core/ConnectionCallbacks.h" // from tv-casting-common

#import <Foundation/Foundation.h>

Expand All @@ -47,7 +48,7 @@ - (void)verifyOrEstablishConnectionWithCompletionBlock:(void (^_Nonnull)(NSError

- (void)verifyOrEstablishConnectionWithCompletionBlock:(void (^_Nonnull)(NSError * _Nullable))completion timeout:(long long)timeout desiredEndpointFilter:(MCEndpointFilter * _Nullable)desiredEndpointFilter
{
ChipLogProgress(AppServer, "MCCastingPlayer.verifyOrEstablishConnectionWithCompletionBlock called");
ChipLogProgress(AppServer, "MCCastingPlayer.verifyOrEstablishConnectionWithCompletionBlock() called");
VerifyOrReturn([[MCCastingApp getSharedInstance] isRunning], ChipLogError(AppServer, "MCCastingApp NOT running"));

dispatch_queue_t workQueue = [[MCCastingApp getSharedInstance] getWorkQueue];
Expand All @@ -63,18 +64,23 @@ - (void)verifyOrEstablishConnectionWithCompletionBlock:(void (^_Nonnull)(NSError

CHIP_ERROR result = idOptions.addTargetAppInfo(targetAppInfo);
if (result != CHIP_NO_ERROR) {
ChipLogError(AppServer, "MCCastingPlayer.verifyOrEstablishConnectionWithCompletionBlock failed to add targetAppInfo: %" CHIP_ERROR_FORMAT, result.Format());
ChipLogError(AppServer, "MCCastingPlayer.verifyOrEstablishConnectionWithCompletionBlock() failed to add targetAppInfo: %" CHIP_ERROR_FORMAT, result.Format());
}
}

// TODO: In the following PRs. Add optional CommissionerDeclarationHandler callback parameter.
_cppCastingPlayer->VerifyOrEstablishConnection(
[completion](CHIP_ERROR err, matter::casting::core::CastingPlayer * castingPlayer) {
dispatch_queue_t clientQueue = [[MCCastingApp getSharedInstance] getClientQueue];
dispatch_async(clientQueue, ^{
completion(err == CHIP_NO_ERROR ? nil : [MCErrorUtils NSErrorFromChipError:err]);
});
}, timeout, idOptions);
void (^connectCallback)(CHIP_ERROR, matter::casting::core::CastingPlayer *) = ^(CHIP_ERROR err, matter::casting::core::CastingPlayer * castingPlayer) {
ChipLogProgress(AppServer, "MCCastingPlayer.verifyOrEstablishConnectionWithCompletionBlock() ConnectCallback()");
dispatch_queue_t clientQueue = [[MCCastingApp getSharedInstance] getClientQueue];
dispatch_async(clientQueue, ^{
completion(err == CHIP_NO_ERROR ? nil : [MCErrorUtils NSErrorFromChipError:err]);
});
};

matter::casting::core::ConnectionCallbacks connectionCallbacks;
connectionCallbacks.mOnConnectionComplete = connectCallback;

// TODO: In the following PRs. Add optional CommissionerDeclarationHandler callback parameter for the Commissioner-Generated passcode commissioning flow.
_cppCastingPlayer->VerifyOrEstablishConnection(connectionCallbacks, timeout, idOptions);
});
}

Expand Down
Loading

0 comments on commit 2415408

Please sign in to comment.