use super::super::types::ChecksumType;
use super::super::Packet;
impl Packet {
pub fn is_checksum_correct(&self) -> bool {
self.calculate_packet_sum() == self.checksum
}
fn calculate_packet_sum(&self) -> ChecksumType {
let mut result: ChecksumType = 0;
for byte in self.source_device_identifier.to_be_bytes() {
(result, _) = result.overflowing_add(byte);
}
for byte in self.destination_device_identifier.to_be_bytes() {
(result, _) = result.overflowing_add(byte);
}
for byte in self.id.to_be_bytes() {
(result, _) = result.overflowing_add(byte);
}
for byte in self.lifetime.to_be_bytes() {
(result, _) = result.overflowing_add(byte);
}
for byte in self.flags.to_be_bytes() {
(result, _) = result.overflowing_add(byte);
}
for byte in self.data_length.to_be_bytes() {
(result, _) = result.overflowing_add(byte);
}
for byte in self.data.iter() {
(result, _) = result.overflowing_add(*byte);
}
result
}
pub fn summarized(mut self) -> Packet {
self.checksum = self.calculate_packet_sum();
self
}
}