Skip to content

Commit 579b1b1

Browse files
Add a way to ask Matter.framework for information about device types. (project-chip#36084)
1 parent 064c205 commit 579b1b1

11 files changed

+442
-100
lines changed
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
#pragma once
19+
20+
#import <Foundation/Foundation.h>
21+
22+
#import <Matter/MTRDefines.h>
23+
24+
NS_ASSUME_NONNULL_BEGIN
25+
26+
MTR_NEWLY_AVAILABLE
27+
@interface MTRDeviceType : NSObject
28+
29+
/**
30+
* Returns an MTRDeviceType for the given ID, if the ID is known. Returns nil
31+
* for unknown IDs.
32+
*/
33+
+ (nullable MTRDeviceType *)deviceTypeForID:(NSNumber *)deviceTypeID;
34+
35+
/**
36+
* The identifier of the device type (32-bit unsigned integer).
37+
*/
38+
@property (nonatomic, readonly, copy) NSNumber * id;
39+
40+
/**
41+
* Returns the name of the device type.
42+
*/
43+
@property (nonatomic, readonly, retain) NSString * name;
44+
45+
/**
46+
* Returns whether this is a utility device type.
47+
*/
48+
@property (nonatomic, readonly, assign) BOOL isUtility;
49+
50+
- (instancetype)init NS_UNAVAILABLE;
51+
+ (instancetype)new NS_UNAVAILABLE;
52+
53+
@end
54+
55+
NS_ASSUME_NONNULL_END
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

src/darwin/Framework/CHIP/MTRDeviceTypeMetadata.h

+20-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,23 @@
2121

2222
#include <lib/core/DataModelTypes.h>
2323

24-
BOOL MTRIsKnownUtilityDeviceType(chip::DeviceTypeId aDeviceTypeId);
24+
NS_ASSUME_NONNULL_BEGIN
25+
26+
enum class MTRDeviceTypeClass
27+
{
28+
Utility,
29+
Simple,
30+
Node, // Might not be a real class, but we have it for Root Node for now.
31+
};
32+
33+
struct MTRDeviceTypeData
34+
{
35+
chip::DeviceTypeId id;
36+
MTRDeviceTypeClass deviceClass;
37+
const char * name;
38+
};
39+
40+
// Returns null for unknown device types.
41+
const MTRDeviceTypeData * _Nullable MTRDeviceTypeDataForID(chip::DeviceTypeId aDeviceTypeId);
42+
43+
NS_ASSUME_NONNULL_END

src/darwin/Framework/CHIP/Matter.h

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#import <Matter/MTRDeviceControllerStartupParams.h>
4949
#import <Matter/MTRDeviceControllerStorageDelegate.h>
5050
#import <Matter/MTRDeviceStorageBehaviorConfiguration.h>
51+
#import <Matter/MTRDeviceType.h>
5152
#import <Matter/MTRDeviceTypeRevision.h>
5253
#import <Matter/MTRDiagnosticLogsType.h>
5354
#import <Matter/MTRError.h>

src/darwin/Framework/CHIP/templates/MTRClusterConstants.zapt

+12
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,15 @@ MTREventIDTypeCluster{{>cluster}}Event{{>event}}ID {{availability (asUpperCamelC
225225
{{> eventIDs clusterName=label}}
226226
{{/zcl_clusters}}
227227
};
228+
229+
#pragma mark - Device Type IDs
230+
231+
typedef NS_ENUM(uint32_t, MTRDeviceTypeIDType) {
232+
{{#zcl_device_types}}
233+
{{! Only include standard device types for now. The template syntax does not get hex numbers, apparently.
234+
ZAP seems to have no is_number_less_than helper, so do is_number_greater_than with the arguments reversed. }}
235+
{{#if (is_number_greater_than 65536 code)}}
236+
MTRDeviceTypeIDType{{asUpperCamelCase caption preserveAcronyms=true}}ID {{availability "" deviceType=(asUpperCamelCase caption preserveAcronyms=true)}} = {{asHex code 8}},
237+
{{/if}}
238+
{{/zcl_device_types}}
239+
} MTR_NEWLY_AVAILABLE;

src/darwin/Framework/CHIP/templates/MTRDeviceTypeMetadata-src.zapt

+5-17
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,11 @@
55
using namespace chip;
66

77
namespace {
8-
enum class DeviceTypeClass {
9-
Utility,
10-
Simple,
11-
Node, // Might not be a real class, but we have it for Root Node for now.
12-
// If new classes get added, plase audit MTRIsKnownUtilityDeviceType below.
13-
};
14-
15-
struct DeviceTypeData {
16-
DeviceTypeId id;
17-
DeviceTypeClass deviceClass;
18-
const char * name;
19-
};
208

21-
constexpr DeviceTypeData knownDeviceTypes[] = {
9+
constexpr MTRDeviceTypeData knownDeviceTypes[] = {
2210
{{#zcl_device_types}}
2311
{{#if class}}
24-
{ {{asHex code 8}}, DeviceTypeClass::{{class}}, "{{caption}}" },
12+
{ {{asHex code 8}}, MTRDeviceTypeClass::{{class}}, "{{caption}}" },
2513
{{/if}}
2614
{{/zcl_device_types}}
2715
};
@@ -34,12 +22,12 @@ static_assert(ExtractVendorFromMEI({{asHex code 8}}) != 0, "Must have class defi
3422

3523
} // anonymous namespace
3624

37-
BOOL MTRIsKnownUtilityDeviceType(DeviceTypeId aDeviceTypeId)
25+
const MTRDeviceTypeData * _Nullable MTRDeviceTypeDataForID(chip::DeviceTypeId aDeviceTypeId)
3826
{
3927
for (auto & deviceType : knownDeviceTypes) {
4028
if (deviceType.id == aDeviceTypeId) {
41-
return deviceType.deviceClass != DeviceTypeClass::Simple;
29+
return &deviceType;
4230
}
4331
}
44-
return NO;
32+
return nullptr;
4533
}

src/darwin/Framework/CHIP/templates/availability.yaml

+70
Original file line numberDiff line numberDiff line change
@@ -9742,6 +9742,73 @@
97429742
- UpdateDirection
97439743
- UpdateTime
97449744
- UpdateStartHue
9745+
device types:
9746+
- AirQualitySensor
9747+
- AirPurifier
9748+
- Aggregator
9749+
- BasicVideoPlayer
9750+
- BridgedNode
9751+
- CastingVideoClient
9752+
- CastingVideoPlayer
9753+
- ColorDimmerSwitch
9754+
- ColorTemperatureLight
9755+
- ContactSensor
9756+
- ContentApp
9757+
- ControlBridge
9758+
- CookSurface
9759+
- Cooktop
9760+
- DeviceEnergyManagement
9761+
- DimmableLight
9762+
- DimmablePlugInUnit
9763+
- DimmerSwitch
9764+
- Dishwasher
9765+
- DoorLock
9766+
- DoorLockController
9767+
- ElectricalSensor
9768+
- EVSE
9769+
- ExtendedColorLight
9770+
- ExtractorHood
9771+
- Fan
9772+
- FlowSensor
9773+
- GenericSwitch
9774+
- HeatingCoolingUnit
9775+
- HumiditySensor
9776+
- LaundryDryer
9777+
- LaundryWasher
9778+
- LightSensor
9779+
- MicrowaveOven
9780+
- ModeSelect
9781+
- NetworkInfrastructureManager
9782+
- OccupancySensor
9783+
- OnOffLight
9784+
- OnOffLightSwitch
9785+
- OnOffPlugInUnit
9786+
- OnOffSensor
9787+
- OTAProvider
9788+
- OTARequestor
9789+
- Oven
9790+
- PressureSensor
9791+
- PowerSource
9792+
- Pump
9793+
- PumpController
9794+
- RainSensor
9795+
- Refrigerator
9796+
- RoboticVacuumCleaner
9797+
- RoomAirConditioner
9798+
- RootNode
9799+
- SecondaryNetworkInterface
9800+
- SmokeCOAlarm
9801+
- Speaker
9802+
- TemperatureControlledCabinet
9803+
- TemperatureSensor
9804+
- Thermostat
9805+
- ThreadBorderRouter
9806+
- VideoRemoteControl
9807+
- WaterFreezeDetector
9808+
- WaterLeakDetector
9809+
- WaterValve
9810+
- WindowCovering
9811+
- WindowCoveringController
97459812
provisional:
97469813
clusters:
97479814
# Targeting 1.4
@@ -9754,8 +9821,11 @@
97549821
- WaterHeaterMode
97559822
- WiFiNetworkManagement
97569823
# Targeting Camera enablement
9824+
- CameraAVStreamManagement
97579825
- Chime
97589826
- WebRTCTransportProvider
9827+
- WebRTCTransportRequestor
9828+
- ZoneManagement
97599829
attributes:
97609830
AccessControl:
97619831
# Targeting 1.4

src/darwin/Framework/CHIP/zap-generated/MTRClusterConstants.h

+71
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)