Skip to content

Commit

Permalink
Add a way to ask Matter.framework for information about device types. (
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple authored Oct 15, 2024
1 parent 064c205 commit 579b1b1
Show file tree
Hide file tree
Showing 11 changed files with 442 additions and 100 deletions.
55 changes: 55 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#import <Foundation/Foundation.h>

#import <Matter/MTRDefines.h>

NS_ASSUME_NONNULL_BEGIN

MTR_NEWLY_AVAILABLE
@interface MTRDeviceType : NSObject

/**
* Returns an MTRDeviceType for the given ID, if the ID is known. Returns nil
* for unknown IDs.
*/
+ (nullable MTRDeviceType *)deviceTypeForID:(NSNumber *)deviceTypeID;

/**
* The identifier of the device type (32-bit unsigned integer).
*/
@property (nonatomic, readonly, copy) NSNumber * id;

/**
* Returns the name of the device type.
*/
@property (nonatomic, readonly, retain) NSString * name;

/**
* Returns whether this is a utility device type.
*/
@property (nonatomic, readonly, assign) BOOL isUtility;

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;

@end

NS_ASSUME_NONNULL_END
59 changes: 59 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceType.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <Matter/MTRDeviceType.h>

#import "MTRDeviceTypeMetadata.h"
#import "MTRLogging_Internal.h"

#include <lib/support/SafeInt.h>

using namespace chip;

@implementation MTRDeviceType

- (nullable instancetype)initWithDeviceTypeID:(NSNumber *)id name:(NSString *)name isUtility:(BOOL)isUtility
{
if (!(self = [super init])) {
return nil;
}

_id = id;
_name = name;
_isUtility = isUtility;
return self;
}

+ (nullable MTRDeviceType *)deviceTypeForID:(NSNumber *)deviceTypeID
{
if (!CanCastTo<DeviceTypeId>(deviceTypeID.unsignedLongLongValue)) {
MTR_LOG_ERROR("Invalid device type ID: 0x%llx", deviceTypeID.unsignedLongLongValue);
return nil;
}

auto * deviceTypeData = MTRDeviceTypeDataForID(static_cast<DeviceTypeId>(deviceTypeID.unsignedLongLongValue));
if (!deviceTypeData) {
return nil;
}

return [[MTRDeviceType alloc]
initWithDeviceTypeID:deviceTypeID
name:[NSString stringWithUTF8String:deviceTypeData->name]
isUtility:(deviceTypeData->deviceClass != MTRDeviceTypeClass::Simple)];
}

@end
21 changes: 20 additions & 1 deletion src/darwin/Framework/CHIP/MTRDeviceTypeMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,23 @@

#include <lib/core/DataModelTypes.h>

BOOL MTRIsKnownUtilityDeviceType(chip::DeviceTypeId aDeviceTypeId);
NS_ASSUME_NONNULL_BEGIN

enum class MTRDeviceTypeClass
{
Utility,
Simple,
Node, // Might not be a real class, but we have it for Root Node for now.
};

struct MTRDeviceTypeData
{
chip::DeviceTypeId id;
MTRDeviceTypeClass deviceClass;
const char * name;
};

// Returns null for unknown device types.
const MTRDeviceTypeData * _Nullable MTRDeviceTypeDataForID(chip::DeviceTypeId aDeviceTypeId);

NS_ASSUME_NONNULL_END
1 change: 1 addition & 0 deletions src/darwin/Framework/CHIP/Matter.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#import <Matter/MTRDeviceControllerStartupParams.h>
#import <Matter/MTRDeviceControllerStorageDelegate.h>
#import <Matter/MTRDeviceStorageBehaviorConfiguration.h>
#import <Matter/MTRDeviceType.h>
#import <Matter/MTRDeviceTypeRevision.h>
#import <Matter/MTRDiagnosticLogsType.h>
#import <Matter/MTRError.h>
Expand Down
12 changes: 12 additions & 0 deletions src/darwin/Framework/CHIP/templates/MTRClusterConstants.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,15 @@ MTREventIDTypeCluster{{>cluster}}Event{{>event}}ID {{availability (asUpperCamelC
{{> eventIDs clusterName=label}}
{{/zcl_clusters}}
};

#pragma mark - Device Type IDs

typedef NS_ENUM(uint32_t, MTRDeviceTypeIDType) {
{{#zcl_device_types}}
{{! Only include standard device types for now. The template syntax does not get hex numbers, apparently.
ZAP seems to have no is_number_less_than helper, so do is_number_greater_than with the arguments reversed. }}
{{#if (is_number_greater_than 65536 code)}}
MTRDeviceTypeIDType{{asUpperCamelCase caption preserveAcronyms=true}}ID {{availability "" deviceType=(asUpperCamelCase caption preserveAcronyms=true)}} = {{asHex code 8}},
{{/if}}
{{/zcl_device_types}}
} MTR_NEWLY_AVAILABLE;
22 changes: 5 additions & 17 deletions src/darwin/Framework/CHIP/templates/MTRDeviceTypeMetadata-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,11 @@
using namespace chip;

namespace {
enum class DeviceTypeClass {
Utility,
Simple,
Node, // Might not be a real class, but we have it for Root Node for now.
// If new classes get added, plase audit MTRIsKnownUtilityDeviceType below.
};

struct DeviceTypeData {
DeviceTypeId id;
DeviceTypeClass deviceClass;
const char * name;
};

constexpr DeviceTypeData knownDeviceTypes[] = {
constexpr MTRDeviceTypeData knownDeviceTypes[] = {
{{#zcl_device_types}}
{{#if class}}
{ {{asHex code 8}}, DeviceTypeClass::{{class}}, "{{caption}}" },
{ {{asHex code 8}}, MTRDeviceTypeClass::{{class}}, "{{caption}}" },
{{/if}}
{{/zcl_device_types}}
};
Expand All @@ -34,12 +22,12 @@ static_assert(ExtractVendorFromMEI({{asHex code 8}}) != 0, "Must have class defi

} // anonymous namespace

BOOL MTRIsKnownUtilityDeviceType(DeviceTypeId aDeviceTypeId)
const MTRDeviceTypeData * _Nullable MTRDeviceTypeDataForID(chip::DeviceTypeId aDeviceTypeId)
{
for (auto & deviceType : knownDeviceTypes) {
if (deviceType.id == aDeviceTypeId) {
return deviceType.deviceClass != DeviceTypeClass::Simple;
return &deviceType;
}
}
return NO;
return nullptr;
}
70 changes: 70 additions & 0 deletions src/darwin/Framework/CHIP/templates/availability.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9742,6 +9742,73 @@
- UpdateDirection
- UpdateTime
- UpdateStartHue
device types:
- AirQualitySensor
- AirPurifier
- Aggregator
- BasicVideoPlayer
- BridgedNode
- CastingVideoClient
- CastingVideoPlayer
- ColorDimmerSwitch
- ColorTemperatureLight
- ContactSensor
- ContentApp
- ControlBridge
- CookSurface
- Cooktop
- DeviceEnergyManagement
- DimmableLight
- DimmablePlugInUnit
- DimmerSwitch
- Dishwasher
- DoorLock
- DoorLockController
- ElectricalSensor
- EVSE
- ExtendedColorLight
- ExtractorHood
- Fan
- FlowSensor
- GenericSwitch
- HeatingCoolingUnit
- HumiditySensor
- LaundryDryer
- LaundryWasher
- LightSensor
- MicrowaveOven
- ModeSelect
- NetworkInfrastructureManager
- OccupancySensor
- OnOffLight
- OnOffLightSwitch
- OnOffPlugInUnit
- OnOffSensor
- OTAProvider
- OTARequestor
- Oven
- PressureSensor
- PowerSource
- Pump
- PumpController
- RainSensor
- Refrigerator
- RoboticVacuumCleaner
- RoomAirConditioner
- RootNode
- SecondaryNetworkInterface
- SmokeCOAlarm
- Speaker
- TemperatureControlledCabinet
- TemperatureSensor
- Thermostat
- ThreadBorderRouter
- VideoRemoteControl
- WaterFreezeDetector
- WaterLeakDetector
- WaterValve
- WindowCovering
- WindowCoveringController
provisional:
clusters:
# Targeting 1.4
Expand All @@ -9754,8 +9821,11 @@
- WaterHeaterMode
- WiFiNetworkManagement
# Targeting Camera enablement
- CameraAVStreamManagement
- Chime
- WebRTCTransportProvider
- WebRTCTransportRequestor
- ZoneManagement
attributes:
AccessControl:
# Targeting 1.4
Expand Down
71 changes: 71 additions & 0 deletions src/darwin/Framework/CHIP/zap-generated/MTRClusterConstants.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 579b1b1

Please sign in to comment.