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