1use std::io;
4
5#[derive(Debug, thiserror::Error)]
7#[non_exhaustive]
8pub enum Error {
9 #[error("transport: {0}")]
11 Io(#[from] io::Error),
12 #[error("controller returned {status_name} ({status:#04x}) for command {opcode:#06x}")]
14 Hci {
15 opcode: u16,
17 status: u8,
19 status_name: String,
21 },
22 #[error("not supported by this controller: {0}")]
25 Unsupported(&'static str),
26 #[error("timed out waiting for {0}")]
28 Timeout(&'static str),
29 #[error("disconnected")]
31 Disconnected,
32 #[error("ATT error {code:#04x} ({}) on handle {handle:#06x} for request {request:#04x}", crate::att::error_name(*code))]
34 Att {
35 request: u8,
37 handle: u16,
39 code: u8,
41 },
42 #[error("pairing failed: {0}")]
44 Pairing(String),
45 #[error("protocol error: {0}")]
47 Protocol(String),
48 #[error("invalid parameter: {0}")]
50 InvalidParameter(String),
51 #[error("the host has stopped")]
53 Stopped,
54}
55
56pub type Result<T, E = Error> = std::result::Result<T, E>;
58
59impl Error {
60 pub(crate) fn hci(opcode: u16, status: u8) -> Self {
61 Error::Hci {
62 opcode,
63 status,
64 status_name: status_name(status).to_string(),
65 }
66 }
67}
68
69pub fn status_name(code: u8) -> &'static str {
71 match code {
72 0x00 => "Success",
73 0x01 => "Unknown HCI Command",
74 0x02 => "Unknown Connection Identifier",
75 0x03 => "Hardware Failure",
76 0x04 => "Page Timeout",
77 0x05 => "Authentication Failure",
78 0x06 => "PIN or Key Missing",
79 0x07 => "Memory Capacity Exceeded",
80 0x08 => "Connection Timeout",
81 0x09 => "Connection Limit Exceeded",
82 0x0a => "Synchronous Connection Limit To A Device Exceeded",
83 0x0b => "Connection Already Exists",
84 0x0c => "Command Disallowed",
85 0x0d => "Connection Rejected due to Limited Resources",
86 0x0e => "Connection Rejected Due To Security Reasons",
87 0x0f => "Connection Rejected due to Unacceptable BD_ADDR",
88 0x10 => "Connection Accept Timeout Exceeded",
89 0x11 => "Unsupported Feature or Parameter Value",
90 0x12 => "Invalid HCI Command Parameters",
91 0x13 => "Remote User Terminated Connection",
92 0x14 => "Remote Device Terminated Connection due to Low Resources",
93 0x15 => "Remote Device Terminated Connection due to Power Off",
94 0x16 => "Connection Terminated By Local Host",
95 0x17 => "Repeated Attempts",
96 0x18 => "Pairing Not Allowed",
97 0x19 => "Unknown LMP PDU",
98 0x1a => "Unsupported Remote Feature",
99 0x1b => "SCO Offset Rejected",
100 0x1c => "SCO Interval Rejected",
101 0x1d => "SCO Air Mode Rejected",
102 0x1e => "Invalid LMP Parameters / Invalid LL Parameters",
103 0x1f => "Unspecified Error",
104 0x20 => "Unsupported LMP Parameter Value / Unsupported LL Parameter Value",
105 0x21 => "Role Change Not Allowed",
106 0x22 => "LMP Response Timeout / LL Response Timeout",
107 0x23 => "LMP Error Transaction Collision / LL Procedure Collision",
108 0x24 => "LMP PDU Not Allowed",
109 0x25 => "Encryption Mode Not Acceptable",
110 0x26 => "Link Key cannot be Changed",
111 0x27 => "Requested QoS Not Supported",
112 0x28 => "Instant Passed",
113 0x29 => "Pairing With Unit Key Not Supported",
114 0x2a => "Different Transaction Collision",
115 0x2c => "QoS Unacceptable Parameter",
116 0x2d => "QoS Rejected",
117 0x2e => "Channel Classification Not Supported",
118 0x2f => "Insufficient Security",
119 0x30 => "Parameter Out Of Mandatory Range",
120 0x32 => "Role Switch Pending",
121 0x34 => "Reserved Slot Violation",
122 0x35 => "Role Switch Failed",
123 0x36 => "Extended Inquiry Response Too Large",
124 0x37 => "Secure Simple Pairing Not Supported By Host",
125 0x38 => "Host Busy - Pairing",
126 0x39 => "Connection Rejected due to No Suitable Channel Found",
127 0x3a => "Controller Busy",
128 0x3b => "Unacceptable Connection Parameters",
129 0x3c => "Advertising Timeout",
130 0x3d => "Connection Terminated due to MIC Failure",
131 0x3e => "Connection Failed to be Established / Synchronization Timeout",
132 0x40 => "Coarse Clock Adjustment Rejected but Will Try to Adjust Using Clock Dragging",
133 0x41 => "Type0 Submap Not Defined",
134 0x42 => "Unknown Advertising Identifier",
135 0x43 => "Limit Reached",
136 0x44 => "Operation Cancelled by Host",
137 0x45 => "Packet Too Long",
138 0x46 => "Too Late",
139 0x47 => "Too Early",
140 0x48 => "Insufficient Channels",
141 _ => "Reserved",
142 }
143}