Skip to main content

openipc_core/
channel.rs

1/// OpenIPC's observed default link id from the reference browser receiver.
2///
3/// The Zig project notes this as the SHA1-derived id for
4/// `link_domain = "default"`.
5pub const DEFAULT_LINK_ID: u32 = 7_669_206;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub enum RadioPort {
9    Video,
10    MavlinkRx,
11    MavlinkTx,
12    Custom(u8),
13}
14
15impl RadioPort {
16    pub const fn as_u8(self) -> u8 {
17        match self {
18            Self::Video => 0,
19            Self::MavlinkRx => 32,
20            Self::MavlinkTx => 160,
21            Self::Custom(value) => value,
22        }
23    }
24}
25
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
27pub struct ChannelId(u32);
28
29impl ChannelId {
30    pub const fn new(raw: u32) -> Self {
31        Self(raw)
32    }
33
34    pub const fn from_link_port(link_id: u32, port: RadioPort) -> Self {
35        Self((link_id << 8) | port.as_u8() as u32)
36    }
37
38    pub const fn default_video() -> Self {
39        Self::from_link_port(DEFAULT_LINK_ID, RadioPort::Video)
40    }
41
42    pub const fn raw(self) -> u32 {
43        self.0
44    }
45
46    pub const fn to_be_bytes(self) -> [u8; 4] {
47        self.0.to_be_bytes()
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn default_video_channel_matches_reference_value() {
57        let id = ChannelId::default_video();
58        assert_eq!(id.raw(), (DEFAULT_LINK_ID << 8));
59        assert_eq!(id.to_be_bytes(), id.raw().to_be_bytes());
60    }
61}