use core::cell::RefCell;
use crate::dm::{
Access, AttributeDescriptor, AttributeQualities, ClusterDescriptor, CommandDescriptor,
Privilege, Resolved,
};
use crate::im::{AttributeId, ClusterHandler, ClusterId, InteractionContext, Status, WriteOp};
use crate::tlv::{ContainerKind, Tag, TlvReader, TlvWriter, Value};
use super::Cluster;
pub const FIXED_LABEL_ID: ClusterId = 0x0040;
pub const USER_LABEL_ID: ClusterId = 0x0041;
pub const REVISION: u16 = 1;
pub const LABEL_LIST: AttributeId = 0x0000;
pub const LABEL_MAX: usize = 16;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Label<'a> {
pub label: &'a str,
pub value: &'a str,
}
impl<'a> Label<'a> {
#[must_use]
pub const fn new(label: &'a str, value: &'a str) -> Option<Self> {
if label.len() > LABEL_MAX || value.len() > LABEL_MAX {
return None;
}
Some(Self { label, value })
}
fn encode(&self, w: &mut TlvWriter<'_>) -> crate::error::Result<()> {
w.start_structure(Tag::Anonymous)?;
w.utf8(Tag::Context(0), self.label)?;
w.utf8(Tag::Context(1), self.value)?;
w.end_container()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct OwnedLabel {
label: heapless::String<LABEL_MAX>,
value: heapless::String<LABEL_MAX>,
}
impl OwnedLabel {
#[must_use]
pub fn label(&self) -> &str {
&self.label
}
#[must_use]
pub fn value(&self) -> &str {
&self.value
}
#[must_use]
pub fn as_label(&self) -> Label<'_> {
Label {
label: &self.label,
value: &self.value,
}
}
pub fn new(label: &str, value: &str) -> Result<Self, Status> {
let mut out = Self::default();
out.label
.push_str(label)
.map_err(|_| Status::ConstraintError)?;
out.value
.push_str(value)
.map_err(|_| Status::ConstraintError)?;
Ok(out)
}
}
fn write_list<'a>(
labels: impl Iterator<Item = Label<'a>>,
w: &mut TlvWriter<'_>,
tag: Tag,
) -> Result<(), Status> {
let full = |r: crate::error::Result<()>| r.map_err(|_| Status::ResourceExhausted);
full(w.start_array(tag))?;
for label in labels {
full(label.encode(w))?;
}
full(w.end_container())
}
fn decode_label(reader: &mut TlvReader<'_>) -> Result<OwnedLabel, Status> {
let mut label = None;
let mut value = None;
loop {
let Some(element) = reader.next_element().map_err(|_| Status::InvalidAction)? else {
return Err(Status::InvalidAction);
};
if element.value == Value::EndOfContainer {
break;
}
match element.tag.context() {
Some(0) => label = Some(element.utf8().map_err(|_| Status::InvalidAction)?),
Some(1) => value = Some(element.utf8().map_err(|_| Status::InvalidAction)?),
_ => reader
.skip_value(&element)
.map_err(|_| Status::InvalidAction)?,
}
}
OwnedLabel::new(label.unwrap_or_default(), value.unwrap_or_default())
}
const FIXED_ATTRIBUTES: &[AttributeDescriptor] =
&[AttributeDescriptor::read_only(LABEL_LIST).with_qualities(AttributeQualities::NON_VOLATILE)];
const NO_COMMANDS: &[CommandDescriptor] = &[];
#[must_use]
pub const fn fixed_cluster() -> ClusterDescriptor<'static> {
ClusterDescriptor {
id: FIXED_LABEL_ID,
revision: REVISION,
feature_map: 0,
attributes: FIXED_ATTRIBUTES,
accepted_commands: NO_COMMANDS,
generated_commands: &[],
events: &[],
}
}
#[derive(Debug, Clone, Copy)]
pub struct FixedLabel<'a> {
labels: &'a [Label<'a>],
}
impl<'a> FixedLabel<'a> {
#[must_use]
pub const fn new(labels: &'a [Label<'a>]) -> Self {
Self { labels }
}
#[must_use]
pub const fn labels(&self) -> &'a [Label<'a>] {
self.labels
}
}
impl ClusterHandler for FixedLabel<'_> {
fn read(
&self,
resolved: &Resolved<'_>,
_ctx: &InteractionContext<'_>,
w: &mut TlvWriter<'_>,
tag: Tag,
) -> Result<(), Status> {
if resolved.attribute != LABEL_LIST {
return Err(Status::UnsupportedAttribute);
}
write_list(self.labels.iter().copied(), w, tag)
}
}
impl Cluster for FixedLabel<'_> {
const ID: ClusterId = FIXED_LABEL_ID;
}
const USER_ATTRIBUTES: &[AttributeDescriptor] = &[AttributeDescriptor::read_write(LABEL_LIST)
.with_access(Access::read_write_with(Privilege::View, Privilege::Manage))
.with_qualities(AttributeQualities::NON_VOLATILE)];
#[must_use]
pub const fn user_cluster() -> ClusterDescriptor<'static> {
ClusterDescriptor {
id: USER_LABEL_ID,
revision: REVISION,
feature_map: 0,
attributes: USER_ATTRIBUTES,
accepted_commands: NO_COMMANDS,
generated_commands: &[],
events: &[],
}
}
#[derive(Debug)]
pub struct UserLabel<const N: usize> {
labels: RefCell<heapless::Vec<OwnedLabel, N>>,
}
impl<const N: usize> Default for UserLabel<N> {
fn default() -> Self {
Self::new()
}
}
impl<const N: usize> UserLabel<N> {
#[must_use]
pub const fn new() -> Self {
Self {
labels: RefCell::new(heapless::Vec::new()),
}
}
#[must_use]
pub fn labels(&self) -> core::cell::Ref<'_, heapless::Vec<OwnedLabel, N>> {
self.labels.borrow()
}
pub fn restore(&self, labels: impl IntoIterator<Item = OwnedLabel>) -> Result<(), Status> {
let mut held = self.labels.borrow_mut();
held.clear();
for label in labels {
held.push(label).map_err(|_| Status::ResourceExhausted)?;
}
Ok(())
}
}
impl<const N: usize> ClusterHandler for UserLabel<N> {
fn read(
&self,
resolved: &Resolved<'_>,
_ctx: &InteractionContext<'_>,
w: &mut TlvWriter<'_>,
tag: Tag,
) -> Result<(), Status> {
if resolved.attribute != LABEL_LIST {
return Err(Status::UnsupportedAttribute);
}
let labels = self.labels.borrow();
write_list(labels.iter().map(OwnedLabel::as_label), w, tag)
}
fn write(
&self,
resolved: &Resolved<'_>,
data: &[u8],
op: WriteOp,
_ctx: &InteractionContext<'_>,
) -> Result<(), Status> {
if resolved.attribute != LABEL_LIST {
return Err(Status::UnsupportedWrite);
}
let mut reader = TlvReader::new_in(data, ContainerKind::Structure);
let element = reader
.next_element()
.map_err(|_| Status::InvalidAction)?
.ok_or(Status::InvalidAction)?;
match op {
WriteOp::Replace => {
if element.value.container() != Some(ContainerKind::Array) {
return Err(Status::InvalidAction);
}
let mut replacement: heapless::Vec<OwnedLabel, N> = heapless::Vec::new();
loop {
let Some(item) = reader.next_element().map_err(|_| Status::InvalidAction)?
else {
return Err(Status::InvalidAction);
};
if item.value == Value::EndOfContainer {
break;
}
if item.value.container() != Some(ContainerKind::Structure) {
return Err(Status::InvalidAction);
}
replacement
.push(decode_label(&mut reader)?)
.map_err(|_| Status::ResourceExhausted)?;
}
*self.labels.borrow_mut() = replacement;
}
WriteOp::Append => {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(Status::InvalidAction);
}
let label = decode_label(&mut reader)?;
self.labels
.borrow_mut()
.push(label)
.map_err(|_| Status::ResourceExhausted)?;
}
}
Ok(())
}
}
impl<const N: usize> Cluster for UserLabel<N> {
const ID: ClusterId = USER_LABEL_ID;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_label_longer_than_sixteen_is_refused_when_the_model_is_built() {
assert!(Label::new("room", "kitchen").is_some());
assert!(Label::new("room", "seventeen chars!!").is_none());
assert!(Label::new("a-seventeen-char!", "x").is_none());
assert!(Label::new("sixteen-chars-ok", "sixteen-chars-ok").is_some());
}
#[test]
fn the_two_clusters_differ_only_in_whether_the_list_can_be_written() {
let fixed = fixed_cluster();
let user = user_cluster();
assert_eq!(fixed.id, 0x0040);
assert_eq!(user.id, 0x0041);
let fixed_list = fixed.attribute(LABEL_LIST).expect("LabelList");
assert_eq!(fixed_list.access.read, Some(Privilege::View));
assert_eq!(fixed_list.access.write, None, "§9.8.5 gives it `RV`");
let user_list = user.attribute(LABEL_LIST).expect("LabelList");
assert_eq!(user_list.access.read, Some(Privilege::View));
assert_eq!(
user_list.access.write,
Some(Privilege::Manage),
"§9.9.5 gives it `RW VM` — renaming is configuration, not use"
);
assert!(
fixed_list
.qualities
.contains(AttributeQualities::NON_VOLATILE)
);
assert!(
user_list
.qualities
.contains(AttributeQualities::NON_VOLATILE)
);
}
#[test]
fn a_user_label_list_is_bounded_by_its_capacity() {
let cluster = UserLabel::<2>::new();
cluster
.restore([
OwnedLabel::new("room", "kitchen").expect("fits"),
OwnedLabel::new("floor", "1").expect("fits"),
])
.expect("two fit");
assert_eq!(
cluster
.restore([
OwnedLabel::new("a", "1").expect("fits"),
OwnedLabel::new("b", "2").expect("fits"),
OwnedLabel::new("c", "3").expect("fits"),
])
.unwrap_err(),
Status::ResourceExhausted,
"§9.6.1's rule for a full list, and the only honest answer without an allocator"
);
}
#[test]
fn an_over_long_label_is_a_constraint_error_not_a_truncation() {
assert_eq!(
OwnedLabel::new("this-label-is-far-too-long", "x").unwrap_err(),
Status::ConstraintError
);
}
}