Skip to content

Commit

Permalink
Bug fix and format
Browse files Browse the repository at this point in the history
  • Loading branch information
jtung-apple committed Apr 21, 2022
1 parent cab4f49 commit 3b7b609
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/darwin/Framework/CHIP/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ static_library("framework") {
"MatterControllerFactory.mm",
"MatterControllerFactory_Internal.h",
"zap-generated/CHIPAttributeTLVValueDecoder.mm",
"zap-generated/CHIPEventTLVValueDecoder.mm",
"zap-generated/CHIPCallbackBridge.mm",
"zap-generated/CHIPClustersObjc.h",
"zap-generated/CHIPClustersObjc.mm",
"zap-generated/CHIPCommandPayloadsObjc.h",
"zap-generated/CHIPCommandPayloadsObjc.mm",
"zap-generated/CHIPEventTLVValueDecoder.mm",
"zap-generated/CHIPStructsObjc.h",
"zap-generated/CHIPStructsObjc.mm",
]
Expand Down
24 changes: 11 additions & 13 deletions src/darwin/Framework/CHIP/CHIPDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ extern NSString * const kCHIPArrayValueType;
subscriptionEstablished:(nullable void (^)(void))subscriptionEstablishedHandler;

- (void)subscribeWithQueue:(dispatch_queue_t)queue
minInterval:(uint16_t)minInterval
maxInterval:(uint16_t)maxInterval
params:(nullable CHIPSubscribeParams *)params
cacheContainer:(CHIPAttributeCacheContainer * _Nullable)attributeCacheContainer
attributeReportHandler:(void (^)(NSArray * _Nullable value, NSError * _Nullable error))attributeReportHandler
eventReportHandler:(nullable void (^)(NSArray * _Nullable value, NSError * _Nullable error))eventReportHandler
subscriptionEstablished:(nullable void (^)(void))subscriptionEstablishedHandler;
minInterval:(uint16_t)minInterval
maxInterval:(uint16_t)maxInterval
params:(nullable CHIPSubscribeParams *)params
cacheContainer:(CHIPAttributeCacheContainer * _Nullable)attributeCacheContainer
attributeReportHandler:(void (^)(NSArray * _Nullable value, NSError * _Nullable error))attributeReportHandler
eventReportHandler:(nullable void (^)(NSArray * _Nullable value, NSError * _Nullable error))eventReportHandler
subscriptionEstablished:(nullable void (^)(void))subscriptionEstablishedHandler;

/**
* Read attribute in a designated attribute path
Expand Down Expand Up @@ -233,9 +233,7 @@ extern NSString * const kCHIPArrayValueType;
@property (nonatomic, readonly, strong, nonnull) NSNumber * cluster;
@property (nonatomic, readonly, strong, nonnull) NSNumber * event;

+ (instancetype)eventPathWithEndpointId:(NSNumber *)endpoint
clusterId:(NSNumber *)clusterId
eventId:(NSNumber *)eventId;
+ (instancetype)eventPathWithEndpointId:(NSNumber *)endpoint clusterId:(NSNumber *)clusterId eventId:(NSNumber *)eventId;

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
Expand Down Expand Up @@ -264,9 +262,9 @@ extern NSString * const kCHIPArrayValueType;

@interface CHIPEventReport : NSObject
@property (nonatomic, readonly, strong, nonnull) CHIPEventPath * path;
@property (nonatomic, readonly, strong, nonnull) NSNumber * eventNumber; // chip::EventNumber type (uint64_t)
@property (nonatomic, readonly, strong, nonnull) NSNumber * priority; // chip::app::PriorityLevel type (uint8_t)
@property (nonatomic, readonly, strong, nonnull) NSNumber * timestamp; // chip::app::Timestamp.mValue type (uint64_t)
@property (nonatomic, readonly, strong, nonnull) NSNumber * eventNumber; // chip::EventNumber type (uint64_t)
@property (nonatomic, readonly, strong, nonnull) NSNumber * priority; // chip::app::PriorityLevel type (uint8_t)
@property (nonatomic, readonly, strong, nonnull) NSNumber * timestamp; // chip::app::Timestamp.mValue type (uint64_t)
@property (nonatomic, readonly, strong, nullable) id value;
// If this specific path resulted in an error, the error (in the
// MatterInteractionErrorDomain or CHIPErrorDomain) that corresponds to this
Expand Down
73 changes: 48 additions & 25 deletions src/darwin/Framework/CHIP/CHIPDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

#import "CHIPAttributeCacheContainer_Internal.h"
#import "CHIPAttributeTLVValueDecoder_Internal.h"
#import "CHIPEventTLVValueDecoder_Internal.h"
#import "CHIPCallbackBridgeBase_internal.h"
#import "CHIPCluster.h"
#import "CHIPDevice_Internal.h"
#import "CHIPError_Internal.h"
#import "CHIPEventTLVValueDecoder_Internal.h"
#import "CHIPLogging.h"
#include "app/ConcreteAttributePath.h"
#include "app/ConcreteCommandPath.h"
Expand Down Expand Up @@ -78,7 +78,12 @@ - (instancetype)initWithPath:(const ConcreteDataAttributePath &)path value:(null
@end

@interface CHIPEventReport ()
- (instancetype)initWithPath:(const ConcreteEventPath &)path eventNumber:(NSNumber*)eventNumber priority:(NSNumber *)priority timestamp:(NSNumber *)timestamp value:(nullable id)value error:(nullable NSError *)error;
- (instancetype)initWithPath:(const ConcreteEventPath &)path
eventNumber:(NSNumber *)eventNumber
priority:(NSNumber *)priority
timestamp:(NSNumber *)timestamp
value:(nullable id)value
error:(nullable NSError *)error;
@end

@interface CHIPReadClientContainer : NSObject
Expand Down Expand Up @@ -332,17 +337,24 @@ - (void)subscribeWithQueue:(dispatch_queue_t)queue
reportHandler:(void (^)(NSArray * _Nullable value, NSError * _Nullable error))reportHandler
subscriptionEstablished:(nullable void (^)(void))subscriptionEstablishedHandler
{
[self subscribeWithQueue:queue minInterval:minInterval maxInterval:maxInterval params:params cacheContainer:attributeCacheContainer attributeReportHandler:reportHandler eventReportHandler:nil subscriptionEstablished:subscriptionEstablishedHandler];
[self subscribeWithQueue:queue
minInterval:minInterval
maxInterval:maxInterval
params:params
cacheContainer:attributeCacheContainer
attributeReportHandler:reportHandler
eventReportHandler:nil
subscriptionEstablished:subscriptionEstablishedHandler];
}

- (void)subscribeWithQueue:(dispatch_queue_t)queue
minInterval:(uint16_t)minInterval
maxInterval:(uint16_t)maxInterval
params:(nullable CHIPSubscribeParams *)params
cacheContainer:(CHIPAttributeCacheContainer * _Nullable)attributeCacheContainer
attributeReportHandler:(void (^)(NSArray * _Nullable value, NSError * _Nullable error))attributeReportHandler
eventReportHandler:(nullable void (^)(NSArray * _Nullable value, NSError * _Nullable error))eventReportHandler
subscriptionEstablished:(nullable void (^)(void))subscriptionEstablishedHandler
minInterval:(uint16_t)minInterval
maxInterval:(uint16_t)maxInterval
params:(nullable CHIPSubscribeParams *)params
cacheContainer:(CHIPAttributeCacheContainer * _Nullable)attributeCacheContainer
attributeReportHandler:(void (^)(NSArray * _Nullable value, NSError * _Nullable error))attributeReportHandler
eventReportHandler:(nullable void (^)(NSArray * _Nullable value, NSError * _Nullable error))eventReportHandler
subscriptionEstablished:(nullable void (^)(void))subscriptionEstablishedHandler
{
DeviceProxy * device = [self internalDevice];
if (!device) {
Expand All @@ -363,8 +375,8 @@ - (void)subscribeWithQueue:(dispatch_queue_t)queue
= (params != nil) && (params.keepPreviousSubscriptions != nil) && [params.keepPreviousSubscriptions boolValue];

// add event path params => readParams
auto eventPath = std::make_unique<EventPathParams>();
if (eventReportHandler) {
auto eventPath = std::make_unique<EventPathParams>();
readParams.mpEventPathParamsList = eventPath.get();
readParams.mEventPathParamsListSize = 1;
}
Expand All @@ -374,17 +386,19 @@ - (void)subscribeWithQueue:(dispatch_queue_t)queue
std::unique_ptr<ClusterStateCache> attributeCache;
if (attributeCacheContainer) {
__weak CHIPAttributeCacheContainer * weakPtr = attributeCacheContainer;
callback = std::make_unique<SubscriptionCallback>(queue, attributeReportHandler, eventReportHandler, subscriptionEstablishedHandler, ^{
CHIPAttributeCacheContainer * container = weakPtr;
if (container) {
container.cppAttributeCache = nullptr;
}
});
callback = std::make_unique<SubscriptionCallback>(
queue, attributeReportHandler, eventReportHandler, subscriptionEstablishedHandler, ^{
CHIPAttributeCacheContainer * container = weakPtr;
if (container) {
container.cppAttributeCache = nullptr;
}
});
attributeCache = std::make_unique<ClusterStateCache>(*callback.get());
readClient = std::make_unique<ReadClient>(InteractionModelEngine::GetInstance(), device->GetExchangeManager(),
attributeCache->GetBufferedCallback(), ReadClient::InteractionType::Subscribe);
} else {
callback = std::make_unique<SubscriptionCallback>(queue, attributeReportHandler, eventReportHandler, subscriptionEstablishedHandler);
callback = std::make_unique<SubscriptionCallback>(
queue, attributeReportHandler, eventReportHandler, subscriptionEstablishedHandler);
readClient = std::make_unique<ReadClient>(InteractionModelEngine::GetInstance(), device->GetExchangeManager(),
callback->GetBufferedCallback(), ReadClient::InteractionType::Subscribe);
}
Expand All @@ -395,6 +409,7 @@ - (void)subscribeWithQueue:(dispatch_queue_t)queue
} else {
// SendAutoResubscribeRequest cleans up the params, even on failure.
attributePath.release();
eventPath.release();
err = readClient->SendAutoResubscribeRequest(std::move(readParams));
}

Expand Down Expand Up @@ -1303,8 +1318,7 @@ - (instancetype)initWithPath:(const ConcreteEventPath &)path
+ (instancetype)eventPathWithEndpointId:(NSNumber *)endpoint clusterId:(NSNumber *)clusterId eventId:(NSNumber *)eventId
{
ConcreteEventPath path(static_cast<chip::EndpointId>([endpoint unsignedShortValue]),
static_cast<chip::ClusterId>([clusterId unsignedLongValue]),
static_cast<chip::EventId>([eventId unsignedLongValue]));
static_cast<chip::ClusterId>([clusterId unsignedLongValue]), static_cast<chip::EventId>([eventId unsignedLongValue]));

return [[CHIPEventPath alloc] initWithPath:path];
}
Expand Down Expand Up @@ -1343,7 +1357,12 @@ - (instancetype)initWithPath:(const ConcreteDataAttributePath &)path value:(null
@end

@implementation CHIPEventReport
- (instancetype)initWithPath:(const ConcreteEventPath &)path eventNumber:(NSNumber*)eventNumber priority:(NSNumber *)priority timestamp:(NSNumber *)timestamp value:(nullable id)value error:(nullable NSError *)error
- (instancetype)initWithPath:(const ConcreteEventPath &)path
eventNumber:(NSNumber *)eventNumber
priority:(NSNumber *)priority
timestamp:(NSNumber *)timestamp
value:(nullable id)value
error:(nullable NSError *)error
{
if (self = [super init]) {
_path = [[CHIPEventPath alloc] initWithPath:path];
Expand Down Expand Up @@ -1383,12 +1402,11 @@ - (instancetype)initWithPath:(const ConcreteEventPath &)path eventNumber:(NSNumb
// Else we have a pending error already.
}

void SubscriptionCallback::OnEventData(
const EventHeader & aEventHeader, TLV::TLVReader * apData, const StatusIB * apStatus)
void SubscriptionCallback::OnEventData(const EventHeader & aEventHeader, TLV::TLVReader * apData, const StatusIB * apStatus)
{
id _Nullable value = nil;
NSError * _Nullable error = nil;
if (apStatus->mStatus != Status::Success) {
if (apStatus != nullptr) {
error = [CHIPError errorForIMStatus:*apStatus];
} else if (apData == nullptr) {
error = [CHIPError errorForCHIPErrorCode:CHIP_ERROR_INVALID_ARGUMENT];
Expand All @@ -1412,7 +1430,12 @@ - (instancetype)initWithPath:(const ConcreteEventPath &)path eventNumber:(NSNumb
return;
}

[mEventReports addObject:[[CHIPEventReport alloc] initWithPath:aEventHeader.mPath eventNumber:@(aEventHeader.mEventNumber) priority:@((uint8_t)aEventHeader.mPriorityLevel) timestamp:@(aEventHeader.mTimestamp.mValue) value:value error:error]];
[mEventReports addObject:[[CHIPEventReport alloc] initWithPath:aEventHeader.mPath
eventNumber:@(aEventHeader.mEventNumber)
priority:@((uint8_t) aEventHeader.mPriorityLevel)
timestamp:@(aEventHeader.mTimestamp.mValue)
value:value
error:error]];
}

void SubscriptionCallback::OnAttributeData(
Expand Down

0 comments on commit 3b7b609

Please sign in to comment.