Skip to content

Commit

Permalink
wip: sound mode update
Browse files Browse the repository at this point in the history
  • Loading branch information
gmallios committed Oct 17, 2023
1 parent e9ee629 commit 208868a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
36 changes: 36 additions & 0 deletions soundcore-lib/src/packets/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,60 @@ use nom::error::VerboseError;
#[derive(Debug)]
pub enum ResponsePacket {
DeviceState(DeviceStateResponse),
SoundModeUpdate(SoundModeUpdateResponse),
DeviceInfo, // TODO
}

impl ResponsePacket {
pub fn from_bytes(bytes: &[u8]) -> Result<Self, nom::Err<VerboseError<&[u8]>>> {
let bytes = parse_and_check_checksum(bytes)?.0;
let (bytes, packet_header) = parse_packet_header(bytes)?;
println!("Packet header: {:?}", packet_header);
Ok(match packet_header.kind {
ResponsePacketKind::StateUpdate => {
Self::DeviceState(parse_state_update_packet(bytes)?.1)
}
ResponsePacketKind::SoundModeUpdate => {
Self::SoundModeUpdate(parse_sound_mode_update_packet(bytes)?.1)
}
_ => unimplemented!(),
})
}
}

mod info;
mod sound_mode;
mod state;

pub use info::*;
pub use sound_mode::*;
pub use state::*;

#[cfg(test)]
mod response_test {
use super::ResponsePacket;

const A3951_STATE_UPDATE_BYTES: [u8; 97] = [
9, 255, 0, 0, 1, 1, 1, 97, 0, 1, 1, 5, 5, 1, 0, 254, 254, 160, 150, 130, 120, 120, 120,
120, 120, 160, 150, 130, 120, 120, 120, 120, 120, 255, 255, 0, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 99, 1, 84, 1, 102, 1,
84, 0, 1, 0, 0, 0, 1, 1, 6, 0, 1, 0, 0, 0, 0, 242,
];

#[test]
fn sound_mode_update() {
let bytes = [
0x09, 0xFF, 0x00, 0x00, 0x01, 0x06, 0x01, 0x0E, 0x00, 0x00, 0x01, 0x01, 0x06, 0x26,
];

let packet = ResponsePacket::from_bytes(&bytes).unwrap();
assert!(matches!(packet, ResponsePacket::SoundModeUpdate(_)));
}

#[test]
fn a3951_state_update() {
let packet = ResponsePacket::from_bytes(&A3951_STATE_UPDATE_BYTES).unwrap();
assert!(matches!(packet, ResponsePacket::DeviceState(_)));
}
}
19 changes: 19 additions & 0 deletions soundcore-lib/src/packets/response/sound_mode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use nom::{combinator::map, error::context};
use serde::{Deserialize, Serialize};

use crate::{
models::SoundMode,
parsers::{parse_sound_mode, SoundcoreParseError, SoundcoreParseResult},
};

#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, Hash)]
pub struct SoundModeUpdateResponse(pub SoundMode);

pub fn parse_sound_mode_update_packet<'a, E: SoundcoreParseError<'a>>(
bytes: &'a [u8],
) -> SoundcoreParseResult<SoundModeUpdateResponse, E> {
context(
"parse_sound_mode_update",
map(parse_sound_mode, SoundModeUpdateResponse),
)(bytes)
}
1 change: 1 addition & 0 deletions soundcore-lib/src/packets/response/state/a3951.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub fn parse_a3951_state_response<'a, E: SoundcoreParseError<'a>>(
// Optional Fields
let (bytes, hearid_eq_preset) = opt(le_u16)(bytes)?;
let (bytes, new_battery) = opt(tuple((le_u8, le_u8)))(bytes)?;
let (bytes, unknown) = opt(le_u8)(bytes)?; // TODO: Unknown field

Ok((
bytes,
Expand Down

0 comments on commit 208868a

Please sign in to comment.