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 read RSSI result 126 * 127 * @param address device associated with this event 128 * @param handle connection handle of this event, 129 * {@link kUnknownConnectionHandle} if unknown 130 * @param cmd_status command status from read RSSI command 131 * @param rssi rssi value in dBm 132 */ 133 void LogMetricReadRssiResult(const hci::Address& address, uint16_t handle, uint32_t cmd_status, int8_t rssi); 134 135 /** 136 * Log failed contact counter report 137 * 138 * @param address device associated with this event 139 * @param handle connection handle of this event, 140 * {@link kUnknownConnectionHandle} if unknown 141 * @param cmd_status command status from read failed contact counter command 142 * @param failed_contact_counter Number of consecutive failed contacts for a 143 * connection corresponding to the Handle 144 */ 145 void LogMetricReadFailedContactCounterResult( 146 const hci::Address& address, uint16_t handle, uint32_t cmd_status, int32_t failed_contact_counter); 147 148 /** 149 * Log transmit power level for a particular device after read 150 * 151 * @param address device associated with this event 152 * @param handle connection handle of this event, 153 * {@link kUnknownConnectionHandle} if unknown 154 * @param cmd_status command status from read failed contact counter command 155 * @param transmit_power_level transmit power level for connection to this 156 * device 157 */ 158 void LogMetricReadTxPowerLevelResult( 159 const hci::Address& address, uint16_t handle, uint32_t cmd_status, int32_t transmit_power_level); 160 161 /** 162 * Logs when there is an event related to Bluetooth Security Manager Protocol 163 * 164 * @param address address of associated device 165 * @param smp_cmd SMP command code associated with this event 166 * @param direction direction of this SMP command 167 * @param smp_fail_reason SMP pairing failure reason code from SMP spec 168 */ 169 void LogMetricSmpPairingEvent( 170 const hci::Address& address, 171 uint16_t smp_cmd, 172 android::bluetooth::DirectionEnum direction, 173 uint16_t smp_fail_reason); 174 175 /** 176 * Logs there is an event related Bluetooth classic pairing 177 * 178 * @param address address of associated device 179 * @param handle connection handle of this event, 180 * {@link kUnknownConnectionHandle} if unknown 181 * @param hci_cmd HCI command associated with this event 182 * @param hci_event HCI event associated with this event 183 * @param cmd_status Command status associated with this event 184 * @param reason_code Reason code associated with this event 185 * @param event_value A status value related to this specific event 186 */ 187 void LogMetricClassicPairingEvent( 188 const hci::Address& address, 189 uint16_t handle, 190 uint32_t hci_cmd, 191 uint16_t hci_event, 192 uint16_t cmd_status, 193 uint16_t reason_code, 194 int64_t event_value); 195 196 /** 197 * Logs when certain Bluetooth SDP attributes are discovered 198 * 199 * @param address address of associated device 200 * @param protocol_uuid 16 bit protocol UUID from Bluetooth Assigned Numbers 201 * @param attribute_id 16 bit attribute ID from Bluetooth Assigned Numbers 202 * @param attribute_size size of this attribute 203 * @param attribute_value pointer to the attribute data, must be larger than 204 * attribute_size 205 */ 206 void LogMetricSdpAttribute( 207 const hci::Address& address, 208 uint16_t protocol_uuid, 209 uint16_t attribute_id, 210 size_t attribute_size, 211 const char* attribute_value); 212 213 /** 214 * Logs when there is a change in Bluetooth socket connection state 215 * 216 * @param address address of associated device, empty if this is a server port 217 * @param port port of this socket connection 218 * @param type type of socket 219 * @param connection_state socket connection state 220 * @param tx_bytes number of bytes transmitted 221 * @param rx_bytes number of bytes received 222 * @param server_port server port of this socket, if any. When both 223 * |server_port| and |port| fields are populated, |port| must be spawned 224 * by |server_port| 225 * @param socket_role role of this socket, server or connection 226 * @param uid socket owner's uid 227 */ 228 void LogMetricSocketConnectionState( 229 const hci::Address& address, 230 int port, 231 int type, 232 android::bluetooth::SocketConnectionstateEnum connection_state, 233 int64_t tx_bytes, 234 int64_t rx_bytes, 235 int uid, 236 int server_port, 237 android::bluetooth::SocketRoleEnum socket_role); 238 239 /** 240 * Logs when a Bluetooth device's manufacturer information is learnt 241 * 242 * @param address address of associated device 243 * @param source_type where is this device info obtained from 244 * @param source_name name of the data source, internal or external 245 * @param manufacturer name of the manufacturer of this device 246 * @param model model of this device 247 * @param hardware_version hardware version of this device 248 * @param software_version software version of this device 249 */ 250 void LogMetricManufacturerInfo( 251 const hci::Address& address, 252 android::bluetooth::AddressTypeEnum address_type, 253 android::bluetooth::DeviceInfoSrcEnum source_type, 254 const std::string& source_name, 255 const std::string& manufacturer, 256 const std::string& model, 257 const std::string& hardware_version, 258 const std::string& software_version); 259 260 /** 261 * Logs when received Bluetooth HAL crash reason report. 262 * 263 * @param address current connected address. 264 * @param error_code the crash reason from bluetooth hal 265 * @param vendor_error_code the vendor crash reason from bluetooth firmware 266 */ 267 void LogMetricBluetoothHalCrashReason( 268 const hci::Address& address, 269 uint32_t error_code, 270 uint32_t vendor_error_code); 271 272 void LogMetricBluetoothLocalSupportedFeatures(uint32_t page_num, uint64_t features); 273 274 void LogMetricBluetoothLocalVersions( 275 uint32_t lmp_manufacturer_name, 276 uint8_t lmp_version, 277 uint32_t lmp_subversion, 278 uint8_t hci_version, 279 uint32_t hci_revision); 280 281 void LogMetricBluetoothDisconnectionReasonReported( 282 uint32_t reason, const hci::Address& address, uint32_t connection_handle); 283 284 void LogMetricBluetoothRemoteSupportedFeatures( 285 const hci::Address& address, uint32_t page, uint64_t features, uint32_t connection_handle); 286 287 void LogMetricBluetoothCodePathCounterMetrics(int32_t key, int64_t count); 288 289 using android::bluetooth::le::LeAclConnectionState; 290 using android::bluetooth::le::LeConnectionOriginType; 291 using android::bluetooth::le::LeConnectionType; 292 using android::bluetooth::le::LeConnectionState; 293 // Adding options 294 struct LEConnectionSessionOptions { 295 // Contains the state of the LE-ACL Connection 296 LeAclConnectionState acl_connection_state = LeAclConnectionState::LE_ACL_UNSPECIFIED; 297 // Origin of the transaction 298 LeConnectionOriginType origin_type = LeConnectionOriginType::ORIGIN_UNSPECIFIED; 299 // Connection Type 300 LeConnectionType transaction_type = LeConnectionType::CONNECTION_TYPE_UNSPECIFIED; 301 // Transaction State 302 LeConnectionState transaction_state = LeConnectionState::STATE_UNSPECIFIED; 303 // Latency of the entire transaction 304 int64_t latency = 0; 305 // Address of the remote device 306 hci::Address remote_address = hci::Address::kEmpty; 307 // UID associated with the device 308 int app_uid = 0; 309 // Latency of the ACL Transaction 310 int64_t acl_latency = 0; 311 // Contains the error code associated with the ACL Connection if failed 312 android::bluetooth::hci::StatusEnum status = android::bluetooth::hci::StatusEnum::STATUS_UNKNOWN; 313 // Cancelled connection 314 bool is_cancelled = false; 315 }; 316 317 // Argument Type 318 enum ArgumentType { GATT_IF, L2CAP_PSM, L2CAP_CID, APP_UID, ACL_STATUS_CODE }; 319 void LogMetricBluetoothLEConnectionMetricEvent( 320 const hci::Address& address, 321 LeConnectionOriginType origin_type, 322 LeConnectionType connection_type, 323 LeConnectionState transaction_state, 324 std::vector<std::pair<os::ArgumentType, int>>& argument_list); 325 326 // Upload LE Session 327 void LogMetricBluetoothLEConnection(os::LEConnectionSessionOptions session_options); 328 329 } // namespace os 330 // 331 } // namespace bluetooth 332