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