Skip to content

Commit 5499e6c

Browse files
committed
Merge branch 'feature/multi-admin' into mdns-disco-multi-admin
2 parents 9b16214 + 40f4fd5 commit 5499e6c

File tree

15 files changed

+157
-35
lines changed

15 files changed

+157
-35
lines changed

examples/onoff_light/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ fn main() {
3535
pid: 0x8002,
3636
hw_ver: 2,
3737
sw_ver: 1,
38+
serial_no: "aabbccdd".to_string(),
3839
device_name: "OnOff Light".to_string(),
3940
};
4041
let dev_att = Box::new(dev_att::HardCodedDevAtt::new());
4142

42-
let mut matter = core::Matter::new(&dev_info, dev_att, comm_data).unwrap();
43+
let mut matter = core::Matter::new(dev_info, dev_att, comm_data).unwrap();
4344
let dm = matter.get_data_model();
4445
{
4546
let mut node = dm.node.write().unwrap();

matter/src/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl Matter {
5454
/// requires a set of device attestation certificates and keys. It is the responsibility of
5555
/// this object to return the device attestation details when queried upon.
5656
pub fn new(
57-
dev_det: &BasicInfoConfig,
57+
dev_det: BasicInfoConfig,
5858
dev_att: Box<dyn DevAttDataFetcher>,
5959
dev_comm: CommissioningData,
6060
) -> Result<Box<Matter>, Error> {

matter/src/data_model/cluster_basic_information.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717

1818
use super::objects::*;
1919
use crate::error::*;
20+
use num_derive::FromPrimitive;
2021

2122
pub const ID: u32 = 0x0028;
23+
24+
#[derive(FromPrimitive)]
2225
enum Attributes {
26+
DMRevision = 0,
2327
VendorId = 2,
2428
ProductId = 4,
2529
HwVer = 7,
2630
SwVer = 9,
31+
SerialNo = 0x0f,
2732
}
2833

2934
#[derive(Default)]
@@ -32,6 +37,16 @@ pub struct BasicInfoConfig {
3237
pub pid: u16,
3338
pub hw_ver: u16,
3439
pub sw_ver: u32,
40+
pub serial_no: String,
41+
}
42+
43+
fn attr_dm_rev_new() -> Result<Attribute, Error> {
44+
Attribute::new(
45+
Attributes::DMRevision as u16,
46+
AttrValue::Uint8(1),
47+
Access::RV,
48+
Quality::FIXED,
49+
)
3550
/// Device name; up to 32 characters
3651
pub device_name: String,
3752
}
@@ -72,19 +87,31 @@ fn attr_sw_ver_new(sw_ver: u32) -> Result<Attribute, Error> {
7287
)
7388
}
7489

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+
}
7598
pub struct BasicInfoCluster {
7699
base: Cluster,
77100
}
78101

79102
impl BasicInfoCluster {
80-
pub fn new(cfg: &BasicInfoConfig) -> Result<Box<Self>, Error> {
103+
pub fn new(cfg: BasicInfoConfig) -> Result<Box<Self>, Error> {
81104
let mut cluster = Box::new(BasicInfoCluster {
82105
base: Cluster::new(ID)?,
83106
});
107+
cluster.base.add_attribute(attr_dm_rev_new()?)?;
84108
cluster.base.add_attribute(attr_vid_new(cfg.vid)?)?;
85109
cluster.base.add_attribute(attr_pid_new(cfg.pid)?)?;
86110
cluster.base.add_attribute(attr_hw_ver_new(cfg.hw_ver)?)?;
87111
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)?)?;
88115
Ok(cluster)
89116
}
90117
}

matter/src/data_model/core.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub struct DataModel {
5151

5252
impl DataModel {
5353
pub fn new(
54-
dev_details: &BasicInfoConfig,
54+
dev_details: BasicInfoConfig,
5555
dev_att: Box<dyn DevAttDataFetcher>,
5656
fabric_mgr: Arc<FabricMgr>,
5757
acl_mgr: Arc<AclMgr>,
@@ -84,7 +84,7 @@ impl DataModel {
8484
) -> Result<AttrValue, IMStatusCode> {
8585
let node = self.node.read().unwrap();
8686
let cluster = node.get_cluster(endpoint, cluster)?;
87-
cluster.base().read_attribute_raw(attr).map(|a| *a)
87+
cluster.base().read_attribute_raw(attr).map(|a| a.clone())
8888
}
8989

9090
// Encode a write attribute from a path that may or may not be wildcard

matter/src/data_model/device_types.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,23 @@ use crate::secure_channel::pake::PaseMgr;
3232
use std::sync::Arc;
3333
use std::sync::RwLockWriteGuard;
3434

35+
pub const DEV_TYPE_ROOT_NODE: DeviceType = DeviceType {
36+
dtype: 0x0016,
37+
drev: 1,
38+
};
39+
3540
type WriteNode<'a> = RwLockWriteGuard<'a, Box<Node>>;
3641

3742
pub fn device_type_add_root_node(
3843
node: &mut WriteNode,
39-
dev_info: &BasicInfoConfig,
44+
dev_info: BasicInfoConfig,
4045
dev_att: Box<dyn DevAttDataFetcher>,
4146
fabric_mgr: Arc<FabricMgr>,
4247
acl_mgr: Arc<AclMgr>,
4348
pase_mgr: PaseMgr,
4449
) -> Result<u32, Error> {
4550
// Add the root endpoint
46-
let endpoint = node.add_endpoint()?;
51+
let endpoint = node.add_endpoint(DEV_TYPE_ROOT_NODE)?;
4752
if endpoint != 0 {
4853
// Somehow endpoint 0 was already added, this shouldn't be the case
4954
return Err(Error::Invalid);
@@ -63,8 +68,13 @@ pub fn device_type_add_root_node(
6368
Ok(endpoint)
6469
}
6570

71+
const DEV_TYPE_ON_OFF_LIGHT: DeviceType = DeviceType {
72+
dtype: 0x0100,
73+
drev: 2,
74+
};
75+
6676
pub fn device_type_add_on_off_light(node: &mut WriteNode) -> Result<u32, Error> {
67-
let endpoint = node.add_endpoint()?;
77+
let endpoint = node.add_endpoint(DEV_TYPE_ON_OFF_LIGHT)?;
6878
node.add_cluster(endpoint, OnOffCluster::new()?)?;
6979
Ok(endpoint)
7080
}

matter/src/data_model/objects/attribute.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,15 @@ bitflags! {
8888
* - instead of arrays, can use linked-lists to conserve space and avoid the internal fragmentation
8989
*/
9090

91-
#[derive(PartialEq, Copy, Clone)]
91+
#[derive(PartialEq, Clone)]
9292
pub enum AttrValue {
9393
Int64(i64),
9494
Uint8(u8),
9595
Uint16(u16),
9696
Uint32(u32),
9797
Uint64(u64),
9898
Bool(bool),
99+
Utf8(String),
99100
Custom,
100101
}
101102

@@ -108,6 +109,7 @@ impl Debug for AttrValue {
108109
AttrValue::Uint32(v) => write!(f, "{:?}", *v),
109110
AttrValue::Uint64(v) => write!(f, "{:?}", *v),
110111
AttrValue::Bool(v) => write!(f, "{:?}", *v),
112+
AttrValue::Utf8(v) => write!(f, "{:?}", *v),
111113
AttrValue::Custom => write!(f, "custom-attribute"),
112114
}?;
113115
Ok(())
@@ -123,6 +125,7 @@ impl ToTLV for AttrValue {
123125
AttrValue::Uint16(v) => tw.u16(tag_type, *v),
124126
AttrValue::Uint32(v) => tw.u32(tag_type, *v),
125127
AttrValue::Uint64(v) => tw.u64(tag_type, *v),
128+
AttrValue::Utf8(v) => tw.utf8(tag_type, v.as_bytes()),
126129
_ => {
127130
error!("Attribute type not yet supported");
128131
Err(Error::AttributeNotFound)

matter/src/data_model/objects/cluster.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ pub trait ClusterType {
8787
pub struct Cluster {
8888
pub(super) id: u32,
8989
attributes: Vec<Attribute>,
90-
feature_map: Option<u32>,
9190
data_ver: u32,
9291
}
9392

@@ -96,7 +95,6 @@ impl Cluster {
9695
let mut c = Cluster {
9796
id,
9897
attributes: Vec::with_capacity(ATTRS_PER_CLUSTER),
99-
feature_map: None,
10098
data_ver: rand::thread_rng().gen_range(0..0xFFFFFFFF),
10199
};
102100
c.add_default_attributes()?;
@@ -112,22 +110,20 @@ impl Cluster {
112110
}
113111

114112
pub fn set_feature_map(&mut self, map: u32) -> Result<(), Error> {
115-
if self.feature_map.is_none() {
116-
self.add_attribute(Attribute::new(
117-
GlobalElements::FeatureMap as u16,
118-
AttrValue::Uint32(map),
119-
Access::RV,
120-
Quality::NONE,
121-
)?)?;
122-
} else {
123-
self.write_attribute_raw(GlobalElements::FeatureMap as u16, AttrValue::Uint32(map))
124-
.map_err(|_| Error::Invalid)?;
125-
}
126-
self.feature_map = Some(map);
113+
self.write_attribute_raw(GlobalElements::FeatureMap as u16, AttrValue::Uint32(map))
114+
.map_err(|_| Error::Invalid)?;
127115
Ok(())
128116
}
129117

130118
fn add_default_attributes(&mut self) -> Result<(), Error> {
119+
// Default feature map is 0
120+
self.add_attribute(Attribute::new(
121+
GlobalElements::FeatureMap as u16,
122+
AttrValue::Uint32(0),
123+
Access::RV,
124+
Quality::NONE,
125+
)?)?;
126+
131127
self.add_attribute(Attribute::new(
132128
GlobalElements::AttributeList as u16,
133129
AttrValue::Custom,
@@ -233,8 +229,7 @@ impl Cluster {
233229
return;
234230
}
235231
GlobalElements::FeatureMap => {
236-
let val = if let Some(m) = self.feature_map { m } else { 0 };
237-
encoder.encode(EncodeValue::Value(&val));
232+
encoder.encode(EncodeValue::Value(&attr.value));
238233
return;
239234
}
240235
_ => {
@@ -284,7 +279,7 @@ impl Cluster {
284279
) -> Result<(), IMStatusCode> {
285280
let a = self.get_attribute_mut(attr_id)?;
286281
if a.value != AttrValue::Custom {
287-
let mut value = a.value;
282+
let mut value = a.value.clone();
288283
value
289284
.update_from_tlv(data)
290285
.map_err(|_| IMStatusCode::Failure)?;

matter/src/data_model/objects/encoder.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,9 @@ pub trait Encoder {
115115
/// Encode a status report
116116
fn encode_status(&mut self, status: IMStatusCode, cluster_status: u16);
117117
}
118+
119+
#[derive(ToTLV, Copy, Clone)]
120+
pub struct DeviceType {
121+
pub dtype: u16,
122+
pub drev: u16,
123+
}

matter/src/data_model/objects/endpoint.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,21 @@ use crate::{data_model::objects::ClusterType, error::*, interaction_model::core:
1919

2020
use std::fmt;
2121

22+
use super::DeviceType;
23+
2224
pub const CLUSTERS_PER_ENDPT: usize = 9;
2325

2426
pub struct Endpoint {
27+
dev_type: DeviceType,
2528
clusters: Vec<Box<dyn ClusterType>>,
2629
}
2730

2831
pub type BoxedClusters = [Box<dyn ClusterType>];
2932

3033
impl Endpoint {
31-
pub fn new() -> Result<Box<Endpoint>, Error> {
34+
pub fn new(dev_type: DeviceType) -> Result<Box<Endpoint>, Error> {
3235
Ok(Box::new(Endpoint {
36+
dev_type,
3337
clusters: Vec::with_capacity(CLUSTERS_PER_ENDPT),
3438
}))
3539
}
@@ -43,6 +47,10 @@ impl Endpoint {
4347
}
4448
}
4549

50+
pub fn get_dev_type(&self) -> &DeviceType {
51+
&self.dev_type
52+
}
53+
4654
fn get_cluster_index(&self, cluster_id: u32) -> Option<usize> {
4755
self.clusters.iter().position(|c| c.base().id == cluster_id)
4856
}

matter/src/data_model/objects/node.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use crate::{
2323
};
2424
use std::fmt;
2525

26+
use super::DeviceType;
27+
2628
pub trait ChangeConsumer {
2729
fn endpoint_added(&self, id: u16, endpoint: &mut Endpoint) -> Result<(), Error>;
2830
}
@@ -59,13 +61,13 @@ impl Node {
5961
self.changes_cb = Some(consumer);
6062
}
6163

62-
pub fn add_endpoint(&mut self) -> Result<u32, Error> {
64+
pub fn add_endpoint(&mut self, dev_type: DeviceType) -> Result<u32, Error> {
6365
let index = self
6466
.endpoints
6567
.iter()
6668
.position(|x| x.is_none())
6769
.ok_or(Error::NoSpace)?;
68-
let mut endpoint = Endpoint::new()?;
70+
let mut endpoint = Endpoint::new(dev_type)?;
6971
if let Some(cb) = &self.changes_cb {
7072
cb.endpoint_added(index as u16, &mut endpoint)?;
7173
}

0 commit comments

Comments
 (0)