use alloc::ffi::CString;
use alloc::format;
use core::cell::RefCell;
use core::ffi::c_char;
use libadb::base::error::{Error, ProtocolError, ReverseError};
#[cfg(feature = "pairing")]
use libadb::pairing::PairingError;
use crate::transport::FfiConnectError;
#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AdbStatus {
Ok = 0,
InvalidArg = 1,
InvalidUri = 2,
Connect = 3,
Io = 4,
Auth = 5,
Protocol = 6,
ChannelClosed = 7,
NoFreeChannels = 8,
Desynchronized = 9,
Reverse = 10,
TlsRequired = 11,
Pairing = 12,
Disconnected = 13,
Internal = 255,
}
#[cfg(not(feature = "tls"))]
const TLS_REQUIRED: &str = "device requires TLS (Android 11+ wireless debugging): \
build libadb-ffi with the `tls` feature, connect over USB, \
or switch the device to plain TCP with `adb tcpip 5555`";
#[cfg(feature = "tls")]
const TLS_REQUIRED: &str = "device requires TLS (Android 11+ wireless debugging), \
which only adb_connect with the private key can start: \
adb_connect_with_authenticator stays in the clear";
std::thread_local! {
static LAST_ERROR: RefCell<Option<CString>> = const { RefCell::new(None) };
}
fn set(msg: impl core::fmt::Display) {
let s = format!("{msg}").replace('\0', " ");
let c = CString::new(s).unwrap_or_else(|_| CString::new("<error>").unwrap());
LAST_ERROR.with(|l| *l.borrow_mut() = Some(c));
}
pub(crate) fn clear_last_error() {
LAST_ERROR.with(|l| *l.borrow_mut() = None);
}
pub(crate) fn fail_invalid_arg(msg: impl core::fmt::Display) -> AdbStatus {
set(msg);
AdbStatus::InvalidArg
}
pub(crate) fn fail_io(msg: impl core::fmt::Display) -> AdbStatus {
set(msg);
AdbStatus::Io
}
pub(crate) fn fail_auth(msg: impl core::fmt::Display) -> AdbStatus {
set(msg);
AdbStatus::Auth
}
pub(crate) fn fail_internal(msg: impl core::fmt::Display) -> AdbStatus {
set(msg);
AdbStatus::Internal
}
#[cfg(feature = "pairing")]
pub(crate) fn fail_invalid_uri(msg: impl core::fmt::Display) -> AdbStatus {
set(msg);
AdbStatus::InvalidUri
}
#[cfg(feature = "pairing")]
pub(crate) fn fail_pairing(e: PairingError<std::io::Error>) -> AdbStatus {
let status = match &e {
PairingError::WrongCode => AdbStatus::Pairing,
PairingError::Closed => AdbStatus::Pairing,
PairingError::Transport(_) => AdbStatus::Io,
PairingError::Frame(_) | PairingError::Spake2(_) | PairingError::PeerInfo(_) => {
AdbStatus::Protocol
}
_ => AdbStatus::Internal,
};
set(e);
status
}
pub(crate) fn fail_ffi_connect(e: FfiConnectError) -> AdbStatus {
let status = match &e {
FfiConnectError::Uri(_) => AdbStatus::InvalidUri,
_ => AdbStatus::Connect,
};
set(e);
status
}
pub(crate) fn fail_error<E: core::fmt::Display>(e: Error<E>) -> AdbStatus {
let status = match &e {
Error::Protocol(ProtocolError::TlsRequired) => {
set(TLS_REQUIRED);
return AdbStatus::TlsRequired;
}
Error::Io(_) | Error::UnexpectedEof => AdbStatus::Io,
Error::Auth(_) => AdbStatus::Auth,
Error::Protocol(_) => AdbStatus::Protocol,
Error::ChannelClosed => AdbStatus::ChannelClosed,
Error::NoFreeChannels => AdbStatus::NoFreeChannels,
Error::Desynchronized => AdbStatus::Desynchronized,
Error::Disconnected => AdbStatus::Disconnected,
Error::Reverse(ReverseError::Failed(_)) => AdbStatus::Reverse,
Error::Reverse(ReverseError::UnexpectedReply) => AdbStatus::Protocol,
_ => AdbStatus::Internal,
};
set(e);
status
}
#[unsafe(no_mangle)]
pub extern "C" fn adb_last_error() -> *const c_char {
LAST_ERROR.with(|l| match &*l.borrow() {
Some(c) => c.as_ptr(),
None => core::ptr::null(),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn disconnected_reaches_c_as_its_own_status() {
let status = fail_error(Error::<core::convert::Infallible>::Disconnected);
assert_eq!(status, AdbStatus::Disconnected);
assert_eq!(
status as i32, 13,
"must match ADB_ERR_DISCONNECTED in libadb.h"
);
}
}