From 5230a52fef5a58d4bbd38f6e1003c3fcf62bb92e Mon Sep 17 00:00:00 2001 From: Gregory Mallios Date: Sun, 10 Dec 2023 22:23:08 +0200 Subject: [PATCH] feat(A3040): Begin A3040 parser work and refactor "toggle" structs --- soundcore-lib/Cargo.toml | 1 + soundcore-lib/src/models.rs | 14 +--- soundcore-lib/src/models/age.rs | 16 ----- soundcore-lib/src/models/game_mode.rs | 6 -- soundcore-lib/src/models/misc.rs | 68 +++++++++++++++++++ soundcore-lib/src/models/side_tone.rs | 12 ---- soundcore-lib/src/models/touch_tone.rs | 12 ---- soundcore-lib/src/models/tws_status.rs | 12 ---- soundcore-lib/src/models/wear_detection.rs | 12 ---- soundcore-lib/src/packets.rs | 12 ++-- soundcore-lib/src/packets/response/state.rs | 2 + .../src/packets/response/state/a3027.rs | 6 +- .../src/packets/response/state/a3028.rs | 6 +- .../src/packets/response/state/a3029.rs | 6 +- .../src/packets/response/state/a3040.rs | 12 ++++ .../src/packets/response/state/a3930.rs | 8 +-- .../src/packets/response/state/a3951.rs | 10 +-- soundcore-lib/src/parsers.rs | 10 +-- soundcore-lib/src/parsers/age.rs | 9 --- soundcore-lib/src/parsers/game_mode.rs | 0 soundcore-lib/src/parsers/side_tone.rs | 10 --- soundcore-lib/src/parsers/touch_tone.rs | 7 -- soundcore-lib/src/parsers/wear_detection.rs | 11 --- 23 files changed, 110 insertions(+), 152 deletions(-) delete mode 100644 soundcore-lib/src/models/age.rs delete mode 100644 soundcore-lib/src/models/game_mode.rs create mode 100644 soundcore-lib/src/models/misc.rs delete mode 100644 soundcore-lib/src/models/side_tone.rs delete mode 100644 soundcore-lib/src/models/touch_tone.rs delete mode 100644 soundcore-lib/src/models/tws_status.rs delete mode 100644 soundcore-lib/src/models/wear_detection.rs create mode 100644 soundcore-lib/src/packets/response/state/a3040.rs delete mode 100644 soundcore-lib/src/parsers/age.rs delete mode 100644 soundcore-lib/src/parsers/game_mode.rs delete mode 100644 soundcore-lib/src/parsers/side_tone.rs delete mode 100644 soundcore-lib/src/parsers/touch_tone.rs delete mode 100644 soundcore-lib/src/parsers/wear_detection.rs diff --git a/soundcore-lib/Cargo.toml b/soundcore-lib/Cargo.toml index 507195e..3974d33 100644 --- a/soundcore-lib/Cargo.toml +++ b/soundcore-lib/Cargo.toml @@ -20,6 +20,7 @@ log = "0.4.17" typeshare = "1.0.1" phf = { version = "0.11", default-features = false, features = ["macros"] } dialoguer = { version = "0.10.4", features = ["fuzzy-select"] } +derive_more = { version = "0.99", features = ["from"] } [dev-dependencies] test_data = { path = "../test_data/" } \ No newline at end of file diff --git a/soundcore-lib/src/models.rs b/soundcore-lib/src/models.rs index a115c81..86ee668 100644 --- a/soundcore-lib/src/models.rs +++ b/soundcore-lib/src/models.rs @@ -1,5 +1,4 @@ mod a3909_button_model; -mod age; mod anc_mode; mod auto_power; mod battery; @@ -11,21 +10,16 @@ mod eq_configuration; mod eq_profile; mod feature_flags; mod fw; -mod game_mode; +mod misc; mod gender; mod hearid; mod packet_header; mod packet_kind; mod serial; -mod side_tone; mod sound_mode; -mod touch_tone; mod trans_mode; -mod tws_status; -mod wear_detection; pub use a3909_button_model::*; -pub use age::*; pub use anc_mode::*; pub use auto_power::*; pub use battery::*; @@ -37,15 +31,11 @@ pub use eq_configuration::*; pub use eq_profile::*; pub use feature_flags::*; pub use fw::*; -pub use game_mode::*; +pub use misc::*; pub use gender::*; pub use hearid::*; pub use packet_header::*; pub use packet_kind::*; pub use serial::*; -pub use side_tone::*; pub use sound_mode::*; -pub use touch_tone::*; pub use trans_mode::*; -pub use tws_status::*; -pub use wear_detection::*; diff --git a/soundcore-lib/src/models/age.rs b/soundcore-lib/src/models/age.rs deleted file mode 100644 index fe8ca9e..0000000 --- a/soundcore-lib/src/models/age.rs +++ /dev/null @@ -1,16 +0,0 @@ -use serde::{Deserialize, Serialize}; - -#[derive( - Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Default, Hash, -)] -pub struct AgeRange(u8); - -impl AgeRange { - pub fn from_u8(value: u8) -> Self { - Self(value) - } - - pub fn as_u8(&self) -> u8 { - self.0 - } -} diff --git a/soundcore-lib/src/models/game_mode.rs b/soundcore-lib/src/models/game_mode.rs deleted file mode 100644 index e6afb07..0000000 --- a/soundcore-lib/src/models/game_mode.rs +++ /dev/null @@ -1,6 +0,0 @@ -use serde::{Deserialize, Serialize}; - -#[derive( - Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Default, Hash, -)] -pub struct GameMode(pub bool); diff --git a/soundcore-lib/src/models/misc.rs b/soundcore-lib/src/models/misc.rs new file mode 100644 index 0000000..3a36414 --- /dev/null +++ b/soundcore-lib/src/models/misc.rs @@ -0,0 +1,68 @@ +use serde::{Deserialize, Serialize}; +use derive_more::From; + +/// +/// "Toggle" types, booleans that represent a toggleable feature and are parsed using the bool_parser. +/// + +#[derive( + Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Default, Hash, From, +)] +pub struct GameMode(pub bool); + +#[derive( + Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Default, Hash, From, +)] +pub struct BassUp(pub bool); + +#[derive( + Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Default, Hash, From, +)] +pub struct LDAC(pub bool); + +#[derive( + Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Default, Hash, From, +)] +pub struct InEarBeep(pub bool); + +#[derive( + Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Default, Hash, From, +)] +pub struct SupportTwoCnn(pub bool); + +#[derive( + Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Default, Hash, From, +)] +pub struct ThreeDimensionalEffect(pub bool); + +#[derive( + Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Default, Hash, From, +)] +pub struct SideTone(pub bool); + +#[derive( + Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Default, Hash, From, +)] +pub struct PowerOnBatteryNotice(pub bool); + +#[derive( + Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Default, Hash, From, +)] +pub struct TwsStatus(pub bool); + +#[derive( + Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Default, Hash, From, +)] +pub struct WearDetection(pub bool); + + +#[derive( + Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Default, Hash, From, +)] +pub struct TouchTone(pub bool); + + +#[derive( + Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Default, Hash, From, +)] +pub struct AgeRange(pub u8); diff --git a/soundcore-lib/src/models/side_tone.rs b/soundcore-lib/src/models/side_tone.rs deleted file mode 100644 index 24d29af..0000000 --- a/soundcore-lib/src/models/side_tone.rs +++ /dev/null @@ -1,12 +0,0 @@ -use serde::{Deserialize, Serialize}; - -#[derive( - Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Default, Hash, -)] -pub struct SideTone(pub bool); - -impl From for SideTone { - fn from(value: bool) -> Self { - Self(value) - } -} diff --git a/soundcore-lib/src/models/touch_tone.rs b/soundcore-lib/src/models/touch_tone.rs deleted file mode 100644 index 03c6633..0000000 --- a/soundcore-lib/src/models/touch_tone.rs +++ /dev/null @@ -1,12 +0,0 @@ -use serde::{Deserialize, Serialize}; - -#[derive( - Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Default, Hash, -)] -pub struct TouchTone(pub bool); - -impl From for TouchTone { - fn from(b: bool) -> Self { - TouchTone(b) - } -} diff --git a/soundcore-lib/src/models/tws_status.rs b/soundcore-lib/src/models/tws_status.rs deleted file mode 100644 index ea739bf..0000000 --- a/soundcore-lib/src/models/tws_status.rs +++ /dev/null @@ -1,12 +0,0 @@ -use serde::{Deserialize, Serialize}; - -#[derive( - Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Default, Hash, -)] -pub struct TwsStatus(pub bool); - -impl From for TwsStatus { - fn from(value: bool) -> Self { - Self(value) - } -} diff --git a/soundcore-lib/src/models/wear_detection.rs b/soundcore-lib/src/models/wear_detection.rs deleted file mode 100644 index 0a0db3c..0000000 --- a/soundcore-lib/src/models/wear_detection.rs +++ /dev/null @@ -1,12 +0,0 @@ -use serde::{Deserialize, Serialize}; - -#[derive( - Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Default, Hash, -)] -pub struct WearDetection(pub bool); - -impl From for WearDetection { - fn from(b: bool) -> Self { - WearDetection(b) - } -} diff --git a/soundcore-lib/src/packets.rs b/soundcore-lib/src/packets.rs index a4db633..4433755 100644 --- a/soundcore-lib/src/packets.rs +++ b/soundcore-lib/src/packets.rs @@ -6,9 +6,9 @@ pub use command::*; pub use request::*; pub use response::*; +const COMMAND_BYTE_SIZE: usize = 2; +const PACKET_PREFIX: [u8; 5] = [0x08, 0xEE, 0x00, 0x00, 0x00]; pub trait SoundcorePacket { - const COMMAND_BYTE_SIZE: usize = 2; - const PACKET_PREFIX: [u8; 5] = [0x08, 0xEE, 0x00, 0x00, 0x00]; type ByteArr; /// Returns the packet's bytes + checksum @@ -19,14 +19,14 @@ impl SoundcorePacket for T where T: RequestPacket, { - type ByteArr = [u8; Self::COMMAND_BYTE_SIZE + Self::PACKET_PREFIX.len() + 3]; + type ByteArr = [u8; COMMAND_BYTE_SIZE + PACKET_PREFIX.len() + 3]; fn bytes(&self) -> Self::ByteArr { - let mut bytes = [0; COMMAND_BYTE_SIZE + Self::PACKET_PREFIX.len() + 3]; + let mut bytes = [0; COMMAND_BYTE_SIZE + PACKET_PREFIX.len() + 3]; // Add the prefix - bytes[..Self::PACKET_PREFIX.len()].copy_from_slice(&Self::PACKET_PREFIX); + bytes[..PACKET_PREFIX.len()].copy_from_slice(&PACKET_PREFIX); // Add the command bytes - bytes[Self::PACKET_PREFIX.len()..Self::PACKET_PREFIX.len() + COMMAND_BYTE_SIZE] + bytes[PACKET_PREFIX.len()..PACKET_PREFIX.len() + COMMAND_BYTE_SIZE] .copy_from_slice(&self.default_bytes()); bytes diff --git a/soundcore-lib/src/packets/response/state.rs b/soundcore-lib/src/packets/response/state.rs index adc3e2e..69403ee 100644 --- a/soundcore-lib/src/packets/response/state.rs +++ b/soundcore-lib/src/packets/response/state.rs @@ -55,9 +55,11 @@ mod a3028; mod a3029; mod a3930; mod a3951; +mod a3040; use a3027::*; use a3028::*; use a3029::*; use a3930::*; use a3951::*; +use a3040::*; \ No newline at end of file diff --git a/soundcore-lib/src/packets/response/state/a3027.rs b/soundcore-lib/src/packets/response/state/a3027.rs index b8be4c3..7d35494 100644 --- a/soundcore-lib/src/packets/response/state/a3027.rs +++ b/soundcore-lib/src/packets/response/state/a3027.rs @@ -16,12 +16,12 @@ use crate::{ }, parsers::{ parse_base_hear_id, parse_bool, parse_dual_fw, parse_fw, parse_serial_number, - parse_single_battery, + parse_single_battery, u8_parser, }, }; use crate::parsers::{ - bool_parser, parse_a3909_button_model, parse_age_range, parse_custom_hear_id, + bool_parser, parse_a3909_button_model, parse_custom_hear_id, parse_dual_battery, parse_gender, parse_sound_mode, parse_stereo_eq_configuration, ParseError, ParseResult, }; @@ -83,7 +83,7 @@ pub fn parse_a3027_state_response<'a, E: ParseError<'a>>( parse_single_battery, parse_stereo_eq_configuration, parse_gender, - parse_age_range, + u8_parser::, parse_base_hear_id, parse_sound_mode, parse_dual_fw, diff --git a/soundcore-lib/src/packets/response/state/a3028.rs b/soundcore-lib/src/packets/response/state/a3028.rs index 4bbd02c..2f8313a 100644 --- a/soundcore-lib/src/packets/response/state/a3028.rs +++ b/soundcore-lib/src/packets/response/state/a3028.rs @@ -16,12 +16,12 @@ use crate::{ }, parsers::{ parse_base_hear_id, parse_bool, parse_dual_fw, parse_fw, parse_serial_number, - parse_single_battery, + parse_single_battery, u8_parser, }, }; use crate::parsers::{ - bool_parser, parse_a3909_button_model, parse_age_range, parse_custom_hear_id, + bool_parser, parse_a3909_button_model, parse_custom_hear_id, parse_dual_battery, parse_gender, parse_sound_mode, parse_stereo_eq_configuration, ParseError, ParseResult, }; @@ -76,7 +76,7 @@ pub fn parse_a3028_state_response<'a, E: ParseError<'a>>( parse_single_battery, parse_stereo_eq_configuration, parse_gender, - parse_age_range, + u8_parser::, parse_base_hear_id, parse_sound_mode, parse_dual_fw, diff --git a/soundcore-lib/src/packets/response/state/a3029.rs b/soundcore-lib/src/packets/response/state/a3029.rs index 8a2b210..9718d86 100644 --- a/soundcore-lib/src/packets/response/state/a3029.rs +++ b/soundcore-lib/src/packets/response/state/a3029.rs @@ -16,12 +16,12 @@ use crate::{ }, parsers::{ parse_base_hear_id, parse_bool, parse_dual_fw, parse_fw, parse_serial_number, - parse_single_battery, + parse_single_battery, u8_parser, }, }; use crate::parsers::{ - bool_parser, parse_a3909_button_model, parse_age_range, parse_custom_hear_id, + bool_parser, parse_a3909_button_model, parse_custom_hear_id, parse_dual_battery, parse_gender, parse_sound_mode, parse_stereo_eq_configuration, ParseError, ParseResult, }; @@ -81,7 +81,7 @@ pub fn parse_a3029_state_response<'a, E: ParseError<'a>>( parse_stereo_eq_configuration, parse_gender, le_u8, - parse_age_range, + u8_parser::, parse_base_hear_id, parse_sound_mode, parse_dual_fw, diff --git a/soundcore-lib/src/packets/response/state/a3040.rs b/soundcore-lib/src/packets/response/state/a3040.rs new file mode 100644 index 0000000..9d3f2b3 --- /dev/null +++ b/soundcore-lib/src/packets/response/state/a3040.rs @@ -0,0 +1,12 @@ +use serde::{Serialize, Deserialize}; + +use crate::models::{SingleBattery, FirmwareVer, SerialNumber}; + + +#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, Hash)] +pub struct A3040StateResponse { + pub battery: SingleBattery, + pub fw: FirmwareVer, + pub sn: SerialNumber, + +} \ No newline at end of file diff --git a/soundcore-lib/src/packets/response/state/a3930.rs b/soundcore-lib/src/packets/response/state/a3930.rs index c14d06d..211dc76 100644 --- a/soundcore-lib/src/packets/response/state/a3930.rs +++ b/soundcore-lib/src/packets/response/state/a3930.rs @@ -7,14 +7,14 @@ use nom::{ }; use serde::{Deserialize, Serialize}; -use crate::models::{ +use crate::{models::{ A3909ButtonModel, AgeRange, Battery, ButtonModel, CustomHearID, DualBattery, EQConfiguration, Gender, HearID, SideTone, SoundMode, SoundcoreFeatureFlags, StereoEQConfiguration, TouchTone, TwsStatus, WearDetection, -}; +}, parsers::u8_parser}; use crate::parsers::{ - bool_parser, parse_a3909_button_model, parse_age_range, parse_custom_hear_id, + bool_parser, parse_a3909_button_model, parse_custom_hear_id, parse_dual_battery, parse_gender, parse_sound_mode, parse_stereo_eq_configuration, ParseError, ParseResult, }; @@ -96,7 +96,7 @@ pub fn parse_a3930_state_response<'a, E: ParseError<'a>>( parse_stereo_eq_configuration, parse_gender, le_u8, - parse_age_range, + u8_parser::, parse_custom_hear_id, parse_a3909_button_model, parse_sound_mode, diff --git a/soundcore-lib/src/packets/response/state/a3951.rs b/soundcore-lib/src/packets/response/state/a3951.rs index b713a61..2e6e87c 100644 --- a/soundcore-lib/src/packets/response/state/a3951.rs +++ b/soundcore-lib/src/packets/response/state/a3951.rs @@ -7,14 +7,14 @@ use nom::{ }; use serde::{Deserialize, Serialize}; -use crate::models::{ - self, A3909ButtonModel, AgeRange, Battery, ButtonModel, CustomHearID, DualBattery, +use crate::{models::{ + A3909ButtonModel, AgeRange, Battery, ButtonModel, CustomHearID, DualBattery, EQConfiguration, Gender, HearID, SideTone, SoundMode, SoundcoreFeatureFlags, StereoEQConfiguration, TouchTone, TwsStatus, WearDetection, -}; +}, parsers::u8_parser}; use crate::parsers::{ - bool_parser, parse_a3909_button_model, parse_age_range, parse_custom_hear_id, + bool_parser, parse_a3909_button_model, parse_custom_hear_id, parse_dual_battery, parse_gender, parse_sound_mode, parse_stereo_eq_configuration, ParseError, ParseResult, }; @@ -102,7 +102,7 @@ pub fn parse_a3951_state_response<'a, E: ParseError<'a>>( parse_dual_battery, parse_stereo_eq_configuration, parse_gender, - parse_age_range, + u8_parser::, parse_custom_hear_id, parse_a3909_button_model, parse_sound_mode, diff --git a/soundcore-lib/src/parsers.rs b/soundcore-lib/src/parsers.rs index 2b5ec1f..0975520 100644 --- a/soundcore-lib/src/parsers.rs +++ b/soundcore-lib/src/parsers.rs @@ -4,7 +4,6 @@ use nom::{ }; pub use a3909_button_model::*; -pub use age::*; pub use auto_power::*; pub use base::*; pub use battery::*; @@ -12,19 +11,15 @@ pub use checksum::*; pub use eq::*; pub use eq_configuration::*; pub use fw::*; -pub use game_mode::*; pub use gender::*; pub use hearid::*; pub use packet_header::*; pub use serial::*; pub use sound_mode::*; -pub use touch_tone::*; -pub use wear_detection::*; use crate::types::SupportedModels; mod a3909_button_model; -mod age; mod auto_power; mod base; mod battery; @@ -32,15 +27,11 @@ mod checksum; mod eq; mod eq_configuration; mod fw; -mod game_mode; mod gender; mod hearid; mod packet_header; mod serial; -mod side_tone; mod sound_mode; -mod touch_tone; -mod wear_detection; pub type ParseResult<'a, T, E> = IResult<&'a [u8], T, E>; pub type TaggedParseResult<'a, T, E> = IResult<&'a [u8], TaggedData, E>; @@ -55,6 +46,7 @@ pub struct TaggedData { pub tag: SupportedModels, } + #[cfg(test)] #[allow(dead_code)] pub type TestParserError<'a> = nom::error::VerboseError<&'a [u8]>; diff --git a/soundcore-lib/src/parsers/age.rs b/soundcore-lib/src/parsers/age.rs deleted file mode 100644 index 5dbc15a..0000000 --- a/soundcore-lib/src/parsers/age.rs +++ /dev/null @@ -1,9 +0,0 @@ -use nom::{combinator::map, error::context, number::complete::le_u8}; - -use crate::models::AgeRange; - -use super::{ParseError, ParseResult}; - -pub fn parse_age_range<'a, E: ParseError<'a>>(bytes: &'a [u8]) -> ParseResult { - context("parse_age_range", map(le_u8, AgeRange::from_u8))(bytes) -} diff --git a/soundcore-lib/src/parsers/game_mode.rs b/soundcore-lib/src/parsers/game_mode.rs deleted file mode 100644 index e69de29..0000000 diff --git a/soundcore-lib/src/parsers/side_tone.rs b/soundcore-lib/src/parsers/side_tone.rs deleted file mode 100644 index f8c4249..0000000 --- a/soundcore-lib/src/parsers/side_tone.rs +++ /dev/null @@ -1,10 +0,0 @@ -use nom::combinator::map; -use nom::error::context; - -use crate::models::SideTone; - -use super::{parse_bool, ParseError, ParseResult}; - -pub fn parse_side_tone<'a, E: ParseError<'a>>(bytes: &'a [u8]) -> ParseResult { - context("parse_side_tone", map(parse_bool, SideTone::from))(bytes) -} diff --git a/soundcore-lib/src/parsers/touch_tone.rs b/soundcore-lib/src/parsers/touch_tone.rs deleted file mode 100644 index 364cd6b..0000000 --- a/soundcore-lib/src/parsers/touch_tone.rs +++ /dev/null @@ -1,7 +0,0 @@ -use super::{base::parse_bool, ParseError, ParseResult}; -use crate::models::TouchTone; -use nom::{combinator::map, error::context}; - -pub fn parse_touch_tone<'a, E: ParseError<'a>>(bytes: &'a [u8]) -> ParseResult { - context("parse_touch_tone", map(parse_bool, TouchTone::from))(bytes) -} diff --git a/soundcore-lib/src/parsers/wear_detection.rs b/soundcore-lib/src/parsers/wear_detection.rs deleted file mode 100644 index a8fbd31..0000000 --- a/soundcore-lib/src/parsers/wear_detection.rs +++ /dev/null @@ -1,11 +0,0 @@ -use nom::{combinator::map, error::context}; - -use crate::models::WearDetection; - -use super::{base::parse_bool, ParseError, ParseResult}; - -pub fn parse_wear_detection<'a, E: ParseError<'a>>( - bytes: &'a [u8], -) -> ParseResult { - context("parse_wear_detection", map(parse_bool, WearDetection::from))(bytes) -}