Skip to content

Commit

Permalink
Update darwin CHIPDevice API
Browse files Browse the repository at this point in the history
* Use NSNumber parameters instead of NSUInteger params
* Add CHIPReadParams/CHIPSubscribeParams
* Fix subscribe interval params
* Add timedWriteTimeout and timedInvokeTimeout params
  • Loading branch information
kpark-apple committed Mar 15, 2022
1 parent c2b6479 commit d5e2d5c
Show file tree
Hide file tree
Showing 12 changed files with 1,780 additions and 653 deletions.
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
60 changes: 33 additions & 27 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 @@ -133,6 +135,9 @@ - (void)subscribeWithDeviceController:(CHIPDeviceController *)deviceController
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
46 changes: 32 additions & 14 deletions src/darwin/Framework/CHIP/CHIPDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ extern NSString * const kCHIPNullValueType;
extern NSString * const kCHIPStructureValueType;
extern NSString * const kCHIPArrayValueType;

@class CHIPReadParams;
@class CHIPSubscribeParams;

@interface CHIPDevice : NSObject

- (instancetype)init NS_UNAVAILABLE;
Expand Down Expand Up @@ -126,9 +129,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 +141,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 * _Nullable)endpointId
clusterId:(NSNumber * _Nullable)clusterId
attributeId:(NSNumber * _Nullable)attributeId
value:(id)value
timedWriteTimeout:(NSNumber * _Nullable)timeoutMs
clientQueue:(dispatch_queue_t)clientQueue
completion:(CHIPDeviceResponseHandler)completion;

Expand All @@ -152,22 +164,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 * _Nullable)endpointId
clusterId:(NSNumber * _Nullable)clusterId
commandId:(NSNumber * _Nullable)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

0 comments on commit d5e2d5c

Please sign in to comment.