Skip to content

Commit 48e529d

Browse files
committed
simplify - pull attributes from cache without intermediate object, should be fast enough
1 parent cd17c9e commit 48e529d

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) {
@@ -1910,7 +1877,7 @@ - (void)_setCachedAttributeValue:(MTRDeviceDataValueDictionary _Nullable)value f
19101877
}
19111878

19121879
[clusterData storeValue:value forAttribute:path.attribute];
1913-
1880+
19141881
if (value != nil
19151882
&& isFromSubscription
19161883
&& !_receivingPrimingReport
@@ -1920,11 +1887,9 @@ - (void)_setCachedAttributeValue:(MTRDeviceDataValueDictionary _Nullable)value f
19201887
// (removals are OK)
19211888

19221889
// log when a device violates expectations for Changes Omitted Quality attributes.
1923-
MTRDeviceInformationalAttributes * attributes = [self _informationalAttributesForCurrentState];
1924-
19251890
using namespace chip::Tracing::DarwinFramework;
19261891
MATTER_LOG_METRIC_BEGIN(kMetricUnexpectedCQualityUpdate);
1927-
[attributes addInformationalAttributesToCurrentMetricScope];
1892+
[self _addInformationalAttributesToCurrentMetricScope];
19281893
MATTER_LOG_METRIC_END(kMetricUnexpectedCQualityUpdate);
19291894

19301895
return;
@@ -3539,18 +3504,40 @@ - (void)removeClientDataForKey:(NSString *)key endpointID:(NSNumber *)endpointID
35393504

35403505
#pragma mark Log Help
35413506

3542-
- (MTRDeviceInformationalAttributes *)_informationalAttributesForCurrentState {
3543-
MTRClusterPath * basicInfoClusterPath = [MTRClusterPath clusterPathWithEndpointID:@(kRootEndpointId) clusterID:@(MTRClusterIDTypeBasicInformationID)];
3544-
MTRDeviceClusterData * basicInfoClusterData = [self _clusterDataForPath:basicInfoClusterPath];
3507+
- (NSNumber *)_informationalNumberAtAttributePath:(MTRAttributePath *)attributePath {
3508+
auto * cachedData = [self _cachedAttributeValueForPath:attributePath];
35453509

3546-
NSNumber * vidObj = basicInfoClusterData.attributes[@(MTRAttributeIDTypeClusterBasicInformationAttributeVendorIDID)][MTRValueKey];
3547-
UInt16 vendorID = vidObj.unsignedShortValue;
3548-
NSNumber * pidObj = basicInfoClusterData.attributes[@(MTRAttributeIDTypeClusterBasicInformationAttributeProductIDID)][MTRValueKey];
3549-
UInt16 productID = pidObj.unsignedShortValue;
3510+
auto * attrReport = [[MTRAttributeReport alloc] initWithResponseValue:@{
3511+
MTRAttributePathKey : attributePath,
3512+
MTRDataKey : cachedData,
3513+
} error:nil];
3514+
// REVIEWERS: is it worth logging the `error` above?
35503515

3551-
BOOL usesThread = [self _deviceUsesThread];
3516+
return attrReport.value;
3517+
}
3518+
3519+
- (NSNumber *)_informationalVendorID {
3520+
auto * vendorIDPath = [MTRAttributePath attributePathWithEndpointID:@(kRootEndpointId)
3521+
clusterID:@(MTRClusterIDTypeBasicInformationID)
3522+
attributeID:@(MTRClusterBasicAttributeVendorIDID)];
35523523

3553-
return [[MTRDeviceInformationalAttributes alloc] initWithVendorID:vendorID productID:productID usesThread:usesThread];
3524+
return [self _informationalNumberAtAttributePath:vendorIDPath];
3525+
}
3526+
3527+
- (NSNumber *)_informationalProductID {
3528+
auto * productIDPath = [MTRAttributePath attributePathWithEndpointID:@(kRootEndpointId)
3529+
clusterID:@(MTRClusterIDTypeBasicInformationID)
3530+
attributeID:@(MTRClusterBasicAttributeProductIDID)];
3531+
3532+
return [self _informationalNumberAtAttributePath:productIDPath];
3533+
}
3534+
3535+
- (void)_addInformationalAttributesToCurrentMetricScope {
3536+
using namespace chip::Tracing::DarwinFramework;
3537+
MATTER_LOG_METRIC(kMetricDeviceVendorID, [self _informationalVendorID].unsignedShortValue);
3538+
MATTER_LOG_METRIC(kMetricDeviceProductID, [self _informationalProductID].unsignedShortValue);
3539+
BOOL usesThread = [self _deviceUsesThread];
3540+
MATTER_LOG_METRIC(kMetricDeviceUsesThread, usesThread);
35543541
}
35553542

35563543
@end

0 commit comments

Comments
 (0)