blob: 10eedc8e13c1c4e5c4c32acee1263feabe1d84f9 [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
Jack He777228a2016-12-08 19:29:00 -080019#include <unistd.h>
20#include <algorithm>
Jack He7ebb85a2018-04-02 13:04:58 -070021#include <array>
Jack He777228a2016-12-08 19:29:00 -080022#include <cerrno>
23#include <chrono>
24#include <cstdint>
25#include <cstring>
26#include <memory>
27#include <mutex>
Marie Janssena84a4ee2016-01-15 16:14:14 -080028
Jack He777228a2016-12-08 19:29:00 -080029#include <base/base64.h>
30#include <base/logging.h>
Jack Heeeb9f352018-06-08 20:13:23 -070031#include <include/hardware/bt_av.h>
Jack He0d4807d2018-12-20 15:46:17 -080032#include <statslog.h>
Marie Janssena84a4ee2016-01-15 16:14:14 -080033
Jack He9a30a9f2018-03-03 00:03:25 -080034#include "bluetooth/metrics/bluetooth.pb.h"
Marie Janssena84a4ee2016-01-15 16:14:14 -080035#include "osi/include/osi.h"
Jack He777228a2016-12-08 19:29:00 -080036#include "stack/include/btm_api_types.h"
Marie Janssena84a4ee2016-01-15 16:14:14 -080037
Jack He0d4807d2018-12-20 15:46:17 -080038#include "address_obfuscator.h"
Jack He42823982018-08-15 12:38:37 -070039#include "leaky_bonded_queue.h"
Chen Chen721030a2020-03-09 16:19:24 -070040#include "metric_id_allocator.h"
Jack He42823982018-08-15 12:38:37 -070041#include "metrics.h"
42#include "time_util.h"
Jack He777228a2016-12-08 19:29:00 -080043
Jack He42823982018-08-15 12:38:37 -070044namespace bluetooth {
45
46namespace common {
Marie Janssena84a4ee2016-01-15 16:14:14 -080047
Jack He9a30a9f2018-03-03 00:03:25 -080048using bluetooth::metrics::BluetoothMetricsProto::A2DPSession;
Jack Heeeb9f352018-06-08 20:13:23 -070049using bluetooth::metrics::BluetoothMetricsProto::A2dpSourceCodec;
Jack He9a30a9f2018-03-03 00:03:25 -080050using bluetooth::metrics::BluetoothMetricsProto::BluetoothLog;
51using bluetooth::metrics::BluetoothMetricsProto::BluetoothSession;
52using bluetooth::metrics::BluetoothMetricsProto::
53 BluetoothSession_ConnectionTechnologyType;
54using bluetooth::metrics::BluetoothMetricsProto::
55 BluetoothSession_DisconnectReasonType;
56using bluetooth::metrics::BluetoothMetricsProto::DeviceInfo;
57using bluetooth::metrics::BluetoothMetricsProto::DeviceInfo_DeviceType;
Jack He42823982018-08-15 12:38:37 -070058using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileConnectionStats;
Jack He7ebb85a2018-04-02 13:04:58 -070059using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType;
Jack He7ebb85a2018-04-02 13:04:58 -070060using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType_ARRAYSIZE;
61using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType_IsValid;
Jack He42823982018-08-15 12:38:37 -070062using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType_MAX;
63using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType_MIN;
64using bluetooth::metrics::BluetoothMetricsProto::PairEvent;
65using bluetooth::metrics::BluetoothMetricsProto::ScanEvent;
66using bluetooth::metrics::BluetoothMetricsProto::ScanEvent_ScanEventType;
67using bluetooth::metrics::BluetoothMetricsProto::ScanEvent_ScanTechnologyType;
68using bluetooth::metrics::BluetoothMetricsProto::WakeEvent;
69using bluetooth::metrics::BluetoothMetricsProto::WakeEvent_WakeEventType;
Jack He777228a2016-12-08 19:29:00 -080070
71static float combine_averages(float avg_a, int64_t ct_a, float avg_b,
72 int64_t ct_b) {
73 if (ct_a > 0 && ct_b > 0) {
74 return (avg_a * ct_a + avg_b * ct_b) / (ct_a + ct_b);
75 } else if (ct_b > 0) {
76 return avg_b;
77 } else {
78 return avg_a;
Marie Janssena84a4ee2016-01-15 16:14:14 -080079 }
80}
81
Jack He777228a2016-12-08 19:29:00 -080082static int32_t combine_averages(int32_t avg_a, int64_t ct_a, int32_t avg_b,
83 int64_t ct_b) {
84 if (ct_a > 0 && ct_b > 0) {
85 return (avg_a * ct_a + avg_b * ct_b) / (ct_a + ct_b);
86 } else if (ct_b > 0) {
87 return avg_b;
88 } else {
89 return avg_a;
90 }
91}
Marie Janssena84a4ee2016-01-15 16:14:14 -080092
Jack He777228a2016-12-08 19:29:00 -080093void A2dpSessionMetrics::Update(const A2dpSessionMetrics& metrics) {
Jack He723bef82017-01-20 11:17:44 -080094 if (metrics.audio_duration_ms >= 0) {
Jack He777228a2016-12-08 19:29:00 -080095 audio_duration_ms = std::max(static_cast<int64_t>(0), audio_duration_ms);
96 audio_duration_ms += metrics.audio_duration_ms;
97 }
Jack He723bef82017-01-20 11:17:44 -080098 if (metrics.media_timer_min_ms >= 0) {
Jack He777228a2016-12-08 19:29:00 -080099 if (media_timer_min_ms < 0) {
100 media_timer_min_ms = metrics.media_timer_min_ms;
101 } else {
102 media_timer_min_ms =
103 std::min(media_timer_min_ms, metrics.media_timer_min_ms);
104 }
105 }
Jack He723bef82017-01-20 11:17:44 -0800106 if (metrics.media_timer_max_ms >= 0) {
Jack He777228a2016-12-08 19:29:00 -0800107 media_timer_max_ms =
108 std::max(media_timer_max_ms, metrics.media_timer_max_ms);
109 }
Jack He723bef82017-01-20 11:17:44 -0800110 if (metrics.media_timer_avg_ms >= 0 && metrics.total_scheduling_count >= 0) {
Jack He777228a2016-12-08 19:29:00 -0800111 if (media_timer_avg_ms < 0 || total_scheduling_count < 0) {
112 media_timer_avg_ms = metrics.media_timer_avg_ms;
113 total_scheduling_count = metrics.total_scheduling_count;
114 } else {
115 media_timer_avg_ms = combine_averages(
116 media_timer_avg_ms, total_scheduling_count,
117 metrics.media_timer_avg_ms, metrics.total_scheduling_count);
118 total_scheduling_count += metrics.total_scheduling_count;
119 }
120 }
Jack He723bef82017-01-20 11:17:44 -0800121 if (metrics.buffer_overruns_max_count >= 0) {
Jack He777228a2016-12-08 19:29:00 -0800122 buffer_overruns_max_count =
123 std::max(buffer_overruns_max_count, metrics.buffer_overruns_max_count);
124 }
Jack He723bef82017-01-20 11:17:44 -0800125 if (metrics.buffer_overruns_total >= 0) {
Jack He777228a2016-12-08 19:29:00 -0800126 buffer_overruns_total =
127 std::max(static_cast<int32_t>(0), buffer_overruns_total);
128 buffer_overruns_total += metrics.buffer_overruns_total;
129 }
Jack He723bef82017-01-20 11:17:44 -0800130 if (metrics.buffer_underruns_average >= 0 &&
131 metrics.buffer_underruns_count >= 0) {
Jack He777228a2016-12-08 19:29:00 -0800132 if (buffer_underruns_average < 0 || buffer_underruns_count < 0) {
133 buffer_underruns_average = metrics.buffer_underruns_average;
134 buffer_underruns_count = metrics.buffer_underruns_count;
135 } else {
136 buffer_underruns_average = combine_averages(
Jack He723bef82017-01-20 11:17:44 -0800137 buffer_underruns_average, buffer_underruns_count,
138 metrics.buffer_underruns_average, metrics.buffer_underruns_count);
Jack He777228a2016-12-08 19:29:00 -0800139 buffer_underruns_count += metrics.buffer_underruns_count;
140 }
141 }
Jack Heeeb9f352018-06-08 20:13:23 -0700142 if (codec_index < 0) {
143 codec_index = metrics.codec_index;
144 }
145 if (!is_a2dp_offload) {
146 is_a2dp_offload = metrics.is_a2dp_offload;
147 }
Jack He777228a2016-12-08 19:29:00 -0800148}
Marie Janssena84a4ee2016-01-15 16:14:14 -0800149
Jack He777228a2016-12-08 19:29:00 -0800150bool A2dpSessionMetrics::operator==(const A2dpSessionMetrics& rhs) const {
151 return audio_duration_ms == rhs.audio_duration_ms &&
152 media_timer_min_ms == rhs.media_timer_min_ms &&
153 media_timer_max_ms == rhs.media_timer_max_ms &&
154 media_timer_avg_ms == rhs.media_timer_avg_ms &&
155 total_scheduling_count == rhs.total_scheduling_count &&
156 buffer_overruns_max_count == rhs.buffer_overruns_max_count &&
157 buffer_overruns_total == rhs.buffer_overruns_total &&
158 buffer_underruns_average == rhs.buffer_underruns_average &&
Jack Heeeb9f352018-06-08 20:13:23 -0700159 buffer_underruns_count == rhs.buffer_underruns_count &&
160 codec_index == rhs.codec_index &&
161 is_a2dp_offload == rhs.is_a2dp_offload;
Jack He777228a2016-12-08 19:29:00 -0800162}
163
164static DeviceInfo_DeviceType get_device_type(device_type_t type) {
165 switch (type) {
166 case DEVICE_TYPE_BREDR:
167 return DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR;
168 case DEVICE_TYPE_LE:
169 return DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_LE;
170 case DEVICE_TYPE_DUMO:
171 return DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_DUMO;
172 case DEVICE_TYPE_UNKNOWN:
173 default:
174 return DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_UNKNOWN;
175 }
176}
177
178static BluetoothSession_ConnectionTechnologyType get_connection_tech_type(
179 connection_tech_t type) {
180 switch (type) {
181 case CONNECTION_TECHNOLOGY_TYPE_LE:
182 return BluetoothSession_ConnectionTechnologyType::
183 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_LE;
184 case CONNECTION_TECHNOLOGY_TYPE_BREDR:
185 return BluetoothSession_ConnectionTechnologyType::
186 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR;
187 case CONNECTION_TECHNOLOGY_TYPE_UNKNOWN:
188 default:
189 return BluetoothSession_ConnectionTechnologyType::
190 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_UNKNOWN;
191 }
192}
193
194static ScanEvent_ScanTechnologyType get_scan_tech_type(scan_tech_t type) {
195 switch (type) {
196 case SCAN_TECH_TYPE_LE:
197 return ScanEvent_ScanTechnologyType::
198 ScanEvent_ScanTechnologyType_SCAN_TECH_TYPE_LE;
199 case SCAN_TECH_TYPE_BREDR:
200 return ScanEvent_ScanTechnologyType::
201 ScanEvent_ScanTechnologyType_SCAN_TECH_TYPE_BREDR;
202 case SCAN_TECH_TYPE_BOTH:
203 return ScanEvent_ScanTechnologyType::
204 ScanEvent_ScanTechnologyType_SCAN_TECH_TYPE_BOTH;
205 case SCAN_TYPE_UNKNOWN:
206 default:
207 return ScanEvent_ScanTechnologyType::
208 ScanEvent_ScanTechnologyType_SCAN_TYPE_UNKNOWN;
209 }
210}
211
212static WakeEvent_WakeEventType get_wake_event_type(wake_event_type_t type) {
213 switch (type) {
214 case WAKE_EVENT_ACQUIRED:
215 return WakeEvent_WakeEventType::WakeEvent_WakeEventType_ACQUIRED;
216 case WAKE_EVENT_RELEASED:
217 return WakeEvent_WakeEventType::WakeEvent_WakeEventType_RELEASED;
218 case WAKE_EVENT_UNKNOWN:
219 default:
220 return WakeEvent_WakeEventType::WakeEvent_WakeEventType_UNKNOWN;
221 }
222}
223
Jack He9bebcc02017-01-17 15:41:30 -0800224static BluetoothSession_DisconnectReasonType get_disconnect_reason_type(
225 disconnect_reason_t type) {
226 switch (type) {
227 case DISCONNECT_REASON_METRICS_DUMP:
228 return BluetoothSession_DisconnectReasonType::
229 BluetoothSession_DisconnectReasonType_METRICS_DUMP;
230 case DISCONNECT_REASON_NEXT_START_WITHOUT_END_PREVIOUS:
231 return BluetoothSession_DisconnectReasonType::
232 BluetoothSession_DisconnectReasonType_NEXT_START_WITHOUT_END_PREVIOUS;
233 case DISCONNECT_REASON_UNKNOWN:
234 default:
235 return BluetoothSession_DisconnectReasonType::
236 BluetoothSession_DisconnectReasonType_UNKNOWN;
237 }
238}
239
Jack Heeeb9f352018-06-08 20:13:23 -0700240static A2dpSourceCodec get_a2dp_source_codec(int64_t codec_index) {
241 switch (codec_index) {
242 case BTAV_A2DP_CODEC_INDEX_SOURCE_SBC:
243 return A2dpSourceCodec::A2DP_SOURCE_CODEC_SBC;
244 case BTAV_A2DP_CODEC_INDEX_SOURCE_AAC:
245 return A2dpSourceCodec::A2DP_SOURCE_CODEC_AAC;
246 case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX:
247 return A2dpSourceCodec::A2DP_SOURCE_CODEC_APTX;
248 case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD:
249 return A2dpSourceCodec::A2DP_SOURCE_CODEC_APTX_HD;
250 case BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC:
251 return A2dpSourceCodec::A2DP_SOURCE_CODEC_LDAC;
252 default:
253 return A2dpSourceCodec::A2DP_SOURCE_CODEC_UNKNOWN;
254 }
255}
256
Jack He777228a2016-12-08 19:29:00 -0800257struct BluetoothMetricsLogger::impl {
258 impl(size_t max_bluetooth_session, size_t max_pair_event,
259 size_t max_wake_event, size_t max_scan_event)
260 : bt_session_queue_(
261 new LeakyBondedQueue<BluetoothSession>(max_bluetooth_session)),
262 pair_event_queue_(new LeakyBondedQueue<PairEvent>(max_pair_event)),
263 wake_event_queue_(new LeakyBondedQueue<WakeEvent>(max_wake_event)),
264 scan_event_queue_(new LeakyBondedQueue<ScanEvent>(max_scan_event)) {
265 bluetooth_log_ = BluetoothLog::default_instance().New();
Jack He7ebb85a2018-04-02 13:04:58 -0700266 headset_profile_connection_counts_.fill(0);
Jack He777228a2016-12-08 19:29:00 -0800267 bluetooth_session_ = nullptr;
268 bluetooth_session_start_time_ms_ = 0;
269 a2dp_session_metrics_ = A2dpSessionMetrics();
270 }
271
272 /* Bluetooth log lock protected */
273 BluetoothLog* bluetooth_log_;
Jack He7ebb85a2018-04-02 13:04:58 -0700274 std::array<int, HeadsetProfileType_ARRAYSIZE>
275 headset_profile_connection_counts_;
Jack He777228a2016-12-08 19:29:00 -0800276 std::recursive_mutex bluetooth_log_lock_;
277 /* End Bluetooth log lock protected */
278 /* Bluetooth session lock protected */
279 BluetoothSession* bluetooth_session_;
280 uint64_t bluetooth_session_start_time_ms_;
281 A2dpSessionMetrics a2dp_session_metrics_;
282 std::recursive_mutex bluetooth_session_lock_;
283 /* End bluetooth session lock protected */
284 std::unique_ptr<LeakyBondedQueue<BluetoothSession>> bt_session_queue_;
285 std::unique_ptr<LeakyBondedQueue<PairEvent>> pair_event_queue_;
286 std::unique_ptr<LeakyBondedQueue<WakeEvent>> wake_event_queue_;
287 std::unique_ptr<LeakyBondedQueue<ScanEvent>> scan_event_queue_;
288};
289
290BluetoothMetricsLogger::BluetoothMetricsLogger()
Jack He9bebcc02017-01-17 15:41:30 -0800291 : pimpl_(new impl(kMaxNumBluetoothSession, kMaxNumPairEvent,
292 kMaxNumWakeEvent, kMaxNumScanEvent)) {}
Jack He777228a2016-12-08 19:29:00 -0800293
294void BluetoothMetricsLogger::LogPairEvent(uint32_t disconnect_reason,
295 uint64_t timestamp_ms,
296 uint32_t device_class,
297 device_type_t device_type) {
298 PairEvent* event = new PairEvent();
Myles Watson914a9dc2016-10-19 13:15:34 -0700299 DeviceInfo* info = event->mutable_device_paired_with();
Marie Janssena84a4ee2016-01-15 16:14:14 -0800300 info->set_device_class(device_class);
Jack He777228a2016-12-08 19:29:00 -0800301 info->set_device_type(get_device_type(device_type));
Marie Janssena84a4ee2016-01-15 16:14:14 -0800302 event->set_disconnect_reason(disconnect_reason);
Marie Janssena84a4ee2016-01-15 16:14:14 -0800303 event->set_event_time_millis(timestamp_ms);
Jack He777228a2016-12-08 19:29:00 -0800304 pimpl_->pair_event_queue_->Enqueue(event);
Jack He9bebcc02017-01-17 15:41:30 -0800305 {
306 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_);
307 pimpl_->bluetooth_log_->set_num_pair_event(
308 pimpl_->bluetooth_log_->num_pair_event() + 1);
309 }
Marie Janssena84a4ee2016-01-15 16:14:14 -0800310}
311
Jack He777228a2016-12-08 19:29:00 -0800312void BluetoothMetricsLogger::LogWakeEvent(wake_event_type_t type,
313 const std::string& requestor,
314 const std::string& name,
315 uint64_t timestamp_ms) {
316 WakeEvent* event = new WakeEvent();
317 event->set_wake_event_type(get_wake_event_type(type));
318 event->set_requestor(requestor);
319 event->set_name(name);
Pavlin Radoslavov9aa6f122016-02-17 15:42:38 -0800320 event->set_event_time_millis(timestamp_ms);
Jack He777228a2016-12-08 19:29:00 -0800321 pimpl_->wake_event_queue_->Enqueue(event);
Jack He9bebcc02017-01-17 15:41:30 -0800322 {
323 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_);
324 pimpl_->bluetooth_log_->set_num_wake_event(
325 pimpl_->bluetooth_log_->num_wake_event() + 1);
326 }
Marie Janssena84a4ee2016-01-15 16:14:14 -0800327}
328
Jack He777228a2016-12-08 19:29:00 -0800329void BluetoothMetricsLogger::LogScanEvent(bool start,
330 const std::string& initator,
331 scan_tech_t type, uint32_t results,
332 uint64_t timestamp_ms) {
333 ScanEvent* event = new ScanEvent();
334 if (start) {
Marie Janssena84a4ee2016-01-15 16:14:14 -0800335 event->set_scan_event_type(ScanEvent::SCAN_EVENT_START);
Jack He777228a2016-12-08 19:29:00 -0800336 } else {
Marie Janssena84a4ee2016-01-15 16:14:14 -0800337 event->set_scan_event_type(ScanEvent::SCAN_EVENT_STOP);
Jack He777228a2016-12-08 19:29:00 -0800338 }
339 event->set_initiator(initator);
340 event->set_scan_technology_type(get_scan_tech_type(type));
Marie Janssena84a4ee2016-01-15 16:14:14 -0800341 event->set_number_results(results);
Marie Janssena84a4ee2016-01-15 16:14:14 -0800342 event->set_event_time_millis(timestamp_ms);
Jack He777228a2016-12-08 19:29:00 -0800343 pimpl_->scan_event_queue_->Enqueue(event);
Jack He9bebcc02017-01-17 15:41:30 -0800344 {
345 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_);
346 pimpl_->bluetooth_log_->set_num_scan_event(
347 pimpl_->bluetooth_log_->num_scan_event() + 1);
348 }
Marie Janssena84a4ee2016-01-15 16:14:14 -0800349}
350
Jack He777228a2016-12-08 19:29:00 -0800351void BluetoothMetricsLogger::LogBluetoothSessionStart(
352 connection_tech_t connection_tech_type, uint64_t timestamp_ms) {
353 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_);
354 if (pimpl_->bluetooth_session_ != nullptr) {
Jack He9bebcc02017-01-17 15:41:30 -0800355 LogBluetoothSessionEnd(DISCONNECT_REASON_NEXT_START_WITHOUT_END_PREVIOUS,
Jack He777228a2016-12-08 19:29:00 -0800356 0);
357 }
358 if (timestamp_ms == 0) {
Jack He42823982018-08-15 12:38:37 -0700359 timestamp_ms = bluetooth::common::time_get_os_boottime_ms();
Jack He777228a2016-12-08 19:29:00 -0800360 }
361 pimpl_->bluetooth_session_start_time_ms_ = timestamp_ms;
362 pimpl_->bluetooth_session_ = new BluetoothSession();
363 pimpl_->bluetooth_session_->set_connection_technology_type(
364 get_connection_tech_type(connection_tech_type));
365}
Pavlin Radoslavov9aa6f122016-02-17 15:42:38 -0800366
Jack He777228a2016-12-08 19:29:00 -0800367void BluetoothMetricsLogger::LogBluetoothSessionEnd(
Jack He9bebcc02017-01-17 15:41:30 -0800368 disconnect_reason_t disconnect_reason, uint64_t timestamp_ms) {
Jack He777228a2016-12-08 19:29:00 -0800369 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_);
370 if (pimpl_->bluetooth_session_ == nullptr) {
371 return;
372 }
373 if (timestamp_ms == 0) {
Jack He42823982018-08-15 12:38:37 -0700374 timestamp_ms = bluetooth::common::time_get_os_boottime_ms();
Jack He777228a2016-12-08 19:29:00 -0800375 }
376 int64_t session_duration_sec =
377 (timestamp_ms - pimpl_->bluetooth_session_start_time_ms_) / 1000;
378 pimpl_->bluetooth_session_->set_session_duration_sec(session_duration_sec);
Jack He9bebcc02017-01-17 15:41:30 -0800379 pimpl_->bluetooth_session_->set_disconnect_reason_type(
380 get_disconnect_reason_type(disconnect_reason));
Jack He777228a2016-12-08 19:29:00 -0800381 pimpl_->bt_session_queue_->Enqueue(pimpl_->bluetooth_session_);
382 pimpl_->bluetooth_session_ = nullptr;
Jack Heeeb9f352018-06-08 20:13:23 -0700383 pimpl_->a2dp_session_metrics_ = A2dpSessionMetrics();
Jack He9bebcc02017-01-17 15:41:30 -0800384 {
385 std::lock_guard<std::recursive_mutex> log_lock(pimpl_->bluetooth_log_lock_);
386 pimpl_->bluetooth_log_->set_num_bluetooth_session(
387 pimpl_->bluetooth_log_->num_bluetooth_session() + 1);
388 }
Jack He777228a2016-12-08 19:29:00 -0800389}
Pavlin Radoslavov9aa6f122016-02-17 15:42:38 -0800390
Jack He777228a2016-12-08 19:29:00 -0800391void BluetoothMetricsLogger::LogBluetoothSessionDeviceInfo(
392 uint32_t device_class, device_type_t device_type) {
393 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_);
394 if (pimpl_->bluetooth_session_ == nullptr) {
395 LogBluetoothSessionStart(CONNECTION_TECHNOLOGY_TYPE_UNKNOWN, 0);
396 }
397 DeviceInfo* info = pimpl_->bluetooth_session_->mutable_device_connected_to();
Pavlin Radoslavov9aa6f122016-02-17 15:42:38 -0800398 info->set_device_class(device_class);
399 info->set_device_type(DeviceInfo::DEVICE_TYPE_BREDR);
Pavlin Radoslavov9aa6f122016-02-17 15:42:38 -0800400}
401
Jack He777228a2016-12-08 19:29:00 -0800402void BluetoothMetricsLogger::LogA2dpSession(
403 const A2dpSessionMetrics& a2dp_session_metrics) {
404 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_);
405 if (pimpl_->bluetooth_session_ == nullptr) {
406 // When no bluetooth session exist, create one on system's behalf
407 // Set connection type: for A2DP it is always BR/EDR
408 LogBluetoothSessionStart(CONNECTION_TECHNOLOGY_TYPE_BREDR, 0);
409 LogBluetoothSessionDeviceInfo(BTM_COD_MAJOR_AUDIO, DEVICE_TYPE_BREDR);
410 }
411 // Accumulate metrics
412 pimpl_->a2dp_session_metrics_.Update(a2dp_session_metrics);
413 // Get or allocate new A2DP session object
414 A2DPSession* a2dp_session =
415 pimpl_->bluetooth_session_->mutable_a2dp_session();
416 a2dp_session->set_audio_duration_millis(
417 pimpl_->a2dp_session_metrics_.audio_duration_ms);
418 a2dp_session->set_media_timer_min_millis(
419 pimpl_->a2dp_session_metrics_.media_timer_min_ms);
420 a2dp_session->set_media_timer_max_millis(
421 pimpl_->a2dp_session_metrics_.media_timer_max_ms);
422 a2dp_session->set_media_timer_avg_millis(
423 pimpl_->a2dp_session_metrics_.media_timer_avg_ms);
424 a2dp_session->set_buffer_overruns_max_count(
425 pimpl_->a2dp_session_metrics_.buffer_overruns_max_count);
426 a2dp_session->set_buffer_overruns_total(
427 pimpl_->a2dp_session_metrics_.buffer_overruns_total);
428 a2dp_session->set_buffer_underruns_average(
429 pimpl_->a2dp_session_metrics_.buffer_underruns_average);
430 a2dp_session->set_buffer_underruns_count(
431 pimpl_->a2dp_session_metrics_.buffer_underruns_count);
Jack Heeeb9f352018-06-08 20:13:23 -0700432 a2dp_session->set_source_codec(
433 get_a2dp_source_codec(pimpl_->a2dp_session_metrics_.codec_index));
434 a2dp_session->set_is_a2dp_offload(
435 pimpl_->a2dp_session_metrics_.is_a2dp_offload);
Jack He777228a2016-12-08 19:29:00 -0800436}
Marie Janssena84a4ee2016-01-15 16:14:14 -0800437
Jack He7ebb85a2018-04-02 13:04:58 -0700438void BluetoothMetricsLogger::LogHeadsetProfileRfcConnection(
439 tBTA_SERVICE_ID service_id) {
440 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_);
441 switch (service_id) {
442 case BTA_HSP_SERVICE_ID:
443 pimpl_->headset_profile_connection_counts_[HeadsetProfileType::HSP]++;
444 break;
445 case BTA_HFP_SERVICE_ID:
446 pimpl_->headset_profile_connection_counts_[HeadsetProfileType::HFP]++;
447 break;
448 default:
449 pimpl_->headset_profile_connection_counts_
450 [HeadsetProfileType::HEADSET_PROFILE_UNKNOWN]++;
451 break;
452 }
453 return;
454}
455
456void BluetoothMetricsLogger::WriteString(std::string* serialized) {
Jack He777228a2016-12-08 19:29:00 -0800457 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_);
Jack He42823982018-08-15 12:38:37 -0700458 LOG(INFO) << __func__ << ": building metrics";
Jack He777228a2016-12-08 19:29:00 -0800459 Build();
Jack He42823982018-08-15 12:38:37 -0700460 LOG(INFO) << __func__ << ": serializing metrics";
Jack He777228a2016-12-08 19:29:00 -0800461 if (!pimpl_->bluetooth_log_->SerializeToString(serialized)) {
Jack He42823982018-08-15 12:38:37 -0700462 LOG(ERROR) << __func__ << ": error serializing metrics";
Marie Janssena84a4ee2016-01-15 16:14:14 -0800463 }
Jack He7ebb85a2018-04-02 13:04:58 -0700464 // Always clean up log objects
465 pimpl_->bluetooth_log_->Clear();
Jack He777228a2016-12-08 19:29:00 -0800466}
Ajay Panickerfe357dc2016-02-18 12:24:10 -0800467
Jack He7ebb85a2018-04-02 13:04:58 -0700468void BluetoothMetricsLogger::WriteBase64String(std::string* serialized) {
469 this->WriteString(serialized);
Jack He777228a2016-12-08 19:29:00 -0800470 base::Base64Encode(*serialized, serialized);
471}
472
Jack He7ebb85a2018-04-02 13:04:58 -0700473void BluetoothMetricsLogger::WriteBase64(int fd) {
Ajay Panicker4336ba52016-02-17 18:18:00 -0800474 std::string protoBase64;
Jack He7ebb85a2018-04-02 13:04:58 -0700475 this->WriteBase64String(&protoBase64);
Pavlin Radoslavovc608acf2016-05-12 11:36:44 -0700476 ssize_t ret;
477 OSI_NO_INTR(ret = write(fd, protoBase64.c_str(), protoBase64.size()));
478 if (ret == -1) {
Jack He42823982018-08-15 12:38:37 -0700479 LOG(ERROR) << __func__
480 << ": error writing to dumpsys fd: " << strerror(errno) << " ("
481 << std::to_string(errno) << ")";
Marie Janssena84a4ee2016-01-15 16:14:14 -0800482 }
Marie Janssena84a4ee2016-01-15 16:14:14 -0800483}
Jack He777228a2016-12-08 19:29:00 -0800484
485void BluetoothMetricsLogger::CutoffSession() {
486 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_);
487 if (pimpl_->bluetooth_session_ != nullptr) {
488 BluetoothSession* new_bt_session =
489 new BluetoothSession(*pimpl_->bluetooth_session_);
490 new_bt_session->clear_a2dp_session();
491 new_bt_session->clear_rfcomm_session();
Jack He9bebcc02017-01-17 15:41:30 -0800492 LogBluetoothSessionEnd(DISCONNECT_REASON_METRICS_DUMP, 0);
Jack He777228a2016-12-08 19:29:00 -0800493 pimpl_->bluetooth_session_ = new_bt_session;
Jack He42823982018-08-15 12:38:37 -0700494 pimpl_->bluetooth_session_start_time_ms_ =
495 bluetooth::common::time_get_os_boottime_ms();
Jack He777228a2016-12-08 19:29:00 -0800496 pimpl_->a2dp_session_metrics_ = A2dpSessionMetrics();
497 }
498}
499
500void BluetoothMetricsLogger::Build() {
501 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_);
502 CutoffSession();
503 BluetoothLog* bluetooth_log = pimpl_->bluetooth_log_;
504 while (!pimpl_->bt_session_queue_->Empty() &&
505 static_cast<size_t>(bluetooth_log->session_size()) <=
506 pimpl_->bt_session_queue_->Capacity()) {
507 bluetooth_log->mutable_session()->AddAllocated(
508 pimpl_->bt_session_queue_->Dequeue());
509 }
510 while (!pimpl_->pair_event_queue_->Empty() &&
511 static_cast<size_t>(bluetooth_log->pair_event_size()) <=
512 pimpl_->pair_event_queue_->Capacity()) {
513 bluetooth_log->mutable_pair_event()->AddAllocated(
514 pimpl_->pair_event_queue_->Dequeue());
515 }
516 while (!pimpl_->scan_event_queue_->Empty() &&
517 static_cast<size_t>(bluetooth_log->scan_event_size()) <=
518 pimpl_->scan_event_queue_->Capacity()) {
519 bluetooth_log->mutable_scan_event()->AddAllocated(
520 pimpl_->scan_event_queue_->Dequeue());
521 }
522 while (!pimpl_->wake_event_queue_->Empty() &&
523 static_cast<size_t>(bluetooth_log->wake_event_size()) <=
524 pimpl_->wake_event_queue_->Capacity()) {
525 bluetooth_log->mutable_wake_event()->AddAllocated(
526 pimpl_->wake_event_queue_->Dequeue());
527 }
528 while (!pimpl_->bt_session_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 }
Jack He7ebb85a2018-04-02 13:04:58 -0700534 for (size_t i = 0; i < HeadsetProfileType_ARRAYSIZE; ++i) {
535 int num_times_connected = pimpl_->headset_profile_connection_counts_[i];
536 if (HeadsetProfileType_IsValid(i) && num_times_connected > 0) {
537 HeadsetProfileConnectionStats* headset_profile_connection_stats =
538 bluetooth_log->add_headset_profile_connection_stats();
539 // Able to static_cast because HeadsetProfileType_IsValid(i) is true
540 headset_profile_connection_stats->set_headset_profile_type(
541 static_cast<HeadsetProfileType>(i));
542 headset_profile_connection_stats->set_num_times_connected(
543 num_times_connected);
544 }
545 }
546 pimpl_->headset_profile_connection_counts_.fill(0);
Jack He777228a2016-12-08 19:29:00 -0800547}
548
549void BluetoothMetricsLogger::ResetSession() {
550 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_session_lock_);
551 if (pimpl_->bluetooth_session_ != nullptr) {
552 delete pimpl_->bluetooth_session_;
553 pimpl_->bluetooth_session_ = nullptr;
554 }
555 pimpl_->bluetooth_session_start_time_ms_ = 0;
556 pimpl_->a2dp_session_metrics_ = A2dpSessionMetrics();
557}
558
559void BluetoothMetricsLogger::ResetLog() {
560 std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_);
561 pimpl_->bluetooth_log_->Clear();
562}
563
564void BluetoothMetricsLogger::Reset() {
565 ResetSession();
566 ResetLog();
567 pimpl_->bt_session_queue_->Clear();
568 pimpl_->pair_event_queue_->Clear();
569 pimpl_->wake_event_queue_->Clear();
570 pimpl_->scan_event_queue_->Clear();
571}
572
Jack He0d4807d2018-12-20 15:46:17 -0800573void LogLinkLayerConnectionEvent(const RawAddress* address,
574 uint32_t connection_handle,
575 android::bluetooth::DirectionEnum direction,
Jack He7a98b402019-02-06 20:24:24 -0800576 uint16_t link_type, uint32_t hci_cmd,
577 uint16_t hci_event, uint16_t hci_ble_event,
578 uint16_t cmd_status, uint16_t reason_code) {
Jack He0d4807d2018-12-20 15:46:17 -0800579 std::string obfuscated_id;
Chen Chen721030a2020-03-09 16:19:24 -0700580 int metric_id = 0;
Jack He0d4807d2018-12-20 15:46:17 -0800581 if (address != nullptr) {
582 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(*address);
Chen Chen721030a2020-03-09 16:19:24 -0700583 metric_id = MetricIdAllocator::GetInstance().AllocateId(*address);
Jack He0d4807d2018-12-20 15:46:17 -0800584 }
585 // nullptr and size 0 represent missing value for obfuscated_id
586 android::util::BytesField bytes_field(
587 address != nullptr ? obfuscated_id.c_str() : nullptr,
588 address != nullptr ? obfuscated_id.size() : 0);
589 int ret = android::util::stats_write(
590 android::util::BLUETOOTH_LINK_LAYER_CONNECTION_EVENT, bytes_field,
591 connection_handle, direction, link_type, hci_cmd, hci_event,
Chen Chen721030a2020-03-09 16:19:24 -0700592 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) {
604 int ret =
605 android::util::stats_write(android::util::BLUETOOTH_HCI_TIMEOUT_REPORTED,
606 static_cast<int64_t>(hci_cmd));
607 if (ret < 0) {
608 LOG(WARNING) << __func__ << ": failed for opcode " << loghex(hci_cmd)
609 << ", error " << ret;
610 }
611}
612
Jack He078c8a82019-01-29 14:49:34 -0800613void LogRemoteVersionInfo(uint16_t handle, uint8_t status, uint8_t version,
614 uint16_t manufacturer_name, uint16_t subversion) {
615 int ret = android::util::stats_write(
616 android::util::BLUETOOTH_REMOTE_VERSION_INFO_REPORTED, handle, status,
617 version, manufacturer_name, subversion);
618 if (ret < 0) {
619 LOG(WARNING) << __func__ << ": failed for handle " << handle << ", status "
620 << loghex(status) << ", version " << loghex(version)
621 << ", manufacturer_name " << loghex(manufacturer_name)
622 << ", subversion " << loghex(subversion) << ", error " << ret;
623 }
624}
Jack He7d7a8de2019-01-20 21:31:12 -0800625
626void LogA2dpAudioUnderrunEvent(const RawAddress& address,
627 uint64_t encoding_interval_millis,
628 int num_missing_pcm_bytes) {
629 std::string obfuscated_id;
Chen Chen721030a2020-03-09 16:19:24 -0700630 int metric_id = 0;
Jack He7d7a8de2019-01-20 21:31:12 -0800631 if (!address.IsEmpty()) {
632 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700633 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack He7d7a8de2019-01-20 21:31:12 -0800634 }
635 // nullptr and size 0 represent missing value for obfuscated_id
636 android::util::BytesField bytes_field(
637 address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
638 address.IsEmpty() ? 0 : obfuscated_id.size());
639 int64_t encoding_interval_nanos = encoding_interval_millis * 1000000;
640 int ret = android::util::stats_write(
641 android::util::BLUETOOTH_A2DP_AUDIO_UNDERRUN_REPORTED, bytes_field,
Chen Chen721030a2020-03-09 16:19:24 -0700642 encoding_interval_nanos, num_missing_pcm_bytes, metric_id);
Jack He7d7a8de2019-01-20 21:31:12 -0800643 if (ret < 0) {
644 LOG(WARNING) << __func__ << ": failed for " << address
645 << ", encoding_interval_nanos " << encoding_interval_nanos
646 << ", num_missing_pcm_bytes " << num_missing_pcm_bytes
647 << ", error " << ret;
648 }
649}
650
651void LogA2dpAudioOverrunEvent(const RawAddress& address,
652 uint64_t encoding_interval_millis,
653 int num_dropped_buffers,
654 int num_dropped_encoded_frames,
655 int num_dropped_encoded_bytes) {
656 std::string obfuscated_id;
Chen Chen721030a2020-03-09 16:19:24 -0700657 int metric_id = 0;
Jack He7d7a8de2019-01-20 21:31:12 -0800658 if (!address.IsEmpty()) {
659 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700660 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack He7d7a8de2019-01-20 21:31:12 -0800661 }
662 // nullptr and size 0 represent missing value for obfuscated_id
663 android::util::BytesField bytes_field(
664 address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
665 address.IsEmpty() ? 0 : obfuscated_id.size());
666 int64_t encoding_interval_nanos = encoding_interval_millis * 1000000;
667 int ret = android::util::stats_write(
668 android::util::BLUETOOTH_A2DP_AUDIO_OVERRUN_REPORTED, bytes_field,
669 encoding_interval_nanos, num_dropped_buffers, num_dropped_encoded_frames,
Chen Chen721030a2020-03-09 16:19:24 -0700670 num_dropped_encoded_bytes, metric_id);
Jack He7d7a8de2019-01-20 21:31:12 -0800671 if (ret < 0) {
672 LOG(WARNING) << __func__ << ": failed to log for " << address
673 << ", encoding_interval_nanos " << encoding_interval_nanos
674 << ", num_dropped_buffers " << num_dropped_buffers
675 << ", num_dropped_encoded_frames "
676 << num_dropped_encoded_frames << ", num_dropped_encoded_bytes "
677 << num_dropped_encoded_bytes << ", error " << ret;
678 }
679}
680
681void LogReadRssiResult(const RawAddress& address, uint16_t handle,
682 uint32_t cmd_status, int8_t rssi) {
683 std::string obfuscated_id;
Chen Chen721030a2020-03-09 16:19:24 -0700684 int metric_id = 0;
Jack He7d7a8de2019-01-20 21:31:12 -0800685 if (!address.IsEmpty()) {
686 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700687 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack He7d7a8de2019-01-20 21:31:12 -0800688 }
689 // nullptr and size 0 represent missing value for obfuscated_id
690 android::util::BytesField bytes_field(
691 address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
692 address.IsEmpty() ? 0 : obfuscated_id.size());
Chen Chen721030a2020-03-09 16:19:24 -0700693 int ret = android::util::stats_write(
694 android::util::BLUETOOTH_DEVICE_RSSI_REPORTED, bytes_field, handle,
695 cmd_status, rssi, metric_id);
Jack He7d7a8de2019-01-20 21:31:12 -0800696 if (ret < 0) {
697 LOG(WARNING) << __func__ << ": failed for " << address << ", handle "
698 << handle << ", status " << loghex(cmd_status) << ", rssi "
699 << rssi << " dBm, error " << ret;
700 }
701}
702
703void LogReadFailedContactCounterResult(const RawAddress& address,
704 uint16_t handle, uint32_t cmd_status,
705 int32_t failed_contact_counter) {
706 std::string obfuscated_id;
Chen Chen721030a2020-03-09 16:19:24 -0700707 int metric_id = 0;
Jack He7d7a8de2019-01-20 21:31:12 -0800708 if (!address.IsEmpty()) {
709 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700710 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack He7d7a8de2019-01-20 21:31:12 -0800711 }
712 // nullptr and size 0 represent missing value for obfuscated_id
713 android::util::BytesField bytes_field(
714 address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
715 address.IsEmpty() ? 0 : obfuscated_id.size());
716 int ret = android::util::stats_write(
717 android::util::BLUETOOTH_DEVICE_FAILED_CONTACT_COUNTER_REPORTED,
Chen Chen721030a2020-03-09 16:19:24 -0700718 bytes_field, handle, cmd_status, failed_contact_counter, metric_id);
Jack He7d7a8de2019-01-20 21:31:12 -0800719 if (ret < 0) {
720 LOG(WARNING) << __func__ << ": failed for " << address << ", handle "
721 << handle << ", status " << loghex(cmd_status)
722 << ", failed_contact_counter " << failed_contact_counter
723 << " packets, error " << ret;
724 }
725}
726
727void LogReadTxPowerLevelResult(const RawAddress& address, uint16_t handle,
728 uint32_t cmd_status,
729 int32_t transmit_power_level) {
730 std::string obfuscated_id;
Chen Chen721030a2020-03-09 16:19:24 -0700731 int metric_id = 0;
Jack He7d7a8de2019-01-20 21:31:12 -0800732 if (!address.IsEmpty()) {
733 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700734 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack He7d7a8de2019-01-20 21:31:12 -0800735 }
736 // nullptr and size 0 represent missing value for obfuscated_id
737 android::util::BytesField bytes_field(
738 address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
739 address.IsEmpty() ? 0 : obfuscated_id.size());
740 int ret = android::util::stats_write(
741 android::util::BLUETOOTH_DEVICE_TX_POWER_LEVEL_REPORTED, bytes_field,
Chen Chen721030a2020-03-09 16:19:24 -0700742 handle, cmd_status, transmit_power_level, metric_id);
Jack He7d7a8de2019-01-20 21:31:12 -0800743 if (ret < 0) {
744 LOG(WARNING) << __func__ << ": failed for " << address << ", handle "
745 << handle << ", status " << loghex(cmd_status)
746 << ", transmit_power_level " << transmit_power_level
747 << " packets, error " << ret;
748 }
749}
750
Jack Heb75275d2019-01-31 15:17:08 -0800751void LogSmpPairingEvent(const RawAddress& address, uint8_t smp_cmd,
752 android::bluetooth::DirectionEnum direction,
753 uint8_t smp_fail_reason) {
754 std::string obfuscated_id;
Chen Chen721030a2020-03-09 16:19:24 -0700755 int metric_id = 0;
Jack Heb75275d2019-01-31 15:17:08 -0800756 if (!address.IsEmpty()) {
757 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700758 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack Heb75275d2019-01-31 15:17:08 -0800759 }
760 // nullptr and size 0 represent missing value for obfuscated_id
761 android::util::BytesField obfuscated_id_field(
762 address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
763 address.IsEmpty() ? 0 : obfuscated_id.size());
764 int ret = android::util::stats_write(
765 android::util::BLUETOOTH_SMP_PAIRING_EVENT_REPORTED, obfuscated_id_field,
Chen Chen721030a2020-03-09 16:19:24 -0700766 smp_cmd, direction, smp_fail_reason, metric_id);
Jack Heb75275d2019-01-31 15:17:08 -0800767 if (ret < 0) {
768 LOG(WARNING) << __func__ << ": failed for " << address << ", smp_cmd "
769 << loghex(smp_cmd) << ", direction " << direction
770 << ", smp_fail_reason " << loghex(smp_fail_reason)
771 << ", error " << ret;
772 }
773}
774
Jack He40553162019-03-20 04:04:36 -0700775void LogClassicPairingEvent(const RawAddress& address, uint16_t handle, uint32_t hci_cmd, uint16_t hci_event,
776 uint16_t cmd_status, uint16_t reason_code, int64_t event_value) {
Jack He0d888e52019-01-31 17:44:46 -0800777 std::string obfuscated_id;
Chen Chen721030a2020-03-09 16:19:24 -0700778 int metric_id = 0;
Jack He0d888e52019-01-31 17:44:46 -0800779 if (!address.IsEmpty()) {
780 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700781 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack He0d888e52019-01-31 17:44:46 -0800782 }
783 // nullptr and size 0 represent missing value for obfuscated_id
784 android::util::BytesField obfuscated_id_field(
785 address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
786 address.IsEmpty() ? 0 : obfuscated_id.size());
Chen Chen721030a2020-03-09 16:19:24 -0700787 int ret = android::util::stats_write(
788 android::util::BLUETOOTH_CLASSIC_PAIRING_EVENT_REPORTED,
789 obfuscated_id_field, handle, hci_cmd, hci_event, cmd_status, reason_code,
790 event_value, metric_id);
Jack He0d888e52019-01-31 17:44:46 -0800791 if (ret < 0) {
Jack He40553162019-03-20 04:04:36 -0700792 LOG(WARNING) << __func__ << ": failed for " << address << ", handle " << handle << ", hci_cmd " << loghex(hci_cmd)
793 << ", hci_event " << loghex(hci_event) << ", cmd_status " << loghex(cmd_status) << ", reason "
794 << loghex(reason_code) << ", event_value " << event_value << ", error " << ret;
Jack He0d888e52019-01-31 17:44:46 -0800795 }
796}
797
Jack He3c162a82019-02-01 18:30:21 -0800798void LogSdpAttribute(const RawAddress& address, uint16_t protocol_uuid,
799 uint16_t attribute_id, size_t attribute_size,
800 const char* attribute_value) {
801 std::string obfuscated_id;
Chen Chen721030a2020-03-09 16:19:24 -0700802 int metric_id = 0;
Jack He3c162a82019-02-01 18:30:21 -0800803 if (!address.IsEmpty()) {
804 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700805 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack He3c162a82019-02-01 18:30:21 -0800806 }
807 // nullptr and size 0 represent missing value for obfuscated_id
808 android::util::BytesField obfuscated_id_field(
809 address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
810 address.IsEmpty() ? 0 : obfuscated_id.size());
811 android::util::BytesField attribute_field(attribute_value, attribute_size);
812 int ret = android::util::stats_write(
813 android::util::BLUETOOTH_SDP_ATTRIBUTE_REPORTED, obfuscated_id_field,
Chen Chen721030a2020-03-09 16:19:24 -0700814 protocol_uuid, attribute_id, attribute_field, metric_id);
Jack He3c162a82019-02-01 18:30:21 -0800815 if (ret < 0) {
816 LOG(WARNING) << __func__ << ": failed for " << address << ", protocol_uuid "
817 << loghex(protocol_uuid) << ", attribute_id "
818 << loghex(attribute_id) << ", error " << ret;
819 }
820}
821
Jack He7a98b402019-02-06 20:24:24 -0800822void LogSocketConnectionState(
823 const RawAddress& address, int port, int type,
824 android::bluetooth::SocketConnectionstateEnum connection_state,
825 int64_t tx_bytes, int64_t rx_bytes, int uid, int server_port,
826 android::bluetooth::SocketRoleEnum socket_role) {
827 std::string obfuscated_id;
Chen Chen721030a2020-03-09 16:19:24 -0700828 int metric_id = 0;
Jack He7a98b402019-02-06 20:24:24 -0800829 if (!address.IsEmpty()) {
830 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700831 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack He7a98b402019-02-06 20:24:24 -0800832 }
833 // nullptr and size 0 represent missing value for obfuscated_id
834 android::util::BytesField obfuscated_id_field(
835 address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
836 address.IsEmpty() ? 0 : obfuscated_id.size());
837 int ret = android::util::stats_write(
838 android::util::BLUETOOTH_SOCKET_CONNECTION_STATE_CHANGED,
839 obfuscated_id_field, port, type, connection_state, tx_bytes, rx_bytes,
Chen Chen721030a2020-03-09 16:19:24 -0700840 uid, server_port, socket_role, metric_id);
Jack He7a98b402019-02-06 20:24:24 -0800841 if (ret < 0) {
842 LOG(WARNING) << __func__ << ": failed for " << address << ", port " << port
843 << ", type " << type << ", state " << connection_state
844 << ", tx_bytes " << tx_bytes << ", rx_bytes " << rx_bytes
845 << ", uid " << uid << ", server_port " << server_port
846 << ", socket_role " << socket_role << ", error " << ret;
847 }
848}
849
Jack He9e026202019-02-06 21:53:41 -0800850void LogManufacturerInfo(const RawAddress& address,
851 android::bluetooth::DeviceInfoSrcEnum source_type,
852 const std::string& source_name,
853 const std::string& manufacturer,
854 const std::string& model,
855 const std::string& hardware_version,
856 const std::string& software_version) {
857 std::string obfuscated_id;
Chen Chen721030a2020-03-09 16:19:24 -0700858 int metric_id = 0;
Jack He9e026202019-02-06 21:53:41 -0800859 if (!address.IsEmpty()) {
860 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
Chen Chen721030a2020-03-09 16:19:24 -0700861 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
Jack He9e026202019-02-06 21:53:41 -0800862 }
863 // nullptr and size 0 represent missing value for obfuscated_id
864 android::util::BytesField obfuscated_id_field(
865 address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
866 address.IsEmpty() ? 0 : obfuscated_id.size());
867 int ret = android::util::stats_write(
868 android::util::BLUETOOTH_DEVICE_INFO_REPORTED, obfuscated_id_field,
869 source_type, source_name.c_str(), manufacturer.c_str(), model.c_str(),
Chen Chen721030a2020-03-09 16:19:24 -0700870 hardware_version.c_str(), software_version.c_str(), metric_id);
Jack He9e026202019-02-06 21:53:41 -0800871 if (ret < 0) {
872 LOG(WARNING) << __func__ << ": failed for " << address << ", source_type "
873 << source_type << ", source_name " << source_name
874 << ", manufacturer " << manufacturer << ", model " << model
875 << ", hardware_version " << hardware_version
876 << ", software_version " << software_version << ", error "
877 << ret;
878 }
879}
880
weichinwengc1a2fc92020-12-15 14:56:19 +0800881void LogBluetoothHalCrashReason(const RawAddress& address, uint32_t error_code,
882 uint32_t vendor_error_code) {
883 std::string obfuscated_id;
884 int metric_id = 0;
885 if (!address.IsEmpty()) {
886 obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(address);
887 metric_id = MetricIdAllocator::GetInstance().AllocateId(address);
888 }
889 // nullptr and size 0 represent missing value for obfuscated_id
890 android::util::BytesField obfuscated_id_field(
891 address.IsEmpty() ? nullptr : obfuscated_id.c_str(),
892 address.IsEmpty() ? 0 : obfuscated_id.size());
893 int ret = android::util::stats_write(
894 android::util::BLUETOOTH_HAL_CRASH_REASON_REPORTED, metric_id,
895 obfuscated_id_field, error_code, vendor_error_code);
896 if (ret < 0) {
897 LOG(WARNING) << __func__ << ": failed for " << address << ", error_code "
898 << loghex(error_code) << ", vendor_error_code "
899 << loghex(vendor_error_code) << ", error " << ret;
900 }
901}
902
Jack He42823982018-08-15 12:38:37 -0700903} // namespace common
904
905} // namespace bluetooth