Skip to content

Commit

Permalink
scroll - (de)serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
elpiel committed May 1, 2020
1 parent 1c121ce commit a3751a3
Show file tree
Hide file tree
Showing 8 changed files with 475 additions and 14 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions arsdk-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ serde = {version = "1.0", features = ["derive"]}
serde_json = "1.0"
dashmap = "3.11"
chrono = "0.4"
scroll = "0.10"
scroll_derive = "0.10"
42 changes: 38 additions & 4 deletions arsdk-rs/src/ardrone3.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::frame::Data;

/// eARCOMMANDS_ID_ARDRONE3_PILOTING_CMD
#[repr(u8)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ArDrone3 {
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_FLATTRIM = 0
Expand Down Expand Up @@ -29,17 +28,52 @@ pub enum ArDrone3 {
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_CANCELMOVETO = 11
CancelMoveTo = 11,
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_STARTPILOTEDPOI = 12
StartPilotdPOI = 12,
StartPilotedPOI = 12,
/// ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_STOPPILOTEDPOI = 13
StopPilotedPOI = 13,
}

impl Data for ArDrone3 {
fn serialize(&self) -> Vec<u8> {
// todo: Fix this hardcoded value

let take_off: u16 = 1;

take_off.to_le_bytes().to_vec()
}
}

pub mod scroll_impl {
use super::*;
use scroll::{ctx, Endian, Pread};

impl<'a> ctx::TryFromCtx<'a, Endian> for ArDrone3 {
type Error = scroll::Error;

// and the lifetime annotation on `&'a [u8]` here
fn try_from_ctx(src: &'a [u8], endian: Endian) -> Result<(Self, usize), Self::Error> {
use ArDrone3::*;

let offset = &mut 0;

let ardrone3 = match src.gread_with::<u16>(offset, endian)? {
0 => FlatTrim,
1 => TakeOff,
2 => PCMD,
3 => Landing,
4 => Emergency,
5 => NavigateHome,
6 => AutoTakeOffMode,
7 => MoveBy,
8 => UserTakeOff,
9 => Circle,
10 => MoveTo,
11 => CancelMoveTo,
12 => StartPilotedPOI,
13 => StopPilotedPOI,
_ => return Err(scroll::Error::Custom("Out of range".into()))
};

Ok((ardrone3, *offset))
}

}
}
64 changes: 63 additions & 1 deletion arsdk-rs/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::ardrone3::ArDrone3;
use crate::common;
use crate::frame::Data;
use crate::jumping_sumo;
#[derive(Debug, PartialEq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]

pub enum Feature {
Common(common::Class), // ARCOMMANDS_ID_FEATURE_COMMON = 0,
Expand Down Expand Up @@ -72,6 +72,68 @@ impl Data for Feature {
}
}

pub mod scroll_impl {
use super::*;
use scroll::{ctx, Endian, Pread, Pwrite};

impl<'a> ctx::TryFromCtx<'a, Endian> for Feature {
type Error = scroll::Error;

// and the lifetime annotation on `&'a [u8]` here
fn try_from_ctx(src: &'a [u8], endian: Endian) -> Result<(Self, usize), Self::Error> {
let mut offset = 0;

let feature = match src.gread_with::<u8>(&mut offset, endian)? {
0 => {
let common = src.gread_with(&mut offset, endian)?;

Self::Common(common)
},
1 => {
let ardrone3 = src.gread_with(&mut offset, endian)?;

Self::ArDrone3(ardrone3)
},
2 => Self::Minidrone,
3 => {
let js_class = src.gread_with(&mut offset, endian)?;

Self::JumpingSumo(js_class)
},
4 => Self::SkyController,
8 => Self::PowerUp,
133 => Self::Generic,
134 => Self::FollowMe,
135 => Self::Wifi,
136 => Self::RC,
137 => Self::DroneManager,
138 => Self::Mapper,
139 => Self::Debug,
140 => Self::ControllerInfo,
141 => Self::MapperMini,
142 => Self::ThermalCam,
144 => Self::Animation,
147 => Self::SequoiaCam,
_ => return Err(scroll::Error::Custom("Out of range".into()))
};

Ok((feature, offset))
}
}

impl<'a> ctx::TryIntoCtx<Endian> for Feature {
type Error = scroll::Error;

fn try_into_ctx(self, this: &mut [u8], _ctx: Endian) -> Result<usize, Self::Error> {
let ser_feature = self.serialize();
let written = this.pwrite_with(ser_feature.as_slice(), 0, ())?;


Ok(written)
}
}
}

// --------------------- Tests --------------------- //

#[cfg(test)]
Expand Down
104 changes: 102 additions & 2 deletions arsdk-rs/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::frame::Data;
use chrono::{offset::Utc, DateTime};

#[derive(Debug, PartialEq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Class {
Network, // ARCOMMANDS_ID_COMMON_CLASS_NETWORK = 0,
NetworkEvent, // ARCOMMANDS_ID_COMMON_CLASS_NETWORKEVENT = 1,
Expand Down Expand Up @@ -39,7 +39,7 @@ pub enum Class {
Factory, // ARCOMMANDS_ID_COMMON_CLASS_FACTORY = 31,
}

#[derive(Debug, PartialEq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Common {
AllStates, // ARCOMMANDS_ID_COMMON_COMMON_CMD_ALLSTATES = 0,
CurrentDate(DateTime<Utc>), // ARCOMMANDS_ID_COMMON_COMMON_CMD_CURRENTDATE = 1,
Expand Down Expand Up @@ -144,6 +144,106 @@ impl Into<u8> for Common {
}
}

pub mod scroll_impl {
use super::*;
use scroll::{ctx, Endian, Pread, Pwrite};

impl<'a> ctx::TryFromCtx<'a, Endian> for Class {
type Error = scroll::Error;

// and the lifetime annotation on `&'a [u8]` here
fn try_from_ctx(src: &'a [u8], endian: Endian) -> Result<(Self, usize), Self::Error> {
let mut offset = 0;

let class = match src.gread_with::<u8>(&mut offset, endian)? {
0 => Self::Network,
1 => Self::NetworkEvent,
2 => Self::Settings,
3 => Self::SettingsState,
4 => {
let common = src.gread_with(&mut offset, endian)?;

Self::Common(common)
}
5 => Self::CommonState,
6 => Self::Overheat,
7 => Self::OverheatState,
8 => Self::Controller,
9 => Self::WifiSettings,
10 => Self::WifiSettingsState,
11 => Self::Mavlink,
12 => Self::MavlinkState,
13 => Self::Calibration,
14 => Self::CalibrationState,
15 => Self::CameraSettingsState,
16 => Self::Gps,
17 => Self::FlightPlanState,
18 => Self::ArLibsVersionsState,
19 => Self::FlightPlanEvent,
20 => Self::Audio,
21 => Self::AudioState,
22 => Self::HeadLights,
23 => Self::HeadLightsState,
24 => Self::Animations,
25 => Self::AnimationsState,
26 => Self::Accessory,
27 => Self::AccessoryState,
28 => Self::Charger,
29 => Self::ChargerState,
30 => Self::Runstate,
31 => Self::Factory,
32 => Self::FlightPlanSettings,
33 => Self::FlightPlanSettingsState,
_ => return Err(scroll::Error::Custom("Out of range".into())),
};

Ok((class, offset))
}
}

impl<'a> ctx::TryIntoCtx<Endian> for Common {
type Error = scroll::Error;

fn try_into_ctx(self, this: &mut [u8], _ctx: Endian) -> Result<usize, Self::Error> {
let ser_common = self.serialize();
let written = this.pwrite_with(ser_common.as_slice(), 0, ())?;

Ok(written)
}
}

impl<'a> ctx::TryFromCtx<'a, Endian> for Common {
type Error = scroll::Error;

fn try_from_ctx(src: &'a [u8], endian: Endian) -> Result<(Self, usize), Self::Error> {
use Common::*;
let mut offset = 0;

let common = match src.gread_with::<u8>(&mut offset, endian)? {
0 => AllStates,
// @TODO: FIX THIS!
1 => CurrentDate(Utc::now()),
// @TODO: FIX THIS!
2 => CurrentTime(Utc::now()),
3 => Reboot,
_ => return Err(scroll::Error::Custom("Out of range".into())),
};

Ok((common, offset))
}
}

impl<'a> ctx::TryIntoCtx<Endian> for Class {
type Error = scroll::Error;

fn try_into_ctx(self, this: &mut [u8], _ctx: Endian) -> Result<usize, Self::Error> {
let ser_class = self.serialize();
let written = this.pwrite_with(ser_class.as_slice(), 0, ())?;

Ok(written)
}
}
}
// --------------------- Tests --------------------- //

#[cfg(test)]
Expand Down
Loading

0 comments on commit a3751a3

Please sign in to comment.