blob: 0664186cacef3579b39c5e09e587a4166c4b00e9 [file] [log] [blame]
Marie Janssena84a4ee2016-01-15 16:14:14 -08001/******************************************************************************
2 *
Jakub Pawlowski3b10fdd2017-09-18 09:00:20 -07003 * Copyright 2016 Google, Inc.
Marie Janssena84a4ee2016-01-15 16:14:14 -08004 *
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 Janssena84a4ee2016-01-15 16:14:14 -080018
Chris Manton2a9d9f22021-09-10 23:05:47 -070019#include "metrics.h"
20
21#include <base/base64.h>
22#include <base/logging.h>
23#include <include/hardware/bt_av.h>
Muhammad Qureshi60818422021-09-22 16:00:14 -070024#include <statslog_bt.h>
Jack He777228a2016-12-08 19:29:00 -080025#include <unistd.h>
Chris Manton2a9d9f22021-09-10 23:05:47 -070026
Jack He777228a2016-12-08 19:29:00 -080027#include <algorithm>
Jack He7ebb85a2018-04-02 13:04:58 -070028#include <array>
Jack He777228a2016-12-08 19:29:00 -080029#include <cerrno>
30#include <chrono>
31#include <cstdint>
32#include <cstring>
33#include <memory>
34#include <mutex>
Marie Janssena84a4ee2016-01-15 16:14:14 -080035
Jack He0d4807d2018-12-20 15:46:17 -080036#include "address_obfuscator.h"
Chris Manton2a9d9f22021-09-10 23:05:47 -070037#include "bluetooth/metrics/bluetooth.pb.h"
Jack He42823982018-08-15 12:38:37 -070038#include "leaky_bonded_queue.h"
Chen Chen721030a2020-03-09 16:19:24 -070039#include "metric_id_allocator.h"
Chris Manton2a9d9f22021-09-10 23:05:47 -070040#include "osi/include/osi.h"
41#include "stack/include/btm_api_types.h"
Jack He42823982018-08-15 12:38:37 -070042#include "time_util.h"
Chris Manton2a9d9f22021-09-10 23:05:47 -070043#include "types/raw_address.h"
Jack He777228a2016-12-08 19:29:00 -080044
Jack He42823982018-08-15 12:38:37 -070045namespace bluetooth {
46
47namespace common {
Marie Janssena84a4ee2016-01-15 16:14:14 -080048
Jack He9a30a9f2018-03-03 00:03:25 -080049using bluetooth::metrics::BluetoothMetricsProto::A2DPSession;
Jack Heeeb9f352018-06-08 20:13:23 -070050using bluetooth::metrics::BluetoothMetricsProto::A2dpSourceCodec;
Jack He9a30a9f2018-03-03 00:03:25 -080051using bluetooth::metrics::BluetoothMetricsProto::BluetoothLog;
52using bluetooth::metrics::BluetoothMetricsProto::BluetoothSession;
53using bluetooth::metrics::BluetoothMetricsProto::
54 BluetoothSession_ConnectionTechnologyType;
55using bluetooth::metrics::BluetoothMetricsProto::
56 BluetoothSession_DisconnectReasonType;
57using bluetooth::metrics::BluetoothMetricsProto::DeviceInfo;
58using bluetooth::metrics::BluetoothMetricsProto::DeviceInfo_DeviceType;
Jack He42823982018-08-15 12:38:37 -070059using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileConnectionStats;
Jack He7ebb85a2018-04-02 13:04:58 -070060using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType;
Jack He7ebb85a2018-04-02 13:04:58 -070061using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType_ARRAYSIZE;
62using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType_IsValid;
Jack He42823982018-08-15 12:38:37 -070063using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType_MAX;
64using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType_MIN;
65using bluetooth::metrics::BluetoothMetricsProto::PairEvent;
66using bluetooth::metrics::BluetoothMetricsProto::ScanEvent;
67using bluetooth::metrics::BluetoothMetricsProto::ScanEvent_ScanEventType;
68using bluetooth::metrics::BluetoothMetricsProto::ScanEvent_ScanTechnologyType;
69using bluetooth::metrics::BluetoothMetricsProto::WakeEvent;
70using bluetooth::metrics::BluetoothMetricsProto::WakeEvent_WakeEventType;
Jack He777228a2016-12-08 19:29:00 -080071
72static 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 Janssena84a4ee2016-01-15 16:14:14 -080080 }
81}
82
Jack He777228a2016-12-08 19:29:00 -080083static 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 Janssena84a4ee2016-01-15 16:14:14 -080093
Jack He777228a2016-12-08 19:29:00 -080094void A2dpSessionMetrics::Update(const A2dpSessionMetrics& metrics) {
Jack He723bef82017-01-20 11:17:44 -080095 if (metrics.audio_duration_ms >= 0) {
Jack He777228a2016-12-08 19:29:00 -080096 audio_duration_ms = std::max(static_cast<int64_t>(0), audio_duration_ms);
97 audio_duration_ms += metrics.audio_duration_ms;
98 }
Jack He723bef82017-01-20 11:17:44 -080099 if (metrics.media_timer_min_ms >= 0) {
Jack He777228a2016-12-08 19:29:00 -0800100 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 He723bef82017-01-20 11:17:44 -0800107 if (metrics.media_timer_max_ms >= 0) {
Jack He777228a2016-12-08 19:29:00 -0800108 media_timer_max_ms =
109 std::max(media_timer_max_ms, metrics.media_timer_max_ms);
110 }
Jack He723bef82017-01-20 11:17:44 -0800111 if (metrics.media_timer_avg_ms >= 0 && metrics.total_scheduling_count >= 0) {
Jack He777228a2016-12-08 19:29:00 -0800112 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 He723bef82017-01-20 11:17:44 -0800122 if (metrics.buffer_overruns_max_count >= 0) {
Jack He777228a2016-12-08 19:29:00 -0800123 buffer_overruns_max_count =
124 std::max(buffer_overruns_max_count, metrics.buffer_overruns_max_count);
125 }
Jack He723bef82017-01-20 11:17:44 -0800126 if (metrics.buffer_overruns_total >= 0) {
Jack He777228a2016-12-08 19:29:00 -0800127 buffer_overruns_total =
128 std::max(static_cast<int32_t>(0), buffer_overruns_total);
129 buffer_overruns_total += metrics.buffer_overruns_total;
130 }
Jack He723bef82017-01-20 11:17:44 -0800131 if (metrics.buffer_underruns_average >= 0 &&
132 metrics.buffer_underruns_count >= 0) {
Jack He777228a2016-12-08 19:29:00 -0800133 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 He723bef82017-01-20 11:17:44 -0800138 buffer_underruns_average, buffer_underruns_count,
139 metrics.buffer_underruns_average, metrics.buffer_underruns_count);
Jack He777228a2016-12-08 19:29:00 -0800140 buffer_underruns_count += metrics.buffer_underruns_count;
141 }
142 }
Jack Heeeb9f352018-06-08 20:13:23 -0700143 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 He777228a2016-12-08 19:29:00 -0800149}
Marie Janssena84a4ee2016-01-15 16:14:14 -0800150
Jack He777228a2016-12-08 19:29:00 -0800151bool 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 Heeeb9f352018-06-08 20:13:23 -0700160 buffer_underruns_count == rhs.buffer_underruns_count &&
161 codec_index == rhs.codec_index &&
162 is_a2dp_offload == rhs.is_a2dp_offload;
Jack He777228a2016-12-08 19:29:00 -0800163}
164
165static 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
179static 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
195static 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
213static 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 He9bebcc02017-01-17 15:41:30 -0800225static 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 Heeeb9f352018-06-08 20:13:23 -0700241static 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 He777228a2016-12-08 19:29:00 -0800258struct 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 He7ebb85a2018-04-02 13:04:58 -0700267 headset_profile_connection_counts_.fill(0);
Jack He777228a2016-12-08 19:29:00 -0800268 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 He7ebb85a2018-04-02 13:04:58 -0700275 std::array<int, HeadsetProfileType_ARRAYSIZE>
276 headset_profile_connection_counts_;
Jack He777228a2016-12-08 19:29:00 -0800277 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
291BluetoothMetricsLogger::BluetoothMetricsLogger()
Jack He9bebcc02017-01-17 15:41:30 -0800292 : pimpl_(new impl(kMaxNumBluetoothSession, kMaxNumPairEvent,
293 kMaxNumWakeEvent, kMaxNumScanEvent)) {}
Jack He777228a2016-12-08 19:29:00 -0800294
295void 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 Watson914a9dc2016-10-19 13:15:34 -0700300 DeviceInfo* info = event->mutable_device_paired_with();
Marie Janssena84a4ee2016-01-15 16:14:14 -0800301 info->set_device_class(device_class);
Jack He777228a2016-12-08 19:29:00 -0800302 info->set_device_type(get_device_type(device_type));
Marie Janssena84a4ee2016-01-15 16:14:14 -0800303 event->set_disconnect_reason(disconnect_reason);
Marie Janssena84a4ee2016-01-15 16:14:14 -0800304 event->set_event_time_millis(timestamp_ms);
Jack He777228a2016-12-08 19:29:00 -0800305 pimpl_->pair_event_queue_->Enqueue(event);
Jack He9bebcc02017-01-17 15:41:30 -0800306 {
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 Janssena84a4ee2016-01-15 16:14:14 -0800311}
312
Jack He777228a2016-12-08 19:29:00 -0800313void 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 Radoslavov9aa6f122016-02-17 15:42:38 -0800321 event->set_event_time_millis(timestamp_ms);
Jack He777228a2016-12-08 19:29:00 -0800322 pimpl_->wake_event_queue_->Enqueue(event);
Jack He9bebcc02017-01-17 15:41:30 -0800323 {
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 Janssena84a4ee2016-01-15 16:14:14 -0800328}
329
Jack He777228a2016-12-08 19:29:00 -0800330void 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 Janssena84a4ee2016-01-15 16:14:14 -0800336 event->set_scan_event_type(ScanEvent::SCAN_EVENT_START);
Jack He777228a2016-12-08 19:29:00 -0800337 } else {
Marie Janssena84a4ee2016-01-15 16:14:14 -0800338 event->set_scan_event_type(ScanEvent::SCAN_EVENT_STOP);
Jack He777228a2016-12-08 19:29:00 -0800339 }
340 event->set_initiator(initator);
341 event->set_scan_technology_type(get_scan_tech_type(type));
Marie Janssena84a4ee2016-01-15 16:14:14 -0800342 event->set_number_results(results);
Marie Janssena84a4ee2016-01-15 16:14:14 -0800343 event->set_event_time_millis(timestamp_ms);
Jack He777228a2016-12-08 19:29:00 -0800344 pimpl_->scan_event_queue_->Enqueue(event);
Jack He9bebcc02017-01-17 15:41:30 -0800345 {
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 Janssena84a4ee2016-01-15 16:14:14 -0800350}
351
Jack He777228a2016-12-08 19:29:00 -0800352void 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 He9bebcc02017-01-17 15:41:30 -0800356 LogBluetoothSessionEnd(DISCONNECT_REASON_NEXT_START_WITHOUT_END_PREVIOUS,
Jack He777228a2016-12-08 19:29:00 -0800357 0);
358 }
359 if (timestamp_ms == 0) {
Jack He42823982018-08-15 12:38:37 -0700360 timestamp_ms = bluetooth::common::time_get_os_boottime_ms();
Jack He777228a2016-12-08 19:29:00 -0800361 }
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 Radoslavov9aa6f122016-02-17 15:42:38 -0800367
Jack He777228a2016-12-08 19:29:00 -0800368void BluetoothMetricsLogger::LogBluetoothSessionEnd(
Jack He9bebcc02017-01-17 15:41:30 -0800369 disconnect_reason_t disconnect_reason, uint64_t timestamp_ms) {
Jack He777228a2016-12-08 19:29:00 -0800370 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 He42823982018-08-15 12:38:37 -0700375 timestamp_ms = bluetooth::common::time_get_os_boottime_ms();
Jack He777228a2016-12-08 19:29:00 -0800376 }
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 He9bebcc02017-01-17 15:41:30 -0800380 pimpl_->bluetooth_session_->set_disconnect_reason_type(
381 get_disconnect_reason_type(disconnect_reason));
Jack He777228a2016-12-08 19:29:00 -0800382 pimpl_->bt_session_queue_->Enqueue(pimpl_->bluetooth_session_);
383 pimpl_->bluetooth_session_ = nullptr;
Jack Heeeb9f352018-06-08 20:13:23 -0700384 pimpl_->a2dp_session_metrics_ = A2dpSessionMetrics();
Jack He9bebcc02017-01-17 15:41:30 -0800385 {
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 He777228a2016-12-08 19:29:00 -0800390}
Pavlin Radoslavov9aa6f122016-02-17 15:42:38 -0800391
Jack He777228a2016-12-08 19:29:00 -0800392void 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 Radoslavov9aa6f122016-02-17 15:42:38 -0800399 info->set_device_class(device_class);
400 info->set_device_type(DeviceInfo::DEVICE_TYPE_BREDR);
Pavlin Radoslavov9aa6f122016-02-17 15:42:38 -0800401}
402
Jack He777228a2016-12-08 19:29:00 -0800403void 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 Heeeb9f352018-06-08 20:13:23 -0700433 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 He777228a2016-12-08 19:29:00 -0800437}
Marie Janssena84a4ee2016-01-15 16:14:14 -0800438
Jack He7ebb85a2018-04-02 13:04:58 -0700439void 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
457void BluetoothMetricsLogger::WriteString(std::string* serialized) {
Jack He777228a2016-12-08 19:29:00 -0800458 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_);
Jack He42823982018-08-15 12:38:37 -0700459 LOG(INFO) << __func__ << ": building metrics";
Jack He777228a2016-12-08 19:29:00 -0800460 Build();
Jack He42823982018-08-15 12:38:37 -0700461 LOG(INFO) << __func__ << ": serializing metrics";
Jack He777228a2016-12-08 19:29:00 -0800462 if (!pimpl_->bluetooth_log_->SerializeToString(serialized)) {
Jack He42823982018-08-15 12:38:37 -0700463 LOG(ERROR) << __func__ << ": error serializing metrics";
Marie Janssena84a4ee2016-01-15 16:14:14 -0800464 }
Jack He7ebb85a2018-04-02 13:04:58 -0700465 // Always clean up log objects
466 pimpl_->bluetooth_log_->Clear();
Jack He777228a2016-12-08 19:29:00 -0800467}
Ajay Panickerfe357dc2016-02-18 12:24:10 -0800468
Jack He7ebb85a2018-04-02 13:04:58 -0700469void BluetoothMetricsLogger::WriteBase64String(std::string* serialized) {
470 this->WriteString(serialized);
Jack He777228a2016-12-08 19:29:00 -0800471 base::Base64Encode(*serialized, serialized);
472}
473
Jack He7ebb85a2018-04-02 13:04:58 -0700474void BluetoothMetricsLogger::WriteBase64(int fd) {
Ajay Panicker4336ba52016-02-17 18:18:00 -0800475 std::string protoBase64;
Jack He7ebb85a2018-04-02 13:04:58 -0700476 this->WriteBase64String(&protoBase64);
Pavlin Radoslavovc608acf2016-05-12 11:36:44 -0700477 ssize_t ret;
478 OSI_NO_INTR(ret = write(fd, protoBase64.c_str(), protoBase64.size()));
479 if (ret == -1) {
Jack He42823982018-08-15 12:38:37 -0700480 LOG(ERROR) << __func__
481 << ": error writing to dumpsys fd: " << strerror(errno) << " ("
482 << std::to_string(errno) << ")";
Marie Janssena84a4ee2016-01-15 16:14:14 -0800483 }
Marie Janssena84a4ee2016-01-15 16:14:14 -0800484}
Jack He777228a2016-12-08 19:29:00 -0800485
486void 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 He9bebcc02017-01-17 15:41:30 -0800493 LogBluetoothSessionEnd(DISCONNECT_REASON_METRICS_DUMP, 0);
Jack He777228a2016-12-08 19:29:00 -0800494 pimpl_->bluetooth_session_ = new_bt_session;
Jack He42823982018-08-15 12:38:37 -0700495 pimpl_->bluetooth_session_start_time_ms_ =
496 bluetooth::common::time_get_os_boottime_ms();
Jack He777228a2016-12-08 19:29:00 -0800497 pimpl_->a2dp_session_metrics_ = A2dpSessionMetrics();
498 }
499}
500
501void 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 He7ebb85a2018-04-02 13:04:58 -0700535 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 He777228a2016-12-08 19:29:00 -0800548}
549
550void 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
560void BluetoothMetricsLogger::ResetLog() {
561 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_);
562 pimpl_->bluetooth_log_->Clear();
563}
564
565void 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 He0d4807d2018-12-20 15:46:17 -0800574void LogLinkLayerConnectionEvent(const RawAddress* address,
575 uint32_t connection_handle,
576 android::bluetooth::DirectionEnum direction,
Jack He7a98b402019-02-06 20:24:24 -0800577 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 He0d4807d2018-12-20 15:46:17 -0800580 std::string obfuscated_id;
Chen Chen721030a2020-03-09 16:19:24 -0700581 int metric_id = 0;
Jack He0d4807d2018-12-20 15:46:17 -0800582 if (address != nullptr) {
583 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(*address);
Chen Chen721030a2020-03-09 16:19:24 -0700584 metric_id = MetricIdAllocator::GetInstance().AllocateId(*address);
Jack He0d4807d2018-12-20 15:46:17 -0800585 }
586 // nullptr and size 0 represent missing value for obfuscated_id
Muhammad Qureshi60818422021-09-22 16:00:14 -0700587 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 He0d4807d2018-12-20 15:46:17 -0800593 if (ret < 0) {
Jack He7a98b402019-02-06 20:24:24 -0800594 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 He0d4807d2018-12-20 15:46:17 -0800600 }
601}
602
Jack He0e818cf2019-01-20 23:16:45 -0800603void LogHciTimeoutEvent(uint32_t hci_cmd) {
Muhammad Qureshi60818422021-09-22 16:00:14 -0700604 int ret = stats_write(BLUETOOTH_HCI_TIMEOUT_REPORTED,
605 static_cast<int64_t>(hci_cmd));
Jack He0e818cf2019-01-20 23:16:45 -0800606 if (ret < 0) {
607 LOG(WARNING) << __func__ << ": failed for opcode " << loghex(hci_cmd)
608 << ", error " << ret;
609 }
610}
611
Jack He078c8a82019-01-29 14:49:34 -0800612void LogRemoteVersionInfo(uint16_t handle, uint8_t status, uint8_t version,
613 uint16_t manufacturer_name, uint16_t subversion) {
Muhammad Qureshi60818422021-09-22 16:00:14 -0700614 int ret = stats_write(BLUETOOTH_REMOTE_VERSION_INFO_REPORTED, handle, status,
615 version, manufacturer_name, subversion);
Jack He078c8a82019-01-29 14:49:34 -0800616 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 He7d7a8de2019-01-20 21:31:12 -0800623
624void LogA2dpAudioUnderrunEvent(const RawAddress& address,
625 uint64_t encoding_interval_millis,
626 int num_missing_pcm_bytes) {
627 std::string obfuscated_id;
Chen Chen721030a2020-03-09 16:19:24 -0700628 int metric_id = 0;
Jack He7d7a8de2019-01-20 21:31:12 -0800629 if (!address.IsEmpty()) {
630 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700631 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack He7d7a8de2019-01-20 21:31:12 -0800632 }
633 // nullptr and size 0 represent missing value for obfuscated_id
Muhammad Qureshi60818422021-09-22 16:00:14 -0700634 BytesField bytes_field(address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
635 address.IsEmpty() ? 0 : obfuscated_id.size());
Jack He7d7a8de2019-01-20 21:31:12 -0800636 int64_t encoding_interval_nanos = encoding_interval_millis * 1000000;
Muhammad Qureshi60818422021-09-22 16:00:14 -0700637 int ret =
638 stats_write(BLUETOOTH_A2DP_AUDIO_UNDERRUN_REPORTED, bytes_field,
639 encoding_interval_nanos, num_missing_pcm_bytes, metric_id);
Jack He7d7a8de2019-01-20 21:31:12 -0800640 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
648void 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 Chen721030a2020-03-09 16:19:24 -0700654 int metric_id = 0;
Jack He7d7a8de2019-01-20 21:31:12 -0800655 if (!address.IsEmpty()) {
656 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700657 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack He7d7a8de2019-01-20 21:31:12 -0800658 }
659 // nullptr and size 0 represent missing value for obfuscated_id
Muhammad Qureshi60818422021-09-22 16:00:14 -0700660 BytesField bytes_field(address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
661 address.IsEmpty() ? 0 : obfuscated_id.size());
Jack He7d7a8de2019-01-20 21:31:12 -0800662 int64_t encoding_interval_nanos = encoding_interval_millis * 1000000;
Muhammad Qureshi60818422021-09-22 16:00:14 -0700663 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 He7d7a8de2019-01-20 21:31:12 -0800667 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 Wue9f5c352021-03-24 20:15:43 +0800677void 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 Qureshi60818422021-09-22 16:00:14 -0700686 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 Wue9f5c352021-03-24 20:15:43 +0800690 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 He7d7a8de2019-01-20 21:31:12 -0800698void LogReadRssiResult(const RawAddress& address, uint16_t handle,
699 uint32_t cmd_status, int8_t rssi) {
700 std::string obfuscated_id;
Chen Chen721030a2020-03-09 16:19:24 -0700701 int metric_id = 0;
Jack He7d7a8de2019-01-20 21:31:12 -0800702 if (!address.IsEmpty()) {
703 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700704 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack He7d7a8de2019-01-20 21:31:12 -0800705 }
706 // nullptr and size 0 represent missing value for obfuscated_id
Muhammad Qureshi60818422021-09-22 16:00:14 -0700707 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 He7d7a8de2019-01-20 21:31:12 -0800711 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
718void LogReadFailedContactCounterResult(const RawAddress& address,
719 uint16_t handle, uint32_t cmd_status,
720 int32_t failed_contact_counter) {
721 std::string obfuscated_id;
Chen Chen721030a2020-03-09 16:19:24 -0700722 int metric_id = 0;
Jack He7d7a8de2019-01-20 21:31:12 -0800723 if (!address.IsEmpty()) {
724 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700725 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack He7d7a8de2019-01-20 21:31:12 -0800726 }
727 // nullptr and size 0 represent missing value for obfuscated_id
Muhammad Qureshi60818422021-09-22 16:00:14 -0700728 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 He7d7a8de2019-01-20 21:31:12 -0800733 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
741void LogReadTxPowerLevelResult(const RawAddress& address, uint16_t handle,
742 uint32_t cmd_status,
743 int32_t transmit_power_level) {
744 std::string obfuscated_id;
Chen Chen721030a2020-03-09 16:19:24 -0700745 int metric_id = 0;
Jack He7d7a8de2019-01-20 21:31:12 -0800746 if (!address.IsEmpty()) {
747 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700748 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack He7d7a8de2019-01-20 21:31:12 -0800749 }
750 // nullptr and size 0 represent missing value for obfuscated_id
Muhammad Qureshi60818422021-09-22 16:00:14 -0700751 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 He7d7a8de2019-01-20 21:31:12 -0800755 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 Heb75275d2019-01-31 15:17:08 -0800763void 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 Chen721030a2020-03-09 16:19:24 -0700767 int metric_id = 0;
Jack Heb75275d2019-01-31 15:17:08 -0800768 if (!address.IsEmpty()) {
769 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700770 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack Heb75275d2019-01-31 15:17:08 -0800771 }
772 // nullptr and size 0 represent missing value for obfuscated_id
Muhammad Qureshi60818422021-09-22 16:00:14 -0700773 BytesField obfuscated_id_field(
Jack Heb75275d2019-01-31 15:17:08 -0800774 address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
775 address.IsEmpty() ? 0 : obfuscated_id.size());
Muhammad Qureshi60818422021-09-22 16:00:14 -0700776 int ret =
777 stats_write(BLUETOOTH_SMP_PAIRING_EVENT_REPORTED, obfuscated_id_field,
778 smp_cmd, direction, smp_fail_reason, metric_id);
Jack Heb75275d2019-01-31 15:17:08 -0800779 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 He40553162019-03-20 04:04:36 -0700787void 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 He0d888e52019-01-31 17:44:46 -0800789 std::string obfuscated_id;
Chen Chen721030a2020-03-09 16:19:24 -0700790 int metric_id = 0;
Jack He0d888e52019-01-31 17:44:46 -0800791 if (!address.IsEmpty()) {
792 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700793 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack He0d888e52019-01-31 17:44:46 -0800794 }
795 // nullptr and size 0 represent missing value for obfuscated_id
Muhammad Qureshi60818422021-09-22 16:00:14 -0700796 BytesField obfuscated_id_field(
Jack He0d888e52019-01-31 17:44:46 -0800797 address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
798 address.IsEmpty() ? 0 : obfuscated_id.size());
Muhammad Qureshi60818422021-09-22 16:00:14 -0700799 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 He0d888e52019-01-31 17:44:46 -0800802 if (ret < 0) {
Jack He40553162019-03-20 04:04:36 -0700803 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 He0d888e52019-01-31 17:44:46 -0800806 }
807}
808
Jack He3c162a82019-02-01 18:30:21 -0800809void 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 Chen721030a2020-03-09 16:19:24 -0700813 int metric_id = 0;
Jack He3c162a82019-02-01 18:30:21 -0800814 if (!address.IsEmpty()) {
815 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700816 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack He3c162a82019-02-01 18:30:21 -0800817 }
818 // nullptr and size 0 represent missing value for obfuscated_id
Muhammad Qureshi60818422021-09-22 16:00:14 -0700819 BytesField obfuscated_id_field(
Jack He3c162a82019-02-01 18:30:21 -0800820 address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
821 address.IsEmpty() ? 0 : obfuscated_id.size());
Muhammad Qureshi60818422021-09-22 16:00:14 -0700822 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 He3c162a82019-02-01 18:30:21 -0800826 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 He7a98b402019-02-06 20:24:24 -0800833void 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 Chen721030a2020-03-09 16:19:24 -0700839 int metric_id = 0;
Jack He7a98b402019-02-06 20:24:24 -0800840 if (!address.IsEmpty()) {
841 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700842 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack He7a98b402019-02-06 20:24:24 -0800843 }
844 // nullptr and size 0 represent missing value for obfuscated_id
Muhammad Qureshi60818422021-09-22 16:00:14 -0700845 BytesField obfuscated_id_field(
Jack He7a98b402019-02-06 20:24:24 -0800846 address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
847 address.IsEmpty() ? 0 : obfuscated_id.size());
Muhammad Qureshi60818422021-09-22 16:00:14 -0700848 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 He7a98b402019-02-06 20:24:24 -0800852 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 He9e026202019-02-06 21:53:41 -0800861void LogManufacturerInfo(const RawAddress& address,
Chen Chenc08fc882022-06-02 15:00:20 -0700862 android::bluetooth::AddressTypeEnum address_type,
Jack He9e026202019-02-06 21:53:41 -0800863 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 Chen721030a2020-03-09 16:19:24 -0700870 int metric_id = 0;
Jack He9e026202019-02-06 21:53:41 -0800871 if (!address.IsEmpty()) {
872 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700873 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack He9e026202019-02-06 21:53:41 -0800874 }
875 // nullptr and size 0 represent missing value for obfuscated_id
Muhammad Qureshi60818422021-09-22 16:00:14 -0700876 BytesField obfuscated_id_field(
Jack He9e026202019-02-06 21:53:41 -0800877 address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
878 address.IsEmpty() ? 0 : obfuscated_id.size());
Chen Chenc08fc882022-06-02 15:00:20 -0700879 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 He9e026202019-02-06 21:53:41 -0800884 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 Chenc08fc882022-06-02 15:00:20 -0700889 << ", 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 He9e026202019-02-06 21:53:41 -0800893 << ret;
894 }
895}
896
weichinwengc1a2fc92020-12-15 14:56:19 +0800897void LogBluetoothHalCrashReason(const RawAddress& address, uint32_t error_code,
898 uint32_t vendor_error_code) {
899 std::string obfuscated_id;
weichinwengc1a2fc92020-12-15 14:56:19 +0800900 if (!address.IsEmpty()) {
901 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
weichinwengc1a2fc92020-12-15 14:56:19 +0800902 }
903 // nullptr and size 0 represent missing value for obfuscated_id
Muhammad Qureshi60818422021-09-22 16:00:14 -0700904 BytesField obfuscated_id_field(
weichinwengc1a2fc92020-12-15 14:56:19 +0800905 address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
906 address.IsEmpty() ? 0 : obfuscated_id.size());
Muhammad Qureshi60818422021-09-22 16:00:14 -0700907 int ret = stats_write(BLUETOOTH_HAL_CRASH_REASON_REPORTED, 0,
908 obfuscated_id_field, error_code, vendor_error_code);
weichinwengc1a2fc92020-12-15 14:56:19 +0800909 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 Wuc033a162022-05-05 01:29:04 -0700916void 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 He42823982018-08-15 12:38:37 -0700965} // namespace common
966
967} // namespace bluetooth