X-Git-Url: https://code.octet-stream.net/m17rt/blobdiff_plain/9058451e46e4d36264282abe381aa9b6fd2c773f..1a444762d8fd7d48e4f56a87c6bd77f837522d5d:/m17core/src/protocol.rs?ds=sidebyside diff --git a/m17core/src/protocol.rs b/m17core/src/protocol.rs index abc9e1b..1c9fa13 100755 --- a/m17core/src/protocol.rs +++ b/m17core/src/protocol.rs @@ -1,9 +1,14 @@ -use crate::address::{encode_address, Address}; +use crate::{ + address::{encode_address, Address}, + bits::BitsMut, +}; pub(crate) const LSF_SYNC: [i8; 8] = [1, 1, 1, 1, -1, -1, 1, -1]; pub(crate) const BERT_SYNC: [i8; 8] = [-1, 1, -1, -1, 1, 1, 1, 1]; pub(crate) const STREAM_SYNC: [i8; 8] = [-1, -1, -1, -1, 1, 1, -1, 1]; pub(crate) const PACKET_SYNC: [i8; 8] = [1, -1, 1, 1, -1, -1, -1, -1]; +pub(crate) const PREAMBLE: [i8; 8] = [1, -1, 1, -1, 1, -1, 1, -1]; +pub(crate) const END_OF_TRANSMISSION: [i8; 8] = [1, 1, 1, 1, 1, 1, -1, 1]; #[derive(Debug, Clone, PartialEq, Eq, Copy)] pub enum Mode { @@ -93,6 +98,26 @@ impl PacketType { pub struct LsfFrame(pub [u8; 30]); impl LsfFrame { + pub fn new_voice(source: &Address, destination: &Address) -> Self { + let mut out = Self([0u8; 30]); + out.set_source(source); + out.set_destination(destination); + out.set_mode(Mode::Stream); + out.set_data_type(DataType::Voice); + out.set_encryption_type(EncryptionType::None); + out + } + + pub fn new_packet(source: &Address, destination: &Address) -> Self { + let mut out = Self([0u8; 30]); + out.set_source(source); + out.set_destination(destination); + out.set_mode(Mode::Packet); + out.set_data_type(DataType::Data); + out.set_encryption_type(EncryptionType::None); + out + } + /// Calculate crc of entire frame. If zero, it is a valid frame. pub fn check_crc(&self) -> u16 { crate::crc::m17_crc(&self.0) @@ -187,6 +212,15 @@ impl LsfFrame { self.recalculate_crc(); } + pub fn set_channel_access_number(&mut self, number: u8) { + let mut bits = BitsMut::new(&mut self.0); + bits.set_bit(12 * 8 + 5, (number >> 3) & 1); + bits.set_bit(12 * 8 + 6, (number >> 2) & 1); + bits.set_bit(12 * 8 + 7, (number >> 1) & 1); + bits.set_bit(13 * 8 + 0, number & 1); + self.recalculate_crc(); + } + fn recalculate_crc(&mut self) { let new_crc = crate::crc::m17_crc(&self.0[0..28]); self.0[28..30].copy_from_slice(&new_crc.to_be_bytes()); @@ -273,3 +307,15 @@ impl Default for LichCollection { Self::new() } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn set_can() { + let mut frame = LsfFrame([0u8; 30]); + frame.set_channel_access_number(11); + assert_eq!(frame.channel_access_number(), 11); + } +}