Skip to content

Commit 4cd197b

Browse files
Remove writeAttributeWithEndpointID implementation from MTRDevice.
This is implemented (differently) by the different subclasses. Once this implementation is removed, removeExpectedValueForAttributePath becomes unused and can be removed. Also removes the unused setExpectedValues declaration in MTRDevice_Internal.h and the implementations of it.
1 parent daa2a57 commit 4cd197b

File tree

3 files changed

+9
-136
lines changed

3 files changed

+9
-136
lines changed

src/darwin/Framework/CHIP/MTRDevice.mm

+9-127
Original file line numberDiff line numberDiff line change
@@ -1831,11 +1831,12 @@ static BOOL AttributeHasChangesOmittedQuality(MTRAttributePath * attributePath)
18311831
attributeID:(NSNumber *)attributeID
18321832
params:(MTRReadParams * _Nullable)params
18331833
{
1834-
#define ErrorStr "MTRDevice readAttributeWithEndpointID:clusterID:attributeID:params: must be handled by subclasses"
1835-
MTR_LOG_ERROR(ErrorStr);
1834+
#define MTRDeviceErrorStr "MTRDevice readAttributeWithEndpointID:clusterID:attributeID:params: must be handled by subclasses"
1835+
MTR_LOG_ERROR(MTRDeviceErrorStr);
18361836
#ifdef DEBUG
1837-
NSAssert(NO, @ErrorStr);
1837+
NSAssert(NO, @MTRDeviceErrorStr);
18381838
#endif // DEBUG
1839+
#undef MTRDeviceErrorStr
18391840
return nil;
18401841
}
18411842

@@ -1846,120 +1847,12 @@ - (void)writeAttributeWithEndpointID:(NSNumber *)endpointID
18461847
expectedValueInterval:(NSNumber *)expectedValueInterval
18471848
timedWriteTimeout:(NSNumber * _Nullable)timeout
18481849
{
1849-
if (timeout) {
1850-
timeout = MTRClampedNumber(timeout, @(1), @(UINT16_MAX));
1851-
}
1852-
expectedValueInterval = MTRClampedNumber(expectedValueInterval, @(1), @(UINT32_MAX));
1853-
MTRAttributePath * attributePath = [MTRAttributePath attributePathWithEndpointID:endpointID
1854-
clusterID:clusterID
1855-
1856-
attributeID:attributeID];
1857-
1858-
__block BOOL useValueAsExpectedValue = YES;
1850+
#define MTRDeviceErrorStr "MTRDevice writeAttributeWithEndpointID:clusterID:attributeID:value:expectedValueInterval:timedWriteTimeout: must be handled by subclasses"
1851+
MTR_LOG_ERROR(MTRDeviceErrorStr);
18591852
#ifdef DEBUG
1860-
os_unfair_lock_lock(&self->_lock);
1861-
[self _callFirstDelegateSynchronouslyWithBlock:^(id delegate) {
1862-
if ([delegate respondsToSelector:@selector(unitTestShouldSkipExpectedValuesForWrite:)]) {
1863-
useValueAsExpectedValue = ![delegate unitTestShouldSkipExpectedValuesForWrite:self];
1864-
}
1865-
}];
1866-
os_unfair_lock_unlock(&self->_lock);
1867-
#endif
1868-
1869-
uint64_t expectedValueID = 0;
1870-
if (useValueAsExpectedValue) {
1871-
// Commit change into expected value cache
1872-
NSDictionary * newExpectedValueDictionary = @{ MTRAttributePathKey : attributePath, MTRDataKey : value };
1873-
[self setExpectedValues:@[ newExpectedValueDictionary ]
1874-
expectedValueInterval:expectedValueInterval
1875-
expectedValueID:&expectedValueID];
1876-
}
1877-
1878-
MTRAsyncWorkItem * workItem = [[MTRAsyncWorkItem alloc] initWithQueue:self.queue];
1879-
uint64_t workItemID = workItem.uniqueID; // capture only the ID, not the work item
1880-
NSNumber * nodeID = _nodeID;
1881-
1882-
// Write request data is an array of items (for now always length 1). Each
1883-
// item is an array containing:
1884-
//
1885-
// [ attribute path, value, timedWriteTimeout, expectedValueID ]
1886-
//
1887-
// where expectedValueID is stored as NSNumber and NSNull represents nil timeouts
1888-
auto * writeData = @[ attributePath, [value copy], timeout ?: [NSNull null], @(expectedValueID) ];
1889-
1890-
NSMutableArray<NSArray *> * writeRequests = [NSMutableArray arrayWithObject:writeData];
1891-
1892-
[workItem setBatchingID:MTRDeviceWorkItemBatchingWriteID data:writeRequests handler:^(id opaqueDataCurrent, id opaqueDataNext) {
1893-
mtr_hide(self); // don't capture self accidentally
1894-
NSMutableArray<NSArray *> * writeRequestsCurrent = opaqueDataCurrent;
1895-
NSMutableArray<NSArray *> * writeRequestsNext = opaqueDataNext;
1896-
1897-
if (writeRequestsCurrent.count != 1) {
1898-
// Very unexpected!
1899-
MTR_LOG_ERROR("Batching write attribute work item [%llu]: Unexpected write request count %lu", workItemID, static_cast<unsigned long>(writeRequestsCurrent.count));
1900-
return MTRNotBatched;
1901-
}
1902-
1903-
MTRBatchingOutcome outcome = MTRNotBatched;
1904-
while (writeRequestsNext.count) {
1905-
// If paths don't match, we cannot replace the earlier write
1906-
// with the later one.
1907-
if (![writeRequestsNext[0][MTRDeviceWriteRequestFieldPathIndex]
1908-
isEqual:writeRequestsCurrent[0][MTRDeviceWriteRequestFieldPathIndex]]) {
1909-
MTR_LOG("Batching write attribute work item [%llu]: cannot replace with next work item due to path mismatch", workItemID);
1910-
return outcome;
1911-
}
1912-
1913-
// Replace our one request with the first one from the next item.
1914-
auto writeItem = writeRequestsNext.firstObject;
1915-
[writeRequestsNext removeObjectAtIndex:0];
1916-
[writeRequestsCurrent replaceObjectAtIndex:0 withObject:writeItem];
1917-
MTR_LOG("Batching write attribute work item [%llu]: replaced with new write value %@ [0x%016llX]",
1918-
workItemID, writeItem, nodeID.unsignedLongLongValue);
1919-
outcome = MTRBatchedPartially;
1920-
}
1921-
NSCAssert(writeRequestsNext.count == 0, @"should have batched everything or returned early");
1922-
return MTRBatchedFully;
1923-
}];
1924-
// The write operation will install a duplicate check handler, to return NO for "isDuplicate". Since a write operation may
1925-
// change values, only read requests after this should be considered for duplicate requests.
1926-
[workItem setDuplicateTypeID:MTRDeviceWorkItemDuplicateReadTypeID handler:^(id opaqueItemData, BOOL * isDuplicate, BOOL * stop) {
1927-
*isDuplicate = NO;
1928-
*stop = YES;
1929-
}];
1930-
[workItem setReadyHandler:^(MTRDevice * self, NSInteger retryCount, MTRAsyncWorkCompletionBlock completion) {
1931-
MTRBaseDevice * baseDevice = [self newBaseDevice];
1932-
// Make sure to use writeRequests here, because that's what our batching
1933-
// handler will modify as needed.
1934-
NSCAssert(writeRequests.count == 1, @"Incorrect number of write requests: %lu", static_cast<unsigned long>(writeRequests.count));
1935-
1936-
auto * request = writeRequests[0];
1937-
MTRAttributePath * path = request[MTRDeviceWriteRequestFieldPathIndex];
1938-
1939-
id timedWriteTimeout = request[MTRDeviceWriteRequestFieldTimeoutIndex];
1940-
if (timedWriteTimeout == [NSNull null]) {
1941-
timedWriteTimeout = nil;
1942-
}
1943-
1944-
[baseDevice
1945-
writeAttributeWithEndpointID:path.endpoint
1946-
clusterID:path.cluster
1947-
attributeID:path.attribute
1948-
value:request[MTRDeviceWriteRequestFieldValueIndex]
1949-
timedWriteTimeout:timedWriteTimeout
1950-
queue:self.queue
1951-
completion:^(NSArray<NSDictionary<NSString *, id> *> * _Nullable values, NSError * _Nullable error) {
1952-
if (error) {
1953-
MTR_LOG_ERROR("Write attribute work item [%llu] failed: %@", workItemID, error);
1954-
if (useValueAsExpectedValue) {
1955-
NSNumber * expectedValueID = request[MTRDeviceWriteRequestFieldExpectedValueIDIndex];
1956-
[self removeExpectedValueForAttributePath:attributePath expectedValueID:expectedValueID.unsignedLongLongValue];
1957-
}
1958-
}
1959-
completion(MTRAsyncWorkComplete);
1960-
}];
1961-
}];
1962-
[_asyncWorkQueue enqueueWorkItem:workItem descriptionWithFormat:@"write %@ 0x%llx 0x%llx", endpointID, clusterID.unsignedLongLongValue, attributeID.unsignedLongLongValue];
1853+
NSAssert(NO, @MTRDeviceErrorStr);
1854+
#endif // DEBUG
1855+
#undef MTRDeviceErrorStr
19631856
}
19641857

19651858
- (void)invokeCommandWithEndpointID:(NSNumber *)endpointID
@@ -2796,11 +2689,6 @@ - (NSArray *)_getAttributesToReportWithNewExpectedValues:(NSArray<NSDictionary<N
27962689
return attributesToReport;
27972690
}
27982691

2799-
- (void)setExpectedValues:(NSArray<NSDictionary<NSString *, id> *> *)values expectedValueInterval:(NSNumber *)expectedValueInterval
2800-
{
2801-
[self setExpectedValues:values expectedValueInterval:expectedValueInterval expectedValueID:nil];
2802-
}
2803-
28042692
// expectedValueID is an out-argument that returns an identifier to be used when removing expected values
28052693
- (void)setExpectedValues:(NSArray<NSDictionary<NSString *, id> *> *)values
28062694
expectedValueInterval:(NSNumber *)expectedValueInterval
@@ -2833,12 +2721,6 @@ - (void)removeExpectedValuesForAttributePaths:(NSArray<MTRAttributePath *> *)att
28332721
}
28342722
}
28352723

2836-
- (void)removeExpectedValueForAttributePath:(MTRAttributePath *)attributePath expectedValueID:(uint64_t)expectedValueID
2837-
{
2838-
std::lock_guard lock(_lock);
2839-
[self _removeExpectedValueForAttributePath:attributePath expectedValueID:expectedValueID];
2840-
}
2841-
28422724
- (void)_removeExpectedValueForAttributePath:(MTRAttributePath *)attributePath expectedValueID:(uint64_t)expectedValueID
28432725
{
28442726
os_unfair_lock_assert_owner(&self->_lock);

src/darwin/Framework/CHIP/MTRDevice_Concrete.mm

-5
Original file line numberDiff line numberDiff line change
@@ -3644,11 +3644,6 @@ - (NSArray *)_getAttributesToReportWithNewExpectedValues:(NSArray<NSDictionary<N
36443644
return attributesToReport;
36453645
}
36463646

3647-
- (void)setExpectedValues:(NSArray<NSDictionary<NSString *, id> *> *)values expectedValueInterval:(NSNumber *)expectedValueInterval
3648-
{
3649-
[self setExpectedValues:values expectedValueInterval:expectedValueInterval expectedValueID:nil];
3650-
}
3651-
36523647
// expectedValueID is an out-argument that returns an identifier to be used when removing expected values
36533648
- (void)setExpectedValues:(NSArray<NSDictionary<NSString *, id> *> *)values
36543649
expectedValueInterval:(NSNumber *)expectedValueInterval

src/darwin/Framework/CHIP/MTRDevice_Internal.h

-4
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,6 @@ MTR_DIRECT_MEMBERS
125125
- (instancetype)initForSubclassesWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceController *)controller;
126126
- (instancetype)initWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceController *)controller;
127127

128-
// Called from MTRClusters for writes and commands
129-
- (void)setExpectedValues:(NSArray<NSDictionary<NSString *, id> *> *)values
130-
expectedValueInterval:(NSNumber *)expectedValueIntervalMs;
131-
132128
// called by controller to clean up and shutdown
133129
- (void)invalidate;
134130

0 commit comments

Comments
 (0)