From 3451184baa2d77c2d5d84e0dc7ca7be95afe8a92 Mon Sep 17 00:00:00 2001 From: Sharad Binjola <31142146+sharadb-amazon@users.noreply.github.com> Date: Fri, 6 Jan 2023 16:31:23 -0800 Subject: [PATCH] iOS TvCasting app: Adding async completion callback to startMatterServer API (#24285) --- .../CastingServerBridge.h | 10 ++++-- .../CastingServerBridge.mm | 33 ++++++++++++++++--- .../TvCasting/TvCasting/TvCastingApp.swift | 6 +++- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/examples/tv-casting-app/darwin/MatterTvCastingBridge/MatterTvCastingBridge/CastingServerBridge.h b/examples/tv-casting-app/darwin/MatterTvCastingBridge/MatterTvCastingBridge/CastingServerBridge.h index 8f1d7d17bf95ac..9c7a4abcb9cdb7 100644 --- a/examples/tv-casting-app/darwin/MatterTvCastingBridge/MatterTvCastingBridge/CastingServerBridge.h +++ b/examples/tv-casting-app/darwin/MatterTvCastingBridge/MatterTvCastingBridge/CastingServerBridge.h @@ -185,9 +185,15 @@ - (void)disconnect:(dispatch_queue_t _Nonnull)clientQueue requestSentHandler:(nullable void (^)())requestSentHandler; /** - @brief Start the Matter server and reconnect to a previously connected Video Player (if any) + @brief Start the Matter server and reconnect to a previously connected Video Player (if any). This API is async + + @param clientQueue Queue to invoke callbacks on + + @param startMatterServerCompletionCallback Called after the Matter server has started and connected (or failed to connect) to a + previously connected video player (if any) are complete */ -- (void)startMatterServer; +- (void)startMatterServer:(dispatch_queue_t _Nonnull)clientQueue + startMatterServerCompletionCallback:(nullable void (^)(MatterError * _Nonnull))startMatterServerCompletionCallback; /** @brief Stop the Matter server diff --git a/examples/tv-casting-app/darwin/MatterTvCastingBridge/MatterTvCastingBridge/CastingServerBridge.mm b/examples/tv-casting-app/darwin/MatterTvCastingBridge/MatterTvCastingBridge/CastingServerBridge.mm index cd860847c4b300..3e368d4ba949d8 100644 --- a/examples/tv-casting-app/darwin/MatterTvCastingBridge/MatterTvCastingBridge/CastingServerBridge.mm +++ b/examples/tv-casting-app/darwin/MatterTvCastingBridge/MatterTvCastingBridge/CastingServerBridge.mm @@ -510,15 +510,20 @@ - (void)shutdownAllSubscriptions:(dispatch_queue_t _Nonnull)clientQueue requestS }); } -- (void)startMatterServer +- (void)startMatterServer:(dispatch_queue_t _Nonnull)clientQueue + startMatterServerCompletionCallback:(nullable void (^)(MatterError * _Nonnull))startMatterServerCompletionCallback { ChipLogProgress(AppServer, "CastingServerBridge().startMatterServer() called"); - dispatch_sync(_chipWorkQueue, ^{ + dispatch_async(_chipWorkQueue, ^{ // Initialize the Matter server CHIP_ERROR err = chip::Server::GetInstance().Init(*self->_serverInitParams); if (err != CHIP_NO_ERROR) { ChipLogError(AppServer, "chip::Server init failed: %s", ErrorStr(err)); + dispatch_async(clientQueue, ^{ + startMatterServerCompletionCallback( + [[MatterError alloc] initWithCode:err.AsInteger() message:[NSString stringWithUTF8String:err.AsString()]]); + }); return; } @@ -527,8 +532,28 @@ - (void)startMatterServer ChipLogProgress( AppServer, "CastingServerBridge().startMatterServer() reconnecting to previously connected VideoPlayer..."); err = CastingServer::GetInstance()->VerifyOrEstablishConnection( - *(self->_previouslyConnectedVideoPlayer), [](TargetVideoPlayerInfo * cppTargetVideoPlayerInfo) {}, - [](CHIP_ERROR err) {}, [](TargetEndpointInfo * cppTargetEndpointInfo) {}); + *(self->_previouslyConnectedVideoPlayer), + [clientQueue, startMatterServerCompletionCallback](TargetVideoPlayerInfo * cppTargetVideoPlayerInfo) { + dispatch_async(clientQueue, ^{ + startMatterServerCompletionCallback( + [[MatterError alloc] initWithCode:CHIP_NO_ERROR.AsInteger() + message:[NSString stringWithUTF8String:CHIP_NO_ERROR.AsString()]]); + }); + }, + [clientQueue, startMatterServerCompletionCallback](CHIP_ERROR err) { + dispatch_async(clientQueue, ^{ + startMatterServerCompletionCallback( + [[MatterError alloc] initWithCode:err.AsInteger() + message:[NSString stringWithUTF8String:err.AsString()]]); + }); + }, + [](TargetEndpointInfo * cppTargetEndpointInfo) {}); + } else { + dispatch_async(clientQueue, ^{ + startMatterServerCompletionCallback( + [[MatterError alloc] initWithCode:CHIP_NO_ERROR.AsInteger() + message:[NSString stringWithUTF8String:CHIP_NO_ERROR.AsString()]]); + }); } }); } diff --git a/examples/tv-casting-app/darwin/TvCasting/TvCasting/TvCastingApp.swift b/examples/tv-casting-app/darwin/TvCasting/TvCasting/TvCastingApp.swift index 322f1a1108d15b..73c4eb17494b1a 100644 --- a/examples/tv-casting-app/darwin/TvCasting/TvCasting/TvCastingApp.swift +++ b/examples/tv-casting-app/darwin/TvCasting/TvCasting/TvCastingApp.swift @@ -64,7 +64,11 @@ struct TvCastingApp: App { { if let castingServerBridge = CastingServerBridge.getSharedInstance() { - castingServerBridge.startMatterServer() + castingServerBridge.startMatterServer(DispatchQueue.main, startMatterServerCompletionCallback: { (error: MatterError) -> () in + DispatchQueue.main.async { + self.Log.info("TvCastingApp.startMatterServerCompletionCallback called with \(error)") + } + }) } } firstAppActivation = false