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 "main/shim/entry.h"
20 #include "main/shim/helpers.h"
21 #include "metrics/bluetooth_event.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(const RawAddress* raw_address, uint32_t connection_handle,
31 android::bluetooth::DirectionEnum direction,
32 uint16_t link_type, uint32_t hci_cmd, uint16_t hci_event,
33 uint16_t hci_ble_event, uint16_t cmd_status,
34 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, link_type,
41 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, int num_missing_pcm_bytes) {
46 Address address = bluetooth::ToGdAddress(raw_address);
47 bluetooth::os::LogMetricA2dpAudioUnderrunEvent(address, encoding_interval_millis,
48 num_missing_pcm_bytes);
49 }
50
LogMetricA2dpAudioOverrunEvent(const RawAddress & raw_address,uint64_t encoding_interval_millis,int num_dropped_buffers,int num_dropped_encoded_frames,int num_dropped_encoded_bytes)51 void LogMetricA2dpAudioOverrunEvent(const RawAddress& raw_address,
52 uint64_t encoding_interval_millis, int num_dropped_buffers,
53 int num_dropped_encoded_frames, int num_dropped_encoded_bytes) {
54 Address address = bluetooth::ToGdAddress(raw_address);
55 bluetooth::os::LogMetricA2dpAudioOverrunEvent(address, encoding_interval_millis,
56 num_dropped_buffers, num_dropped_encoded_frames,
57 num_dropped_encoded_bytes);
58 }
59
LogMetricA2dpPlaybackEvent(const RawAddress & raw_address,int playback_state,int audio_coding_mode)60 void LogMetricA2dpPlaybackEvent(const RawAddress& raw_address, int playback_state,
61 int audio_coding_mode) {
62 Address address = bluetooth::ToGdAddress(raw_address);
63 bluetooth::os::LogMetricA2dpPlaybackEvent(address, playback_state, audio_coding_mode);
64 }
65
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)66 void LogMetricA2dpSessionMetricsEvent(const RawAddress& raw_address, int64_t audio_duration_ms,
67 int media_timer_min_ms, int media_timer_max_ms,
68 int media_timer_avg_ms, int total_scheduling_count,
69 int buffer_overruns_max_count, int buffer_overruns_total,
70 float buffer_underruns_average, int buffer_underruns_count,
71 int64_t codec_index, bool is_a2dp_offload) {
72 Address address = bluetooth::ToGdAddress(raw_address);
73 bluetooth::os::LogMetricA2dpSessionMetricsEvent(
74 address, audio_duration_ms, media_timer_min_ms, media_timer_max_ms, media_timer_avg_ms,
75 total_scheduling_count, buffer_overruns_max_count, buffer_overruns_total,
76 buffer_underruns_average, buffer_underruns_count, codec_index, is_a2dp_offload);
77 }
78
LogMetricHfpPacketLossStats(const RawAddress & raw_address,int num_decoded_frames,double packet_loss_ratio,uint16_t codec_type)79 void LogMetricHfpPacketLossStats(const RawAddress& raw_address, int num_decoded_frames,
80 double packet_loss_ratio, uint16_t codec_type) {
81 Address address = bluetooth::ToGdAddress(raw_address);
82 bluetooth::os::LogMetricHfpPacketLossStats(address, num_decoded_frames, packet_loss_ratio,
83 codec_type);
84 }
85
LogMetricMmcTranscodeRttStats(int maximum_rtt,double mean_rtt,int num_requests,int codec_type)86 void LogMetricMmcTranscodeRttStats(int maximum_rtt, double mean_rtt, int num_requests,
87 int codec_type) {
88 bluetooth::os::LogMetricMmcTranscodeRttStats(maximum_rtt, mean_rtt, num_requests, codec_type);
89 }
90
LogMetricReadRssiResult(const RawAddress & raw_address,uint16_t handle,uint32_t cmd_status,int8_t rssi)91 void LogMetricReadRssiResult(const RawAddress& raw_address, uint16_t handle, uint32_t cmd_status,
92 int8_t rssi) {
93 Address address = bluetooth::ToGdAddress(raw_address);
94 bluetooth::os::LogMetricReadRssiResult(address, handle, cmd_status, rssi);
95 }
96
LogMetricReadFailedContactCounterResult(const RawAddress & raw_address,uint16_t handle,uint32_t cmd_status,int32_t failed_contact_counter)97 void LogMetricReadFailedContactCounterResult(const RawAddress& raw_address, uint16_t handle,
98 uint32_t cmd_status, int32_t failed_contact_counter) {
99 Address address = bluetooth::ToGdAddress(raw_address);
100 bluetooth::os::LogMetricReadFailedContactCounterResult(address, handle, cmd_status,
101 failed_contact_counter);
102 }
103
LogMetricReadTxPowerLevelResult(const RawAddress & raw_address,uint16_t handle,uint32_t cmd_status,int32_t transmit_power_level)104 void LogMetricReadTxPowerLevelResult(const RawAddress& raw_address, uint16_t handle,
105 uint32_t cmd_status, int32_t transmit_power_level) {
106 Address address = bluetooth::ToGdAddress(raw_address);
107 bluetooth::os::LogMetricReadTxPowerLevelResult(address, handle, cmd_status, transmit_power_level);
108 }
109
LogMetricSmpPairingEvent(const RawAddress & raw_address,uint16_t smp_cmd,android::bluetooth::DirectionEnum direction,uint16_t smp_fail_reason)110 void LogMetricSmpPairingEvent(const RawAddress& raw_address, uint16_t smp_cmd,
111 android::bluetooth::DirectionEnum direction,
112 uint16_t smp_fail_reason) {
113 Address address = bluetooth::ToGdAddress(raw_address);
114 bluetooth::os::LogMetricSmpPairingEvent(address, smp_cmd, direction, smp_fail_reason);
115 }
116
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)117 void LogMetricClassicPairingEvent(const RawAddress& raw_address, uint16_t handle, uint32_t hci_cmd,
118 uint16_t hci_event, uint16_t cmd_status, uint16_t reason_code,
119 int64_t event_value) {
120 Address address = bluetooth::ToGdAddress(raw_address);
121 bluetooth::os::LogMetricClassicPairingEvent(address, handle, hci_cmd, hci_event, cmd_status,
122 reason_code, event_value);
123 }
124
LogMetricSdpAttribute(const RawAddress & raw_address,uint16_t protocol_uuid,uint16_t attribute_id,size_t attribute_size,const char * attribute_value)125 void LogMetricSdpAttribute(const RawAddress& raw_address, uint16_t protocol_uuid,
126 uint16_t attribute_id, size_t attribute_size,
127 const char* attribute_value) {
128 Address address = bluetooth::ToGdAddress(raw_address);
129 bluetooth::os::LogMetricSdpAttribute(address, protocol_uuid, attribute_id, attribute_size,
130 attribute_value);
131 }
132
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)133 void LogMetricSocketConnectionState(const RawAddress& raw_address, int port, int type,
134 android::bluetooth::SocketConnectionstateEnum connection_state,
135 int64_t tx_bytes, int64_t rx_bytes, int uid, int server_port,
136 android::bluetooth::SocketRoleEnum socket_role) {
137 Address address = bluetooth::ToGdAddress(raw_address);
138 bluetooth::os::LogMetricSocketConnectionState(address, port, type, connection_state, tx_bytes,
139 rx_bytes, uid, server_port, socket_role);
140 }
141
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)142 void LogMetricManufacturerInfo(const RawAddress& raw_address,
143 android::bluetooth::AddressTypeEnum address_type,
144 android::bluetooth::DeviceInfoSrcEnum source_type,
145 const std::string& source_name, const std::string& manufacturer,
146 const std::string& model, const std::string& hardware_version,
147 const std::string& software_version) {
148 Address address = bluetooth::ToGdAddress(raw_address);
149 bluetooth::os::LogMetricManufacturerInfo(address, address_type, source_type, source_name,
150 manufacturer, model, hardware_version, software_version);
151 }
152
LogMetricLePairingFail(const RawAddress & raw_address,uint8_t failure_reason,bool is_outgoing)153 void LogMetricLePairingFail(const RawAddress& raw_address, uint8_t failure_reason,
154 bool is_outgoing) {
155 bluetooth::metrics::LogLePairingFail(raw_address, failure_reason, is_outgoing);
156 }
157
LogMetricLeConnectionStatus(hci::Address address,bool is_connect,hci::ErrorCode reason)158 void LogMetricLeConnectionStatus(hci::Address address, bool is_connect, hci::ErrorCode reason) {
159 bluetooth::os::LogMetricBluetoothEvent(
160 address,
161 is_connect ? android::bluetooth::EventType::GATT_CONNECT_NATIVE
162 : android::bluetooth::EventType::GATT_DISCONNECT_NATIVE,
163 bluetooth::metrics::MapErrorCodeToState(reason));
164 }
165
LogMetricLeDeviceInAcceptList(hci::Address address,bool is_add)166 void LogMetricLeDeviceInAcceptList(hci::Address address, bool is_add) {
167 bluetooth::os::LogMetricBluetoothEvent(
168 address, android::bluetooth::EventType::LE_DEVICE_IN_ACCEPT_LIST,
169 is_add ? android::bluetooth::State::START : android::bluetooth::State::END);
170 }
171
LogMetricLeConnectionLifecycle(hci::Address address,bool is_connect,bool is_direct)172 void LogMetricLeConnectionLifecycle(hci::Address address, bool is_connect, bool is_direct) {
173 if (is_connect) {
174 bluetooth::os::LogMetricBluetoothEvent(address,
175 android::bluetooth::EventType::GATT_CONNECT_NATIVE,
176 is_direct ? android::bluetooth::State::DIRECT_CONNECT
177 : android::bluetooth::State::INDIRECT_CONNECT);
178 } else {
179 bluetooth::os::LogMetricBluetoothEvent(address,
180 android::bluetooth::EventType::GATT_DISCONNECT_NATIVE,
181 android::bluetooth::State::START);
182 }
183 }
184
CountCounterMetrics(int32_t key,int64_t count)185 bool CountCounterMetrics(int32_t key, int64_t count) {
186 auto counter_metrics = GetCounterMetrics();
187 if (counter_metrics == nullptr) {
188 return false;
189 }
190 return counter_metrics->Count(key, count);
191 }
192
193 } // namespace shim
194 } // namespace bluetooth
195