1 /******************************************************************************
2  *
3  *  Copyright 2021 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #define LOG_TAG "BluetoothMetrics"
20 
21 #include "os/metrics.h"
22 
23 #include <Counter.h>
24 #include <bluetooth/log.h>
25 #include <statslog_bt.h>
26 
27 #include "a2dp_constants.h"
28 #include "common/audit_log.h"
29 #include "common/metric_id_manager.h"
30 #include "common/strings.h"
31 #include "hardware/bt_av.h"
32 #include "hci/hci_packets.h"
33 
34 namespace std {
35 template <>
36 struct formatter<android::bluetooth::DirectionEnum>
37     : enum_formatter<android::bluetooth::DirectionEnum> {};
38 template <>
39 struct formatter<android::bluetooth::SocketConnectionstateEnum>
40     : enum_formatter<android::bluetooth::SocketConnectionstateEnum> {};
41 template <>
42 struct formatter<android::bluetooth::SocketRoleEnum>
43     : enum_formatter<android::bluetooth::SocketRoleEnum> {};
44 template <>
45 struct formatter<android::bluetooth::DeviceInfoSrcEnum>
46     : enum_formatter<android::bluetooth::DeviceInfoSrcEnum> {};
47 template <>
48 struct formatter<android::bluetooth::AddressTypeEnum>
49     : enum_formatter<android::bluetooth::AddressTypeEnum> {};
50 template <>
51 struct formatter<android::bluetooth::EventType> : enum_formatter<android::bluetooth::EventType> {};
52 template <>
53 struct formatter<android::bluetooth::State> : enum_formatter<android::bluetooth::State> {};
54 }  // namespace std
55 
56 namespace bluetooth {
57 namespace os {
58 
59 using bluetooth::common::MetricIdManager;
60 using bluetooth::hci::Address;
61 using bluetooth::hci::ErrorCode;
62 using bluetooth::hci::EventCode;
63 
64 /**
65  * nullptr and size 0 represent missing value for obfuscated_id
66  */
67 static const BytesField byteField(nullptr, 0);
68 
LogMetricLinkLayerConnectionEvent(const Address * address,uint32_t connection_handle,android::bluetooth::DirectionEnum direction,uint16_t link_type,uint32_t hci_cmd,uint16_t hci_event,uint16_t hci_ble_event,uint16_t cmd_status,uint16_t reason_code)69 void LogMetricLinkLayerConnectionEvent(const Address* address, uint32_t connection_handle,
70                                        android::bluetooth::DirectionEnum direction,
71                                        uint16_t link_type, uint32_t hci_cmd, uint16_t hci_event,
72                                        uint16_t hci_ble_event, uint16_t cmd_status,
73                                        uint16_t reason_code) {
74   int metric_id = 0;
75   if (address != nullptr) {
76     metric_id = MetricIdManager::GetInstance().AllocateId(*address);
77   }
78   int ret = stats_write(BLUETOOTH_LINK_LAYER_CONNECTION_EVENT, byteField, connection_handle,
79                         direction, link_type, hci_cmd, hci_event, hci_ble_event, cmd_status,
80                         reason_code, metric_id);
81   if (ret < 0) {
82     log::warn(
83             "Failed to log status {} , reason {}, from cmd {}, event {},  ble_event {}, for {}, "
84             "handle "
85             "{}, type {}, error {}",
86             common::ToHexString(cmd_status), common::ToHexString(reason_code),
87             common::ToHexString(hci_cmd), common::ToHexString(hci_event),
88             common::ToHexString(hci_ble_event),
89             address ? ADDRESS_TO_LOGGABLE_CSTR(*address) : "(NULL)", connection_handle,
90             common::ToHexString(link_type), ret);
91   }
92 }
93 
LogMetricHciTimeoutEvent(uint32_t hci_cmd)94 void LogMetricHciTimeoutEvent(uint32_t hci_cmd) {
95   int ret = stats_write(BLUETOOTH_HCI_TIMEOUT_REPORTED, static_cast<int64_t>(hci_cmd));
96   if (ret < 0) {
97     log::warn("Failed for opcode {}, error {}", common::ToHexString(hci_cmd), ret);
98   }
99 }
100 
LogMetricRemoteVersionInfo(uint16_t handle,uint8_t status,uint8_t version,uint16_t manufacturer_name,uint16_t subversion)101 void LogMetricRemoteVersionInfo(uint16_t handle, uint8_t status, uint8_t version,
102                                 uint16_t manufacturer_name, uint16_t subversion) {
103   int ret = stats_write(BLUETOOTH_REMOTE_VERSION_INFO_REPORTED, handle, status, version,
104                         manufacturer_name, subversion);
105   if (ret < 0) {
106     log::warn(
107             "Failed for handle {}, status {}, version {}, manufacturer_name {}, subversion {}, "
108             "error "
109             "{}",
110             handle, common::ToHexString(status), common::ToHexString(version),
111             common::ToHexString(manufacturer_name), common::ToHexString(subversion), ret);
112   }
113 }
114 
LogMetricA2dpAudioUnderrunEvent(const Address & address,uint64_t encoding_interval_millis,int num_missing_pcm_bytes)115 void LogMetricA2dpAudioUnderrunEvent(const Address& address, uint64_t encoding_interval_millis,
116                                      int num_missing_pcm_bytes) {
117   int metric_id = 0;
118   if (!address.IsEmpty()) {
119     metric_id = MetricIdManager::GetInstance().AllocateId(address);
120   }
121   int64_t encoding_interval_nanos = encoding_interval_millis * 1000000;
122   int ret = stats_write(BLUETOOTH_A2DP_AUDIO_UNDERRUN_REPORTED, byteField, encoding_interval_nanos,
123                         num_missing_pcm_bytes, metric_id);
124   if (ret < 0) {
125     log::warn("Failed for {}, encoding_interval_nanos {}, num_missing_pcm_bytes {}, error {}",
126               address, encoding_interval_nanos, num_missing_pcm_bytes, ret);
127   }
128 }
129 
LogMetricA2dpAudioOverrunEvent(const Address & address,uint64_t encoding_interval_millis,int num_dropped_buffers,int num_dropped_encoded_frames,int num_dropped_encoded_bytes)130 void LogMetricA2dpAudioOverrunEvent(const Address& address, uint64_t encoding_interval_millis,
131                                     int num_dropped_buffers, int num_dropped_encoded_frames,
132                                     int num_dropped_encoded_bytes) {
133   int metric_id = 0;
134   if (!address.IsEmpty()) {
135     metric_id = MetricIdManager::GetInstance().AllocateId(address);
136   }
137 
138   int64_t encoding_interval_nanos = encoding_interval_millis * 1000000;
139   int ret = stats_write(BLUETOOTH_A2DP_AUDIO_OVERRUN_REPORTED, byteField, encoding_interval_nanos,
140                         num_dropped_buffers, num_dropped_encoded_frames, num_dropped_encoded_bytes,
141                         metric_id);
142   if (ret < 0) {
143     log::warn(
144             "Failed to log for {}, encoding_interval_nanos {}, num_dropped_buffers {}, "
145             "num_dropped_encoded_frames {}, num_dropped_encoded_bytes {}, error {}",
146             address, encoding_interval_nanos, num_dropped_buffers, num_dropped_encoded_frames,
147             num_dropped_encoded_bytes, ret);
148   }
149 }
150 
LogMetricA2dpPlaybackEvent(const Address & address,int playback_state,int audio_coding_mode)151 void LogMetricA2dpPlaybackEvent(const Address& address, int playback_state, int audio_coding_mode) {
152   int metric_id = 0;
153   if (!address.IsEmpty()) {
154     metric_id = MetricIdManager::GetInstance().AllocateId(address);
155   }
156 
157   int ret = stats_write(BLUETOOTH_A2DP_PLAYBACK_STATE_CHANGED, byteField, playback_state,
158                         audio_coding_mode, metric_id);
159   if (ret < 0) {
160     log::warn("Failed to log for {}, playback_state {}, audio_coding_mode {},error {}", address,
161               playback_state, audio_coding_mode, ret);
162   }
163 }
164 
LogMetricA2dpSessionMetricsEvent(const hci::Address & address,int64_t audio_duration_ms,int media_timer_min_ms,int media_timer_max_ms,int media_timer_avg_ms,int total_scheduling_count,int buffer_overruns_max_count,int buffer_overruns_total,float buffer_underruns_average,int buffer_underruns_count,int64_t codec_index,bool is_a2dp_offload)165 void LogMetricA2dpSessionMetricsEvent(const hci::Address& address, int64_t audio_duration_ms,
166                                       int media_timer_min_ms, int media_timer_max_ms,
167                                       int media_timer_avg_ms, int total_scheduling_count,
168                                       int buffer_overruns_max_count, int buffer_overruns_total,
169                                       float buffer_underruns_average, int buffer_underruns_count,
170                                       int64_t codec_index, bool is_a2dp_offload) {
171   char const* expresslog_metric_id;
172   a2dp::CodecId codec_id;
173   switch (codec_index) {
174     case BTAV_A2DP_CODEC_INDEX_SOURCE_SBC:
175       expresslog_metric_id = "bluetooth.value_sbc_codec_usage_over_a2dp";
176       codec_id = a2dp::CodecId::SBC;
177       break;
178     case BTAV_A2DP_CODEC_INDEX_SOURCE_AAC:
179       expresslog_metric_id = "bluetooth.value_aac_codec_usage_over_a2dp";
180       codec_id = a2dp::CodecId::AAC;
181       break;
182     case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX:
183       expresslog_metric_id = "bluetooth.value_aptx_codec_usage_over_a2dp";
184       codec_id = a2dp::CodecId::APTX;
185       break;
186     case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD:
187       expresslog_metric_id = "bluetooth.value_aptx_hd_codec_usage_over_a2dp";
188       codec_id = a2dp::CodecId::APTX_HD;
189       break;
190     case BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC:
191       expresslog_metric_id = "bluetooth.value_ldac_codec_usage_over_a2dp";
192       codec_id = a2dp::CodecId::LDAC;
193       break;
194     case BTAV_A2DP_CODEC_INDEX_SOURCE_OPUS:
195       expresslog_metric_id = "bluetooth.value_opus_codec_usage_over_a2dp";
196       codec_id = a2dp::CodecId::OPUS;
197       break;
198     default:
199       return;
200   }
201 
202   android::expresslog::Counter::logIncrement(expresslog_metric_id);
203 
204   int32_t metric_id = MetricIdManager::GetInstance().AllocateId(address);
205   int ret = stats_write(A2DP_SESSION_REPORTED, audio_duration_ms, media_timer_min_ms,
206                         media_timer_max_ms, media_timer_avg_ms, total_scheduling_count,
207                         buffer_overruns_max_count, buffer_overruns_total, buffer_underruns_average,
208                         buffer_underruns_count, static_cast<uint64_t>(codec_id), is_a2dp_offload,
209                         metric_id);
210 
211   if (ret < 0) {
212     log::warn("failed to log a2dp_session_reported");
213   }
214 }
215 
LogMetricHfpPacketLossStats(const Address &,int,double,uint16_t)216 void LogMetricHfpPacketLossStats(const Address& /* address */, int /* num_decoded_frames */,
217                                  double /* packet_loss_ratio */, uint16_t /* codec_type */) {}
218 
LogMetricMmcTranscodeRttStats(int,double,int,int)219 void LogMetricMmcTranscodeRttStats(int /*maximum_rtt*/, double /*mean_rtt*/, int /*num_requests*/,
220                                    int /*codec_type*/) {}
221 
LogMetricReadRssiResult(const Address & address,uint16_t handle,uint32_t cmd_status,int8_t rssi)222 void LogMetricReadRssiResult(const Address& address, uint16_t handle, uint32_t cmd_status,
223                              int8_t rssi) {
224   int metric_id = 0;
225   if (!address.IsEmpty()) {
226     metric_id = MetricIdManager::GetInstance().AllocateId(address);
227   }
228   int ret = stats_write(BLUETOOTH_DEVICE_RSSI_REPORTED, byteField, handle, cmd_status, rssi,
229                         metric_id);
230   if (ret < 0) {
231     log::warn("Failed for {}, handle {}, status {}, rssi {} dBm, error {}", address, handle,
232               common::ToHexString(cmd_status), rssi, ret);
233   }
234 }
235 
LogMetricReadFailedContactCounterResult(const Address & address,uint16_t handle,uint32_t cmd_status,int32_t failed_contact_counter)236 void LogMetricReadFailedContactCounterResult(const Address& address, uint16_t handle,
237                                              uint32_t cmd_status, int32_t failed_contact_counter) {
238   int metric_id = 0;
239   if (!address.IsEmpty()) {
240     metric_id = MetricIdManager::GetInstance().AllocateId(address);
241   }
242   int ret = stats_write(BLUETOOTH_DEVICE_FAILED_CONTACT_COUNTER_REPORTED, byteField, handle,
243                         cmd_status, failed_contact_counter, metric_id);
244   if (ret < 0) {
245     log::warn("Failed for {}, handle {}, status {}, failed_contact_counter {} packets, error {}",
246               address, handle, common::ToHexString(cmd_status), failed_contact_counter, ret);
247   }
248 }
249 
LogMetricReadTxPowerLevelResult(const Address & address,uint16_t handle,uint32_t cmd_status,int32_t transmit_power_level)250 void LogMetricReadTxPowerLevelResult(const Address& address, uint16_t handle, uint32_t cmd_status,
251                                      int32_t transmit_power_level) {
252   int metric_id = 0;
253   if (!address.IsEmpty()) {
254     metric_id = MetricIdManager::GetInstance().AllocateId(address);
255   }
256   int ret = stats_write(BLUETOOTH_DEVICE_TX_POWER_LEVEL_REPORTED, byteField, handle, cmd_status,
257                         transmit_power_level, metric_id);
258   if (ret < 0) {
259     log::warn("Failed for {}, handle {}, status {}, transmit_power_level {} packets, error {}",
260               address, handle, common::ToHexString(cmd_status), transmit_power_level, ret);
261   }
262 }
263 
LogMetricSmpPairingEvent(const Address & address,uint16_t smp_cmd,android::bluetooth::DirectionEnum direction,uint16_t smp_fail_reason)264 void LogMetricSmpPairingEvent(const Address& address, uint16_t smp_cmd,
265                               android::bluetooth::DirectionEnum direction,
266                               uint16_t smp_fail_reason) {
267   int metric_id = 0;
268   if (!address.IsEmpty()) {
269     metric_id = MetricIdManager::GetInstance().AllocateId(address);
270   }
271   int ret = stats_write(BLUETOOTH_SMP_PAIRING_EVENT_REPORTED, byteField, smp_cmd, direction,
272                         smp_fail_reason, metric_id);
273   if (ret < 0) {
274     log::warn("Failed for {}, smp_cmd {}, direction {}, smp_fail_reason {}, error {}", address,
275               common::ToHexString(smp_cmd), direction, common::ToHexString(smp_fail_reason), ret);
276   }
277 }
278 
LogMetricClassicPairingEvent(const Address & address,uint16_t handle,uint32_t hci_cmd,uint16_t hci_event,uint16_t cmd_status,uint16_t reason_code,int64_t event_value)279 void LogMetricClassicPairingEvent(const Address& address, uint16_t handle, uint32_t hci_cmd,
280                                   uint16_t hci_event, uint16_t cmd_status, uint16_t reason_code,
281                                   int64_t event_value) {
282   int metric_id = 0;
283   if (!address.IsEmpty()) {
284     metric_id = MetricIdManager::GetInstance().AllocateId(address);
285   }
286   int ret = stats_write(BLUETOOTH_CLASSIC_PAIRING_EVENT_REPORTED, byteField, handle, hci_cmd,
287                         hci_event, cmd_status, reason_code, event_value, metric_id);
288   if (ret < 0) {
289     log::warn(
290             "Failed for {}, handle {}, hci_cmd {}, hci_event {}, cmd_status {}, reason {}, "
291             "event_value "
292             "{}, error {}",
293             address, handle, common::ToHexString(hci_cmd), common::ToHexString(hci_event),
294             common::ToHexString(cmd_status), common::ToHexString(reason_code), event_value, ret);
295   }
296 
297   if (static_cast<EventCode>(hci_event) == EventCode::SIMPLE_PAIRING_COMPLETE) {
298     common::LogConnectionAdminAuditEvent("Pairing", address, static_cast<ErrorCode>(cmd_status));
299   }
300 }
301 
LogMetricSdpAttribute(const Address & address,uint16_t protocol_uuid,uint16_t attribute_id,size_t attribute_size,const char * attribute_value)302 void LogMetricSdpAttribute(const Address& address, uint16_t protocol_uuid, uint16_t attribute_id,
303                            size_t attribute_size, const char* attribute_value) {
304   int metric_id = 0;
305   if (!address.IsEmpty()) {
306     metric_id = MetricIdManager::GetInstance().AllocateId(address);
307   }
308   BytesField attribute_field(attribute_value, attribute_size);
309   int ret = stats_write(BLUETOOTH_SDP_ATTRIBUTE_REPORTED, byteField, protocol_uuid, attribute_id,
310                         attribute_field, metric_id);
311   if (ret < 0) {
312     log::warn("Failed for {}, protocol_uuid {}, attribute_id {}, error {}", address,
313               common::ToHexString(protocol_uuid), common::ToHexString(attribute_id), ret);
314   }
315 }
316 
LogMetricSocketConnectionState(const Address & address,int port,int type,android::bluetooth::SocketConnectionstateEnum connection_state,int64_t tx_bytes,int64_t rx_bytes,int uid,int server_port,android::bluetooth::SocketRoleEnum socket_role)317 void LogMetricSocketConnectionState(const Address& address, int port, int type,
318                                     android::bluetooth::SocketConnectionstateEnum connection_state,
319                                     int64_t tx_bytes, int64_t rx_bytes, int uid, int server_port,
320                                     android::bluetooth::SocketRoleEnum socket_role) {
321   int metric_id = 0;
322   if (!address.IsEmpty()) {
323     metric_id = MetricIdManager::GetInstance().AllocateId(address);
324   }
325   int ret = stats_write(BLUETOOTH_SOCKET_CONNECTION_STATE_CHANGED, byteField, port, type,
326                         connection_state, tx_bytes, rx_bytes, uid, server_port, socket_role,
327                         metric_id);
328   if (ret < 0) {
329     log::warn(
330             "Failed for {}, port {}, type {}, state {}, tx_bytes {}, rx_bytes {}, uid {}, "
331             "server_port "
332             "{}, socket_role {}, error {}",
333             address, port, type, connection_state, tx_bytes, rx_bytes, uid, server_port,
334             socket_role, ret);
335   }
336 }
337 
LogMetricManufacturerInfo(const Address & address,android::bluetooth::AddressTypeEnum address_type,android::bluetooth::DeviceInfoSrcEnum source_type,const std::string & source_name,const std::string & manufacturer,const std::string & model,const std::string & hardware_version,const std::string & software_version)338 void LogMetricManufacturerInfo(const Address& address,
339                                android::bluetooth::AddressTypeEnum address_type,
340                                android::bluetooth::DeviceInfoSrcEnum source_type,
341                                const std::string& source_name, const std::string& manufacturer,
342                                const std::string& model, const std::string& hardware_version,
343                                const std::string& software_version) {
344   int metric_id = 0;
345   if (!address.IsEmpty()) {
346     metric_id = MetricIdManager::GetInstance().AllocateId(address);
347   }
348   int ret = stats_write(BLUETOOTH_DEVICE_INFO_REPORTED, byteField, source_type, source_name.c_str(),
349                         manufacturer.c_str(), model.c_str(), hardware_version.c_str(),
350                         software_version.c_str(), metric_id, address_type, address.address[5],
351                         address.address[4], address.address[3]);
352   if (ret < 0) {
353     log::warn(
354             "Failed for {}, source_type {}, source_name {}, manufacturer {}, model {}, "
355             "hardware_version {}, software_version {}, MAC address type {} MAC address prefix {} "
356             "{} "
357             "{}, error {}",
358             address, source_type, source_name, manufacturer, model, hardware_version,
359             software_version, address_type, address.address[5], address.address[4],
360             address.address[3], ret);
361   }
362 }
363 
LogMetricBluetoothHalCrashReason(const Address & address,uint32_t error_code,uint32_t vendor_error_code)364 void LogMetricBluetoothHalCrashReason(const Address& address, uint32_t error_code,
365                                       uint32_t vendor_error_code) {
366   int ret = stats_write(BLUETOOTH_HAL_CRASH_REASON_REPORTED, 0 /* metric_id */, byteField,
367                         error_code, vendor_error_code);
368   if (ret < 0) {
369     log::warn("Failed for {}, error_code {}, vendor_error_code {}, error {}", address,
370               common::ToHexString(error_code), common::ToHexString(vendor_error_code), ret);
371   }
372 }
373 
LogMetricBluetoothLocalSupportedFeatures(uint32_t page_num,uint64_t features)374 void LogMetricBluetoothLocalSupportedFeatures(uint32_t page_num, uint64_t features) {
375   int ret = stats_write(BLUETOOTH_LOCAL_SUPPORTED_FEATURES_REPORTED, page_num,
376                         static_cast<int64_t>(features));
377   if (ret < 0) {
378     log::warn(
379             "Failed for LogMetricBluetoothLocalSupportedFeatures, page_num {}, features {}, error "
380             "{}",
381             page_num, features, ret);
382   }
383 }
384 
LogMetricBluetoothLocalVersions(uint32_t lmp_manufacturer_name,uint8_t lmp_version,uint32_t lmp_subversion,uint8_t hci_version,uint32_t hci_revision)385 void LogMetricBluetoothLocalVersions(uint32_t lmp_manufacturer_name, uint8_t lmp_version,
386                                      uint32_t lmp_subversion, uint8_t hci_version,
387                                      uint32_t hci_revision) {
388   int ret = stats_write(BLUETOOTH_LOCAL_VERSIONS_REPORTED,
389                         static_cast<int32_t>(lmp_manufacturer_name),
390                         static_cast<int32_t>(lmp_version), static_cast<int32_t>(lmp_subversion),
391                         static_cast<int32_t>(hci_version), static_cast<int32_t>(hci_revision));
392   if (ret < 0) {
393     log::warn(
394             "Failed for LogMetricBluetoothLocalVersions, lmp_manufacturer_name {}, lmp_version {}, "
395             "lmp_subversion {}, hci_version {}, hci_revision {}, error {}",
396             lmp_manufacturer_name, lmp_version, lmp_subversion, hci_version, hci_revision, ret);
397   }
398 }
399 
LogMetricBluetoothDisconnectionReasonReported(uint32_t reason,const Address & address,uint32_t connection_handle)400 void LogMetricBluetoothDisconnectionReasonReported(uint32_t reason, const Address& address,
401                                                    uint32_t connection_handle) {
402   int metric_id = 0;
403   if (!address.IsEmpty()) {
404     metric_id = MetricIdManager::GetInstance().AllocateId(address);
405   }
406   int ret = stats_write(BLUETOOTH_DISCONNECTION_REASON_REPORTED, reason, metric_id,
407                         connection_handle);
408   if (ret < 0) {
409     log::warn(
410             "Failed for LogMetricBluetoothDisconnectionReasonReported, reason {}, metric_id {}, "
411             "connection_handle {}, error {}",
412             reason, metric_id, connection_handle, ret);
413   }
414 }
415 
LogMetricBluetoothRemoteSupportedFeatures(const Address & address,uint32_t page,uint64_t features,uint32_t connection_handle)416 void LogMetricBluetoothRemoteSupportedFeatures(const Address& address, uint32_t page,
417                                                uint64_t features, uint32_t connection_handle) {
418   int metric_id = 0;
419   if (!address.IsEmpty()) {
420     metric_id = MetricIdManager::GetInstance().AllocateId(address);
421   }
422   int ret = stats_write(BLUETOOTH_REMOTE_SUPPORTED_FEATURES_REPORTED, metric_id, page,
423                         static_cast<int64_t>(features), connection_handle);
424   if (ret < 0) {
425     log::warn(
426             "Failed for LogMetricBluetoothRemoteSupportedFeatures, metric_id {}, page {}, features "
427             "{}, "
428             "connection_handle {}, error {}",
429             metric_id, page, features, connection_handle, ret);
430   }
431 }
432 
LogMetricBluetoothCodePathCounterMetrics(int32_t key,int64_t count)433 void LogMetricBluetoothCodePathCounterMetrics(int32_t key, int64_t count) {
434   int ret = stats_write(BLUETOOTH_CODE_PATH_COUNTER, key, count);
435   if (ret < 0) {
436     log::warn("Failed counter metrics for {}, count {}, error {}", key, count, ret);
437   }
438 }
439 
LogMetricBluetoothLEConnection(os::LEConnectionSessionOptions session_options)440 void LogMetricBluetoothLEConnection(os::LEConnectionSessionOptions session_options) {
441   int metric_id = 0;
442   if (!session_options.remote_address.IsEmpty()) {
443     metric_id = MetricIdManager::GetInstance().AllocateId(session_options.remote_address);
444   }
445   int ret = stats_write(BLUETOOTH_LE_SESSION_CONNECTED, session_options.acl_connection_state,
446                         session_options.origin_type, session_options.transaction_type,
447                         session_options.transaction_state, session_options.latency, metric_id,
448                         session_options.app_uid, session_options.acl_latency,
449                         session_options.status, session_options.is_cancelled);
450 
451   if (ret < 0) {
452     log::warn(
453             "Failed BluetoothLeSessionConnected - Address: {}, ACL Connection State: {}, Origin "
454             "Type:  "
455             "{}",
456             session_options.remote_address,
457             common::ToHexString(session_options.acl_connection_state),
458             common::ToHexString(session_options.origin_type));
459   }
460 }
461 
LogMetricBluetoothEvent(const Address & address,android::bluetooth::EventType event_type,android::bluetooth::State state)462 void LogMetricBluetoothEvent(const Address& address, android::bluetooth::EventType event_type,
463                              android::bluetooth::State state) {
464   if (address.IsEmpty()) {
465     log::warn("Failed BluetoothEvent Upload - Address is Empty");
466     return;
467   }
468   int metric_id = MetricIdManager::GetInstance().AllocateId(address);
469   int ret = stats_write(BLUETOOTH_CROSS_LAYER_EVENT_REPORTED,
470                         event_type,
471                         state,
472                         0,
473                         metric_id,
474                         BytesField(nullptr, 0));
475   if (ret < 0) {
476     log::warn("Failed BluetoothEvent Upload - Address {}, Event_type {}, State {}", address,
477               event_type, state);
478   }
479 }
480 
481 }  // namespace os
482 }  // namespace bluetooth
483