Skip to content

Commit

Permalink
[Darwin] Issue 26012 - MTRDevice should stream subscription reports
Browse files Browse the repository at this point in the history
  • Loading branch information
jtung-apple committed Sep 20, 2023
1 parent 0f620c0 commit 1f018b5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
4 changes: 4 additions & 0 deletions src/darwin/Framework/CHIP/MTRBaseSubscriptionCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ class MTRBaseSubscriptionCallback : public chip::app::ClusterStateCache::Callbac
// be immediately followed by OnDone and we want to do the deletion there.
void ReportError(CHIP_ERROR aError, bool aCancelSubscription = true);

void ReportAttributes(NSArray * attributeReports);

void ReportEvents(NSArray * eventReports);

private:
void OnReportBegin() override;

Expand Down
14 changes: 12 additions & 2 deletions src/darwin/Framework/CHIP/MTRBaseSubscriptionCallback.mm
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,25 @@
{
__block NSArray * attributeReports = mAttributeReports;
mAttributeReports = nil;
auto attributeCallback = mAttributeReportCallback;

__block NSArray * eventReports = mEventReports;
mEventReports = nil;
auto eventCallback = mEventReportCallback;

ReportAttributes(attributeReports);
ReportEvents(eventReports);
}

void MTRBaseSubscriptionCallback::ReportAttributes(NSArray * attributeReports)
{
auto attributeCallback = mAttributeReportCallback;
if (attributeCallback != nil && attributeReports.count) {
attributeCallback(attributeReports);
}
}

void MTRBaseSubscriptionCallback::ReportEvents(NSArray * eventReports)
{
auto eventCallback = mEventReportCallback;
if (eventCallback != nil && eventReports.count) {
eventCallback(eventReports);
}
Expand Down
30 changes: 15 additions & 15 deletions src/darwin/Framework/CHIP/MTRDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1527,25 +1527,24 @@ - (void)invokeCommandWithEndpointID:(NSNumber *)endpointID
}

MTREventPath * eventPath = [[MTREventPath alloc] initWithPath:aEventHeader.mPath];
NSDictionary * eventReport;
if (apStatus != nullptr) {
[mEventReports addObject:@ { MTREventPathKey : eventPath, MTRErrorKey : [MTRError errorForIMStatus:*apStatus] }];
} else if (apData == nullptr) {
[mEventReports addObject:@ {
MTREventPathKey : eventPath,
MTRErrorKey : [MTRError errorForCHIPErrorCode:CHIP_ERROR_INVALID_ARGUMENT]
}];
eventReport = @ { MTREventPathKey : eventPath, MTRErrorKey : [MTRError errorForCHIPErrorCode:CHIP_ERROR_INVALID_ARGUMENT] };
} else {
id value = MTRDecodeDataValueDictionaryFromCHIPTLV(apData);
if (value == nil) {
MTR_LOG_ERROR("Failed to decode event data for path %@", eventPath);
[mEventReports addObject:@ {
eventReport = @ {
MTREventPathKey : eventPath,
MTRErrorKey : [MTRError errorForCHIPErrorCode:CHIP_ERROR_DECODE_FAILED],
}];
};
} else {
[mEventReports addObject:[MTRBaseDevice eventReportForHeader:aEventHeader andData:value]];
eventReport = [MTRBaseDevice eventReportForHeader:aEventHeader andData:value];
}
}
ReportEvents(@[ eventReport ]);
}

void SubscriptionCallback::OnAttributeData(
Expand All @@ -1563,24 +1562,25 @@ - (void)invokeCommandWithEndpointID:(NSNumber *)endpointID
}

MTRAttributePath * attributePath = [[MTRAttributePath alloc] initWithPath:aPath];
NSDictionary * attributeReport;
if (aStatus.mStatus != Status::Success) {
[mAttributeReports addObject:@ { MTRAttributePathKey : attributePath, MTRErrorKey : [MTRError errorForIMStatus:aStatus] }];
attributeReport = @ { MTRAttributePathKey : attributePath, MTRErrorKey : [MTRError errorForIMStatus:aStatus] };
} else if (apData == nullptr) {
[mAttributeReports addObject:@ {
MTRAttributePathKey : attributePath,
MTRErrorKey : [MTRError errorForCHIPErrorCode:CHIP_ERROR_INVALID_ARGUMENT]
}];
attributeReport =
@ { MTRAttributePathKey : attributePath, MTRErrorKey : [MTRError errorForCHIPErrorCode:CHIP_ERROR_INVALID_ARGUMENT] };
} else {
id value = MTRDecodeDataValueDictionaryFromCHIPTLV(apData);
if (value == nil) {
MTR_LOG_ERROR("Failed to decode attribute data for path %@", attributePath);
[mAttributeReports addObject:@ {
attributeReport = @ {
MTRAttributePathKey : attributePath,
MTRErrorKey : [MTRError errorForCHIPErrorCode:CHIP_ERROR_DECODE_FAILED],
}];
};
} else {
[mAttributeReports addObject:@ { MTRAttributePathKey : attributePath, MTRDataKey : value }];
attributeReport = @ { MTRAttributePathKey : attributePath, MTRDataKey : value };
}
}

ReportAttributes(@[ attributeReport ]);
}
} // anonymous namespace

0 comments on commit 1f018b5

Please sign in to comment.