Skip to content

Commit 1260206

Browse files
bzbarsky-applepull[bot]
authored andcommitted
Minor cleanup around read/subscribe(To)AttributePaths. (#26171)
1) Use initWithArray:copyItems:YES when copying arrays, instead of hand-rolling the copy. 2) For read, empty paths should just lead to an empty result, not an error, just like they would if the read actually happened. Fixes #26169
1 parent 9f369cb commit 1260206

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

src/darwin/Framework/CHIP/MTRBaseDevice.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,10 @@ MTR_NEWLY_AVAILABLE
287287
*
288288
* Lists of attribute and event paths to read can be provided via attributePaths and eventPaths.
289289
*
290-
* The completion will be called with an error if the input parameters are invalid (e.g., both attributePaths and eventPaths are
291-
* empty.) or the entire read interaction fails. Otherwise it will be called with values, which may be empty (e.g. if no paths
292-
* matched the wildcard paths passed in) or may include per-path errors if particular paths failed.
290+
* The completion will be called with an error if the entire read interaction fails. Otherwise it
291+
* will be called with an array of values. This array may be empty (e.g. if no paths matched the
292+
* wildcard paths passed in, or if empty lists of paths were passed in) or may include per-path
293+
* errors if particular paths failed.
293294
*
294295
* If the sum of the lengths of attributePaths and eventPaths exceeds 9, the read may fail due to the device not supporting that
295296
* many read paths.
@@ -394,8 +395,8 @@ MTR_NEWLY_AVAILABLE
394395
*
395396
* Lists of attribute and event paths to subscribe to can be provided via attributePaths and eventPaths.
396397
*
397-
* The reportHandler will be called with an error if the inputs are invalid (e.g., both attributePaths and eventPaths are
398-
* empty), or if the subscription fails entirely.
398+
* The reportHandler will be called with an error if the subscription fails
399+
* entirely (including when both attributePaths and eventPaths are empty).
399400
*
400401
* The reportHandler will be called with arrays of response-value dictionaries
401402
* (which may be data or errors) as path-specific data is received.

src/darwin/Framework/CHIP/MTRBaseDevice.mm

+15-24
Original file line numberDiff line numberDiff line change
@@ -917,26 +917,21 @@ - (void)readAttributePaths:(NSArray<MTRAttributeRequestPath *> * _Nullable)attri
917917
completion:(MTRDeviceResponseHandler)completion
918918
{
919919
if ((attributePaths == nil || [attributePaths count] == 0) && (eventPaths == nil || [eventPaths count] == 0)) {
920+
// No paths, just return an empty array.
920921
dispatch_async(queue, ^{
921-
completion(nil, [MTRError errorForCHIPErrorCode:CHIP_ERROR_INVALID_ARGUMENT]);
922+
completion(@[], nil);
922923
});
923924
return;
924925
}
925926

926-
NSMutableArray<MTRAttributeRequestPath *> * attributes = nil;
927+
NSArray<MTRAttributeRequestPath *> * attributes = nil;
927928
if (attributePaths != nil) {
928-
attributes = [[NSMutableArray alloc] init];
929-
for (MTRAttributeRequestPath * attributePath in attributePaths) {
930-
[attributes addObject:[attributePath copy]];
931-
}
929+
attributes = [[NSArray alloc] initWithArray:attributePaths copyItems:YES];
932930
}
933931

934-
NSMutableArray<MTREventRequestPath *> * events = nil;
932+
NSArray<MTREventRequestPath *> * events = nil;
935933
if (eventPaths != nil) {
936-
events = [[NSMutableArray alloc] init];
937-
for (MTRAttributeRequestPath * eventPath in eventPaths) {
938-
[events addObject:[eventPath copy]];
939-
}
934+
events = [[NSArray alloc] initWithArray:eventPaths copyItems:YES];
940935
}
941936
params = (params == nil) ? nil : [params copy];
942937
auto * bridge = new MTRDataValueDictionaryCallbackBridge(queue, completion,
@@ -1006,9 +1001,9 @@ - (void)readAttributePaths:(NSArray<MTRAttributeRequestPath *> * _Nullable)attri
10061001
chip::app::ReadPrepareParams readParams(session);
10071002
[params toReadPrepareParams:readParams];
10081003
readParams.mpAttributePathParamsList = attributePathParamsList.Get();
1009-
readParams.mAttributePathParamsListSize = [attributePaths count];
1004+
readParams.mAttributePathParamsListSize = [attributes count];
10101005
readParams.mpEventPathParamsList = eventPathParamsList.Get();
1011-
readParams.mEventPathParamsListSize = [eventPaths count];
1006+
readParams.mEventPathParamsListSize = [events count];
10121007

10131008
AttributePathParams * attributePathParamsListToFree = attributePathParamsList.Get();
10141009
EventPathParams * eventPathParamsListToFree = eventPathParamsList.Get();
@@ -1288,8 +1283,10 @@ - (void)subscribeToAttributePaths:(NSArray<MTRAttributeRequestPath *> * _Nullabl
12881283
resubscriptionScheduled:(MTRDeviceResubscriptionScheduledHandler _Nullable)resubscriptionScheduled
12891284
{
12901285
if ((attributePaths == nil || [attributePaths count] == 0) && (eventPaths == nil || [eventPaths count] == 0)) {
1286+
// Per spec a server would respond InvalidAction to this, so just go
1287+
// ahead and do that.
12911288
dispatch_async(queue, ^{
1292-
reportHandler(nil, [MTRError errorForCHIPErrorCode:CHIP_ERROR_INVALID_ARGUMENT]);
1289+
reportHandler(nil, [MTRError errorForIMStatus:StatusIB(Status::InvalidAction)]);
12931290
});
12941291
return;
12951292
}
@@ -1303,20 +1300,14 @@ - (void)subscribeToAttributePaths:(NSArray<MTRAttributeRequestPath *> * _Nullabl
13031300
}
13041301

13051302
// Copy params before going async.
1306-
NSMutableArray<MTRAttributeRequestPath *> * attributes = nil;
1303+
NSArray<MTRAttributeRequestPath *> * attributes = nil;
13071304
if (attributePaths != nil) {
1308-
attributes = [[NSMutableArray alloc] init];
1309-
for (MTRAttributeRequestPath * attributePath in attributePaths) {
1310-
[attributes addObject:[attributePath copy]];
1311-
}
1305+
attributes = [[NSArray alloc] initWithArray:attributePaths copyItems:YES];
13121306
}
13131307

1314-
NSMutableArray<MTREventRequestPath *> * events = nil;
1308+
NSArray<MTREventRequestPath *> * events = nil;
13151309
if (eventPaths != nil) {
1316-
events = [[NSMutableArray alloc] init];
1317-
for (MTRAttributeRequestPath * eventPath in eventPaths) {
1318-
[events addObject:[eventPath copy]];
1319-
}
1310+
events = [[NSArray alloc] initWithArray:eventPaths copyItems:YES];
13201311
}
13211312

13221313
params = (params == nil) ? nil : [params copy];

0 commit comments

Comments
 (0)