Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Jakub Pawlowski | 3b10fdd | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 3 | * Copyright 2016 Google, Inc. |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 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 | ******************************************************************************/ |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 18 | |
Chris Manton | 2a9d9f2 | 2021-09-10 23:05:47 -0700 | [diff] [blame] | 19 | #include "metrics.h" |
| 20 | |
| 21 | #include <base/base64.h> |
| 22 | #include <base/logging.h> |
| 23 | #include <include/hardware/bt_av.h> |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 24 | #include <statslog_bt.h> |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 25 | #include <unistd.h> |
Chris Manton | 2a9d9f2 | 2021-09-10 23:05:47 -0700 | [diff] [blame] | 26 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 27 | #include <algorithm> |
Jack He | 7ebb85a | 2018-04-02 13:04:58 -0700 | [diff] [blame] | 28 | #include <array> |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 29 | #include <cerrno> |
| 30 | #include <chrono> |
| 31 | #include <cstdint> |
| 32 | #include <cstring> |
| 33 | #include <memory> |
| 34 | #include <mutex> |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 35 | |
Jack He | 0d4807d | 2018-12-20 15:46:17 -0800 | [diff] [blame] | 36 | #include "address_obfuscator.h" |
Chris Manton | 2a9d9f2 | 2021-09-10 23:05:47 -0700 | [diff] [blame] | 37 | #include "bluetooth/metrics/bluetooth.pb.h" |
Jack He | 4282398 | 2018-08-15 12:38:37 -0700 | [diff] [blame] | 38 | #include "leaky_bonded_queue.h" |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 39 | #include "metric_id_allocator.h" |
Chris Manton | 2a9d9f2 | 2021-09-10 23:05:47 -0700 | [diff] [blame] | 40 | #include "osi/include/osi.h" |
| 41 | #include "stack/include/btm_api_types.h" |
Jack He | 4282398 | 2018-08-15 12:38:37 -0700 | [diff] [blame] | 42 | #include "time_util.h" |
Chris Manton | 2a9d9f2 | 2021-09-10 23:05:47 -0700 | [diff] [blame] | 43 | #include "types/raw_address.h" |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 44 | |
Jack He | 4282398 | 2018-08-15 12:38:37 -0700 | [diff] [blame] | 45 | namespace bluetooth { |
| 46 | |
| 47 | namespace common { |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 48 | |
Jack He | 9a30a9f | 2018-03-03 00:03:25 -0800 | [diff] [blame] | 49 | using bluetooth::metrics::BluetoothMetricsProto::A2DPSession; |
Jack He | eeb9f35 | 2018-06-08 20:13:23 -0700 | [diff] [blame] | 50 | using bluetooth::metrics::BluetoothMetricsProto::A2dpSourceCodec; |
Jack He | 9a30a9f | 2018-03-03 00:03:25 -0800 | [diff] [blame] | 51 | using bluetooth::metrics::BluetoothMetricsProto::BluetoothLog; |
| 52 | using bluetooth::metrics::BluetoothMetricsProto::BluetoothSession; |
| 53 | using bluetooth::metrics::BluetoothMetricsProto:: |
| 54 | BluetoothSession_ConnectionTechnologyType; |
| 55 | using bluetooth::metrics::BluetoothMetricsProto:: |
| 56 | BluetoothSession_DisconnectReasonType; |
| 57 | using bluetooth::metrics::BluetoothMetricsProto::DeviceInfo; |
| 58 | using bluetooth::metrics::BluetoothMetricsProto::DeviceInfo_DeviceType; |
Jack He | 4282398 | 2018-08-15 12:38:37 -0700 | [diff] [blame] | 59 | using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileConnectionStats; |
Jack He | 7ebb85a | 2018-04-02 13:04:58 -0700 | [diff] [blame] | 60 | using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType; |
Jack He | 7ebb85a | 2018-04-02 13:04:58 -0700 | [diff] [blame] | 61 | using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType_ARRAYSIZE; |
| 62 | using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType_IsValid; |
Jack He | 4282398 | 2018-08-15 12:38:37 -0700 | [diff] [blame] | 63 | using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType_MAX; |
| 64 | using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType_MIN; |
| 65 | using bluetooth::metrics::BluetoothMetricsProto::PairEvent; |
| 66 | using bluetooth::metrics::BluetoothMetricsProto::ScanEvent; |
| 67 | using bluetooth::metrics::BluetoothMetricsProto::ScanEvent_ScanEventType; |
| 68 | using bluetooth::metrics::BluetoothMetricsProto::ScanEvent_ScanTechnologyType; |
| 69 | using bluetooth::metrics::BluetoothMetricsProto::WakeEvent; |
| 70 | using bluetooth::metrics::BluetoothMetricsProto::WakeEvent_WakeEventType; |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 71 | |
| 72 | static float combine_averages(float avg_a, int64_t ct_a, float avg_b, |
| 73 | int64_t ct_b) { |
| 74 | if (ct_a > 0 && ct_b > 0) { |
| 75 | return (avg_a * ct_a + avg_b * ct_b) / (ct_a + ct_b); |
| 76 | } else if (ct_b > 0) { |
| 77 | return avg_b; |
| 78 | } else { |
| 79 | return avg_a; |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 83 | static int32_t combine_averages(int32_t avg_a, int64_t ct_a, int32_t avg_b, |
| 84 | int64_t ct_b) { |
| 85 | if (ct_a > 0 && ct_b > 0) { |
| 86 | return (avg_a * ct_a + avg_b * ct_b) / (ct_a + ct_b); |
| 87 | } else if (ct_b > 0) { |
| 88 | return avg_b; |
| 89 | } else { |
| 90 | return avg_a; |
| 91 | } |
| 92 | } |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 93 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 94 | void A2dpSessionMetrics::Update(const A2dpSessionMetrics& metrics) { |
Jack He | 723bef8 | 2017-01-20 11:17:44 -0800 | [diff] [blame] | 95 | if (metrics.audio_duration_ms >= 0) { |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 96 | audio_duration_ms = std::max(static_cast<int64_t>(0), audio_duration_ms); |
| 97 | audio_duration_ms += metrics.audio_duration_ms; |
| 98 | } |
Jack He | 723bef8 | 2017-01-20 11:17:44 -0800 | [diff] [blame] | 99 | if (metrics.media_timer_min_ms >= 0) { |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 100 | if (media_timer_min_ms < 0) { |
| 101 | media_timer_min_ms = metrics.media_timer_min_ms; |
| 102 | } else { |
| 103 | media_timer_min_ms = |
| 104 | std::min(media_timer_min_ms, metrics.media_timer_min_ms); |
| 105 | } |
| 106 | } |
Jack He | 723bef8 | 2017-01-20 11:17:44 -0800 | [diff] [blame] | 107 | if (metrics.media_timer_max_ms >= 0) { |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 108 | media_timer_max_ms = |
| 109 | std::max(media_timer_max_ms, metrics.media_timer_max_ms); |
| 110 | } |
Jack He | 723bef8 | 2017-01-20 11:17:44 -0800 | [diff] [blame] | 111 | if (metrics.media_timer_avg_ms >= 0 && metrics.total_scheduling_count >= 0) { |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 112 | if (media_timer_avg_ms < 0 || total_scheduling_count < 0) { |
| 113 | media_timer_avg_ms = metrics.media_timer_avg_ms; |
| 114 | total_scheduling_count = metrics.total_scheduling_count; |
| 115 | } else { |
| 116 | media_timer_avg_ms = combine_averages( |
| 117 | media_timer_avg_ms, total_scheduling_count, |
| 118 | metrics.media_timer_avg_ms, metrics.total_scheduling_count); |
| 119 | total_scheduling_count += metrics.total_scheduling_count; |
| 120 | } |
| 121 | } |
Jack He | 723bef8 | 2017-01-20 11:17:44 -0800 | [diff] [blame] | 122 | if (metrics.buffer_overruns_max_count >= 0) { |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 123 | buffer_overruns_max_count = |
| 124 | std::max(buffer_overruns_max_count, metrics.buffer_overruns_max_count); |
| 125 | } |
Jack He | 723bef8 | 2017-01-20 11:17:44 -0800 | [diff] [blame] | 126 | if (metrics.buffer_overruns_total >= 0) { |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 127 | buffer_overruns_total = |
| 128 | std::max(static_cast<int32_t>(0), buffer_overruns_total); |
| 129 | buffer_overruns_total += metrics.buffer_overruns_total; |
| 130 | } |
Jack He | 723bef8 | 2017-01-20 11:17:44 -0800 | [diff] [blame] | 131 | if (metrics.buffer_underruns_average >= 0 && |
| 132 | metrics.buffer_underruns_count >= 0) { |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 133 | if (buffer_underruns_average < 0 || buffer_underruns_count < 0) { |
| 134 | buffer_underruns_average = metrics.buffer_underruns_average; |
| 135 | buffer_underruns_count = metrics.buffer_underruns_count; |
| 136 | } else { |
| 137 | buffer_underruns_average = combine_averages( |
Jack He | 723bef8 | 2017-01-20 11:17:44 -0800 | [diff] [blame] | 138 | buffer_underruns_average, buffer_underruns_count, |
| 139 | metrics.buffer_underruns_average, metrics.buffer_underruns_count); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 140 | buffer_underruns_count += metrics.buffer_underruns_count; |
| 141 | } |
| 142 | } |
Jack He | eeb9f35 | 2018-06-08 20:13:23 -0700 | [diff] [blame] | 143 | if (codec_index < 0) { |
| 144 | codec_index = metrics.codec_index; |
| 145 | } |
| 146 | if (!is_a2dp_offload) { |
| 147 | is_a2dp_offload = metrics.is_a2dp_offload; |
| 148 | } |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 149 | } |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 150 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 151 | bool A2dpSessionMetrics::operator==(const A2dpSessionMetrics& rhs) const { |
| 152 | return audio_duration_ms == rhs.audio_duration_ms && |
| 153 | media_timer_min_ms == rhs.media_timer_min_ms && |
| 154 | media_timer_max_ms == rhs.media_timer_max_ms && |
| 155 | media_timer_avg_ms == rhs.media_timer_avg_ms && |
| 156 | total_scheduling_count == rhs.total_scheduling_count && |
| 157 | buffer_overruns_max_count == rhs.buffer_overruns_max_count && |
| 158 | buffer_overruns_total == rhs.buffer_overruns_total && |
| 159 | buffer_underruns_average == rhs.buffer_underruns_average && |
Jack He | eeb9f35 | 2018-06-08 20:13:23 -0700 | [diff] [blame] | 160 | buffer_underruns_count == rhs.buffer_underruns_count && |
| 161 | codec_index == rhs.codec_index && |
| 162 | is_a2dp_offload == rhs.is_a2dp_offload; |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static DeviceInfo_DeviceType get_device_type(device_type_t type) { |
| 166 | switch (type) { |
| 167 | case DEVICE_TYPE_BREDR: |
| 168 | return DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR; |
| 169 | case DEVICE_TYPE_LE: |
| 170 | return DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_LE; |
| 171 | case DEVICE_TYPE_DUMO: |
| 172 | return DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_DUMO; |
| 173 | case DEVICE_TYPE_UNKNOWN: |
| 174 | default: |
| 175 | return DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_UNKNOWN; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | static BluetoothSession_ConnectionTechnologyType get_connection_tech_type( |
| 180 | connection_tech_t type) { |
| 181 | switch (type) { |
| 182 | case CONNECTION_TECHNOLOGY_TYPE_LE: |
| 183 | return BluetoothSession_ConnectionTechnologyType:: |
| 184 | BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_LE; |
| 185 | case CONNECTION_TECHNOLOGY_TYPE_BREDR: |
| 186 | return BluetoothSession_ConnectionTechnologyType:: |
| 187 | BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR; |
| 188 | case CONNECTION_TECHNOLOGY_TYPE_UNKNOWN: |
| 189 | default: |
| 190 | return BluetoothSession_ConnectionTechnologyType:: |
| 191 | BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_UNKNOWN; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | static ScanEvent_ScanTechnologyType get_scan_tech_type(scan_tech_t type) { |
| 196 | switch (type) { |
| 197 | case SCAN_TECH_TYPE_LE: |
| 198 | return ScanEvent_ScanTechnologyType:: |
| 199 | ScanEvent_ScanTechnologyType_SCAN_TECH_TYPE_LE; |
| 200 | case SCAN_TECH_TYPE_BREDR: |
| 201 | return ScanEvent_ScanTechnologyType:: |
| 202 | ScanEvent_ScanTechnologyType_SCAN_TECH_TYPE_BREDR; |
| 203 | case SCAN_TECH_TYPE_BOTH: |
| 204 | return ScanEvent_ScanTechnologyType:: |
| 205 | ScanEvent_ScanTechnologyType_SCAN_TECH_TYPE_BOTH; |
| 206 | case SCAN_TYPE_UNKNOWN: |
| 207 | default: |
| 208 | return ScanEvent_ScanTechnologyType:: |
| 209 | ScanEvent_ScanTechnologyType_SCAN_TYPE_UNKNOWN; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | static WakeEvent_WakeEventType get_wake_event_type(wake_event_type_t type) { |
| 214 | switch (type) { |
| 215 | case WAKE_EVENT_ACQUIRED: |
| 216 | return WakeEvent_WakeEventType::WakeEvent_WakeEventType_ACQUIRED; |
| 217 | case WAKE_EVENT_RELEASED: |
| 218 | return WakeEvent_WakeEventType::WakeEvent_WakeEventType_RELEASED; |
| 219 | case WAKE_EVENT_UNKNOWN: |
| 220 | default: |
| 221 | return WakeEvent_WakeEventType::WakeEvent_WakeEventType_UNKNOWN; |
| 222 | } |
| 223 | } |
| 224 | |
Jack He | 9bebcc0 | 2017-01-17 15:41:30 -0800 | [diff] [blame] | 225 | static BluetoothSession_DisconnectReasonType get_disconnect_reason_type( |
| 226 | disconnect_reason_t type) { |
| 227 | switch (type) { |
| 228 | case DISCONNECT_REASON_METRICS_DUMP: |
| 229 | return BluetoothSession_DisconnectReasonType:: |
| 230 | BluetoothSession_DisconnectReasonType_METRICS_DUMP; |
| 231 | case DISCONNECT_REASON_NEXT_START_WITHOUT_END_PREVIOUS: |
| 232 | return BluetoothSession_DisconnectReasonType:: |
| 233 | BluetoothSession_DisconnectReasonType_NEXT_START_WITHOUT_END_PREVIOUS; |
| 234 | case DISCONNECT_REASON_UNKNOWN: |
| 235 | default: |
| 236 | return BluetoothSession_DisconnectReasonType:: |
| 237 | BluetoothSession_DisconnectReasonType_UNKNOWN; |
| 238 | } |
| 239 | } |
| 240 | |
Jack He | eeb9f35 | 2018-06-08 20:13:23 -0700 | [diff] [blame] | 241 | static A2dpSourceCodec get_a2dp_source_codec(int64_t codec_index) { |
| 242 | switch (codec_index) { |
| 243 | case BTAV_A2DP_CODEC_INDEX_SOURCE_SBC: |
| 244 | return A2dpSourceCodec::A2DP_SOURCE_CODEC_SBC; |
| 245 | case BTAV_A2DP_CODEC_INDEX_SOURCE_AAC: |
| 246 | return A2dpSourceCodec::A2DP_SOURCE_CODEC_AAC; |
| 247 | case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX: |
| 248 | return A2dpSourceCodec::A2DP_SOURCE_CODEC_APTX; |
| 249 | case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD: |
| 250 | return A2dpSourceCodec::A2DP_SOURCE_CODEC_APTX_HD; |
| 251 | case BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC: |
| 252 | return A2dpSourceCodec::A2DP_SOURCE_CODEC_LDAC; |
| 253 | default: |
| 254 | return A2dpSourceCodec::A2DP_SOURCE_CODEC_UNKNOWN; |
| 255 | } |
| 256 | } |
| 257 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 258 | struct BluetoothMetricsLogger::impl { |
| 259 | impl(size_t max_bluetooth_session, size_t max_pair_event, |
| 260 | size_t max_wake_event, size_t max_scan_event) |
| 261 | : bt_session_queue_( |
| 262 | new LeakyBondedQueue<BluetoothSession>(max_bluetooth_session)), |
| 263 | pair_event_queue_(new LeakyBondedQueue<PairEvent>(max_pair_event)), |
| 264 | wake_event_queue_(new LeakyBondedQueue<WakeEvent>(max_wake_event)), |
| 265 | scan_event_queue_(new LeakyBondedQueue<ScanEvent>(max_scan_event)) { |
| 266 | bluetooth_log_ = BluetoothLog::default_instance().New(); |
Jack He | 7ebb85a | 2018-04-02 13:04:58 -0700 | [diff] [blame] | 267 | headset_profile_connection_counts_.fill(0); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 268 | bluetooth_session_ = nullptr; |
| 269 | bluetooth_session_start_time_ms_ = 0; |
| 270 | a2dp_session_metrics_ = A2dpSessionMetrics(); |
| 271 | } |
| 272 | |
| 273 | /* Bluetooth log lock protected */ |
| 274 | BluetoothLog* bluetooth_log_; |
Jack He | 7ebb85a | 2018-04-02 13:04:58 -0700 | [diff] [blame] | 275 | std::array<int, HeadsetProfileType_ARRAYSIZE> |
| 276 | headset_profile_connection_counts_; |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 277 | std::recursive_mutex bluetooth_log_lock_; |
| 278 | /* End Bluetooth log lock protected */ |
| 279 | /* Bluetooth session lock protected */ |
| 280 | BluetoothSession* bluetooth_session_; |
| 281 | uint64_t bluetooth_session_start_time_ms_; |
| 282 | A2dpSessionMetrics a2dp_session_metrics_; |
| 283 | std::recursive_mutex bluetooth_session_lock_; |
| 284 | /* End bluetooth session lock protected */ |
| 285 | std::unique_ptr<LeakyBondedQueue<BluetoothSession>> bt_session_queue_; |
| 286 | std::unique_ptr<LeakyBondedQueue<PairEvent>> pair_event_queue_; |
| 287 | std::unique_ptr<LeakyBondedQueue<WakeEvent>> wake_event_queue_; |
| 288 | std::unique_ptr<LeakyBondedQueue<ScanEvent>> scan_event_queue_; |
| 289 | }; |
| 290 | |
| 291 | BluetoothMetricsLogger::BluetoothMetricsLogger() |
Jack He | 9bebcc0 | 2017-01-17 15:41:30 -0800 | [diff] [blame] | 292 | : pimpl_(new impl(kMaxNumBluetoothSession, kMaxNumPairEvent, |
| 293 | kMaxNumWakeEvent, kMaxNumScanEvent)) {} |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 294 | |
| 295 | void BluetoothMetricsLogger::LogPairEvent(uint32_t disconnect_reason, |
| 296 | uint64_t timestamp_ms, |
| 297 | uint32_t device_class, |
| 298 | device_type_t device_type) { |
| 299 | PairEvent* event = new PairEvent(); |
Myles Watson | 914a9dc | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 300 | DeviceInfo* info = event->mutable_device_paired_with(); |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 301 | info->set_device_class(device_class); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 302 | info->set_device_type(get_device_type(device_type)); |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 303 | event->set_disconnect_reason(disconnect_reason); |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 304 | event->set_event_time_millis(timestamp_ms); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 305 | pimpl_->pair_event_queue_->Enqueue(event); |
Jack He | 9bebcc0 | 2017-01-17 15:41:30 -0800 | [diff] [blame] | 306 | { |
| 307 | std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_); |
| 308 | pimpl_->bluetooth_log_->set_num_pair_event( |
| 309 | pimpl_->bluetooth_log_->num_pair_event() + 1); |
| 310 | } |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 313 | void BluetoothMetricsLogger::LogWakeEvent(wake_event_type_t type, |
| 314 | const std::string& requestor, |
| 315 | const std::string& name, |
| 316 | uint64_t timestamp_ms) { |
| 317 | WakeEvent* event = new WakeEvent(); |
| 318 | event->set_wake_event_type(get_wake_event_type(type)); |
| 319 | event->set_requestor(requestor); |
| 320 | event->set_name(name); |
Pavlin Radoslavov | 9aa6f12 | 2016-02-17 15:42:38 -0800 | [diff] [blame] | 321 | event->set_event_time_millis(timestamp_ms); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 322 | pimpl_->wake_event_queue_->Enqueue(event); |
Jack He | 9bebcc0 | 2017-01-17 15:41:30 -0800 | [diff] [blame] | 323 | { |
| 324 | std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_); |
| 325 | pimpl_->bluetooth_log_->set_num_wake_event( |
| 326 | pimpl_->bluetooth_log_->num_wake_event() + 1); |
| 327 | } |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 330 | void BluetoothMetricsLogger::LogScanEvent(bool start, |
| 331 | const std::string& initator, |
| 332 | scan_tech_t type, uint32_t results, |
| 333 | uint64_t timestamp_ms) { |
| 334 | ScanEvent* event = new ScanEvent(); |
| 335 | if (start) { |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 336 | event->set_scan_event_type(ScanEvent::SCAN_EVENT_START); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 337 | } else { |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 338 | event->set_scan_event_type(ScanEvent::SCAN_EVENT_STOP); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 339 | } |
| 340 | event->set_initiator(initator); |
| 341 | event->set_scan_technology_type(get_scan_tech_type(type)); |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 342 | event->set_number_results(results); |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 343 | event->set_event_time_millis(timestamp_ms); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 344 | pimpl_->scan_event_queue_->Enqueue(event); |
Jack He | 9bebcc0 | 2017-01-17 15:41:30 -0800 | [diff] [blame] | 345 | { |
| 346 | std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_); |
| 347 | pimpl_->bluetooth_log_->set_num_scan_event( |
| 348 | pimpl_->bluetooth_log_->num_scan_event() + 1); |
| 349 | } |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 352 | void BluetoothMetricsLogger::LogBluetoothSessionStart( |
| 353 | connection_tech_t connection_tech_type, uint64_t timestamp_ms) { |
| 354 | std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_); |
| 355 | if (pimpl_->bluetooth_session_ != nullptr) { |
Jack He | 9bebcc0 | 2017-01-17 15:41:30 -0800 | [diff] [blame] | 356 | LogBluetoothSessionEnd(DISCONNECT_REASON_NEXT_START_WITHOUT_END_PREVIOUS, |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 357 | 0); |
| 358 | } |
| 359 | if (timestamp_ms == 0) { |
Jack He | 4282398 | 2018-08-15 12:38:37 -0700 | [diff] [blame] | 360 | timestamp_ms = bluetooth::common::time_get_os_boottime_ms(); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 361 | } |
| 362 | pimpl_->bluetooth_session_start_time_ms_ = timestamp_ms; |
| 363 | pimpl_->bluetooth_session_ = new BluetoothSession(); |
| 364 | pimpl_->bluetooth_session_->set_connection_technology_type( |
| 365 | get_connection_tech_type(connection_tech_type)); |
| 366 | } |
Pavlin Radoslavov | 9aa6f12 | 2016-02-17 15:42:38 -0800 | [diff] [blame] | 367 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 368 | void BluetoothMetricsLogger::LogBluetoothSessionEnd( |
Jack He | 9bebcc0 | 2017-01-17 15:41:30 -0800 | [diff] [blame] | 369 | disconnect_reason_t disconnect_reason, uint64_t timestamp_ms) { |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 370 | std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_); |
| 371 | if (pimpl_->bluetooth_session_ == nullptr) { |
| 372 | return; |
| 373 | } |
| 374 | if (timestamp_ms == 0) { |
Jack He | 4282398 | 2018-08-15 12:38:37 -0700 | [diff] [blame] | 375 | timestamp_ms = bluetooth::common::time_get_os_boottime_ms(); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 376 | } |
| 377 | int64_t session_duration_sec = |
| 378 | (timestamp_ms - pimpl_->bluetooth_session_start_time_ms_) / 1000; |
| 379 | pimpl_->bluetooth_session_->set_session_duration_sec(session_duration_sec); |
Jack He | 9bebcc0 | 2017-01-17 15:41:30 -0800 | [diff] [blame] | 380 | pimpl_->bluetooth_session_->set_disconnect_reason_type( |
| 381 | get_disconnect_reason_type(disconnect_reason)); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 382 | pimpl_->bt_session_queue_->Enqueue(pimpl_->bluetooth_session_); |
| 383 | pimpl_->bluetooth_session_ = nullptr; |
Jack He | eeb9f35 | 2018-06-08 20:13:23 -0700 | [diff] [blame] | 384 | pimpl_->a2dp_session_metrics_ = A2dpSessionMetrics(); |
Jack He | 9bebcc0 | 2017-01-17 15:41:30 -0800 | [diff] [blame] | 385 | { |
| 386 | std::lock_guard<std::recursive_mutex> log_lock(pimpl_->bluetooth_log_lock_); |
| 387 | pimpl_->bluetooth_log_->set_num_bluetooth_session( |
| 388 | pimpl_->bluetooth_log_->num_bluetooth_session() + 1); |
| 389 | } |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 390 | } |
Pavlin Radoslavov | 9aa6f12 | 2016-02-17 15:42:38 -0800 | [diff] [blame] | 391 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 392 | void BluetoothMetricsLogger::LogBluetoothSessionDeviceInfo( |
| 393 | uint32_t device_class, device_type_t device_type) { |
| 394 | std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_); |
| 395 | if (pimpl_->bluetooth_session_ == nullptr) { |
| 396 | LogBluetoothSessionStart(CONNECTION_TECHNOLOGY_TYPE_UNKNOWN, 0); |
| 397 | } |
| 398 | DeviceInfo* info = pimpl_->bluetooth_session_->mutable_device_connected_to(); |
Pavlin Radoslavov | 9aa6f12 | 2016-02-17 15:42:38 -0800 | [diff] [blame] | 399 | info->set_device_class(device_class); |
| 400 | info->set_device_type(DeviceInfo::DEVICE_TYPE_BREDR); |
Pavlin Radoslavov | 9aa6f12 | 2016-02-17 15:42:38 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 403 | void BluetoothMetricsLogger::LogA2dpSession( |
| 404 | const A2dpSessionMetrics& a2dp_session_metrics) { |
| 405 | std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_); |
| 406 | if (pimpl_->bluetooth_session_ == nullptr) { |
| 407 | // When no bluetooth session exist, create one on system's behalf |
| 408 | // Set connection type: for A2DP it is always BR/EDR |
| 409 | LogBluetoothSessionStart(CONNECTION_TECHNOLOGY_TYPE_BREDR, 0); |
| 410 | LogBluetoothSessionDeviceInfo(BTM_COD_MAJOR_AUDIO, DEVICE_TYPE_BREDR); |
| 411 | } |
| 412 | // Accumulate metrics |
| 413 | pimpl_->a2dp_session_metrics_.Update(a2dp_session_metrics); |
| 414 | // Get or allocate new A2DP session object |
| 415 | A2DPSession* a2dp_session = |
| 416 | pimpl_->bluetooth_session_->mutable_a2dp_session(); |
| 417 | a2dp_session->set_audio_duration_millis( |
| 418 | pimpl_->a2dp_session_metrics_.audio_duration_ms); |
| 419 | a2dp_session->set_media_timer_min_millis( |
| 420 | pimpl_->a2dp_session_metrics_.media_timer_min_ms); |
| 421 | a2dp_session->set_media_timer_max_millis( |
| 422 | pimpl_->a2dp_session_metrics_.media_timer_max_ms); |
| 423 | a2dp_session->set_media_timer_avg_millis( |
| 424 | pimpl_->a2dp_session_metrics_.media_timer_avg_ms); |
| 425 | a2dp_session->set_buffer_overruns_max_count( |
| 426 | pimpl_->a2dp_session_metrics_.buffer_overruns_max_count); |
| 427 | a2dp_session->set_buffer_overruns_total( |
| 428 | pimpl_->a2dp_session_metrics_.buffer_overruns_total); |
| 429 | a2dp_session->set_buffer_underruns_average( |
| 430 | pimpl_->a2dp_session_metrics_.buffer_underruns_average); |
| 431 | a2dp_session->set_buffer_underruns_count( |
| 432 | pimpl_->a2dp_session_metrics_.buffer_underruns_count); |
Jack He | eeb9f35 | 2018-06-08 20:13:23 -0700 | [diff] [blame] | 433 | a2dp_session->set_source_codec( |
| 434 | get_a2dp_source_codec(pimpl_->a2dp_session_metrics_.codec_index)); |
| 435 | a2dp_session->set_is_a2dp_offload( |
| 436 | pimpl_->a2dp_session_metrics_.is_a2dp_offload); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 437 | } |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 438 | |
Jack He | 7ebb85a | 2018-04-02 13:04:58 -0700 | [diff] [blame] | 439 | void BluetoothMetricsLogger::LogHeadsetProfileRfcConnection( |
| 440 | tBTA_SERVICE_ID service_id) { |
| 441 | std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_); |
| 442 | switch (service_id) { |
| 443 | case BTA_HSP_SERVICE_ID: |
| 444 | pimpl_->headset_profile_connection_counts_[HeadsetProfileType::HSP]++; |
| 445 | break; |
| 446 | case BTA_HFP_SERVICE_ID: |
| 447 | pimpl_->headset_profile_connection_counts_[HeadsetProfileType::HFP]++; |
| 448 | break; |
| 449 | default: |
| 450 | pimpl_->headset_profile_connection_counts_ |
| 451 | [HeadsetProfileType::HEADSET_PROFILE_UNKNOWN]++; |
| 452 | break; |
| 453 | } |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | void BluetoothMetricsLogger::WriteString(std::string* serialized) { |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 458 | std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_); |
Jack He | 4282398 | 2018-08-15 12:38:37 -0700 | [diff] [blame] | 459 | LOG(INFO) << __func__ << ": building metrics"; |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 460 | Build(); |
Jack He | 4282398 | 2018-08-15 12:38:37 -0700 | [diff] [blame] | 461 | LOG(INFO) << __func__ << ": serializing metrics"; |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 462 | if (!pimpl_->bluetooth_log_->SerializeToString(serialized)) { |
Jack He | 4282398 | 2018-08-15 12:38:37 -0700 | [diff] [blame] | 463 | LOG(ERROR) << __func__ << ": error serializing metrics"; |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 464 | } |
Jack He | 7ebb85a | 2018-04-02 13:04:58 -0700 | [diff] [blame] | 465 | // Always clean up log objects |
| 466 | pimpl_->bluetooth_log_->Clear(); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 467 | } |
Ajay Panicker | fe357dc | 2016-02-18 12:24:10 -0800 | [diff] [blame] | 468 | |
Jack He | 7ebb85a | 2018-04-02 13:04:58 -0700 | [diff] [blame] | 469 | void BluetoothMetricsLogger::WriteBase64String(std::string* serialized) { |
| 470 | this->WriteString(serialized); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 471 | base::Base64Encode(*serialized, serialized); |
| 472 | } |
| 473 | |
Jack He | 7ebb85a | 2018-04-02 13:04:58 -0700 | [diff] [blame] | 474 | void BluetoothMetricsLogger::WriteBase64(int fd) { |
Ajay Panicker | 4336ba5 | 2016-02-17 18:18:00 -0800 | [diff] [blame] | 475 | std::string protoBase64; |
Jack He | 7ebb85a | 2018-04-02 13:04:58 -0700 | [diff] [blame] | 476 | this->WriteBase64String(&protoBase64); |
Pavlin Radoslavov | c608acf | 2016-05-12 11:36:44 -0700 | [diff] [blame] | 477 | ssize_t ret; |
| 478 | OSI_NO_INTR(ret = write(fd, protoBase64.c_str(), protoBase64.size())); |
| 479 | if (ret == -1) { |
Jack He | 4282398 | 2018-08-15 12:38:37 -0700 | [diff] [blame] | 480 | LOG(ERROR) << __func__ |
| 481 | << ": error writing to dumpsys fd: " << strerror(errno) << " (" |
| 482 | << std::to_string(errno) << ")"; |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 483 | } |
Marie Janssen | a84a4ee | 2016-01-15 16:14:14 -0800 | [diff] [blame] | 484 | } |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 485 | |
| 486 | void BluetoothMetricsLogger::CutoffSession() { |
| 487 | std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_); |
| 488 | if (pimpl_->bluetooth_session_ != nullptr) { |
| 489 | BluetoothSession* new_bt_session = |
| 490 | new BluetoothSession(*pimpl_->bluetooth_session_); |
| 491 | new_bt_session->clear_a2dp_session(); |
| 492 | new_bt_session->clear_rfcomm_session(); |
Jack He | 9bebcc0 | 2017-01-17 15:41:30 -0800 | [diff] [blame] | 493 | LogBluetoothSessionEnd(DISCONNECT_REASON_METRICS_DUMP, 0); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 494 | pimpl_->bluetooth_session_ = new_bt_session; |
Jack He | 4282398 | 2018-08-15 12:38:37 -0700 | [diff] [blame] | 495 | pimpl_->bluetooth_session_start_time_ms_ = |
| 496 | bluetooth::common::time_get_os_boottime_ms(); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 497 | pimpl_->a2dp_session_metrics_ = A2dpSessionMetrics(); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | void BluetoothMetricsLogger::Build() { |
| 502 | std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_); |
| 503 | CutoffSession(); |
| 504 | BluetoothLog* bluetooth_log = pimpl_->bluetooth_log_; |
| 505 | while (!pimpl_->bt_session_queue_->Empty() && |
| 506 | static_cast<size_t>(bluetooth_log->session_size()) <= |
| 507 | pimpl_->bt_session_queue_->Capacity()) { |
| 508 | bluetooth_log->mutable_session()->AddAllocated( |
| 509 | pimpl_->bt_session_queue_->Dequeue()); |
| 510 | } |
| 511 | while (!pimpl_->pair_event_queue_->Empty() && |
| 512 | static_cast<size_t>(bluetooth_log->pair_event_size()) <= |
| 513 | pimpl_->pair_event_queue_->Capacity()) { |
| 514 | bluetooth_log->mutable_pair_event()->AddAllocated( |
| 515 | pimpl_->pair_event_queue_->Dequeue()); |
| 516 | } |
| 517 | while (!pimpl_->scan_event_queue_->Empty() && |
| 518 | static_cast<size_t>(bluetooth_log->scan_event_size()) <= |
| 519 | pimpl_->scan_event_queue_->Capacity()) { |
| 520 | bluetooth_log->mutable_scan_event()->AddAllocated( |
| 521 | pimpl_->scan_event_queue_->Dequeue()); |
| 522 | } |
| 523 | while (!pimpl_->wake_event_queue_->Empty() && |
| 524 | static_cast<size_t>(bluetooth_log->wake_event_size()) <= |
| 525 | pimpl_->wake_event_queue_->Capacity()) { |
| 526 | bluetooth_log->mutable_wake_event()->AddAllocated( |
| 527 | pimpl_->wake_event_queue_->Dequeue()); |
| 528 | } |
| 529 | while (!pimpl_->bt_session_queue_->Empty() && |
| 530 | static_cast<size_t>(bluetooth_log->wake_event_size()) <= |
| 531 | pimpl_->wake_event_queue_->Capacity()) { |
| 532 | bluetooth_log->mutable_wake_event()->AddAllocated( |
| 533 | pimpl_->wake_event_queue_->Dequeue()); |
| 534 | } |
Jack He | 7ebb85a | 2018-04-02 13:04:58 -0700 | [diff] [blame] | 535 | for (size_t i = 0; i < HeadsetProfileType_ARRAYSIZE; ++i) { |
| 536 | int num_times_connected = pimpl_->headset_profile_connection_counts_[i]; |
| 537 | if (HeadsetProfileType_IsValid(i) && num_times_connected > 0) { |
| 538 | HeadsetProfileConnectionStats* headset_profile_connection_stats = |
| 539 | bluetooth_log->add_headset_profile_connection_stats(); |
| 540 | // Able to static_cast because HeadsetProfileType_IsValid(i) is true |
| 541 | headset_profile_connection_stats->set_headset_profile_type( |
| 542 | static_cast<HeadsetProfileType>(i)); |
| 543 | headset_profile_connection_stats->set_num_times_connected( |
| 544 | num_times_connected); |
| 545 | } |
| 546 | } |
| 547 | pimpl_->headset_profile_connection_counts_.fill(0); |
Jack He | 777228a | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | void BluetoothMetricsLogger::ResetSession() { |
| 551 | std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_); |
| 552 | if (pimpl_->bluetooth_session_ != nullptr) { |
| 553 | delete pimpl_->bluetooth_session_; |
| 554 | pimpl_->bluetooth_session_ = nullptr; |
| 555 | } |
| 556 | pimpl_->bluetooth_session_start_time_ms_ = 0; |
| 557 | pimpl_->a2dp_session_metrics_ = A2dpSessionMetrics(); |
| 558 | } |
| 559 | |
| 560 | void BluetoothMetricsLogger::ResetLog() { |
| 561 | std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_); |
| 562 | pimpl_->bluetooth_log_->Clear(); |
| 563 | } |
| 564 | |
| 565 | void BluetoothMetricsLogger::Reset() { |
| 566 | ResetSession(); |
| 567 | ResetLog(); |
| 568 | pimpl_->bt_session_queue_->Clear(); |
| 569 | pimpl_->pair_event_queue_->Clear(); |
| 570 | pimpl_->wake_event_queue_->Clear(); |
| 571 | pimpl_->scan_event_queue_->Clear(); |
| 572 | } |
| 573 | |
Jack He | 0d4807d | 2018-12-20 15:46:17 -0800 | [diff] [blame] | 574 | void LogLinkLayerConnectionEvent(const RawAddress* address, |
| 575 | uint32_t connection_handle, |
| 576 | android::bluetooth::DirectionEnum direction, |
Jack He | 7a98b40 | 2019-02-06 20:24:24 -0800 | [diff] [blame] | 577 | uint16_t link_type, uint32_t hci_cmd, |
| 578 | uint16_t hci_event, uint16_t hci_ble_event, |
| 579 | uint16_t cmd_status, uint16_t reason_code) { |
Jack He | 0d4807d | 2018-12-20 15:46:17 -0800 | [diff] [blame] | 580 | std::string obfuscated_id; |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 581 | int metric_id = 0; |
Jack He | 0d4807d | 2018-12-20 15:46:17 -0800 | [diff] [blame] | 582 | if (address != nullptr) { |
| 583 | obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(*address); |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 584 | metric_id = MetricIdAllocator::GetInstance().AllocateId(*address); |
Jack He | 0d4807d | 2018-12-20 15:46:17 -0800 | [diff] [blame] | 585 | } |
| 586 | // nullptr and size 0 represent missing value for obfuscated_id |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 587 | BytesField bytes_field(address != nullptr ? obfuscated_id.c_str() : nullptr, |
| 588 | address != nullptr ? obfuscated_id.size() : 0); |
| 589 | int ret = |
| 590 | stats_write(BLUETOOTH_LINK_LAYER_CONNECTION_EVENT, bytes_field, |
| 591 | connection_handle, direction, link_type, hci_cmd, hci_event, |
| 592 | hci_ble_event, cmd_status, reason_code, metric_id); |
Jack He | 0d4807d | 2018-12-20 15:46:17 -0800 | [diff] [blame] | 593 | if (ret < 0) { |
Jack He | 7a98b40 | 2019-02-06 20:24:24 -0800 | [diff] [blame] | 594 | LOG(WARNING) << __func__ << ": failed to log status " << loghex(cmd_status) |
| 595 | << ", reason " << loghex(reason_code) << " from cmd " |
| 596 | << loghex(hci_cmd) << ", event " << loghex(hci_event) |
| 597 | << ", ble_event " << loghex(hci_ble_event) << " for " |
| 598 | << address << ", handle " << connection_handle << ", type " |
| 599 | << loghex(link_type) << ", error " << ret; |
Jack He | 0d4807d | 2018-12-20 15:46:17 -0800 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
Jack He | 0e818cf | 2019-01-20 23:16:45 -0800 | [diff] [blame] | 603 | void LogHciTimeoutEvent(uint32_t hci_cmd) { |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 604 | int ret = stats_write(BLUETOOTH_HCI_TIMEOUT_REPORTED, |
| 605 | static_cast<int64_t>(hci_cmd)); |
Jack He | 0e818cf | 2019-01-20 23:16:45 -0800 | [diff] [blame] | 606 | if (ret < 0) { |
| 607 | LOG(WARNING) << __func__ << ": failed for opcode " << loghex(hci_cmd) |
| 608 | << ", error " << ret; |
| 609 | } |
| 610 | } |
| 611 | |
Jack He | 078c8a8 | 2019-01-29 14:49:34 -0800 | [diff] [blame] | 612 | void LogRemoteVersionInfo(uint16_t handle, uint8_t status, uint8_t version, |
| 613 | uint16_t manufacturer_name, uint16_t subversion) { |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 614 | int ret = stats_write(BLUETOOTH_REMOTE_VERSION_INFO_REPORTED, handle, status, |
| 615 | version, manufacturer_name, subversion); |
Jack He | 078c8a8 | 2019-01-29 14:49:34 -0800 | [diff] [blame] | 616 | if (ret < 0) { |
| 617 | LOG(WARNING) << __func__ << ": failed for handle " << handle << ", status " |
| 618 | << loghex(status) << ", version " << loghex(version) |
| 619 | << ", manufacturer_name " << loghex(manufacturer_name) |
| 620 | << ", subversion " << loghex(subversion) << ", error " << ret; |
| 621 | } |
| 622 | } |
Jack He | 7d7a8de | 2019-01-20 21:31:12 -0800 | [diff] [blame] | 623 | |
| 624 | void LogA2dpAudioUnderrunEvent(const RawAddress& address, |
| 625 | uint64_t encoding_interval_millis, |
| 626 | int num_missing_pcm_bytes) { |
| 627 | std::string obfuscated_id; |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 628 | int metric_id = 0; |
Jack He | 7d7a8de | 2019-01-20 21:31:12 -0800 | [diff] [blame] | 629 | if (!address.IsEmpty()) { |
| 630 | obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address); |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 631 | metric_id = MetricIdAllocator::GetInstance().AllocateId(address); |
Jack He | 7d7a8de | 2019-01-20 21:31:12 -0800 | [diff] [blame] | 632 | } |
| 633 | // nullptr and size 0 represent missing value for obfuscated_id |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 634 | BytesField bytes_field(address.IsEmpty() ? nullptr : obfuscated_id.c_str(), |
| 635 | address.IsEmpty() ? 0 : obfuscated_id.size()); |
Jack He | 7d7a8de | 2019-01-20 21:31:12 -0800 | [diff] [blame] | 636 | int64_t encoding_interval_nanos = encoding_interval_millis * 1000000; |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 637 | int ret = |
| 638 | stats_write(BLUETOOTH_A2DP_AUDIO_UNDERRUN_REPORTED, bytes_field, |
| 639 | encoding_interval_nanos, num_missing_pcm_bytes, metric_id); |
Jack He | 7d7a8de | 2019-01-20 21:31:12 -0800 | [diff] [blame] | 640 | if (ret < 0) { |
| 641 | LOG(WARNING) << __func__ << ": failed for " << address |
| 642 | << ", encoding_interval_nanos " << encoding_interval_nanos |
| 643 | << ", num_missing_pcm_bytes " << num_missing_pcm_bytes |
| 644 | << ", error " << ret; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | void LogA2dpAudioOverrunEvent(const RawAddress& address, |
| 649 | uint64_t encoding_interval_millis, |
| 650 | int num_dropped_buffers, |
| 651 | int num_dropped_encoded_frames, |
| 652 | int num_dropped_encoded_bytes) { |
| 653 | std::string obfuscated_id; |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 654 | int metric_id = 0; |
Jack He | 7d7a8de | 2019-01-20 21:31:12 -0800 | [diff] [blame] | 655 | if (!address.IsEmpty()) { |
| 656 | obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address); |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 657 | metric_id = MetricIdAllocator::GetInstance().AllocateId(address); |
Jack He | 7d7a8de | 2019-01-20 21:31:12 -0800 | [diff] [blame] | 658 | } |
| 659 | // nullptr and size 0 represent missing value for obfuscated_id |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 660 | BytesField bytes_field(address.IsEmpty() ? nullptr : obfuscated_id.c_str(), |
| 661 | address.IsEmpty() ? 0 : obfuscated_id.size()); |
Jack He | 7d7a8de | 2019-01-20 21:31:12 -0800 | [diff] [blame] | 662 | int64_t encoding_interval_nanos = encoding_interval_millis * 1000000; |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 663 | int ret = stats_write(BLUETOOTH_A2DP_AUDIO_OVERRUN_REPORTED, bytes_field, |
| 664 | encoding_interval_nanos, num_dropped_buffers, |
| 665 | num_dropped_encoded_frames, num_dropped_encoded_bytes, |
| 666 | metric_id); |
Jack He | 7d7a8de | 2019-01-20 21:31:12 -0800 | [diff] [blame] | 667 | if (ret < 0) { |
| 668 | LOG(WARNING) << __func__ << ": failed to log for " << address |
| 669 | << ", encoding_interval_nanos " << encoding_interval_nanos |
| 670 | << ", num_dropped_buffers " << num_dropped_buffers |
| 671 | << ", num_dropped_encoded_frames " |
| 672 | << num_dropped_encoded_frames << ", num_dropped_encoded_bytes " |
| 673 | << num_dropped_encoded_bytes << ", error " << ret; |
| 674 | } |
| 675 | } |
| 676 | |
Josh Wu | e9f5c35 | 2021-03-24 20:15:43 +0800 | [diff] [blame] | 677 | void LogA2dpPlaybackEvent(const RawAddress& address, int playback_state, |
| 678 | int audio_coding_mode) { |
| 679 | std::string obfuscated_id; |
| 680 | int metric_id = 0; |
| 681 | if (!address.IsEmpty()) { |
| 682 | obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address); |
| 683 | metric_id = MetricIdAllocator::GetInstance().AllocateId(address); |
| 684 | } |
| 685 | // nullptr and size 0 represent missing value for obfuscated_id |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 686 | BytesField bytes_field(address.IsEmpty() ? nullptr : obfuscated_id.c_str(), |
| 687 | address.IsEmpty() ? 0 : obfuscated_id.size()); |
| 688 | int ret = stats_write(BLUETOOTH_A2DP_PLAYBACK_STATE_CHANGED, bytes_field, |
| 689 | playback_state, audio_coding_mode, metric_id); |
Josh Wu | e9f5c35 | 2021-03-24 20:15:43 +0800 | [diff] [blame] | 690 | if (ret < 0) { |
| 691 | LOG(WARNING) << __func__ << ": failed to log for " << address |
| 692 | << ", playback_state " << playback_state |
| 693 | << ", audio_coding_mode " << audio_coding_mode << ", error " |
| 694 | << ret; |
| 695 | } |
| 696 | } |
| 697 | |
Jack He | 7d7a8de | 2019-01-20 21:31:12 -0800 | [diff] [blame] | 698 | void LogReadRssiResult(const RawAddress& address, uint16_t handle, |
| 699 | uint32_t cmd_status, int8_t rssi) { |
| 700 | std::string obfuscated_id; |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 701 | int metric_id = 0; |
Jack He | 7d7a8de | 2019-01-20 21:31:12 -0800 | [diff] [blame] | 702 | if (!address.IsEmpty()) { |
| 703 | obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address); |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 704 | metric_id = MetricIdAllocator::GetInstance().AllocateId(address); |
Jack He | 7d7a8de | 2019-01-20 21:31:12 -0800 | [diff] [blame] | 705 | } |
| 706 | // nullptr and size 0 represent missing value for obfuscated_id |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 707 | BytesField bytes_field(address.IsEmpty() ? nullptr : obfuscated_id.c_str(), |
| 708 | address.IsEmpty() ? 0 : obfuscated_id.size()); |
| 709 | int ret = stats_write(BLUETOOTH_DEVICE_RSSI_REPORTED, bytes_field, handle, |
| 710 | cmd_status, rssi, metric_id); |
Jack He | 7d7a8de | 2019-01-20 21:31:12 -0800 | [diff] [blame] | 711 | if (ret < 0) { |
| 712 | LOG(WARNING) << __func__ << ": failed for " << address << ", handle " |
| 713 | << handle << ", status " << loghex(cmd_status) << ", rssi " |
| 714 | << rssi << " dBm, error " << ret; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | void LogReadFailedContactCounterResult(const RawAddress& address, |
| 719 | uint16_t handle, uint32_t cmd_status, |
| 720 | int32_t failed_contact_counter) { |
| 721 | std::string obfuscated_id; |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 722 | int metric_id = 0; |
Jack He | 7d7a8de | 2019-01-20 21:31:12 -0800 | [diff] [blame] | 723 | if (!address.IsEmpty()) { |
| 724 | obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address); |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 725 | metric_id = MetricIdAllocator::GetInstance().AllocateId(address); |
Jack He | 7d7a8de | 2019-01-20 21:31:12 -0800 | [diff] [blame] | 726 | } |
| 727 | // nullptr and size 0 represent missing value for obfuscated_id |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 728 | BytesField bytes_field(address.IsEmpty() ? nullptr : obfuscated_id.c_str(), |
| 729 | address.IsEmpty() ? 0 : obfuscated_id.size()); |
| 730 | int ret = |
| 731 | stats_write(BLUETOOTH_DEVICE_FAILED_CONTACT_COUNTER_REPORTED, bytes_field, |
| 732 | handle, cmd_status, failed_contact_counter, metric_id); |
Jack He | 7d7a8de | 2019-01-20 21:31:12 -0800 | [diff] [blame] | 733 | if (ret < 0) { |
| 734 | LOG(WARNING) << __func__ << ": failed for " << address << ", handle " |
| 735 | << handle << ", status " << loghex(cmd_status) |
| 736 | << ", failed_contact_counter " << failed_contact_counter |
| 737 | << " packets, error " << ret; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | void LogReadTxPowerLevelResult(const RawAddress& address, uint16_t handle, |
| 742 | uint32_t cmd_status, |
| 743 | int32_t transmit_power_level) { |
| 744 | std::string obfuscated_id; |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 745 | int metric_id = 0; |
Jack He | 7d7a8de | 2019-01-20 21:31:12 -0800 | [diff] [blame] | 746 | if (!address.IsEmpty()) { |
| 747 | obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address); |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 748 | metric_id = MetricIdAllocator::GetInstance().AllocateId(address); |
Jack He | 7d7a8de | 2019-01-20 21:31:12 -0800 | [diff] [blame] | 749 | } |
| 750 | // nullptr and size 0 represent missing value for obfuscated_id |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 751 | BytesField bytes_field(address.IsEmpty() ? nullptr : obfuscated_id.c_str(), |
| 752 | address.IsEmpty() ? 0 : obfuscated_id.size()); |
| 753 | int ret = stats_write(BLUETOOTH_DEVICE_TX_POWER_LEVEL_REPORTED, bytes_field, |
| 754 | handle, cmd_status, transmit_power_level, metric_id); |
Jack He | 7d7a8de | 2019-01-20 21:31:12 -0800 | [diff] [blame] | 755 | if (ret < 0) { |
| 756 | LOG(WARNING) << __func__ << ": failed for " << address << ", handle " |
| 757 | << handle << ", status " << loghex(cmd_status) |
| 758 | << ", transmit_power_level " << transmit_power_level |
| 759 | << " packets, error " << ret; |
| 760 | } |
| 761 | } |
| 762 | |
Jack He | b75275d | 2019-01-31 15:17:08 -0800 | [diff] [blame] | 763 | void LogSmpPairingEvent(const RawAddress& address, uint8_t smp_cmd, |
| 764 | android::bluetooth::DirectionEnum direction, |
| 765 | uint8_t smp_fail_reason) { |
| 766 | std::string obfuscated_id; |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 767 | int metric_id = 0; |
Jack He | b75275d | 2019-01-31 15:17:08 -0800 | [diff] [blame] | 768 | if (!address.IsEmpty()) { |
| 769 | obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address); |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 770 | metric_id = MetricIdAllocator::GetInstance().AllocateId(address); |
Jack He | b75275d | 2019-01-31 15:17:08 -0800 | [diff] [blame] | 771 | } |
| 772 | // nullptr and size 0 represent missing value for obfuscated_id |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 773 | BytesField obfuscated_id_field( |
Jack He | b75275d | 2019-01-31 15:17:08 -0800 | [diff] [blame] | 774 | address.IsEmpty() ? nullptr : obfuscated_id.c_str(), |
| 775 | address.IsEmpty() ? 0 : obfuscated_id.size()); |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 776 | int ret = |
| 777 | stats_write(BLUETOOTH_SMP_PAIRING_EVENT_REPORTED, obfuscated_id_field, |
| 778 | smp_cmd, direction, smp_fail_reason, metric_id); |
Jack He | b75275d | 2019-01-31 15:17:08 -0800 | [diff] [blame] | 779 | if (ret < 0) { |
| 780 | LOG(WARNING) << __func__ << ": failed for " << address << ", smp_cmd " |
| 781 | << loghex(smp_cmd) << ", direction " << direction |
| 782 | << ", smp_fail_reason " << loghex(smp_fail_reason) |
| 783 | << ", error " << ret; |
| 784 | } |
| 785 | } |
| 786 | |
Jack He | 4055316 | 2019-03-20 04:04:36 -0700 | [diff] [blame] | 787 | void LogClassicPairingEvent(const RawAddress& address, uint16_t handle, uint32_t hci_cmd, uint16_t hci_event, |
| 788 | uint16_t cmd_status, uint16_t reason_code, int64_t event_value) { |
Jack He | 0d888e5 | 2019-01-31 17:44:46 -0800 | [diff] [blame] | 789 | std::string obfuscated_id; |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 790 | int metric_id = 0; |
Jack He | 0d888e5 | 2019-01-31 17:44:46 -0800 | [diff] [blame] | 791 | if (!address.IsEmpty()) { |
| 792 | obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address); |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 793 | metric_id = MetricIdAllocator::GetInstance().AllocateId(address); |
Jack He | 0d888e5 | 2019-01-31 17:44:46 -0800 | [diff] [blame] | 794 | } |
| 795 | // nullptr and size 0 represent missing value for obfuscated_id |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 796 | BytesField obfuscated_id_field( |
Jack He | 0d888e5 | 2019-01-31 17:44:46 -0800 | [diff] [blame] | 797 | address.IsEmpty() ? nullptr : obfuscated_id.c_str(), |
| 798 | address.IsEmpty() ? 0 : obfuscated_id.size()); |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 799 | int ret = stats_write(BLUETOOTH_CLASSIC_PAIRING_EVENT_REPORTED, |
| 800 | obfuscated_id_field, handle, hci_cmd, hci_event, |
| 801 | cmd_status, reason_code, event_value, metric_id); |
Jack He | 0d888e5 | 2019-01-31 17:44:46 -0800 | [diff] [blame] | 802 | if (ret < 0) { |
Jack He | 4055316 | 2019-03-20 04:04:36 -0700 | [diff] [blame] | 803 | LOG(WARNING) << __func__ << ": failed for " << address << ", handle " << handle << ", hci_cmd " << loghex(hci_cmd) |
| 804 | << ", hci_event " << loghex(hci_event) << ", cmd_status " << loghex(cmd_status) << ", reason " |
| 805 | << loghex(reason_code) << ", event_value " << event_value << ", error " << ret; |
Jack He | 0d888e5 | 2019-01-31 17:44:46 -0800 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | |
Jack He | 3c162a8 | 2019-02-01 18:30:21 -0800 | [diff] [blame] | 809 | void LogSdpAttribute(const RawAddress& address, uint16_t protocol_uuid, |
| 810 | uint16_t attribute_id, size_t attribute_size, |
| 811 | const char* attribute_value) { |
| 812 | std::string obfuscated_id; |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 813 | int metric_id = 0; |
Jack He | 3c162a8 | 2019-02-01 18:30:21 -0800 | [diff] [blame] | 814 | if (!address.IsEmpty()) { |
| 815 | obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address); |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 816 | metric_id = MetricIdAllocator::GetInstance().AllocateId(address); |
Jack He | 3c162a8 | 2019-02-01 18:30:21 -0800 | [diff] [blame] | 817 | } |
| 818 | // nullptr and size 0 represent missing value for obfuscated_id |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 819 | BytesField obfuscated_id_field( |
Jack He | 3c162a8 | 2019-02-01 18:30:21 -0800 | [diff] [blame] | 820 | address.IsEmpty() ? nullptr : obfuscated_id.c_str(), |
| 821 | address.IsEmpty() ? 0 : obfuscated_id.size()); |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 822 | BytesField attribute_field(attribute_value, attribute_size); |
| 823 | int ret = |
| 824 | stats_write(BLUETOOTH_SDP_ATTRIBUTE_REPORTED, obfuscated_id_field, |
| 825 | protocol_uuid, attribute_id, attribute_field, metric_id); |
Jack He | 3c162a8 | 2019-02-01 18:30:21 -0800 | [diff] [blame] | 826 | if (ret < 0) { |
| 827 | LOG(WARNING) << __func__ << ": failed for " << address << ", protocol_uuid " |
| 828 | << loghex(protocol_uuid) << ", attribute_id " |
| 829 | << loghex(attribute_id) << ", error " << ret; |
| 830 | } |
| 831 | } |
| 832 | |
Jack He | 7a98b40 | 2019-02-06 20:24:24 -0800 | [diff] [blame] | 833 | void LogSocketConnectionState( |
| 834 | const RawAddress& address, int port, int type, |
| 835 | android::bluetooth::SocketConnectionstateEnum connection_state, |
| 836 | int64_t tx_bytes, int64_t rx_bytes, int uid, int server_port, |
| 837 | android::bluetooth::SocketRoleEnum socket_role) { |
| 838 | std::string obfuscated_id; |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 839 | int metric_id = 0; |
Jack He | 7a98b40 | 2019-02-06 20:24:24 -0800 | [diff] [blame] | 840 | if (!address.IsEmpty()) { |
| 841 | obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address); |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 842 | metric_id = MetricIdAllocator::GetInstance().AllocateId(address); |
Jack He | 7a98b40 | 2019-02-06 20:24:24 -0800 | [diff] [blame] | 843 | } |
| 844 | // nullptr and size 0 represent missing value for obfuscated_id |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 845 | BytesField obfuscated_id_field( |
Jack He | 7a98b40 | 2019-02-06 20:24:24 -0800 | [diff] [blame] | 846 | address.IsEmpty() ? nullptr : obfuscated_id.c_str(), |
| 847 | address.IsEmpty() ? 0 : obfuscated_id.size()); |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 848 | int ret = |
| 849 | stats_write(BLUETOOTH_SOCKET_CONNECTION_STATE_CHANGED, |
| 850 | obfuscated_id_field, port, type, connection_state, tx_bytes, |
| 851 | rx_bytes, uid, server_port, socket_role, metric_id); |
Jack He | 7a98b40 | 2019-02-06 20:24:24 -0800 | [diff] [blame] | 852 | if (ret < 0) { |
| 853 | LOG(WARNING) << __func__ << ": failed for " << address << ", port " << port |
| 854 | << ", type " << type << ", state " << connection_state |
| 855 | << ", tx_bytes " << tx_bytes << ", rx_bytes " << rx_bytes |
| 856 | << ", uid " << uid << ", server_port " << server_port |
| 857 | << ", socket_role " << socket_role << ", error " << ret; |
| 858 | } |
| 859 | } |
| 860 | |
Jack He | 9e02620 | 2019-02-06 21:53:41 -0800 | [diff] [blame] | 861 | void LogManufacturerInfo(const RawAddress& address, |
Chen Chen | c08fc88 | 2022-06-02 15:00:20 -0700 | [diff] [blame] | 862 | android::bluetooth::AddressTypeEnum address_type, |
Jack He | 9e02620 | 2019-02-06 21:53:41 -0800 | [diff] [blame] | 863 | android::bluetooth::DeviceInfoSrcEnum source_type, |
| 864 | const std::string& source_name, |
| 865 | const std::string& manufacturer, |
| 866 | const std::string& model, |
| 867 | const std::string& hardware_version, |
| 868 | const std::string& software_version) { |
| 869 | std::string obfuscated_id; |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 870 | int metric_id = 0; |
Jack He | 9e02620 | 2019-02-06 21:53:41 -0800 | [diff] [blame] | 871 | if (!address.IsEmpty()) { |
| 872 | obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address); |
Chen Chen | 721030a | 2020-03-09 16:19:24 -0700 | [diff] [blame] | 873 | metric_id = MetricIdAllocator::GetInstance().AllocateId(address); |
Jack He | 9e02620 | 2019-02-06 21:53:41 -0800 | [diff] [blame] | 874 | } |
| 875 | // nullptr and size 0 represent missing value for obfuscated_id |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 876 | BytesField obfuscated_id_field( |
Jack He | 9e02620 | 2019-02-06 21:53:41 -0800 | [diff] [blame] | 877 | address.IsEmpty() ? nullptr : obfuscated_id.c_str(), |
| 878 | address.IsEmpty() ? 0 : obfuscated_id.size()); |
Chen Chen | c08fc88 | 2022-06-02 15:00:20 -0700 | [diff] [blame] | 879 | int ret = stats_write( |
| 880 | BLUETOOTH_DEVICE_INFO_REPORTED, obfuscated_id_field, source_type, |
| 881 | source_name.c_str(), manufacturer.c_str(), model.c_str(), |
| 882 | hardware_version.c_str(), software_version.c_str(), metric_id, |
| 883 | address_type, address.address[5], address.address[4], address.address[3]); |
Jack He | 9e02620 | 2019-02-06 21:53:41 -0800 | [diff] [blame] | 884 | if (ret < 0) { |
| 885 | LOG(WARNING) << __func__ << ": failed for " << address << ", source_type " |
| 886 | << source_type << ", source_name " << source_name |
| 887 | << ", manufacturer " << manufacturer << ", model " << model |
| 888 | << ", hardware_version " << hardware_version |
Chen Chen | c08fc88 | 2022-06-02 15:00:20 -0700 | [diff] [blame] | 889 | << ", software_version " << software_version |
| 890 | << " MAC address type " << address_type |
| 891 | << " MAC address prefix " << address.address[5] << " " |
| 892 | << address.address[4] << " " << address.address[3] << ", error " |
Jack He | 9e02620 | 2019-02-06 21:53:41 -0800 | [diff] [blame] | 893 | << ret; |
| 894 | } |
| 895 | } |
| 896 | |
weichinweng | c1a2fc9 | 2020-12-15 14:56:19 +0800 | [diff] [blame] | 897 | void LogBluetoothHalCrashReason(const RawAddress& address, uint32_t error_code, |
| 898 | uint32_t vendor_error_code) { |
| 899 | std::string obfuscated_id; |
weichinweng | c1a2fc9 | 2020-12-15 14:56:19 +0800 | [diff] [blame] | 900 | if (!address.IsEmpty()) { |
| 901 | obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address); |
weichinweng | c1a2fc9 | 2020-12-15 14:56:19 +0800 | [diff] [blame] | 902 | } |
| 903 | // nullptr and size 0 represent missing value for obfuscated_id |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 904 | BytesField obfuscated_id_field( |
weichinweng | c1a2fc9 | 2020-12-15 14:56:19 +0800 | [diff] [blame] | 905 | address.IsEmpty() ? nullptr : obfuscated_id.c_str(), |
| 906 | address.IsEmpty() ? 0 : obfuscated_id.size()); |
Muhammad Qureshi | 6081842 | 2021-09-22 16:00:14 -0700 | [diff] [blame] | 907 | int ret = stats_write(BLUETOOTH_HAL_CRASH_REASON_REPORTED, 0, |
| 908 | obfuscated_id_field, error_code, vendor_error_code); |
weichinweng | c1a2fc9 | 2020-12-15 14:56:19 +0800 | [diff] [blame] | 909 | if (ret < 0) { |
| 910 | LOG(WARNING) << __func__ << ": failed for " << address << ", error_code " |
| 911 | << loghex(error_code) << ", vendor_error_code " |
| 912 | << loghex(vendor_error_code) << ", error " << ret; |
| 913 | } |
| 914 | } |
| 915 | |
Josh Wu | c033a16 | 2022-05-05 01:29:04 -0700 | [diff] [blame] | 916 | void LogLeAudioConnectionSessionReported( |
| 917 | int32_t group_size, int32_t group_metric_id, |
| 918 | int64_t connection_duration_nanos, |
| 919 | std::vector<int64_t>& device_connecting_offset_nanos, |
| 920 | std::vector<int64_t>& device_connected_offset_nanos, |
| 921 | std::vector<int64_t>& device_connection_duration_nanos, |
| 922 | std::vector<int32_t>& device_connection_status, |
| 923 | std::vector<int32_t>& device_disconnection_status, |
| 924 | std::vector<RawAddress>& device_address, |
| 925 | std::vector<int64_t>& streaming_offset_nanos, |
| 926 | std::vector<int64_t>& streaming_duration_nanos, |
| 927 | std::vector<int32_t>& streaming_context_type) { |
| 928 | std::vector<int32_t> device_metric_id(device_address.size()); |
| 929 | for (uint64_t i = 0; i < device_address.size(); i++) { |
| 930 | if (!device_address[i].IsEmpty()) { |
| 931 | device_metric_id[i] = |
| 932 | MetricIdAllocator::GetInstance().AllocateId(device_address[i]); |
| 933 | } else { |
| 934 | device_metric_id[i] = 0; |
| 935 | } |
| 936 | } |
| 937 | int ret = stats_write( |
| 938 | LE_AUDIO_CONNECTION_SESSION_REPORTED, group_size, group_metric_id, |
| 939 | connection_duration_nanos, device_connecting_offset_nanos, |
| 940 | device_connected_offset_nanos, device_connection_duration_nanos, |
| 941 | device_connection_status, device_disconnection_status, device_metric_id, |
| 942 | streaming_offset_nanos, streaming_duration_nanos, streaming_context_type); |
| 943 | if (ret < 0) { |
| 944 | LOG(WARNING) << __func__ << ": failed for group " << group_metric_id |
| 945 | << "device_connecting_offset_nanos[" |
| 946 | << device_connecting_offset_nanos.size() << "], " |
| 947 | << "device_connected_offset_nanos[" |
| 948 | << device_connected_offset_nanos.size() << "], " |
| 949 | << "device_connection_duration_nanos[" |
| 950 | << device_connection_duration_nanos.size() << "], " |
| 951 | << "device_connection_status[" |
| 952 | << device_connection_status.size() << "], " |
| 953 | << "device_disconnection_status[" |
| 954 | << device_disconnection_status.size() << "], " |
| 955 | << "device_metric_id[" << device_metric_id.size() << "], " |
| 956 | << "streaming_offset_nanos[" << streaming_offset_nanos.size() |
| 957 | << "], " |
| 958 | << "streaming_duration_nanos[" |
| 959 | << streaming_duration_nanos.size() << "], " |
| 960 | << "streaming_context_type[" << streaming_context_type.size() |
| 961 | << "]"; |
| 962 | } |
| 963 | } |
| 964 | |
Jack He | 4282398 | 2018-08-15 12:38:37 -0700 | [diff] [blame] | 965 | } // namespace common |
| 966 | |
| 967 | } // namespace bluetooth |