#![allow(unused_imports)]
use crate::dm::conformance::{Clause, Condition, Conform, Conformance};
use crate::dm::meta::{EventPriority, Reporting};
use crate::dm::spec::{Attribute, Cluster, Command, Event, Feature};
use crate::dm::{Access, AccessQualities, AttributeQualities, Privilege};
use crate::error::Result;
use crate::im::{AttributeId, ClusterId, CommandId, EventId};
use crate::tlv::{
ContainerKind, Element, FromTlv, Nullable, Tag, TlvList, TlvReader, TlvWriter, ToTlv, Value,
};
pub const ID: ClusterId = 0x050C;
pub const REVISION: u16 = 2;
pub const PICS: &str = "APPLAUNCHER";
pub mod feature {
pub const APPLICATION_PLATFORM: u32 = 1 << 0;
}
pub const FEATURES: &[Feature] = &[Feature {
bit: 0,
code: "AP",
name: "ApplicationPlatform",
conform: Conform::OPTIONAL,
}];
pub mod attribute {
use crate::im::AttributeId;
pub const CATALOG_LIST: AttributeId = 0x00;
pub const CURRENT_APP: AttributeId = 0x01;
}
pub mod command {
use crate::im::CommandId;
pub const LAUNCH_APP: CommandId = 0x00;
pub const STOP_APP: CommandId = 0x01;
pub const HIDE_APP: CommandId = 0x02;
pub const LAUNCHER_RESPONSE: CommandId = 0x03;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum StatusEnum {
Success = 0,
AppNotAvailable = 1,
SystemBusy = 2,
PendingUserApproval = 3,
Downloading = 4,
Installing = 5,
}
impl StatusEnum {
#[must_use]
pub const fn from_value(value: u8) -> Option<Self> {
match value {
0 => Some(Self::Success),
1 => Some(Self::AppNotAvailable),
2 => Some(Self::SystemBusy),
3 => Some(Self::PendingUserApproval),
4 => Some(Self::Downloading),
5 => Some(Self::Installing),
_ => None,
}
}
#[must_use]
pub const fn value(self) -> u8 {
self as u8
}
}
impl ToTlv for StatusEnum {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
self.value().to_tlv(w, tag)
}
}
impl<'a> FromTlv<'a> for StatusEnum {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ApplicationEPStruct<'a> {
pub application: ApplicationStruct<'a>,
pub endpoint: Option<u16>,
}
impl<'a> ToTlv for ApplicationEPStruct<'a> {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.application.to_tlv(w, Tag::Context(0))?;
if let Some(value) = &self.endpoint {
value.to_tlv(w, Tag::Context(1))?;
}
w.end_container()
}
}
impl<'a> FromTlv<'a> for ApplicationEPStruct<'a> {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
}
let mut application = None;
let mut endpoint = None;
loop {
let Some(field) = reader.next_element()? else {
return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
};
if field.value == Value::EndOfContainer {
break;
}
match field.tag.context() {
Some(0) => application = Some(<ApplicationStruct<'a>>::from_tlv(reader, &field)?),
Some(1) => endpoint = Some(<u16>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
application: application.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
endpoint,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ApplicationStruct<'a> {
pub catalog_vendor_id: u16,
pub application_id: &'a str,
}
impl<'a> ToTlv for ApplicationStruct<'a> {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.catalog_vendor_id.to_tlv(w, Tag::Context(0))?;
self.application_id.to_tlv(w, Tag::Context(1))?;
w.end_container()
}
}
impl<'a> FromTlv<'a> for ApplicationStruct<'a> {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
}
let mut catalog_vendor_id = None;
let mut application_id = None;
loop {
let Some(field) = reader.next_element()? else {
return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
};
if field.value == Value::EndOfContainer {
break;
}
match field.tag.context() {
Some(0) => catalog_vendor_id = Some(<u16>::from_tlv(reader, &field)?),
Some(1) => application_id = Some(<&'a str>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
catalog_vendor_id: catalog_vendor_id
.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
application_id: application_id
.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LaunchAppFields<'a> {
pub application: Option<ApplicationStruct<'a>>,
pub data: Option<&'a [u8]>,
}
impl<'a> ToTlv for LaunchAppFields<'a> {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
if let Some(value) = &self.application {
value.to_tlv(w, Tag::Context(0))?;
}
if let Some(value) = &self.data {
value.to_tlv(w, Tag::Context(1))?;
}
w.end_container()
}
}
impl<'a> FromTlv<'a> for LaunchAppFields<'a> {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
}
let mut application = None;
let mut data = None;
loop {
let Some(field) = reader.next_element()? else {
return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
};
if field.value == Value::EndOfContainer {
break;
}
match field.tag.context() {
Some(0) => application = Some(<ApplicationStruct<'a>>::from_tlv(reader, &field)?),
Some(1) => data = Some(<&'a [u8]>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self { application, data })
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct StopAppFields<'a> {
pub application: Option<ApplicationStruct<'a>>,
}
impl<'a> ToTlv for StopAppFields<'a> {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
if let Some(value) = &self.application {
value.to_tlv(w, Tag::Context(0))?;
}
w.end_container()
}
}
impl<'a> FromTlv<'a> for StopAppFields<'a> {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
}
let mut application = None;
loop {
let Some(field) = reader.next_element()? else {
return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
};
if field.value == Value::EndOfContainer {
break;
}
match field.tag.context() {
Some(0) => application = Some(<ApplicationStruct<'a>>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self { application })
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct HideAppFields<'a> {
pub application: Option<ApplicationStruct<'a>>,
}
impl<'a> ToTlv for HideAppFields<'a> {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
if let Some(value) = &self.application {
value.to_tlv(w, Tag::Context(0))?;
}
w.end_container()
}
}
impl<'a> FromTlv<'a> for HideAppFields<'a> {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
}
let mut application = None;
loop {
let Some(field) = reader.next_element()? else {
return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
};
if field.value == Value::EndOfContainer {
break;
}
match field.tag.context() {
Some(0) => application = Some(<ApplicationStruct<'a>>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self { application })
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LauncherResponseFields<'a> {
pub status: StatusEnum,
pub data: Option<&'a [u8]>,
}
impl<'a> ToTlv for LauncherResponseFields<'a> {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.status.to_tlv(w, Tag::Context(0))?;
if let Some(value) = &self.data {
value.to_tlv(w, Tag::Context(1))?;
}
w.end_container()
}
}
impl<'a> FromTlv<'a> for LauncherResponseFields<'a> {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
}
let mut status = None;
let mut data = None;
loop {
let Some(field) = reader.next_element()? else {
return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
};
if field.value == Value::EndOfContainer {
break;
}
match field.tag.context() {
Some(0) => status = Some(<StatusEnum>::from_tlv(reader, &field)?),
Some(1) => data = Some(<&'a [u8]>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
status: status.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
data,
})
}
}
pub const ATTRIBUTES: &[Attribute] = &[
Attribute {
id: 0x0000,
name: "CatalogList",
kind: "list[uint16]",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::NON_VOLATILE,
reporting: Reporting::OnChange,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Feature(0),
}],
},
},
Attribute {
id: 0x0001,
name: "CurrentApp",
kind: "ApplicationEPStruct",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::NULLABLE,
reporting: Reporting::OnChange,
conform: Conform::OPTIONAL,
},
];
pub const COMMANDS: &[Command] = &[
Command {
id: 0x00,
name: "LaunchApp",
to_server: true,
response: Some(0x03),
access: Access::invoke(Privilege::Operate),
conform: Conform::MANDATORY,
},
Command {
id: 0x01,
name: "StopApp",
to_server: true,
response: Some(0x03),
access: Access::invoke(Privilege::Operate),
conform: Conform::MANDATORY,
},
Command {
id: 0x02,
name: "HideApp",
to_server: true,
response: Some(0x03),
access: Access::invoke(Privilege::Operate),
conform: Conform::MANDATORY,
},
Command {
id: 0x03,
name: "LauncherResponse",
to_server: false,
response: None,
access: Access::invoke(Privilege::Operate),
conform: Conform::MANDATORY,
},
];
pub const EVENTS: &[Event] = &[];
pub const CLUSTER: Cluster = Cluster {
id: ID,
name: "Application Launcher",
revision: REVISION,
pics: "APPLAUNCHER",
derived_from: None,
features: FEATURES,
attributes: ATTRIBUTES,
commands: COMMANDS,
events: EVENTS,
};