Skip to content

Commit

Permalink
Addressing review comments, adding Discover button to Commissioner Di…
Browse files Browse the repository at this point in the history
…scovery UI, using optional Bools to denote the state of the CommissioningView
  • Loading branch information
sharadb-amazon committed Jun 14, 2022
1 parent ceea1a8 commit b84bab9
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@

@interface CastingServerBridge : NSObject

+ (CastingServerBridge *)getSharedInstance;
+ (CastingServerBridge * _Nullable)getSharedInstance;

- (bool)discoverCommissioners;
- (void)discoverCommissioners:(dispatch_queue_t _Nonnull)clientQueue
discoveryRequestSentHandler:(nullable void (^)(bool))discoveryRequestSentHandler;

- (DiscoveredNodeData *)getDiscoveredCommissioner:(int)index;
- (void)getDiscoveredCommissioner:(int)index
clientQueue:(dispatch_queue_t _Nonnull)clientQueue
discoveredCommissionerHandler:(nullable void (^)(DiscoveredNodeData * _Nullable))discoveredCommissionerHandler;

- (bool)sendUserDirectedCommissioningRequest:(NSString *)commissionerIpAddress
- (void)sendUserDirectedCommissioningRequest:(NSString * _Nonnull)commissionerIpAddress
commissionerPort:(uint16_t)commissionerPort
platformInterface:(unsigned int)platformInterface;
platformInterface:(unsigned int)platformInterface
clientQueue:(dispatch_queue_t _Nonnull)clientQueue
udcRequestSentHandler:(nullable void (^)(bool))udcRequestSentHandler;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@
#import "CastingServerBridge.h"
#import "CastingServer.h"

#import "DiscoveredNodeDataConverter.hpp"

#include <lib/support/CHIPMem.h>
#include <platform/PlatformManager.h>

@interface CastingServerBridge ()

// queue used to serialize all work performed by the CastingServerBridge
@property (atomic, readonly) dispatch_queue_t chipWorkQueue;

@end

@implementation CastingServerBridge

+ (CastingServerBridge *)getSharedInstance
+ (CastingServerBridge * _Nullable)getSharedInstance
{
static CastingServerBridge * instance = nil;
static dispatch_once_t onceToken;
Expand Down Expand Up @@ -61,59 +70,87 @@ - (instancetype)init
return nil;
}

_chipWorkQueue = chip::DeviceLayer::PlatformMgrImpl().GetWorkQueue();

chip::DeviceLayer::PlatformMgrImpl().StartEventLoopTask();

CastingServer::GetInstance()->Init();
}
return self;
}

- (bool)discoverCommissioners
- (void)discoverCommissioners:(dispatch_queue_t _Nonnull)clientQueue
discoveryRequestSentHandler:(nullable void (^)(bool))discoveryRequestSentHandler
{
ChipLogProgress(AppServer, "CastingServerBridge().discoverCommissioners() called");
CHIP_ERROR err = CastingServer::GetInstance()->DiscoverCommissioners();
if (err != CHIP_NO_ERROR) {
ChipLogError(AppServer, "CastingServerBridge().discoverCommissioners() failed: %" CHIP_ERROR_FORMAT, err.Format());
return false;
}
return true;
dispatch_async(_chipWorkQueue, ^{
bool discoveryRequestStatus = true;
CHIP_ERROR err = CastingServer::GetInstance()->DiscoverCommissioners();
if (err != CHIP_NO_ERROR) {
ChipLogError(AppServer, "CastingServerBridge().discoverCommissioners() failed: %" CHIP_ERROR_FORMAT, err.Format());
discoveryRequestStatus = false;
}

dispatch_async(clientQueue, ^{
discoveryRequestSentHandler(discoveryRequestStatus);
});
});
}

- (DiscoveredNodeData *)getDiscoveredCommissioner:(int)index
- (void)getDiscoveredCommissioner:(int)index
clientQueue:(dispatch_queue_t _Nonnull)clientQueue
discoveredCommissionerHandler:(nullable void (^)(DiscoveredNodeData * _Nullable))discoveredCommissionerHandler
{
ChipLogProgress(AppServer, "CastingServerBridge().getDiscoveredCommissioner() called");

DiscoveredNodeData * commissioner = nil;
const chip::Dnssd::DiscoveredNodeData * chipDiscoveredNodeData = CastingServer::GetInstance()->GetDiscoveredCommissioner(index);
if (chipDiscoveredNodeData != nullptr) {
commissioner = [[DiscoveredNodeData alloc] initWithChipDiscoveredNodeData:(void *) chipDiscoveredNodeData];
}
return commissioner;
dispatch_async(_chipWorkQueue, ^{
DiscoveredNodeData * commissioner = nil;
const chip::Dnssd::DiscoveredNodeData * chipDiscoveredNodeData
= CastingServer::GetInstance()->GetDiscoveredCommissioner(index);
if (chipDiscoveredNodeData != nullptr) {
commissioner = [DiscoveredNodeDataConverter convertToObjC:chipDiscoveredNodeData];
}

dispatch_async(clientQueue, ^{
discoveredCommissionerHandler(commissioner);
});
});
}

- (bool)sendUserDirectedCommissioningRequest:(NSString *)commissionerIpAddress
- (void)sendUserDirectedCommissioningRequest:(NSString * _Nonnull)commissionerIpAddress
commissionerPort:(uint16_t)commissionerPort
platformInterface:(unsigned int)platformInterface
clientQueue:(dispatch_queue_t _Nonnull)clientQueue
udcRequestSentHandler:(nullable void (^)(bool))udcRequestSentHandler
{
ChipLogProgress(
AppServer, "CastingServerBridge().sendUserDirectedCommissioningRequest() called with port %d", commissionerPort);
chip::Inet::IPAddress commissionerAddrInet;
if (chip::Inet::IPAddress::FromString([commissionerIpAddress UTF8String], commissionerAddrInet) == false) {
ChipLogError(AppServer, "CastingServerBridge().sendUserDirectedCommissioningRequest() failed to parse IP address");
return false;
}

chip::Inet::InterfaceId interfaceId = chip::Inet::InterfaceId(platformInterface);

chip::Transport::PeerAddress commissionerPeerAddress
= chip::Transport::PeerAddress::UDP(commissionerAddrInet, commissionerPort, interfaceId);
dispatch_async(chip::DeviceLayer::PlatformMgrImpl().GetWorkQueue(), ^{
bool udcRequestStatus;
chip::Inet::IPAddress commissionerAddrInet;
if (chip::Inet::IPAddress::FromString([commissionerIpAddress UTF8String], commissionerAddrInet) == false) {
ChipLogError(AppServer, "CastingServerBridge().sendUserDirectedCommissioningRequest() failed to parse IP address");
udcRequestStatus = false;
} else {
chip::Inet::InterfaceId interfaceId = chip::Inet::InterfaceId(platformInterface);

chip::Transport::PeerAddress commissionerPeerAddress
= chip::Transport::PeerAddress::UDP(commissionerAddrInet, commissionerPort, interfaceId);

CHIP_ERROR err = CastingServer::GetInstance()->SendUserDirectedCommissioningRequest(commissionerPeerAddress);
if (err != CHIP_NO_ERROR) {
ChipLogError(AppServer, "CastingServerBridge().sendUserDirectedCommissioningRequest() failed: %" CHIP_ERROR_FORMAT,
err.Format());
udcRequestStatus = false;
} else {
udcRequestStatus = true;
}
}

CHIP_ERROR err = CastingServer::GetInstance()->SendUserDirectedCommissioningRequest(commissionerPeerAddress);
if (err != CHIP_NO_ERROR) {
ChipLogError(
AppServer, "CastingServerBridge().sendUserDirectedCommissioningRequest() failed: %" CHIP_ERROR_FORMAT, err.Format());
return false;
}
return true;
dispatch_async(clientQueue, ^{
udcRequestSentHandler(udcRequestStatus);
});
});
}
@end
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

@property uint16_t pairingHint;

@property uint8_t * rotatingId;
@property const uint8_t * rotatingId;

@property size_t rotatingIdLen;

Expand All @@ -52,8 +52,6 @@

@property size_t numIPs;

- (DiscoveredNodeData *)initWithChipDiscoveredNodeData:(void *)chipDiscoveredNodedata;

- (DiscoveredNodeData *)initWithDeviceName:(NSString *)deviceName vendorId:(uint16_t)vendorId productId:(uint16_t)productId;

@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,6 @@

@implementation DiscoveredNodeData

- (DiscoveredNodeData *)initWithChipDiscoveredNodeData:(void *)chipDiscoveredNodedata
{
self = [super init];
if (self) {
chip::Dnssd::DiscoveredNodeData * data = (chip::Dnssd::DiscoveredNodeData *) chipDiscoveredNodedata;

// from CommissionNodeData
_deviceType = data->commissionData.deviceType;
_vendorId = data->commissionData.vendorId;
_productId = data->commissionData.productId;
_longDiscriminator = data->commissionData.longDiscriminator;
_commissioningMode = data->commissionData.commissioningMode;
_pairingHint = data->commissionData.pairingHint;
_deviceName = [NSString stringWithCString:data->commissionData.deviceName encoding:NSASCIIStringEncoding];
_rotatingIdLen = data->commissionData.rotatingIdLen;
_rotatingId = data->commissionData.rotatingId;
_instanceName = [NSString stringWithCString:data->commissionData.instanceName encoding:NSASCIIStringEncoding];

// from CommonResolutionData
_port = data->resolutionData.port;
_hostName = [NSString stringWithCString:data->resolutionData.hostName encoding:NSASCIIStringEncoding];
_platformInterface = data->resolutionData.interfaceId.GetPlatformInterface();
_numIPs = data->resolutionData.numIPs;
if (data->resolutionData.numIPs > 0) {
_ipAddresses = [NSMutableArray new];
}
for (int i = 0; i < data->resolutionData.numIPs; i++) {
char addrCString[chip::Inet::IPAddress::kMaxStringLength];
data->resolutionData.ipAddress->ToString(addrCString, chip::Inet::IPAddress::kMaxStringLength);
_ipAddresses[i] = [NSString stringWithCString:addrCString encoding:NSASCIIStringEncoding];
}
}
return self;
}

- (DiscoveredNodeData *)initWithDeviceName:(NSString *)deviceName vendorId:(uint16_t)vendorId productId:(uint16_t)productId
{
self = [super init];
Expand All @@ -73,4 +38,36 @@ - (NSString *)description
return [NSString stringWithFormat:@"%@ with Product ID: %d and Vendor ID: %d", _deviceName, _productId, _vendorId];
}

- (BOOL)isEqualToDiscoveredNodeData:(DiscoveredNodeData *)other
{
return [self.instanceName isEqualToString:other.instanceName];
}

- (BOOL)isEqual:(id)other
{
if (other == nil) {
return NO;
}

if (self == other) {
return YES;
}

if (![other isKindOfClass:[DiscoveredNodeData class]]) {
return NO;
}

return [self isEqualToDiscoveredNodeData:(DiscoveredNodeData *) other];
}

- (NSUInteger)hash
{
const NSUInteger prime = 31;
NSUInteger result = 1;

result = prime * result + [self.instanceName hash];

return result;
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
*
* Copyright (c) 2020-2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "DiscoveredNodeData.h"
#include <lib/dnssd/Resolver.h>

#ifndef DiscoveredNodeDataConverter_h
#define DiscoveredNodeDataConverter_h

@interface DiscoveredNodeDataConverter : NSObject

+ (DiscoveredNodeData *)convertToObjC:(const chip::Dnssd::DiscoveredNodeData *)chipDiscoveredNodedata;

@end

#endif /* DiscoveredNodeDataConverter_h */
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
*
* Copyright (c) 2020-2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import <Foundation/Foundation.h>

#import "DiscoveredNodeDataConverter.hpp"

@implementation DiscoveredNodeDataConverter

+ (DiscoveredNodeData *)convertToObjC:(const chip::Dnssd::DiscoveredNodeData *)chipDiscoveredNodeData
{
DiscoveredNodeData * objCDiscoveredNodeData = [DiscoveredNodeData new];

// from CommissionNodeData
objCDiscoveredNodeData.deviceType = chipDiscoveredNodeData->commissionData.deviceType;
objCDiscoveredNodeData.vendorId = chipDiscoveredNodeData->commissionData.vendorId;
objCDiscoveredNodeData.productId = chipDiscoveredNodeData->commissionData.productId;
objCDiscoveredNodeData.longDiscriminator = chipDiscoveredNodeData->commissionData.longDiscriminator;
objCDiscoveredNodeData.commissioningMode = chipDiscoveredNodeData->commissionData.commissioningMode;
objCDiscoveredNodeData.pairingHint = chipDiscoveredNodeData->commissionData.pairingHint;
objCDiscoveredNodeData.deviceName = [NSString stringWithCString:chipDiscoveredNodeData->commissionData.deviceName
encoding:NSASCIIStringEncoding];
objCDiscoveredNodeData.rotatingIdLen = chipDiscoveredNodeData->commissionData.rotatingIdLen;
objCDiscoveredNodeData.rotatingId = chipDiscoveredNodeData->commissionData.rotatingId;
objCDiscoveredNodeData.instanceName = [NSString stringWithCString:chipDiscoveredNodeData->commissionData.instanceName
encoding:NSASCIIStringEncoding];

// from CommonResolutionData
objCDiscoveredNodeData.port = chipDiscoveredNodeData->resolutionData.port;
objCDiscoveredNodeData.hostName = [NSString stringWithCString:chipDiscoveredNodeData->resolutionData.hostName
encoding:NSASCIIStringEncoding];
objCDiscoveredNodeData.platformInterface = chipDiscoveredNodeData->resolutionData.interfaceId.GetPlatformInterface();
objCDiscoveredNodeData.numIPs = chipDiscoveredNodeData->resolutionData.numIPs;
if (chipDiscoveredNodeData->resolutionData.numIPs > 0) {
objCDiscoveredNodeData.ipAddresses = [NSMutableArray new];
}
for (int i = 0; i < chipDiscoveredNodeData->resolutionData.numIPs; i++) {
char addrCString[chip::Inet::IPAddress::kMaxStringLength];
chipDiscoveredNodeData->resolutionData.ipAddress->ToString(addrCString, chip::Inet::IPAddress::kMaxStringLength);
objCDiscoveredNodeData.ipAddresses[i] = [NSString stringWithCString:addrCString encoding:NSASCIIStringEncoding];
}
return objCDiscoveredNodeData;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
3C7507AF28529A5F00D7DB3A /* CommissioningView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C7507AE28529A5F00D7DB3A /* CommissioningView.swift */; };
3C7507B72853A3AD00D7DB3A /* CommissionerDiscoveryViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C7507B62853A3AD00D7DB3A /* CommissionerDiscoveryViewModel.swift */; };
3C7507B92853EFF000D7DB3A /* CommissioningViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C7507B82853EFF000D7DB3A /* CommissioningViewModel.swift */; };
3C7507BC2857A6EE00D7DB3A /* DiscoveredNodeDataConverter.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3C7507BB2857A6EE00D7DB3A /* DiscoveredNodeDataConverter.mm */; };
3C9ACC05284ABF4000718B2D /* libTvCastingCommon.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3C9ACC04284ABF2F00718B2D /* libTvCastingCommon.a */; };
3CC0E8FA2841DD3400EC6A18 /* TvCastingApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CC0E8F92841DD3400EC6A18 /* TvCastingApp.swift */; };
3CC0E8FC2841DD3400EC6A18 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CC0E8FB2841DD3400EC6A18 /* ContentView.swift */; };
Expand All @@ -30,6 +31,8 @@
3C7507AE28529A5F00D7DB3A /* CommissioningView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommissioningView.swift; sourceTree = "<group>"; };
3C7507B62853A3AD00D7DB3A /* CommissionerDiscoveryViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommissionerDiscoveryViewModel.swift; sourceTree = "<group>"; };
3C7507B82853EFF000D7DB3A /* CommissioningViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommissioningViewModel.swift; sourceTree = "<group>"; };
3C7507BB2857A6EE00D7DB3A /* DiscoveredNodeDataConverter.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = DiscoveredNodeDataConverter.mm; sourceTree = "<group>"; };
3C7507BD2857A72A00D7DB3A /* DiscoveredNodeDataConverter.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = DiscoveredNodeDataConverter.hpp; sourceTree = "<group>"; };
3C9ACC04284ABF2F00718B2D /* libTvCastingCommon.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libTvCastingCommon.a; path = lib/libTvCastingCommon.a; sourceTree = BUILT_PRODUCTS_DIR; };
3CC0E8F62841DD3400EC6A18 /* TvCasting.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TvCasting.app; sourceTree = BUILT_PRODUCTS_DIR; };
3CC0E8F92841DD3400EC6A18 /* TvCastingApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TvCastingApp.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -115,6 +118,8 @@
3CC0E9092841DD7000EC6A18 /* CastingServerBridge.mm */,
3C7507A52851187500D7DB3A /* DiscoveredNodeData.h */,
3C7507A62851188500D7DB3A /* DiscoveredNodeData.mm */,
3C7507BB2857A6EE00D7DB3A /* DiscoveredNodeDataConverter.mm */,
3C7507BD2857A72A00D7DB3A /* DiscoveredNodeDataConverter.hpp */,
);
path = MatterBridge;
sourceTree = "<group>";
Expand Down Expand Up @@ -230,6 +235,7 @@
3C7507AD285299DF00D7DB3A /* CommissionerDiscoveryView.swift in Sources */,
3CC0E8FA2841DD3400EC6A18 /* TvCastingApp.swift in Sources */,
3C7507B92853EFF000D7DB3A /* CommissioningViewModel.swift in Sources */,
3C7507BC2857A6EE00D7DB3A /* DiscoveredNodeDataConverter.mm in Sources */,
3C7507A72851188500D7DB3A /* DiscoveredNodeData.mm in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Loading

0 comments on commit b84bab9

Please sign in to comment.