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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
Copyright 2020 Timo Saarinen

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.
*/

use super::*;

// -------------------------------------------------------------------------------------------------

/// Type 20: Data Link Management Message
#[derive(Clone, Debug, PartialEq)]
pub struct DataLinkManagementMessage {
    /// True if the data is about own vessel, false if about other.
    pub own_vessel: bool,

    /// AIS station type.
    pub station: Station,

    /// Interrogation case based on data length
    pub case: InterrogationCase,

    /// Source MMSI (30 bits)
    pub mmsi: u32,

    /// Offset number 1 (12 bits)
    pub offset1: u16,

    /// Reserved offset number (4)
    pub number1: u8,

    /// Allocation timeout in munites (4 bits)
    pub timeout1: u8,

    /// Repeat increment (11 bits)
    pub increment1: u8,

    /// Offset number 2 (12 bits)
    pub offset2: u16,

    /// Reserved offset number (4)
    pub number2: u8,

    /// Allocation timeout in munites (4 bits)
    pub timeout2: u8,

    /// Repeat increment (11 bits)
    pub increment2: u8,

    /// Offset number 3 (12 bits)
    pub offset3: u16,

    /// Reserved offset number (4)
    pub number3: u8,

    /// Allocation timeout in munites (4 bits)
    pub timeout3: u8,

    /// Repeat increment (11 bits)
    pub increment3: u8,

    /// Offset number 4 (12 bits)
    pub offset4: u16,

    /// Reserved offset number (4)
    pub number4: u8,

    /// Allocation timeout in munites (4 bits)
    pub timeout4: u8,

    /// Repeat increment (11 bits)
    pub increment4: u8,
}

// -------------------------------------------------------------------------------------------------

/// AIS VDM/VDO type 20: Data Link Management Message
pub(crate) fn handle(
    bv: &BitVec,
    station: Station,
    own_vessel: bool,
) -> Result<ParsedMessage, ParseError> {
    let case = InterrogationCase::new(bv);
    Ok(ParsedMessage::DataLinkManagementMessage(
        DataLinkManagementMessage {
            own_vessel,
            station,
            case,
            mmsi: { pick_u64(&bv, 8, 30) as u32 },
            offset1: { pick_u64(&bv, 40, 12) as u16 },
            number1: { pick_u64(&bv, 52, 4) as u8 },
            timeout1: { pick_u64(&bv, 56, 3) as u8 },
            increment1: { pick_u64(&bv, 59, 11) as u8 },
            offset2: { pick_u64(&bv, 70, 12) as u16 },
            number2: { pick_u64(&bv, 82, 4) as u8 },
            timeout2: { pick_u64(&bv, 86, 3) as u8 },
            increment2: { pick_u64(&bv, 89, 11) as u8 },
            offset3: { pick_u64(&bv, 100, 12) as u16 },
            number3: { pick_u64(&bv, 112, 4) as u8 },
            timeout3: { pick_u64(&bv, 116, 3) as u8 },
            increment3: { pick_u64(&bv, 119, 11) as u8 },
            offset4: { pick_u64(&bv, 130, 12) as u16 },
            number4: { pick_u64(&bv, 142, 4) as u8 },
            timeout4: { pick_u64(&bv, 146, 3) as u8 },
            increment4: { pick_u64(&bv, 149, 11) as u8 },
        },
    ))
}

// -------------------------------------------------------------------------------------------------

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_parse_vdm_type20() {
        let mut p = NmeaParser::new();
        match p.parse_sentence("!AIVDM,1,1,,A,Dh3OvjB8IN>4,0*1D") {
            Ok(ps) => {
                match ps {
                    // The expected result
                    ParsedMessage::DataLinkManagementMessage(dlmm) => {
                        assert_eq!(dlmm.mmsi, 3669705);
                        assert_eq!(dlmm.offset1, 2182);
                        assert_eq!(dlmm.number1, 5);
                        assert_eq!(dlmm.timeout1, 7);
                        assert_eq!(dlmm.increment1, 225);
                    }
                    ParsedMessage::Incomplete => {
                        assert!(false);
                    }
                    _ => {
                        assert!(false);
                    }
                }
            }
            Err(e) => {
                assert_eq!(e.to_string(), "OK");
            }
        }
    }
}