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
30 // ENUM design that mixed the connection transport type with the device type. The connection can
31 // only be LE or Classic, but the device 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 // ENUM definition for profile disconnection status that in sync with ChromeOS structured metrics
116 // MetricProfileDisconnectionStatus and BlueZ's metrics_profile_disconn_state.
117 enum class MetricProfileDisconnectionStatus : int64_t {
118   PROFILE_DISCONN_STATE_STARTING = 0,
119   PROFILE_DISCONN_STATE_SUCCEED = 1,
120   PROFILE_DISCONN_STATE_ALREADY_DISCONNECTED = 2,
121   PROFILE_DISCONN_STATE_BUSY_DISCONNECTING = 3,
122   PROFILE_DISCONN_STATE_DISCONNECTION_REFUSED = 4,
123   PROFILE_DISCONN_STATE_DISCONNECTION_CANCELED = 5,
124   PROFILE_DISCONN_STATE_BT_IO_CONNECT_ERROR = 6,
125   PROFILE_DISCONN_STATE_INVALID_PARAMS = 7,
126   PROFILE_DISCONN_STATE_UNKNOWN_ERROR = 8,
127 };
128 
129 // ENUM definition for ACL connection status that in sync with ChromeOS structured metrics
130 // MetricAclConnectionStatus and BlueZ's metrics_conn_state.
131 enum class MetricAclConnectionStatus : int64_t {
132   ACL_CONN_STATE_STARTING = 0,
133   ACL_CONN_STATE_SUCCEED = 1,
134   ACL_CONN_STATE_ALREADY = 2,
135   ACL_CONN_STATE_BUSY = 3,
136   ACL_CONN_STATE_NONPOWERED = 4,
137   ACL_CONN_STATE_TIMEOUT = 5,
138   ACL_CONN_STATE_PROFILE_UNAVAILABLE = 6,
139   ACL_CONN_STATE_NOT_CONNECTED = 7,
140   ACL_CONN_STATE_NOT_PERMITTED = 8,
141   ACL_CONN_STATE_INVALID_PARAMS = 9,
142   ACL_CONN_STATE_CONNECTION_REFUSED = 10,
143   ACL_CONN_STATE_CANCELED = 11,
144   ACL_CONN_STATE_EVENT_INVALID = 12,
145   ACL_CONN_STATE_DEVICE_NOT_FOUND = 13,
146   ACL_CONN_STATE_BT_IO_CONNECT_ERROR = 14,
147   ACL_CONN_STATE_UNKNOWN_COMMAND = 15,
148   ACL_CONN_STATE_DISCONNECTED = 16,
149   ACL_CONN_STATE_CONNECT_FAILED = 17,
150   ACL_CONN_STATE_NOT_SUPPORTED = 18,
151   ACL_CONN_STATE_NO_RESOURCES = 19,
152   ACL_CONN_STATE_AUTH_FAILED = 20,
153   ACL_CONN_STATE_FAILED = 21,
154   ACL_CONN_STATE_UNKNOWN = 22,
155 };
156 
157 // ENUM definition for ACL disconnection status that in sync with ChromeOS structured metrics
158 // MetricAclDisconnectionStatus and BlueZ's metrics_disconn_state.
159 enum class MetricAclDisconnectionStatus : int64_t {
160   ACL_DISCONN_STATE_STARTING = 0,
161   ACL_DISCONN_STATE_TIMEOUT = 1,
162   ACL_DISCONN_STATE_LOCAL_HOST = 2,
163   ACL_DISCONN_STATE_REMOTE = 3,
164   ACL_DISCONN_STATE_AUTH_FAILURE = 4,
165   ACL_DISCONN_STATE_LOCAL_HOST_SUSPEND = 5,
166   ACL_DISCONN_STATE_UNKNOWN = 6,
167 };
168 
169 // A binary ENUM defines the metrics event is logged for: either for an attempt to connect or to
170 // disconnect.
171 enum class StateChangeType : int64_t {
172   STATE_CHANGE_TYPE_DISCONNECT = 0,
173   STATE_CHANGE_TYPE_CONNECT = 1
174 };
175 
176 // ENUM definition for ACL disconnection status that in sync with ChromeOS structured metrics
177 // MetricAclConnectionDirection and BlueZ's metrics_acl_connection_direction.
178 enum class MetricAclConnectionDirection : int64_t {
179   ACL_CONNECTION_DIRECTION_UNKNOWN = 0,
180   ACL_CONNECTION_OUTGOING = 1,
181   ACL_CONNECTION_INCOMING = 2,
182 };
183 
184 // ENUM definition for ACL disconnection status that in sync with ChromeOS structured metrics
185 // MetricAclConnectionInitiator and BlueZ's metrics_acl_connection_initiator.
186 enum class MetricAclConnectionInitiator : int64_t {
187   ACL_CONNECTION_INITIATOR_UNKNOWN = 0,
188   ACL_CONNECTION_INITIATOR_CLIENT = 1,
189   ACL_CONNECTION_INITIATOR_SYSTEM = 2,
190 };
191 
192 // ENUM definition for ACL disconnection status that in sync with ChromeOS structured metrics
193 // MetricTransportType and BlueZ's metrics_transport_type.
194 enum class MetricTransportType {
195   TRANSPORT_TYPE_UNKNOWN = 0,
196   TRANSPORT_TYPE_USB = 1,
197   TRANSPORT_TYPE_UART = 2,
198   TRANSPORT_TYPE_SDIO = 3,
199 };
200 
201 // ENUM definition for suspend id state that in sync with ChromeOS structured metrics
202 // BluetoothSuspendIdStateChanged/SuspendIdState.
203 enum class SuspendIdState : int64_t { NoRecord = 0, Recorded = 1 };
204 
205 // A struct holds the parsed profile connection event.
206 struct ProfileConnectionEvent {
207   int64_t type;
208   int64_t profile;
209   int64_t state;
210 };
211 
212 // Convert topshim::btif::BtState to AdapterState.
213 AdapterState ToAdapterState(uint32_t state);
214 
215 // Convert to SuspendIdState.
216 SuspendIdState ToSuspendIdState(uint32_t state);
217 
218 // Convert topshim::btif::BtDeviceType to ConnectionType
219 ConnectionType ToPairingDeviceType(std::string addr, uint32_t device_type);
220 
221 // Convert topshim::btif::bond_state info (status, addr, bond_state, and fail_reason) to
222 // PairingState
223 PairingState ToPairingState(uint32_t status, uint32_t bond_state, int32_t fail_reason);
224 
225 // Convert Floss profile connection info to ProfileConnectionEvent
226 ProfileConnectionEvent ToProfileConnectionEvent(std::string addr, uint32_t profile, uint32_t status,
227                                                 uint32_t state);
228 
229 // A struct holds the parsed ACL connection event.
230 struct AclConnectionEvent {
231   int64_t start_time;
232   int64_t state;
233   int64_t initiator;
234   int64_t direction;
235   int64_t start_status;
236   int64_t status;
237 };
238 
239 // Initialize a (dis)connection attempt event.
240 void PendingAclConnectAttemptEvent(std::string addr, int64_t time, uint32_t acl_state);
241 
242 // Convert Floss ACL connection info to AclConnectionEvent.
243 AclConnectionEvent ToAclConnectionEvent(std::string addr, int64_t time, uint32_t acl_status,
244                                         uint32_t acl_state, uint32_t direction,
245                                         uint32_t hci_reason);
246 
247 // A struct to hold the chipset info.
248 struct MetricsChipsetInfo {
249   int64_t vid;
250   int64_t pid;
251   int64_t transport;
252   std::string chipset_string;
253 };
254 
255 // Get the info of the chipset.
256 MetricsChipsetInfo GetMetricsChipsetInfo();
257 
258 }  // namespace metrics
259 }  // namespace bluetooth
260