ableos_userland/libraries/messaging/src/lib.rs
2022-12-05 10:31:18 -06:00

50 lines
1,008 B
Rust

#![no_std]
#[macro_use]
extern crate alloc;
use alloc::vec::Vec;
use cryptography::hashing::checksum;
use process::PID;
use versioning::Version;
pub const VERSION: Version = Version::new(0, 1, 0);
#[repr(C)]
pub struct Message {
src: PID, // bytes 0-3
dest: PID, // bytes 4-7
version: Version, // bytes 8-10
checksum: u64, // bytes 11-19
length: usize, // bytes 20-23
timestamp: u64, // bytes 24-31
body: Vec<u8>,
}
impl Message {
pub fn set_checksum(&mut self) {
self.checksum = self.compute_body_checksum()
}
fn compute_body_checksum(&mut self) -> u64 {
checksum(&self.body)
}
}
#[test]
fn test_checksum() {
let mut message = Message {
src: 123,
dest: 456,
version: Version::new(0, 1, 0),
checksum: 0,
timestamp: 0,
length: 5,
body: vec![1, 2],
};
message.set_checksum();
assert_eq!(message.checksum, 3);
assert_ne!(message.checksum, 1)
}