use crate::{Error, MESSAGE_SIZE, PROTOCOL_VERSION};
macro_rules! u8_enum {
(
$(#[$meta:meta])*
pub enum $name:ident { $( $(#[$vmeta:meta])* $var:ident = $val:literal ),* $(,)? }
) => {
$(#[$meta])*
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum $name {
$( $(#[$vmeta])* $var, )*
Other(u8),
}
impl From<u8> for $name {
fn from(v: u8) -> Self {
match v { $( $val => $name::$var, )* o => $name::Other(o) }
}
}
impl From<$name> for u8 {
fn from(v: $name) -> u8 {
match v { $( $name::$var => $val, )* $name::Other(o) => o }
}
}
};
}
u8_enum! {
pub enum MessageType {
BasicId = 0,
Location = 1,
Auth = 2,
SelfId = 3,
System = 4,
OperatorId = 5,
MessagePack = 15,
}
}
u8_enum! {
pub enum IdType {
None = 0,
SerialNumber = 1,
CaaRegistrationId = 2,
UtmAssignedUuid = 3,
SpecificSessionId = 4,
}
}
u8_enum! {
pub enum UaType {
None = 0,
Aeroplane = 1,
HelicopterOrMultirotor = 2,
Gyroplane = 3,
HybridLift = 4,
Ornithopter = 5,
Glider = 6,
Kite = 7,
FreeBalloon = 8,
CaptiveBalloon = 9,
Airship = 10,
FreeFallParachute = 11,
Rocket = 12,
TetheredPoweredAircraft = 13,
GroundObstacle = 14,
Unspecified = 15,
}
}
u8_enum! {
pub enum Status {
Undeclared = 0,
Ground = 1,
Airborne = 2,
Emergency = 3,
RemoteIdSystemFailure = 4,
}
}
u8_enum! {
pub enum HeightType {
AboveTakeoff = 0,
AboveGround = 1,
}
}
u8_enum! {
pub enum AuthType {
None = 0,
UasIdSignature = 1,
OperatorIdSignature = 2,
MessageSetSignature = 3,
NetworkRemoteId = 4,
SpecificAuthentication = 5,
}
}
u8_enum! {
pub enum DescriptionType {
Text = 0,
Emergency = 1,
ExtendedStatus = 2,
}
}
u8_enum! {
pub enum OperatorLocationType {
Takeoff = 0,
LiveGnss = 1,
Fixed = 2,
}
}
u8_enum! {
pub enum ClassificationType {
Undeclared = 0,
Eu = 1,
}
}
u8_enum! {
pub enum CategoryEu {
Undeclared = 0,
Open = 1,
Specific = 2,
Certified = 3,
}
}
u8_enum! {
pub enum ClassEu {
Undeclared = 0,
Class0 = 1,
Class1 = 2,
Class2 = 3,
Class3 = 4,
Class4 = 5,
Class5 = 6,
Class6 = 7,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HorizontalAccuracy(pub u8);
impl HorizontalAccuracy {
pub fn meters(self) -> Option<f32> {
Some(match self.0 {
1 => 18520.0,
2 => 7408.0,
3 => 3704.0,
4 => 1852.0,
5 => 926.0,
6 => 555.6,
7 => 185.2,
8 => 92.6,
9 => 30.0,
10 => 10.0,
11 => 3.0,
12 => 1.0,
_ => return None,
})
}
pub fn from_meters(meters: f32) -> Self {
let table = [
1.0, 3.0, 10.0, 30.0, 92.6, 185.2, 555.6, 926.0, 1852.0, 3704.0, 7408.0, 18520.0,
];
for (i, bound) in table.iter().enumerate() {
if meters < *bound {
return HorizontalAccuracy(12 - i as u8);
}
}
HorizontalAccuracy(0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct VerticalAccuracy(pub u8);
impl VerticalAccuracy {
pub fn meters(self) -> Option<f32> {
Some(match self.0 {
1 => 150.0,
2 => 45.0,
3 => 25.0,
4 => 10.0,
5 => 3.0,
6 => 1.0,
_ => return None,
})
}
pub fn from_meters(meters: f32) -> Self {
let table = [1.0, 3.0, 10.0, 25.0, 45.0, 150.0];
for (i, bound) in table.iter().enumerate() {
if meters < *bound {
return VerticalAccuracy(6 - i as u8);
}
}
VerticalAccuracy(0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SpeedAccuracy(pub u8);
impl SpeedAccuracy {
pub fn meters_per_second(self) -> Option<f32> {
Some(match self.0 {
1 => 10.0,
2 => 3.0,
3 => 1.0,
4 => 0.3,
_ => return None,
})
}
pub fn from_meters_per_second(mps: f32) -> Self {
let table = [0.3, 1.0, 3.0, 10.0];
for (i, bound) in table.iter().enumerate() {
if mps < *bound {
return SpeedAccuracy(4 - i as u8);
}
}
SpeedAccuracy(0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TimestampAccuracy(pub u8);
impl TimestampAccuracy {
pub fn seconds(self) -> Option<f32> {
match self.0 {
1..=15 => Some(self.0 as f32 / 10.0),
_ => None,
}
}
}
const LATLON_MULT: f64 = 10_000_000.0;
pub const EPOCH_2019_UNIX: u64 = 1_546_300_800;
fn round_f32(x: f32) -> i64 {
if x >= 0.0 {
(x as f64 + 0.5) as i64
} else {
(x as f64 - 0.5) as i64
}
}
fn round_f64(x: f64) -> i64 {
if x >= 0.0 {
(x + 0.5) as i64
} else {
(x - 0.5) as i64
}
}
fn clamp(v: i64, lo: i64, hi: i64) -> i64 {
v.max(lo).min(hi)
}
fn decode_latlon(raw: i32) -> f64 {
raw as f64 / LATLON_MULT
}
fn encode_latlon(v: f64) -> i32 {
clamp(round_f64(v * LATLON_MULT), -1_800_000_000, 1_800_000_000) as i32
}
fn decode_altitude(raw: u16) -> Option<f32> {
if raw == 0 {
None
} else {
Some(raw as f32 * 0.5 - 1000.0)
}
}
fn encode_altitude(v: Option<f32>, field: &'static str) -> Result<u16, Error> {
match v {
None => Ok(0),
Some(m) if !(-1000.0..=31767.5).contains(&m) => Err(Error::OutOfRange(field)),
Some(m) => Ok(clamp(round_f32((m + 1000.0) / 0.5), 0, u16::MAX as i64) as u16),
}
}
fn read_u16(b: &[u8], at: usize) -> u16 {
u16::from_le_bytes([b[at], b[at + 1]])
}
fn read_i32(b: &[u8], at: usize) -> i32 {
i32::from_le_bytes([b[at], b[at + 1], b[at + 2], b[at + 3]])
}
fn read_u32(b: &[u8], at: usize) -> u32 {
u32::from_le_bytes([b[at], b[at + 1], b[at + 2], b[at + 3]])
}
fn nibble(v: u8, field: &'static str) -> Result<u8, Error> {
if v > 15 {
Err(Error::OutOfRange(field))
} else {
Ok(v)
}
}
fn header(t: MessageType, version: u8) -> Result<u8, Error> {
Ok((u8::from(t) << 4) | nibble(version, "protocol_version")?)
}
fn padded_str(bytes: &[u8]) -> Option<&str> {
let end = bytes.iter().position(|&b| b == 0).unwrap_or(bytes.len());
let s = &bytes[..end];
if s.iter().all(|b| (0x20..0x7f).contains(b)) {
core::str::from_utf8(s).ok()
} else {
None
}
}
fn fill_padded<const N: usize>(text: &str, field: &'static str) -> Result<[u8; N], Error> {
let mut out = [0u8; N];
if text.len() > N {
return Err(Error::OutOfRange(field));
}
out[..text.len()].copy_from_slice(text.as_bytes());
Ok(out)
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Message {
BasicId(BasicId),
Location(Location),
Auth(Auth),
SelfId(SelfId),
System(System),
OperatorId(OperatorId),
}
impl Message {
pub fn decode(data: &[u8]) -> Result<Self, Error> {
if data.len() < MESSAGE_SIZE {
return Err(Error::TooShort {
need: MESSAGE_SIZE,
got: data.len(),
});
}
let raw: &[u8; MESSAGE_SIZE] = data[..MESSAGE_SIZE].try_into().expect("length checked");
Ok(match MessageType::from(raw[0] >> 4) {
MessageType::BasicId => Message::BasicId(BasicId::decode(raw)?),
MessageType::Location => Message::Location(Location::decode(raw)?),
MessageType::Auth => Message::Auth(Auth::decode(raw)?),
MessageType::SelfId => Message::SelfId(SelfId::decode(raw)?),
MessageType::System => Message::System(System::decode(raw)?),
MessageType::OperatorId => Message::OperatorId(OperatorId::decode(raw)?),
MessageType::MessagePack | MessageType::Other(_) => {
return Err(Error::UnknownMessageType(raw[0] >> 4))
}
})
}
pub fn encode(&self) -> Result<[u8; MESSAGE_SIZE], Error> {
match self {
Message::BasicId(m) => m.encode(),
Message::Location(m) => m.encode(),
Message::Auth(m) => m.encode(),
Message::SelfId(m) => m.encode(),
Message::System(m) => m.encode(),
Message::OperatorId(m) => m.encode(),
}
}
pub fn message_type(&self) -> MessageType {
match self {
Message::BasicId(_) => MessageType::BasicId,
Message::Location(_) => MessageType::Location,
Message::Auth(_) => MessageType::Auth,
Message::SelfId(_) => MessageType::SelfId,
Message::System(_) => MessageType::System,
Message::OperatorId(_) => MessageType::OperatorId,
}
}
pub fn protocol_version(&self) -> u8 {
match self {
Message::BasicId(m) => m.protocol_version,
Message::Location(m) => m.protocol_version,
Message::Auth(m) => m.protocol_version,
Message::SelfId(m) => m.protocol_version,
Message::System(m) => m.protocol_version,
Message::OperatorId(m) => m.protocol_version,
}
}
}
fn expect_type(raw: &[u8; MESSAGE_SIZE], t: MessageType) -> Result<u8, Error> {
let found = raw[0] >> 4;
if found != u8::from(t) {
return Err(Error::WrongMessageType {
expected: t.into(),
found,
});
}
Ok(raw[0] & 0x0F)
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BasicId {
pub protocol_version: u8,
pub id_type: IdType,
pub ua_type: UaType,
pub uas_id: [u8; 20],
}
impl BasicId {
pub fn new(id_type: IdType, ua_type: UaType, uas_id: &str) -> Result<Self, Error> {
Ok(BasicId {
protocol_version: PROTOCOL_VERSION,
id_type,
ua_type,
uas_id: fill_padded(uas_id, "uas_id")?,
})
}
pub fn decode(raw: &[u8; MESSAGE_SIZE]) -> Result<Self, Error> {
let protocol_version = expect_type(raw, MessageType::BasicId)?;
let mut uas_id = [0u8; 20];
uas_id.copy_from_slice(&raw[2..22]);
Ok(BasicId {
protocol_version,
id_type: IdType::from(raw[1] >> 4),
ua_type: UaType::from(raw[1] & 0x0F),
uas_id,
})
}
pub fn encode(&self) -> Result<[u8; MESSAGE_SIZE], Error> {
let mut out = [0u8; MESSAGE_SIZE];
out[0] = header(MessageType::BasicId, self.protocol_version)?;
out[1] = (nibble(self.id_type.into(), "id_type")? << 4)
| nibble(self.ua_type.into(), "ua_type")?;
out[2..22].copy_from_slice(&self.uas_id);
Ok(out)
}
pub fn uas_id_str(&self) -> Option<&str> {
padded_str(&self.uas_id)
}
pub fn serial_number(&self) -> Option<SerialNumber<'_>> {
if self.id_type != IdType::SerialNumber {
return None;
}
SerialNumber::parse(self.uas_id_str()?)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SerialNumber<'a> {
pub manufacturer_code: &'a str,
pub length: u8,
pub serial: &'a str,
}
impl<'a> SerialNumber<'a> {
pub fn parse(s: &'a str) -> Option<Self> {
let allowed =
|c: u8| (c.is_ascii_digit() || c.is_ascii_uppercase()) && c != b'O' && c != b'I';
let b = s.as_bytes();
if b.len() < 6 || b.len() > 20 {
return None;
}
let length = match b[4] {
c @ b'1'..=b'9' => c - b'0',
c @ b'A'..=b'F' => c - b'A' + 10,
_ => return None,
};
if b.len() != 5 + length as usize
|| !b[..4].iter().copied().all(allowed)
|| !b[5..].iter().copied().all(allowed)
{
return None;
}
Some(SerialNumber {
manufacturer_code: &s[..4],
length,
serial: &s[5..],
})
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Location {
pub protocol_version: u8,
pub status: Status,
pub height_type: HeightType,
pub direction: Option<f32>,
pub speed_horizontal: Option<f32>,
pub speed_vertical: Option<f32>,
pub latitude: f64,
pub longitude: f64,
pub altitude_baro: Option<f32>,
pub altitude_geo: Option<f32>,
pub height: Option<f32>,
pub horizontal_accuracy: HorizontalAccuracy,
pub vertical_accuracy: VerticalAccuracy,
pub baro_accuracy: VerticalAccuracy,
pub speed_accuracy: SpeedAccuracy,
pub timestamp_accuracy: TimestampAccuracy,
pub timestamp: Option<f32>,
}
impl Default for Location {
fn default() -> Self {
Location {
protocol_version: PROTOCOL_VERSION,
status: Status::Undeclared,
height_type: HeightType::AboveTakeoff,
direction: None,
speed_horizontal: None,
speed_vertical: None,
latitude: 0.0,
longitude: 0.0,
altitude_baro: None,
altitude_geo: None,
height: None,
horizontal_accuracy: HorizontalAccuracy(0),
vertical_accuracy: VerticalAccuracy(0),
baro_accuracy: VerticalAccuracy(0),
speed_accuracy: SpeedAccuracy(0),
timestamp_accuracy: TimestampAccuracy(0),
timestamp: None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum LocationIssue {
LatitudeOutOfRange,
LongitudeOutOfRange,
DirectionOutOfRange,
VerticalSpeedOutOfRange,
TimestampOutOfRange,
ReservedCode,
}
impl Location {
pub fn decode(raw: &[u8; MESSAGE_SIZE]) -> Result<Self, Error> {
let protocol_version = expect_type(raw, MessageType::Location)?;
let flags = raw[1];
let speed_mult = flags & 0x01 != 0;
let ew = flags & 0x02 != 0;
let dir = raw[2] as f32 + if ew { 180.0 } else { 0.0 };
let speed = if speed_mult {
raw[3] as f32 * 0.75 + 255.0 * 0.25
} else {
raw[3] as f32 * 0.25
};
let vspeed = (raw[4] as i8) as f32 * 0.5;
let ts = read_u16(raw, 21);
Ok(Location {
protocol_version,
status: Status::from(flags >> 4),
height_type: HeightType::from((flags >> 2) & 0x01),
direction: if dir == 361.0 { None } else { Some(dir) },
speed_horizontal: if speed == 255.0 { None } else { Some(speed) },
speed_vertical: if vspeed == 63.0 { None } else { Some(vspeed) },
latitude: decode_latlon(read_i32(raw, 5)),
longitude: decode_latlon(read_i32(raw, 9)),
altitude_baro: decode_altitude(read_u16(raw, 13)),
altitude_geo: decode_altitude(read_u16(raw, 15)),
height: decode_altitude(read_u16(raw, 17)),
horizontal_accuracy: HorizontalAccuracy(raw[19] & 0x0F),
vertical_accuracy: VerticalAccuracy(raw[19] >> 4),
speed_accuracy: SpeedAccuracy(raw[20] & 0x0F),
baro_accuracy: VerticalAccuracy(raw[20] >> 4),
timestamp: if ts == 0xFFFF {
None
} else {
Some(ts as f32 / 10.0)
},
timestamp_accuracy: TimestampAccuracy(raw[23] & 0x0F),
})
}
pub fn encode(&self) -> Result<[u8; MESSAGE_SIZE], Error> {
let mut out = [0u8; MESSAGE_SIZE];
out[0] = header(MessageType::Location, self.protocol_version)?;
let (dir_raw, ew) = match self.direction {
None => (181u8, true),
Some(d) if !(0.0..=360.0).contains(&d) => return Err(Error::OutOfRange("direction")),
Some(d) => {
let mut di = round_f32(d);
if di == 360 {
di = 0;
}
if di < 180 {
(di as u8, false)
} else {
((di - 180) as u8, true)
}
}
};
let (speed_raw, mult) = match self.speed_horizontal {
None => (255u8, true),
Some(s) if !(0.0..=254.25).contains(&s) => {
return Err(Error::OutOfRange("speed_horizontal"))
}
Some(s) if s <= 255.0 * 0.25 => (round_f32(s / 0.25) as u8, false),
Some(s) => (
clamp(round_f32((s - 255.0 * 0.25) / 0.75), 0, 255) as u8,
true,
),
};
let vs_raw = match self.speed_vertical {
None => 126i8,
Some(v) if !(-62.0..=62.0).contains(&v) => {
return Err(Error::OutOfRange("speed_vertical"))
}
Some(v) => clamp(round_f32(v / 0.5), -128, 127) as i8,
};
if !(-90.0..=90.0).contains(&self.latitude) {
return Err(Error::OutOfRange("latitude"));
}
if !(-180.0..=180.0).contains(&self.longitude) {
return Err(Error::OutOfRange("longitude"));
}
let ts_raw: u16 = match self.timestamp {
None => 0xFFFF,
Some(t) if !(0.0..=3600.0).contains(&t) => return Err(Error::OutOfRange("timestamp")),
Some(t) => clamp(round_f32(t * 10.0), 0, 36000) as u16,
};
out[1] = (nibble(self.status.into(), "status")? << 4)
| ((u8::from(self.height_type) & 0x01) << 2)
| ((ew as u8) << 1)
| mult as u8;
if u8::from(self.height_type) > 1 {
return Err(Error::OutOfRange("height_type"));
}
out[2] = dir_raw;
out[3] = speed_raw;
out[4] = vs_raw as u8;
out[5..9].copy_from_slice(&encode_latlon(self.latitude).to_le_bytes());
out[9..13].copy_from_slice(&encode_latlon(self.longitude).to_le_bytes());
out[13..15]
.copy_from_slice(&encode_altitude(self.altitude_baro, "altitude_baro")?.to_le_bytes());
out[15..17]
.copy_from_slice(&encode_altitude(self.altitude_geo, "altitude_geo")?.to_le_bytes());
out[17..19].copy_from_slice(&encode_altitude(self.height, "height")?.to_le_bytes());
out[19] = (nibble(self.vertical_accuracy.0, "vertical_accuracy")? << 4)
| nibble(self.horizontal_accuracy.0, "horizontal_accuracy")?;
out[20] = (nibble(self.baro_accuracy.0, "baro_accuracy")? << 4)
| nibble(self.speed_accuracy.0, "speed_accuracy")?;
out[21..23].copy_from_slice(&ts_raw.to_le_bytes());
out[23] = nibble(self.timestamp_accuracy.0, "timestamp_accuracy")?;
Ok(out)
}
pub fn position(&self) -> Option<(f64, f64)> {
if self.latitude == 0.0 && self.longitude == 0.0 {
None
} else {
Some((self.latitude, self.longitude))
}
}
pub fn check(&self) -> impl Iterator<Item = LocationIssue> {
let mut found = [None; 6];
if !(-90.0..=90.0).contains(&self.latitude) {
found[0] = Some(LocationIssue::LatitudeOutOfRange);
}
if !(-180.0..=180.0).contains(&self.longitude) {
found[1] = Some(LocationIssue::LongitudeOutOfRange);
}
if self.direction.is_some_and(|d| d >= 360.0) {
found[2] = Some(LocationIssue::DirectionOutOfRange);
}
if self
.speed_vertical
.is_some_and(|v| !(-62.0..=62.0).contains(&v))
{
found[3] = Some(LocationIssue::VerticalSpeedOutOfRange);
}
if self.timestamp.is_some_and(|t| t > 3600.0) {
found[4] = Some(LocationIssue::TimestampOutOfRange);
}
if matches!(self.status, Status::Other(_))
|| self.horizontal_accuracy.0 > 12
|| self.vertical_accuracy.0 > 6
|| self.baro_accuracy.0 > 6
|| self.speed_accuracy.0 > 4
{
found[5] = Some(LocationIssue::ReservedCode);
}
found.into_iter().flatten()
}
pub fn is_plausible(&self) -> bool {
self.check().next().is_none()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Auth {
pub protocol_version: u8,
pub auth_type: AuthType,
pub page: u8,
pub last_page_index: u8,
pub length: u8,
pub timestamp: u32,
pub data: [u8; 23],
}
impl Auth {
pub fn decode(raw: &[u8; MESSAGE_SIZE]) -> Result<Self, Error> {
let protocol_version = expect_type(raw, MessageType::Auth)?;
let page = raw[1] & 0x0F;
let auth_type = AuthType::from(raw[1] >> 4);
let mut data = [0u8; 23];
if page == 0 {
let last_page_index = raw[2];
let length = raw[3];
if last_page_index >= 16 {
return Err(Error::InvalidAuthPage("last page index above 15"));
}
if (length as usize) > 17 + last_page_index as usize * 23 {
return Err(Error::InvalidAuthPage(
"length does not fit in the announced pages",
));
}
data[..17].copy_from_slice(&raw[8..25]);
Ok(Auth {
protocol_version,
auth_type,
page,
last_page_index,
length,
timestamp: read_u32(raw, 4),
data,
})
} else {
data.copy_from_slice(&raw[2..25]);
Ok(Auth {
protocol_version,
auth_type,
page,
last_page_index: 0,
length: 0,
timestamp: 0,
data,
})
}
}
pub fn encode(&self) -> Result<[u8; MESSAGE_SIZE], Error> {
let mut out = [0u8; MESSAGE_SIZE];
out[0] = header(MessageType::Auth, self.protocol_version)?;
out[1] = (nibble(self.auth_type.into(), "auth_type")? << 4) | nibble(self.page, "page")?;
if self.page == 0 {
if self.last_page_index >= 16 {
return Err(Error::OutOfRange("last_page_index"));
}
if (self.length as usize) > 17 + self.last_page_index as usize * 23 {
return Err(Error::OutOfRange("length"));
}
out[2] = self.last_page_index;
out[3] = self.length;
out[4..8].copy_from_slice(&self.timestamp.to_le_bytes());
out[8..25].copy_from_slice(&self.data[..17]);
} else {
out[2..25].copy_from_slice(&self.data);
}
Ok(out)
}
pub fn page_data(&self) -> &[u8] {
if self.page == 0 {
&self.data[..17]
} else {
&self.data
}
}
pub fn unix_timestamp(&self) -> u64 {
self.timestamp as u64 + EPOCH_2019_UNIX
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SelfId {
pub protocol_version: u8,
pub description_type: DescriptionType,
pub description: [u8; 23],
}
impl SelfId {
pub fn new(description_type: DescriptionType, text: &str) -> Result<Self, Error> {
Ok(SelfId {
protocol_version: PROTOCOL_VERSION,
description_type,
description: fill_padded(text, "description")?,
})
}
pub fn decode(raw: &[u8; MESSAGE_SIZE]) -> Result<Self, Error> {
let protocol_version = expect_type(raw, MessageType::SelfId)?;
let mut description = [0u8; 23];
description.copy_from_slice(&raw[2..25]);
Ok(SelfId {
protocol_version,
description_type: DescriptionType::from(raw[1]),
description,
})
}
pub fn encode(&self) -> Result<[u8; MESSAGE_SIZE], Error> {
let mut out = [0u8; MESSAGE_SIZE];
out[0] = header(MessageType::SelfId, self.protocol_version)?;
out[1] = self.description_type.into();
out[2..25].copy_from_slice(&self.description);
Ok(out)
}
pub fn description_str(&self) -> Option<&str> {
padded_str(&self.description)
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct System {
pub protocol_version: u8,
pub operator_location_type: OperatorLocationType,
pub classification_type: ClassificationType,
pub operator_latitude: f64,
pub operator_longitude: f64,
pub area_count: u16,
pub area_radius: u16,
pub area_ceiling: Option<f32>,
pub area_floor: Option<f32>,
pub category_eu: CategoryEu,
pub class_eu: ClassEu,
pub operator_altitude_geo: Option<f32>,
pub timestamp: u32,
}
impl Default for System {
fn default() -> Self {
System {
protocol_version: PROTOCOL_VERSION,
operator_location_type: OperatorLocationType::Takeoff,
classification_type: ClassificationType::Undeclared,
operator_latitude: 0.0,
operator_longitude: 0.0,
area_count: 1,
area_radius: 0,
area_ceiling: None,
area_floor: None,
category_eu: CategoryEu::Undeclared,
class_eu: ClassEu::Undeclared,
operator_altitude_geo: None,
timestamp: 0,
}
}
}
impl System {
pub fn decode(raw: &[u8; MESSAGE_SIZE]) -> Result<Self, Error> {
let protocol_version = expect_type(raw, MessageType::System)?;
Ok(System {
protocol_version,
operator_location_type: OperatorLocationType::from(raw[1] & 0x03),
classification_type: ClassificationType::from((raw[1] >> 2) & 0x07),
operator_latitude: decode_latlon(read_i32(raw, 2)),
operator_longitude: decode_latlon(read_i32(raw, 6)),
area_count: read_u16(raw, 10),
area_radius: raw[12] as u16 * 10,
area_ceiling: decode_altitude(read_u16(raw, 13)),
area_floor: decode_altitude(read_u16(raw, 15)),
category_eu: CategoryEu::from(raw[17] >> 4),
class_eu: ClassEu::from(raw[17] & 0x0F),
operator_altitude_geo: decode_altitude(read_u16(raw, 18)),
timestamp: read_u32(raw, 20),
})
}
pub fn encode(&self) -> Result<[u8; MESSAGE_SIZE], Error> {
let mut out = [0u8; MESSAGE_SIZE];
out[0] = header(MessageType::System, self.protocol_version)?;
let olt = u8::from(self.operator_location_type);
let ct = u8::from(self.classification_type);
if olt > 3 {
return Err(Error::OutOfRange("operator_location_type"));
}
if ct > 7 {
return Err(Error::OutOfRange("classification_type"));
}
if !(-90.0..=90.0).contains(&self.operator_latitude) {
return Err(Error::OutOfRange("operator_latitude"));
}
if !(-180.0..=180.0).contains(&self.operator_longitude) {
return Err(Error::OutOfRange("operator_longitude"));
}
if self.area_radius > 2550 {
return Err(Error::OutOfRange("area_radius"));
}
out[1] = (ct << 2) | olt;
out[2..6].copy_from_slice(&encode_latlon(self.operator_latitude).to_le_bytes());
out[6..10].copy_from_slice(&encode_latlon(self.operator_longitude).to_le_bytes());
out[10..12].copy_from_slice(&self.area_count.to_le_bytes());
out[12] = (self.area_radius / 10) as u8;
out[13..15]
.copy_from_slice(&encode_altitude(self.area_ceiling, "area_ceiling")?.to_le_bytes());
out[15..17].copy_from_slice(&encode_altitude(self.area_floor, "area_floor")?.to_le_bytes());
out[17] = (nibble(self.category_eu.into(), "category_eu")? << 4)
| nibble(self.class_eu.into(), "class_eu")?;
out[18..20].copy_from_slice(
&encode_altitude(self.operator_altitude_geo, "operator_altitude_geo")?.to_le_bytes(),
);
out[20..24].copy_from_slice(&self.timestamp.to_le_bytes());
Ok(out)
}
pub fn operator_position(&self) -> Option<(f64, f64)> {
if self.operator_latitude == 0.0 && self.operator_longitude == 0.0 {
None
} else {
Some((self.operator_latitude, self.operator_longitude))
}
}
pub fn takeoff_position(&self) -> Option<(f64, f64)> {
if self.operator_location_type == OperatorLocationType::Takeoff {
self.operator_position()
} else {
None
}
}
pub fn live_operator_position(&self) -> Option<(f64, f64)> {
if self.operator_location_type == OperatorLocationType::LiveGnss {
self.operator_position()
} else {
None
}
}
pub fn fixed_operator_position(&self) -> Option<(f64, f64)> {
if self.operator_location_type == OperatorLocationType::Fixed {
self.operator_position()
} else {
None
}
}
pub fn unix_timestamp(&self) -> Option<u64> {
if self.timestamp == 0 {
None
} else {
Some(self.timestamp as u64 + EPOCH_2019_UNIX)
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct OperatorId {
pub protocol_version: u8,
pub operator_id_type: u8,
pub operator_id: [u8; 20],
}
impl OperatorId {
pub fn new(operator_id: &str) -> Result<Self, Error> {
Ok(OperatorId {
protocol_version: PROTOCOL_VERSION,
operator_id_type: 0,
operator_id: fill_padded(operator_id, "operator_id")?,
})
}
pub fn decode(raw: &[u8; MESSAGE_SIZE]) -> Result<Self, Error> {
let protocol_version = expect_type(raw, MessageType::OperatorId)?;
let mut operator_id = [0u8; 20];
operator_id.copy_from_slice(&raw[2..22]);
Ok(OperatorId {
protocol_version,
operator_id_type: raw[1],
operator_id,
})
}
pub fn encode(&self) -> Result<[u8; MESSAGE_SIZE], Error> {
let mut out = [0u8; MESSAGE_SIZE];
out[0] = header(MessageType::OperatorId, self.protocol_version)?;
out[1] = self.operator_id_type;
out[2..22].copy_from_slice(&self.operator_id);
Ok(out)
}
pub fn operator_id_str(&self) -> Option<&str> {
padded_str(&self.operator_id)
}
pub fn identity(&self) -> Option<crate::operator::OperatorIdentity<'_>> {
self.operator_id_str()
.map(crate::operator::OperatorIdentity::parse)
}
}