Skip to content

Commit

Permalink
Updating this
Browse files Browse the repository at this point in the history
  • Loading branch information
woody-apple committed Oct 28, 2024
1 parent a376079 commit 4d6f1d3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 64 deletions.
38 changes: 20 additions & 18 deletions src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
#import "MTRXPCClientProtocol.h"
#import "MTRXPCServerProtocol.h"

#define MTR_DEVICECONTROLLER_SIMPLE_REMOTE_XPC_GETTER(NAME, TYPE, DEFAULT_VALUE, GETTER_NAME) \
MTR_SIMPLE_REMOTE_XPC_GETTER(self.xpcConnection, NAME, TYPE, DEFAULT_VALUE, GETTER_NAME, deviceController : self.uniqueIdentifier)
#define MTR_DEVICECONTROLLER_SIMPLE_REMOTE_XPC_GETTER(NAME, TYPE, DEFAULT_VALUE, GETTER_NAME) \
MTR_SIMPLE_REMOTE_XPC_GETTER(self.xpcConnection, NAME, TYPE, DEFAULT_VALUE, GETTER_NAME, deviceController \
: self.uniqueIdentifier)

#define MTR_DEVICECONTROLLER_SIMPLE_REMOTE_XPC_COMMAND(METHOD_SIGNATURE, ADDITIONAL_ARGUMENTS) \
MTR_SIMPLE_REMOTE_XPC_COMMAND(self.xpcConnection, METHOD_SIGNATURE, ADDITIONAL_ARGUMENTS, deviceController : self.uniqueIdentifier)
#define MTR_DEVICECONTROLLER_SIMPLE_REMOTE_XPC_COMMAND(METHOD_SIGNATURE, ADDITIONAL_ARGUMENTS) \
MTR_SIMPLE_REMOTE_XPC_COMMAND(self.xpcConnection, METHOD_SIGNATURE, ADDITIONAL_ARGUMENTS, deviceController \
: self.uniqueIdentifier)

@interface MTRDeviceController_XPC ()

Expand All @@ -51,7 +53,9 @@ @implementation MTRDeviceController_XPC

#pragma mark - Node ID Management

MTR_DEVICECONTROLLER_SIMPLE_REMOTE_XPC_COMMAND(updateControllerConfiguration : (NSDictionary *) controllerState, updateControllerConfiguration : (NSDictionary *) controllerState)
MTR_DEVICECONTROLLER_SIMPLE_REMOTE_XPC_COMMAND(updateControllerConfiguration
: (NSDictionary *) controllerState, updateControllerConfiguration
: (NSDictionary *) controllerState)

- (void)_updateRegistrationInfo
{
Expand Down Expand Up @@ -455,24 +459,22 @@ - (oneway void)controller:(NSUUID *)controller controllerConfigurationUpdated:(N
MTR_LOG("Received controllerConfigurationUpdated: controllerNode ID %@ deviceInfoList %@", self.controllerNodeID, deviceInfoList);

for (NSDictionary * deviceInfo in deviceInfoList) {
if (![deviceInfo isKindOfClass:[NSDictionary class]]) {
MTR_LOG_ERROR(" - deviceInfo %@ not NSDictionary class %@", deviceInfo, NSStringFromClass([deviceInfo class]));
continue;
}
NSNumber * nodeID = deviceInfo[MTRDeviceControllerRegistrationNodeIDKey];
if (nodeID && ![nodeID isKindOfClass:[NSNumber class]]) {
MTR_LOG_ERROR(" - deviceInfo %@ nodeID not NSNumber class %@", deviceInfo, NSStringFromClass([nodeID class]));
continue;
if (!MTR_SAFE_CAST(deviceInfo, NSDictionary)) {
MTR_LOG_ERROR(" - Missing or malformed device Info");
}
NSDictionary * deviceInternalState = deviceInfo[MTRDeviceControllerRegistrationDeviceInternalStateKey];
if (deviceInternalState && ![deviceInternalState isKindOfClass:[NSDictionary class]]) {
MTR_LOG_ERROR(" - deviceInfo %@ deviceInternalState not NSNumber class %@", deviceInfo, NSStringFromClass([deviceInternalState class]));

NSNumber * nodeID = MTR_SAFE_CAST(deviceInfo[MTRDeviceControllerRegistrationNodeIDKey], NSNumber);
if (!nodeID) {
MTR_LOG_ERROR(" - Missing or malformed nodeID");
continue;
}
if (!nodeID || !deviceInternalState) {
MTR_LOG(" - deviceInfo %@ missing elements: nodeID %@ internalState %@", deviceInfo, nodeID, deviceInternalState);

NSDictionary * deviceInternalState = MTR_SAFE_CAST(deviceInfo[MTRDeviceControllerRegistrationDeviceInternalStateKey], NSDictionary);
if (!deviceInternalState) {
MTR_LOG_ERROR(" - Missing or malformed deviceInternalState");
continue;
}

auto * device = static_cast<MTRDevice_XPC *>([self deviceForNodeID:nodeID]);
[device device:nodeID internalStateUpdated:deviceInternalState];
}
Expand Down
81 changes: 35 additions & 46 deletions src/darwin/Framework/CHIP/MTRDevice_XPC.mm
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#import "MTRSetupPayload.h"
#import "MTRTimeUtils.h"
#import "MTRUnfairLock.h"
#import "MTRUtilities.h"
#import "NSDataSpanConversion.h"
#import "NSStringSpanConversion.h"

Expand Down Expand Up @@ -113,48 +114,39 @@ - (NSString *)description
}

// TODO: Add these to the description
// MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyDeviceInternalState, _internalDeviceStateForDescription, properties);
// MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyLastSubscriptionAttemptWait, _lastSubscriptionAttemptWaitForDescription, properties);
// MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyMostRecentReportTime, _mostRecentReportTimeForDescription, properties);
// MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyLastSubscriptionFailureTime, _lastSubscriptionFailureTimeForDescription, properties);

return [NSString
stringWithFormat:@"<%@: %p, node: %016llX-%016llX (%llu), VID: %@, PID: %@, WiFi: %@, Thread: %@, controller: %@>",
NSStringFromClass(self.class), self,
_deviceController.compressedFabricID.unsignedLongLongValue,
_nodeID.unsignedLongLongValue,
_nodeID.unsignedLongLongValue,
[self._internalState objectForKey:kMTRDeviceInternalPropertyKeyVendorID],
[self._internalState objectForKey:kMTRDeviceInternalPropertyKeyProductID],
wifi,
thread,
_deviceController.uniqueIdentifier];
return [NSString stringWithFormat:@"<%@: %p, node: %016llX-%016llX (%llu), VID: %@, PID: %@, WiFi: %@, Thread: %@, controller: %@ state: %ld>",
NSStringFromClass(self.class), self,
_deviceController.compressedFabricID.unsignedLongLongValue,
_nodeID.unsignedLongLongValue,
_nodeID.unsignedLongLongValue,
[self vendorID],
[self productID],
wifi,
thread,
_deviceController.uniqueIdentifier,
[self state]];
}

- (nullable NSNumber *)vendorID
{
return [[self._internalState objectForKey:kMTRDeviceInternalPropertyKeyVendorID] copy];
return [self._internalState objectForKey:kMTRDeviceInternalPropertyKeyVendorID];
}

- (nullable NSNumber *)productID
{
return [[self._internalState objectForKey:kMTRDeviceInternalPropertyKeyProductID] copy];
return [self._internalState objectForKey:kMTRDeviceInternalPropertyKeyProductID];
}

#pragma mark - Client Callbacks (MTRDeviceDelegate)

// required methods for MTRDeviceDelegates
- (oneway void)device:(NSNumber *)nodeID stateChanged:(MTRDeviceState)state
{
if (!MTR_SAFE_CAST(nodeID, NSNumber)) {
MTR_LOG_ERROR("%@ invalid device:stateChanged: nodeID: %@", self, nodeID);
return;
}

MTR_LOG("%@ %s", self, __PRETTY_FUNCTION__);
[self _lockAndCallDelegatesWithBlock:^(id<MTRDeviceDelegate> delegate) {
[delegate device:self stateChanged:state];
}];
// Not needed, since internal will get this
}

- (oneway void)device:(NSNumber *)nodeID receivedAttributeReport:(NSArray<MTRDeviceResponseValueDictionary> *)attributeReport
Expand Down Expand Up @@ -211,17 +203,7 @@ - (oneway void)deviceBecameActive:(NSNumber *)nodeID

- (oneway void)deviceCachePrimed:(NSNumber *)nodeID
{
MTR_LOG("%@ %s", self, __PRETTY_FUNCTION__);
if (!MTR_SAFE_CAST(nodeID, NSNumber)) {
MTR_LOG_ERROR("%@ invalid deviceCachePrimed: nodeID: %@", self, nodeID);
return;
}

[self _lockAndCallDelegatesWithBlock:^(id<MTRDeviceDelegate> delegate) {
if ([delegate respondsToSelector:@selector(deviceCachePrimed:)]) {
[delegate deviceCachePrimed:self];
}
}];
// Not needed since this is a state udpate now
}

- (oneway void)deviceConfigurationChanged:(NSNumber *)nodeID
Expand Down Expand Up @@ -278,46 +260,53 @@ - (oneway void)device:(NSNumber *)nodeID internalStateUpdated:(NSDictionary *)di
return;
}

NSNumber * oldStateNumber = self._internalState[kMTRDeviceInternalPropertyDeviceState];
NSNumber * newStateNumber = dictionary[kMTRDeviceInternalPropertyDeviceState];
NSNumber * oldStateNumber = MTR_SAFE_CAST(self._internalState[kMTRDeviceInternalPropertyDeviceState], NSNumber);
NSNumber * newStateNumber = MTR_SAFE_CAST(dictionary[kMTRDeviceInternalPropertyDeviceState], NSNumber);

VerifyOrReturn([self _internalState:dictionary hasValidValuesForKeys:requiredInternalStateKeys valueRequired:YES]);
VerifyOrReturn([self _internalState:dictionary hasValidValuesForKeys:optionalInternalStateKeys valueRequired:NO]);

[self _setInternalState:dictionary];

// Call delegate if state changed. State is considered changed if:
// 1) old state is nil but new state is not nil
// 2) old state is not nil but new state is nil
// 3) both old and new state are not nil, and they are not equal
if ((!oldStateNumber && newStateNumber) || (oldStateNumber && !newStateNumber) || (oldStateNumber && newStateNumber && ![newStateNumber isEqualToNumber:oldStateNumber])) {
MTRDeviceState state = static_cast<MTRDeviceState>(newStateNumber ? newStateNumber.unsignedIntegerValue : MTRDeviceStateUnknown);
if (!MTREqualObjects(oldStateNumber, newStateNumber)) {
MTRDeviceState state = self.state;
[self _lockAndCallDelegatesWithBlock:^(id<MTRDeviceDelegate> delegate) {
[delegate device:self stateChanged:state];
}];
}

NSNumber * oldPrimedState = MTR_SAFE_CAST(self._internalState[kMTRDeviceInternalPropertyDeviceCachePrimed], NSNumber);
NSNumber * newPrimedState = MTR_SAFE_CAST(dictionary[kMTRDeviceInternalPropertyDeviceCachePrimed], NSNumber);

if (!MTREqualObjects(oldPrimedState, newPrimedState)) {
[self _lockAndCallDelegatesWithBlock:^(id<MTRDeviceDelegate> delegate) {
if ([delegate respondsToSelector:@selector(deviceCachePrimed:)]) {
[delegate deviceCachePrimed:self];
}
}];
}
}

- (MTRDeviceState)state
{
NSNumber * stateNumber = self._internalState[kMTRDeviceInternalPropertyDeviceState];
NSNumber * stateNumber = MTR_SAFE_CAST(self._internalState[kMTRDeviceInternalPropertyDeviceState], NSNumber);
return stateNumber ? static_cast<MTRDeviceState>(stateNumber.unsignedIntegerValue) : MTRDeviceStateUnknown;
}

- (BOOL)deviceCachePrimed
{
NSNumber * deviceCachePrimedNumber = self._internalState[kMTRDeviceInternalPropertyDeviceCachePrimed];
NSNumber * deviceCachePrimedNumber = MTR_SAFE_CAST(self._internalState[kMTRDeviceInternalPropertyDeviceCachePrimed], NSNumber);
return deviceCachePrimedNumber.boolValue;
}

- (nullable NSDate *)estimatedStartTime
{
return self._internalState[kMTRDeviceInternalPropertyEstimatedStartTime];
return MTR_SAFE_CAST(self._internalState[kMTRDeviceInternalPropertyEstimatedStartTime], NSDate);
}

- (nullable NSNumber *)estimatedSubscriptionLatency
{
return self._internalState[kMTRDeviceInternalPropertyEstimatedSubscriptionLatency];
return MTR_SAFE_CAST(self._internalState[kMTRDeviceInternalPropertyEstimatedSubscriptionLatency], NSNumber);
}

#pragma mark - Remote Commands
Expand Down

0 comments on commit 4d6f1d3

Please sign in to comment.