Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Jan 7, 2024
1 parent e3cc550 commit d6104db
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,45 @@

@class MTREndpoint;

/**
* @brief MTRCastingPlayer represents a Matter commissioner that is able to play media to a physical
* output or to a display screen which is part of the device.
*/
@interface MTRCastingPlayer : NSObject

/**
+ (NSInteger)kMinCommissioningWindowTimeoutSec;

minimum matter::casting::core::kCommissioningWindowTimeoutSec
/**
* @brief (async) Verifies that a connection exists with this CastingPlayer, or triggers a new session request. If the
* CastingApp does not have the nodeId and fabricIndex of this CastingPlayer cached on disk, this will execute the user
* directed commissioning process.
*
* @param completion - called back when the connection process completes. Parameter is nil if it completed successfully
* @param timeout - time (in sec) to keep the commissioning window open, if commissioning is required.
* Needs to be >= CastingPlayer.kMinCommissioningWindowTimeoutSec.
* @param desiredEndpointFilter - Attributes (such as VendorId) describing an Endpoint that the client wants to interact
* with after commissioning. If this value is passed in, the VerifyOrEstablishConnection will force User Directed
* Commissioning, in case the desired Endpoint is not found in the on-device cached information about the CastingPlayer
* (if any)
*/
- (void)verifyOrEstablishConnectionWithCompletionBlock:(void (^_Nonnull)(NSError * _Nullable))completion timeout:(long long)timeout desiredEndpointFilter:(MTREndpointFilter * _Nullable)desiredEndpointFilter;

/**
* @brief (async) Verifies that a connection exists with this CastingPlayer, or triggers a new session request. If the
* CastingApp does not have the nodeId and fabricIndex of this CastingPlayer cached on disk, this will execute the user
* directed commissioning process.
*
* @param completion - called back when the connection process completes. Parameter is nil if it completed successfully
* @param desiredEndpointFilter - Attributes (such as VendorId) describing an Endpoint that the client wants to interact
* with after commissioning. If this value is passed in, the VerifyOrEstablishConnection will force User Directed
* Commissioning, in case the desired Endpoint is not found in the on-device cached information about the CastingPlayer
* (if any)
*/
- (void)verifyOrEstablishConnectionWithCompletionBlock:(void (^_Nonnull)(NSError * _Nullable))completion desiredEndpointFilter:(MTREndpointFilter * _Nullable)desiredEndpointFilter;

/**
* @brief Sets the internal connection state of this CastingPlayer to "disconnected"
*/
- (void)disconnect;

- (NSString * _Nonnull)identifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ @interface MTRCastingPlayer ()

@implementation MTRCastingPlayer

static const NSInteger kMinCommissioningWindowTimeoutSec = matter::casting::core::kCommissioningWindowTimeoutSec;

+ (NSInteger)kMinCommissioningWindowTimeoutSec
{
return kMinCommissioningWindowTimeoutSec;
}

- (void)verifyOrEstablishConnectionWithCompletionBlock:(void (^_Nonnull)(NSError * _Nullable))completion desiredEndpointFilter:(MTREndpointFilter * _Nullable)desiredEndpointFilter
{
[self verifyOrEstablishConnectionWithCompletionBlock:completion timeout:matter::casting::core::kCommissioningWindowTimeoutSec desiredEndpointFilter:desiredEndpointFilter];
[self verifyOrEstablishConnectionWithCompletionBlock:completion timeout:kMinCommissioningWindowTimeoutSec desiredEndpointFilter:desiredEndpointFilter];
}

- (void)verifyOrEstablishConnectionWithCompletionBlock:(void (^_Nonnull)(NSError * _Nullable))completion timeout:(long long)timeout desiredEndpointFilter:(MTREndpointFilter * _Nullable)desiredEndpointFilter
Expand Down Expand Up @@ -123,6 +130,7 @@ - (NSArray * _Nonnull)ipAddresses

- (NSArray<MTREndpoint *> * _Nonnull)endpoints
{
// TODO convert to Obj-C endpoints and return
return [NSMutableArray new];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,34 @@
#ifndef MTRCastingPlayerDiscovery_h
#define MTRCastingPlayerDiscovery_h

/**
* MTRCastingPlayerDiscovery sends notification with ADD_CASTING_PLAYER_NOTIFICATION_NAME
* through the NSNotificationCenter if a new MTRCastingPlayer is added to the network
*/
extern NSString * _Nonnull const ADD_CASTING_PLAYER_NOTIFICATION_NAME;

/**
* MTRCastingPlayerDiscovery sends notification with REMOVE_CASTING_PLAYER_NOTIFICATION_NAME
* through the NSNotificationCenter if a MTRCastingPlayer is removed from the network
*/
extern NSString * _Nonnull const REMOVE_CASTING_PLAYER_NOTIFICATION_NAME;

/**
* MTRCastingPlayerDiscovery sends notification with UPDATE_CASTING_PLAYER_NOTIFICATION_NAME
* through the NSNotificationCenter if a previously added MTRCastingPlayer is updated
*/
extern NSString * _Nonnull const UPDATE_CASTING_PLAYER_NOTIFICATION_NAME;

/**
* MTRCastingPlayerDiscovery sends ADD / REMOVE / UPDATE notifications through the
* NSNotificationCenter with userInfo set to an NSDictionary that has CASTING_PLAYER_KEY as the
* key to a MTRCastingPlayer object as value.
*/
extern NSString * _Nonnull const CASTING_PLAYER_KEY;

/**
* @brief MTRCastingPlayerDiscovery is a singleton utility class for discovering CastingPlayers.
*/
@interface MTRCastingPlayerDiscovery : NSObject
+ (MTRCastingPlayerDiscovery * _Nonnull)sharedInstance;

Expand All @@ -34,9 +57,27 @@ extern NSString * _Nonnull const CASTING_PLAYER_KEY;

@property (nonatomic, strong) NSArray<MTRCastingPlayer *> * _Nonnull castingPlayers;

/**
* @brief Starts the discovery for CastingPlayers
*
* @return Returns nil if discovery for CastingPlayers started successfully, NSError * describing the error otherwise.
*/
- (NSError * _Nullable)start;
- (NSError * _Nullable)start:(const uint32_t)targetPlayerDeviceType;

/**
* @brief Starts the discovery for CastingPlayers
*
* @param filterBydeviceType if passed as a non-zero value, MTRCastingPlayerDiscovery will only discover
* MTRCastingPlayers whose deviceType matches filterBydeviceType
* @return Returns nil if discovery for MTRCastingPlayers started successfully, NSError * describing the error otherwise.
*/
- (NSError * _Nullable)start:(const uint32_t)filterBydeviceType;

/**
* @brief Stop the discovery for CastingPlayers
*
* @return Returns nil if discovery for MTRCastingPlayers stopped successfully, NSError * describing the error otherwise.
*/
- (NSError * _Nullable)stop;

@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ @implementation MTRCastingPlayerDiscovery
{
self = [super init];
if (self) {
// dispatch_sync
core::CastingPlayerDiscovery::GetInstance()->SetDelegate(MTRDiscoveryDelegateImpl::GetInstance());
dispatch_queue_t workQueue = [[MTRCastingApp getSharedInstance] getWorkQueue];
dispatch_sync(workQueue, ^{
core::CastingPlayerDiscovery::GetInstance()->SetDelegate(MTRDiscoveryDelegateImpl::GetInstance());
});
}
return self;
}
Expand All @@ -75,18 +77,18 @@ + (MTRCastingPlayerDiscovery *)sharedInstance

- (NSError *)start
{
return [self start:0]; // default to targetPlayerDeviceType: 0
return [self start:0]; // default to filterBydeviceType: 0
}

- (NSError *)start:(const uint32_t)targetPlayerDeviceType
- (NSError *)start:(const uint32_t)filterBydeviceType
{
ChipLogProgress(AppServer, "MTRCastingPlayerDiscovery.start called");
VerifyOrReturnValue([[MTRCastingApp getSharedInstance] isRunning], [MTRErrorUtils NSErrorFromChipError:CHIP_ERROR_INCORRECT_STATE]);

dispatch_queue_t workQueue = [[MTRCastingApp getSharedInstance] getWorkQueue];
__block CHIP_ERROR err = CHIP_NO_ERROR;
dispatch_sync(workQueue, ^{
err = core::CastingPlayerDiscovery::GetInstance()->StartDiscovery(targetPlayerDeviceType);
err = core::CastingPlayerDiscovery::GetInstance()->StartDiscovery(filterBydeviceType);
});

return [MTRErrorUtils NSErrorFromChipError:err];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ + (MatterError *)ecdsaAsn1SignatureToRawWithFeLengthBytes:(NSUInteger)feLengthBy
CHIP_ERROR err = chip::Crypto::EcdsaAsn1SignatureToRaw(feLengthBytes, chip::ByteSpan(asn1SignatureByteSpan.data(), asn1SignatureByteSpan.size()), rawSignatureMutableByteSpan);
if (err != CHIP_NO_ERROR) {
ChipLogError(AppServer, "chip::Crypto::EcdsaAsn1SignatureToRaw() failed");
// delete[] rawSignatureBytes;
return [MTRErrorUtils MatterErrorFromChipError:err];
}

// copy from rawSignatureMutableByteSpan into *outRawSignature
*outRawSignature = [NSData dataWithBytes:rawSignatureMutableByteSpan.data() length:rawSignatureMutableByteSpan.size()];
// delete[] rawSignatureBytes;
return MATTER_NO_ERROR;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,33 @@

@protocol MTRDataSource <NSObject>

/**
* @brief Queue used when calling the client code on completion blocks from any MatterTvCastingBridge API
*/
- (dispatch_queue_t _Nonnull)clientQueue;

/**
* @brief Provide UniqueId used to generate the RotatingDeviceId advertised during commissioning by the MTRCastingApp
*/
- (NSData * _Nonnull)castingAppDidReceiveRequestForRotatingDeviceIdUniqueId:(id _Nonnull)sender;

/**
* @brief Provides MTRCommissionableData (such as setupPasscode, discriminator, etc) used to get the MTRCastingApp commissioned
*/
- (MTRCommissionableData * _Nonnull)castingAppDidReceiveRequestForCommissionableData:(id _Nonnull)sender;

/**
* @brief Provides MTRDeviceAttestationCredentials of the CastingApp used during commissioning
*/
- (MTRDeviceAttestationCredentials * _Nonnull)castingAppDidReceiveRequestForDeviceAttestationCredentials:(id _Nonnull)sender;

/**
* @brief Request to signs a message using the device attestation private key
*
* @param csrData - The message to sign using the attestation private key.
* @param outRawSignature [in, out] - Buffer to receive the signature in raw <r,s> format.
* @returns MATTER_NO_ERROR on success. Otherwise, a MATTER_ERROR with a code corresponding to the underlying failure
*/
- (MatterError * _Nonnull)castingApp:(id _Nonnull)sender didReceiveRequestToSignCertificateRequest:(NSData * _Nonnull)csrData outRawSignature:(NSData ** _Nonnull)outRawSignature;

@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ @implementation MTREndpoint

- (id<MTRCluster> _Nullable)clusterForType:(MTREndpointClusterType)type
{
// TODO
return nil;
}

- (BOOL)hasCluster:(MTREndpointClusterType)type
{
// TODO
return nil;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
*/
@interface MTREndpointFilter : NSObject
// value of 0 means unspecified
@property (nonatomic, readonly) uint16_t vendorId;
@property (nonatomic, readonly) uint16_t productId;
@property (nonatomic) uint16_t vendorId;
@property (nonatomic) uint16_t productId;
// std::vector<chip::app::Clusters::Descriptor::Structs::DeviceTypeStruct::DecodableType> requiredDeviceTypes;

@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
#ifndef MTRErrorUtils_h
#define MTRErrorUtils_h

/**
* @brief - Conversion utilities to/from CHIP_ERROR (C++) / MatterError (Objective C) / NSError
*/
@interface MTRErrorUtils : NSObject

+ (MatterError * _Nonnull)MatterErrorFromChipError:(CHIP_ERROR)chipError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ FOUNDATION_EXPORT const unsigned char MatterTvCastingBridgeVersionString[];
#import "MTRDeviceAttestationCredentials.h"
#import "MTREndpoint.h"
#import "MTREndpointClusterType.h"
#import "MTREndpointFilter.h"
#import "MTRLaunchResponse.h"
#import "MTRMediaPlaybackCluster.h"
#import "MTRObserver.h"
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ import os.log
class MTRConnectionExampleViewModel: ObservableObject {
let Log = Logger(subsystem: "com.matter.casting",
category: "MTRConnectionExampleViewModel")


// VendorId of the MTREndpoint on the MTRCastingPlayer that the MTRCastingApp desires to interact with after connection
let kDesiredEndpointVendorId: UInt16 = 65521;

@Published var connectionSuccess: Bool?;

@Published var connectionStatus: String?;

func connect(selectedCastingPlayer: MTRCastingPlayer?) {
let desiredEndpointFilter: MTREndpointFilter = MTREndpointFilter()
desiredEndpointFilter.vendorId = kDesiredEndpointVendorId
selectedCastingPlayer?.verifyOrEstablishConnection(completionBlock: { err in
self.Log.error("MTRConnectionExampleViewModel connect() completed with \(err)")
if(err == nil)
Expand All @@ -40,6 +45,6 @@ class MTRConnectionExampleViewModel: ObservableObject {
self.connectionSuccess = false
self.connectionStatus = "Connection failed with \(String(describing: err))"
}
}, desiredEndpointFilter: nil)
}, desiredEndpointFilter: desiredEndpointFilter)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class CastingPlayer : public std::enable_shared_from_this<CastingPlayer>
* @param onCompleted for success - called back with CHIP_NO_ERROR and CastingPlayer *.
* For failure - called back with an error and nullptr.
* @param commissioningWindowTimeoutSec (Optional) time (in sec) to keep the commissioning window open, if commissioning is
* required. Defaults to kCommissioningWindowTimeoutSec.
* required. Needs to be >= kCommissioningWindowTimeoutSec.
* @param desiredEndpointFilter (Optional) Attributes (such as VendorId) describing an Endpoint that the client wants to
* interact with after commissioning. If this value is passed in, the VerifyOrEstablishConnection will force User Directed
* Commissioning, in case the desired Endpoint is not found in the on device CastingStore.
Expand All @@ -130,6 +130,9 @@ class CastingPlayer : public std::enable_shared_from_this<CastingPlayer>
unsigned long long int commissioningWindowTimeoutSec = kCommissioningWindowTimeoutSec,
EndpointFilter desiredEndpointFilter = EndpointFilter());

/**
* @brief Sets the internal connection state of this CastingPlayer to "disconnected"
*/
void Disconnect();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ class DeviceDiscoveryDelegateImpl : public chip::Controller::DeviceDiscoveryDele
};

/**
* @brief CastingPlayerDiscovery represents the discovery of Casting Players.
* This class is a singleton.
* @brief CastingPlayerDiscovery is a singleton utility class for discovering CastingPlayers.
*/
class CastingPlayerDiscovery
{
Expand Down
Loading

0 comments on commit d6104db

Please sign in to comment.