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 #include "os/metrics.h"
20 
21 #include <bluetooth/log.h>
22 #include <metrics/structured_events.h>
23 
24 #include "metrics/utils.h"
25 
26 namespace bluetooth {
27 namespace os {
28 
29 using bluetooth::hci::Address;
30 
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)31 void LogMetricClassicPairingEvent(const Address& address, uint16_t handle, uint32_t hci_cmd,
32                                   uint16_t hci_event, uint16_t cmd_status, uint16_t reason_code,
33                                   int64_t event_value) {}
34 
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)35 void LogMetricSocketConnectionState(const Address& address, int port, int type,
36                                     android::bluetooth::SocketConnectionstateEnum connection_state,
37                                     int64_t tx_bytes, int64_t rx_bytes, int uid, int server_port,
38                                     android::bluetooth::SocketRoleEnum socket_role) {}
39 
LogMetricHciTimeoutEvent(uint32_t hci_cmd)40 void LogMetricHciTimeoutEvent(uint32_t hci_cmd) {}
41 
LogMetricA2dpAudioUnderrunEvent(const Address & address,uint64_t encoding_interval_millis,int num_missing_pcm_bytes)42 void LogMetricA2dpAudioUnderrunEvent(const Address& address, uint64_t encoding_interval_millis,
43                                      int num_missing_pcm_bytes) {}
44 
LogMetricA2dpAudioOverrunEvent(const Address & address,uint64_t encoding_interval_millis,int num_dropped_buffers,int num_dropped_encoded_frames,int num_dropped_encoded_bytes)45 void LogMetricA2dpAudioOverrunEvent(const Address& address, uint64_t encoding_interval_millis,
46                                     int num_dropped_buffers, int num_dropped_encoded_frames,
47                                     int num_dropped_encoded_bytes) {
48   std::string boot_id;
49   std::string addr_string;
50 
51   if (!metrics::GetBootId(&boot_id)) {
52     return;
53   }
54 
55   addr_string = address.ToString();
56 
57   log::debug("A2dpAudioOverrun: {}, {}, {}, {}, {}, {}", boot_id, address, encoding_interval_millis,
58              num_dropped_buffers, num_dropped_encoded_bytes, num_dropped_encoded_bytes);
59 
60   ::metrics::structured::events::bluetooth::BluetoothA2dpAudioOverrun()
61           .SetBootId(boot_id)
62           .SetDeviceId(addr_string)
63           .SetEncodingInterval(encoding_interval_millis)
64           .SetDroppedBuffers(num_dropped_buffers)
65           .SetDroppedFrames(num_dropped_encoded_frames)
66           .SetDroppedBytes(num_dropped_encoded_bytes)
67           .Record();
68 }
69 
LogMetricHfpPacketLossStats(const Address & address,int num_decoded_frames,double packet_loss_ratio,uint16_t codec_type)70 void LogMetricHfpPacketLossStats(const Address& address, int num_decoded_frames,
71                                  double packet_loss_ratio, uint16_t codec_type) {
72   std::string boot_id;
73   std::string addr_string;
74 
75   if (!metrics::GetBootId(&boot_id)) {
76     return;
77   }
78 
79   addr_string = address.ToString();
80 
81   log::debug("HfpPacketLoss: {}, {}, {}, {:f}, {}", boot_id, address, num_decoded_frames,
82              packet_loss_ratio, codec_type);
83 
84   ::metrics::structured::events::bluetooth::BluetoothHfpPacketLoss()
85           .SetBootId(boot_id)
86           .SetDeviceId(addr_string)
87           .SetDecodedFrames(num_decoded_frames)
88           .SetPacketLossRatio(packet_loss_ratio)
89           .SetCodecType(codec_type)
90           .Record();
91 }
92 
LogMetricMmcTranscodeRttStats(int maximum_rtt,double mean_rtt,int num_requests,int codec_type)93 void LogMetricMmcTranscodeRttStats(int maximum_rtt, double mean_rtt, int num_requests,
94                                    int codec_type) {
95   std::string boot_id;
96 
97   if (!metrics::GetBootId(&boot_id)) {
98     return;
99   }
100 
101   log::debug("MmcTranscodeRttStats: {}, {}, {:f}, {}, {}", boot_id, maximum_rtt, mean_rtt,
102              num_requests, codec_type);
103 
104   ::metrics::structured::events::bluetooth::BluetoothMmcTranscodeRtt()
105           .SetBootId(boot_id)
106           .SetMaximumRtt(maximum_rtt)
107           .SetMeanRtt(mean_rtt)
108           .SetNumRequests(num_requests)
109           .SetCodecType(codec_type)
110           .Record();
111 }
112 
LogMetricReadRssiResult(const Address & address,uint16_t handle,uint32_t cmd_status,int8_t rssi)113 void LogMetricReadRssiResult(const Address& address, uint16_t handle, uint32_t cmd_status,
114                              int8_t rssi) {}
115 
LogMetricReadFailedContactCounterResult(const Address & address,uint16_t handle,uint32_t cmd_status,int32_t failed_contact_counter)116 void LogMetricReadFailedContactCounterResult(const Address& address, uint16_t handle,
117                                              uint32_t cmd_status, int32_t failed_contact_counter) {}
118 
LogMetricReadTxPowerLevelResult(const Address & address,uint16_t handle,uint32_t cmd_status,int32_t transmit_power_level)119 void LogMetricReadTxPowerLevelResult(const Address& address, uint16_t handle, uint32_t cmd_status,
120                                      int32_t transmit_power_level) {}
121 
LogMetricRemoteVersionInfo(uint16_t handle,uint8_t status,uint8_t version,uint16_t manufacturer_name,uint16_t subversion)122 void LogMetricRemoteVersionInfo(uint16_t handle, uint8_t status, uint8_t version,
123                                 uint16_t manufacturer_name, uint16_t subversion) {}
124 
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)125 void LogMetricLinkLayerConnectionEvent(const Address* address, uint32_t connection_handle,
126                                        android::bluetooth::DirectionEnum direction,
127                                        uint16_t link_type, uint32_t hci_cmd, uint16_t hci_event,
128                                        uint16_t hci_ble_event, uint16_t cmd_status,
129                                        uint16_t reason_code) {}
130 
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)131 void LogMetricManufacturerInfo(const Address& address,
132                                android::bluetooth::AddressTypeEnum address_type,
133                                android::bluetooth::DeviceInfoSrcEnum source_type,
134                                const std::string& source_name, const std::string& manufacturer,
135                                const std::string& model, const std::string& hardware_version,
136                                const std::string& software_version) {}
137 
LogMetricSdpAttribute(const Address & address,uint16_t protocol_uuid,uint16_t attribute_id,size_t attribute_size,const char * attribute_value)138 void LogMetricSdpAttribute(const Address& address, uint16_t protocol_uuid, uint16_t attribute_id,
139                            size_t attribute_size, const char* attribute_value) {}
140 
LogMetricSmpPairingEvent(const Address & address,uint16_t smp_cmd,android::bluetooth::DirectionEnum direction,uint16_t smp_fail_reason)141 void LogMetricSmpPairingEvent(const Address& address, uint16_t smp_cmd,
142                               android::bluetooth::DirectionEnum direction,
143                               uint16_t smp_fail_reason) {}
144 
LogMetricA2dpPlaybackEvent(const Address & address,int playback_state,int audio_coding_mode)145 void LogMetricA2dpPlaybackEvent(const Address& address, int playback_state, int audio_coding_mode) {
146 }
147 
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)148 void LogMetricA2dpSessionMetricsEvent(const hci::Address& address, int64_t audio_duration_ms,
149                                       int media_timer_min_ms, int media_timer_max_ms,
150                                       int media_timer_avg_ms, int total_scheduling_count,
151                                       int buffer_overruns_max_count, int buffer_overruns_total,
152                                       float buffer_underruns_average, int buffer_underruns_count,
153                                       int64_t codec_index, bool is_a2dp_offload) {
154   std::string boot_id;
155   std::string addr_string;
156 
157   if (!metrics::GetBootId(&boot_id)) {
158     return;
159   }
160 
161   addr_string = address.ToString();
162 
163   log::debug("A2dpSessionMetrics: {}, {}, {}, {}, {}, {}, {}, {}, {}, {:f}, {}, {}, {}", boot_id,
164              address, audio_duration_ms, media_timer_min_ms, media_timer_max_ms, media_timer_avg_ms,
165              total_scheduling_count, buffer_overruns_max_count, buffer_overruns_total,
166              buffer_underruns_average, buffer_underruns_count, codec_index, is_a2dp_offload);
167 
168   ::metrics::structured::events::bluetooth::BluetoothA2dpSession()
169           .SetBootId(boot_id)
170           .SetDeviceId(addr_string)
171           .SetAudioDuration(audio_duration_ms)
172           .SetMediaTimerMin(media_timer_min_ms)
173           .SetMediaTimerMax(media_timer_max_ms)
174           .SetMediaTimerAvg(media_timer_avg_ms)
175           .SetTotalSchedulingCount(total_scheduling_count)
176           .SetBufferOverrunsMaxCount(buffer_overruns_max_count)
177           .SetBufferOverrunsTotal(buffer_overruns_total)
178           .SetBufferUnderrunsAvg(buffer_underruns_average)
179           .SetBufferUnderrunsCount(buffer_underruns_count)
180           .SetCodecIndex(codec_index)
181           .SetIsA2dpOffload(is_a2dp_offload)
182           .Record();
183 }
184 
LogMetricBluetoothHalCrashReason(const Address & address,uint32_t error_code,uint32_t vendor_error_code)185 void LogMetricBluetoothHalCrashReason(const Address& address, uint32_t error_code,
186                                       uint32_t vendor_error_code) {}
187 
LogMetricBluetoothLocalSupportedFeatures(uint32_t page_num,uint64_t features)188 void LogMetricBluetoothLocalSupportedFeatures(uint32_t page_num, uint64_t features) {}
189 
LogMetricBluetoothLocalVersions(uint32_t lmp_manufacturer_name,uint8_t lmp_version,uint32_t lmp_subversion,uint8_t hci_version,uint32_t hci_revision)190 void LogMetricBluetoothLocalVersions(uint32_t lmp_manufacturer_name, uint8_t lmp_version,
191                                      uint32_t lmp_subversion, uint8_t hci_version,
192                                      uint32_t hci_revision) {}
193 
LogMetricBluetoothDisconnectionReasonReported(uint32_t reason,const Address & address,uint32_t connection_handle)194 void LogMetricBluetoothDisconnectionReasonReported(uint32_t reason, const Address& address,
195                                                    uint32_t connection_handle) {}
196 
LogMetricBluetoothRemoteSupportedFeatures(const Address & address,uint32_t page,uint64_t features,uint32_t connection_handle)197 void LogMetricBluetoothRemoteSupportedFeatures(const Address& address, uint32_t page,
198                                                uint64_t features, uint32_t connection_handle) {}
199 
LogMetricBluetoothCodePathCounterMetrics(int32_t key,int64_t count)200 void LogMetricBluetoothCodePathCounterMetrics(int32_t key, int64_t count) {}
201 
LogMetricBluetoothLEConnection(os::LEConnectionSessionOptions)202 void LogMetricBluetoothLEConnection(os::LEConnectionSessionOptions /* session_options */) {}
203 
LogMetricBluetoothEvent(const Address & address,android::bluetooth::EventType event_type,android::bluetooth::State state)204 void LogMetricBluetoothEvent(const Address& address, android::bluetooth::EventType event_type,
205                              android::bluetooth::State state) {}
206 }  // namespace os
207 }  // namespace bluetooth
208