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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use core::marker::PhantomData;

use crate::protocol::{Behold, ComponentId, Sequence, Sequencer, SystemId};

use crate::prelude::*;

/// MAVLink device `ID`.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MavLinkId {
    /// System `ID`.
    pub system: SystemId,
    /// Component `ID`.
    pub component: ComponentId,
}

/// MAVLink device with defined `ID` and internal frame sequence counter.
///
/// # Examples
///
/// ```no_run
/// use mavio::dialects::minimal::messages::Heartbeat;
/// use mavio::prelude::*;
///
/// // Create a `MAVLink2` device with system and component ids
/// let device = Endpoint::v2(MavLinkId::new(17, 42));
/// device.advance(3).discard();
///
/// // Build a new frame from the provided message
/// let frame = device.next_frame(&Heartbeat::default()).unwrap();
///
/// assert_eq!(frame.sequence(), 3, "should be correct sequence number");
/// assert_eq!(frame.system_id(), 17, "should be the defined system `ID`");
/// assert_eq!(frame.component_id(), 42, "should be the defined component `ID`");
/// ```
#[derive(Debug)]
pub struct Endpoint<V: MaybeVersioned> {
    id: MavLinkId,
    sequencer: Sequencer,
    _version: PhantomData<V>,
}

impl MavLinkId {
    /// Creates a new `ID` from the combination of MAVLink system and component ids.
    pub fn new(system: SystemId, component: ComponentId) -> Self {
        Self { system, component }
    }
}

impl Endpoint<Versionless> {
    /// Creates a new device with specified [`MavLinkId`].
    pub fn new<V: MaybeVersioned>(id: MavLinkId) -> Endpoint<V> {
        Endpoint {
            id,
            sequencer: Sequencer::new(),
            _version: PhantomData,
        }
    }

    /// Creates a MAVLink1 device with specified [`MavLinkId`].
    #[inline]
    pub fn v1(id: MavLinkId) -> Endpoint<V1> {
        Endpoint::new(id)
    }

    /// Creates a MAVLink2 device with specified [`MavLinkId`].
    #[inline]
    pub fn v2(id: MavLinkId) -> Endpoint<V2> {
        Endpoint::new(id)
    }

    /// Creates a device without a specified MAVLink protocol version.
    #[inline]
    pub fn versionless(id: MavLinkId) -> Endpoint<Versionless> {
        Endpoint::new(id)
    }

    /// Produces a next versionless frame from MAVLink message.
    ///
    /// The actual protocol version still has to be specified as a generic parameter using
    /// [turbofish](https://turbo.fish/about) syntax.
    pub fn next_frame<V: Versioned>(&self, message: &dyn Message) -> Result<Frame<Versionless>> {
        Ok(self._next_frame::<V>(message)?.into_versionless())
    }
}

impl<V: MaybeVersioned> Endpoint<V> {
    /// Device `ID`.
    #[inline(always)]
    pub fn id(&self) -> MavLinkId {
        self.id
    }

    /// MAVLink system `ID`.
    #[inline(always)]
    pub fn system_id(&self) -> SystemId {
        self.id.system
    }

    /// MAVLink component `ID`.
    #[inline(always)]
    pub fn component_id(&self) -> ComponentId {
        self.id.component
    }

    /// Next MAVLink frame sequence.
    #[inline(always)]
    pub fn next_sequence(&self) -> Sequence {
        self.sequencer.next()
    }

    /// Returns a reference to internal [`Sequencer`].
    #[inline(always)]
    pub fn sequencer(&self) -> &Sequencer {
        &self.sequencer
    }

    /// Skips `increment` items in sequence and return the updated current value.
    ///
    /// The return value is wrapped in [`Behold`] since it is not guaranteed in multithreaded
    /// environments, that the [`Endpoint::next_frame`] will use the same value of a sequence in
    /// this thread.
    pub fn advance(&self, increment: Sequence) -> Behold<Sequence> {
        self.sequencer.advance(increment)
    }

    /// Forks existing endpoint.
    ///
    /// Forking is similar to cloning, except the internal frame [`Sequencer`] will be forked to
    /// start from the next value. This method is available for all targets, while cloning is
    /// possible only for `alloc` targets.
    ///
    /// See [`Sequencer::fork`] for details.
    pub fn fork(&self) -> Self {
        Self {
            id: self.id,
            sequencer: self.sequencer.fork(),
            _version: PhantomData,
        }
    }

    /// Synchronizes this endpoint with another one.
    ///
    /// Synchronizes internal sequencer with the sequencer of the `other` [`Endpoint`].
    ///
    /// See [`Sequencer::sync`] for details.
    pub fn sync<Version: MaybeVersioned>(&self, other: &Endpoint<Version>) {
        self.sequencer.sync(other.sequencer())
    }

    /// <sup>`alloc`</sup>
    /// Joins another endpoint with current one.
    ///
    /// From this moment internal sequencers will share the same counter. The current sequencer will
    /// be synced with the one it joins.
    ///
    /// Available only when `alloc` feature is enabled.
    ///
    /// See [`Sequencer::join`] for details.
    #[cfg(feature = "alloc")]
    pub fn join<Version: MaybeVersioned>(&self, other: &mut Endpoint<Version>) {
        self.sequencer.join(&mut other.sequencer)
    }

    fn _next_frame<Version: Versioned>(&self, message: &dyn Message) -> Result<Frame<Version>> {
        let frame = Frame::builder()
            .sequence(self.next_sequence())
            .system_id(self.system_id())
            .component_id(self.component_id())
            .version(Version::v())
            .message(message)?
            .build();
        Ok(frame)
    }
}

impl<V: Versioned> Endpoint<V> {
    /// Produces a next frame from MAVLink message.
    pub fn next_frame(&self, message: &dyn Message) -> Result<Frame<V>> {
        self._next_frame::<V>(message)
    }
}

#[cfg(feature = "alloc")]
impl<V: MaybeVersioned> Clone for Endpoint<V> {
    fn clone(&self) -> Self {
        Self {
            id: self.id,
            sequencer: self.sequencer.clone(),
            _version: PhantomData,
        }
    }
}