use anyhow::{Result, bail};
use tracing::{error, warn};
use zerocopy::{
BigEndian, FromBytes as ZFromBytes, Immutable, IntoBytes, KnownLayout, U16, U32,
};
use crate::{
client::pdu_connection::FromBytes,
models::{
common::{BasicHeaderSegment, HEADER_LEN, SendingData},
data_fromat::ZeroCopyType,
logout::common::{LogoutReason, RawLogoutReason},
opcode::{BhsOpcode, Opcode, RawBhsOpcode},
},
};
#[repr(C)]
#[derive(Debug, Default, PartialEq, ZFromBytes, IntoBytes, KnownLayout, Immutable)]
pub struct LogoutRequest {
pub opcode: RawBhsOpcode, pub reason: RawLogoutReason, reserved0: [u8; 2], pub total_ahs_length: u8, pub data_segment_length: [u8; 3], reserved1: [u8; 8],
pub initiator_task_tag: u32, pub cid: U16<BigEndian>,
reserved2: [u8; 2], pub cmd_sn: U32<BigEndian>, pub exp_stat_sn: U32<BigEndian>, reserved3: [u8; 16], }
impl LogoutRequest {
pub fn to_bhs_bytes(&self, buf: &mut [u8]) -> Result<()> {
buf.fill(0);
if buf.len() != HEADER_LEN {
bail!("buffer length must be {HEADER_LEN}, got {}", buf.len());
}
buf.copy_from_slice(self.as_bytes());
Ok(())
}
pub fn from_bhs_bytes(buf: &mut [u8]) -> Result<&mut Self> {
let hdr = <Self as zerocopy::FromBytes>::mut_from_bytes(buf)
.map_err(|e| anyhow::anyhow!("failed convert buffer LogoutRequest: {e}"))?;
if hdr.opcode.opcode_known() != Some(Opcode::LogoutReq) {
anyhow::bail!(
"LogoutRequest: invalid opcode 0x{:02x}",
hdr.opcode.opcode_raw()
);
}
Ok(hdr)
}
}
#[derive(Debug, Default)]
pub struct LogoutRequestBuilder {
pub header: LogoutRequest,
}
impl LogoutRequestBuilder {
pub fn new(reason: LogoutReason, itt: u32, cid: u16) -> Self {
Self {
header: LogoutRequest {
opcode: {
let mut tmp = RawBhsOpcode::default();
tmp.set_opcode_known(Opcode::LogoutReq);
tmp.set_i();
tmp
},
reason: reason.into(),
total_ahs_length: 0,
data_segment_length: [0, 0, 0],
initiator_task_tag: itt,
cid: cid.into(),
..Default::default()
},
}
}
pub fn connection_id(mut self, cid: u16) -> Self {
self.header.cid.set(cid);
self
}
pub fn cmd_sn(mut self, cmd_sn: u32) -> Self {
self.header.cmd_sn.set(cmd_sn);
self
}
pub fn exp_stat_sn(mut self, exp_stat_sn: u32) -> Self {
self.header.exp_stat_sn.set(exp_stat_sn);
self
}
}
impl SendingData for LogoutRequest {
fn get_final_bit(&self) -> bool {
true
}
fn set_final_bit(&mut self) {
warn!("Logout Request cannot be marked as Final");
}
fn get_continue_bit(&self) -> bool {
false
}
fn set_continue_bit(&mut self) {
warn!("Logout Request cannot be marked as Contine");
}
}
impl FromBytes for LogoutRequest {
fn from_bhs_bytes(bytes: &mut [u8]) -> Result<&mut Self> {
LogoutRequest::from_bhs_bytes(bytes)
}
}
impl BasicHeaderSegment for LogoutRequest {
#[inline]
fn to_bhs_bytes(&self, buf: &mut [u8]) -> Result<()> {
self.to_bhs_bytes(buf)
}
#[inline]
fn get_opcode(&self) -> Result<BhsOpcode> {
BhsOpcode::try_from(self.opcode.raw())
}
#[inline]
fn get_initiator_task_tag(&self) -> u32 {
self.initiator_task_tag
}
#[inline]
fn get_ahs_length_bytes(&self) -> usize {
(self.total_ahs_length as usize) * 4
}
#[inline]
fn set_ahs_length_bytes(&mut self, len: u8) {
self.total_ahs_length = len >> 2;
}
#[inline]
fn get_data_length_bytes(&self) -> usize {
u32::from_be_bytes([
0,
self.data_segment_length[0],
self.data_segment_length[1],
self.data_segment_length[2],
]) as usize
}
#[inline]
fn set_data_length_bytes(&mut self, len: u32) {
error!("LogoutReq must have zero DataSegmentLength");
let be = len.to_be_bytes();
self.data_segment_length = [be[1], be[2], be[3]];
}
#[inline]
fn get_header_diggest(&self, _: bool) -> usize {
0
}
#[inline]
fn get_data_diggest(&self, _: bool) -> usize {
0
}
}
impl ZeroCopyType for LogoutRequest {}