-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add async USB HID transport #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
okhsunrog
wants to merge
5
commits into
hauntedfail:main
Choose a base branch
from
okhsunrog:agent/usb-transport
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ee06457
fix(protocol): validate SCP responses across BLE
okhsunrog ed85be5
fix(protocol): strip holder product CRC
okhsunrog b4839e2
feat(usb): add async nusb HID transport
okhsunrog d846471
test: add BLE and USB hardware validation
okhsunrog e02a999
fix: address transport review feedback
okhsunrog File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,24 @@ | ||
| use iqos::{Iqos, IqosUsb, ProductNumberKind}; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| let usb = IqosUsb::connect().await?; | ||
| println!("Connected: {}", usb.location()); | ||
| println!("Model: {:?}", usb.model()); | ||
| println!("USB metadata: {:?}", usb.device_info()); | ||
|
|
||
| let iqos = Iqos::new(usb); | ||
| let product_number = iqos.read_product_number(ProductNumberKind::Stick).await?; | ||
| println!("Product number: {product_number}"); | ||
|
|
||
| let battery_voltage = iqos.read_battery_voltage().await?; | ||
| println!("Battery voltage: {battery_voltage:.3} V"); | ||
|
|
||
| let diagnosis = iqos.read_diagnosis().await?; | ||
| println!( | ||
| "Diagnosis: puffs={:?} days={:?} battery={:?}", | ||
| diagnosis.total_smoking_count, diagnosis.days_used, diagnosis.battery_voltage | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or 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 hidden or 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,114 @@ | ||
| //! Runtime-selectable IQOS transport. | ||
|
|
||
| use async_trait::async_trait; | ||
|
|
||
| use crate::{ | ||
| Result, | ||
| protocol::{DeviceInfo, DeviceModel}, | ||
| transport::{Transport, TransportKind}, | ||
| }; | ||
|
|
||
| #[cfg(feature = "btleplug-support")] | ||
| use crate::IqosBle; | ||
| #[cfg(feature = "usb-support")] | ||
| use crate::{Error, IqosUsb}; | ||
|
|
||
| /// Runtime transport used by applications that support both BLE and USB. | ||
| #[derive(Debug)] | ||
| pub enum IqosTransport { | ||
| /// Bluetooth Low Energy session. | ||
| #[cfg(feature = "btleplug-support")] | ||
| Ble(IqosBle), | ||
| /// USB HID session. | ||
| #[cfg(feature = "usb-support")] | ||
| Usb(IqosUsb), | ||
| } | ||
|
|
||
| impl IqosTransport { | ||
| /// Return the detected device model. | ||
| #[must_use] | ||
| pub const fn model(&self) -> DeviceModel { | ||
| match self { | ||
| #[cfg(feature = "btleplug-support")] | ||
| Self::Ble(transport) => transport.model(), | ||
| #[cfg(feature = "usb-support")] | ||
| Self::Usb(transport) => transport.model(), | ||
| } | ||
| } | ||
|
|
||
| /// Return metadata collected while opening the transport. | ||
| #[must_use] | ||
| pub const fn device_info(&self) -> &DeviceInfo { | ||
| match self { | ||
| #[cfg(feature = "btleplug-support")] | ||
| Self::Ble(transport) => transport.device_info(), | ||
| #[cfg(feature = "usb-support")] | ||
| Self::Usb(transport) => transport.device_info(), | ||
| } | ||
| } | ||
|
|
||
| /// Return the BLE battery percentage when available. | ||
| /// | ||
| /// USB exposes battery voltage through SCP but does not provide the direct | ||
| /// GATT percentage characteristic. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error when the BLE read fails or when called on USB. | ||
| #[cfg_attr(not(feature = "btleplug-support"), allow(clippy::unused_async))] | ||
| pub async fn read_battery_level(&self) -> Result<u8> { | ||
| match self { | ||
| #[cfg(feature = "btleplug-support")] | ||
| Self::Ble(transport) => transport.read_battery_level().await, | ||
| #[cfg(feature = "usb-support")] | ||
| Self::Usb(_) => Err(Error::Unsupported( | ||
| "battery percentage is unavailable over USB; read battery voltage instead" | ||
| .to_string(), | ||
| )), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "btleplug-support")] | ||
| impl From<IqosBle> for IqosTransport { | ||
| fn from(transport: IqosBle) -> Self { | ||
| Self::Ble(transport) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "usb-support")] | ||
| impl From<IqosUsb> for IqosTransport { | ||
| fn from(transport: IqosUsb) -> Self { | ||
| Self::Usb(transport) | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl Transport for IqosTransport { | ||
| fn kind(&self) -> TransportKind { | ||
| match self { | ||
| #[cfg(feature = "btleplug-support")] | ||
| Self::Ble(transport) => transport.kind(), | ||
| #[cfg(feature = "usb-support")] | ||
| Self::Usb(transport) => transport.kind(), | ||
| } | ||
| } | ||
|
|
||
| async fn request(&self, command: &[u8]) -> Result<Vec<u8>> { | ||
| match self { | ||
| #[cfg(feature = "btleplug-support")] | ||
| Self::Ble(transport) => transport.request(command).await, | ||
| #[cfg(feature = "usb-support")] | ||
| Self::Usb(transport) => transport.request(command).await, | ||
| } | ||
| } | ||
|
|
||
| async fn send(&self, command: &[u8]) -> Result<()> { | ||
| match self { | ||
| #[cfg(feature = "btleplug-support")] | ||
| Self::Ble(transport) => transport.send(command).await, | ||
| #[cfg(feature = "usb-support")] | ||
| Self::Usb(transport) => transport.send(command).await, | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.