|
| 1 | +/** |
| 2 | + * Copyright (c) 2024 Project CHIP Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#import "MTRDeviceStorageBehaviorConfiguration.h" |
| 18 | + |
| 19 | +#import "MTRLogging_Internal.h" |
| 20 | + |
| 21 | +#define kReportToPersistenceDelayTimeDefault (15) |
| 22 | +#define kReportToPersistenceDelayTimeMaxDefault (20 * kReportToPersistenceDelayTimeDefault) |
| 23 | +#define kRecentReportTimesMaxCountDefault (12) |
| 24 | +#define kTimeBetweenReportsTooShortThresholdDefault (15) |
| 25 | +#define kTimeBetweenReportsTooShortMinThresholdDefault (5) |
| 26 | +#define kReportToPersistenceDelayMaxMultiplierDefault (10) |
| 27 | +#define kDeviceReportingExcessivelyIntervalThresholdDefault (5 * 60) |
| 28 | + |
| 29 | +@implementation MTRDeviceStorageBehaviorConfiguration |
| 30 | + |
| 31 | ++ (instancetype)configurationWithReportToPersistenceDelayTime:(NSTimeInterval)reportToPersistenceDelayTime |
| 32 | + reportToPersistenceDelayTimeMax:(NSTimeInterval)reportToPersistenceDelayTimeMax |
| 33 | + recentReportTimesMaxCount:(NSUInteger)recentReportTimesMaxCount |
| 34 | + timeBetweenReportsTooShortThreshold:(NSTimeInterval)timeBetweenReportsTooShortThreshold |
| 35 | + timeBetweenReportsTooShortMinThreshold:(NSTimeInterval)timeBetweenReportsTooShortMinThreshold |
| 36 | + reportToPersistenceDelayMaxMultiplier:(double)reportToPersistenceDelayMaxMultiplier |
| 37 | + deviceReportingExcessivelyIntervalThreshold:(NSTimeInterval)deviceReportingExcessivelyIntervalThreshold |
| 38 | +{ |
| 39 | + auto newConfiguration = [[MTRDeviceStorageBehaviorConfiguration alloc] init]; |
| 40 | + newConfiguration.reportToPersistenceDelayTime = reportToPersistenceDelayTime; |
| 41 | + newConfiguration.reportToPersistenceDelayTimeMax = reportToPersistenceDelayTimeMax; |
| 42 | + newConfiguration.recentReportTimesMaxCount = recentReportTimesMaxCount; |
| 43 | + newConfiguration.timeBetweenReportsTooShortThreshold = timeBetweenReportsTooShortThreshold; |
| 44 | + newConfiguration.timeBetweenReportsTooShortMinThreshold = timeBetweenReportsTooShortMinThreshold; |
| 45 | + newConfiguration.reportToPersistenceDelayMaxMultiplier = reportToPersistenceDelayMaxMultiplier; |
| 46 | + newConfiguration.deviceReportingExcessivelyIntervalThreshold = deviceReportingExcessivelyIntervalThreshold; |
| 47 | + |
| 48 | + return newConfiguration; |
| 49 | +} |
| 50 | + |
| 51 | ++ (instancetype)configurationWithDefaultStorageBehavior |
| 52 | +{ |
| 53 | + auto newConfiguration = [[MTRDeviceStorageBehaviorConfiguration alloc] init]; |
| 54 | + [newConfiguration checkValuesAndResetToDefaultIfNecessary]; |
| 55 | + return newConfiguration; |
| 56 | +} |
| 57 | + |
| 58 | ++ (instancetype)configurationWithStorageBehaviorOptimizationDisabled |
| 59 | +{ |
| 60 | + auto newConfiguration = [[MTRDeviceStorageBehaviorConfiguration alloc] init]; |
| 61 | + newConfiguration.disableStorageBehaviorOptimization = YES; |
| 62 | + return newConfiguration; |
| 63 | +} |
| 64 | + |
| 65 | +- (NSString *)description |
| 66 | +{ |
| 67 | + return [NSString stringWithFormat:@"<MTRDeviceStorageBehaviorConfiguration(%p): disabled %s reportToPersistenceDelayTime %lf reportToPersistenceDelayTimeMax %lf recentReportTimesMaxCount %lu timeBetweenReportsTooShortThreshold %lf timeBetweenReportsTooShortMinThreshold %lf reportToPersistenceDelayMaxMultiplier %lf deviceReportingExcessivelyIntervalThreshold %lf", self, _disableStorageBehaviorOptimization ? "YES" : "NO", _reportToPersistenceDelayTime, _reportToPersistenceDelayTimeMax, static_cast<unsigned long>(_recentReportTimesMaxCount), _timeBetweenReportsTooShortThreshold, _timeBetweenReportsTooShortMinThreshold, _reportToPersistenceDelayMaxMultiplier, _deviceReportingExcessivelyIntervalThreshold]; |
| 68 | +} |
| 69 | + |
| 70 | +- (void)checkValuesAndResetToDefaultIfNecessary |
| 71 | +{ |
| 72 | + if (_disableStorageBehaviorOptimization) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + // Sanity check all the values, and if any is out of range, reset to default values |
| 77 | + if ((_reportToPersistenceDelayTime <= 0) || (_reportToPersistenceDelayTimeMax <= 0) || (_reportToPersistenceDelayTimeMax < _reportToPersistenceDelayTime) || (_recentReportTimesMaxCount < 2) || (_timeBetweenReportsTooShortThreshold <= 0) || (_timeBetweenReportsTooShortMinThreshold <= 0) || (_timeBetweenReportsTooShortMinThreshold > _timeBetweenReportsTooShortThreshold) || (_reportToPersistenceDelayMaxMultiplier <= 1) || (_deviceReportingExcessivelyIntervalThreshold <= 0)) { |
| 78 | + MTR_LOG_ERROR("%@ storage behavior: MTRDeviceStorageBehaviorConfiguration values out of bounds - resetting to default", self); |
| 79 | + |
| 80 | + _reportToPersistenceDelayTime = kReportToPersistenceDelayTimeDefault; |
| 81 | + _reportToPersistenceDelayTimeMax = kReportToPersistenceDelayTimeMaxDefault; |
| 82 | + _recentReportTimesMaxCount = kRecentReportTimesMaxCountDefault; |
| 83 | + _timeBetweenReportsTooShortThreshold = kTimeBetweenReportsTooShortThresholdDefault; |
| 84 | + _timeBetweenReportsTooShortMinThreshold = kTimeBetweenReportsTooShortMinThresholdDefault; |
| 85 | + _reportToPersistenceDelayMaxMultiplier = kReportToPersistenceDelayMaxMultiplierDefault; |
| 86 | + _deviceReportingExcessivelyIntervalThreshold = kDeviceReportingExcessivelyIntervalThresholdDefault; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +- (id)copyWithZone:(NSZone *)zone |
| 91 | +{ |
| 92 | + auto newConfiguration = [[MTRDeviceStorageBehaviorConfiguration alloc] init]; |
| 93 | + newConfiguration.disableStorageBehaviorOptimization = _disableStorageBehaviorOptimization; |
| 94 | + newConfiguration.reportToPersistenceDelayTime = _reportToPersistenceDelayTime; |
| 95 | + newConfiguration.reportToPersistenceDelayTimeMax = _reportToPersistenceDelayTimeMax; |
| 96 | + newConfiguration.recentReportTimesMaxCount = _recentReportTimesMaxCount; |
| 97 | + newConfiguration.timeBetweenReportsTooShortThreshold = _timeBetweenReportsTooShortThreshold; |
| 98 | + newConfiguration.timeBetweenReportsTooShortMinThreshold = _timeBetweenReportsTooShortMinThreshold; |
| 99 | + newConfiguration.reportToPersistenceDelayMaxMultiplier = _reportToPersistenceDelayMaxMultiplier; |
| 100 | + newConfiguration.deviceReportingExcessivelyIntervalThreshold = _deviceReportingExcessivelyIntervalThreshold; |
| 101 | + |
| 102 | + return newConfiguration; |
| 103 | +} |
| 104 | + |
| 105 | +@end |
0 commit comments