From 211af57181737799f7b05bfaf34cc4a867373672 Mon Sep 17 00:00:00 2001 From: Lachezar Lechev Date: Fri, 1 May 2020 16:38:01 +0300 Subject: [PATCH] [WIP] `Scroll` impl and it Compiles --- Cargo.lock | 19 ++++++++++++++ arsdk-rs/Cargo.toml | 2 ++ arsdk-rs/src/ardrone3.rs | 42 ++++++++++++++++++++++++++--- arsdk-rs/src/command.rs | 57 ++++++++++++++++++++++++++++++++++++++++ arsdk-rs/src/frame.rs | 52 ++++++++++++++++++++++++++++++++++++ bebop2/src/lib.rs | 3 +++ 6 files changed, 171 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0b8e330..cb3d9aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,6 +32,8 @@ dependencies = [ "chrono", "dashmap", "pnet", + "scroll", + "scroll_derive", "serde", "serde_json", ] @@ -373,6 +375,23 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed3d612bc64430efeb3f7ee6ef26d590dce0c43249217bddc62112540c7941e1" +[[package]] +name = "scroll" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb2332cb595d33f7edd5700f4cbf94892e680c7f0ae56adab58a35190b66cb1" + +[[package]] +name = "scroll_derive" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8584eea9b9ff42825b46faf46a8c24d2cff13ec152fa2a50df788b87c07ee28" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "serde" version = "1.0.106" diff --git a/arsdk-rs/Cargo.toml b/arsdk-rs/Cargo.toml index 8c0e742..c668557 100644 --- a/arsdk-rs/Cargo.toml +++ b/arsdk-rs/Cargo.toml @@ -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" diff --git a/arsdk-rs/src/ardrone3.rs b/arsdk-rs/src/ardrone3.rs index 661e3a5..d4565d9 100644 --- a/arsdk-rs/src/ardrone3.rs +++ b/arsdk-rs/src/ardrone3.rs @@ -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 @@ -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 { - // 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, BE}; + + 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::(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)) + } + + } +} diff --git a/arsdk-rs/src/command.rs b/arsdk-rs/src/command.rs index 81cf1f4..1342809 100644 --- a/arsdk-rs/src/command.rs +++ b/arsdk-rs/src/command.rs @@ -72,6 +72,63 @@ impl Data for Feature { } } +pub mod scroll_impl { + use super::*; + use scroll::{ctx, Endian, Pread, BE}; + + 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 offset = &mut 0; + + let feature: u8 = src.gread_with(offset, endian)?; + + let feature = match feature { + 0 => { + todo!() + // Self::Common() + }, + 1 => { + let ardrone3 = src.gread_with(offset, endian)?; + + Self::ArDrone3(ardrone3) + }, + 2 => Self::Minidrone, + 3 => { + todo!() + // Self::JumpingSumo(_) + }, + 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)) + + + // let buffer_id = src.gread_with(offset, endian)?; + // let sequence_id = src.gread_with(offset, endian)?; + // let feature_len: u32 = src.gread_with(offset, endian)?; + // let feature = src.gread_with(offset, endian)?; + // Ok((Feature { frame_type, buffer_id, sequence_id, feature }, *offset)) + } + } +} + // --------------------- Tests --------------------- // #[cfg(test)] diff --git a/arsdk-rs/src/frame.rs b/arsdk-rs/src/frame.rs index a52c7e8..e1d3c5c 100644 --- a/arsdk-rs/src/frame.rs +++ b/arsdk-rs/src/frame.rs @@ -1,6 +1,7 @@ use crate::{command, Drone}; use anyhow::{anyhow, Error as AnyError, Result as AnyResult}; use std::convert::TryFrom; +use scroll_derive::{Pread, Pwrite}; pub trait IntoRawFrame { fn into_raw(self) -> RawFrame; @@ -191,6 +192,57 @@ impl Into for BufferID { } } +pub mod impl_scroll { + use super::*; + + use scroll::{ctx, Endian, Pread, LE}; + + impl<'a> ctx::TryFromCtx<'a, Endian> for Frame { + 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 offset = &mut 0; + + let frame_type = src.gread_with(offset, endian)?; + let buffer_id = src.gread_with(offset, endian)?; + let sequence_id = src.gread_with(offset, endian)?; + let feature_len: u32 = src.gread_with(offset, endian)?; + let feature = src.gread_with(offset, endian)?; + + Ok((Frame { frame_type, buffer_id, sequence_id, feature }, *offset)) + } + } + + impl<'a> ctx::TryFromCtx<'a, Endian> for Type { + 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 offset = &mut 0; + let frame_value = src.gread::(offset)?; + + Type::try_from(frame_value) + .map(|frame_type| (frame_type, *offset)) + .map_err(|err| scroll::Error::Custom(err.to_string())) + } + } + + impl<'a> ctx::TryFromCtx<'a, Endian> for BufferID { + 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 offset = &mut 0; + let id_value = src.gread::(offset)?; + + BufferID::try_from(id_value) + .map(|buffer_id| (buffer_id, *offset)) + .map_err(|err| scroll::Error::Custom(err.to_string())) + } + } +} + // --------------------- Tests --------------------- // #[cfg(test)] diff --git a/bebop2/src/lib.rs b/bebop2/src/lib.rs index 5c3691c..a6b425c 100644 --- a/bebop2/src/lib.rs +++ b/bebop2/src/lib.rs @@ -22,6 +22,9 @@ impl Bebop2 { Ok(Self { drone }) } + // ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_NAVIGATEHOME + // ARCOMMANDS_ID_ARDRONE3_PILOTING_CMD_AUTOTAKEOFFMODE + pub fn take_off(&self) -> AnyResult<()> { // Ardrone3 // ARCOMMANDS_ID_ARDRONE3_CLASS_PILOTING