Skip to content

Commit 3b091c2

Browse files
authored
Merge pull request project-chip#51 from kedars/feature/types_for_id
IM: Use Types for IDs
2 parents 5b2af5f + cef9919 commit 3b091c2

File tree

11 files changed

+89
-63
lines changed

11 files changed

+89
-63
lines changed

matter/src/acl.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::{
2121
};
2222

2323
use crate::{
24-
data_model::objects::{Access, Privilege},
24+
data_model::objects::{Access, ClusterId, EndptId, Privilege},
2525
error::Error,
2626
fabric,
2727
interaction_model::messages::GenericPath,
@@ -240,13 +240,17 @@ impl<'a> AccessReq<'a> {
240240

241241
#[derive(FromTLV, ToTLV, Copy, Clone, Debug, PartialEq)]
242242
pub struct Target {
243-
cluster: Option<u32>,
244-
endpoint: Option<u16>,
243+
cluster: Option<ClusterId>,
244+
endpoint: Option<EndptId>,
245245
device_type: Option<u32>,
246246
}
247247

248248
impl Target {
249-
pub fn new(endpoint: Option<u16>, cluster: Option<u32>, device_type: Option<u32>) -> Self {
249+
pub fn new(
250+
endpoint: Option<EndptId>,
251+
cluster: Option<ClusterId>,
252+
device_type: Option<u32>,
253+
) -> Self {
250254
Self {
251255
cluster,
252256
endpoint,

matter/src/data_model/core/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ enum ResumeReq {
234234
}
235235

236236
impl objects::ChangeConsumer for DataModel {
237-
fn endpoint_added(&self, id: u16, endpoint: &mut Endpoint) -> Result<(), Error> {
237+
fn endpoint_added(&self, id: EndptId, endpoint: &mut Endpoint) -> Result<(), Error> {
238238
endpoint.add_cluster(DescriptorCluster::new(id, self.clone())?)?;
239239
Ok(())
240240
}

matter/src/data_model/core/read.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ impl ResumeReadReq {
134134
impl DataModel {
135135
pub fn read_attribute_raw(
136136
&self,
137-
endpoint: u16,
138-
cluster: u32,
139-
attr: u16,
137+
endpoint: EndptId,
138+
cluster: ClusterId,
139+
attr: AttrId,
140140
) -> Result<AttrValue, IMStatusCode> {
141141
let node = self.node.read().unwrap();
142142
let cluster = node.get_cluster(endpoint, cluster)?;

matter/src/data_model/device_types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub fn device_type_add_root_node(
4646
fabric_mgr: Arc<FabricMgr>,
4747
acl_mgr: Arc<AclMgr>,
4848
pase_mgr: PaseMgr,
49-
) -> Result<u32, Error> {
49+
) -> Result<EndptId, Error> {
5050
// Add the root endpoint
5151
let endpoint = node.add_endpoint(DEV_TYPE_ROOT_NODE)?;
5252
if endpoint != 0 {
@@ -78,7 +78,7 @@ pub const DEV_TYPE_ON_SMART_SPEAKER: DeviceType = DeviceType {
7878
drev: 2,
7979
};
8080

81-
pub fn device_type_add_on_off_light(node: &mut WriteNode) -> Result<u32, Error> {
81+
pub fn device_type_add_on_off_light(node: &mut WriteNode) -> Result<EndptId, Error> {
8282
let endpoint = node.add_endpoint(DEV_TYPE_ON_OFF_LIGHT)?;
8383
node.add_cluster(endpoint, OnOffCluster::new()?)?;
8484
Ok(endpoint)

matter/src/data_model/objects/attribute.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
use super::{GlobalElements, Privilege};
18+
use super::{AttrId, GlobalElements, Privilege};
1919
use crate::{
2020
error::*,
2121
// TODO: This layer shouldn't really depend on the TLV layer, should create an abstraction layer
@@ -153,7 +153,7 @@ impl AttrValue {
153153

154154
#[derive(Debug, Clone)]
155155
pub struct Attribute {
156-
pub(super) id: u16,
156+
pub(super) id: AttrId,
157157
pub(super) value: AttrValue,
158158
pub(super) quality: Quality,
159159
pub(super) access: Access,
@@ -171,7 +171,7 @@ impl Default for Attribute {
171171
}
172172

173173
impl Attribute {
174-
pub fn new(id: u16, value: AttrValue, access: Access, quality: Quality) -> Self {
174+
pub fn new(id: AttrId, value: AttrValue, access: Access, quality: Quality) -> Self {
175175
Attribute {
176176
id,
177177
value,
@@ -189,8 +189,8 @@ impl Attribute {
189189
}
190190
}
191191

192-
pub fn is_system_attr(attr_id: u16) -> bool {
193-
attr_id >= (GlobalElements::ServerGenCmd as u16)
192+
pub fn is_system_attr(attr_id: AttrId) -> bool {
193+
attr_id >= (GlobalElements::ServerGenCmd as AttrId)
194194
}
195195
}
196196

matter/src/data_model/objects/cluster.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use num_derive::FromPrimitive;
2828
use rand::Rng;
2929
use std::fmt::{self, Debug};
3030

31-
use super::Encoder;
31+
use super::{AttrId, ClusterId, Encoder};
3232

3333
pub const ATTRS_PER_CLUSTER: usize = 10;
3434
pub const CMDS_PER_CLUSTER: usize = 8;
@@ -56,7 +56,7 @@ pub struct AttrDetails {
5656
/// List Index, if any
5757
pub list_index: Option<Nullable<u16>>,
5858
/// The actual attribute ID
59-
pub attr_id: u16,
59+
pub attr_id: AttrId,
6060
}
6161

6262
impl AttrDetails {
@@ -102,13 +102,13 @@ pub trait ClusterType {
102102
}
103103

104104
pub struct Cluster {
105-
pub(super) id: u32,
105+
pub(super) id: ClusterId,
106106
attributes: Vec<Attribute>,
107107
data_ver: u32,
108108
}
109109

110110
impl Cluster {
111-
pub fn new(id: u32) -> Result<Cluster, Error> {
111+
pub fn new(id: ClusterId) -> Result<Cluster, Error> {
112112
let mut c = Cluster {
113113
id,
114114
attributes: Vec::with_capacity(ATTRS_PER_CLUSTER),
@@ -118,7 +118,7 @@ impl Cluster {
118118
Ok(c)
119119
}
120120

121-
pub fn id(&self) -> u32 {
121+
pub fn id(&self) -> ClusterId {
122122
self.id
123123
}
124124

@@ -167,18 +167,18 @@ impl Cluster {
167167
}
168168
}
169169

170-
fn get_attribute_index(&self, attr_id: u16) -> Option<usize> {
170+
fn get_attribute_index(&self, attr_id: AttrId) -> Option<usize> {
171171
self.attributes.iter().position(|c| c.id == attr_id)
172172
}
173173

174-
fn get_attribute(&self, attr_id: u16) -> Result<&Attribute, Error> {
174+
fn get_attribute(&self, attr_id: AttrId) -> Result<&Attribute, Error> {
175175
let index = self
176176
.get_attribute_index(attr_id)
177177
.ok_or(Error::AttributeNotFound)?;
178178
Ok(&self.attributes[index])
179179
}
180180

181-
fn get_attribute_mut(&mut self, attr_id: u16) -> Result<&mut Attribute, Error> {
181+
fn get_attribute_mut(&mut self, attr_id: AttrId) -> Result<&mut Attribute, Error> {
182182
let index = self
183183
.get_attribute_index(attr_id)
184184
.ok_or(Error::AttributeNotFound)?;
@@ -188,7 +188,7 @@ impl Cluster {
188188
// Returns a slice of attribute, with either a single attribute or all (wildcard)
189189
pub fn get_wildcard_attribute(
190190
&self,
191-
attribute: Option<u16>,
191+
attribute: Option<AttrId>,
192192
) -> Result<(&[Attribute], bool), IMStatusCode> {
193193
if let Some(a) = attribute {
194194
if let Some(i) = self.get_attribute_index(a) {
@@ -266,7 +266,7 @@ impl Cluster {
266266
encoder.encode_status(IMStatusCode::UnsupportedAttribute, 0)
267267
}
268268

269-
pub fn read_attribute_raw(&self, attr_id: u16) -> Result<&AttrValue, IMStatusCode> {
269+
pub fn read_attribute_raw(&self, attr_id: AttrId) -> Result<&AttrValue, IMStatusCode> {
270270
let a = self
271271
.get_attribute(attr_id)
272272
.map_err(|_| IMStatusCode::UnsupportedAttribute)?;
@@ -300,7 +300,7 @@ impl Cluster {
300300

301301
pub fn write_attribute_from_tlv(
302302
&mut self,
303-
attr_id: u16,
303+
attr_id: AttrId,
304304
data: &TLVElement,
305305
) -> Result<(), IMStatusCode> {
306306
let a = self.get_attribute_mut(attr_id)?;
@@ -319,7 +319,7 @@ impl Cluster {
319319
}
320320
}
321321

322-
pub fn write_attribute_raw(&mut self, attr_id: u16, value: AttrValue) -> Result<(), Error> {
322+
pub fn write_attribute_raw(&mut self, attr_id: AttrId, value: AttrValue) -> Result<(), Error> {
323323
let a = self.get_attribute_mut(attr_id)?;
324324
a.set_value(value).map(|_| {
325325
self.cluster_changed();

matter/src/data_model/objects/endpoint.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{data_model::objects::ClusterType, error::*, interaction_model::core:
1919

2020
use std::fmt;
2121

22-
use super::DeviceType;
22+
use super::{ClusterId, DeviceType};
2323

2424
pub const CLUSTERS_PER_ENDPT: usize = 9;
2525

@@ -51,18 +51,21 @@ impl Endpoint {
5151
&self.dev_type
5252
}
5353

54-
fn get_cluster_index(&self, cluster_id: u32) -> Option<usize> {
54+
fn get_cluster_index(&self, cluster_id: ClusterId) -> Option<usize> {
5555
self.clusters.iter().position(|c| c.base().id == cluster_id)
5656
}
5757

58-
pub fn get_cluster(&self, cluster_id: u32) -> Result<&dyn ClusterType, Error> {
58+
pub fn get_cluster(&self, cluster_id: ClusterId) -> Result<&dyn ClusterType, Error> {
5959
let index = self
6060
.get_cluster_index(cluster_id)
6161
.ok_or(Error::ClusterNotFound)?;
6262
Ok(self.clusters[index].as_ref())
6363
}
6464

65-
pub fn get_cluster_mut(&mut self, cluster_id: u32) -> Result<&mut dyn ClusterType, Error> {
65+
pub fn get_cluster_mut(
66+
&mut self,
67+
cluster_id: ClusterId,
68+
) -> Result<&mut dyn ClusterType, Error> {
6669
let index = self
6770
.get_cluster_index(cluster_id)
6871
.ok_or(Error::ClusterNotFound)?;
@@ -72,7 +75,7 @@ impl Endpoint {
7275
// Returns a slice of clusters, with either a single cluster or all (wildcard)
7376
pub fn get_wildcard_clusters(
7477
&self,
75-
cluster: Option<u32>,
78+
cluster: Option<ClusterId>,
7679
) -> Result<(&BoxedClusters, bool), IMStatusCode> {
7780
if let Some(c) = cluster {
7881
if let Some(i) = self.get_cluster_index(c) {
@@ -88,7 +91,7 @@ impl Endpoint {
8891
// Returns a slice of clusters, with either a single cluster or all (wildcard)
8992
pub fn get_wildcard_clusters_mut(
9093
&mut self,
91-
cluster: Option<u32>,
94+
cluster: Option<ClusterId>,
9295
) -> Result<(&mut BoxedClusters, bool), IMStatusCode> {
9396
if let Some(c) = cluster {
9497
if let Some(i) = self.get_cluster_index(c) {

matter/src/data_model/objects/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
* limitations under the License.
1616
*/
1717

18+
pub type EndptId = u16;
19+
pub type ClusterId = u32;
20+
pub type AttrId = u16;
21+
pub type CmdId = u32;
22+
1823
mod attribute;
1924
pub use attribute::*;
2025

matter/src/data_model/objects/node.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ use crate::{
2323
};
2424
use std::fmt;
2525

26-
use super::DeviceType;
26+
use super::{ClusterId, DeviceType, EndptId};
2727

2828
pub trait ChangeConsumer {
29-
fn endpoint_added(&self, id: u16, endpoint: &mut Endpoint) -> Result<(), Error>;
29+
fn endpoint_added(&self, id: EndptId, endpoint: &mut Endpoint) -> Result<(), Error>;
3030
}
3131

3232
pub const ENDPTS_PER_ACC: usize = 3;
@@ -61,21 +61,21 @@ impl Node {
6161
self.changes_cb = Some(consumer);
6262
}
6363

64-
pub fn add_endpoint(&mut self, dev_type: DeviceType) -> Result<u32, Error> {
64+
pub fn add_endpoint(&mut self, dev_type: DeviceType) -> Result<EndptId, Error> {
6565
let index = self
6666
.endpoints
6767
.iter()
6868
.position(|x| x.is_none())
6969
.ok_or(Error::NoSpace)?;
7070
let mut endpoint = Endpoint::new(dev_type)?;
7171
if let Some(cb) = &self.changes_cb {
72-
cb.endpoint_added(index as u16, &mut endpoint)?;
72+
cb.endpoint_added(index as EndptId, &mut endpoint)?;
7373
}
7474
self.endpoints[index] = Some(endpoint);
75-
Ok(index as u32)
75+
Ok(index as EndptId)
7676
}
7777

78-
pub fn get_endpoint(&self, endpoint_id: u16) -> Result<&Endpoint, Error> {
78+
pub fn get_endpoint(&self, endpoint_id: EndptId) -> Result<&Endpoint, Error> {
7979
if (endpoint_id as usize) < ENDPTS_PER_ACC {
8080
let endpoint = self.endpoints[endpoint_id as usize]
8181
.as_ref()
@@ -86,7 +86,7 @@ impl Node {
8686
}
8787
}
8888

89-
pub fn get_endpoint_mut(&mut self, endpoint_id: u16) -> Result<&mut Endpoint, Error> {
89+
pub fn get_endpoint_mut(&mut self, endpoint_id: EndptId) -> Result<&mut Endpoint, Error> {
9090
if (endpoint_id as usize) < ENDPTS_PER_ACC {
9191
let endpoint = self.endpoints[endpoint_id as usize]
9292
.as_mut()
@@ -97,17 +97,21 @@ impl Node {
9797
}
9898
}
9999

100-
pub fn get_cluster_mut(&mut self, e: u16, c: u32) -> Result<&mut dyn ClusterType, Error> {
100+
pub fn get_cluster_mut(
101+
&mut self,
102+
e: EndptId,
103+
c: ClusterId,
104+
) -> Result<&mut dyn ClusterType, Error> {
101105
self.get_endpoint_mut(e)?.get_cluster_mut(c)
102106
}
103107

104-
pub fn get_cluster(&self, e: u16, c: u32) -> Result<&dyn ClusterType, Error> {
108+
pub fn get_cluster(&self, e: EndptId, c: ClusterId) -> Result<&dyn ClusterType, Error> {
105109
self.get_endpoint(e)?.get_cluster(c)
106110
}
107111

108112
pub fn add_cluster(
109113
&mut self,
110-
endpoint_id: u32,
114+
endpoint_id: EndptId,
111115
cluster: Box<dyn ClusterType>,
112116
) -> Result<(), Error> {
113117
let endpoint_id = endpoint_id as usize;
@@ -124,7 +128,7 @@ impl Node {
124128
// Returns a slice of endpoints, with either a single endpoint or all (wildcard)
125129
pub fn get_wildcard_endpoints(
126130
&self,
127-
endpoint: Option<u16>,
131+
endpoint: Option<EndptId>,
128132
) -> Result<(&BoxedEndpoints, usize, bool), IMStatusCode> {
129133
if let Some(e) = endpoint {
130134
let e = e as usize;
@@ -140,7 +144,7 @@ impl Node {
140144

141145
pub fn get_wildcard_endpoints_mut(
142146
&mut self,
143-
endpoint: Option<u16>,
147+
endpoint: Option<EndptId>,
144148
) -> Result<(&mut BoxedEndpoints, usize, bool), IMStatusCode> {
145149
if let Some(e) = endpoint {
146150
let e = e as usize;
@@ -171,7 +175,7 @@ impl Node {
171175
let (endpoints, mut endpoint_id, wildcard) = self.get_wildcard_endpoints(path.endpoint)?;
172176
for e in endpoints.iter() {
173177
if let Some(e) = e {
174-
current_path.endpoint = Some(endpoint_id as u16);
178+
current_path.endpoint = Some(endpoint_id as EndptId);
175179
f(&current_path, e.as_ref())
176180
.or_else(|e| if !wildcard { Err(e) } else { Ok(()) })?;
177181
}
@@ -202,7 +206,7 @@ impl Node {
202206
self.get_wildcard_endpoints_mut(path.endpoint)?;
203207
for e in endpoints.iter_mut() {
204208
if let Some(e) = e {
205-
current_path.endpoint = Some(endpoint_id as u16);
209+
current_path.endpoint = Some(endpoint_id as EndptId);
206210
f(&current_path, e.as_mut())
207211
.or_else(|e| if !wildcard { Err(e) } else { Ok(()) })?;
208212
}

matter/src/data_model/system_model/descriptor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ pub enum Attributes {
3737

3838
pub struct DescriptorCluster {
3939
base: Cluster,
40-
endpoint_id: u16,
40+
endpoint_id: EndptId,
4141
data_model: DataModel,
4242
}
4343

4444
impl DescriptorCluster {
45-
pub fn new(endpoint_id: u16, data_model: DataModel) -> Result<Box<Self>, Error> {
45+
pub fn new(endpoint_id: EndptId, data_model: DataModel) -> Result<Box<Self>, Error> {
4646
let mut c = Box::new(DescriptorCluster {
4747
endpoint_id,
4848
data_model,

0 commit comments

Comments
 (0)