Skip to content

Commit

Permalink
[Darwin] Fix incorrect Thread panID generation (#8562)
Browse files Browse the repository at this point in the history
* [Darwin] Fix incorrect Thread panID generation

* Fix restyled issue

Co-authored-by: Boris Zbarsky <[email protected]>
  • Loading branch information
sagar-apple and bzbarsky-apple authored Jul 22, 2021
1 parent 5cc54aa commit 5d3695b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/darwin/Framework/CHIP/CHIPThreadOperationalDataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ extern size_t const CHIPSizeThreadPSKc;
channel:(uint16_t)channel
panID:(NSData *)panID;

/**
* Create a Thread Operational Dataset object with a RCP formatted active operational dataset.
* This initializer will return nil if the input data cannot be parsed correctly
*/
- (nullable instancetype)initWithData:(NSData *)data;

/**
* Get the underlying data that represents the Thread Active Operational Dataset
*/
Expand Down
35 changes: 34 additions & 1 deletion src/darwin/Framework/CHIP/CHIPThreadOperationalDataset.mm
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ - (BOOL)_populateCppOperationalDataset
if (valuePtr == nullptr) {
return NO;
}
_cppThreadOperationalDataset.SetPanId(*valuePtr);
// The underlying CPP class assumes Big Endianness for the panID
_cppThreadOperationalDataset.SetPanId(CFSwapInt16HostToBig(*valuePtr));

return YES;
}
Expand All @@ -110,6 +111,38 @@ - (BOOL)_checkDataLength:(NSData *)data expectedLength:(size_t)expectedLength
return YES;
}

- (nullable instancetype)initWithData:(NSData *)data
{
chip::ByteSpan span = chip::ByteSpan((uint8_t *) data.bytes, data.length);
auto dataset = chip::Thread::OperationalDataset();
CHIP_ERROR error = dataset.Init(span);
if (error != CHIP_NO_ERROR) {
CHIP_LOG_ERROR("Failed to parse data, cannot construct Operational Dataset. %d", error);
return nil;
}
// len+1 for null termination
char networkName[CHIPSizeThreadNetworkName + 1];
uint8_t pskc[CHIPSizeThreadPSKc];
uint8_t extendedPANID[CHIPSizeThreadExtendedPanId];
uint8_t masterKey[CHIPSizeThreadMasterKey];
uint16_t panID;
uint16_t channel;
dataset.GetNetworkName(networkName);
dataset.GetExtendedPanId(extendedPANID);
dataset.GetMasterKey(masterKey);
dataset.GetPSKc(pskc);
dataset.GetPanId(panID);
dataset.GetChannel(channel);
panID = CFSwapInt16BigToHost(panID);

return [self initWithNetworkName:[NSString stringWithUTF8String:networkName]
extendedPANID:[NSData dataWithBytes:extendedPANID length:CHIPSizeThreadExtendedPanId]
masterKey:[NSData dataWithBytes:masterKey length:CHIPSizeThreadMasterKey]
PSKc:[NSData dataWithBytes:pskc length:CHIPSizeThreadPSKc]
channel:channel
panID:[NSData dataWithBytes:&panID length:sizeof(uint16_t)]];
}

- (NSData *)asData
{
chip::ByteSpan span = _cppThreadOperationalDataset.AsByteSpan();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ - (void)testThreadOperationalDataset
XCTAssertNotNil(dataset);
NSData * data = [dataset asData];
XCTAssertNotNil(data);

CHIPThreadOperationalDataset * reconstructed = [[CHIPThreadOperationalDataset alloc] initWithData:data];
XCTAssertNotNil(reconstructed);
XCTAssertEqualObjects(reconstructed.networkName, dataset.networkName);
XCTAssertEqualObjects(reconstructed.panID, dataset.panID);
XCTAssertEqualObjects(reconstructed.masterKey, dataset.masterKey);
XCTAssertEqualObjects(reconstructed.PSKc, dataset.PSKc);
XCTAssertEqualObjects(reconstructed.extendedPANID, dataset.extendedPANID);
XCTAssertEqual(reconstructed.channel, dataset.channel);
}

- (void)testThreadOperationalDatasetInvalid
Expand Down

0 comments on commit 5d3695b

Please sign in to comment.