generated from marc2332/tauri-deno-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from SARDONYX-sard/feature/fix-is_connected-ge…
…tter fix: fix `is_connected` getter
- Loading branch information
Showing
10 changed files
with
184 additions
and
123 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
crates/bluetooth/src/device/windows/device_info/buffer/errors.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,18 @@ | ||
use snafu::Snafu; | ||
use windows::core::Error; | ||
|
||
use crate::device::windows::device_info::device_property::DevPropType; | ||
|
||
/// Custom error type using snafu | ||
#[derive(Debug, Snafu)] | ||
pub enum DevicePropertyError { | ||
#[snafu(display("Failed to retrieve device property: {}", source))] | ||
DevicePropertyError { source: Error }, | ||
|
||
#[snafu(display( | ||
"Expected device property type {}, but got {}", | ||
DevPropType::from_u32(*expected).map_or("Unknown", |t|t.as_str()), | ||
DevPropType::from_u32(*actual).map_or("Unknown", |t|t.as_str()), | ||
))] | ||
TypeError { actual: u32, expected: u32 }, | ||
} |
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
File renamed without changes.
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 was deleted.
Oops, something went wrong.
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,11 @@ | ||
#[derive(Debug, snafu::Snafu)] | ||
pub enum RevealError { | ||
/// Failed to cast key '{key}' to the expected type '{expected_type}' | ||
TypeCastError { key: String, expected_type: String }, | ||
/// Unknown type for key '{key}': {unknown_type} | ||
UnknownTypeError { key: String, unknown_type: String }, | ||
|
||
#[snafu(transparent)] | ||
#[cfg(target_os = "windows")] | ||
Error { source: windows::core::Error }, | ||
} |
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,56 @@ | ||
use windows::core::{IInspectable, Interface as _, HSTRING}; | ||
use windows::Foundation::IReference; | ||
|
||
use crate::device::device_info::LocalTime; | ||
|
||
pub trait RevealValue: Sized { | ||
/// Reveals the value from an IInspectable. | ||
/// | ||
/// # Errors | ||
/// Returns an error if the value cannot be cast to the expected type. | ||
fn reveal(value: &IInspectable) -> windows::core::Result<Self>; | ||
} | ||
|
||
impl RevealValue for bool { | ||
fn reveal(value: &IInspectable) -> windows::core::Result<Self> { | ||
value.cast::<IReference<Self>>()?.Value() | ||
} | ||
} | ||
|
||
impl RevealValue for String { | ||
fn reveal(value: &IInspectable) -> windows::core::Result<Self> { | ||
Ok(value.cast::<IReference<HSTRING>>()?.Value()?.to_string()) | ||
} | ||
} | ||
|
||
impl RevealValue for u8 { | ||
fn reveal(value: &IInspectable) -> windows::core::Result<Self> { | ||
value.cast::<IReference<Self>>()?.Value() | ||
} | ||
} | ||
|
||
impl RevealValue for LocalTime { | ||
fn reveal(value: &IInspectable) -> windows::core::Result<Self> { | ||
let val = value | ||
.cast::<IReference<windows::Foundation::DateTime>>()? | ||
.Value()?; | ||
let utc_time = windows_datetime_to_chrono(val.UniversalTime); | ||
Ok(Self::from_utc(&utc_time)) | ||
} | ||
} | ||
|
||
fn windows_datetime_to_chrono(universal_time: i64) -> chrono::DateTime<chrono::Utc> { | ||
use chrono::TimeZone as _; | ||
|
||
// Windows FILETIME epoch (1601-01-01) to Unix epoch (1970-01-01) in 100ns units | ||
const EPOCH_DIFFERENCE_100NS: i64 = 11_644_473_600 * 10_000_000; | ||
// Adjust to Unix epoch | ||
let unix_time_100ns = universal_time - EPOCH_DIFFERENCE_100NS; | ||
// Convert 100ns to seconds and nanoseconds | ||
let seconds = unix_time_100ns / 10_000_000; | ||
let nanoseconds = (unix_time_100ns % 10_000_000) * 100; | ||
// Create chrono::DateTime | ||
chrono::Utc | ||
.timestamp_opt(seconds, nanoseconds as u32) | ||
.unwrap() | ||
} |
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,65 @@ | ||
mod errors; | ||
mod impls; | ||
|
||
use windows::core::{IInspectable, HSTRING}; | ||
use windows::Foundation::Collections::IKeyValuePair; | ||
|
||
pub use errors::RevealError; | ||
pub use impls::RevealValue; | ||
|
||
/// print property key and value. | ||
/// | ||
/// # Errors | ||
/// If failed to type cast | ||
pub fn reveal_value<T>(prop: IKeyValuePair<HSTRING, IInspectable>) -> Result<T, RevealError> | ||
where | ||
T: RevealValue, | ||
{ | ||
let key = prop.Key()?; | ||
let value = prop.Value()?; | ||
|
||
let runtime_class_name = value.GetRuntimeClassName()?.to_string(); | ||
match runtime_class_name.as_str() { | ||
"Windows.Foundation.IReference`1<Boolean>" => T::reveal(&value).map_or_else( | ||
|_| { | ||
Err(RevealError::TypeCastError { | ||
key: key.to_string(), | ||
expected_type: "Boolean".to_string(), | ||
}) | ||
}, | ||
Ok, | ||
), | ||
"Windows.Foundation.IReference`1<String>" => T::reveal(&value).map_or_else( | ||
|_| { | ||
Err(RevealError::TypeCastError { | ||
key: key.to_string(), | ||
expected_type: "String".to_string(), | ||
}) | ||
}, | ||
Ok, | ||
), | ||
"Windows.Foundation.IReference`1<UInt8>" => T::reveal(&value).map_or_else( | ||
|_| { | ||
Err(RevealError::TypeCastError { | ||
key: key.to_string(), | ||
expected_type: "UInt8".to_string(), | ||
}) | ||
}, | ||
Ok, | ||
), | ||
"Windows.Foundation.IReference`1<Windows.Foundation.DateTime>" => T::reveal(&value) | ||
.map_or_else( | ||
|_| { | ||
Err(RevealError::TypeCastError { | ||
key: key.to_string(), | ||
expected_type: "DateTime".to_string(), | ||
}) | ||
}, | ||
Ok, | ||
), | ||
unknown => Err(RevealError::UnknownTypeError { | ||
key: key.to_string(), | ||
unknown_type: unknown.to_string(), | ||
}), | ||
} | ||
} |
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 |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
"HKEY", | ||
"HRESULT", | ||
"HSTRING", | ||
"impls", | ||
"Inspectable", | ||
"LOCALMFG", | ||
"PCSTR", | ||
"PCWSTR", | ||
|