]>
code.octet-stream.net Git - m17rt/blob - m17core/src/protocol.rs
1 use crate::address
::{encode_address
, Address
};
3 pub(crate) const LSF_SYNC
: [i8; 8] = [1, 1, 1, 1, -1, -1, 1, -1];
4 pub(crate) const BERT_SYNC
: [i8; 8] = [-1, 1, -1, -1, 1, 1, 1, 1];
5 pub(crate) const STREAM_SYNC
: [i8; 8] = [-1, -1, -1, -1, 1, 1, -1, 1];
6 pub(crate) const PACKET_SYNC
: [i8; 8] = [1, -1, 1, 1, -1, -1, -1, -1];
8 #[derive(Debug, Clone, PartialEq, Eq, Copy)]
13 #[derive(Debug, Clone, PartialEq, Eq, Copy)]
20 #[derive(Debug, Clone, PartialEq, Eq, Copy)]
21 pub enum EncryptionType
{
28 #[derive(Debug, Clone, PartialEq, Eq)]
36 #[derive(Debug, Clone, PartialEq, Eq, Copy)]
57 pub fn from_proto(buf
: &[u8]) -> Option
<(Self, usize)> {
60 .and_then(|chunk
| chunk
.valid().chars().next())
61 .map(|c
| match c
as u32 {
62 0x00 => (PacketType
::Raw
, 1),
63 0x01 => (PacketType
::Ax25
, 1),
64 0x02 => (PacketType
::Aprs
, 1),
65 0x03 => (PacketType
::SixLowPan
, 1),
66 0x04 => (PacketType
::Ipv4
, 1),
67 0x05 => (PacketType
::Sms
, 1),
68 0x06 => (PacketType
::Winlink
, 1),
69 _
=> (PacketType
::Other(c
), c
.len_utf8()),
73 pub fn as_proto(&self) -> ([u8; 4], usize) {
75 PacketType
::Raw
=> ([0, 0, 0, 0], 1),
76 PacketType
::Ax25
=> ([1, 0, 0, 0], 1),
77 PacketType
::Aprs
=> ([2, 0, 0, 0], 1),
78 PacketType
::SixLowPan
=> ([3, 0, 0, 0], 1),
79 PacketType
::Ipv4
=> ([4, 0, 0, 0], 1),
80 PacketType
::Sms
=> ([5, 0, 0, 0], 1),
81 PacketType
::Winlink
=> ([6, 0, 0, 0], 1),
82 PacketType
::Other(c
) => {
83 let mut buf
= [0u8; 4];
84 let s
= c
.encode_utf8(&mut buf
);
92 #[derive(Debug, Clone, PartialEq, Eq)]
93 pub struct LsfFrame(pub [u8; 30]);
96 /// Calculate crc of entire frame. If zero, it is a valid frame.
97 pub fn check_crc(&self) -> u16 {
98 crate::crc
::m17_crc(&self.0)
101 pub fn destination(&self) -> Address
{
102 crate::address
::decode_address((&self.0[0..6]).try_into().unwrap
())
105 pub fn source(&self) -> Address
{
106 crate::address
::decode_address((&self.0[6..12]).try_into().unwrap
())
109 pub fn mode(&self) -> Mode
{
110 if self.lsf_type() & 0x0001 > 0 {
117 pub fn data_type(&self) -> DataType
{
118 match (self.0[12] >> 1) & 0x03 {
119 0b00 => DataType
::Reserved
,
120 0b01 => DataType
::Data
,
121 0b10 => DataType
::Voice
,
122 0b11 => DataType
::VoiceAndData
,
127 pub fn encryption_type(&self) -> EncryptionType
{
128 match (self.lsf_type() >> 3) & 0x0003 {
129 0b00 => EncryptionType
::None
,
130 0b01 => EncryptionType
::Scrambler
,
131 0b10 => EncryptionType
::Aes
,
132 0b11 => EncryptionType
::Other
,
137 // TODO: encryption sub-type
139 pub fn channel_access_number(&self) -> u8 {
140 ((self.lsf_type() >> 7) & 0x000f) as u8
143 pub fn meta(&self) -> [u8; 14] {
144 self.0[14..28].try_into().unwrap
()
147 pub fn set_destination(&mut self, destination
: &Address
) {
148 self.0[0..6].copy_from_slice(&encode_address(&destination
));
149 self.recalculate_crc();
152 pub fn set_source(&mut self, source
: &Address
) {
153 self.0[6..12].copy_from_slice(&encode_address(&source
));
154 self.recalculate_crc();
157 pub fn set_mode(&mut self, mode
: Mode
) {
158 let existing_type
= self.lsf_type();
159 let new_type
= (existing_type
& !0x0001) | if mode
== Mode
::Stream
{ 1 } else { 0 };
160 self.0[12..14].copy_from_slice(&new_type
.to_be_bytes());
161 self.recalculate_crc();
164 pub fn set_data_type(&mut self, data_type
: DataType
) {
165 let type_part
= match data_type
{
166 DataType
::Reserved
=> 0b00 << 1,
167 DataType
::Data
=> 0b01 << 1,
168 DataType
::Voice
=> 0b10 << 1,
169 DataType
::VoiceAndData
=> 0b11 << 1,
171 let existing_type
= self.lsf_type();
172 let new_type
= (existing_type
& !0x0006) | type_part
;
173 self.0[12..14].copy_from_slice(&new_type
.to_be_bytes());
174 self.recalculate_crc();
177 pub fn set_encryption_type(&mut self, encryption_type
: EncryptionType
) {
178 let type_part
= match encryption_type
{
179 EncryptionType
::None
=> 0b00 << 3,
180 EncryptionType
::Scrambler
=> 0b01 << 3,
181 EncryptionType
::Aes
=> 0b10 << 3,
182 EncryptionType
::Other
=> 0b11 << 3,
184 let existing_type
= self.lsf_type();
185 let new_type
= (existing_type
& !0x0018) | type_part
;
186 self.0[12..14].copy_from_slice(&new_type
.to_be_bytes());
187 self.recalculate_crc();
190 fn recalculate_crc(&mut self) {
191 let new_crc
= crate::crc
::m17_crc(&self.0[0..28]);
192 self.0[28..30].copy_from_slice(&new_crc
.to_be_bytes());
193 debug_assert_eq
!(self.check_crc(), 0);
196 fn lsf_type(&self) -> u16 {
197 u16::from_be_bytes([self.0[12], self.0[13]])
201 #[derive(Debug, Clone, PartialEq, Eq, Default)]
202 pub struct StreamFrame
{
203 /// Which LICH segment is given in this frame, from 0 to 5 inclusive
205 /// Decoded LICH segment
206 pub lich_part
: [u8; 5],
207 /// Which frame in the transmission this is, starting from 0
208 pub frame_number
: u16,
209 /// Is this the last frame in the transmission?
210 pub end_of_stream
: bool
,
211 /// Raw application data in this frame
212 pub stream_data
: [u8; 16],
215 #[derive(Debug, Clone, PartialEq, Eq)]
216 pub struct PacketFrame
{
217 /// Application packet payload (chunk)
218 pub payload
: [u8; 25],
220 /// Frame counter, which provides different information depending on whether this is the last frame or not.
221 pub counter
: PacketFrameCounter
,
224 #[derive(Debug, Clone, PartialEq, Eq)]
225 pub enum PacketFrameCounter
{
226 /// Any packet frame that comes after the LSF and is not the final frame.
228 /// Which frame this is in the superframe, from 0 to 31 inclusive.
230 /// If a 33rd frame exists (index 32), it will be a `FinalFrame` instead.
232 /// All 25 bytes of of `payload` are filled and valid.
235 /// The final frame in the packet superframe.
237 /// The number of bytes in `payload` that are filled.
242 pub struct LichCollection([Option
<[u8; 5]>; 6]);
244 impl LichCollection
{
245 pub fn new() -> Self {
249 pub fn valid_segments(&self) -> usize {
250 self.0.iter
().filter
(|s
| s
.is
_some
()).count()
253 pub fn set_segment(&mut self, counter
: u8, part
: [u8; 5]) {
254 self.0[counter
as usize] = Some(part
);
257 pub fn try_assemble(&self) -> Option
<[u8; 30]> {
258 let mut out
= [0u8; 30];
259 for (i
, segment
) in self.0.iter
().enumerate() {
260 let Some(segment
) = segment
else {
263 for (j
, seg_val
) in segment
.iter
().enumerate() {
264 out
[i
* 5 + j
] = *seg_val
;
271 impl Default
for LichCollection
{
272 fn default() -> Self {