-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathprimaryheader.rs
More file actions
100 lines (82 loc) · 3.22 KB
/
primaryheader.rs
File metadata and controls
100 lines (82 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// Copyright (C) 2019 Kubos Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//! Packet Definition for SpacePacket
// use crate::packet::{LinkPacket, PayloadType};
// use crate::CommsResult;
use crate::types;
use deku::prelude::*;
#[derive(Eq, Debug, PartialEq, Clone, DekuRead, DekuWrite)]
pub struct PrimaryHeader {
/// Packet Version Number - 3 bits
#[deku(bits = "3")]
pub version: u8,
/// Packet Type - 1 bit
pub packet_type: types::PacketType,
/// Secondary Header Flag - 1 bit
pub sec_header_flag: types::SecondaryHeaderFlag,
/// Application Process ID - 11 bits
#[deku(bits = "11", endian = "big")]
pub app_proc_id: u16,
/// Sequence Flags - 2 bits
pub sequence_flags: types::SeqFlag,
/// Packet Sequence Count or Packet Name - 14 bits
#[deku(bits = "14", endian = "big")]
pub sequence_count: u16,
/// Packet Data Length - 2 bytes
#[deku(endian = "big")]
pub data_length: u16,
// TODO: maybe figure out if https://docs.rs/deku/0.12.4/deku/#vec is appropriate here
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_python_spacepacket_primary_header() {
//this is the equivalent of an all-zero primary header except for a data length of 64 followed by two bytes set to all 1 as a "payload"
let raw = b"\x00\x00\xc0\x00\x00\x40\xff\xff";
let expected = PrimaryHeader {
version: 0,
packet_type: types::PacketType::Data,
sec_header_flag: types::SecondaryHeaderFlag::NotPresent,
app_proc_id: 0,
sequence_flags: types::SeqFlag::Unsegmented,
sequence_count: 0,
data_length: 64,
};
let (rest, parsed) = PrimaryHeader::from_bytes((raw, 0)).expect("failed to parse header");
assert_eq!(parsed, expected);
assert_eq!(rest.0, [255, 255])
}
#[test]
fn parse_incomplete_primary_header() {
//this is the equivalent of an all-zero primary header except for a data length of 64 followed by two bytes set to all 1 as a "payload"
let raw_short = b"\x00\x00\xc0\x00\x00";
let expected = PrimaryHeader {
version: 0,
packet_type: types::PacketType::Data,
sec_header_flag: types::SecondaryHeaderFlag::NotPresent,
app_proc_id: 0,
sequence_flags: types::SeqFlag::Unsegmented,
sequence_count: 0,
data_length: 64,
};
let inc = PrimaryHeader::from_bytes((raw_short, 0));
assert_eq!(inc.is_err(), true);
let raw = b"\x00\x00\xc0\x00\x00\x40";
let (_rest, parsed) = PrimaryHeader::from_bytes((raw, 0)).expect("failed to parse header");
assert_eq!(parsed, expected);
}
}