use alloc::vec::Vec;
use crate::operation::{
CommandCode, DeviceReply, Operation, OperationError, PayloadReader, PayloadWriter,
};
#[derive(Debug, Clone, Copy, Default)]
pub struct ReadTransducerInformation;
#[derive(Debug, Clone, Copy, Default)]
pub struct ReadOutputInformation;
macro_rules! raw_information_operation {
($type:ty, $command:literal) => {
impl Operation for $type {
type Output = Vec<u8>;
fn command(&self) -> CommandCode {
CommandCode::new($command)
}
fn encode_request(&self, _writer: &mut PayloadWriter) -> Result<(), OperationError> {
Ok(())
}
fn decode_reply(&self, reply: &DeviceReply) -> Result<Self::Output, OperationError> {
reply.require_success(self.command())?;
if reply.data.is_empty() {
return Err(OperationError::InvalidValue("empty device information"));
}
Ok(reply.data.clone())
}
}
};
}
raw_information_operation!(ReadTransducerInformation, 14);
raw_information_operation!(ReadOutputInformation, 15);
#[derive(Debug, Clone, Copy, Default)]
pub struct ReadFinalAssemblyNumber;
impl Operation for ReadFinalAssemblyNumber {
type Output = u32;
fn command(&self) -> CommandCode {
CommandCode::new(16)
}
fn encode_request(&self, _writer: &mut PayloadWriter) -> Result<(), OperationError> {
Ok(())
}
fn decode_reply(&self, reply: &DeviceReply) -> Result<Self::Output, OperationError> {
reply.require_success(self.command())?;
let mut reader = PayloadReader::new(&reply.data);
let number = reader.u24("final assembly number")?;
reader.finish()?;
Ok(number)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WriteFinalAssemblyNumber(pub u32);
impl WriteFinalAssemblyNumber {
pub const fn new(value: u32) -> Result<Self, OperationError> {
if value > 0x00ff_ffff {
return Err(OperationError::InvalidValue("24-bit integer"));
}
Ok(Self(value))
}
}
impl Operation for WriteFinalAssemblyNumber {
type Output = u32;
fn command(&self) -> CommandCode {
CommandCode::new(19)
}
fn encode_request(&self, writer: &mut PayloadWriter) -> Result<(), OperationError> {
writer.u24(self.0)
}
fn decode_reply(&self, reply: &DeviceReply) -> Result<Self::Output, OperationError> {
reply.require_success(self.command())?;
if reply.data.is_empty() {
return Ok(self.0);
}
let mut reader = PayloadReader::new(&reply.data);
let number = reader.u24("final assembly number")?;
reader.finish()?;
Ok(number)
}
}