diff --git a/matter/src/acl.rs b/matter/src/acl.rs index a3553c0..708ddee 100644 --- a/matter/src/acl.rs +++ b/matter/src/acl.rs @@ -21,7 +21,7 @@ use std::{ }; use crate::{ - data_model::objects::{Access, Privilege}, + data_model::objects::{Access, ClusterId, EndptId, Privilege}, error::Error, fabric, interaction_model::messages::GenericPath, @@ -240,13 +240,17 @@ impl<'a> AccessReq<'a> { #[derive(FromTLV, ToTLV, Copy, Clone, Debug, PartialEq)] pub struct Target { - cluster: Option, - endpoint: Option, + cluster: Option, + endpoint: Option, device_type: Option, } impl Target { - pub fn new(endpoint: Option, cluster: Option, device_type: Option) -> Self { + pub fn new( + endpoint: Option, + cluster: Option, + device_type: Option, + ) -> Self { Self { cluster, endpoint, diff --git a/matter/src/data_model/core/mod.rs b/matter/src/data_model/core/mod.rs index 337d0aa..4386cab 100644 --- a/matter/src/data_model/core/mod.rs +++ b/matter/src/data_model/core/mod.rs @@ -234,7 +234,7 @@ enum ResumeReq { } impl objects::ChangeConsumer for DataModel { - fn endpoint_added(&self, id: u16, endpoint: &mut Endpoint) -> Result<(), Error> { + fn endpoint_added(&self, id: EndptId, endpoint: &mut Endpoint) -> Result<(), Error> { endpoint.add_cluster(DescriptorCluster::new(id, self.clone())?)?; Ok(()) } diff --git a/matter/src/data_model/core/read.rs b/matter/src/data_model/core/read.rs index c450073..07eb1a3 100644 --- a/matter/src/data_model/core/read.rs +++ b/matter/src/data_model/core/read.rs @@ -134,9 +134,9 @@ impl ResumeReadReq { impl DataModel { pub fn read_attribute_raw( &self, - endpoint: u16, - cluster: u32, - attr: u16, + endpoint: EndptId, + cluster: ClusterId, + attr: AttrId, ) -> Result { let node = self.node.read().unwrap(); let cluster = node.get_cluster(endpoint, cluster)?; diff --git a/matter/src/data_model/device_types.rs b/matter/src/data_model/device_types.rs index 46d133e..9c37971 100644 --- a/matter/src/data_model/device_types.rs +++ b/matter/src/data_model/device_types.rs @@ -46,7 +46,7 @@ pub fn device_type_add_root_node( fabric_mgr: Arc, acl_mgr: Arc, pase_mgr: PaseMgr, -) -> Result { +) -> Result { // Add the root endpoint let endpoint = node.add_endpoint(DEV_TYPE_ROOT_NODE)?; if endpoint != 0 { @@ -78,7 +78,7 @@ pub const DEV_TYPE_ON_SMART_SPEAKER: DeviceType = DeviceType { drev: 2, }; -pub fn device_type_add_on_off_light(node: &mut WriteNode) -> Result { +pub fn device_type_add_on_off_light(node: &mut WriteNode) -> Result { let endpoint = node.add_endpoint(DEV_TYPE_ON_OFF_LIGHT)?; node.add_cluster(endpoint, OnOffCluster::new()?)?; Ok(endpoint) diff --git a/matter/src/data_model/objects/attribute.rs b/matter/src/data_model/objects/attribute.rs index 419696f..28be5c6 100644 --- a/matter/src/data_model/objects/attribute.rs +++ b/matter/src/data_model/objects/attribute.rs @@ -15,7 +15,7 @@ * limitations under the License. */ -use super::{GlobalElements, Privilege}; +use super::{AttrId, GlobalElements, Privilege}; use crate::{ error::*, // TODO: This layer shouldn't really depend on the TLV layer, should create an abstraction layer @@ -153,7 +153,7 @@ impl AttrValue { #[derive(Debug, Clone)] pub struct Attribute { - pub(super) id: u16, + pub(super) id: AttrId, pub(super) value: AttrValue, pub(super) quality: Quality, pub(super) access: Access, @@ -171,7 +171,7 @@ impl Default for Attribute { } impl Attribute { - pub fn new(id: u16, value: AttrValue, access: Access, quality: Quality) -> Self { + pub fn new(id: AttrId, value: AttrValue, access: Access, quality: Quality) -> Self { Attribute { id, value, @@ -189,8 +189,8 @@ impl Attribute { } } - pub fn is_system_attr(attr_id: u16) -> bool { - attr_id >= (GlobalElements::ServerGenCmd as u16) + pub fn is_system_attr(attr_id: AttrId) -> bool { + attr_id >= (GlobalElements::ServerGenCmd as AttrId) } } diff --git a/matter/src/data_model/objects/cluster.rs b/matter/src/data_model/objects/cluster.rs index 4e02ef7..7ca8350 100644 --- a/matter/src/data_model/objects/cluster.rs +++ b/matter/src/data_model/objects/cluster.rs @@ -28,7 +28,7 @@ use num_derive::FromPrimitive; use rand::Rng; use std::fmt::{self, Debug}; -use super::Encoder; +use super::{AttrId, ClusterId, Encoder}; pub const ATTRS_PER_CLUSTER: usize = 10; pub const CMDS_PER_CLUSTER: usize = 8; @@ -56,7 +56,7 @@ pub struct AttrDetails { /// List Index, if any pub list_index: Option>, /// The actual attribute ID - pub attr_id: u16, + pub attr_id: AttrId, } impl AttrDetails { @@ -102,13 +102,13 @@ pub trait ClusterType { } pub struct Cluster { - pub(super) id: u32, + pub(super) id: ClusterId, attributes: Vec, data_ver: u32, } impl Cluster { - pub fn new(id: u32) -> Result { + pub fn new(id: ClusterId) -> Result { let mut c = Cluster { id, attributes: Vec::with_capacity(ATTRS_PER_CLUSTER), @@ -118,7 +118,7 @@ impl Cluster { Ok(c) } - pub fn id(&self) -> u32 { + pub fn id(&self) -> ClusterId { self.id } @@ -167,18 +167,18 @@ impl Cluster { } } - fn get_attribute_index(&self, attr_id: u16) -> Option { + fn get_attribute_index(&self, attr_id: AttrId) -> Option { self.attributes.iter().position(|c| c.id == attr_id) } - fn get_attribute(&self, attr_id: u16) -> Result<&Attribute, Error> { + fn get_attribute(&self, attr_id: AttrId) -> Result<&Attribute, Error> { let index = self .get_attribute_index(attr_id) .ok_or(Error::AttributeNotFound)?; Ok(&self.attributes[index]) } - fn get_attribute_mut(&mut self, attr_id: u16) -> Result<&mut Attribute, Error> { + fn get_attribute_mut(&mut self, attr_id: AttrId) -> Result<&mut Attribute, Error> { let index = self .get_attribute_index(attr_id) .ok_or(Error::AttributeNotFound)?; @@ -188,7 +188,7 @@ impl Cluster { // Returns a slice of attribute, with either a single attribute or all (wildcard) pub fn get_wildcard_attribute( &self, - attribute: Option, + attribute: Option, ) -> Result<(&[Attribute], bool), IMStatusCode> { if let Some(a) = attribute { if let Some(i) = self.get_attribute_index(a) { @@ -266,7 +266,7 @@ impl Cluster { encoder.encode_status(IMStatusCode::UnsupportedAttribute, 0) } - pub fn read_attribute_raw(&self, attr_id: u16) -> Result<&AttrValue, IMStatusCode> { + pub fn read_attribute_raw(&self, attr_id: AttrId) -> Result<&AttrValue, IMStatusCode> { let a = self .get_attribute(attr_id) .map_err(|_| IMStatusCode::UnsupportedAttribute)?; @@ -300,7 +300,7 @@ impl Cluster { pub fn write_attribute_from_tlv( &mut self, - attr_id: u16, + attr_id: AttrId, data: &TLVElement, ) -> Result<(), IMStatusCode> { let a = self.get_attribute_mut(attr_id)?; @@ -319,7 +319,7 @@ impl Cluster { } } - pub fn write_attribute_raw(&mut self, attr_id: u16, value: AttrValue) -> Result<(), Error> { + pub fn write_attribute_raw(&mut self, attr_id: AttrId, value: AttrValue) -> Result<(), Error> { let a = self.get_attribute_mut(attr_id)?; a.set_value(value).map(|_| { self.cluster_changed(); diff --git a/matter/src/data_model/objects/endpoint.rs b/matter/src/data_model/objects/endpoint.rs index 7ea22dd..466e7a6 100644 --- a/matter/src/data_model/objects/endpoint.rs +++ b/matter/src/data_model/objects/endpoint.rs @@ -19,7 +19,7 @@ use crate::{data_model::objects::ClusterType, error::*, interaction_model::core: use std::fmt; -use super::DeviceType; +use super::{ClusterId, DeviceType}; pub const CLUSTERS_PER_ENDPT: usize = 9; @@ -51,18 +51,21 @@ impl Endpoint { &self.dev_type } - fn get_cluster_index(&self, cluster_id: u32) -> Option { + fn get_cluster_index(&self, cluster_id: ClusterId) -> Option { self.clusters.iter().position(|c| c.base().id == cluster_id) } - pub fn get_cluster(&self, cluster_id: u32) -> Result<&dyn ClusterType, Error> { + pub fn get_cluster(&self, cluster_id: ClusterId) -> Result<&dyn ClusterType, Error> { let index = self .get_cluster_index(cluster_id) .ok_or(Error::ClusterNotFound)?; Ok(self.clusters[index].as_ref()) } - pub fn get_cluster_mut(&mut self, cluster_id: u32) -> Result<&mut dyn ClusterType, Error> { + pub fn get_cluster_mut( + &mut self, + cluster_id: ClusterId, + ) -> Result<&mut dyn ClusterType, Error> { let index = self .get_cluster_index(cluster_id) .ok_or(Error::ClusterNotFound)?; @@ -72,7 +75,7 @@ impl Endpoint { // Returns a slice of clusters, with either a single cluster or all (wildcard) pub fn get_wildcard_clusters( &self, - cluster: Option, + cluster: Option, ) -> Result<(&BoxedClusters, bool), IMStatusCode> { if let Some(c) = cluster { if let Some(i) = self.get_cluster_index(c) { @@ -88,7 +91,7 @@ impl Endpoint { // Returns a slice of clusters, with either a single cluster or all (wildcard) pub fn get_wildcard_clusters_mut( &mut self, - cluster: Option, + cluster: Option, ) -> Result<(&mut BoxedClusters, bool), IMStatusCode> { if let Some(c) = cluster { if let Some(i) = self.get_cluster_index(c) { diff --git a/matter/src/data_model/objects/mod.rs b/matter/src/data_model/objects/mod.rs index 2791cb4..2fb3aff 100644 --- a/matter/src/data_model/objects/mod.rs +++ b/matter/src/data_model/objects/mod.rs @@ -15,6 +15,11 @@ * limitations under the License. */ +pub type EndptId = u16; +pub type ClusterId = u32; +pub type AttrId = u16; +pub type CmdId = u32; + mod attribute; pub use attribute::*; diff --git a/matter/src/data_model/objects/node.rs b/matter/src/data_model/objects/node.rs index 1f2b7cf..ba2f0b2 100644 --- a/matter/src/data_model/objects/node.rs +++ b/matter/src/data_model/objects/node.rs @@ -23,10 +23,10 @@ use crate::{ }; use std::fmt; -use super::DeviceType; +use super::{ClusterId, DeviceType, EndptId}; pub trait ChangeConsumer { - fn endpoint_added(&self, id: u16, endpoint: &mut Endpoint) -> Result<(), Error>; + fn endpoint_added(&self, id: EndptId, endpoint: &mut Endpoint) -> Result<(), Error>; } pub const ENDPTS_PER_ACC: usize = 3; @@ -61,7 +61,7 @@ impl Node { self.changes_cb = Some(consumer); } - pub fn add_endpoint(&mut self, dev_type: DeviceType) -> Result { + pub fn add_endpoint(&mut self, dev_type: DeviceType) -> Result { let index = self .endpoints .iter() @@ -69,13 +69,13 @@ impl Node { .ok_or(Error::NoSpace)?; let mut endpoint = Endpoint::new(dev_type)?; if let Some(cb) = &self.changes_cb { - cb.endpoint_added(index as u16, &mut endpoint)?; + cb.endpoint_added(index as EndptId, &mut endpoint)?; } self.endpoints[index] = Some(endpoint); - Ok(index as u32) + Ok(index as EndptId) } - pub fn get_endpoint(&self, endpoint_id: u16) -> Result<&Endpoint, Error> { + pub fn get_endpoint(&self, endpoint_id: EndptId) -> Result<&Endpoint, Error> { if (endpoint_id as usize) < ENDPTS_PER_ACC { let endpoint = self.endpoints[endpoint_id as usize] .as_ref() @@ -86,7 +86,7 @@ impl Node { } } - pub fn get_endpoint_mut(&mut self, endpoint_id: u16) -> Result<&mut Endpoint, Error> { + pub fn get_endpoint_mut(&mut self, endpoint_id: EndptId) -> Result<&mut Endpoint, Error> { if (endpoint_id as usize) < ENDPTS_PER_ACC { let endpoint = self.endpoints[endpoint_id as usize] .as_mut() @@ -97,17 +97,21 @@ impl Node { } } - pub fn get_cluster_mut(&mut self, e: u16, c: u32) -> Result<&mut dyn ClusterType, Error> { + pub fn get_cluster_mut( + &mut self, + e: EndptId, + c: ClusterId, + ) -> Result<&mut dyn ClusterType, Error> { self.get_endpoint_mut(e)?.get_cluster_mut(c) } - pub fn get_cluster(&self, e: u16, c: u32) -> Result<&dyn ClusterType, Error> { + pub fn get_cluster(&self, e: EndptId, c: ClusterId) -> Result<&dyn ClusterType, Error> { self.get_endpoint(e)?.get_cluster(c) } pub fn add_cluster( &mut self, - endpoint_id: u32, + endpoint_id: EndptId, cluster: Box, ) -> Result<(), Error> { let endpoint_id = endpoint_id as usize; @@ -124,7 +128,7 @@ impl Node { // Returns a slice of endpoints, with either a single endpoint or all (wildcard) pub fn get_wildcard_endpoints( &self, - endpoint: Option, + endpoint: Option, ) -> Result<(&BoxedEndpoints, usize, bool), IMStatusCode> { if let Some(e) = endpoint { let e = e as usize; @@ -140,7 +144,7 @@ impl Node { pub fn get_wildcard_endpoints_mut( &mut self, - endpoint: Option, + endpoint: Option, ) -> Result<(&mut BoxedEndpoints, usize, bool), IMStatusCode> { if let Some(e) = endpoint { let e = e as usize; @@ -171,7 +175,7 @@ impl Node { let (endpoints, mut endpoint_id, wildcard) = self.get_wildcard_endpoints(path.endpoint)?; for e in endpoints.iter() { if let Some(e) = e { - current_path.endpoint = Some(endpoint_id as u16); + current_path.endpoint = Some(endpoint_id as EndptId); f(¤t_path, e.as_ref()) .or_else(|e| if !wildcard { Err(e) } else { Ok(()) })?; } @@ -202,7 +206,7 @@ impl Node { self.get_wildcard_endpoints_mut(path.endpoint)?; for e in endpoints.iter_mut() { if let Some(e) = e { - current_path.endpoint = Some(endpoint_id as u16); + current_path.endpoint = Some(endpoint_id as EndptId); f(¤t_path, e.as_mut()) .or_else(|e| if !wildcard { Err(e) } else { Ok(()) })?; } diff --git a/matter/src/data_model/system_model/descriptor.rs b/matter/src/data_model/system_model/descriptor.rs index 919b586..4fba0fa 100644 --- a/matter/src/data_model/system_model/descriptor.rs +++ b/matter/src/data_model/system_model/descriptor.rs @@ -37,12 +37,12 @@ pub enum Attributes { pub struct DescriptorCluster { base: Cluster, - endpoint_id: u16, + endpoint_id: EndptId, data_model: DataModel, } impl DescriptorCluster { - pub fn new(endpoint_id: u16, data_model: DataModel) -> Result, Error> { + pub fn new(endpoint_id: EndptId, data_model: DataModel) -> Result, Error> { let mut c = Box::new(DescriptorCluster { endpoint_id, data_model, diff --git a/matter/src/interaction_model/messages.rs b/matter/src/interaction_model/messages.rs index d1de33f..aac30f7 100644 --- a/matter/src/interaction_model/messages.rs +++ b/matter/src/interaction_model/messages.rs @@ -16,6 +16,7 @@ */ use crate::{ + data_model::objects::{ClusterId, EndptId}, error::Error, tlv::{FromTLV, TLVElement, TLVWriter, TagType, ToTLV}, }; @@ -25,13 +26,13 @@ use crate::{ #[derive(Default, Clone, Copy, Debug, PartialEq, FromTLV, ToTLV)] #[tlvargs(datatype = "list")] pub struct GenericPath { - pub endpoint: Option, - pub cluster: Option, + pub endpoint: Option, + pub cluster: Option, pub leaf: Option, } impl GenericPath { - pub fn new(endpoint: Option, cluster: Option, leaf: Option) -> Self { + pub fn new(endpoint: Option, cluster: Option, leaf: Option) -> Self { Self { endpoint, cluster, @@ -40,7 +41,7 @@ impl GenericPath { } /// Returns Ok, if the path is non wildcard, otherwise returns an error - pub fn not_wildcard(&self) -> Result<(u16, u32, u32), Error> { + pub fn not_wildcard(&self) -> Result<(EndptId, ClusterId, u32), Error> { match *self { GenericPath { endpoint: Some(e), @@ -257,7 +258,7 @@ pub mod ib { use std::fmt::Debug; use crate::{ - data_model::objects::{AttrDetails, EncodeValue}, + data_model::objects::{AttrDetails, AttrId, ClusterId, EncodeValue, EndptId}, error::Error, interaction_model::core::IMStatusCode, tlv::{FromTLV, Nullable, TLVElement, TLVWriter, TagType, ToTLV}, @@ -275,7 +276,12 @@ pub mod ib { } impl<'a> InvResp<'a> { - pub fn cmd_new(endpoint: u16, cluster: u32, cmd: u16, data: EncodeValue<'a>) -> Self { + pub fn cmd_new( + endpoint: EndptId, + cluster: ClusterId, + cmd: u16, + data: EncodeValue<'a>, + ) -> Self { Self::Cmd(CmdData::new( CmdPath::new(Some(endpoint), Some(cluster), Some(cmd)), data, @@ -448,9 +454,9 @@ pub mod ib { pub struct AttrPath { pub tag_compression: Option, pub node: Option, - pub endpoint: Option, - pub cluster: Option, - pub attr: Option, + pub endpoint: Option, + pub cluster: Option, + pub attr: Option, pub list_index: Option>, } @@ -490,7 +496,11 @@ pub mod ib { } impl CmdPath { - pub fn new(endpoint: Option, cluster: Option, command: Option) -> Self { + pub fn new( + endpoint: Option, + cluster: Option, + command: Option, + ) -> Self { Self { path: GenericPath { endpoint, @@ -525,8 +535,8 @@ pub mod ib { #[derive(FromTLV, ToTLV, Copy, Clone)] pub struct ClusterPath { pub node: Option, - pub endpoint: u16, - pub cluster: u32, + pub endpoint: EndptId, + pub cluster: ClusterId, } #[derive(FromTLV, ToTLV, Copy, Clone)] @@ -539,8 +549,8 @@ pub mod ib { #[tlvargs(datatype = "list")] pub struct EventPath { pub node: Option, - pub endpoint: Option, - pub cluster: Option, + pub endpoint: Option, + pub cluster: Option, pub event: Option, pub is_urgent: Option, }