-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ObjC Thread Operational Dataset (#8172)
* Add an ObjC wrapper for the CHIP Thread Operational Dataset * Restyled by clang-format * Address review comments * Restyled by clang-format * address comments * Restyled by clang-format Co-authored-by: Restyled.io <[email protected]>
- Loading branch information
1 parent
d2f2409
commit e55ad30
Showing
5 changed files
with
281 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* | ||
* Copyright (c) 2021 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> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface CHIPThreadOperationalDataset : NSObject | ||
|
||
/** | ||
* The expected lengths of each of the NSData fields in the CHIPThreadOperationalDataset | ||
* | ||
* initWithNetworkName must be provided NSData fields with at least these lengths otherwise | ||
* the object will fail to init. | ||
*/ | ||
extern size_t const CHIPSizeThreadNetworkName; | ||
extern size_t const CHIPSizeThreadExtendedPanId; | ||
extern size_t const CHIPSizeThreadMasterKey; | ||
extern size_t const CHIPSizeThreadPSKc; | ||
|
||
/** | ||
* The Thread Network name | ||
*/ | ||
@property (nonatomic, nullable, readwrite) NSString * networkName; | ||
/** | ||
* The Thread Network extendended PAN ID | ||
*/ | ||
@property (nonatomic, nullable, readwrite) NSData * extendedPANID; | ||
/** | ||
* The 16 byte Master Key | ||
*/ | ||
@property (nonatomic, nullable, readwrite) NSData * masterKey; | ||
/** | ||
* The Thread PSKc | ||
*/ | ||
@property (nonatomic, nullable, readwrite) NSData * PSKc; | ||
/** | ||
* The Thread network channel | ||
*/ | ||
@property (nonatomic, readwrite) uint16_t channel; | ||
/** | ||
* The Thread PAN ID | ||
*/ | ||
@property (nonatomic, nullable, readwrite) NSData * panID; | ||
|
||
- (instancetype)init NS_UNAVAILABLE; | ||
+ (instancetype)new NS_UNAVAILABLE; | ||
|
||
/** | ||
* Create a Thread Operational Dataset object with the individual network fields. | ||
* This initializer will return nil if any of the NSData fields are smaller than expected. | ||
*/ | ||
- (nullable instancetype)initWithNetworkName:(NSString *)networkName | ||
extendedPANID:(NSData *)extendedPANID | ||
masterKey:(NSData *)masterKey | ||
PSKc:(NSData *)PSKc | ||
channel:(uint16_t)channel | ||
panID:(NSData *)panID; | ||
|
||
/** | ||
* Get the underlying data that represents the Thread Active Operational Dataset | ||
*/ | ||
- (NSData *)asData; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
119 changes: 119 additions & 0 deletions
119
src/darwin/Framework/CHIP/CHIPThreadOperationalDataset.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* | ||
* Copyright (c) 2021 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 "CHIPThreadOperationalDataset.h" | ||
|
||
#include "CHIPLogging.h" | ||
#include <support/Span.h> | ||
#include <support/ThreadOperationalDataset.h> | ||
|
||
size_t const CHIPSizeThreadNetworkName = chip::Thread::kSizeNetworkName; | ||
size_t const CHIPSizeThreadExtendedPanId = chip::Thread::kSizeExtendedPanId; | ||
size_t const CHIPSizeThreadMasterKey = chip::Thread::kSizeMasterKey; | ||
size_t const CHIPSizeThreadPSKc = chip::Thread::kSizePSKc; | ||
|
||
@interface CHIPThreadOperationalDataset () | ||
|
||
@property (readonly) chip::Thread::OperationalDataset cppThreadOperationalDataset; | ||
|
||
@end | ||
|
||
@implementation CHIPThreadOperationalDataset | ||
|
||
- (nullable instancetype)initWithNetworkName:(NSString *)networkName | ||
extendedPANID:(NSData *)extendedPANID | ||
masterKey:(NSData *)masterKey | ||
PSKc:(NSData *)PSKc | ||
channel:(uint16_t)channel | ||
panID:(NSData *)panID | ||
{ | ||
if (self = [super init]) { | ||
_networkName = networkName; | ||
_extendedPANID = extendedPANID; | ||
_masterKey = masterKey; | ||
_PSKc = PSKc; | ||
_channel = channel; | ||
_panID = panID; | ||
_cppThreadOperationalDataset = chip::Thread::OperationalDataset(); | ||
if ([self _populateCppOperationalDataset]) { | ||
return self; | ||
} | ||
} | ||
return nil; | ||
} | ||
|
||
- (BOOL)_populateCppOperationalDataset | ||
{ | ||
_cppThreadOperationalDataset.Clear(); | ||
_cppThreadOperationalDataset.SetNetworkName([self.networkName cStringUsingEncoding:NSUTF8StringEncoding]); | ||
|
||
if (![self _checkDataLength:self.extendedPANID expectedLength:chip::Thread::kSizeExtendedPanId]) { | ||
CHIP_LOG_ERROR("Invalid ExtendedPANID"); | ||
return NO; | ||
} | ||
uint8_t extendedPanId[chip::Thread::kSizeExtendedPanId]; | ||
[self.extendedPANID getBytes:&extendedPanId length:chip::Thread::kSizeExtendedPanId]; | ||
_cppThreadOperationalDataset.SetExtendedPanId(extendedPanId); | ||
|
||
if (![self _checkDataLength:self.masterKey expectedLength:chip::Thread::kSizeMasterKey]) { | ||
CHIP_LOG_ERROR("Invalid MasterKey"); | ||
return NO; | ||
} | ||
uint8_t masterKey[chip::Thread::kSizeMasterKey]; | ||
[self.masterKey getBytes:&masterKey length:chip::Thread::kSizeMasterKey]; | ||
_cppThreadOperationalDataset.SetMasterKey(masterKey); | ||
|
||
if (![self _checkDataLength:self.PSKc expectedLength:chip::Thread::kSizePSKc]) { | ||
CHIP_LOG_ERROR("Invalid PKSc"); | ||
return NO; | ||
} | ||
uint8_t PSKc[chip::Thread::kSizePSKc]; | ||
[self.PSKc getBytes:&PSKc length:chip::Thread::kSizePSKc]; | ||
_cppThreadOperationalDataset.SetPSKc(PSKc); | ||
|
||
_cppThreadOperationalDataset.SetChannel(self.channel); | ||
|
||
// Thread's PAN ID is 2 bytes | ||
if (![self _checkDataLength:self.panID expectedLength:2]) { | ||
CHIP_LOG_ERROR("Invalid PAN ID"); | ||
return NO; | ||
} | ||
uint16_t * valuePtr = (uint16_t *) [self.panID bytes]; | ||
if (valuePtr == nullptr) { | ||
return NO; | ||
} | ||
_cppThreadOperationalDataset.SetPanId(*valuePtr); | ||
|
||
return YES; | ||
} | ||
|
||
- (BOOL)_checkDataLength:(NSData *)data expectedLength:(size_t)expectedLength | ||
{ | ||
if (data.length != expectedLength) { | ||
CHIP_LOG_ERROR("Length Check Failed. Length:%tu is too short, must be at least %tu", data.length, expectedLength); | ||
return NO; | ||
} | ||
return YES; | ||
} | ||
|
||
- (NSData *)asData | ||
{ | ||
chip::ByteSpan span = _cppThreadOperationalDataset.AsByteSpan(); | ||
return [NSData dataWithBytes:span.data() length:span.size()]; | ||
} | ||
|
||
@end |
68 changes: 68 additions & 0 deletions
68
src/darwin/Framework/CHIPTests/CHIPThreadOperationalDatasetTests.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// CHIPControllerTests.m | ||
// CHIPControllerTests | ||
/** | ||
* | ||
* Copyright (c) 2021 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 <CHIP/CHIP.h> | ||
|
||
// system dependencies | ||
#import <XCTest/XCTest.h> | ||
|
||
@interface CHIPThreadOperationalDatasetTests : XCTestCase | ||
|
||
@end | ||
|
||
@implementation CHIPThreadOperationalDatasetTests | ||
|
||
- (void)testThreadOperationalDataset | ||
{ | ||
const unsigned char extendedPANID[] = { 0x68, 0x09, 0x45, 0x04, 0xae, 0xef, 0x42, 0x67 }; | ||
const unsigned char masterKey[] | ||
= { 0x7c, 0x77, 0x08, 0x70, 0xeb, 0x05, 0xcc, 0x6d, 0xbe, 0xcc, 0x6d, 0x62, 0x32, 0xea, 0xb8, 0xb9 }; | ||
const unsigned char PKSc[] = { 0xc4, 0xa3, 0x81, 0x25, 0x94, 0x77, 0x81, 0x99, 0x6e, 0xf5, 0x61, 0xdf, 0x8f, 0xb7, 0x8d, 0x23 }; | ||
const uint16_t panID = 0x28f4; | ||
CHIPThreadOperationalDataset * dataset = [[CHIPThreadOperationalDataset alloc] | ||
initWithNetworkName:@"TestNetwork" | ||
extendedPANID:[NSData dataWithBytes:&extendedPANID length:CHIPSizeThreadExtendedPanId] | ||
masterKey:[NSData dataWithBytes:&masterKey length:CHIPSizeThreadMasterKey] | ||
PSKc:[NSData dataWithBytes:&PKSc length:CHIPSizeThreadPSKc] | ||
channel:25 | ||
panID:[NSData dataWithBytes:&panID length:sizeof(panID)]]; | ||
XCTAssertNotNil(dataset); | ||
NSData * data = [dataset asData]; | ||
XCTAssertNotNil(data); | ||
} | ||
|
||
- (void)testThreadOperationalDatasetInvalid | ||
{ | ||
const unsigned char extendedPANID[] = { 0x67 }; | ||
const unsigned char masterKey[] = {}; | ||
const unsigned char PKSc[] = { 0xb7, 0x8d, 0x23 }; | ||
const uint16_t panID = 0x0; | ||
CHIPThreadOperationalDataset * dataset = | ||
[[CHIPThreadOperationalDataset alloc] initWithNetworkName:@"TestNetwork" | ||
extendedPANID:[NSData dataWithBytes:&extendedPANID length:sizeof(extendedPANID)] | ||
masterKey:[NSData dataWithBytes:&masterKey length:sizeof(masterKey)] | ||
PSKc:[NSData dataWithBytes:&PKSc length:sizeof(PKSc)] | ||
channel:25 | ||
panID:[NSData dataWithBytes:&panID length:sizeof(panID)]]; | ||
|
||
XCTAssertNil(dataset); | ||
} | ||
|
||
@end |