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 #include <bluetooth/log.h>
21 
22 #include <cstdint>
23 #include <string>
24 
25 #include "macros.h"
26 
27 /*
28  *  Definitions for HCI Error Codes that are passed in the events
29  */
30 typedef enum : uint8_t {
31   HCI_SUCCESS = 0x00,
32   HCI_ERR_ILLEGAL_COMMAND = 0x01,
33   HCI_ERR_NO_CONNECTION = 0x02,
34   HCI_ERR_HW_FAILURE = 0x03,
35   HCI_ERR_PAGE_TIMEOUT = 0x04,
36   HCI_ERR_AUTH_FAILURE = 0x05,
37   HCI_ERR_KEY_MISSING = 0x06,
38   HCI_ERR_MEMORY_FULL = 0x07,
39   HCI_ERR_CONNECTION_TOUT = 0x08,
40   HCI_ERR_MAX_NUM_OF_CONNECTIONS = 0x09,
41   HCI_ERR_MAX_NUM_OF_SCOS = 0x0A,
42   HCI_ERR_CONNECTION_EXISTS = 0x0B,
43   HCI_ERR_COMMAND_DISALLOWED = 0x0C,
44   HCI_ERR_HOST_REJECT_RESOURCES = 0x0D,
45   HCI_ERR_HOST_REJECT_SECURITY = 0x0E,
46   HCI_ERR_HOST_REJECT_DEVICE = 0x0F,
47   HCI_ERR_HOST_TIMEOUT = 0x10,  // stack/btm/btm_ble_gap,
48   HCI_ERR_ILLEGAL_PARAMETER_FMT = 0x12,
49   HCI_ERR_PEER_USER = 0x13,
50   HCI_ERR_REMOTE_LOW_RESOURCE = 0x14,
51   HCI_ERR_REMOTE_POWER_OFF = 0x15,
52   HCI_ERR_CONN_CAUSE_LOCAL_HOST = 0x16,
53   HCI_ERR_REPEATED_ATTEMPTS = 0x17,
54   HCI_ERR_PAIRING_NOT_ALLOWED = 0x18,
55   HCI_ERR_UNSUPPORTED_REM_FEATURE = 0x1A,  // stack/btm/btm_ble_gap
56   HCI_ERR_UNSPECIFIED = 0x1F,
57   HCI_ERR_LMP_RESPONSE_TIMEOUT = 0x22,     // GATT_CONN_LMP_TIMEOUT
58   HCI_ERR_LMP_ERR_TRANS_COLLISION = 0x23,  // TODO remove
59   HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE = 0x25,
60   HCI_ERR_UNIT_KEY_USED = 0x26,
61   HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED = 0x29,
62   HCI_ERR_DIFF_TRANSACTION_COLLISION = 0x2A,  // stack/btm/btm_sec
63   HCI_ERR_INSUFFCIENT_SECURITY = 0x2F,        // btif/btu
64   HCI_ERR_ROLE_SWITCH_PENDING = 0x32,         // stack/btm/btm_sco
65   HCI_ERR_ROLE_SWITCH_FAILED = 0x35,
66   HCI_ERR_HOST_BUSY_PAIRING = 0x38,          // stack/btm/btm_sec
67   HCI_ERR_UNACCEPT_CONN_INTERVAL = 0x3B,     // stack/l2cap/l2c_ble
68   HCI_ERR_ADVERTISING_TIMEOUT = 0x3C,        // stack/btm/btm_ble
69   HCI_ERR_CONN_FAILED_ESTABLISHMENT = 0x3E,  // GATT_CONN_FAIL_ESTABLISH
70   HCI_ERR_LIMIT_REACHED = 0x43,              // stack/btm/btm_ble_multi_adv.cc
71 
72   _HCI_ERR_MAX_ERR = 0x43,
73   HCI_ERR_UNDEFINED = 0xff,
74 } tHCI_ERROR_CODE;
75 
76 #define HCI_ERR_MAX_ERR _HCI_ERR_MAX_ERR  // HACK for now for SMP
77 
hci_error_code_text(const tHCI_ERROR_CODE & error_code)78 inline std::string hci_error_code_text(const tHCI_ERROR_CODE& error_code) {
79   switch (error_code) {
80     CASE_RETURN_TEXT(HCI_SUCCESS);
81     CASE_RETURN_TEXT(HCI_ERR_ILLEGAL_COMMAND);
82     CASE_RETURN_TEXT(HCI_ERR_NO_CONNECTION);
83     CASE_RETURN_TEXT(HCI_ERR_HW_FAILURE);
84     CASE_RETURN_TEXT(HCI_ERR_PAGE_TIMEOUT);
85     CASE_RETURN_TEXT(HCI_ERR_AUTH_FAILURE);
86     CASE_RETURN_TEXT(HCI_ERR_KEY_MISSING);
87     CASE_RETURN_TEXT(HCI_ERR_MEMORY_FULL);
88     CASE_RETURN_TEXT(HCI_ERR_CONNECTION_TOUT);
89     CASE_RETURN_TEXT(HCI_ERR_MAX_NUM_OF_CONNECTIONS);
90     CASE_RETURN_TEXT(HCI_ERR_MAX_NUM_OF_SCOS);
91     CASE_RETURN_TEXT(HCI_ERR_CONNECTION_EXISTS);
92     CASE_RETURN_TEXT(HCI_ERR_COMMAND_DISALLOWED);
93     CASE_RETURN_TEXT(HCI_ERR_HOST_REJECT_RESOURCES);
94     CASE_RETURN_TEXT(HCI_ERR_HOST_REJECT_SECURITY);
95     CASE_RETURN_TEXT(HCI_ERR_HOST_REJECT_DEVICE);
96     CASE_RETURN_TEXT(HCI_ERR_HOST_TIMEOUT);
97     CASE_RETURN_TEXT(HCI_ERR_ILLEGAL_PARAMETER_FMT);
98     CASE_RETURN_TEXT(HCI_ERR_PEER_USER);
99     CASE_RETURN_TEXT(HCI_ERR_REMOTE_LOW_RESOURCE);
100     CASE_RETURN_TEXT(HCI_ERR_REMOTE_POWER_OFF);
101     CASE_RETURN_TEXT(HCI_ERR_CONN_CAUSE_LOCAL_HOST);
102     CASE_RETURN_TEXT(HCI_ERR_REPEATED_ATTEMPTS);
103     CASE_RETURN_TEXT(HCI_ERR_PAIRING_NOT_ALLOWED);
104     CASE_RETURN_TEXT(HCI_ERR_UNSUPPORTED_REM_FEATURE);
105     CASE_RETURN_TEXT(HCI_ERR_UNSPECIFIED);
106     CASE_RETURN_TEXT(HCI_ERR_LMP_RESPONSE_TIMEOUT);
107     CASE_RETURN_TEXT(HCI_ERR_LMP_ERR_TRANS_COLLISION);
108     CASE_RETURN_TEXT(HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE);
109     CASE_RETURN_TEXT(HCI_ERR_UNIT_KEY_USED);
110     CASE_RETURN_TEXT(HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED);
111     CASE_RETURN_TEXT(HCI_ERR_DIFF_TRANSACTION_COLLISION);
112     CASE_RETURN_TEXT(HCI_ERR_INSUFFCIENT_SECURITY);
113     CASE_RETURN_TEXT(HCI_ERR_ROLE_SWITCH_PENDING);
114     CASE_RETURN_TEXT(HCI_ERR_ROLE_SWITCH_FAILED);
115     CASE_RETURN_TEXT(HCI_ERR_HOST_BUSY_PAIRING);
116     CASE_RETURN_TEXT(HCI_ERR_UNACCEPT_CONN_INTERVAL);
117     CASE_RETURN_TEXT(HCI_ERR_ADVERTISING_TIMEOUT);
118     CASE_RETURN_TEXT(HCI_ERR_CONN_FAILED_ESTABLISHMENT);
119     CASE_RETURN_TEXT(HCI_ERR_LIMIT_REACHED);
120     default:
121       return base::StringPrintf("UNKNOWN[0x%02hx]", error_code);
122   }
123 }
124 
125 // Context equivalence
126 using tHCI_STATUS = tHCI_ERROR_CODE;
hci_status_code_text(const tHCI_STATUS & status_code)127 inline std::string hci_status_code_text(const tHCI_STATUS& status_code) {
128   return hci_error_code_text(status_code);
129 }
130 
131 using tHCI_REASON = tHCI_ERROR_CODE;
hci_reason_code_text(const tHCI_REASON & reason_code)132 inline std::string hci_reason_code_text(const tHCI_REASON& reason_code) {
133   return hci_error_code_text(reason_code);
134 }
135 
136 // Conversion from raw packet value
to_hci_error_code(const uint8_t & error_code)137 inline tHCI_ERROR_CODE to_hci_error_code(const uint8_t& error_code) {
138   if (error_code > _HCI_ERR_MAX_ERR) return HCI_ERR_UNDEFINED;
139   return static_cast<tHCI_ERROR_CODE>(error_code);
140 }
141 
to_hci_status_code(const uint8_t & status_code)142 inline tHCI_STATUS to_hci_status_code(const uint8_t& status_code) {
143   if (status_code > _HCI_ERR_MAX_ERR) return HCI_ERR_UNDEFINED;
144   return static_cast<tHCI_STATUS>(status_code);
145 }
146 
to_hci_reason_code(const uint8_t & reason_code)147 inline tHCI_REASON to_hci_reason_code(const uint8_t& reason_code) {
148   if (reason_code > _HCI_ERR_MAX_ERR) return HCI_ERR_UNDEFINED;
149   return static_cast<tHCI_REASON>(reason_code);
150 }
151 
152 namespace fmt {
153 template <>
154 struct formatter<tHCI_ERROR_CODE> : enum_formatter<tHCI_ERROR_CODE> {};
155 }  // namespace fmt
156