Skip to content

Commit 2ba9901

Browse files
iOS TvCasting app: Adding async completion callback to startMatterServer API (project-chip#24285)
1 parent 8c7c641 commit 2ba9901

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,15 @@
185185
- (void)disconnect:(dispatch_queue_t _Nonnull)clientQueue requestSentHandler:(nullable void (^)())requestSentHandler;
186186

187187
/**
188-
@brief Start the Matter server and reconnect to a previously connected Video Player (if any)
188+
@brief Start the Matter server and reconnect to a previously connected Video Player (if any). This API is async
189+
190+
@param clientQueue Queue to invoke callbacks on
191+
192+
@param startMatterServerCompletionCallback Called after the Matter server has started and connected (or failed to connect) to a
193+
previously connected video player (if any) are complete
189194
*/
190-
- (void)startMatterServer;
195+
- (void)startMatterServer:(dispatch_queue_t _Nonnull)clientQueue
196+
startMatterServerCompletionCallback:(nullable void (^)(MatterError * _Nonnull))startMatterServerCompletionCallback;
191197

192198
/**
193199
@brief Stop the Matter server

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

+29-4
Original file line numberDiff line numberDiff line change
@@ -510,15 +510,20 @@ - (void)shutdownAllSubscriptions:(dispatch_queue_t _Nonnull)clientQueue requestS
510510
});
511511
}
512512

513-
- (void)startMatterServer
513+
- (void)startMatterServer:(dispatch_queue_t _Nonnull)clientQueue
514+
startMatterServerCompletionCallback:(nullable void (^)(MatterError * _Nonnull))startMatterServerCompletionCallback
514515
{
515516
ChipLogProgress(AppServer, "CastingServerBridge().startMatterServer() called");
516517

517-
dispatch_sync(_chipWorkQueue, ^{
518+
dispatch_async(_chipWorkQueue, ^{
518519
// Initialize the Matter server
519520
CHIP_ERROR err = chip::Server::GetInstance().Init(*self->_serverInitParams);
520521
if (err != CHIP_NO_ERROR) {
521522
ChipLogError(AppServer, "chip::Server init failed: %s", ErrorStr(err));
523+
dispatch_async(clientQueue, ^{
524+
startMatterServerCompletionCallback(
525+
[[MatterError alloc] initWithCode:err.AsInteger() message:[NSString stringWithUTF8String:err.AsString()]]);
526+
});
522527
return;
523528
}
524529

@@ -527,8 +532,28 @@ - (void)startMatterServer
527532
ChipLogProgress(
528533
AppServer, "CastingServerBridge().startMatterServer() reconnecting to previously connected VideoPlayer...");
529534
err = CastingServer::GetInstance()->VerifyOrEstablishConnection(
530-
*(self->_previouslyConnectedVideoPlayer), [](TargetVideoPlayerInfo * cppTargetVideoPlayerInfo) {},
531-
[](CHIP_ERROR err) {}, [](TargetEndpointInfo * cppTargetEndpointInfo) {});
535+
*(self->_previouslyConnectedVideoPlayer),
536+
[clientQueue, startMatterServerCompletionCallback](TargetVideoPlayerInfo * cppTargetVideoPlayerInfo) {
537+
dispatch_async(clientQueue, ^{
538+
startMatterServerCompletionCallback(
539+
[[MatterError alloc] initWithCode:CHIP_NO_ERROR.AsInteger()
540+
message:[NSString stringWithUTF8String:CHIP_NO_ERROR.AsString()]]);
541+
});
542+
},
543+
[clientQueue, startMatterServerCompletionCallback](CHIP_ERROR err) {
544+
dispatch_async(clientQueue, ^{
545+
startMatterServerCompletionCallback(
546+
[[MatterError alloc] initWithCode:err.AsInteger()
547+
message:[NSString stringWithUTF8String:err.AsString()]]);
548+
});
549+
},
550+
[](TargetEndpointInfo * cppTargetEndpointInfo) {});
551+
} else {
552+
dispatch_async(clientQueue, ^{
553+
startMatterServerCompletionCallback(
554+
[[MatterError alloc] initWithCode:CHIP_NO_ERROR.AsInteger()
555+
message:[NSString stringWithUTF8String:CHIP_NO_ERROR.AsString()]]);
556+
});
532557
}
533558
});
534559
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ struct TvCastingApp: App {
6464
{
6565
if let castingServerBridge = CastingServerBridge.getSharedInstance()
6666
{
67-
castingServerBridge.startMatterServer()
67+
castingServerBridge.startMatterServer(DispatchQueue.main, startMatterServerCompletionCallback: { (error: MatterError) -> () in
68+
DispatchQueue.main.async {
69+
self.Log.info("TvCastingApp.startMatterServerCompletionCallback called with \(error)")
70+
}
71+
})
6872
}
6973
}
7074
firstAppActivation = false

0 commit comments

Comments
 (0)