Skip to content

Commit

Permalink
Fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
woody-apple committed Oct 29, 2024
1 parent 472c18c commit 7f32eb8
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
61 changes: 61 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ - (NSXPCInterface *)_interfaceForClientProtocol

allowedClasses = [MTRDeviceController_XPC _allowedClasses];

[interface setClasses:allowedClasses
forSelector:@selector(controller:controllerConfigurationUpdated:)
argumentIndex:1
ofReply:NO];

allowedClasses = [MTRDeviceController_XPC _allowedClasses];

[interface setClasses:allowedClasses
forSelector:@selector(controller:controllerConfigurationUpdated:)
argumentIndex:1
Expand Down Expand Up @@ -487,6 +494,60 @@ - (BOOL)isRunning
return YES;
}

- (oneway void)controller:(NSUUID *)controller controllerConfigurationUpdated:(NSDictionary *)configuration
{
// Reuse the same format as config dictionary, and add values for internal states
// @{
// MTRDeviceControllerRegistrationControllerContextKey: @{
// MTRDeviceControllerRegistrationControllerNodeIDKey: controllerNodeID
// }
// MTRDeviceControllerRegistrationNodeIDsKey: @[
// @{
// MTRDeviceControllerRegistrationNodeIDKey: nodeID,
// MTRDeviceControllerRegistrationDeviceInternalStateKey: deviceInternalStateDictionary
// }
// ]
// }

NSDictionary * controllerContext = MTR_SAFE_CAST(configuration[MTRDeviceControllerRegistrationControllerContextKey], NSDictionary);
NSNumber * controllerNodeID = MTR_SAFE_CAST(controllerContext[MTRDeviceControllerRegistrationControllerNodeIDKey], NSNumber);
if (controllerNodeID && controllerNodeID) {
_controllerNodeID = controllerContext[MTRDeviceControllerRegistrationControllerNodeIDKey];
}

NSArray * deviceInfoList = MTR_SAFE_CAST(configuration[MTRDeviceControllerRegistrationNodeIDsKey], NSArray);

MTR_LOG("Received controllerConfigurationUpdated: controllerNode ID %@ deviceInfoList %@", self.controllerNodeID, deviceInfoList);

for (NSDictionary * deviceInfo in deviceInfoList) {
if (!MTR_SAFE_CAST(deviceInfo, NSDictionary)) {
MTR_LOG_ERROR(" - Missing or malformed device Info");
}

NSNumber * nodeID = MTR_SAFE_CAST(deviceInfo[MTRDeviceControllerRegistrationNodeIDKey], NSNumber);
if (!nodeID) {
MTR_LOG_ERROR(" - Missing or malformed nodeID");
continue;
}

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];
}
}

// TODO: Create DeviceControllerRunningState that is a tri-state with "unknown", "not running", and "running" states
- (BOOL)isRunning
{
// For XPC controller, always return yes
return YES;
}

// Not Supported via XPC
//- (oneway void)controller:(NSUUID *)controller statusUpdate:(MTRCommissioningStatus)status {
// }
Expand Down
34 changes: 33 additions & 1 deletion src/darwin/Framework/CHIP/MTRDevice_Concrete.mm
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,7 @@ - (void)_callDelegateDeviceCachePrimed
[delegate deviceCachePrimed:self];
}
}];
[self _notifyDelegateOfPrivateInternalPropertiesChanges];
}

// assume lock is held
Expand Down Expand Up @@ -1015,6 +1016,7 @@ - (void)_changeState:(MTRDeviceState)state
[self _callDelegatesWithBlock:^(id<MTRDeviceDelegate> delegate) {
[delegate device:self stateChanged:state];
}];
[self _notifyDelegateOfPrivateInternalPropertiesChanges];
} else {
MTR_LOG(
"%@ Not reporting reachability state change, since no change in state %lu => %lu", self, static_cast<unsigned long>(lastState), static_cast<unsigned long>(state));
Expand Down Expand Up @@ -1072,6 +1074,15 @@ - (void)_updateEstimatedSubscriptionLatency:(NSNumber *)estimatedSubscriptionLat
_estimatedSubscriptionLatencyForDescription = estimatedSubscriptionLatency;
}

- (void)_updateEstimatedSubscriptionLatency:(NSNumber *)estimatedSubscriptionLatency
{
os_unfair_lock_assert_owner(&_lock);
_estimatedSubscriptionLatency = estimatedSubscriptionLatency;

std::lock_guard lock(_descriptionLock);
_estimatedSubscriptionLatencyForDescription = estimatedSubscriptionLatency;
}

// First Time Sync happens 2 minutes after reachability (this can be changed in the future)
#define MTR_DEVICE_TIME_UPDATE_INITIAL_WAIT_TIME_SEC (60 * 2)
- (void)_handleSubscriptionEstablished
Expand Down Expand Up @@ -1464,6 +1475,7 @@ - (void)_handleUnsolicitedMessageFromPublisher
[delegate deviceBecameActive:self];
}
}];
[self _notifyDelegateOfPrivateInternalPropertiesChanges];

// in case this is called during exponential back off of subscription
// reestablishment, this starts the attempt right away
Expand Down Expand Up @@ -1802,6 +1814,15 @@ - (void)_updateDeviceCachePrimed:(BOOL)deviceCachePrimed
_deviceCachePrimedForDescription = deviceCachePrimed;
}

- (void)_updateDeviceCachePrimed:(BOOL)deviceCachePrimed
{
os_unfair_lock_assert_owner(&_lock);
_deviceCachePrimed = deviceCachePrimed;

std::lock_guard lock(_descriptionLock);
_deviceCachePrimedForDescription = deviceCachePrimed;
}

- (void)_handleReportEnd
{
MTR_LOG("%@ handling report end", self);
Expand Down Expand Up @@ -2042,6 +2063,15 @@ - (void)_updateEstimatedStartTime:(NSDate *)estimatedStartTime
_estimatedStartTimeForDescription = _estimatedStartTime;
}

- (void)_updateEstimatedStartTime:(NSDate *)estimatedStartTime
{
os_unfair_lock_assert_owner(&_lock);
_estimatedStartTime = estimatedStartTime;

std::lock_guard lock(_descriptionLock);
_estimatedStartTimeForDescription = _estimatedStartTime;
}

- (void)_handleEventReport:(NSArray<NSDictionary<NSString *, id> *> *)eventReport
{
std::lock_guard lock(_lock);
Expand Down Expand Up @@ -2467,7 +2497,9 @@ - (void)_setupSubscriptionWithReason:(NSString *)reason
mtr_strongify(self);
VerifyOrReturn(self);

[self _markDeviceAsUnreachableIfNeverSubscribed];
if ( !HaveSubscriptionEstablishedRightNow(self->_internalDeviceState) )
[self _markDeviceAsUnreachableIfNeverSubscribed];

});
}

Expand Down

0 comments on commit 7f32eb8

Please sign in to comment.