-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lib): Add firmware version packet
Some devices don't provide the firmware version in their state update packet. For those, we need to request it separately. This can be done immediately after fetching the initial state.
- Loading branch information
Showing
15 changed files
with
192 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use nom::{ | ||
combinator::{all_consuming, map}, | ||
error::{context, ContextError, ParseError}, | ||
sequence::tuple, | ||
}; | ||
|
||
use crate::packets::{ | ||
parsing::{take_firmware_version, take_serial_number, ParseResult}, | ||
structures::{FirmwareVersion, SerialNumber}, | ||
}; | ||
|
||
// TODO think of a better name. this could be misleading since this does not update the firmware on the device, | ||
// it simply updates our state with the version number of the firmware running on the device. | ||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] | ||
pub struct FirmwareVersionUpdatePacket { | ||
pub left_firmware_version: FirmwareVersion, | ||
pub right_firmware_version: FirmwareVersion, | ||
pub serial_number: SerialNumber, | ||
} | ||
|
||
pub fn take_firmware_version_update_packet<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>( | ||
input: &'a [u8], | ||
) -> ParseResult<FirmwareVersionUpdatePacket, E> { | ||
context( | ||
"SoundModeUpdatePacket", | ||
all_consuming(map( | ||
tuple(( | ||
take_firmware_version, | ||
take_firmware_version, | ||
take_serial_number, | ||
)), | ||
|(left_firmware_version, right_firmware_version, serial_number)| { | ||
FirmwareVersionUpdatePacket { | ||
left_firmware_version, | ||
right_firmware_version, | ||
serial_number, | ||
} | ||
}, | ||
)), | ||
)(input) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use nom::error::VerboseError; | ||
|
||
use crate::packets::{ | ||
inbound::take_firmware_version_update_packet, | ||
parsing::{take_checksum, take_packet_header}, | ||
structures::{FirmwareVersion, SerialNumber}, | ||
}; | ||
|
||
fn strip(input: &[u8]) -> &[u8] { | ||
let input = take_checksum::<VerboseError<&[u8]>>(input).unwrap().0; | ||
let input = take_packet_header::<VerboseError<&[u8]>>(input).unwrap().0; | ||
input | ||
} | ||
|
||
#[test] | ||
fn it_parses_a_manually_crafted_packet() { | ||
let input: &[u8] = &[ | ||
0x09, 0xff, 0x00, 0x00, 0x01, 0x01, 0x05, 0x25, 0x00, 0x31, 0x32, 0x2e, 0x33, 0x34, | ||
0x32, 0x33, 0x2e, 0x34, 0x35, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, | ||
0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0xca, | ||
]; | ||
let input = strip(input); | ||
let packet = take_firmware_version_update_packet::<VerboseError<&[u8]>>(input) | ||
.unwrap() | ||
.1; | ||
assert_eq!(FirmwareVersion::new(12, 34), packet.left_firmware_version); | ||
assert_eq!(FirmwareVersion::new(23, 45), packet.right_firmware_version); | ||
assert_eq!( | ||
SerialNumber("0123456789ABCDEF".into()), | ||
packet.serial_number | ||
); | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
lib/src/packets/outbound/request_firmware_version_packet.rs
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,31 @@ | ||
use super::OutboundPacket; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] | ||
pub struct RequestFirmwareVersionPacket {} | ||
|
||
impl RequestFirmwareVersionPacket { | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
} | ||
|
||
impl OutboundPacket for RequestFirmwareVersionPacket { | ||
fn command(&self) -> [u8; 7] { | ||
[0x08, 0xee, 0x00, 0x00, 0x00, 0x01, 0x05] | ||
} | ||
|
||
fn body(&self) -> Vec<u8> { | ||
Vec::new() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::packets::outbound::{OutboundPacketBytes, RequestFirmwareVersionPacket}; | ||
|
||
#[test] | ||
fn it_matches_a_manually_crafted_packet() { | ||
let expected: &[u8] = &[0x08, 0xee, 0x00, 0x00, 0x00, 0x01, 0x05, 0x0a, 0x00, 0x06]; | ||
assert_eq!(expected, RequestFirmwareVersionPacket::new().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
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 |
---|---|---|
|
@@ -7,4 +7,5 @@ pub enum PacketType { | |
SetSoundModeOk, | ||
SetEqualizerOk, | ||
StateUpdate, | ||
FirmwareVersionUpdate, | ||
} |
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,5 +1,7 @@ | ||
mod ambient_sound_mode_update; | ||
mod firmware_version_update; | ||
mod state_update; | ||
|
||
pub use ambient_sound_mode_update::*; | ||
pub use firmware_version_update::*; | ||
pub use state_update::*; |
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,14 @@ | ||
use crate::{ | ||
packets::inbound::FirmwareVersionUpdatePacket, | ||
state::{DeviceState, DeviceStateTransformer}, | ||
}; | ||
|
||
impl DeviceStateTransformer for FirmwareVersionUpdatePacket { | ||
fn transform(&self, state: &DeviceState) -> DeviceState { | ||
DeviceState { | ||
left_firmware_version: Some(self.left_firmware_version), | ||
right_firmware_version: Some(self.right_firmware_version), | ||
..state.clone() | ||
} | ||
} | ||
} |
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