pub mod battle;
pub mod button;
pub mod buzzer;
pub mod card;
pub mod command;
pub mod communication;
pub mod control;
pub mod display;
pub mod joystick;
pub mod light;
pub mod monitor;
pub mod motor;
pub mod navigation;
pub mod sensor;
pub mod vibrator;
use std::convert::TryFrom;
use num_enum::IntoPrimitive;
use num_enum::TryFromPrimitive;
use crate::system::{*};
use crate::communication::extractor::Extractor;
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum DataType {
#[num_enum(default)]
None = 0x00,
Ping = 0x01,
Ack = 0x02,
Error = 0x03,
Request = 0x04,
Message = 0x05,
Address = 0x06,
Information = 0x07,
Update = 0x08,
UpdateLocation = 0x09,
Encrypt = 0x0A,
SystemCount = 0x0B,
SystemInformation = 0x0C,
Registration = 0x0D,
Administrator = 0x0E,
Monitor = 0x0F,
Control = 0x10,
Command = 0x11,
Pairing = 0x12,
Rssi = 0x13,
TimeSync = 0x14,
TransmissionPower = 0x15,
Configuration = 0x16,
Echo = 0x17,
Battle = 0x1F,
LightManual = 0x20,
LightMode = 0x21,
LightEvent = 0x22,
LightDefault = 0x23,
RawMotion = 0x30,
RawFlow = 0x31,
State = 0x40,
Attitude = 0x41,
Position = 0x42,
Altitude = 0x43,
Motion = 0x44,
Range = 0x45,
Count = 0x50,
Bias = 0x51,
Trim = 0x52,
Weight = 0x53,
LostConnection = 0x54,
MagnetometerOffset = 0x55,
Motor = 0x60,
MotorSingle = 0x61,
Buzzer = 0x62,
Vibrator = 0x63,
Button = 0x70,
Joystick = 0x71,
DisplayClear = 0x80,
DisplayInvert = 0x81,
DisplayDrawPoint = 0x82,
DisplayDrawLine = 0x83,
DisplayDrawRect = 0x84,
DisplayDrawCircle = 0x85,
DisplayDrawString = 0x86,
DisplayDrawStringAlign = 0x87,
DisplayDrawImage = 0x88,
CardClassify = 0x90,
CardRange = 0x91,
CardRaw = 0x92,
CardColor = 0x93,
CardList = 0x94,
CardFunctionList = 0x95,
InformationAssembledForController = 0xA0,
InformationAssembledForEntry = 0xA1,
InformationAssembledForByBlocks = 0xA2,
NavigationTarget = 0xD0,
NavigationLocation = 0xD1,
NavigationMonitor = 0xD2,
NavigationHeading = 0xD3,
NavigationCounter = 0xD4,
NavigationSatellite = 0xD5,
NavigationLocationAdjust = 0xD6,
NavigationTargetEcef = 0xD8,
NavigationLocationEcef = 0xD9,
GpsRtkNavigationState = 0xDA,
GpsRtkExtendedRawMeasurementData = 0xDB,
}
impl DataType {
pub fn from_u8(data_u8: u8) -> DataType {
match DataType::try_from( data_u8 ) {
Ok(data) => { data },
_ => { DataType::None },
}
}
}
#[derive(Debug, Clone)]
pub enum Data {
None,
ErrorMessage (String),
Header (Header),
Ping (Ping),
Ack (Ack),
Error (Error),
Request (Request),
RequestOption (RequestOption),
Address (Address),
Information (Information),
Update (Update),
UpdateLocation (UpdateLocation),
SystemInformation (SystemInformation),
Administrator (Administrator),
Monitor0 (monitor::Monitor0),
Monitor4 (monitor::Monitor4),
Monitor8 (monitor::Monitor8),
Quad8 (control::Quad8),
Quad8AndRequestData (control::Quad8AndRequestData),
ControlPosition16 (control::Position16),
ControlPosition (control::Position),
Command (command::Command),
CommandLightEvent (command::CommandLightEvent),
CommandLightEventColor (command::CommandLightEventColor),
Pairing (communication::Pairing),
Rssi (communication::Rssi),
BattleIrMessage (battle::IrMessage),
BattleLightEventCommand (battle::LightEventCommand),
BattleIrMessageLightEventCommand (battle::IrMessageLightEventCommand),
LightManual (light::Manual),
LightMode (light::Mode),
LightEvent (light::Event),
RawMotion (sensor::RawMotion),
RawFlow (sensor::RawFlow),
State (State),
Attitude (sensor::Attitude),
Position (sensor::Position),
PositionVelocity (sensor::PositionVelocity),
Motion (sensor::Motion),
Range (sensor::Range),
Count (Count),
Bias (sensor::Bias),
Trim (sensor::Trim),
LostConnection (communication::LostConnection),
MagnetometerOffset (sensor::MagnetometerOffset),
MotorV (motor::MotorV),
MotorRV (motor::MotorRV),
MotorVA (motor::MotorVA),
MotorRVA (motor::MotorRVA),
MotorSingleV (motor::MotorSingleV),
MotorSingleRV (motor::MotorSingleRV),
Melody (buzzer::Melody),
BuzzerScale (buzzer::BuzzerScale),
BuzzerHz (buzzer::BuzzerHz),
Button (button::Button),
Joystick (joystick::Joystick),
DisplayClearAll (display::ClearAll),
DisplayClear (display::Clear),
DisplayInvert (display::Invert),
DisplayDrawPoint (display::DrawPoint),
DisplayDrawLine (display::DrawLine),
DisplayDrawRect (display::DrawRect),
DisplayDrawCircle (display::DrawCircle),
DisplayDrawString (display::DrawString),
DisplayDrawStringAlign (display::DrawStringAlign),
DisplayDrawImage (display::DrawImage),
CardClassify (card::Classify),
CardRange (card::Range),
CardRaw (card::Raw),
CardColor (card::Color),
CardListCard (card::ListCard),
CardListFunction (card::ListFunction),
NavigationTargetMove (navigation::TargetMove),
NavigationTargetAction (navigation::TargetAction),
NavigationLocation (navigation::Location),
NavigationMonitor (navigation::Monitor),
NavigationHeading (navigation::Heading),
NavigationCounter (navigation::Counter),
NavigationSatellite (navigation::Satellite),
NavigationLocationAdjust (navigation::LocationAdjust),
}
pub trait Serializable
{
fn to_vec(&self) -> Vec<u8>;
}
#[derive(Debug, Copy, Clone)]
pub struct Header {
pub data_type: DataType,
pub length: u8,
pub from: DeviceType,
pub to: DeviceType,
}
impl Header {
pub fn new() -> Header{
Header {
data_type: DataType::None,
length: 0,
from: DeviceType::None,
to: DeviceType::None,
}
}
pub const fn size() -> usize { 4 }
pub fn parse(slice_data: &[u8]) -> Result<Header, &'static str> {
if slice_data.len() == Header::size() {
Ok(Header{
data_type: DataType::from_u8(slice_data[0]),
length: slice_data[1],
from: DeviceType::from_u8(slice_data[2]),
to: DeviceType::from_u8(slice_data[3]),
})
}
else { Err("Wrong length") }
}
}
impl Serializable for Header {
fn to_vec(&self) -> Vec<u8> {
let mut vec_data : Vec<u8> = Vec::new();
vec_data.push(self.data_type.into());
vec_data.push(self.length);
vec_data.push(self.from.into());
vec_data.push(self.to.into());
vec_data
}
}
#[derive(Debug, Copy, Clone)]
pub struct Ping {
pub system_time: u64,
}
impl Ping {
pub fn new() -> Ping{
Ping {
system_time: 0,
}
}
pub const fn size() -> usize { 8 }
pub fn parse(slice_data: &[u8]) -> Result<Ping, &'static str> {
if slice_data.len() == Ping::size() {
let mut ext: Extractor = Extractor::from_slice(slice_data);
Ok(Ping{
system_time: ext.get_u64(),
})
}
else { Err("Wrong length") }
}
}
impl Serializable for Ping {
fn to_vec(&self) -> Vec<u8> {
let mut vec_data : Vec<u8> = Vec::new();
vec_data.extend_from_slice(&self.system_time.to_le_bytes());
vec_data
}
}
#[derive(Debug, Copy, Clone)]
pub struct Ack {
pub system_time: u64,
pub data_type: DataType,
pub crc16: u16,
}
impl Ack {
pub fn new() -> Ack{
Ack {
system_time: 0,
data_type: DataType::None,
crc16: 1,
}
}
pub const fn size() -> usize { 11 }
pub fn parse(slice_data: &[u8]) -> Result<Ack, &'static str> {
if slice_data.len() == Ack::size() {
let mut ext: Extractor = Extractor::from_slice(slice_data);
Ok(Ack{
system_time: ext.get_u64(),
data_type: DataType::from_u8(ext.get_u8()),
crc16: ext.get_u16(),
})
}
else { Err("Wrong length") }
}
}
impl Serializable for Ack {
fn to_vec(&self) -> Vec<u8> {
let mut vec_data : Vec<u8> = Vec::new();
vec_data.extend_from_slice(&self.system_time.to_le_bytes());
vec_data.push(self.data_type.into());
vec_data.extend_from_slice(&self.crc16.to_le_bytes());
vec_data
}
}
#[derive(Debug, Copy, Clone)]
pub struct Error {
pub system_time: u64,
pub error_flags_for_sensor: u32,
pub error_flags_for_state: u32,
}
impl Error {
pub fn new() -> Error{
Error {
system_time: 0,
error_flags_for_sensor: 0,
error_flags_for_state: 0,
}
}
pub const fn size() -> usize { 16 }
pub fn parse(slice_data: &[u8]) -> Result<Error, &'static str> {
if slice_data.len() == Error::size() {
let mut ext: Extractor = Extractor::from_slice(slice_data);
Ok(Error{
system_time: ext.get_u64(),
error_flags_for_sensor: ext.get_u32(),
error_flags_for_state: ext.get_u32(),
})
}
else { Err("Wrong length") }
}
}
impl Serializable for Error {
fn to_vec(&self) -> Vec<u8> {
let mut vec_data : Vec<u8> = Vec::new();
vec_data.extend_from_slice(&self.system_time.to_le_bytes());
vec_data.extend_from_slice(&self.error_flags_for_sensor.to_le_bytes());
vec_data.extend_from_slice(&self.error_flags_for_state.to_le_bytes());
vec_data
}
}
#[derive(Debug, Copy, Clone)]
pub struct Request {
pub data_type: DataType,
}
impl Request {
pub fn new() -> Request{
Request {
data_type: DataType::None,
}
}
pub const fn size() -> usize { 1 }
pub fn parse(slice_data: &[u8]) -> Result<Request, &'static str> {
if slice_data.len() == Request::size() {
let mut ext: Extractor = Extractor::from_slice(slice_data);
Ok(Request{
data_type: DataType::from_u8(ext.get_u8()),
})
}
else { Err("Wrong length") }
}
}
impl Serializable for Request {
fn to_vec(&self) -> Vec<u8> {
let mut vec_data : Vec<u8> = Vec::new();
vec_data.push(self.data_type.into());
vec_data
}
}
#[derive(Debug, Copy, Clone)]
pub struct RequestOption {
pub data_type: DataType,
pub option: u32,
}
impl RequestOption {
pub fn new() -> RequestOption{
RequestOption {
data_type: DataType::None,
option: 0,
}
}
pub const fn size() -> usize { 5 }
pub fn parse(slice_data: &[u8]) -> Result<RequestOption, &'static str> {
if slice_data.len() == RequestOption::size() {
let mut ext: Extractor = Extractor::from_slice(slice_data);
Ok(RequestOption{
data_type: DataType::from_u8(ext.get_u8()),
option: ext.get_u32(),
})
}
else { Err("Wrong length") }
}
}
impl Serializable for RequestOption {
fn to_vec(&self) -> Vec<u8> {
let mut vec_data : Vec<u8> = Vec::new();
vec_data.push(self.data_type.into());
vec_data.extend_from_slice(&self.option.to_le_bytes());
vec_data
}
}
#[derive(Debug, Copy, Clone)]
pub struct SystemInformation {
pub crc32_bootloader: u32,
pub crc32_application: u32,
}
impl SystemInformation {
pub fn new() -> SystemInformation{
SystemInformation {
crc32_bootloader: 0,
crc32_application: 0,
}
}
pub const fn size() -> usize { 8 }
pub fn parse(slice_data: &[u8]) -> Result<SystemInformation, &'static str> {
if slice_data.len() == SystemInformation::size() {
let mut ext: Extractor = Extractor::from_slice(slice_data);
Ok(SystemInformation{
crc32_bootloader: ext.get_u32(),
crc32_application: ext.get_u32(),
})
}
else { Err("Wrong length") }
}
}
impl Serializable for SystemInformation {
fn to_vec(&self) -> Vec<u8> {
let mut vec_data : Vec<u8> = Vec::new();
vec_data.extend_from_slice(&self.crc32_bootloader.to_le_bytes());
vec_data.extend_from_slice(&self.crc32_application.to_le_bytes());
vec_data
}
}
#[derive(Debug, Copy, Clone)]
pub struct Information {
pub mode_update: ModeUpdate,
pub model_number: ModelNumber,
pub version: Version,
pub year: u16,
pub month: u8,
pub day: u8,
}
impl Information {
pub fn new() -> Information{
Information {
mode_update: ModeUpdate::None,
model_number: ModelNumber::None,
version: Version{ major:21, minor:1, build:1 },
year: 2021,
month: 1,
day: 1,
}
}
pub const fn size() -> usize { 13 }
pub fn parse(slice_data: &[u8]) -> Result<Information, &'static str> {
if slice_data.len() == Information::size() {
let mut ext: Extractor = Extractor::from_slice(slice_data);
Ok(Information{
mode_update: ModeUpdate::from_u8(ext.get_u8()),
model_number: ModelNumber::from_u32(ext.get_u32()),
version: Version::from_u32(ext.get_u32()),
year: ext.get_u16(),
month: ext.get_u8(),
day: ext.get_u8(),
})
}
else { Err("Wrong length") }
}
}
impl Serializable for Information {
fn to_vec(&self) -> Vec<u8> {
let mut vec_data : Vec<u8> = Vec::new();
vec_data.push(self.mode_update.into());
vec_data.extend_from_slice(&self.model_number.to_array());
vec_data.extend_from_slice(&self.version.to_array());
vec_data.extend_from_slice(&self.year.to_le_bytes());
vec_data.push(self.month.into());
vec_data.push(self.day.into());
vec_data
}
}
#[derive(Debug, Copy, Clone)]
pub struct UpdateLocation {
pub index_block_next: u16,
}
impl UpdateLocation {
pub fn new() -> UpdateLocation{
UpdateLocation {
index_block_next: 0,
}
}
pub const fn size() -> usize { 2 }
pub fn parse(slice_data: &[u8]) -> Result<UpdateLocation, &'static str> {
if slice_data.len() == UpdateLocation::size() {
let mut ext: Extractor = Extractor::from_slice(slice_data);
Ok(UpdateLocation{
index_block_next: ext.get_u16(),
})
}
else { Err("Wrong length") }
}
}
impl Serializable for UpdateLocation {
fn to_vec(&self) -> Vec<u8> {
let mut vec_data : Vec<u8> = Vec::new();
vec_data.extend_from_slice(&self.index_block_next.to_le_bytes());
vec_data
}
}
#[derive(Debug, Clone)]
pub struct Update {
pub index_block_next: u16,
pub vec_data: Vec<u8>,
}
impl Update {
pub fn new() -> Update{
Update {
index_block_next: 0,
vec_data: Vec::new(),
}
}
pub const fn size() -> usize { 2 }
pub fn parse(slice_data: &[u8]) -> Result<Update, &'static str> {
if slice_data.len() > Update::size() {
let mut ext: Extractor = Extractor::from_slice(slice_data);
Ok(Update{
index_block_next: ext.get_u16(),
vec_data: slice_data[Update::size()..].to_vec(),
})
}
else { Err("Wrong length") }
}
pub fn get_length(&self) -> usize {
Update::size() + self.vec_data.len()
}
}
impl Serializable for Update {
fn to_vec(&self) -> Vec<u8> {
let mut vec_data : Vec<u8> = Vec::new();
vec_data.extend_from_slice(&self.index_block_next.to_le_bytes());
vec_data.extend_from_slice(&self.vec_data[..]);
vec_data
}
}
#[derive(Debug, Clone)]
pub struct Address {
pub vec_address: Vec<u8>,
}
impl Address {
pub fn new() -> Address{
Address {
vec_address: Vec::new(),
}
}
pub const fn size() -> usize { 16 }
pub fn parse(slice_data: &[u8]) -> Result<Address, &'static str> {
if slice_data.len() == Address::size() {
Ok(Address{
vec_address: slice_data[..].to_vec(),
})
}
else { Err("Wrong length") }
}
}
impl Serializable for Address {
fn to_vec(&self) -> Vec<u8> {
let mut vec_address : Vec<u8> = Vec::new();
vec_address.extend_from_slice(&self.vec_address[..]);
vec_address
}
}
#[derive(Debug, Clone)]
pub struct Administrator {
pub vec_key: Vec<u8>,
}
impl Administrator {
pub fn new() -> Administrator{
Administrator {
vec_key: Vec::new(),
}
}
pub const fn size() -> usize { 16 }
pub fn parse(slice_data: &[u8]) -> Result<Administrator, &'static str> {
if slice_data.len() == Administrator::size() {
Ok(Administrator{
vec_key: slice_data[..].to_vec(),
})
}
else { Err("Wrong length") }
}
}
impl Serializable for Administrator {
fn to_vec(&self) -> Vec<u8> {
let mut vec_key : Vec<u8> = Vec::new();
vec_key.extend_from_slice(&self.vec_key[..]);
vec_key
}
}
#[derive(Debug, Copy, Clone)]
pub struct Count {
pub time_system: u32,
pub time_flight: u32,
pub count_takeoff: u16,
pub count_landing: u16,
pub count_accident: u16,
}
impl Count {
pub fn new() -> Count{
Count {
time_system: 0,
time_flight: 0,
count_takeoff: 0,
count_landing: 0,
count_accident: 0,
}
}
pub const fn size() -> usize { 14 }
pub fn parse(slice_data: &[u8]) -> Result<Count, &'static str> {
if slice_data.len() == Count::size() {
let mut ext: Extractor = Extractor::from_slice(slice_data);
Ok(Count{
time_system: ext.get_u32(),
time_flight: ext.get_u32(),
count_takeoff: ext.get_u16(),
count_landing: ext.get_u16(),
count_accident: ext.get_u16(),
})
}
else { Err("Wrong length") }
}
}
impl Serializable for Count {
fn to_vec(&self) -> Vec<u8> {
let mut vec_data : Vec<u8> = Vec::new();
vec_data.extend_from_slice(&self.time_system.to_le_bytes());
vec_data.extend_from_slice(&self.time_flight.to_le_bytes());
vec_data.extend_from_slice(&self.count_takeoff.to_le_bytes());
vec_data.extend_from_slice(&self.count_landing.to_le_bytes());
vec_data.extend_from_slice(&self.count_accident.to_le_bytes());
vec_data
}
}
#[derive(Debug, Copy, Clone)]
pub struct State {
pub mode_system: ModeSystem,
pub mode_flight: ModeFlight,
pub mode_control_flight: ModeControlFlight,
pub mode_movement: ModeMovement,
pub headless: Headless,
pub control_speed: u8,
pub sensor_orientation: SensorOrientation,
pub battery: u8,
}
impl State {
pub fn new() -> State{
State {
mode_system: ModeSystem::None,
mode_flight: ModeFlight::None,
mode_control_flight: ModeControlFlight::None,
mode_movement: ModeMovement::None,
headless: Headless::None,
control_speed: 0,
sensor_orientation: SensorOrientation::None,
battery: 0,
}
}
pub const fn size() -> usize { 8 }
pub fn parse(slice_data: &[u8]) -> Result<State, &'static str> {
if slice_data.len() == State::size() {
let mut ext: Extractor = Extractor::from_slice(slice_data);
Ok(State{
mode_system: ModeSystem::from_u8(ext.get_u8()),
mode_flight: ModeFlight::from_u8(ext.get_u8()),
mode_control_flight: ModeControlFlight::from_u8(ext.get_u8()),
mode_movement: ModeMovement::from_u8(ext.get_u8()),
headless: Headless::from_u8(ext.get_u8()),
control_speed: ext.get_u8(),
sensor_orientation: SensorOrientation::from_u8(ext.get_u8()),
battery: ext.get_u8(),
})
}
else { Err("Wrong length") }
}
}
impl Serializable for State {
fn to_vec(&self) -> Vec<u8> {
let mut vec_data : Vec<u8> = Vec::new();
vec_data.push(self.mode_system.into());
vec_data
}
}