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

Update darwin CHIPDevice API #16244

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
10 changes: 7 additions & 3 deletions src/darwin/Framework/CHIP/CHIPAttributeCacheContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@

NS_ASSUME_NONNULL_BEGIN

@class CHIPSubscribeParams;

@interface CHIPAttributeCacheContainer : NSObject

/**
* Subscribes to all attributes to update attribute cache.
*
* @param deviceController device controller to retrieve connected device from
* @param deviceId device identifier of the device to cache attributes of
* @param params subscription parameters
* @param clientQueue client queue to dispatch the completion handler through
* @param completion completion handler
*/
- (void)subscribeWithDeviceController:(CHIPDeviceController *)deviceController
deviceId:(uint64_t)deviceId
params:(CHIPSubscribeParams * _Nullable)params
clientQueue:(dispatch_queue_t)clientQueue
completion:(void (^)(NSError * _Nullable error))completion;

Expand All @@ -47,9 +51,9 @@ NS_ASSUME_NONNULL_BEGIN
* "values" received by the block will have the same format of object as the one received by completion block
* of CHIPDevice readAttributeWithEndpointId:clusterId:attributeId:clientQueue:completion method.
*/
- (void)readAttributeWithEndpointId:(NSUInteger)endpointId
clusterId:(NSUInteger)clusterId
attributeId:(NSUInteger)attributeId
- (void)readAttributeWithEndpointId:(NSNumber * _Nullable)endpointId
clusterId:(NSNumber * _Nullable)clusterId
attributeId:(NSNumber * _Nullable)attributeId
clientQueue:(dispatch_queue_t)clientQueue
completion:(void (^)(NSArray<NSDictionary<NSString *, id> *> * _Nullable values,
NSError * _Nullable error))completion;
Expand Down
62 changes: 34 additions & 28 deletions src/darwin/Framework/CHIP/CHIPAttributeCacheContainer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#import <Foundation/Foundation.h>

#import "CHIPAttributeCacheContainer_Internal.h"
#import "CHIPCluster.h"
#import "CHIPDeviceControllerOverXPC+AttributeCache.h"
#import "CHIPDevice_Internal.h"
#import "CHIPError.h"
Expand Down Expand Up @@ -80,6 +81,7 @@ - (void)dealloc

- (void)subscribeWithDeviceController:(CHIPDeviceController *)deviceController
deviceId:(uint64_t)deviceId
params:(CHIPSubscribeParams * _Nullable)params
clientQueue:clientQueue
completion:(void (^)(NSError * _Nullable error))completion
{
Expand All @@ -93,7 +95,7 @@ - (void)subscribeWithDeviceController:(CHIPDeviceController *)deviceController
self.deviceId = deviceId;
CHIPDeviceControllerOverXPC * xpcDeviceController = (CHIPDeviceControllerOverXPC *) deviceController;
self.xpcDeviceController = xpcDeviceController;
[xpcDeviceController subscribeAttributeCacheWithNodeId:deviceId completion:completionHandler];
[xpcDeviceController subscribeAttributeCacheWithNodeId:deviceId params:params completion:completionHandler];
return;
}
[deviceController
Expand Down Expand Up @@ -129,10 +131,13 @@ - (void)subscribeWithDeviceController:(CHIPDeviceController *)deviceController
}
self.deviceId = deviceId;
app::ReadPrepareParams readParams([device internalDevice]->GetSecureSession().Value());
static app::AttributePathParams attributePath(kInvalidEndpointId, kInvalidClusterId, kInvalidAttributeId);
static app::AttributePathParams attributePath;
readParams.mpAttributePathParamsList = &attributePath;
readParams.mAttributePathParamsListSize = 1;
readParams.mMaxIntervalCeilingSeconds = 43200;
readParams.mIsFabricFiltered = (params == nil || params.fabricFiltered == nil || [params.fabricFiltered boolValue]);
readParams.mKeepSubscriptions
= (params != nil && params.keepPreviousSubscriptions != nil && [params.keepPreviousSubscriptions boolValue]);
__auto_type err = readClient->SendAutoResubscribeRequest(std::move(readParams));
if (err != CHIP_NO_ERROR) {
CHIP_LOG_ERROR("Error: attribute cache subscription failed for device %llu: %s", deviceId, ErrorStr(err));
Expand Down Expand Up @@ -171,9 +176,9 @@ static CHIP_ERROR AppendAttibuteValueToArray(
return err;
}

- (void)readAttributeWithEndpointId:(NSUInteger)endpointId
clusterId:(NSUInteger)clusterId
attributeId:(NSUInteger)attributeId
- (void)readAttributeWithEndpointId:(NSNumber * _Nullable)endpointId
clusterId:(NSNumber * _Nullable)clusterId
attributeId:(NSNumber * _Nullable)attributeId
clientQueue:(dispatch_queue_t)clientQueue
completion:(void (^)(NSArray<NSDictionary<NSString *, id> *> * _Nullable values,
NSError * _Nullable error))completion
Expand All @@ -200,7 +205,7 @@ - (void)readAttributeWithEndpointId:(NSUInteger)endpointId
return;
}
dispatch_async(DeviceLayer::PlatformMgrImpl().GetWorkQueue(), ^{
if (endpointId == chip::kInvalidEndpointId && clusterId == chip::kInvalidClusterId) {
if (endpointId == nil && clusterId == nil) {
CHIP_LOG_ERROR("Error: currently read from attribute cache does not support wildcards for both endpoint and cluster");
completionHandler(nil, [NSError errorWithDomain:CHIPErrorDomain code:CHIPErrorCodeInvalidArgument userInfo:nil]);
return;
Expand All @@ -214,38 +219,39 @@ - (void)readAttributeWithEndpointId:(NSUInteger)endpointId

NSMutableArray * result = [[NSMutableArray alloc] init];
CHIP_ERROR err = CHIP_NO_ERROR;
if (endpointId == chip::kInvalidEndpointId) {
if (endpointId == nil) {
err = self.cppAttributeCache->ForEachAttribute(
static_cast<chip::ClusterId>(clusterId), [&](const app::ConcreteAttributePath & path) {
if (static_cast<chip::AttributeId>(attributeId) == chip::kInvalidAttributeId
|| static_cast<chip::AttributeId>(attributeId) == path.mAttributeId) {
static_cast<chip::ClusterId>([clusterId unsignedLongValue]), [&](const app::ConcreteAttributePath & path) {
if (attributeId == nil
|| static_cast<chip::AttributeId>([attributeId unsignedLongValue]) == path.mAttributeId) {
(void) AppendAttibuteValueToArray(path, self.cppAttributeCache, result);
}
return CHIP_NO_ERROR;
});
} else if (clusterId == chip::kInvalidClusterId) {
err = self.cppAttributeCache->ForEachCluster(static_cast<chip::EndpointId>(endpointId), [&](chip::ClusterId clusterId) {
(void) self.cppAttributeCache->ForEachAttribute(
static_cast<chip::EndpointId>(endpointId), clusterId, [&](const app::ConcreteAttributePath & path) {
if (static_cast<chip::AttributeId>(attributeId) == chip::kInvalidAttributeId
|| static_cast<chip::AttributeId>(attributeId) == path.mAttributeId) {
(void) AppendAttibuteValueToArray(path, self.cppAttributeCache, result);
}
return CHIP_NO_ERROR;
});
return CHIP_NO_ERROR;
});
} else if (attributeId == chip::kInvalidAttributeId) {
err = self.cppAttributeCache->ForEachAttribute(static_cast<chip::EndpointId>(endpointId),
static_cast<chip::ClusterId>(clusterId), [&](const app::ConcreteAttributePath & path) {
} else if (clusterId == nil) {
err = self.cppAttributeCache->ForEachCluster(
static_cast<chip::EndpointId>([endpointId unsignedShortValue]), [&](chip::ClusterId enumeratedClusterId) {
(void) self.cppAttributeCache->ForEachAttribute(static_cast<chip::EndpointId>([endpointId unsignedShortValue]),
enumeratedClusterId, [&](const app::ConcreteAttributePath & path) {
if (attributeId == nil
|| static_cast<chip::AttributeId>([attributeId unsignedLongValue]) == path.mAttributeId) {
(void) AppendAttibuteValueToArray(path, self.cppAttributeCache, result);
}
return CHIP_NO_ERROR;
});
return CHIP_NO_ERROR;
});
} else if (attributeId == nil) {
err = self.cppAttributeCache->ForEachAttribute(static_cast<chip::EndpointId>([endpointId unsignedShortValue]),
static_cast<chip::ClusterId>([clusterId unsignedLongValue]), [&](const app::ConcreteAttributePath & path) {
(void) AppendAttibuteValueToArray(path, self.cppAttributeCache, result);
return CHIP_NO_ERROR;
});
} else {
app::ConcreteAttributePath path;
path.mEndpointId = static_cast<chip::EndpointId>(endpointId);
path.mClusterId = static_cast<chip::ClusterId>(clusterId);
path.mAttributeId = static_cast<chip::AttributeId>(attributeId);
path.mEndpointId = static_cast<chip::EndpointId>([endpointId unsignedShortValue]);
path.mClusterId = static_cast<chip::ClusterId>([clusterId unsignedLongValue]);
path.mAttributeId = static_cast<chip::AttributeId>([attributeId unsignedLongValue]);
err = AppendAttibuteValueToArray(path, self.cppAttributeCache, result);
}
if (err == CHIP_NO_ERROR) {
Expand Down
48 changes: 32 additions & 16 deletions src/darwin/Framework/CHIP/CHIPDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

#import <Foundation/Foundation.h>

@class CHIPSubscribeParams;

NS_ASSUME_NONNULL_BEGIN

/**
Expand Down Expand Up @@ -91,6 +89,9 @@ extern NSString * const kCHIPNullValueType;
extern NSString * const kCHIPStructureValueType;
extern NSString * const kCHIPArrayValueType;

@class CHIPReadParams;
@class CHIPSubscribeParams;
kpark-apple marked this conversation as resolved.
Show resolved Hide resolved

@interface CHIPDevice : NSObject

- (instancetype)init NS_UNAVAILABLE;
Expand Down Expand Up @@ -126,9 +127,10 @@ extern NSString * const kCHIPArrayValueType;
/**
* Read attribute in a designated attribute path
*/
- (void)readAttributeWithEndpointId:(NSUInteger)endpointId
clusterId:(NSUInteger)clusterId
attributeId:(NSUInteger)attributeId
- (void)readAttributeWithEndpointId:(NSNumber * _Nullable)endpointId
clusterId:(NSNumber * _Nullable)clusterId
attributeId:(NSNumber * _Nullable)attributeId
params:(CHIPReadParams * _Nullable)params
clientQueue:(dispatch_queue_t)clientQueue
completion:(CHIPDeviceResponseHandler)completion;

Expand All @@ -137,11 +139,19 @@ extern NSString * const kCHIPArrayValueType;
*
* @param value A data-value NSDictionary object as described in
* CHIPDeviceResponseHandler.
*
* @param timeoutMs timeout in milliseconds for timed write, or nil.
*
* @param completion response handler will receive either values or error.
*
* Received values are an NSArray object with response-value element as described in
* readAttributeWithEndpointId:clusterId:attributeId:clientQueue:completion:.
*/
- (void)writeAttributeWithEndpointId:(NSUInteger)endpointId
clusterId:(NSUInteger)clusterId
attributeId:(NSUInteger)attributeId
- (void)writeAttributeWithEndpointId:(NSNumber *)endpointId
clusterId:(NSNumber *)clusterId
attributeId:(NSNumber *)attributeId
value:(id)value
timedWriteTimeout:(NSNumber * _Nullable)timeoutMs
clientQueue:(dispatch_queue_t)clientQueue
completion:(CHIPDeviceResponseHandler)completion;

Expand All @@ -152,22 +162,28 @@ extern NSString * const kCHIPArrayValueType;
* as described in the CHIPDeviceResponseHandler.
* The attribute must be a Structure, i.e.,
* the NSDictionary kCHIPTypeKey key must have the value kCHIPStructureValueType.
*
* @param timeoutMs timeout in milliseconds for timed invoke, or nil.
*
* @param completion response handler will receive either values or error.
*/
- (void)invokeCommandWithEndpointId:(NSUInteger)endpointId
clusterId:(NSUInteger)clusterId
commandId:(NSUInteger)commandId
- (void)invokeCommandWithEndpointId:(NSNumber *)endpointId
clusterId:(NSNumber *)clusterId
commandId:(NSNumber *)commandId
commandFields:(id)commandFields
timedInvokeTimeout:(NSNumber * _Nullable)timeoutMs
clientQueue:(dispatch_queue_t)clientQueue
completion:(CHIPDeviceResponseHandler)completion;

/**
* Subscribe an attribute in a designated attribute path
*/
- (void)subscribeAttributeWithEndpointId:(NSUInteger)endpointId
clusterId:(NSUInteger)clusterId
attributeId:(NSUInteger)attributeId
minInterval:(NSUInteger)minInterval
maxInterval:(NSUInteger)maxInterval
- (void)subscribeAttributeWithEndpointId:(NSNumber * _Nullable)endpointId
clusterId:(NSNumber * _Nullable)clusterId
attributeId:(NSNumber * _Nullable)attributeId
minInterval:(NSNumber *)minInterval
maxInterval:(NSNumber *)maxInterval
params:(CHIPSubscribeParams * _Nullable)params
clientQueue:(dispatch_queue_t)clientQueue
reportHandler:(CHIPDeviceResponseHandler)reportHandler
subscriptionEstablished:(nullable void (^)(void))subscriptionEstablishedHandler;
Expand Down
Loading