data_model: provide an API for adding attributes in bulk
This commit is contained in:
		
							parent
							
								
									1a0a41812d
								
							
						
					
					
						commit
						fb82caf391
					
				
					 4 changed files with 73 additions and 92 deletions
				
			
		|  | @ -42,59 +42,6 @@ pub struct BasicInfoConfig { | ||||||
|     pub device_name: String, |     pub device_name: String, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn attr_dm_rev_new() -> Result<Attribute, Error> { |  | ||||||
|     Attribute::new( |  | ||||||
|         Attributes::DMRevision as u16, |  | ||||||
|         AttrValue::Uint8(1), |  | ||||||
|         Access::RV, |  | ||||||
|         Quality::FIXED, |  | ||||||
|     ) |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| fn attr_vid_new(vid: u16) -> Result<Attribute, Error> { |  | ||||||
|     Attribute::new( |  | ||||||
|         Attributes::VendorId as u16, |  | ||||||
|         AttrValue::Uint16(vid), |  | ||||||
|         Access::RV, |  | ||||||
|         Quality::FIXED, |  | ||||||
|     ) |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| fn attr_pid_new(pid: u16) -> Result<Attribute, Error> { |  | ||||||
|     Attribute::new( |  | ||||||
|         Attributes::ProductId as u16, |  | ||||||
|         AttrValue::Uint16(pid), |  | ||||||
|         Access::RV, |  | ||||||
|         Quality::FIXED, |  | ||||||
|     ) |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| fn attr_hw_ver_new(hw_ver: u16) -> Result<Attribute, Error> { |  | ||||||
|     Attribute::new( |  | ||||||
|         Attributes::HwVer as u16, |  | ||||||
|         AttrValue::Uint16(hw_ver), |  | ||||||
|         Access::RV, |  | ||||||
|         Quality::FIXED, |  | ||||||
|     ) |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| fn attr_sw_ver_new(sw_ver: u32) -> Result<Attribute, Error> { |  | ||||||
|     Attribute::new( |  | ||||||
|         Attributes::SwVer as u16, |  | ||||||
|         AttrValue::Uint32(sw_ver), |  | ||||||
|         Access::RV, |  | ||||||
|         Quality::FIXED, |  | ||||||
|     ) |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| fn attr_serial_no_new(label: String) -> Result<Attribute, Error> { |  | ||||||
|     Attribute::new( |  | ||||||
|         Attributes::SerialNo as u16, |  | ||||||
|         AttrValue::Utf8(label), |  | ||||||
|         Access::RV, |  | ||||||
|         Quality::FIXED, |  | ||||||
|     ) |  | ||||||
| } |  | ||||||
| pub struct BasicInfoCluster { | pub struct BasicInfoCluster { | ||||||
|     base: Cluster, |     base: Cluster, | ||||||
| } | } | ||||||
|  | @ -104,14 +51,47 @@ impl BasicInfoCluster { | ||||||
|         let mut cluster = Box::new(BasicInfoCluster { |         let mut cluster = Box::new(BasicInfoCluster { | ||||||
|             base: Cluster::new(ID)?, |             base: Cluster::new(ID)?, | ||||||
|         }); |         }); | ||||||
|         cluster.base.add_attribute(attr_dm_rev_new()?)?; | 
 | ||||||
|         cluster.base.add_attribute(attr_vid_new(cfg.vid)?)?; |         let attrs = [ | ||||||
|         cluster.base.add_attribute(attr_pid_new(cfg.pid)?)?; |             Attribute::new( | ||||||
|         cluster.base.add_attribute(attr_hw_ver_new(cfg.hw_ver)?)?; |                 Attributes::DMRevision as u16, | ||||||
|         cluster.base.add_attribute(attr_sw_ver_new(cfg.sw_ver)?)?; |                 AttrValue::Uint8(1), | ||||||
|         cluster |                 Access::RV, | ||||||
|             .base |                 Quality::FIXED, | ||||||
|             .add_attribute(attr_serial_no_new(cfg.serial_no)?)?; |             )?, | ||||||
|  |             Attribute::new( | ||||||
|  |                 Attributes::VendorId as u16, | ||||||
|  |                 AttrValue::Uint16(cfg.vid), | ||||||
|  |                 Access::RV, | ||||||
|  |                 Quality::FIXED, | ||||||
|  |             )?, | ||||||
|  |             Attribute::new( | ||||||
|  |                 Attributes::ProductId as u16, | ||||||
|  |                 AttrValue::Uint16(cfg.pid), | ||||||
|  |                 Access::RV, | ||||||
|  |                 Quality::FIXED, | ||||||
|  |             )?, | ||||||
|  |             Attribute::new( | ||||||
|  |                 Attributes::HwVer as u16, | ||||||
|  |                 AttrValue::Uint16(cfg.hw_ver), | ||||||
|  |                 Access::RV, | ||||||
|  |                 Quality::FIXED, | ||||||
|  |             )?, | ||||||
|  |             Attribute::new( | ||||||
|  |                 Attributes::SwVer as u16, | ||||||
|  |                 AttrValue::Uint32(cfg.sw_ver), | ||||||
|  |                 Access::RV, | ||||||
|  |                 Quality::FIXED, | ||||||
|  |             )?, | ||||||
|  |             Attribute::new( | ||||||
|  |                 Attributes::SerialNo as u16, | ||||||
|  |                 AttrValue::Utf8(cfg.serial_no), | ||||||
|  |                 Access::RV, | ||||||
|  |                 Quality::FIXED, | ||||||
|  |             )?, | ||||||
|  |         ]; | ||||||
|  |         cluster.base.add_attributes(&attrs[..])?; | ||||||
|  | 
 | ||||||
|         Ok(cluster) |         Ok(cluster) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -151,7 +151,7 @@ impl AttrValue { | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #[derive(Debug)] | #[derive(Debug, Clone)] | ||||||
| pub struct Attribute { | pub struct Attribute { | ||||||
|     pub(super) id: u16, |     pub(super) id: u16, | ||||||
|     pub(super) value: AttrValue, |     pub(super) value: AttrValue, | ||||||
|  |  | ||||||
|  | @ -30,7 +30,7 @@ use std::fmt::{self, Debug}; | ||||||
| 
 | 
 | ||||||
| use super::Encoder; | use super::Encoder; | ||||||
| 
 | 
 | ||||||
| pub const ATTRS_PER_CLUSTER: usize = 8; | pub const ATTRS_PER_CLUSTER: usize = 10; | ||||||
| pub const CMDS_PER_CLUSTER: usize = 8; | pub const CMDS_PER_CLUSTER: usize = 8; | ||||||
| 
 | 
 | ||||||
| #[derive(FromPrimitive, Debug)] | #[derive(FromPrimitive, Debug)] | ||||||
|  | @ -132,6 +132,15 @@ impl Cluster { | ||||||
|         )?) |         )?) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     pub fn add_attributes(&mut self, attrs: &[Attribute]) -> Result<(), Error> { | ||||||
|  |         if self.attributes.len() + attrs.len() <= self.attributes.capacity() { | ||||||
|  |             self.attributes.extend_from_slice(attrs); | ||||||
|  |             Ok(()) | ||||||
|  |         } else { | ||||||
|  |             Err(Error::NoSpace) | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     pub fn add_attribute(&mut self, attr: Attribute) -> Result<(), Error> { |     pub fn add_attribute(&mut self, attr: Attribute) -> Result<(), Error> { | ||||||
|         if self.attributes.len() < self.attributes.capacity() { |         if self.attributes.len() < self.attributes.capacity() { | ||||||
|             self.attributes.push(attr); |             self.attributes.push(attr); | ||||||
|  |  | ||||||
|  | @ -48,9 +48,27 @@ impl DescriptorCluster { | ||||||
|             data_model, |             data_model, | ||||||
|             base: Cluster::new(ID)?, |             base: Cluster::new(ID)?, | ||||||
|         }); |         }); | ||||||
|         c.base.add_attribute(attr_devtypelist_new()?)?; |         let attrs = [ | ||||||
|         c.base.add_attribute(attr_serverlist_new()?)?; |             Attribute::new( | ||||||
|         c.base.add_attribute(attr_partslist_new()?)?; |                 Attributes::DeviceTypeList as u16, | ||||||
|  |                 AttrValue::Custom, | ||||||
|  |                 Access::RV, | ||||||
|  |                 Quality::NONE, | ||||||
|  |             )?, | ||||||
|  |             Attribute::new( | ||||||
|  |                 Attributes::ServerList as u16, | ||||||
|  |                 AttrValue::Custom, | ||||||
|  |                 Access::RV, | ||||||
|  |                 Quality::NONE, | ||||||
|  |             )?, | ||||||
|  |             Attribute::new( | ||||||
|  |                 Attributes::PartsList as u16, | ||||||
|  |                 AttrValue::Custom, | ||||||
|  |                 Access::RV, | ||||||
|  |                 Quality::NONE, | ||||||
|  |             )?, | ||||||
|  |         ]; | ||||||
|  |         c.base.add_attributes(&attrs[..])?; | ||||||
|         Ok(c) |         Ok(c) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | @ -133,29 +151,3 @@ impl ClusterType for DescriptorCluster { | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 |  | ||||||
| fn attr_devtypelist_new() -> Result<Attribute, Error> { |  | ||||||
|     Attribute::new( |  | ||||||
|         Attributes::DeviceTypeList as u16, |  | ||||||
|         AttrValue::Custom, |  | ||||||
|         Access::RV, |  | ||||||
|         Quality::NONE, |  | ||||||
|     ) |  | ||||||
| } |  | ||||||
| fn attr_serverlist_new() -> Result<Attribute, Error> { |  | ||||||
|     Attribute::new( |  | ||||||
|         Attributes::ServerList as u16, |  | ||||||
|         AttrValue::Custom, |  | ||||||
|         Access::RV, |  | ||||||
|         Quality::NONE, |  | ||||||
|     ) |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| fn attr_partslist_new() -> Result<Attribute, Error> { |  | ||||||
|     Attribute::new( |  | ||||||
|         Attributes::PartsList as u16, |  | ||||||
|         AttrValue::Custom, |  | ||||||
|         Access::RV, |  | ||||||
|         Quality::NONE, |  | ||||||
|     ) |  | ||||||
| } |  | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue