|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Project CHIP Authors |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +#import "HomeKitConnector.h" |
| 20 | +#import "../CHIPCommandBridge.h" |
| 21 | +#import <lib/support/logging/CHIPLogging.h> |
| 22 | + |
| 23 | +#import <HomeKit/HomeKit.h> |
| 24 | + |
| 25 | +const int64_t kHomeManagerSetupTimeout = 10LL * NSEC_PER_SEC; |
| 26 | +NSString * kControllerIdPrefixStr = @(kControllerIdPrefix); |
| 27 | + |
| 28 | +@interface HomeKitConnector () <HMHomeManagerDelegate> |
| 29 | +@property (nonatomic, assign) BOOL connectorStarted; |
| 30 | +@property (nonatomic, strong) HMHomeManager * homeManager; |
| 31 | +@property (nonatomic, assign) BOOL homeManagerReady; |
| 32 | +@end |
| 33 | + |
| 34 | +@implementation HomeKitConnector { |
| 35 | + dispatch_group_t _homeManagerReadyGroup; |
| 36 | +} |
| 37 | + |
| 38 | ++ (instancetype)sharedInstance |
| 39 | +{ |
| 40 | + static HomeKitConnector * sharedInstance = nil; |
| 41 | + static dispatch_once_t onceToken; |
| 42 | + dispatch_once(&onceToken, ^{ |
| 43 | + sharedInstance = [[HomeKitConnector alloc] init]; |
| 44 | + }); |
| 45 | + return sharedInstance; |
| 46 | +} |
| 47 | + |
| 48 | +- (void)start |
| 49 | +{ |
| 50 | + VerifyOrReturn(!_connectorStarted); |
| 51 | + _connectorStarted = YES; |
| 52 | + |
| 53 | + _homeManagerReady = NO; |
| 54 | + _homeManagerReadyGroup = dispatch_group_create(); |
| 55 | + dispatch_group_enter(_homeManagerReadyGroup); |
| 56 | + |
| 57 | + _homeManager = [[HMHomeManager alloc] init]; |
| 58 | + _homeManager.delegate = self; |
| 59 | + |
| 60 | + // Wait until homeManagerDidUpdateHomes is called or timeout |
| 61 | + dispatch_group_wait(_homeManagerReadyGroup, dispatch_time(DISPATCH_TIME_NOW, kHomeManagerSetupTimeout)); |
| 62 | +} |
| 63 | + |
| 64 | +- (void)stop |
| 65 | +{ |
| 66 | + VerifyOrReturn(_connectorStarted); |
| 67 | + |
| 68 | + _homeManager.delegate = nil; |
| 69 | + _homeManager = nil; |
| 70 | +} |
| 71 | + |
| 72 | +- (void)homeManagerDidUpdateHomes:(HMHomeManager *)manager |
| 73 | +{ |
| 74 | + VerifyOrReturn(!_homeManagerReady); |
| 75 | + dispatch_group_leave(_homeManagerReadyGroup); |
| 76 | +} |
| 77 | + |
| 78 | +- (HMHome *)homeFor:(NSString *)controllerID |
| 79 | +{ |
| 80 | + [[HomeKitConnector sharedInstance] start]; |
| 81 | + |
| 82 | + __auto_type * homes = _homeManager.homes; |
| 83 | + VerifyOrReturnValue(0 != homes.count, nil, ChipLogError(chipTool, "HomeKit is not configured with any homes.")); |
| 84 | + |
| 85 | + NSNumber * fabricId = nil; |
| 86 | + if ([controllerID hasPrefix:kControllerIdPrefixStr]) { |
| 87 | + __auto_type * fabricIdString = [controllerID substringFromIndex:kControllerIdPrefixStr.length]; |
| 88 | + fabricId = @([fabricIdString integerValue]); |
| 89 | + } else { |
| 90 | + fabricId = CHIPCommandBridge::GetCommissionerFabricId([controllerID UTF8String]); |
| 91 | + } |
| 92 | + |
| 93 | + // When multiple homes exist, the first controller corresponds to the first home, the second controller to the second home, etc. |
| 94 | + // If there are fewer homes than controllers, controllers beyond the last home will be associated with the final home in the list. |
| 95 | + NSUInteger index = [fabricId unsignedShortValue] - 1; |
| 96 | + if (index >= homes.count) { |
| 97 | + index = homes.count - 1; |
| 98 | + } |
| 99 | + |
| 100 | + return homes[index]; |
| 101 | +} |
| 102 | + |
| 103 | +- (NSString *)homeControllerIDFor:(NSString *)controllerID |
| 104 | +{ |
| 105 | + __auto_type * home = [self homeFor:controllerID]; |
| 106 | + return home.matterControllerID; |
| 107 | +} |
| 108 | + |
| 109 | +- (NSXPCConnection * (^)(void) )connectBlockFor:(NSString *)controllerID; |
| 110 | +{ |
| 111 | + __auto_type * home = [self homeFor:controllerID]; |
| 112 | + ChipLogProgress(chipTool, "Controller '%s' will be associated with home '%s'.", [controllerID UTF8String], [home.matterControllerID UTF8String]); |
| 113 | + |
| 114 | + if ([controllerID hasPrefix:kControllerIdPrefixStr]) { |
| 115 | + if ([home respondsToSelector:NSSelectorFromString(@"matterStartupParametersXPCConnectBlock")]) { |
| 116 | + return [home valueForKey:@"matterStartupParametersXPCConnectBlock"]; |
| 117 | + } |
| 118 | + |
| 119 | + ChipLogError(chipTool, "Error: 'matterStartupParametersXPCConnectBlock' not available for controller '%s'.", [controllerID UTF8String]); |
| 120 | + return nil; |
| 121 | + } else { |
| 122 | + return home.matterControllerXPCConnectBlock; |
| 123 | + } |
| 124 | +} |
| 125 | +@end |
0 commit comments