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 #include <frameworks/proto_logging/stats/enums/bluetooth/rfcomm/enums.pb.h> 25 26 #include <vector> 27 28 #include "hci/address.h" 29 #include "types/raw_address.h" 30 31 namespace bluetooth { 32 namespace os { 33 34 /** 35 * Unknown connection handle for metrics purpose 36 */ 37 static const uint32_t kUnknownConnectionHandle = 0xFFFF; 38 39 /** 40 * Log link layer connection event 41 * 42 * @param address Stack wide consistent Bluetooth address of this event, 43 * nullptr if unknown 44 * @param connection_handle connection handle of this event, 45 * {@link kUnknownConnectionHandle} if unknown 46 * @param direction direction of this connection 47 * @param link_type type of the link 48 * @param hci_cmd HCI command opecode associated with this event, if any 49 * @param hci_event HCI event code associated with this event, if any 50 * @param hci_ble_event HCI BLE event code associated with this event, if any 51 * @param cmd_status Command status associated with this event, if any 52 * @param reason_code Reason code associated with this event, if any 53 */ 54 void LogMetricLinkLayerConnectionEvent(const hci::Address* address, uint32_t connection_handle, 55 android::bluetooth::DirectionEnum direction, 56 uint16_t link_type, uint32_t hci_cmd, uint16_t hci_event, 57 uint16_t hci_ble_event, uint16_t cmd_status, 58 uint16_t reason_code); 59 60 /** 61 * Logs when Bluetooth controller failed to reply with command status within 62 * a timeout period after receiving an HCI command from the host 63 * 64 * @param hci_cmd opcode of HCI command that caused this timeout 65 */ 66 void LogMetricHciTimeoutEvent(uint32_t hci_cmd); 67 68 /** 69 * Logs when we receive Bluetooth Read Remote Version Information Complete 70 * Event from the remote device, as documented by the Bluetooth Core HCI 71 * specification 72 * 73 * Reference: 5.0 Core Specification, Vol 2, Part E, Page 1118 74 * 75 * @param handle handle of associated ACL connection 76 * @param status HCI command status of this event 77 * @param version version code from read remote version complete event 78 * @param manufacturer_name manufacturer code from read remote version complete 79 * event 80 * @param subversion subversion code from read remote version complete event 81 */ 82 void LogMetricRemoteVersionInfo(uint16_t handle, uint8_t status, uint8_t version, 83 uint16_t manufacturer_name, uint16_t subversion); 84 85 /** 86 * Log A2DP audio buffer underrun event 87 * 88 * @param address A2DP device associated with this event 89 * @param encoding_interval_millis encoding interval in milliseconds 90 * @param num_missing_pcm_bytes number of PCM bytes that cannot be read from 91 * the source 92 */ 93 void LogMetricA2dpAudioUnderrunEvent(const hci::Address& address, uint64_t encoding_interval_millis, 94 int num_missing_pcm_bytes); 95 96 /** 97 * Log A2DP audio buffer overrun event 98 * 99 * @param address A2DP device associated with this event 100 * @param encoding_interval_millis encoding interval in milliseconds 101 * @param num_dropped_buffers number of encoded buffers dropped from Tx queue 102 * @param num_dropped_encoded_frames number of encoded frames dropped from Tx 103 * queue 104 * @param num_dropped_encoded_bytes number of encoded bytes dropped from Tx 105 * queue 106 */ 107 void LogMetricA2dpAudioOverrunEvent(const hci::Address& address, uint64_t encoding_interval_millis, 108 int num_dropped_buffers, int num_dropped_encoded_frames, 109 int num_dropped_encoded_bytes); 110 111 /** 112 * Log A2DP audio playback state changed event 113 * 114 * @param address A2DP device associated with this event 115 * @param playback_state A2DP audio playback state, on/off 116 * @param audio_coding_mode A2DP audio codec encoding mode, hw/sw 117 */ 118 void LogMetricA2dpPlaybackEvent(const hci::Address& address, int playback_state, 119 int audio_coding_mode); 120 121 /** 122 * Log A2DP audio session metrics 123 * 124 * @param address A2DP device associated with this session 125 * @param audio_duration_ms duration of the A2DP session 126 * @param media_timer_min_ms min time interval for the media timer 127 * @param media_timer_max_ms max time interval for the media timer 128 * @param media_timer_avg_ms avg time interval for the media timer 129 * @param total_scheduling_count total scheduling count 130 * @param buffer_overruns_max_count max count of Tx queue messages dropped 131 caused by buffer overruns 132 * @param buffer_overruns_total total count of Tx queue messages dropped 133 caused by buffer overruns 134 * @param buffer_underruns_average avg number of bytes short in buffer 135 underruns 136 * @param buffer_underruns_count count of buffer underruns 137 * @param codec_index A2DP codec index (SBC=0, AAC=1, etc...) 138 * @param is_a2dp_offload if A2DP is offload 139 */ 140 void LogMetricA2dpSessionMetricsEvent(const hci::Address& address, int64_t audio_duration_ms, 141 int media_timer_min_ms, int media_timer_max_ms, 142 int media_timer_avg_ms, int total_scheduling_count, 143 int buffer_overruns_max_count, int buffer_overruns_total, 144 float buffer_underruns_average, int buffer_underruns_count, 145 int64_t codec_index, bool is_a2dp_offload); 146 147 /** 148 * Log HFP audio capture packet loss statistics 149 * 150 * @param address HFP device associated with this stats 151 * @param num_decoded_frames number of decoded frames 152 * @param packet_loss_ratio ratio of packet loss frames 153 * @param codec_id codec ID of the packet (mSBC=2, LC3=3) 154 */ 155 void LogMetricHfpPacketLossStats(const hci::Address& address, int num_decoded_frames, 156 double packet_loss_ratio, uint16_t codec_id); 157 158 /** 159 * Log Mmc transcode round-trip time statistics 160 * 161 * @param maximum_rtt maximum round-trip time in this session 162 * @param mean_rtt the average of round-trip time in this session 163 * @param num_requests the number of transcoding requests in the session 164 * @param codec_type codec type used in this session 165 */ 166 void LogMetricMmcTranscodeRttStats(int maximum_rtt, double mean_rtt, int num_requests, 167 int codec_type); 168 169 /** 170 * Log read RSSI result 171 * 172 * @param address device associated with this event 173 * @param handle connection handle of this event, 174 * {@link kUnknownConnectionHandle} if unknown 175 * @param cmd_status command status from read RSSI command 176 * @param rssi rssi value in dBm 177 */ 178 void LogMetricReadRssiResult(const hci::Address& address, uint16_t handle, uint32_t cmd_status, 179 int8_t rssi); 180 181 /** 182 * Log failed contact counter report 183 * 184 * @param address device associated with this event 185 * @param handle connection handle of this event, 186 * {@link kUnknownConnectionHandle} if unknown 187 * @param cmd_status command status from read failed contact counter command 188 * @param failed_contact_counter Number of consecutive failed contacts for a 189 * connection corresponding to the Handle 190 */ 191 void LogMetricReadFailedContactCounterResult(const hci::Address& address, uint16_t handle, 192 uint32_t cmd_status, int32_t failed_contact_counter); 193 194 /** 195 * Log transmit power level for a particular device after read 196 * 197 * @param address device associated with this event 198 * @param handle connection handle of this event, 199 * {@link kUnknownConnectionHandle} if unknown 200 * @param cmd_status command status from read failed contact counter command 201 * @param transmit_power_level transmit power level for connection to this 202 * device 203 */ 204 void LogMetricReadTxPowerLevelResult(const hci::Address& address, uint16_t handle, 205 uint32_t cmd_status, int32_t transmit_power_level); 206 207 /** 208 * Logs when there is an event related to Bluetooth Security Manager Protocol 209 * 210 * @param address address of associated device 211 * @param smp_cmd SMP command code associated with this event 212 * @param direction direction of this SMP command 213 * @param smp_fail_reason SMP pairing failure reason code from SMP spec 214 */ 215 void LogMetricSmpPairingEvent(const hci::Address& address, uint16_t smp_cmd, 216 android::bluetooth::DirectionEnum direction, 217 uint16_t smp_fail_reason); 218 219 /** 220 * Logs there is an event related Bluetooth classic pairing 221 * 222 * @param address address of associated device 223 * @param handle connection handle of this event, 224 * {@link kUnknownConnectionHandle} if unknown 225 * @param hci_cmd HCI command associated with this event 226 * @param hci_event HCI event associated with this event 227 * @param cmd_status Command status associated with this event 228 * @param reason_code Reason code associated with this event 229 * @param event_value A status value related to this specific event 230 */ 231 void LogMetricClassicPairingEvent(const hci::Address& address, uint16_t handle, uint32_t hci_cmd, 232 uint16_t hci_event, uint16_t cmd_status, uint16_t reason_code, 233 int64_t event_value); 234 235 /** 236 * Logs when certain Bluetooth SDP attributes are discovered 237 * 238 * @param address address of associated device 239 * @param protocol_uuid 16 bit protocol UUID from Bluetooth Assigned Numbers 240 * @param attribute_id 16 bit attribute ID from Bluetooth Assigned Numbers 241 * @param attribute_size size of this attribute 242 * @param attribute_value pointer to the attribute data, must be larger than 243 * attribute_size 244 */ 245 void LogMetricSdpAttribute(const hci::Address& address, uint16_t protocol_uuid, 246 uint16_t attribute_id, size_t attribute_size, 247 const char* attribute_value); 248 249 /** 250 * Logs when there is a change in Bluetooth socket connection state 251 * 252 * @param address address of associated device, empty if this is a server port 253 * @param port port of this socket connection 254 * @param type type of socket 255 * @param connection_state socket connection state 256 * @param tx_bytes number of bytes transmitted 257 * @param rx_bytes number of bytes received 258 * @param server_port server port of this socket, if any. When both 259 * |server_port| and |port| fields are populated, |port| must be spawned 260 * by |server_port| 261 * @param socket_role role of this socket, server or connection 262 * @param uid socket owner's uid 263 * @param connection_duration_ms duration of socket connection in milliseconds 264 * @param error_code error code of socket failures 265 * @param is_hardware_offload whether this is a offload socket 266 */ 267 void LogMetricSocketConnectionState(const hci::Address& address, int port, int type, 268 android::bluetooth::SocketConnectionstateEnum connection_state, 269 int64_t tx_bytes, int64_t rx_bytes, int uid, int server_port, 270 android::bluetooth::SocketRoleEnum socket_role, 271 uint64_t connection_duration_ms, 272 android::bluetooth::SocketErrorEnum error_code, 273 bool is_hardware_offload); 274 275 /** 276 * Logs when a Bluetooth device's manufacturer information is learnt 277 * 278 * @param address address of associated device 279 * @param source_type where is this device info obtained from 280 * @param source_name name of the data source, internal or external 281 * @param manufacturer name of the manufacturer of this device 282 * @param model model of this device 283 * @param hardware_version hardware version of this device 284 * @param software_version software version of this device 285 */ 286 void LogMetricManufacturerInfo(const hci::Address& address, 287 android::bluetooth::AddressTypeEnum address_type, 288 android::bluetooth::DeviceInfoSrcEnum source_type, 289 const std::string& source_name, const std::string& manufacturer, 290 const std::string& model, const std::string& hardware_version, 291 const std::string& software_version); 292 293 /** 294 * Logs when received Bluetooth HAL crash reason report. 295 * 296 * @param address current connected address. 297 * @param error_code the crash reason from bluetooth hal 298 * @param vendor_error_code the vendor crash reason from bluetooth firmware 299 */ 300 void LogMetricBluetoothHalCrashReason(const hci::Address& address, uint32_t error_code, 301 uint32_t vendor_error_code); 302 303 void LogMetricBluetoothLocalSupportedFeatures(uint32_t page_num, uint64_t features); 304 305 void LogMetricBluetoothLocalVersions(uint32_t lmp_manufacturer_name, uint8_t lmp_version, 306 uint32_t lmp_subversion, uint8_t hci_version, 307 uint32_t hci_revision); 308 309 void LogMetricBluetoothDisconnectionReasonReported(uint32_t reason, const hci::Address& address, 310 uint32_t connection_handle); 311 312 void LogMetricBluetoothRemoteSupportedFeatures(const hci::Address& address, uint32_t page, 313 uint64_t features, uint32_t connection_handle); 314 315 void LogMetricBluetoothCodePathCounterMetrics(int32_t key, int64_t count); 316 317 using android::bluetooth::le::LeAclConnectionState; 318 using android::bluetooth::le::LeConnectionOriginType; 319 using android::bluetooth::le::LeConnectionState; 320 using android::bluetooth::le::LeConnectionType; 321 // Adding options 322 struct LEConnectionSessionOptions { 323 // Contains the state of the LE-ACL Connection 324 LeAclConnectionState acl_connection_state = LeAclConnectionState::LE_ACL_UNSPECIFIED; 325 // Origin of the transaction 326 LeConnectionOriginType origin_type = LeConnectionOriginType::ORIGIN_UNSPECIFIED; 327 // Connection Type 328 LeConnectionType transaction_type = LeConnectionType::CONNECTION_TYPE_UNSPECIFIED; 329 // Transaction State 330 LeConnectionState transaction_state = LeConnectionState::STATE_UNSPECIFIED; 331 // Latency of the entire transaction 332 int64_t latency = 0; 333 // Address of the remote device 334 hci::Address remote_address = hci::Address::kEmpty; 335 // UID associated with the device 336 int app_uid = 0; 337 // Latency of the ACL Transaction 338 int64_t acl_latency = 0; 339 // Contains the error code associated with the ACL Connection if failed 340 android::bluetooth::hci::StatusEnum status = android::bluetooth::hci::StatusEnum::STATUS_UNKNOWN; 341 // Cancelled connection 342 bool is_cancelled = false; 343 }; 344 345 // Upload LE Session 346 void LogMetricBluetoothLEConnection(os::LEConnectionSessionOptions session_options); 347 348 /** 349 * Logs a Bluetooth Event 350 * 351 * @param address address of associated device 352 * @param event_type type of event where this is getting logged from 353 * @param state state associated with the event 354 */ 355 void LogMetricBluetoothEvent(const hci::Address& address, android::bluetooth::EventType event_type, 356 android::bluetooth::State state); 357 358 /** 359 * Logs an RFCOMM connection when an RFCOMM port closes 360 * 361 * @param address address of the peer device 362 * @param close_reason reason that the port was closed 363 * @param security security level of the connection 364 * @param last_event event processed prior to "CLOSED" 365 * @param previous_state state prior to "CLOSED" 366 * @param open_duration_ms that the socket was opened, 0 if connection failed 367 * @param uid UID of the app that called connect 368 * @param sdp_status status code for sdp 369 * @param is_server true if device is server 370 * @param sdp_initiated true if sdp started for thie connection 371 * @param sdp_duration_ms duration of sdp, 0 if it didn't happen 372 */ 373 void LogMetricRfcommConnectionAtClose(const hci::Address& address, 374 android::bluetooth::rfcomm::PortResult close_reason, 375 android::bluetooth::rfcomm::SocketConnectionSecurity security, 376 android::bluetooth::rfcomm::RfcommPortEvent last_event, 377 android::bluetooth::rfcomm::RfcommPortState previous_state, 378 int32_t open_duration_ms, int32_t uid, 379 android::bluetooth::BtaStatus sdp_status, bool is_server, 380 bool sdp_initiated, int32_t sdp_duration_ms); 381 382 void LogMetricLeAudioConnectionSessionReported( 383 int32_t group_size, int32_t group_metric_id, int64_t connection_duration_nanos, 384 const std::vector<int64_t>& device_connecting_offset_nanos, 385 const std::vector<int64_t>& device_connected_offset_nanos, 386 const std::vector<int64_t>& device_connection_duration_nanos, 387 const std::vector<int32_t>& device_connection_status, 388 const std::vector<int32_t>& device_disconnection_status, 389 const std::vector<RawAddress>& device_address, 390 const std::vector<int64_t>& streaming_offset_nanos, 391 const std::vector<int64_t>& streaming_duration_nanos, 392 const std::vector<int32_t>& streaming_context_type); 393 394 void LogMetricLeAudioBroadcastSessionReported(int64_t duration_nanos); 395 396 } // namespace os 397 } // namespace bluetooth 398