Skip to content

Commit 634f795

Browse files
committed
simplify - pull attributes from cache without intermediate object, should be fast enough
1 parent 0529a00 commit 634f795

File tree

1 file changed

+33
-46
lines changed

1 file changed

+33
-46
lines changed

src/darwin/Framework/CHIP/MTRDevice.mm

+33-46
Original file line numberDiff line numberDiff line change
@@ -93,39 +93,6 @@ - (id)strongObject
9393
}
9494
@end
9595

96-
// convenience object for commonly-logged device attributes
97-
@interface MTRDeviceInformationalAttributes : NSObject
98-
@property (readonly) UInt16 vendorID;
99-
@property (readonly) UInt16 productID;
100-
@property (readonly) BOOL usesThread;
101-
102-
- (void)addInformationalAttributesToCurrentMetricScope;
103-
104-
@end
105-
106-
@implementation MTRDeviceInformationalAttributes
107-
108-
- (instancetype)initWithVendorID:(UInt16)vendorID productID:(UInt16)productID usesThread:(BOOL)usesThread {
109-
self = [super init];
110-
111-
if (self) {
112-
_vendorID = vendorID;
113-
_productID = productID;
114-
_usesThread = usesThread;
115-
}
116-
117-
return self;
118-
}
119-
120-
- (void)addInformationalAttributesToCurrentMetricScope {
121-
using namespace chip::Tracing::DarwinFramework;
122-
MATTER_LOG_METRIC(kMetricDeviceVendorID, _vendorID);
123-
MATTER_LOG_METRIC(kMetricDeviceProductID, _productID);
124-
MATTER_LOG_METRIC(kMetricDeviceUsesThread, _usesThread);
125-
}
126-
127-
@end
128-
12996
NSNumber * MTRClampedNumber(NSNumber * aNumber, NSNumber * min, NSNumber * max)
13097
{
13198
if ([aNumber compare:min] == NSOrderedAscending) {
@@ -1896,7 +1863,7 @@ - (void)_setCachedAttributeValue:(MTRDeviceDataValueDictionary _Nullable)value f
18961863
}
18971864

18981865
[clusterData storeValue:value forAttribute:path.attribute];
1899-
1866+
19001867
if (value != nil
19011868
&& isFromSubscription
19021869
&& !_receivingPrimingReport
@@ -1906,11 +1873,9 @@ - (void)_setCachedAttributeValue:(MTRDeviceDataValueDictionary _Nullable)value f
19061873
// (removals are OK)
19071874

19081875
// log when a device violates expectations for Changes Omitted Quality attributes.
1909-
MTRDeviceInformationalAttributes * attributes = [self _informationalAttributesForCurrentState];
1910-
19111876
using namespace chip::Tracing::DarwinFramework;
19121877
MATTER_LOG_METRIC_BEGIN(kMetricUnexpectedCQualityUpdate);
1913-
[attributes addInformationalAttributesToCurrentMetricScope];
1878+
[self _addInformationalAttributesToCurrentMetricScope];
19141879
MATTER_LOG_METRIC_END(kMetricUnexpectedCQualityUpdate);
19151880

19161881
return;
@@ -3567,18 +3532,40 @@ - (void)removeClientDataForKey:(NSString *)key endpointID:(NSNumber *)endpointID
35673532

35683533
#pragma mark Log Help
35693534

3570-
- (MTRDeviceInformationalAttributes *)_informationalAttributesForCurrentState {
3571-
MTRClusterPath * basicInfoClusterPath = [MTRClusterPath clusterPathWithEndpointID:@(kRootEndpointId) clusterID:@(MTRClusterIDTypeBasicInformationID)];
3572-
MTRDeviceClusterData * basicInfoClusterData = [self _clusterDataForPath:basicInfoClusterPath];
3535+
- (NSNumber *)_informationalNumberAtAttributePath:(MTRAttributePath *)attributePath {
3536+
auto * cachedData = [self _cachedAttributeValueForPath:attributePath];
35733537

3574-
NSNumber * vidObj = basicInfoClusterData.attributes[@(MTRAttributeIDTypeClusterBasicInformationAttributeVendorIDID)][MTRValueKey];
3575-
UInt16 vendorID = vidObj.unsignedShortValue;
3576-
NSNumber * pidObj = basicInfoClusterData.attributes[@(MTRAttributeIDTypeClusterBasicInformationAttributeProductIDID)][MTRValueKey];
3577-
UInt16 productID = pidObj.unsignedShortValue;
3538+
auto * attrReport = [[MTRAttributeReport alloc] initWithResponseValue:@{
3539+
MTRAttributePathKey : attributePath,
3540+
MTRDataKey : cachedData,
3541+
} error:nil];
3542+
// REVIEWERS: is it worth logging the `error` above?
35783543

3579-
BOOL usesThread = [self _deviceUsesThread];
3544+
return attrReport.value;
3545+
}
3546+
3547+
- (NSNumber *)_informationalVendorID {
3548+
auto * vendorIDPath = [MTRAttributePath attributePathWithEndpointID:@(kRootEndpointId)
3549+
clusterID:@(MTRClusterIDTypeBasicInformationID)
3550+
attributeID:@(MTRClusterBasicAttributeVendorIDID)];
35803551

3581-
return [[MTRDeviceInformationalAttributes alloc] initWithVendorID:vendorID productID:productID usesThread:usesThread];
3552+
return [self _informationalNumberAtAttributePath:vendorIDPath];
3553+
}
3554+
3555+
- (NSNumber *)_informationalProductID {
3556+
auto * productIDPath = [MTRAttributePath attributePathWithEndpointID:@(kRootEndpointId)
3557+
clusterID:@(MTRClusterIDTypeBasicInformationID)
3558+
attributeID:@(MTRClusterBasicAttributeProductIDID)];
3559+
3560+
return [self _informationalNumberAtAttributePath:productIDPath];
3561+
}
3562+
3563+
- (void)_addInformationalAttributesToCurrentMetricScope {
3564+
using namespace chip::Tracing::DarwinFramework;
3565+
MATTER_LOG_METRIC(kMetricDeviceVendorID, [self _informationalVendorID].unsignedShortValue);
3566+
MATTER_LOG_METRIC(kMetricDeviceProductID, [self _informationalProductID].unsignedShortValue);
3567+
BOOL usesThread = [self _deviceUsesThread];
3568+
MATTER_LOG_METRIC(kMetricDeviceUsesThread, usesThread);
35823569
}
35833570

35843571
@end

0 commit comments

Comments
 (0)