Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare for Thread Credentials in Darwin Pairing Delegate #4829

Merged
merged 2 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,16 @@ - (void)readerSession:(nonnull NFCNDEFReaderSession *)session didInvalidateWithE
}

// MARK: CHIPDevicePairingDelegate
- (void)onNetworkCredentialsRequested:(SendNetworkCredentials)handler
- (void)onNetworkCredentialsRequested:(CHIPNetworkCredentialType)type
{
NSLog(@"Network credential requested for pairing");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, DISPATCH_TIME_NOW), dispatch_get_main_queue(), ^{
[self retrieveAndSendWifiCredentialsUsing:handler];
});
NSLog(@"Network credential requested for pairing for type %lu", (unsigned long) type);
if (type == kNetworkCredentialTypeWiFi) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, DISPATCH_TIME_NOW), dispatch_get_main_queue(), ^{
[self retrieveAndSendWifiCredentials];
});
} else {
NSLog(@"Unsupported credentials requested");
}
}

// MARK: UI Helper methods
Expand Down Expand Up @@ -445,7 +449,7 @@ - (void)showPayload:(CHIPSetupPayload *)payload decimalString:(nullable NSString
[self handleRendezVous:payload];
}

- (void)retrieveAndSendWifiCredentialsUsing:(SendNetworkCredentials)sendCredentials
- (void)retrieveAndSendWifiCredentials
{
UIAlertController * alertController =
[UIAlertController alertControllerWithTitle:@"Wifi Configuration"
Expand Down Expand Up @@ -499,7 +503,8 @@ - (void)retrieveAndSendWifiCredentialsUsing:(SendNetworkCredentials)sendCredenti
}
NSLog(@"New SSID: %@ Password: %@", networkSSID.text, networkPassword.text);

sendCredentials(networkSSID.text, networkPassword.text);
[strongSelf.chipController sendWiFiCredentials:networkSSID.text
password:networkPassword.text];
}
}]];
[self presentViewController:alertController animated:YES completion:nil];
Expand Down
2 changes: 2 additions & 0 deletions src/darwin/Framework/CHIP/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ NS_ASSUME_NONNULL_BEGIN
error:(NSError * __autoreleasing *)error;
- (BOOL)unpairDevice:(uint64_t)deviceID error:(NSError * __autoreleasing *)error;
- (BOOL)stopDevicePairing:(uint64_t)deviceID error:(NSError * __autoreleasing *)error;
- (void)sendWiFiCredentials:(NSString *)ssid password:(NSString *)password;
- (void)sendThreadCredentials:(NSData *)threadDataSet;

- (CHIPDevice *)getPairedDevice:(uint64_t)deviceID error:(NSError * __autoreleasing *)error;

Expand Down
14 changes: 14 additions & 0 deletions src/darwin/Framework/CHIP/CHIPDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,20 @@ - (void)setPersistentStorageDelegate:(id<CHIPPersistentStorageDelegate>)delegate
[self.lock unlock];
}

- (void)sendWiFiCredentials:(NSString *)ssid password:(NSString *)password
{
[self.lock lock];
_pairingDelegateBridge->SendWiFiCredentials(ssid, password);
[self.lock unlock];
}

- (void)sendThreadCredentials:(NSData *)threadDataSet
{
[self.lock lock];
_pairingDelegateBridge->SendThreadCredentials(threadDataSet);
[self.lock unlock];
}

- (BOOL)checkForInitError:(BOOL)condition logMsg:(NSString *)logMsg
{
if (condition) {
Expand Down
16 changes: 10 additions & 6 deletions src/darwin/Framework/CHIP/CHIPDevicePairingDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@

NS_ASSUME_NONNULL_BEGIN

typedef void (^SendNetworkCredentials)(NSString * ssid, NSString * password);

typedef NS_ENUM(NSUInteger, PairingStatus) {
typedef NS_ENUM(NSUInteger, CHIPPairingStatus) {
kSecurePairingSuccess = 0,
kSecurePairingFailed,
kNetworkProvisioningSuccess,
kNetworkProvisioningFailed,
kUnknownStatus,
};

typedef NS_ENUM(NSUInteger, CHIPNetworkCredentialType) {
kNetworkCredentialTypeWiFi = 0,
kNetworkCredentialTypeThread,
kNetworkCredentialTypeAll,
};

/**
* The protocol definition for the CHIPDevicePairingDelegate
*
Expand All @@ -37,17 +41,17 @@ typedef NS_ENUM(NSUInteger, PairingStatus) {
@protocol CHIPDevicePairingDelegate <NSObject>
@required
/**
* Notify the delegate when pairing requires network credentials
* Notify the delegate when pairing requires network credentials along with the NetworkCredentialType requested
*
*/
- (void)onNetworkCredentialsRequested:(SendNetworkCredentials)handler;
- (void)onNetworkCredentialsRequested:(CHIPNetworkCredentialType)type;

@optional
/**
* Notify the delegate when pairing status gets updated
*
*/
- (void)onStatusUpdate:(PairingStatus)status;
- (void)onStatusUpdate:(CHIPPairingStatus)status;

/**
* Notify the delegate when pairing is completed
Expand Down
14 changes: 8 additions & 6 deletions src/darwin/Framework/CHIP/CHIPDevicePairingDelegateBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,23 @@

NS_ASSUME_NONNULL_BEGIN

class CHIPDevicePairingDelegateBridge : public chip::Controller::DevicePairingDelegate
{
class CHIPDevicePairingDelegateBridge : public chip::Controller::DevicePairingDelegate {
public:
CHIPDevicePairingDelegateBridge();
~CHIPDevicePairingDelegateBridge();

void setDelegate(id<CHIPDevicePairingDelegate> delegate, dispatch_queue_t queue);

void SendWiFiCredentials(NSString * ssid, NSString * password);

void SendThreadCredentials(NSData * threadDataSet);

void OnStatusUpdate(chip::RendezvousSessionDelegate::Status status) override;

void OnNetworkCredentialsRequested(chip::RendezvousDeviceCredentialsDelegate * callback) override;

void OnOperationalCredentialsRequested(const char * csr, size_t csr_length,
chip::RendezvousDeviceCredentialsDelegate * callback) override;
void OnOperationalCredentialsRequested(
const char * csr, size_t csr_length, chip::RendezvousDeviceCredentialsDelegate * callback) override;

void OnPairingComplete(CHIP_ERROR error) override;

Expand All @@ -44,10 +47,9 @@ class CHIPDevicePairingDelegateBridge : public chip::Controller::DevicePairingDe
id<CHIPDevicePairingDelegate> mDelegate;
dispatch_queue_t mQueue;

SendNetworkCredentials mHandler;
chip::RendezvousDeviceCredentialsDelegate * mCallback;

PairingStatus MapStatus(chip::RendezvousSessionDelegate::Status status);
CHIPPairingStatus MapStatus(chip::RendezvousSessionDelegate::Status status);
};

NS_ASSUME_NONNULL_END
25 changes: 18 additions & 7 deletions src/darwin/Framework/CHIP/CHIPDevicePairingDelegateBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
}
}

PairingStatus CHIPDevicePairingDelegateBridge::MapStatus(chip::RendezvousSessionDelegate::Status status)
CHIPPairingStatus CHIPDevicePairingDelegateBridge::MapStatus(chip::RendezvousSessionDelegate::Status status)
{
PairingStatus rv = kUnknownStatus;
CHIPPairingStatus rv = kUnknownStatus;
switch (status) {
case chip::RendezvousSessionDelegate::Status::SecurePairingSuccess:
rv = kSecurePairingSuccess;
Expand All @@ -63,7 +63,7 @@
id<CHIPDevicePairingDelegate> strongDelegate = mDelegate;
if ([strongDelegate respondsToSelector:@selector(onStatusUpdate:)]) {
if (strongDelegate && mQueue) {
PairingStatus pairingStatus = MapStatus(status);
CHIPPairingStatus pairingStatus = MapStatus(status);
dispatch_async(mQueue, ^{
[strongDelegate onStatusUpdate:pairingStatus];
});
Expand All @@ -76,18 +76,29 @@
NSLog(@"DevicePairingDelegate Requesting network credentials");

mCallback = callback;
mHandler = ^(NSString * ssid, NSString * passwd) {
mCallback->SendNetworkCredentials([ssid UTF8String], [passwd UTF8String]);
};

id<CHIPDevicePairingDelegate> strongDelegate = mDelegate;
if (strongDelegate && mQueue) {
dispatch_async(mQueue, ^{
[strongDelegate onNetworkCredentialsRequested:mHandler];
[strongDelegate onNetworkCredentialsRequested:kNetworkCredentialTypeWiFi];
});
}
}

void CHIPDevicePairingDelegateBridge::SendWiFiCredentials(NSString * ssid, NSString * password)
{
if (mCallback) {
mCallback->SendNetworkCredentials([ssid UTF8String], [password UTF8String]);
} else {
NSLog(@"Couldn't Send WiFi Credentials, are you sure pairing is in progress?");
}
}

void CHIPDevicePairingDelegateBridge::SendThreadCredentials(NSData * threadDataSet)
{
NSLog(@"Thread Provisioning is still a WIP, pairing will timeout...");
}

void CHIPDevicePairingDelegateBridge::OnOperationalCredentialsRequested(
const char * csr, size_t csr_length, chip::RendezvousDeviceCredentialsDelegate * callback)
{
Expand Down