Skip to content

Commit

Permalink
feat(A3040): Begin A3040 parser work and refactor "toggle" structs
Browse files Browse the repository at this point in the history
  • Loading branch information
gmallios committed Dec 10, 2023
1 parent 638ebce commit 5230a52
Show file tree
Hide file tree
Showing 23 changed files with 110 additions and 152 deletions.
1 change: 1 addition & 0 deletions soundcore-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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/" }
14 changes: 2 additions & 12 deletions soundcore-lib/src/models.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod a3909_button_model;
mod age;
mod anc_mode;
mod auto_power;
mod battery;
Expand All @@ -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::*;
Expand All @@ -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::*;
16 changes: 0 additions & 16 deletions soundcore-lib/src/models/age.rs

This file was deleted.

6 changes: 0 additions & 6 deletions soundcore-lib/src/models/game_mode.rs

This file was deleted.

68 changes: 68 additions & 0 deletions soundcore-lib/src/models/misc.rs
Original file line number Diff line number Diff line change
@@ -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);
12 changes: 0 additions & 12 deletions soundcore-lib/src/models/side_tone.rs

This file was deleted.

12 changes: 0 additions & 12 deletions soundcore-lib/src/models/touch_tone.rs

This file was deleted.

12 changes: 0 additions & 12 deletions soundcore-lib/src/models/tws_status.rs

This file was deleted.

12 changes: 0 additions & 12 deletions soundcore-lib/src/models/wear_detection.rs

This file was deleted.

12 changes: 6 additions & 6 deletions soundcore-lib/src/packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,14 +19,14 @@ impl<T> 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
Expand Down
2 changes: 2 additions & 0 deletions soundcore-lib/src/packets/response/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
6 changes: 3 additions & 3 deletions soundcore-lib/src/packets/response/state/a3027.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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::<AgeRange, E>,
parse_base_hear_id,
parse_sound_mode,
parse_dual_fw,
Expand Down
6 changes: 3 additions & 3 deletions soundcore-lib/src/packets/response/state/a3028.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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::<AgeRange, E>,
parse_base_hear_id,
parse_sound_mode,
parse_dual_fw,
Expand Down
6 changes: 3 additions & 3 deletions soundcore-lib/src/packets/response/state/a3029.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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::<AgeRange, E>,
parse_base_hear_id,
parse_sound_mode,
parse_dual_fw,
Expand Down
12 changes: 12 additions & 0 deletions soundcore-lib/src/packets/response/state/a3040.rs
Original file line number Diff line number Diff line change
@@ -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,

}
8 changes: 4 additions & 4 deletions soundcore-lib/src/packets/response/state/a3930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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::<AgeRange, E>,
parse_custom_hear_id,
parse_a3909_button_model,
parse_sound_mode,
Expand Down
10 changes: 5 additions & 5 deletions soundcore-lib/src/packets/response/state/a3951.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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::<AgeRange, E>,
parse_custom_hear_id,
parse_a3909_button_model,
parse_sound_mode,
Expand Down
Loading

0 comments on commit 5230a52

Please sign in to comment.