Skip to content

Commit

Permalink
WriteAll fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Jan 4, 2024
1 parent c5637ce commit 1a80c2e
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@

@interface MTRCastingPlayer : NSObject

/**
minimum matter::casting::core::kCommissioningWindowTimeoutSec
*/
- (void)verifyOrEstablishConnectionWithCompletionBlock:(void (^_Nonnull)(NSError * _Nullable))completion timeout:(long long)timeout desiredEndpointFilter:(MTREndpointFilter * _Nullable)desiredEndpointFilter;

- (void)verifyOrEstablishConnectionWithCompletionBlock:(void (^_Nonnull)(NSError * _Nullable))completion desiredEndpointFilter:(MTREndpointFilter * _Nullable)desiredEndpointFilter;

-(void)disconnect;

- (NSString * _Nonnull)identifier;
- (NSString * _Nonnull)deviceName;
- (uint16_t)vendorId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ - (void)verifyOrEstablishConnectionWithCompletionBlock:(void (^_Nonnull)(NSError

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

dispatch_queue_t workQueue = [[MTRCastingApp getSharedInstance] getWorkQueue];
Expand All @@ -60,6 +60,17 @@ - (void)verifyOrEstablishConnectionWithCompletionBlock:(void (^_Nonnull)(NSError
});
}

- (void)disconnect
{
ChipLogProgress(AppServer, "MTRCastingPlayer.disconnect called");
VerifyOrReturn([[MTRCastingApp getSharedInstance] isRunning], ChipLogError(AppServer, "MTRCastingApp NOT running"));

dispatch_queue_t workQueue = [[MTRCastingApp getSharedInstance] getWorkQueue];
dispatch_sync(workQueue, ^{
_cppCastingPlayer->Disconnect();
});
}

- (NSString * _Nonnull)description
{
return [NSString stringWithFormat:@"%@ with Product ID: %d and Vendor ID: %d. Resolved IPAddr?: %@",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@
{
VerifyOrReturnError(mDataSource != nullptr, CHIP_ERROR_INCORRECT_STATE);

NSData * csrData = [NSData dataWithBytes:messageToSign.data() length:messageToSign.size()];
__block NSData * signedData = [NSData dataWithBytes:outSignatureBuffer.data() length:outSignatureBuffer.size()];
__block NSData * csrData = [NSData dataWithBytes:messageToSign.data() length:messageToSign.size()];
__block MatterError * err = nil;
dispatch_sync(mDataSource.clientQueue, ^{
err = [mDataSource castingApp:@"MTRDeviceAttestationCredentialsProvider.SignWithDeviceAttestationKey()"
Expand Down
28 changes: 22 additions & 6 deletions examples/tv-casting-app/tv-casting-common/core/CastingApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ CHIP_ERROR CastingApp::Start()
// perform post server startup registrations
ReturnErrorOnFailure(PostStartRegistrations());

// reconnect (or verify connection) to the CastingPlayer that the app was connected to before being stopped, if any
if(CastingPlayer::GetTargetCastingPlayer() != nullptr)
{
CastingPlayer::GetTargetCastingPlayer()->VerifyOrEstablishConnection(
[](CHIP_ERROR err, matter::casting::core::CastingPlayer * castingPlayer)
{
if(err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "CastingApp::Start Could not reconnect to CastingPlayer(ID: %s) %" CHIP_ERROR_FORMAT, castingPlayer->GetId(),
err.Format());
}
else
{
ChipLogProgress(AppServer, "CastingApp::Start Reconnected to CastingPlayer(ID: %s)", castingPlayer->GetId());
}
}
);
}

return CHIP_NO_ERROR;
}

Expand All @@ -113,8 +132,8 @@ CHIP_ERROR CastingApp::PostStartRegistrations()
chip::BindingManager::GetInstance().Init(
{ &server.GetFabricTable(), server.GetCASESessionManager(), &server.GetPersistentStorage() });

// TODO: Set FabricDelegate
// chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&mPersistenceManager);
// Set FabricDelegate
chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(support::CastingStore::GetInstance());

// Register DeviceEvent Handler
ReturnErrorOnFailure(chip::DeviceLayer::PlatformMgrImpl().AddEventHandler(ChipDeviceEventHandler::Handle, 0));
Expand All @@ -128,15 +147,12 @@ CHIP_ERROR CastingApp::Stop()
ChipLogProgress(Discovery, "CastingApp::Stop() called");
VerifyOrReturnError(mState == CASTING_APP_RUNNING, CHIP_ERROR_INCORRECT_STATE);

// TODO: add logic to capture CastingPlayers that we are currently connected to, so we can automatically reconnect with them on
// Start() again

// Shutdown the Matter server
chip::Server::GetInstance().Shutdown();

mState = CASTING_APP_NOT_RUNNING; // CastingApp stopped successfully, set state to NOT_RUNNING

return CHIP_ERROR_NOT_IMPLEMENTED;
return CHIP_NO_ERROR;
}

CHIP_ERROR CastingApp::ShutdownAllSubscriptions()
Expand Down
4 changes: 3 additions & 1 deletion examples/tv-casting-app/tv-casting-common/core/CastingApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ class CastingApp
/**
* @brief Starts the Matter server that the CastingApp runs on and registers all the necessary delegates
* CastingApp.
* If the CastingApp was previously connected to a CastingPlayer and then Stopped by calling the Stop()
* API, it will re-connect to the CastingPlayer.
*
* @return CHIP_ERROR - CHIP_NO_ERROR if Matter server started successfully, specific error code otherwise.
*/
CHIP_ERROR Start();

/**
* @brief Stops the Matter server that the CastingApp runs on
* @brief Stops the Matter server that the CastingApp runs on.
*
* @return CHIP_ERROR - CHIP_NO_ERROR if Matter server stopped successfully, specific error code otherwise.
*/
Expand Down
14 changes: 13 additions & 1 deletion examples/tv-casting-app/tv-casting-common/core/CastingPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void CastingPlayer::VerifyOrEstablishConnection(ConnectCallback onCompleted, uns
unsigned index = (unsigned int) std::distance(cachedCastingPlayers.begin(), it);
if (ContainsDesiredEndpoint(&cachedCastingPlayers[index], desiredEndpointFilter))
{
ChipLogProgress(AppServer, "CastingPlayer::VerifyOrEstablishConnection calling FindOrEstablishSession on cached CastingPlayer");
*this = cachedCastingPlayers[index];

FindOrEstablishSession(
Expand All @@ -80,7 +81,12 @@ void CastingPlayer::VerifyOrEstablishConnection(ConnectCallback onCompleted, uns
[](void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error) {
ChipLogError(AppServer, "CastingPlayer::VerifyOrEstablishConnection Connection to CastingPlayer failed");
CastingPlayer::GetTargetCastingPlayer()->mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
support::CastingStore::GetInstance()->Delete(*CastingPlayer::GetTargetCastingPlayer());
CHIP_ERROR err = support::CastingStore::GetInstance()->Delete(*CastingPlayer::GetTargetCastingPlayer());
if(err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "CastingStore::Delete() failed. Err: %" CHIP_ERROR_FORMAT, err.Format());
}

VerifyOrReturn(CastingPlayer::GetTargetCastingPlayer()->mOnCompleted);
CastingPlayer::GetTargetCastingPlayer()->mOnCompleted(error, nullptr);
mTargetCastingPlayer = nullptr;
Expand Down Expand Up @@ -123,6 +129,12 @@ void CastingPlayer::VerifyOrEstablishConnection(ConnectCallback onCompleted, uns
}
}

void CastingPlayer::Disconnect()
{
mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
mTargetCastingPlayer = nullptr;
}

void CastingPlayer::RegisterEndpoint(const memory::Strong<Endpoint> endpoint)
{
auto it = std::find_if(mEndpoints.begin(), mEndpoints.end(), [endpoint](const memory::Strong<Endpoint> & _endpoint) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ class CastingPlayer : public std::enable_shared_from_this<CastingPlayer>
unsigned long long int commissioningWindowTimeoutSec = kCommissioningWindowTimeoutSec,
EndpointFilter desiredEndpointFilter = EndpointFilter());

void Disconnect();

/**
* @brief Find an existing session for this CastingPlayer, or trigger a new session
* request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void DeviceDiscoveryDelegateImpl::OnDiscoveredDevice(const chip::Dnssd::Discover
{
ChipLogProgress(Discovery, "DeviceDiscoveryDelegateImpl::OnDiscoveredDevice() called");
VerifyOrReturn(mClientDelegate != nullptr,
ChipLogError(NotSpecified, "CastingPlayerDeviceDiscoveryDelegate, mClientDelegate is a nullptr"));
ChipLogError(Discovery, "DeviceDiscoveryDelegateImpl::OnDiscoveredDevice mClientDelegate is a nullptr"));

// convert nodeData to CastingPlayer
CastingPlayerAttributes attributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ CHIP_ERROR CastingStore::WriteAll(std::vector<core::CastingPlayer> castingPlayer
{
chip::TLV::TLVType castingPlayerContainerType = chip::TLV::kTLVType_Structure;
// CastingPlayer container starts
ReturnErrorOnFailure(tlvWriter.StartContainer(chip::TLV::ContextTag(kCastingPlayerContainerTag),
ReturnErrorOnFailure(tlvWriter.StartContainer(chip::TLV::AnonymousTag(),
chip::TLV::kTLVType_Structure, castingPlayerContainerType));

ReturnErrorOnFailure(tlvWriter.Put(chip::TLV::ContextTag(kCastingPlayerIdTag), castingPlayer.GetId()));
Expand All @@ -489,7 +489,7 @@ CHIP_ERROR CastingStore::WriteAll(std::vector<core::CastingPlayer> castingPlayer
{
chip::TLV::TLVType endpointContainerType = chip::TLV::kTLVType_Structure;
// Endpoint container starts
ReturnErrorOnFailure(tlvWriter.StartContainer(chip::TLV::ContextTag(kCastingPlayerEndpointContainerTag),
ReturnErrorOnFailure(tlvWriter.StartContainer(chip::TLV::AnonymousTag(),
chip::TLV::kTLVType_Structure, endpointContainerType));
ReturnErrorOnFailure(tlvWriter.Put(chip::TLV::ContextTag(kCastingPlayerEndpointIdTag), endpoint->GetId()));
ReturnErrorOnFailure(tlvWriter.Put(chip::TLV::ContextTag(kCastingPlayerEndpointVendorIdTag), endpoint->GetVendorId()));
Expand All @@ -507,7 +507,7 @@ CHIP_ERROR CastingStore::WriteAll(std::vector<core::CastingPlayer> castingPlayer
chip::TLV::TLVType deviceTypeStructContainerType = chip::TLV::kTLVType_Structure;
// DeviceTypeStruct container starts
ReturnErrorOnFailure(
tlvWriter.StartContainer(chip::TLV::ContextTag(kCastingPlayerEndpointDeviceTypeStructContainerTag),
tlvWriter.StartContainer(chip::TLV::AnonymousTag(),
chip::TLV::kTLVType_Structure, deviceTypeStructContainerType));
ReturnErrorOnFailure(
tlvWriter.Put(chip::TLV::ContextTag(kCastingPlayerEndpointDeviceTypeTag), deviceTypeStruct.deviceType));
Expand All @@ -523,7 +523,7 @@ CHIP_ERROR CastingStore::WriteAll(std::vector<core::CastingPlayer> castingPlayer
// ServerList container starts
chip::TLV::TLVType serverListContainerType = chip::TLV::kTLVType_Array;
ReturnErrorOnFailure(tlvWriter.StartContainer(chip::TLV::ContextTag(kCastingPlayerEndpointServerListContainerTag),
chip::TLV::kTLVType_Array, serverListContainerType));
chip::TLV::kTLVType_Structure, serverListContainerType));
std::vector<chip::ClusterId> serverList = endpoint->GetServerList();
for (chip::ClusterId clusterId : serverList)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ void ChipDeviceEventHandler::Handle(const chip::DeviceLayer::ChipDeviceEvent * e
[](void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error) {
ChipLogError(AppServer, "ChipDeviceEventHandler::Handle: Connection to CastingPlayer failed");
CastingPlayer::GetTargetCastingPlayer()->mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
support::CastingStore::GetInstance()->Delete(*CastingPlayer::GetTargetCastingPlayer());
CHIP_ERROR err = support::CastingStore::GetInstance()->Delete(*CastingPlayer::GetTargetCastingPlayer());
if(err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "CastingStore::Delete() failed. Err: %" CHIP_ERROR_FORMAT, err.Format());
}

VerifyOrReturn(CastingPlayer::GetTargetCastingPlayer()->mOnCompleted);
CastingPlayer::GetTargetCastingPlayer()->mOnCompleted(error, nullptr);
CastingPlayer::mTargetCastingPlayer = nullptr;
Expand Down Expand Up @@ -187,6 +192,17 @@ void ChipDeviceEventHandler::HandleCommissioningComplete(const chip::DeviceLayer
runPostCommissioning = true;
}

CHIP_ERROR ChipDeviceEventHandler::SetUdcStatus(bool udcInProgress)
{
if (sUdcInProgress == udcInProgress)
{
ChipLogError(AppServer, "UDC in progress state is already %d", sUdcInProgress);
return CHIP_ERROR_INCORRECT_STATE;
}
sUdcInProgress = udcInProgress;
return CHIP_NO_ERROR;
}

}; // namespace support
}; // namespace casting
}; // namespace matter
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,7 @@ class ChipDeviceEventHandler
* If UDC was already in progress when this method was called, it will return a CHIP_ERROR_INCORRECT_STATE without changing the
* internal state.
*/
static CHIP_ERROR SetUdcStatus(bool udcInProgress)
{
if (sUdcInProgress == udcInProgress)
{
ChipLogError(AppServer, "UDC in progress state is already %d", sUdcInProgress);
return CHIP_ERROR_INCORRECT_STATE;
}
sUdcInProgress = udcInProgress;
return CHIP_NO_ERROR;
}
static CHIP_ERROR SetUdcStatus(bool udcInProgress);

private:
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,14 @@ void EndpointListLoader::Complete()
mSessionHandle = nullptr;
mNewEndpointsToLoad = 0;

// done loading endpoints, callback client OnCompleted
support::CastingStore::GetInstance()->AddOrUpdate(*CastingPlayer::GetTargetCastingPlayer());
// done loading endpoints, store TargetCastingPlayer
CHIP_ERROR err = support::CastingStore::GetInstance()->AddOrUpdate(*CastingPlayer::GetTargetCastingPlayer());
if(err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "CastingStore::AddOrUpdate() failed. Err: %" CHIP_ERROR_FORMAT, err.Format());
}

// callback client OnCompleted
VerifyOrReturn(CastingPlayer::GetTargetCastingPlayer()->mOnCompleted);
CastingPlayer::GetTargetCastingPlayer()->mOnCompleted(CHIP_NO_ERROR, CastingPlayer::GetTargetCastingPlayer());
}
Expand Down

0 comments on commit 1a80c2e

Please sign in to comment.