Skip to content

Commit 690e396

Browse files
authored
Merge pull request #13 from kedars/feature/data-model-updates
Support more common cluster attributes
2 parents 1a0a418 + 6fa4554 commit 690e396

File tree

9 files changed

+127
-113
lines changed

9 files changed

+127
-113
lines changed

examples/onoff_light/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ fn main() {
3535
pid: 0x8002,
3636
hw_ver: 2,
3737
sw_ver: 1,
38+
sw_ver_str: "1".to_string(),
3839
serial_no: "aabbccdd".to_string(),
3940
device_name: "OnOff Light".to_string(),
4041
};

matter/src/data_model/cluster_basic_information.rs

Lines changed: 49 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum Attributes {
2828
ProductId = 4,
2929
HwVer = 7,
3030
SwVer = 9,
31+
SwVerString = 0xa,
3132
SerialNo = 0x0f,
3233
}
3334

@@ -37,64 +38,12 @@ pub struct BasicInfoConfig {
3738
pub pid: u16,
3839
pub hw_ver: u16,
3940
pub sw_ver: u32,
41+
pub sw_ver_str: String,
4042
pub serial_no: String,
4143
/// Device name; up to 32 characters
4244
pub device_name: String,
4345
}
4446

45-
fn attr_dm_rev_new() -> Result<Attribute, Error> {
46-
Attribute::new(
47-
Attributes::DMRevision as u16,
48-
AttrValue::Uint8(1),
49-
Access::RV,
50-
Quality::FIXED,
51-
)
52-
}
53-
54-
fn attr_vid_new(vid: u16) -> Result<Attribute, Error> {
55-
Attribute::new(
56-
Attributes::VendorId as u16,
57-
AttrValue::Uint16(vid),
58-
Access::RV,
59-
Quality::FIXED,
60-
)
61-
}
62-
63-
fn attr_pid_new(pid: u16) -> Result<Attribute, Error> {
64-
Attribute::new(
65-
Attributes::ProductId as u16,
66-
AttrValue::Uint16(pid),
67-
Access::RV,
68-
Quality::FIXED,
69-
)
70-
}
71-
72-
fn attr_hw_ver_new(hw_ver: u16) -> Result<Attribute, Error> {
73-
Attribute::new(
74-
Attributes::HwVer as u16,
75-
AttrValue::Uint16(hw_ver),
76-
Access::RV,
77-
Quality::FIXED,
78-
)
79-
}
80-
81-
fn attr_sw_ver_new(sw_ver: u32) -> Result<Attribute, Error> {
82-
Attribute::new(
83-
Attributes::SwVer as u16,
84-
AttrValue::Uint32(sw_ver),
85-
Access::RV,
86-
Quality::FIXED,
87-
)
88-
}
89-
90-
fn attr_serial_no_new(label: String) -> Result<Attribute, Error> {
91-
Attribute::new(
92-
Attributes::SerialNo as u16,
93-
AttrValue::Utf8(label),
94-
Access::RV,
95-
Quality::FIXED,
96-
)
97-
}
9847
pub struct BasicInfoCluster {
9948
base: Cluster,
10049
}
@@ -104,14 +53,53 @@ impl BasicInfoCluster {
10453
let mut cluster = Box::new(BasicInfoCluster {
10554
base: Cluster::new(ID)?,
10655
});
107-
cluster.base.add_attribute(attr_dm_rev_new()?)?;
108-
cluster.base.add_attribute(attr_vid_new(cfg.vid)?)?;
109-
cluster.base.add_attribute(attr_pid_new(cfg.pid)?)?;
110-
cluster.base.add_attribute(attr_hw_ver_new(cfg.hw_ver)?)?;
111-
cluster.base.add_attribute(attr_sw_ver_new(cfg.sw_ver)?)?;
112-
cluster
113-
.base
114-
.add_attribute(attr_serial_no_new(cfg.serial_no)?)?;
56+
57+
let attrs = [
58+
Attribute::new(
59+
Attributes::DMRevision as u16,
60+
AttrValue::Uint8(1),
61+
Access::RV,
62+
Quality::FIXED,
63+
)?,
64+
Attribute::new(
65+
Attributes::VendorId as u16,
66+
AttrValue::Uint16(cfg.vid),
67+
Access::RV,
68+
Quality::FIXED,
69+
)?,
70+
Attribute::new(
71+
Attributes::ProductId as u16,
72+
AttrValue::Uint16(cfg.pid),
73+
Access::RV,
74+
Quality::FIXED,
75+
)?,
76+
Attribute::new(
77+
Attributes::HwVer as u16,
78+
AttrValue::Uint16(cfg.hw_ver),
79+
Access::RV,
80+
Quality::FIXED,
81+
)?,
82+
Attribute::new(
83+
Attributes::SwVer as u16,
84+
AttrValue::Uint32(cfg.sw_ver),
85+
Access::RV,
86+
Quality::FIXED,
87+
)?,
88+
Attribute::new(
89+
Attributes::SwVerString as u16,
90+
AttrValue::Utf8(cfg.sw_ver_str),
91+
Access::RV,
92+
Quality::FIXED,
93+
)?,
94+
Attribute::new(
95+
Attributes::SerialNo as u16,
96+
AttrValue::Utf8(cfg.serial_no),
97+
Access::RV,
98+
Quality::FIXED,
99+
)?,
100+
];
101+
cluster.base.add_attributes(&attrs[..])?;
102+
115103
Ok(cluster)
116104
}
117105
}

matter/src/data_model/objects/attribute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl AttrValue {
151151
}
152152
}
153153

154-
#[derive(Debug)]
154+
#[derive(Debug, Clone)]
155155
pub struct Attribute {
156156
pub(super) id: u16,
157157
pub(super) value: AttrValue,

matter/src/data_model/objects/cluster.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use std::fmt::{self, Debug};
3030

3131
use super::Encoder;
3232

33-
pub const ATTRS_PER_CLUSTER: usize = 8;
33+
pub const ATTRS_PER_CLUSTER: usize = 10;
3434
pub const CMDS_PER_CLUSTER: usize = 8;
3535

3636
#[derive(FromPrimitive, Debug)]
@@ -132,6 +132,15 @@ impl Cluster {
132132
)?)
133133
}
134134

135+
pub fn add_attributes(&mut self, attrs: &[Attribute]) -> Result<(), Error> {
136+
if self.attributes.len() + attrs.len() <= self.attributes.capacity() {
137+
self.attributes.extend_from_slice(attrs);
138+
Ok(())
139+
} else {
140+
Err(Error::NoSpace)
141+
}
142+
}
143+
135144
pub fn add_attribute(&mut self, attr: Attribute) -> Result<(), Error> {
136145
if self.attributes.len() < self.attributes.capacity() {
137146
self.attributes.push(attr);

matter/src/data_model/sdm/noc.rs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::cert::Cert;
2323
use crate::crypto::{self, CryptoKeyPair, KeyPair};
2424
use crate::data_model::objects::*;
2525
use crate::data_model::sdm::dev_att;
26-
use crate::fabric::{Fabric, FabricMgr};
26+
use crate::fabric::{Fabric, FabricMgr, MAX_SUPPORTED_FABRICS};
2727
use crate::interaction_model::command::CommandReq;
2828
use crate::interaction_model::core::IMStatusCode;
2929
use crate::interaction_model::messages::ib;
@@ -125,8 +125,33 @@ impl NocCluster {
125125
failsafe,
126126
base: Cluster::new(ID)?,
127127
});
128-
c.base.add_attribute(attr_currfabindex_new()?)?;
129-
c.base.add_attribute(attr_fabrics_new()?)?;
128+
let attrs = [
129+
Attribute::new(
130+
Attributes::CurrentFabricIndex as u16,
131+
AttrValue::Custom,
132+
Access::RV,
133+
Quality::NONE,
134+
)?,
135+
Attribute::new(
136+
Attributes::Fabrics as u16,
137+
AttrValue::Custom,
138+
Access::RV | Access::FAB_SCOPED,
139+
Quality::NONE,
140+
)?,
141+
Attribute::new(
142+
Attributes::SupportedFabrics as u16,
143+
AttrValue::Uint8(MAX_SUPPORTED_FABRICS as u8),
144+
Access::RV,
145+
Quality::FIXED,
146+
)?,
147+
Attribute::new(
148+
Attributes::CommissionedFabrics as u16,
149+
AttrValue::Custom,
150+
Access::RV,
151+
Quality::NONE,
152+
)?,
153+
];
154+
c.base.add_attributes(&attrs[..])?;
130155
Ok(c)
131156
}
132157

@@ -389,24 +414,6 @@ impl NocCluster {
389414
}
390415
}
391416

392-
fn attr_currfabindex_new() -> Result<Attribute, Error> {
393-
Attribute::new(
394-
Attributes::CurrentFabricIndex as u16,
395-
AttrValue::Custom,
396-
Access::RV,
397-
Quality::NONE,
398-
)
399-
}
400-
401-
fn attr_fabrics_new() -> Result<Attribute, Error> {
402-
Attribute::new(
403-
Attributes::Fabrics as u16,
404-
AttrValue::Custom,
405-
Access::RV | Access::FAB_SCOPED,
406-
Quality::NONE,
407-
)
408-
}
409-
410417
impl ClusterType for NocCluster {
411418
fn base(&self) -> &Cluster {
412419
&self.base
@@ -450,6 +457,10 @@ impl ClusterType for NocCluster {
450457
});
451458
let _ = tw.end_container();
452459
})),
460+
Some(Attributes::CommissionedFabrics) => {
461+
let count = self.fabric_mgr.used_count() as u8;
462+
encoder.encode(EncodeValue::Value(&count))
463+
}
453464
_ => {
454465
error!("Attribute not supported: this shouldn't happen");
455466
}

matter/src/data_model/system_model/descriptor.rs

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,27 @@ impl DescriptorCluster {
4848
data_model,
4949
base: Cluster::new(ID)?,
5050
});
51-
c.base.add_attribute(attr_devtypelist_new()?)?;
52-
c.base.add_attribute(attr_serverlist_new()?)?;
53-
c.base.add_attribute(attr_partslist_new()?)?;
51+
let attrs = [
52+
Attribute::new(
53+
Attributes::DeviceTypeList as u16,
54+
AttrValue::Custom,
55+
Access::RV,
56+
Quality::NONE,
57+
)?,
58+
Attribute::new(
59+
Attributes::ServerList as u16,
60+
AttrValue::Custom,
61+
Access::RV,
62+
Quality::NONE,
63+
)?,
64+
Attribute::new(
65+
Attributes::PartsList as u16,
66+
AttrValue::Custom,
67+
Access::RV,
68+
Quality::NONE,
69+
)?,
70+
];
71+
c.base.add_attributes(&attrs[..])?;
5472
Ok(c)
5573
}
5674

@@ -133,29 +151,3 @@ impl ClusterType for DescriptorCluster {
133151
}
134152
}
135153
}
136-
137-
fn attr_devtypelist_new() -> Result<Attribute, Error> {
138-
Attribute::new(
139-
Attributes::DeviceTypeList as u16,
140-
AttrValue::Custom,
141-
Access::RV,
142-
Quality::NONE,
143-
)
144-
}
145-
fn attr_serverlist_new() -> Result<Attribute, Error> {
146-
Attribute::new(
147-
Attributes::ServerList as u16,
148-
AttrValue::Custom,
149-
Access::RV,
150-
Quality::NONE,
151-
)
152-
}
153-
154-
fn attr_partslist_new() -> Result<Attribute, Error> {
155-
Attribute::new(
156-
Attributes::PartsList as u16,
157-
AttrValue::Custom,
158-
Access::RV,
159-
Quality::NONE,
160-
)
161-
}

matter/src/fabric.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,17 @@ impl FabricMgr {
364364
true
365365
}
366366

367+
pub fn used_count(&self) -> usize {
368+
let mgr = self.inner.read().unwrap();
369+
let mut count = 0;
370+
for i in 1..MAX_SUPPORTED_FABRICS {
371+
if mgr.fabrics[i].is_some() {
372+
count += 1;
373+
}
374+
}
375+
count
376+
}
377+
367378
// Parameters to T are the Fabric and its Fabric Index
368379
pub fn for_each<T>(&self, mut f: T) -> Result<(), Error>
369380
where

matter/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
//! pid: 0xFFF1,
5050
//! hw_ver: 2,
5151
//! sw_ver: 1,
52+
//! sw_ver_str: "1".to_string(),
5253
//! serial_no: "aabbcc".to_string(),
5354
//! device_name: "OnOff Light".to_string(),
5455
//! };

matter/tests/common/im_engine.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ impl ImEngine {
9494
pid: 11,
9595
hw_ver: 12,
9696
sw_ver: 13,
97+
sw_ver_str: "13".to_string(),
9798
serial_no: "aabbccdd".to_string(),
9899
device_name: "Test Device".to_string(),
99100
};

0 commit comments

Comments
 (0)