use crate::tlv;
use anyhow;
use serde_json;
pub type NameSupport = u8;
pub mod namesupport {
pub const GROUP_NAMES: u8 = 0x80;
}
pub fn encode_add_group(group_id: u8, group_name: String) -> anyhow::Result<Vec<u8>> {
let tlv = tlv::TlvItemEnc {
tag: 0,
value: tlv::TlvItemValueEnc::StructInvisible(vec![
(0, tlv::TlvItemValueEnc::UInt8(group_id)).into(),
(1, tlv::TlvItemValueEnc::String(group_name)).into(),
]),
};
Ok(tlv.encode()?)
}
pub fn encode_view_group(group_id: u8) -> anyhow::Result<Vec<u8>> {
let tlv = tlv::TlvItemEnc {
tag: 0,
value: tlv::TlvItemValueEnc::StructInvisible(vec![
(0, tlv::TlvItemValueEnc::UInt8(group_id)).into(),
]),
};
Ok(tlv.encode()?)
}
pub fn encode_get_group_membership(group_list: Vec<u8>) -> anyhow::Result<Vec<u8>> {
let tlv = tlv::TlvItemEnc {
tag: 0,
value: tlv::TlvItemValueEnc::StructInvisible(vec![
(0, tlv::TlvItemValueEnc::StructAnon(group_list.into_iter().map(|v| (0, tlv::TlvItemValueEnc::UInt8(v)).into()).collect())).into(),
]),
};
Ok(tlv.encode()?)
}
pub fn encode_remove_group(group_id: u8) -> anyhow::Result<Vec<u8>> {
let tlv = tlv::TlvItemEnc {
tag: 0,
value: tlv::TlvItemValueEnc::StructInvisible(vec![
(0, tlv::TlvItemValueEnc::UInt8(group_id)).into(),
]),
};
Ok(tlv.encode()?)
}
pub fn encode_add_group_if_identifying(group_id: u8, group_name: String) -> anyhow::Result<Vec<u8>> {
let tlv = tlv::TlvItemEnc {
tag: 0,
value: tlv::TlvItemValueEnc::StructInvisible(vec![
(0, tlv::TlvItemValueEnc::UInt8(group_id)).into(),
(1, tlv::TlvItemValueEnc::String(group_name)).into(),
]),
};
Ok(tlv.encode()?)
}
pub fn decode_name_support(inp: &tlv::TlvItemValue) -> anyhow::Result<NameSupport> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(*v as u8)
} else {
Err(anyhow::anyhow!("Expected Integer"))
}
}
pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
if cluster_id != 0x0004 {
return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0004, got {}\"}}", cluster_id);
}
match attribute_id {
0x0000 => {
match decode_name_support(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
_ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
}
}
pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
vec![
(0x0000, "NameSupport"),
]
}
#[derive(Debug, serde::Serialize)]
pub struct AddGroupResponse {
pub status: Option<u8>,
pub group_id: Option<u8>,
}
#[derive(Debug, serde::Serialize)]
pub struct ViewGroupResponse {
pub status: Option<u8>,
pub group_id: Option<u8>,
pub group_name: Option<String>,
}
#[derive(Debug, serde::Serialize)]
pub struct GetGroupMembershipResponse {
pub capacity: Option<u8>,
pub group_list: Option<Vec<u8>>,
}
#[derive(Debug, serde::Serialize)]
pub struct RemoveGroupResponse {
pub status: Option<u8>,
pub group_id: Option<u8>,
}
pub fn decode_add_group_response(inp: &tlv::TlvItemValue) -> anyhow::Result<AddGroupResponse> {
if let tlv::TlvItemValue::List(_fields) = inp {
let item = tlv::TlvItem { tag: 0, value: inp.clone() };
Ok(AddGroupResponse {
status: item.get_int(&[0]).map(|v| v as u8),
group_id: item.get_int(&[1]).map(|v| v as u8),
})
} else {
Err(anyhow::anyhow!("Expected struct fields"))
}
}
pub fn decode_view_group_response(inp: &tlv::TlvItemValue) -> anyhow::Result<ViewGroupResponse> {
if let tlv::TlvItemValue::List(_fields) = inp {
let item = tlv::TlvItem { tag: 0, value: inp.clone() };
Ok(ViewGroupResponse {
status: item.get_int(&[0]).map(|v| v as u8),
group_id: item.get_int(&[1]).map(|v| v as u8),
group_name: item.get_string_owned(&[2]),
})
} else {
Err(anyhow::anyhow!("Expected struct fields"))
}
}
pub fn decode_get_group_membership_response(inp: &tlv::TlvItemValue) -> anyhow::Result<GetGroupMembershipResponse> {
if let tlv::TlvItemValue::List(_fields) = inp {
let item = tlv::TlvItem { tag: 0, value: inp.clone() };
Ok(GetGroupMembershipResponse {
capacity: item.get_int(&[0]).map(|v| v as u8),
group_list: {
if let Some(tlv::TlvItemValue::List(l)) = item.get(&[1]) {
let items: Vec<u8> = l.iter().filter_map(|e| { if let tlv::TlvItemValue::Int(v) = &e.value { Some(*v as u8) } else { None } }).collect();
Some(items)
} else {
None
}
},
})
} else {
Err(anyhow::anyhow!("Expected struct fields"))
}
}
pub fn decode_remove_group_response(inp: &tlv::TlvItemValue) -> anyhow::Result<RemoveGroupResponse> {
if let tlv::TlvItemValue::List(_fields) = inp {
let item = tlv::TlvItem { tag: 0, value: inp.clone() };
Ok(RemoveGroupResponse {
status: item.get_int(&[0]).map(|v| v as u8),
group_id: item.get_int(&[1]).map(|v| v as u8),
})
} else {
Err(anyhow::anyhow!("Expected struct fields"))
}
}