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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//     dis-rust - A rust implementation of the DIS simulation protocol.
//     Copyright (C) 2022 Thomas Mann
// 
//     This software is dual-licensed. It is available under the conditions of
//     the GNU Affero General Public License (see the LICENSE file included) 
//     or under a commercial license (email contact@coffeebreakdevs.com for
//     details).

use std::u8;

use bytes::{BytesMut, BufMut};
use chrono::{Utc, Timelike};
use num_derive::FromPrimitive;    

#[derive(Copy, Clone, Debug, PartialEq)]
/// PDU Header Record as defined in IEEE 1278.1 standard. Used to communicate PDU information during the simulation.
pub struct PDUHeaderRecord {
    pub protocol_version: u8,
    pub exercise_id: u8,
    pub pdu_type: u8,
    pub protocol_family: u8,
    pub timestamp: u32,
    pub length: u16,
    pub padding: u16
}

impl PDUHeaderRecord {
    /// Provides a function to create a new PDUHeaderRecord. 
    /// Timestamps are automatically generated using the current system time.
    pub fn new(pdu_type: PDUType, protocol_family: ProtocolFamily, exercise_id: u8, length: u16,) -> Self {
        PDUHeaderRecord {
            protocol_version: ProtocolVersion::DIS_PDUv2_Fourth_Draft_Revised as u8,
            exercise_id,
            pdu_type: pdu_type as u8,
            protocol_family: protocol_family as u8,
            timestamp: PDUHeaderRecord::calculate_dis_timestamp() as u32,
            length: length as u16,
            padding: 0 as u16
        }
    }

    /// Provides a function to create a default PDUHeaderRecord. The header uses exercise ID 1.
    /// Timestamps are automatically generated using the current system time.
    pub fn default(pdu_type: PDUType, protocol_family: ProtocolFamily, length: u16,) -> Self {
        PDUHeaderRecord {
            protocol_version: ProtocolVersion::DIS_PDUv2_Fourth_Draft_Revised as u8,
            exercise_id: 1,
            pdu_type: pdu_type as u8,
            protocol_family: protocol_family as u8,
            timestamp: PDUHeaderRecord::calculate_dis_timestamp() as u32,
            length: length as u16,
            padding: 0 as u16
        }
    }

    /// Calculates the timestamp in DIS time units (1.68 micro-seconds) within a micro-second
    fn calculate_dis_timestamp() -> u32 {
        let current_minute = ((Utc::now().minute() * 60) * 1e6 as u32) as u64 ;
        let current_second = (Utc::now().second() * 1e6 as u32) as u64;
        let current_nanos = (Utc::now().nanosecond() / 1000) as u64;
        let dis_units = (current_second + current_minute + current_nanos) as f32 / 1.68;
        dis_units as u32
    }

    /// Fills a BytesMut struct with a PDUHeaderRecord serialised into binary. This buffer is then ready to be sent via
    /// UDP to the simluation network.
    pub fn serialize(&self, buf: &mut BytesMut) {
        buf.put_u8(self.protocol_version);
        buf.put_u8(self.exercise_id);
        buf.put_u8(self.pdu_type);
        buf.put_u8(self.protocol_family);
        buf.put_u32(self.timestamp);
        buf.put_u16(self.length);
        buf.put_u16(self.padding);
    }
    
}


#[derive(Copy, Clone, Debug, FromPrimitive, PartialEq)]
/// Enum to represent the Protocol Family the PDU header is recording.
pub enum ProtocolFamily {
    Other = 0,
    EntityInformation = 1,
    Warfare = 2,
    Logistics = 3,
    RadioCommunications = 4,
    SimulationManagement = 5,
    DistributedEmissionRegeneration = 6
}

#[derive(Copy, Clone, Debug, FromPrimitive, PartialEq)]
#[allow(non_camel_case_types)]
/// Enum to represent the Protocol Version the PDU header is recording.
pub enum ProtocolVersion {
    Other = 0,
    DIS_PDUv1 = 1,
    IEEE1278 = 2,
    DIS_PDUv2_Third_Draft = 3,
    DIS_PDUv2_Fourth_Draft_Revised = 4,
    IEEE1278_1 = 5
}

#[derive(Copy, Clone, Debug, FromPrimitive, PartialEq)]
/// Enum to represent the PDU Type the PDU header is recording.
pub enum PDUType {
    Other = 0,
    EntityState = 1,
    Fire = 2,
    Detonation = 3,
    Collision = 4,
    ServiceRequest = 5,
    ResupplyOffer = 6,
    ResupplyReceived = 7,
    ResupplyCancel = 8,
    RepairComplete = 9,
    RepairRepsonse = 10,
    CreateEntity = 11,
    RemoveEntity = 12,
    StartResume = 13,
    StopFreeze = 14,
    Acknowledge = 15,
    ActionRequest = 16,
    ActionReponse = 17,
    DataQuery = 18,
    SetData = 19,
    Data = 20,
    EventReport = 21,
    Comment = 22,
    ElectromagneticEmission = 23,
    Designator = 24,
    Transmitter = 25,
    Signal = 26,
    Recevier = 27,
    // ..
}