1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #pragma once
18
19 #include <base/strings/stringprintf.h>
20
21 #include <cstdint>
22 #include <string>
23
24 /*
25 * Definitions for HCI Error Codes that are passed in the events
26 */
27 typedef enum : uint8_t {
28 HCI_SUCCESS = 0x00,
29 HCI_ERR_ILLEGAL_COMMAND = 0x01,
30 HCI_ERR_NO_CONNECTION = 0x02,
31 HCI_ERR_HW_FAILURE = 0x03,
32 HCI_ERR_PAGE_TIMEOUT = 0x04,
33 HCI_ERR_AUTH_FAILURE = 0x05,
34 HCI_ERR_KEY_MISSING = 0x06,
35 HCI_ERR_MEMORY_FULL = 0x07,
36 HCI_ERR_CONNECTION_TOUT = 0x08,
37 HCI_ERR_MAX_NUM_OF_CONNECTIONS = 0x09,
38 HCI_ERR_MAX_NUM_OF_SCOS = 0x0A,
39 HCI_ERR_CONNECTION_EXISTS = 0x0B,
40 HCI_ERR_COMMAND_DISALLOWED = 0x0C,
41 HCI_ERR_HOST_REJECT_RESOURCES = 0x0D,
42 HCI_ERR_HOST_REJECT_SECURITY = 0x0E,
43 HCI_ERR_HOST_REJECT_DEVICE = 0x0F,
44 HCI_ERR_HOST_TIMEOUT = 0x10, // stack/btm/btm_ble_gap,
45 HCI_ERR_ILLEGAL_PARAMETER_FMT = 0x12,
46 HCI_ERR_PEER_USER = 0x13,
47 HCI_ERR_REMOTE_POWER_OFF = 0x15,
48 HCI_ERR_CONN_CAUSE_LOCAL_HOST = 0x16,
49 HCI_ERR_REPEATED_ATTEMPTS = 0x17,
50 HCI_ERR_PAIRING_NOT_ALLOWED = 0x18,
51 HCI_ERR_UNSUPPORTED_REM_FEATURE = 0x1A, // stack/btm/btm_ble_gap
52 HCI_ERR_UNSPECIFIED = 0x1F,
53 HCI_ERR_LMP_RESPONSE_TIMEOUT = 0x22, // GATT_CONN_LMP_TIMEOUT
54 HCI_ERR_LMP_ERR_TRANS_COLLISION = 0x23, // TODO remove
55 HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE = 0x25,
56 HCI_ERR_UNIT_KEY_USED = 0x26,
57 HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED = 0x29,
58 HCI_ERR_DIFF_TRANSACTION_COLLISION = 0x2A, // stack/btm/btm_sec
59 HCI_ERR_INSUFFCIENT_SECURITY = 0x2F, // btif/btu
60 HCI_ERR_ROLE_SWITCH_PENDING = 0x32, // stack/btm/btm_sco
61 HCI_ERR_ROLE_SWITCH_FAILED = 0x35,
62 HCI_ERR_HOST_BUSY_PAIRING = 0x38, // stack/btm/btm_sec
63 HCI_ERR_UNACCEPT_CONN_INTERVAL = 0x3B, // stack/l2cap/l2c_ble
64 HCI_ERR_ADVERTISING_TIMEOUT = 0x3C, // stack/btm/btm_ble
65 HCI_ERR_CONN_FAILED_ESTABLISHMENT = 0x3E, // GATT_CONN_FAIL_ESTABLISH
66 HCI_ERR_LIMIT_REACHED = 0x43, // stack/btm/btm_ble_multi_adv.cc
67
68 _HCI_ERR_MAX_ERR = 0x43,
69 HCI_ERR_UNDEFINED = 0xff,
70 } tHCI_ERROR_CODE;
71
72 #define HCI_ERR_MAX_ERR _HCI_ERR_MAX_ERR // HACK for now for SMP
73
hci_error_code_text(const tHCI_ERROR_CODE & error_code)74 inline std::string hci_error_code_text(const tHCI_ERROR_CODE& error_code) {
75 switch (error_code) {
76 case HCI_SUCCESS:
77 return std::string("Success");
78 case HCI_ERR_ILLEGAL_COMMAND:
79 return std::string("Illegal Command");
80 case HCI_ERR_NO_CONNECTION:
81 return std::string("Unknown Connection");
82 case HCI_ERR_HW_FAILURE:
83 return std::string("Hardware Failure");
84 case HCI_ERR_PAGE_TIMEOUT:
85 return std::string("Page Timeout");
86 case HCI_ERR_AUTH_FAILURE:
87 return std::string("Authentication Failure");
88 case HCI_ERR_KEY_MISSING:
89 return std::string("Pin or Key Missing");
90 case HCI_ERR_MEMORY_FULL:
91 return std::string("Memory Capacity Exceeded");
92 case HCI_ERR_CONNECTION_TOUT:
93 return std::string("Connection Timeout");
94 case HCI_ERR_MAX_NUM_OF_CONNECTIONS:
95 return std::string("Connection Limit Exceeded");
96 case HCI_ERR_MAX_NUM_OF_SCOS:
97 return std::string("Synchronous Connection Limit Exceeded");
98 case HCI_ERR_CONNECTION_EXISTS:
99 return std::string("Connection Already Exists");
100 case HCI_ERR_COMMAND_DISALLOWED:
101 return std::string("Command Disallowed");
102 case HCI_ERR_HOST_REJECT_RESOURCES:
103 return std::string("Connection Rejected Limited Resources");
104 case HCI_ERR_HOST_REJECT_SECURITY:
105 return std::string("Connection Rejected Security Reasons");
106 case HCI_ERR_HOST_REJECT_DEVICE:
107 return std::string("Connection Rejected Unacceptable BdAddr");
108 case HCI_ERR_HOST_TIMEOUT:
109 return std::string("Connection Accept Timeout");
110 case HCI_ERR_ILLEGAL_PARAMETER_FMT:
111 return std::string("Unsupported Feature or Parameter Value");
112 case HCI_ERR_PEER_USER:
113 return std::string("Remote Terminated Connection");
114 case HCI_ERR_CONN_CAUSE_LOCAL_HOST:
115 return std::string("Local Terminated Connection");
116 case HCI_ERR_REPEATED_ATTEMPTS:
117 return std::string("Repeated Attempts");
118 case HCI_ERR_PAIRING_NOT_ALLOWED:
119 return std::string("Pairing not Allowed");
120 case HCI_ERR_UNSUPPORTED_REM_FEATURE:
121 return std::string("Unsupported Remote or Lmp Feature");
122 case HCI_ERR_UNSPECIFIED:
123 return std::string("Unspecified Error");
124 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
125 return std::string("Gatt Connection Lmp Timeout");
126 case HCI_ERR_LMP_ERR_TRANS_COLLISION:
127 return std::string("Link Layer Collision");
128 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
129 return std::string("Encryption Mode not Acceptable");
130 case HCI_ERR_UNIT_KEY_USED:
131 return std::string("Unit Key Used");
132 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
133 return std::string("Pairing with Unit Key Unsupported");
134 case HCI_ERR_DIFF_TRANSACTION_COLLISION:
135 return std::string("Diff Transaction Collision");
136 case HCI_ERR_INSUFFCIENT_SECURITY:
137 return std::string("Insufficient Security");
138 case HCI_ERR_ROLE_SWITCH_PENDING:
139 return std::string("Role Switch Pending");
140 case HCI_ERR_HOST_BUSY_PAIRING:
141 return std::string("Host Busy Pairing");
142 case HCI_ERR_UNACCEPT_CONN_INTERVAL:
143 return std::string("Unacceptable Connection Interval");
144 case HCI_ERR_ADVERTISING_TIMEOUT:
145 return std::string("Advertising Timeout");
146 case HCI_ERR_CONN_FAILED_ESTABLISHMENT:
147 return std::string("Connection Failed Establishment");
148 case HCI_ERR_LIMIT_REACHED:
149 return std::string("Limit Reached");
150 default:
151 return base::StringPrintf("Unknown Error[%02hx]", error_code);
152 }
153 }
154
155 // Context equivalence
156 using tHCI_STATUS = tHCI_ERROR_CODE;
hci_status_code_text(const tHCI_STATUS & status_code)157 inline std::string hci_status_code_text(const tHCI_STATUS& status_code) {
158 return hci_error_code_text(status_code);
159 }
160
161 using tHCI_REASON = tHCI_ERROR_CODE;
hci_reason_code_text(const tHCI_REASON & reason_code)162 inline std::string hci_reason_code_text(const tHCI_REASON& reason_code) {
163 return hci_error_code_text(reason_code);
164 }
165
166 // Conversion from raw packet value
to_hci_error_code(const uint8_t & error_code)167 inline tHCI_ERROR_CODE to_hci_error_code(const uint8_t& error_code) {
168 if (error_code > _HCI_ERR_MAX_ERR) return HCI_ERR_UNDEFINED;
169 return static_cast<tHCI_ERROR_CODE>(error_code);
170 }
171
to_hci_status_code(const uint8_t & status_code)172 inline tHCI_STATUS to_hci_status_code(const uint8_t& status_code) {
173 if (status_code > _HCI_ERR_MAX_ERR) return HCI_ERR_UNDEFINED;
174 return static_cast<tHCI_STATUS>(status_code);
175 }
176
to_hci_reason_code(const uint8_t & reason_code)177 inline tHCI_REASON to_hci_reason_code(const uint8_t& reason_code) {
178 if (reason_code > _HCI_ERR_MAX_ERR) return HCI_ERR_UNDEFINED;
179 return static_cast<tHCI_REASON>(reason_code);
180 }
181