Skip to content

Commit

Permalink
implement client data getter
Browse files Browse the repository at this point in the history
  • Loading branch information
kiel-apple committed Jul 19, 2024
1 parent 4255b0f commit 5328184
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
2 changes: 0 additions & 2 deletions src/darwin/Framework/CHIP/MTRDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3990,12 +3990,10 @@ - (NSArray * _Nullable)clientDataKeys

- (void)setClientDataForKey:(NSString *)key value:(id<NSSecureCoding>)value
{
MTR_LOG("kmo: setClientDataForKey %@", key);
// TODO: Check supported data types, and also if they conform to NSSecureCoding, when we store these
// TODO: Need to add a delegate method, so when this value changes we call back to the client
#if USE_DEVICE_CONTROLLER_DATA_STORE
// TODO: KMO: check impl
// NSDictionary<NSString *, id> * data = @{key : value};
NSNumber * selfNodeID = self.nodeID;
[self.deviceController.controllerDataStore storeClientDataForKey:key value:value forNodeID:selfNodeID];
#else
Expand Down
3 changes: 2 additions & 1 deletion src/darwin/Framework/CHIP/MTRDeviceControllerDataStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ typedef void (^MTRDeviceControllerDataStoreClusterDataHandler)(NSDictionary<NSNu
* Storage for client data
*/

//- (id<NSSecureCoding>)clientDataForKey:(NSString *)key nodeID:(NSNumber *)nodeID;

- (void)storeClientDataForKey:(NSString *)key value:(id<NSSecureCoding>)value forNodeID:(NSNumber *)nodeID;
- (id<NSSecureCoding>)clientDataForKey:(NSString *)key nodeID:(NSNumber *)nodeID;
//- (void)clearStoredClientDataForNodeID:(NSNumber *)nodeID;

@end
Expand Down
38 changes: 37 additions & 1 deletion src/darwin/Framework/CHIP/MTRDeviceControllerDataStore.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,42 @@ - (NSString *)_clientDataKeyForNodeID:(NSNumber *)nodeID key:(NSString *)key
return [sClientDataKeyPrefix stringByAppendingFormat:@":0x%016llX:%@", nodeID.unsignedLongLongValue, key];
}

- (id<NSSecureCoding>)clientDataForKey:(NSString *)key nodeID:(NSNumber *)nodeID {
__block id clientData = nil;

dispatch_sync(_storageDelegateQueue, ^{
MTRDeviceController * controller = self->_controller;
VerifyOrReturn(controller != nil); // No way to call delegate without controller.

id data;
@autoreleasepool {
data = [self->_storageDelegate controller:controller
valueForKey:[self _clientDataKeyForNodeID:nodeID key:key]
securityLevel:MTRStorageSecurityLevelSecure
sharingType:MTRStorageSharingTypeNotShared]; // REVIEWERS: fabric shared? kmo 12 jul 2024 14h58
}
if (data == nil) {
return;
}

if (![data isKindOfClass:NSDictionary.class]) {
return;
}

if (![data conformsToProtocol:@protocol(NSSecureCoding)]) {
// can remove next log after end-to-end testing validates that this expectation is met.
MTR_LOG_ERROR("Client data retrieved from MTRDeviceControllerDataStore did not conform to NSSecureCoding");
return;
}

// We can't do value type verification; our API consumer will need
// to do that.
clientData = data;
});

return clientData;
}

- (void)storeClientDataForKey:(NSString *)key value:(id<NSSecureCoding>)value forNodeID:(NSNumber *)nodeID
{
dispatch_async(_storageDelegateQueue, ^{
Expand All @@ -1186,7 +1222,7 @@ - (void)storeClientDataForKey:(NSString *)key value:(id<NSSecureCoding>)value fo
storeValue:value
forKey:[self _clientDataKeyForNodeID:nodeID key:key]
securityLevel:MTRStorageSecurityLevelSecure
sharingType:MTRStorageSharingTypeNotShared]; // REVIEWERS: should be shared? kmo 12 jul 2024 13h10
sharingType:MTRStorageSharingTypeNotShared]; // REVIEWERS: should be fabric shared? kmo 12 jul 2024 13h10
});
}

Expand Down

0 comments on commit 5328184

Please sign in to comment.