Skip to content

Commit 2eea04d

Browse files
Address review comments.
1 parent 1ba3b2d commit 2eea04d

8 files changed

+66
-87
lines changed

src/darwin/Framework/CHIP/MTRCallbackBridgeBase.h

+2
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ class MTRCallbackBridge : public MTRCallbackBridgeBase {
215215
}
216216

217217
if (!callbackBridge->mQueue) {
218+
ChipLogDetail(Controller, "%s %f seconds: can't dispatch response; no queue", callbackBridge->mCookie.UTF8String,
219+
-[callbackBridge->mRequestTime timeIntervalSinceNow]);
218220
if (!callbackBridge->mKeepAlive) {
219221
delete callbackBridge;
220222
}

src/darwin/Framework/CHIP/MTRDeviceController.h

+6
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,12 @@ MTR_AVAILABLE(ios(16.1), macos(13.0), watchos(9.1), tvos(16.1))
248248
*/
249249
- (void)removeServerEndpoint:(MTRServerEndpoint *)endpoint queue:(dispatch_queue_t)queue completion:(dispatch_block_t)completion MTR_NEWLY_AVAILABLE;
250250

251+
/**
252+
* Remove the given server endpoint without being notified when the removal
253+
* completes.
254+
*/
255+
- (void)removeServerEndpoint:(MTRServerEndpoint *)endpoint MTR_NEWLY_AVAILABLE;
256+
251257
/**
252258
* Compute a PASE verifier for the desired setup passcode.
253259
*

src/darwin/Framework/CHIP/MTRDeviceController.mm

+17-3
Original file line numberDiff line numberDiff line change
@@ -981,19 +981,33 @@ - (BOOL)addServerEndpoint:(MTRServerEndpoint *)endpoint
981981
return YES;
982982
}
983983

984-
- (void)removeServerEndpoint:(MTRServerEndpoint *)endpoint queue:(dispatch_queue_t)queue completion:(dispatch_block_t)completion MTR_NEWLY_AVAILABLE
984+
- (void)removeServerEndpoint:(MTRServerEndpoint *)endpoint queue:(dispatch_queue_t)queue completion:(dispatch_block_t)completion
985+
{
986+
[self removeServerEndpointInternal:endpoint queue:queue completion:completion];
987+
}
988+
989+
- (void)removeServerEndpoint:(MTRServerEndpoint *)endpoint
990+
{
991+
[self removeServerEndpointInternal:endpoint queue:nil completion:nil];
992+
}
993+
994+
- (void)removeServerEndpointInternal:(MTRServerEndpoint *)endpoint queue:(dispatch_queue_t _Nullable)queue completion:(dispatch_block_t _Nullable)completion
985995
{
986996
VerifyOrReturn([self checkIsRunning]);
987997

988998
// We need to unhook the endpoint from the Matter side before we can start
989999
// tearing it down.
9901000
[self asyncDispatchToMatterQueue:^() {
9911001
[self removeServerEndpointOnMatterQueue:endpoint];
992-
dispatch_async(queue, completion);
1002+
if (queue != nil && completion != nil) {
1003+
dispatch_async(queue, completion);
1004+
}
9931005
}
9941006
errorHandler:^(NSError * error) {
9951007
// Error means we got shut down, so the endpoint is removed now.
996-
dispatch_async(queue, completion);
1008+
if (queue != nil && completion != nil) {
1009+
dispatch_async(queue, completion);
1010+
}
9971011
}];
9981012
}
9991013

src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ - (BOOL)startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParams
422422
return;
423423
}
424424

425-
[MTRServerAccessControl init];
425+
InitializeServerAccessControl();
426426

427427
if (startupParams.hasStorage) {
428428
_persistentStorageDelegate = new (std::nothrow) MTRPersistentStorageDelegateBridge(startupParams.storage);

src/darwin/Framework/CHIP/ServerEndpoint/MTRServerAccessControl.h

+1-12
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,10 @@
1919

2020
NS_ASSUME_NONNULL_BEGIN
2121

22-
/**
23-
* An access control implementation that looks at the MTRAccessGrants we have.
24-
*/
25-
@interface MTRServerAccessControl : NSObject
26-
27-
- (instancetype)init NS_UNAVAILABLE;
28-
+ (instancetype)new NS_UNAVAILABLE;
29-
+ (instancetype)alloc NS_UNAVAILABLE;
30-
3122
/**
3223
* Initialize the access control module. Must be called on the Matter task
3324
* queue.
3425
*/
35-
+ (void)init;
36-
37-
@end
26+
void InitializeServerAccessControl();
3827

3928
NS_ASSUME_NONNULL_END

src/darwin/Framework/CHIP/ServerEndpoint/MTRServerAccessControl.mm

+6-7
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ int MatterGetAccessPrivilegeForReadAttribute(ClusterId cluster, AttributeId attr
150150
{
151151
NSNumber * _Nullable neededPrivilege = [[MTRDeviceControllerFactory sharedInstance] neededReadPrivilegeForClusterID:@(cluster) attributeID:@(attribute)];
152152
if (neededPrivilege == nil) {
153-
// Default is View.
154-
return kMatterAccessPrivilegeView;
153+
// No privileges declared for this attribute on this cluster. Treat as
154+
// "needs admin privileges", so we fail closed.
155+
return kMatterAccessPrivilegeAdminister;
155156
}
156157

157158
switch (neededPrivilege.unsignedLongLongValue) {
@@ -182,12 +183,10 @@ int MatterGetAccessPrivilegeForWriteAttribute(ClusterId cluster, AttributeId att
182183
return kMatterAccessPrivilegeOperate;
183184
}
184185

185-
@implementation MTRServerAccessControl
186-
187-
+ (void)init
186+
void InitializeServerAccessControl()
188187
{
188+
assertChipStackLockedByCurrentThread();
189+
189190
// Ensure the access control bits are created. No-op after the first call.
190191
gControllerAccessControl.get();
191192
}
192-
193-
@end

src/darwin/Framework/CHIP/ServerEndpoint/MTRServerCluster.mm

+28-59
Original file line numberDiff line numberDiff line change
@@ -71,32 +71,13 @@ @implementation MTRServerCluster {
7171
std::unique_ptr<MTRServerAttributeAccessInterface> _attributeAccessInterface;
7272
// We can't use something like std::unique_ptr<EmberAfAttributeMetadata[]>
7373
// because EmberAfAttributeMetadata does not have a default constructor, so
74-
// we can't alloc and then initializer later. And we need a contiguous
75-
// buffer for all the attribute metadata, so we need to do this by hand.
76-
EmberAfAttributeMetadata * _matterAttributeMetadata;
77-
size_t _matterAttributeMetadataCount;
74+
// we can't alloc and then initializer later.
75+
std::vector<EmberAfAttributeMetadata> _matterAttributeMetadata;
7876

7977
std::unique_ptr<CommandId[]> _matterAcceptedCommandList;
8078
std::unique_ptr<CommandId[]> _matterGeneratedCommandList;
8179
}
8280

83-
- (void)dealloc
84-
{
85-
[self deallocateAttributeMetadata];
86-
}
87-
88-
- (void)deallocateAttributeMetadata
89-
{
90-
if (_matterAttributeMetadata != nullptr) {
91-
for (size_t i = 0; i < _matterAttributeMetadataCount; ++i) {
92-
_matterAttributeMetadata[i].~EmberAfAttributeMetadata();
93-
}
94-
free(_matterAttributeMetadata);
95-
_matterAttributeMetadata = nullptr;
96-
}
97-
_matterAttributeMetadataCount = 0;
98-
}
99-
10081
- (nullable instancetype)initWithClusterID:(NSNumber *)clusterID revision:(NSNumber *)revision
10182
{
10283
auto clusterIDValue = clusterID.unsignedLongLongValue;
@@ -225,18 +206,13 @@ - (BOOL)addAttribute:(MTRServerAttribute *)attribute
225206
return YES;
226207
}
227208

228-
#define MTR_DECLARE_LIST_ATTRIBUTE(attrID) \
229-
DECLARE_DYNAMIC_ATTRIBUTE(attrID, ARRAY, 0, 0)
230-
231209
static constexpr EmberAfAttributeMetadata sDescriptorAttributesMetadata[] = {
232-
MTR_DECLARE_LIST_ATTRIBUTE(MTRAttributeIDTypeClusterDescriptorAttributeDeviceTypeListID),
233-
MTR_DECLARE_LIST_ATTRIBUTE(MTRAttributeIDTypeClusterDescriptorAttributeServerListID),
234-
MTR_DECLARE_LIST_ATTRIBUTE(MTRAttributeIDTypeClusterDescriptorAttributeClientListID),
235-
MTR_DECLARE_LIST_ATTRIBUTE(MTRAttributeIDTypeClusterDescriptorAttributePartsListID),
210+
DECLARE_DYNAMIC_ATTRIBUTE(MTRAttributeIDTypeClusterDescriptorAttributeDeviceTypeListID, ARRAY, 0, 0),
211+
DECLARE_DYNAMIC_ATTRIBUTE(MTRAttributeIDTypeClusterDescriptorAttributeServerListID, ARRAY, 0, 0),
212+
DECLARE_DYNAMIC_ATTRIBUTE(MTRAttributeIDTypeClusterDescriptorAttributeClientListID, ARRAY, 0, 0),
213+
DECLARE_DYNAMIC_ATTRIBUTE(MTRAttributeIDTypeClusterDescriptorAttributePartsListID, ARRAY, 0, 0),
236214
};
237215

238-
#undef MTR_DECLARE_LIST_ATTRIBUTE
239-
240216
- (BOOL)associateWithController:(nullable MTRDeviceController *)controller
241217
{
242218
MTRDeviceController * existingController = _deviceController;
@@ -292,49 +268,40 @@ - (BOOL)associateWithController:(nullable MTRDeviceController *)controller
292268
return NO;
293269
}
294270

295-
_matterAttributeMetadata = static_cast<EmberAfAttributeMetadata *>(calloc(attributeCount, sizeof(EmberAfAttributeMetadata)));
296-
if (_matterAttributeMetadata == nullptr) {
297-
return NO;
298-
}
299-
300-
_matterAttributeMetadataCount = attributeCount;
301-
302271
size_t attrIndex = 0;
303272
for (; attrIndex < _attributes.count; ++attrIndex) {
304273
auto * attr = _attributes[attrIndex];
305-
// Placement-new into the right slot.
306-
new (&_matterAttributeMetadata[attrIndex])
307-
EmberAfAttributeMetadata(DECLARE_DYNAMIC_ATTRIBUTE(static_cast<AttributeId>(attr.attributeID.unsignedLongLongValue),
308-
// The type does not actually matter, since we plan to
309-
// handle this entirely via AttributeAccessInterface.
310-
// Claim Array because that one will keep random IM
311-
// code from trying to do things with the attribute
312-
// store.
313-
ARRAY,
314-
// Size in bytes does not matter, since we plan to
315-
// handle this entirely via AttributeAccessInterface.
316-
0,
317-
// ATTRIBUTE_MASK_NULLABLE is not relevant because we
318-
// are handling this all via AttributeAccessInterface.
319-
0));
274+
_matterAttributeMetadata.emplace_back(EmberAfAttributeMetadata(DECLARE_DYNAMIC_ATTRIBUTE(static_cast<AttributeId>(attr.attributeID.unsignedLongLongValue),
275+
// The type does not actually matter, since we plan to
276+
// handle this entirely via AttributeAccessInterface.
277+
// Claim Array because that one will keep random IM
278+
// code from trying to do things with the attribute
279+
// store.
280+
ARRAY,
281+
// Size in bytes does not matter, since we plan to
282+
// handle this entirely via AttributeAccessInterface.
283+
0,
284+
// ATTRIBUTE_MASK_NULLABLE is not relevant because we
285+
// are handling this all via AttributeAccessInterface.
286+
0)));
320287
}
321288

322289
if (needsFeatureMap) {
323-
new (&_matterAttributeMetadata[attrIndex]) EmberAfAttributeMetadata(DECLARE_DYNAMIC_ATTRIBUTE(MTRAttributeIDTypeGlobalAttributeFeatureMapID,
324-
BITMAP32, 4, 0));
290+
_matterAttributeMetadata.emplace_back(EmberAfAttributeMetadata(DECLARE_DYNAMIC_ATTRIBUTE(MTRAttributeIDTypeGlobalAttributeFeatureMapID,
291+
BITMAP32, 4, 0)));
325292
++attrIndex;
326293
}
327294

328295
if (needsDescriptorAttributes) {
329296
for (auto & data : sDescriptorAttributesMetadata) {
330-
new (&_matterAttributeMetadata[attrIndex]) EmberAfAttributeMetadata(data);
297+
_matterAttributeMetadata.emplace_back(data);
331298
++attrIndex;
332299
}
333300
}
334301

335302
// Add our ClusterRevision bit.
336-
new (&_matterAttributeMetadata[attrIndex]) EmberAfAttributeMetadata(DECLARE_DYNAMIC_ATTRIBUTE(MTRAttributeIDTypeGlobalAttributeClusterRevisionID,
337-
INT16U, 2, 0));
303+
_matterAttributeMetadata.emplace_back(EmberAfAttributeMetadata(DECLARE_DYNAMIC_ATTRIBUTE(MTRAttributeIDTypeGlobalAttributeClusterRevisionID,
304+
INT16U, 2, 0)));
338305
++attrIndex;
339306

340307
_attributeAccessInterface = std::make_unique<MTRServerAttributeAccessInterface>(_parentEndpoint,
@@ -362,7 +329,7 @@ - (void)invalidate
362329
// queue after associateWithController succeeds, but we are no longer being
363330
// looked at from that queue, so it's safe to reset it here.
364331
_matterAccessGrants = [NSSet set];
365-
[self deallocateAttributeMetadata];
332+
_matterAttributeMetadata.clear();
366333
_attributeAccessInterface.reset();
367334
_matterAcceptedCommandList.reset();
368335
_matterGeneratedCommandList.reset();
@@ -413,7 +380,9 @@ - (void)setParentEndpoint:(EndpointId)endpoint
413380

414381
- (Span<const EmberAfAttributeMetadata>)matterAttributeMetadata
415382
{
416-
return Span<const EmberAfAttributeMetadata>(_matterAttributeMetadata, _matterAttributeMetadataCount);
383+
// This is always called after our _matterAttributeMetadata has been set up
384+
// by associateWithController.
385+
return Span<const EmberAfAttributeMetadata>(_matterAttributeMetadata.data(), _matterAttributeMetadata.size());
417386
}
418387

419388
- (CommandId *)matterAcceptedCommands

src/darwin/Framework/CHIP/ServerEndpoint/MTRServerEndpoint.mm

+5-5
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,10 @@ - (BOOL)addServerCluster:(MTRServerCluster *)serverCluster
175175
DECLARE_DYNAMIC_ATTRIBUTE(attrID, ARRAY, 0, 0)
176176

177177
static constexpr EmberAfAttributeMetadata sDescriptorAttributesMetadata[] = {
178-
MTR_DECLARE_LIST_ATTRIBUTE(MTRAttributeIDTypeClusterDescriptorAttributeDeviceTypeListID),
179-
MTR_DECLARE_LIST_ATTRIBUTE(MTRAttributeIDTypeClusterDescriptorAttributeServerListID),
180-
MTR_DECLARE_LIST_ATTRIBUTE(MTRAttributeIDTypeClusterDescriptorAttributeClientListID),
181-
MTR_DECLARE_LIST_ATTRIBUTE(MTRAttributeIDTypeClusterDescriptorAttributePartsListID),
178+
DECLARE_DYNAMIC_ATTRIBUTE(MTRAttributeIDTypeClusterDescriptorAttributeDeviceTypeListID, ARRAY, 0, 0),
179+
DECLARE_DYNAMIC_ATTRIBUTE(MTRAttributeIDTypeClusterDescriptorAttributeServerListID, ARRAY, 0, 0),
180+
DECLARE_DYNAMIC_ATTRIBUTE(MTRAttributeIDTypeClusterDescriptorAttributeClientListID, ARRAY, 0, 0),
181+
DECLARE_DYNAMIC_ATTRIBUTE(MTRAttributeIDTypeClusterDescriptorAttributePartsListID, ARRAY, 0, 0),
182182
DECLARE_DYNAMIC_ATTRIBUTE(MTRAttributeIDTypeGlobalAttributeFeatureMapID, BITMAP32, 4, 0),
183183
DECLARE_DYNAMIC_ATTRIBUTE(MTRAttributeIDTypeGlobalAttributeClusterRevisionID, INT16U, 2, 0),
184184
};
@@ -257,7 +257,7 @@ - (BOOL)finishAssociationWithController:(nullable MTRDeviceController *)controll
257257

258258
auto attrMetadata = cluster.matterAttributeMetadata;
259259
metadata.attributes = attrMetadata.data();
260-
// This cast is safe because cluster's check for this constraint on
260+
// This cast is safe because clusters check for this constraint on
261261
// number of attributes.
262262
metadata.attributeCount = static_cast<uint16_t>(attrMetadata.size());
263263

0 commit comments

Comments
 (0)