Skip to content

Commit

Permalink
[Darwin] Adding metrics for MTRDeviceController APIs (#32582)
Browse files Browse the repository at this point in the history
* [Darwin] Adding metrics for MTRDeviceController APIs

- Adding initial set of metrics for MTRDeviceController APIs around
  device commissioning
- Added uniqueIdentifer to MTRMetrics
- Changed MetricsCollector to removed the end event for begin / instant
  event

* Update src/darwin/Framework/CHIPTests/MTRMetricsTests.m

Co-authored-by: Karsten Sperling <[email protected]>

* Addressed code review feedback

* Fixing namespace naming to be consistent

* Re-styler fixes

---------

Co-authored-by: Karsten Sperling <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Apr 19, 2024
1 parent 462febc commit 9a9c3dd
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 26 deletions.
13 changes: 13 additions & 0 deletions src/darwin/Framework/CHIP/MTRBaseDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#import "MTREventTLVValueDecoder_Internal.h"
#import "MTRFramework.h"
#import "MTRLogging_Internal.h"
#import "MTRMetricKeys.h"
#import "MTRSetupPayload_Internal.h"
#import "NSDataSpanConversion.h"
#import "NSStringSpanConversion.h"
Expand Down Expand Up @@ -61,6 +62,7 @@
using chip::Optional;
using chip::SessionHandle;
using chip::Messaging::ExchangeManager;
using namespace chip::Tracing::DarwinFramework;

NSString * const MTRAttributePathKey = @"attributePath";
NSString * const MTRCommandPathKey = @"commandPath";
Expand Down Expand Up @@ -1861,9 +1863,12 @@ - (void)_openCommissioningWindowWithSetupPasscode:(nullable NSNumber *)setupPass
queue:(dispatch_queue_t)queue
completion:(MTRDeviceOpenCommissioningWindowHandler)completion
{
MATTER_LOG_METRIC_BEGIN(kMetricOpenPairingWindow);

if (self.isPASEDevice) {
MTR_LOG_ERROR("Can't open a commissioning window over PASE");
dispatch_async(queue, ^{
MATTER_LOG_METRIC_END(kMetricOpenPairingWindow, CHIP_ERROR_INCORRECT_STATE);
completion(nil, [MTRError errorForCHIPErrorCode:CHIP_ERROR_INCORRECT_STATE]);
});
return;
Expand All @@ -1873,6 +1878,7 @@ - (void)_openCommissioningWindowWithSetupPasscode:(nullable NSNumber *)setupPass
if (!CanCastTo<uint16_t>(durationVal)) {
MTR_LOG_ERROR("Error: Duration %llu is too large.", durationVal);
dispatch_async(queue, ^{
MATTER_LOG_METRIC_END(kMetricOpenPairingWindow, CHIP_ERROR_INVALID_INTEGER_VALUE);
completion(nil, [MTRError errorForCHIPErrorCode:CHIP_ERROR_INVALID_INTEGER_VALUE]);
});
return;
Expand All @@ -1883,6 +1889,7 @@ - (void)_openCommissioningWindowWithSetupPasscode:(nullable NSNumber *)setupPass
if (discriminatorVal > 0xFFF) {
MTR_LOG_ERROR("Error: Discriminator %llu is too large. Max value %d", discriminatorVal, 0xFFF);
dispatch_async(queue, ^{
MATTER_LOG_METRIC_END(kMetricOpenPairingWindow, CHIP_ERROR_INVALID_INTEGER_VALUE);
completion(nil, [MTRError errorForCHIPErrorCode:CHIP_ERROR_INVALID_INTEGER_VALUE]);
});
return;
Expand All @@ -1894,6 +1901,7 @@ - (void)_openCommissioningWindowWithSetupPasscode:(nullable NSNumber *)setupPass
if (!CanCastTo<uint32_t>(passcodeVal) || !SetupPayload::IsValidSetupPIN(static_cast<uint32_t>(passcodeVal))) {
MTR_LOG_ERROR("Error: Setup passcode %llu is not valid", passcodeVal);
dispatch_async(queue, ^{
MATTER_LOG_METRIC_END(kMetricOpenPairingWindow, CHIP_ERROR_INVALID_INTEGER_VALUE);
completion(nil, [MTRError errorForCHIPErrorCode:CHIP_ERROR_INVALID_INTEGER_VALUE]);
});
return;
Expand All @@ -1906,19 +1914,22 @@ - (void)_openCommissioningWindowWithSetupPasscode:(nullable NSNumber *)setupPass
auto resultCallback = ^(CHIP_ERROR status, const SetupPayload & payload) {
if (status != CHIP_NO_ERROR) {
dispatch_async(queue, ^{
MATTER_LOG_METRIC_END(kMetricOpenPairingWindow, status);
completion(nil, [MTRError errorForCHIPErrorCode:status]);
});
return;
}
auto * payloadObj = [[MTRSetupPayload alloc] initWithSetupPayload:payload];
if (payloadObj == nil) {
dispatch_async(queue, ^{
MATTER_LOG_METRIC_END(kMetricOpenPairingWindow, CHIP_ERROR_NO_MEMORY);
completion(nil, [MTRError errorForCHIPErrorCode:CHIP_ERROR_NO_MEMORY]);
});
return;
}

dispatch_async(queue, ^{
MATTER_LOG_METRIC_END(kMetricOpenPairingWindow, CHIP_NO_ERROR);
completion(payloadObj, nil);
});
};
Expand All @@ -1930,6 +1941,7 @@ - (void)_openCommissioningWindowWithSetupPasscode:(nullable NSNumber *)setupPass

if (errorCode != CHIP_NO_ERROR) {
dispatch_async(queue, ^{
MATTER_LOG_METRIC_END(kMetricOpenPairingWindow, errorCode);
completion(nil, [MTRError errorForCHIPErrorCode:errorCode]);
});
return;
Expand All @@ -1939,6 +1951,7 @@ - (void)_openCommissioningWindowWithSetupPasscode:(nullable NSNumber *)setupPass
}
errorHandler:^(NSError * error) {
dispatch_async(queue, ^{
MATTER_LOG_METRIC_END(kMetricOpenPairingWindow, [MTRError errorToCHIPErrorCode:error]);
completion(nil, error);
});
}];
Expand Down
Loading

0 comments on commit 9a9c3dd

Please sign in to comment.