• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 2021 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #pragma once
20 
21 #include <frameworks/proto_logging/stats/enums/bluetooth/enums.pb.h>
22 #include <frameworks/proto_logging/stats/enums/bluetooth/hci/enums.pb.h>
23 #include <frameworks/proto_logging/stats/enums/bluetooth/le/enums.pb.h>
24 
25 #include "hci/address.h"
26 
27 namespace bluetooth {
28 
29 namespace os {
30 /**
31  * Unknown connection handle for metrics purpose
32  */
33 static const uint32_t kUnknownConnectionHandle = 0xFFFF;
34 
35 /**
36  * Log link layer connection event
37  *
38  * @param address Stack wide consistent Bluetooth address of this event,
39  *                nullptr if unknown
40  * @param connection_handle connection handle of this event,
41  *                          {@link kUnknownConnectionHandle} if unknown
42  * @param direction direction of this connection
43  * @param link_type type of the link
44  * @param hci_cmd HCI command opecode associated with this event, if any
45  * @param hci_event HCI event code associated with this event, if any
46  * @param hci_ble_event HCI BLE event code associated with this event, if any
47  * @param cmd_status Command status associated with this event, if any
48  * @param reason_code Reason code associated with this event, if any
49  */
50 void LogMetricLinkLayerConnectionEvent(
51     const hci::Address* address,
52     uint32_t connection_handle,
53     android::bluetooth::DirectionEnum direction,
54     uint16_t link_type,
55     uint32_t hci_cmd,
56     uint16_t hci_event,
57     uint16_t hci_ble_event,
58     uint16_t cmd_status,
59     uint16_t reason_code);
60 
61 /**
62  * Logs when Bluetooth controller failed to reply with command status within
63  * a timeout period after receiving an HCI command from the host
64  *
65  * @param hci_cmd opcode of HCI command that caused this timeout
66  */
67 void LogMetricHciTimeoutEvent(uint32_t hci_cmd);
68 
69 /**
70  * Logs when we receive Bluetooth Read Remote Version Information Complete
71  * Event from the remote device, as documented by the Bluetooth Core HCI
72  * specification
73  *
74  * Reference: 5.0 Core Specification, Vol 2, Part E, Page 1118
75  *
76  * @param handle handle of associated ACL connection
77  * @param status HCI command status of this event
78  * @param version version code from read remote version complete event
79  * @param manufacturer_name manufacturer code from read remote version complete
80  *                          event
81  * @param subversion subversion code from read remote version complete event
82  */
83 void LogMetricRemoteVersionInfo(
84     uint16_t handle, uint8_t status, uint8_t version, uint16_t manufacturer_name, uint16_t subversion);
85 
86 /**
87  * Log A2DP audio buffer underrun event
88  *
89  * @param address A2DP device associated with this event
90  * @param encoding_interval_millis encoding interval in milliseconds
91  * @param num_missing_pcm_bytes number of PCM bytes that cannot be read from
92  *                              the source
93  */
94 void LogMetricA2dpAudioUnderrunEvent(
95     const hci::Address& address, uint64_t encoding_interval_millis, int num_missing_pcm_bytes);
96 
97 /**
98  * Log A2DP audio buffer overrun event
99  *
100  * @param address A2DP device associated with this event
101  * @param encoding_interval_millis encoding interval in milliseconds
102  * @param num_dropped_buffers number of encoded buffers dropped from Tx queue
103  * @param num_dropped_encoded_frames number of encoded frames dropped from Tx
104  *                                   queue
105  * @param num_dropped_encoded_bytes number of encoded bytes dropped from Tx
106  *                                  queue
107  */
108 void LogMetricA2dpAudioOverrunEvent(
109     const hci::Address& address,
110     uint64_t encoding_interval_millis,
111     int num_dropped_buffers,
112     int num_dropped_encoded_frames,
113     int num_dropped_encoded_bytes);
114 
115 /**
116  * Log A2DP audio playback state changed event
117  *
118  * @param address A2DP device associated with this event
119  * @param playback_state A2DP audio playback state, on/off
120  * @param audio_coding_mode A2DP audio codec encoding mode, hw/sw
121  */
122 void LogMetricA2dpPlaybackEvent(const hci::Address& address, int playback_state, int audio_coding_mode);
123 
124 /**
125  * Log HFP audio capture packet loss statistics
126  *
127  * @param address HFP device associated with this stats
128  * @param num_decoded_frames number of decoded frames
129  * @param packet_loss_ratio ratio of packet loss frames
130  */
131 void LogMetricHfpPacketLossStats(
132     const hci::Address& address, int num_decoded_frames, double packet_loss_ratio);
133 
134 /**
135  * Log read RSSI result
136  *
137  * @param address device associated with this event
138  * @param handle connection handle of this event,
139  *               {@link kUnknownConnectionHandle} if unknown
140  * @param cmd_status command status from read RSSI command
141  * @param rssi rssi value in dBm
142  */
143 void LogMetricReadRssiResult(const hci::Address& address, uint16_t handle, uint32_t cmd_status, int8_t rssi);
144 
145 /**
146  * Log failed contact counter report
147  *
148  * @param address device associated with this event
149  * @param handle connection handle of this event,
150  *               {@link kUnknownConnectionHandle} if unknown
151  * @param cmd_status command status from read failed contact counter command
152  * @param failed_contact_counter Number of consecutive failed contacts for a
153  *                               connection corresponding to the Handle
154  */
155 void LogMetricReadFailedContactCounterResult(
156     const hci::Address& address, uint16_t handle, uint32_t cmd_status, int32_t failed_contact_counter);
157 
158 /**
159  * Log transmit power level for a particular device after read
160  *
161  * @param address device associated with this event
162  * @param handle connection handle of this event,
163  *               {@link kUnknownConnectionHandle} if unknown
164  * @param cmd_status command status from read failed contact counter command
165  * @param transmit_power_level transmit power level for connection to this
166  *                             device
167  */
168 void LogMetricReadTxPowerLevelResult(
169     const hci::Address& address, uint16_t handle, uint32_t cmd_status, int32_t transmit_power_level);
170 
171 /**
172  * Logs when there is an event related to Bluetooth Security Manager Protocol
173  *
174  * @param address address of associated device
175  * @param smp_cmd SMP command code associated with this event
176  * @param direction direction of this SMP command
177  * @param smp_fail_reason SMP pairing failure reason code from SMP spec
178  */
179 void LogMetricSmpPairingEvent(
180     const hci::Address& address,
181     uint16_t smp_cmd,
182     android::bluetooth::DirectionEnum direction,
183     uint16_t smp_fail_reason);
184 
185 /**
186  * Logs there is an event related Bluetooth classic pairing
187  *
188  * @param address address of associated device
189  * @param handle connection handle of this event,
190  *               {@link kUnknownConnectionHandle} if unknown
191  * @param hci_cmd HCI command associated with this event
192  * @param hci_event HCI event associated with this event
193  * @param cmd_status Command status associated with this event
194  * @param reason_code Reason code associated with this event
195  * @param event_value A status value related to this specific event
196  */
197 void LogMetricClassicPairingEvent(
198     const hci::Address& address,
199     uint16_t handle,
200     uint32_t hci_cmd,
201     uint16_t hci_event,
202     uint16_t cmd_status,
203     uint16_t reason_code,
204     int64_t event_value);
205 
206 /**
207  * Logs when certain Bluetooth SDP attributes are discovered
208  *
209  * @param address address of associated device
210  * @param protocol_uuid 16 bit protocol UUID from Bluetooth Assigned Numbers
211  * @param attribute_id 16 bit attribute ID from Bluetooth Assigned Numbers
212  * @param attribute_size size of this attribute
213  * @param attribute_value pointer to the attribute data, must be larger than
214  *                        attribute_size
215  */
216 void LogMetricSdpAttribute(
217     const hci::Address& address,
218     uint16_t protocol_uuid,
219     uint16_t attribute_id,
220     size_t attribute_size,
221     const char* attribute_value);
222 
223 /**
224  * Logs when there is a change in Bluetooth socket connection state
225  *
226  * @param address address of associated device, empty if this is a server port
227  * @param port port of this socket connection
228  * @param type type of socket
229  * @param connection_state socket connection state
230  * @param tx_bytes number of bytes transmitted
231  * @param rx_bytes number of bytes received
232  * @param server_port server port of this socket, if any. When both
233  *        |server_port| and |port| fields are populated, |port| must be spawned
234  *        by |server_port|
235  * @param socket_role role of this socket, server or connection
236  * @param uid socket owner's uid
237  */
238 void LogMetricSocketConnectionState(
239     const hci::Address& address,
240     int port,
241     int type,
242     android::bluetooth::SocketConnectionstateEnum connection_state,
243     int64_t tx_bytes,
244     int64_t rx_bytes,
245     int uid,
246     int server_port,
247     android::bluetooth::SocketRoleEnum socket_role);
248 
249 /**
250  * Logs when a Bluetooth device's manufacturer information is learnt
251  *
252  * @param address address of associated device
253  * @param source_type where is this device info obtained from
254  * @param source_name name of the data source, internal or external
255  * @param manufacturer name of the manufacturer of this device
256  * @param model model of this device
257  * @param hardware_version hardware version of this device
258  * @param software_version software version of this device
259  */
260 void LogMetricManufacturerInfo(
261     const hci::Address& address,
262     android::bluetooth::AddressTypeEnum address_type,
263     android::bluetooth::DeviceInfoSrcEnum source_type,
264     const std::string& source_name,
265     const std::string& manufacturer,
266     const std::string& model,
267     const std::string& hardware_version,
268     const std::string& software_version);
269 
270 /**
271  * Logs when received Bluetooth HAL crash reason report.
272  *
273  * @param address current connected address.
274  * @param error_code the crash reason from bluetooth hal
275  * @param vendor_error_code the vendor crash reason from bluetooth firmware
276  */
277 void LogMetricBluetoothHalCrashReason(
278     const hci::Address& address,
279     uint32_t error_code,
280     uint32_t vendor_error_code);
281 
282 void LogMetricBluetoothLocalSupportedFeatures(uint32_t page_num, uint64_t features);
283 
284 void LogMetricBluetoothLocalVersions(
285     uint32_t lmp_manufacturer_name,
286     uint8_t lmp_version,
287     uint32_t lmp_subversion,
288     uint8_t hci_version,
289     uint32_t hci_revision);
290 
291 void LogMetricBluetoothDisconnectionReasonReported(
292     uint32_t reason, const hci::Address& address, uint32_t connection_handle);
293 
294 void LogMetricBluetoothRemoteSupportedFeatures(
295     const hci::Address& address, uint32_t page, uint64_t features, uint32_t connection_handle);
296 
297 void LogMetricBluetoothCodePathCounterMetrics(int32_t key, int64_t count);
298 
299 using android::bluetooth::le::LeAclConnectionState;
300 using android::bluetooth::le::LeConnectionOriginType;
301 using android::bluetooth::le::LeConnectionType;
302 using android::bluetooth::le::LeConnectionState;
303 // Adding options
304 struct LEConnectionSessionOptions {
305   // Contains the state of the LE-ACL Connection
306   LeAclConnectionState acl_connection_state = LeAclConnectionState::LE_ACL_UNSPECIFIED;
307   // Origin of the transaction
308   LeConnectionOriginType origin_type = LeConnectionOriginType::ORIGIN_UNSPECIFIED;
309   // Connection Type
310   LeConnectionType transaction_type = LeConnectionType::CONNECTION_TYPE_UNSPECIFIED;
311   // Transaction State
312   LeConnectionState transaction_state = LeConnectionState::STATE_UNSPECIFIED;
313   // Latency of the entire transaction
314   int64_t latency = 0;
315   // Address of the remote device
316   hci::Address remote_address = hci::Address::kEmpty;
317   // UID associated with the device
318   int app_uid = 0;
319   // Latency of the ACL Transaction
320   int64_t acl_latency = 0;
321   // Contains the error code associated with the ACL Connection if failed
322   android::bluetooth::hci::StatusEnum status = android::bluetooth::hci::StatusEnum::STATUS_UNKNOWN;
323   // Cancelled connection
324   bool is_cancelled = false;
325 };
326 
327 // Argument Type
328 enum ArgumentType { GATT_IF, L2CAP_PSM, L2CAP_CID, APP_UID, ACL_STATUS_CODE };
329 void LogMetricBluetoothLEConnectionMetricEvent(
330     const hci::Address& address,
331     LeConnectionOriginType origin_type,
332     LeConnectionType connection_type,
333     LeConnectionState transaction_state,
334     std::vector<std::pair<os::ArgumentType, int>>& argument_list);
335 
336 // Upload LE Session
337 void LogMetricBluetoothLEConnection(os::LEConnectionSessionOptions session_options);
338 
339 }  // namespace os
340    //
341 }  // namespace bluetooth
342