1 /*
2  * Copyright 2022 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 #pragma once
17 
18 #include <cstdint>
19 #include <string>
20 
21 namespace bluetooth {
22 namespace metrics {
23 
24 // ENUM definition for adapter state that in sync with ChromeOS structured metrics
25 // BluetoothAdapterStateChanged/AdapterState.
26 enum class AdapterState : int64_t { OFF = 0, ON = 1 };
27 
28 // ENUM definition for device/connection type that in sync with ChromeOS structured metrics
29 // BluetoothPairingStateChanged/DeviceType and BlueZ metrics_conn_type. Note this is a non-optimal ENUM design that
30 // mixed the connection transport type with the device type. The connection can only be LE or Classic, but the device
31 // type can also be Dual.
32 enum class ConnectionType : int64_t {
33   CONN_TYPE_UNKNOWN = 0,
34   CONN_TYPE_BREDR = 1,
35   CONN_TYPE_LE = 2,
36   CONN_TYPE_END = 3,
37 };
38 
39 // ENUM definition for pairing state that in sync with ChromeOS structured metrics
40 // BluetoothPairingStateChanged/PairingState and BlueZ metrics_pair_result.
41 enum class PairingState : int64_t {
42   PAIR_STARTING = 0,
43   PAIR_SUCCEED = 1,
44   // The controller is not powered.
45   PAIR_FAIL_NONPOWERED = 2,
46   // The remote device has been paired with the local host.
47   PAIR_FAIL_ALREADY_PAIRED = 3,
48   // This can be invalid address type, invalid IO capability.
49   PAIR_FAIL_INVALID_PARAMS = 4,
50   // The pairing is in progress or being canceled.
51   PAIR_FAIL_BUSY = 5,
52   // Simple pairing or pairing is not supported on the remote device.
53   PAIR_FAIL_NOT_SUPPORTED = 6,
54   // Fail to set up connection with the remote device.
55   PAIR_FAIL_ESTABLISH_CONN = 7,
56   // The authentication failure can be caused by incorrect PIN/link key or
57   // missing PIN/link key during pairing or authentication procedure.
58   // This can also be a failure during message integrity check.
59   PAIR_FAIL_AUTH_FAILED = 8,
60   // The pairing request is rejected by the remote device.
61   PAIR_FAIL_REJECTED = 9,
62   // The pairing was cancelled.
63   PAIR_FAIL_CANCELLED = 10,
64   // The connection was timeout.
65   PAIR_FAIL_TIMEOUT = 11,
66   PAIR_FAIL_UNKNOWN = 12,
67   // BT IO connection error
68   PAIR_FAIL_BT_IO_CONNECT_ERROR = 13,
69   // Unknown command.
70   PAIR_FAIL_UNKNOWN_COMMAND = 14,
71   // The peer was not connected.
72   PAIR_FAIL_NOT_CONNECTED = 15,
73   // Exceeded the limit of resource such as memory, connections.
74   PAIR_FAIL_NO_RESOURCES = 16,
75   // Disconnected due to power, user termination or other reasons.
76   PAIR_FAIL_DISCONNECTED = 17,
77   // Failed due to all the other reasons such as hardware, invalid LMP
78   // PDU, transaction collision, role change, slot violation etc.
79   PAIR_FAIL_FAILED = 18,
80   PAIR_FAIL_END = 19,
81 };
82 
83 // ENUM definition for pairing state that in sync with ChromeOS structured metrics
84 // BluetoothProfileConnectionStateChanged/Profile and BlueZ metrics_bluetooth_profile.
85 enum class Profile : int64_t {
86   UNKNOWN = 0,
87   HSP = 1,
88   HFP = 2,
89   A2DP = 3,
90   AVRCP = 4,
91   HID = 5,
92   HOG = 6,
93   GATT = 7,
94   GAP = 8,
95   DEVICE_INFO = 9,
96   BATTERY = 10,
97   NEARBY = 11,
98   PHONEHUB = 12,
99 };
100 
101 // ENUM definition for profile connection status that in sync with ChromeOS structured metrics
102 // MetricProfileConnectionStatus and BlueZ's metrics_profile_conn_state.
103 enum class MetricProfileConnectionStatus : int64_t {
104   PROFILE_CONN_STATE_STARTING = 0,
105   PROFILE_CONN_STATE_SUCCEED = 1,
106   PROFILE_CONN_STATE_ALREADY_CONNECTED = 2,
107   PROFILE_CONN_STATE_BUSY_CONNECTING = 3,
108   PROFILE_CONN_STATE_CONNECTION_REFUSED = 4,
109   PROFILE_CONN_STATE_CONNECTION_CANCELED = 5,
110   PROFILE_CONN_STATE_REMOTE_UNAVAILABLE = 6,
111   PROFILE_CONN_STATE_PROFILE_NOT_SUPPORTED = 7,
112   PROFILE_CONN_STATE_UNKNOWN_ERROR = 8,
113 
114 };
115 
116 // ENUM definition for profile disconnection status that in sync with ChromeOS structured metrics
117 // MetricProfileDisconnectionStatus and BlueZ's metrics_profile_disconn_state.
118 enum class MetricProfileDisconnectionStatus : int64_t {
119   PROFILE_DISCONN_STATE_STARTING = 0,
120   PROFILE_DISCONN_STATE_SUCCEED = 1,
121   PROFILE_DISCONN_STATE_ALREADY_DISCONNECTED = 2,
122   PROFILE_DISCONN_STATE_BUSY_DISCONNECTING = 3,
123   PROFILE_DISCONN_STATE_DISCONNECTION_REFUSED = 4,
124   PROFILE_DISCONN_STATE_DISCONNECTION_CANCELED = 5,
125   PROFILE_DISCONN_STATE_BT_IO_CONNECT_ERROR = 6,
126   PROFILE_DISCONN_STATE_INVALID_PARAMS = 7,
127   PROFILE_DISCONN_STATE_UNKNOWN_ERROR = 8,
128 };
129 
130 // ENUM definition for ACL connection status that in sync with ChromeOS structured metrics
131 // MetricAclConnectionStatus and BlueZ's metrics_conn_state.
132 enum class MetricAclConnectionStatus : int64_t {
133   ACL_CONN_STATE_STARTING = 0,
134   ACL_CONN_STATE_SUCCEED = 1,
135   ACL_CONN_STATE_ALREADY = 2,
136   ACL_CONN_STATE_BUSY = 3,
137   ACL_CONN_STATE_NONPOWERED = 4,
138   ACL_CONN_STATE_TIMEOUT = 5,
139   ACL_CONN_STATE_PROFILE_UNAVAILABLE = 6,
140   ACL_CONN_STATE_NOT_CONNECTED = 7,
141   ACL_CONN_STATE_NOT_PERMITTED = 8,
142   ACL_CONN_STATE_INVALID_PARAMS = 9,
143   ACL_CONN_STATE_CONNECTION_REFUSED = 10,
144   ACL_CONN_STATE_CANCELED = 11,
145   ACL_CONN_STATE_EVENT_INVALID = 12,
146   ACL_CONN_STATE_DEVICE_NOT_FOUND = 13,
147   ACL_CONN_STATE_BT_IO_CONNECT_ERROR = 14,
148   ACL_CONN_STATE_UNKNOWN_COMMAND = 15,
149   ACL_CONN_STATE_DISCONNECTED = 16,
150   ACL_CONN_STATE_CONNECT_FAILED = 17,
151   ACL_CONN_STATE_NOT_SUPPORTED = 18,
152   ACL_CONN_STATE_NO_RESOURCES = 19,
153   ACL_CONN_STATE_AUTH_FAILED = 20,
154   ACL_CONN_STATE_FAILED = 21,
155   ACL_CONN_STATE_UNKNOWN = 22,
156 };
157 
158 // ENUM definition for ACL disconnection status that in sync with ChromeOS structured metrics
159 // MetricAclDisconnectionStatus and BlueZ's metrics_disconn_state.
160 enum class MetricAclDisconnectionStatus : int64_t {
161   ACL_DISCONN_STATE_STARTING = 0,
162   ACL_DISCONN_STATE_TIMEOUT = 1,
163   ACL_DISCONN_STATE_LOCAL_HOST = 2,
164   ACL_DISCONN_STATE_REMOTE = 3,
165   ACL_DISCONN_STATE_AUTH_FAILURE = 4,
166   ACL_DISCONN_STATE_LOCAL_HOST_SUSPEND = 5,
167   ACL_DISCONN_STATE_UNKNOWN = 6,
168 };
169 
170 // A binary ENUM defines the metrics event is logged for: either for an attempt to connect or to disconnect.
171 enum class StateChangeType : int64_t { STATE_CHANGE_TYPE_DISCONNECT = 0, STATE_CHANGE_TYPE_CONNECT = 1 };
172 
173 // ENUM definition for ACL disconnection status that in sync with ChromeOS structured metrics
174 // MetricAclConnectionDirection and BlueZ's metrics_acl_connection_direction.
175 enum class MetricAclConnectionDirection : int64_t {
176   ACL_CONNECTION_DIRECTION_UNKNOWN = 0,
177   ACL_CONNECTION_OUTGOING = 1,
178   ACL_CONNECTION_INCOMING = 2,
179 };
180 
181 // ENUM definition for ACL disconnection status that in sync with ChromeOS structured metrics
182 // MetricAclConnectionInitiator and BlueZ's metrics_acl_connection_initiator.
183 enum class MetricAclConnectionInitiator : int64_t {
184   ACL_CONNECTION_INITIATOR_UNKNOWN = 0,
185   ACL_CONNECTION_INITIATOR_CLIENT = 1,
186   ACL_CONNECTION_INITIATOR_SYSTEM = 2,
187 };
188 
189 // ENUM definition for ACL disconnection status that in sync with ChromeOS structured metrics
190 // MetricTransportType and BlueZ's metrics_transport_type.
191 enum class MetricTransportType {
192   TRANSPORT_TYPE_UNKNOWN = 0,
193   TRANSPORT_TYPE_USB = 1,
194   TRANSPORT_TYPE_UART = 2,
195   TRANSPORT_TYPE_SDIO = 3,
196 };
197 
198 // ENUM definition for suspend id state that in sync with ChromeOS structured metrics
199 // BluetoothSuspendIdStateChanged/SuspendIdState.
200 enum class SuspendIdState : int64_t { NoRecord = 0, Recorded = 1 };
201 
202 // A struct holds the parsed profile connection event.
203 struct ProfileConnectionEvent {
204   int64_t type;
205   int64_t profile;
206   int64_t state;
207 };
208 
209 // Convert topshim::btif::BtState to AdapterState.
210 AdapterState ToAdapterState(uint32_t state);
211 
212 // Convert to SuspendIdState.
213 SuspendIdState ToSuspendIdState(uint32_t state);
214 
215 // Convert topshim::btif::BtDeviceType to ConnectionType
216 ConnectionType ToPairingDeviceType(std::string addr, uint32_t device_type);
217 
218 // Convert topshim::btif::bond_state info (status, addr, bond_state, and fail_reason) to PairingState
219 PairingState ToPairingState(uint32_t status, uint32_t bond_state, int32_t fail_reason);
220 
221 // Convert Floss profile connection info to ProfileConnectionEvent
222 ProfileConnectionEvent ToProfileConnectionEvent(std::string addr, uint32_t profile, uint32_t status, uint32_t state);
223 
224 // A struct holds the parsed ACL connection event.
225 struct AclConnectionEvent {
226   int64_t start_time;
227   int64_t state;
228   int64_t initiator;
229   int64_t direction;
230   int64_t start_status;
231   int64_t status;
232 };
233 
234 // Initialize a (dis)connection attempt event.
235 void PendingAclConnectAttemptEvent(std::string addr, int64_t time, uint32_t acl_state);
236 
237 // Convert Floss ACL connection info to AclConnectionEvent.
238 AclConnectionEvent ToAclConnectionEvent(
239     std::string addr, int64_t time, uint32_t acl_status, uint32_t acl_state, uint32_t direction, uint32_t hci_reason);
240 
241 // A struct to hold the chipset info.
242 struct MetricsChipsetInfo {
243   int64_t vid;
244   int64_t pid;
245   int64_t transport;
246   std::string chipset_string;
247 };
248 
249 // Get the info of the chipset.
250 MetricsChipsetInfo GetMetricsChipsetInfo();
251 
252 }  // namespace metrics
253 }  // namespace bluetooth
254