|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 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 | +#import <Matter/MTRDeviceType.h> |
| 19 | + |
| 20 | +#import "MTRDeviceTypeMetadata.h" |
| 21 | +#import "MTRLogging_Internal.h" |
| 22 | + |
| 23 | +#include <lib/support/SafeInt.h> |
| 24 | + |
| 25 | +using namespace chip; |
| 26 | + |
| 27 | +@implementation MTRDeviceType |
| 28 | + |
| 29 | +- (nullable instancetype)initWithDeviceTypeID:(NSNumber *)id name:(NSString *)name isUtility:(BOOL)isUtility |
| 30 | +{ |
| 31 | + if (!(self = [super init])) { |
| 32 | + return nil; |
| 33 | + } |
| 34 | + |
| 35 | + _id = id; |
| 36 | + _name = name; |
| 37 | + _isUtility = isUtility; |
| 38 | + return self; |
| 39 | +} |
| 40 | + |
| 41 | ++ (nullable MTRDeviceType *)deviceTypeForID:(NSNumber *)deviceTypeID |
| 42 | +{ |
| 43 | + if (!CanCastTo<DeviceTypeId>(deviceTypeID.unsignedLongLongValue)) { |
| 44 | + MTR_LOG_ERROR("Invalid device type ID: 0x%llx", deviceTypeID.unsignedLongLongValue); |
| 45 | + return nil; |
| 46 | + } |
| 47 | + |
| 48 | + auto * deviceTypeData = MTRDeviceTypeDataForID(static_cast<DeviceTypeId>(deviceTypeID.unsignedLongLongValue)); |
| 49 | + if (!deviceTypeData) { |
| 50 | + return nil; |
| 51 | + } |
| 52 | + |
| 53 | + return [[MTRDeviceType alloc] |
| 54 | + initWithDeviceTypeID:deviceTypeID |
| 55 | + name:[NSString stringWithUTF8String:deviceTypeData->name] |
| 56 | + isUtility:(deviceTypeData->deviceClass != MTRDeviceTypeClass::Simple)]; |
| 57 | +} |
| 58 | + |
| 59 | +@end |
0 commit comments