1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "main/shim/metrics_api.h"
18 
19 #include "hci/address.h"
20 #include "main/shim/entry.h"
21 #include "main/shim/helpers.h"
22 #include "metrics/counter_metrics.h"
23 #include "os/metrics.h"
24 #include "types/raw_address.h"
25 
26 using bluetooth::hci::Address;
27 
28 namespace bluetooth {
29 namespace shim {
LogMetricLinkLayerConnectionEvent(const RawAddress * raw_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)30 void LogMetricLinkLayerConnectionEvent(
31     const RawAddress* raw_address, uint32_t connection_handle,
32     android::bluetooth::DirectionEnum direction, uint16_t link_type,
33     uint32_t hci_cmd, uint16_t hci_event, uint16_t hci_ble_event,
34     uint16_t cmd_status, uint16_t reason_code) {
35   Address address = Address::kEmpty;
36   if (raw_address != nullptr) {
37     address = bluetooth::ToGdAddress(*raw_address);
38   }
39   bluetooth::os::LogMetricLinkLayerConnectionEvent(
40       raw_address == nullptr ? nullptr : &address, connection_handle, direction,
41       link_type, hci_cmd, hci_event, hci_ble_event, cmd_status, reason_code);
42 }
43 
LogMetricA2dpAudioUnderrunEvent(const RawAddress & raw_address,uint64_t encoding_interval_millis,int num_missing_pcm_bytes)44 void LogMetricA2dpAudioUnderrunEvent(const RawAddress& raw_address,
45                                      uint64_t encoding_interval_millis,
46                                      int num_missing_pcm_bytes) {
47   Address address = bluetooth::ToGdAddress(raw_address);
48   bluetooth::os::LogMetricA2dpAudioUnderrunEvent(
49       address, encoding_interval_millis, num_missing_pcm_bytes);
50 }
51 
LogMetricA2dpAudioOverrunEvent(const RawAddress & raw_address,uint64_t encoding_interval_millis,int num_dropped_buffers,int num_dropped_encoded_frames,int num_dropped_encoded_bytes)52 void LogMetricA2dpAudioOverrunEvent(const RawAddress& raw_address,
53                                     uint64_t encoding_interval_millis,
54                                     int num_dropped_buffers,
55                                     int num_dropped_encoded_frames,
56                                     int num_dropped_encoded_bytes) {
57   Address address = bluetooth::ToGdAddress(raw_address);
58   bluetooth::os::LogMetricA2dpAudioOverrunEvent(
59       address, encoding_interval_millis, num_dropped_buffers,
60       num_dropped_encoded_frames, num_dropped_encoded_bytes);
61 }
62 
LogMetricA2dpPlaybackEvent(const RawAddress & raw_address,int playback_state,int audio_coding_mode)63 void LogMetricA2dpPlaybackEvent(const RawAddress& raw_address,
64                                 int playback_state, int audio_coding_mode) {
65   Address address = bluetooth::ToGdAddress(raw_address);
66   bluetooth::os::LogMetricA2dpPlaybackEvent(address, playback_state,
67                                             audio_coding_mode);
68 }
69 
LogMetricA2dpSessionMetricsEvent(const RawAddress & raw_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)70 void LogMetricA2dpSessionMetricsEvent(
71     const RawAddress& raw_address, int64_t audio_duration_ms,
72     int media_timer_min_ms, int media_timer_max_ms, int media_timer_avg_ms,
73     int total_scheduling_count, int buffer_overruns_max_count,
74     int buffer_overruns_total, float buffer_underruns_average,
75     int buffer_underruns_count, int64_t codec_index, bool is_a2dp_offload) {
76   Address address = bluetooth::ToGdAddress(raw_address);
77   bluetooth::os::LogMetricA2dpSessionMetricsEvent(
78       address, audio_duration_ms, media_timer_min_ms, media_timer_max_ms,
79       media_timer_avg_ms, total_scheduling_count, buffer_overruns_max_count,
80       buffer_overruns_total, buffer_underruns_average, buffer_underruns_count,
81       codec_index, is_a2dp_offload);
82 }
83 
LogMetricHfpPacketLossStats(const RawAddress & raw_address,int num_decoded_frames,double packet_loss_ratio,uint16_t codec_type)84 void LogMetricHfpPacketLossStats(const RawAddress& raw_address,
85                                  int num_decoded_frames,
86                                  double packet_loss_ratio,
87                                  uint16_t codec_type) {
88   Address address = bluetooth::ToGdAddress(raw_address);
89   bluetooth::os::LogMetricHfpPacketLossStats(address, num_decoded_frames,
90                                              packet_loss_ratio, codec_type);
91 }
92 
LogMetricMmcTranscodeRttStats(int maximum_rtt,double mean_rtt,int num_requests,int codec_type)93 void LogMetricMmcTranscodeRttStats(int maximum_rtt, double mean_rtt,
94                                    int num_requests, int codec_type) {
95   bluetooth::os::LogMetricMmcTranscodeRttStats(maximum_rtt, mean_rtt,
96                                                num_requests, codec_type);
97 }
98 
LogMetricReadRssiResult(const RawAddress & raw_address,uint16_t handle,uint32_t cmd_status,int8_t rssi)99 void LogMetricReadRssiResult(const RawAddress& raw_address, uint16_t handle,
100                              uint32_t cmd_status, int8_t rssi) {
101   Address address = bluetooth::ToGdAddress(raw_address);
102   bluetooth::os::LogMetricReadRssiResult(address, handle, cmd_status, rssi);
103 }
104 
LogMetricReadFailedContactCounterResult(const RawAddress & raw_address,uint16_t handle,uint32_t cmd_status,int32_t failed_contact_counter)105 void LogMetricReadFailedContactCounterResult(const RawAddress& raw_address,
106                                              uint16_t handle,
107                                              uint32_t cmd_status,
108                                              int32_t failed_contact_counter) {
109   Address address = bluetooth::ToGdAddress(raw_address);
110   bluetooth::os::LogMetricReadFailedContactCounterResult(
111       address, handle, cmd_status, failed_contact_counter);
112 }
113 
LogMetricReadTxPowerLevelResult(const RawAddress & raw_address,uint16_t handle,uint32_t cmd_status,int32_t transmit_power_level)114 void LogMetricReadTxPowerLevelResult(const RawAddress& raw_address,
115                                      uint16_t handle, uint32_t cmd_status,
116                                      int32_t transmit_power_level) {
117   Address address = bluetooth::ToGdAddress(raw_address);
118   bluetooth::os::LogMetricReadTxPowerLevelResult(address, handle, cmd_status,
119                                                  transmit_power_level);
120 }
121 
LogMetricSmpPairingEvent(const RawAddress & raw_address,uint16_t smp_cmd,android::bluetooth::DirectionEnum direction,uint16_t smp_fail_reason)122 void LogMetricSmpPairingEvent(const RawAddress& raw_address, uint16_t smp_cmd,
123                               android::bluetooth::DirectionEnum direction,
124                               uint16_t smp_fail_reason) {
125   Address address = bluetooth::ToGdAddress(raw_address);
126   bluetooth::os::LogMetricSmpPairingEvent(address, smp_cmd, direction,
127                                           smp_fail_reason);
128 }
129 
LogMetricClassicPairingEvent(const RawAddress & raw_address,uint16_t handle,uint32_t hci_cmd,uint16_t hci_event,uint16_t cmd_status,uint16_t reason_code,int64_t event_value)130 void LogMetricClassicPairingEvent(const RawAddress& raw_address,
131                                   uint16_t handle, uint32_t hci_cmd,
132                                   uint16_t hci_event, uint16_t cmd_status,
133                                   uint16_t reason_code, int64_t event_value) {
134   Address address = bluetooth::ToGdAddress(raw_address);
135   bluetooth::os::LogMetricClassicPairingEvent(address, handle, hci_cmd,
136                                               hci_event, cmd_status,
137                                               reason_code, event_value);
138 }
139 
LogMetricSdpAttribute(const RawAddress & raw_address,uint16_t protocol_uuid,uint16_t attribute_id,size_t attribute_size,const char * attribute_value)140 void LogMetricSdpAttribute(const RawAddress& raw_address,
141                            uint16_t protocol_uuid, uint16_t attribute_id,
142                            size_t attribute_size, const char* attribute_value) {
143   Address address = bluetooth::ToGdAddress(raw_address);
144   bluetooth::os::LogMetricSdpAttribute(address, protocol_uuid, attribute_id,
145                                        attribute_size, attribute_value);
146 }
147 
LogMetricSocketConnectionState(const RawAddress & raw_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)148 void LogMetricSocketConnectionState(
149     const RawAddress& raw_address, int port, int type,
150     android::bluetooth::SocketConnectionstateEnum connection_state,
151     int64_t tx_bytes, int64_t rx_bytes, int uid, int server_port,
152     android::bluetooth::SocketRoleEnum socket_role) {
153   Address address = bluetooth::ToGdAddress(raw_address);
154   bluetooth::os::LogMetricSocketConnectionState(
155       address, port, type, connection_state, tx_bytes, rx_bytes, uid,
156       server_port, socket_role);
157 }
158 
LogMetricManufacturerInfo(const RawAddress & raw_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)159 void LogMetricManufacturerInfo(
160     const RawAddress& raw_address,
161     android::bluetooth::AddressTypeEnum address_type,
162     android::bluetooth::DeviceInfoSrcEnum source_type,
163     const std::string& source_name, const std::string& manufacturer,
164     const std::string& model, const std::string& hardware_version,
165     const std::string& software_version) {
166   Address address = bluetooth::ToGdAddress(raw_address);
167   bluetooth::os::LogMetricManufacturerInfo(address, address_type, source_type,
168                                            source_name, manufacturer, model,
169                                            hardware_version, software_version);
170 }
171 
CountCounterMetrics(int32_t key,int64_t count)172 bool CountCounterMetrics(int32_t key, int64_t count) {
173   auto counter_metrics = GetCounterMetrics();
174   if (counter_metrics == nullptr) {
175     return false;
176   }
177   return counter_metrics->Count(key, count);
178 }
179 
LogMetricBluetoothLEConnectionMetricEvent(const RawAddress & 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)180 void LogMetricBluetoothLEConnectionMetricEvent(
181     const RawAddress& raw_address,
182     android::bluetooth::le::LeConnectionOriginType origin_type,
183     android::bluetooth::le::LeConnectionType connection_type,
184     android::bluetooth::le::LeConnectionState transaction_state,
185     std::vector<std::pair<os::ArgumentType, int>> argument_list) {
186 
187   Address address = bluetooth::ToGdAddress(raw_address);
188   bluetooth::os::LogMetricBluetoothLEConnectionMetricEvent(address, origin_type, connection_type, transaction_state, argument_list);
189 }
190 
191 }  // namespace shim
192 }  // namespace bluetooth
193