Skip to content

Commit 6e2d920

Browse files
committed
Version 0.2.3
1 parent 16faa5b commit 6e2d920

File tree

6 files changed

+237
-203
lines changed

6 files changed

+237
-203
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changes
22

3+
## 0.2.3
4+
* Bump `jni-min-helper` to 0.3. Threatened by the UB found in <https://github.com/wuwbobo2021/android-bluetooth-serial-rs/issues/2>.
5+
* `check_attached_intent()` no longer returns the device if the permission is lost.
6+
* Added default `serialport` feature which can be turned off (this choice will be removed in 0.3.0, see <https://github.com/kevinmehall/nusb/pull/150>).
7+
38
## 0.2.2
49
* Fixed support for newest Android versions: `check_attached_intent()` does not work, `PermissionRequest` never returns the result of being permitted, both are caused by the bad implementation of `PartialEq` for `DeviceInfo`.
510
* Added `UsbSerial` trait to prepare for driver implementations of non-CDC serial adapters.

Cargo.toml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "android-usbser"
3-
version = "0.2.2"
3+
version = "0.2.3"
44
authors = ["wuwbobo2021 <wuwbobo@outlook.com>"]
55
edition = "2021"
66
license = "MIT OR Apache-2.0"
@@ -12,11 +12,8 @@ Android host driver for USB serial adapters, currently works with CDC-ACM device
1212
keywords = ["usb", "serial", "serialport", "uart", "android"]
1313
categories = ["api-bindings", "hardware-support"]
1414

15-
[package.metadata.android.sdk]
16-
min_sdk_version = 16
17-
target_sdk_version = 30
18-
1915
[package.metadata.docs.rs]
16+
features = ["serialport"]
2017
default-target = "aarch64-linux-android"
2118
targets = [
2219
"aarch64-linux-android",
@@ -25,14 +22,18 @@ targets = [
2522
"x86_64-linux-android",
2623
]
2724

25+
[features]
26+
default = ["serialport"]
27+
serialport = ["dep:serialport"]
28+
2829
[dependencies]
2930
log = "0.4"
3031
getset = "0.1"
3132
nusb = "0.1.12"
32-
serialport = "4.6"
3333
futures-core = "0.3"
3434
futures-lite = "2.5"
35-
jni-min-helper = { version = "0.2.6", features = ["futures"] }
35+
jni-min-helper = { version = "0.3", features = ["futures"] }
36+
serialport = { version = "4.7", optional = true }
3637

3738
[lib]
3839
name = "android_usbser"

lib.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@
1111
//! The initial version of this crate performs USB transfers through JNI calls but not `nusb`,
1212
//! do not use it except you have encountered compatibility problems.
1313
14-
mod ser_cdc;
1514
mod usb_conn;
1615
mod usb_info;
1716
mod usb_sync;
17+
18+
#[cfg(feature = "serialport")]
19+
mod ser_cdc;
20+
#[cfg(feature = "serialport")]
1821
pub use ser_cdc::*;
1922

2023
/// Equals `std::io::Error`.
2124
pub type Error = std::io::Error;
2225

23-
/// Android helper for `nusb`. It may be merged into that crate in the future.
26+
/// Android helper for `nusb`. It may be removed in version 0.3.0 (this is blocked by
27+
/// <https://github.com/kevinmehall/nusb/pull/150>).
2428
///
2529
/// Reference:
2630
/// - <https://developer.android.com/develop/connectivity/usb/host>
@@ -40,26 +44,27 @@ pub mod usb {
4044
use jni_min_helper::*;
4145
if let JavaException = err {
4246
let err = jni_clear_ex(err);
43-
jni_last_cleared_ex()
44-
.ok_or(JavaException)
45-
.and_then(|ex| Ok((ex, jni_attach_vm()?)))
46-
.and_then(|(ex, ref mut env)| {
47-
Ok((ex.get_class_name(env)?, ex.get_throwable_msg(env)?))
48-
})
49-
.map(|(cls, msg)| Error::other(format!("{cls}: {msg}")))
50-
.unwrap_or(Error::other(err))
47+
if let Some(ex) = jni_last_cleared_ex() {
48+
jni_with_env(|env| Ok((ex.get_class_name(env)?, ex.get_throwable_msg(env)?)))
49+
.map(|(cls, msg)| Error::other(format!("{cls}: {msg}")))
50+
.unwrap_or(Error::other(err))
51+
} else {
52+
Error::other(err)
53+
}
5154
} else {
5255
Error::other(err)
5356
}
5457
}
5558
}
5659

60+
#[cfg(feature = "serialport")]
5761
use nusb::transfer::{Queue, RequestBuffer};
5862

5963
/// Serial driver implementations inside this crate should implement this trait.
6064
///
6165
/// TODO: add crate-level functions `probe() -> Result<Vec<DeviceInfo>, Error>`
6266
/// and `open(dev_info: &DeviceInfo, timeout: Duration) -> Result<Box<dyn UsbSerial>, Error>`.
67+
#[cfg(feature = "serialport")]
6368
pub trait UsbSerial: serialport::SerialPort {
6469
/// Sets baudrate, parity check mode, data bits and stop bits.
6570
fn configure(&mut self, conf: &SerialConfig) -> std::io::Result<()>;
@@ -72,9 +77,11 @@ pub trait UsbSerial: serialport::SerialPort {
7277
fn sealer(_: private::Internal);
7378
}
7479

80+
#[cfg(feature = "serialport")]
7581
use serialport::{DataBits, Parity, StopBits};
7682

7783
/// Serial parameters including baudrate, parity check mode, data bits and stop bits.
84+
#[cfg(feature = "serialport")]
7885
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
7986
pub struct SerialConfig {
8087
pub baud_rate: u32,
@@ -83,6 +90,7 @@ pub struct SerialConfig {
8390
pub stop_bits: StopBits,
8491
}
8592

93+
#[cfg(feature = "serialport")]
8694
impl Default for SerialConfig {
8795
fn default() -> Self {
8896
Self {
@@ -94,6 +102,7 @@ impl Default for SerialConfig {
94102
}
95103
}
96104

105+
#[cfg(feature = "serialport")]
97106
impl std::str::FromStr for SerialConfig {
98107
type Err = Error;
99108

@@ -153,6 +162,7 @@ impl std::str::FromStr for SerialConfig {
153162
}
154163
}
155164

165+
#[cfg(feature = "serialport")]
156166
impl std::fmt::Display for SerialConfig {
157167
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
158168
let baud_rate = self.baud_rate;
@@ -175,6 +185,7 @@ impl std::fmt::Display for SerialConfig {
175185
}
176186
}
177187

188+
#[allow(unused)]
178189
mod private {
179190
/// Used as a parameter of the hidden function in sealed traits.
180191
#[derive(Debug)]

ser_cdc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ impl CdcSerial {
6767
let endps: Vec<_> = alt.endpoints().collect();
6868
let endp_r = endps.iter().find(|endp| endp.direction() == Direction::In);
6969
let endp_w = endps.iter().find(|endp| endp.direction() == Direction::Out);
70-
if endp_r.is_some() && endp_w.is_some() {
71-
addr_r = Some(endp_r.unwrap().address());
72-
addr_w = Some(endp_w.unwrap().address());
70+
if let (Some(endp_r), Some(endp_w)) = (endp_r, endp_w) {
71+
addr_r = Some(endp_r.address());
72+
addr_w = Some(endp_w.address());
7373
break;
7474
}
7575
}

0 commit comments

Comments
 (0)