-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(soundcore_lib): ✨ Add DeviceInfo with parser for A3951 and other…
… small fixes
- Loading branch information
Showing
17 changed files
with
135 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod request; | ||
mod response; | ||
|
||
pub use request::*; | ||
pub use response::*; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// TODO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// TODO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// TODO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,28 @@ | ||
mod a3951; | ||
|
||
pub use a3951::*; | ||
use nom::{combinator::map, error::context}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::models::{DeviceFirmware, SerialNumber}; | ||
use crate::{ | ||
models::{DeviceFirmware, SerialNumber}, | ||
parsers::{SoundcoreParseError, SoundcoreParseResult}, | ||
}; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone, Hash, Default)] | ||
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone, Hash)] | ||
pub struct DeviceInfoResponse { | ||
pub sn: Option<SerialNumber>, | ||
pub fw: Option<DeviceFirmware>, | ||
} | ||
|
||
// TODO: Add more parsers | ||
pub fn parse_device_info_packet<'a, E: SoundcoreParseError<'a>>( | ||
bytes: &'a [u8], | ||
) -> SoundcoreParseResult<DeviceInfoResponse, E> { | ||
context("parse_device_info", |bytes| { | ||
map( | ||
parse_a3951_device_info_packet::<'a, E>, | ||
DeviceInfoResponse::from, | ||
)(bytes) | ||
})(bytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use nom::{ | ||
combinator::{all_consuming, map}, | ||
error::context, | ||
sequence::{pair}, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{ | ||
models::{DeviceFirmware, SerialNumber}, | ||
parsers::{parse_dual_fw, parse_serial_number, SoundcoreParseError, SoundcoreParseResult}, | ||
}; | ||
|
||
use super::DeviceInfoResponse; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone, Hash)] | ||
pub struct A3951DeviceInfoResponse { | ||
pub sn: SerialNumber, | ||
pub fw: DeviceFirmware, | ||
} | ||
|
||
pub fn parse_a3951_device_info_packet<'a, E: SoundcoreParseError<'a>>( | ||
bytes: &'a [u8], | ||
) -> SoundcoreParseResult<A3951DeviceInfoResponse, E> { | ||
context( | ||
"parse_a3951_device_info", | ||
all_consuming(map(pair(parse_dual_fw, parse_serial_number), |(fw, sn)| { | ||
A3951DeviceInfoResponse { | ||
fw: DeviceFirmware::DUAL(fw.0, fw.1), | ||
sn, | ||
} | ||
})), | ||
)(bytes) | ||
} | ||
|
||
impl From<A3951DeviceInfoResponse> for DeviceInfoResponse { | ||
fn from(value: A3951DeviceInfoResponse) -> Self { | ||
DeviceInfoResponse { | ||
sn: Some(value.sn), | ||
fw: Some(value.fw), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use nom::{combinator::map, error::context, sequence::pair}; | ||
|
||
use crate::models::FirmwareVer; | ||
|
||
use super::{parse_str, SoundcoreParseError, SoundcoreParseResult}; | ||
|
||
pub fn parse_fw<'a, E: SoundcoreParseError<'a>>( | ||
bytes: &'a [u8], | ||
) -> SoundcoreParseResult<FirmwareVer, E> { | ||
context( | ||
"parse_fw", | ||
map(parse_str(5usize), |ver| { | ||
let (major, minor) = match ver.split_once('.') { | ||
Some((major_str, minor_str)) => ( | ||
major_str.parse().unwrap_or_default(), | ||
minor_str.parse().unwrap_or_default(), | ||
), | ||
None => (0, 0), | ||
}; | ||
FirmwareVer::new(major, minor) | ||
}), | ||
)(bytes) | ||
} | ||
|
||
pub fn parse_dual_fw<'a, E: SoundcoreParseError<'a>>( | ||
bytes: &'a [u8], | ||
) -> SoundcoreParseResult<(FirmwareVer, FirmwareVer), E> { | ||
context("map_dual_fw", pair(parse_fw, parse_fw))(bytes) | ||
} | ||
|
||
#[cfg(test)] | ||
mod fw_parser { | ||
// TODO: Add tests | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters