#[cfg(test)]
mod abs_send_time_extension_test;
use shared::{
error::{Error, Result},
marshal::{Marshal, MarshalSize, Unmarshal},
};
use bytes::{Buf, BufMut};
pub const ABS_SEND_TIME_EXTENSION_SIZE: usize = 3;
#[derive(PartialEq, Eq, Debug, Default, Copy, Clone)]
pub struct AbsSendTimeExtension {
pub timestamp: u64,
}
impl Unmarshal for AbsSendTimeExtension {
fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
where
Self: Sized,
B: Buf,
{
if raw_packet.remaining() < ABS_SEND_TIME_EXTENSION_SIZE {
return Err(Error::ErrBufferTooSmall);
}
let b0 = raw_packet.get_u8();
let b1 = raw_packet.get_u8();
let b2 = raw_packet.get_u8();
let timestamp = (b0 as u64) << 16 | (b1 as u64) << 8 | b2 as u64;
Ok(AbsSendTimeExtension { timestamp })
}
}
impl MarshalSize for AbsSendTimeExtension {
fn marshal_size(&self) -> usize {
ABS_SEND_TIME_EXTENSION_SIZE
}
}
impl Marshal for AbsSendTimeExtension {
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
if buf.remaining_mut() < ABS_SEND_TIME_EXTENSION_SIZE {
return Err(Error::ErrBufferTooSmall);
}
buf.put_u8(((self.timestamp & 0xFF0000) >> 16) as u8);
buf.put_u8(((self.timestamp & 0xFF00) >> 8) as u8);
buf.put_u8((self.timestamp & 0xFF) as u8);
Ok(ABS_SEND_TIME_EXTENSION_SIZE)
}
}
impl AbsSendTimeExtension {
pub fn new(send_time_ntp: u64) -> Self {
AbsSendTimeExtension {
timestamp: send_time_ntp >> 14,
}
}
pub fn estimate(&self, receive_ntp: u64) -> u64 {
let mut ntp = receive_ntp & 0xFFFFFFC000000000 | (self.timestamp & 0xFFFFFF) << 14;
if receive_ntp < ntp {
ntp -= 0x1000000 << 14;
}
ntp
}
}