• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gd/hci/address.h"
20 #include "gd/metrics/counter_metrics.h"
21 #include "gd/os/metrics.h"
22 #include "main/shim/entry.h"
23 #include "main/shim/helpers.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 
LogMetricHfpPacketLossStats(const RawAddress & raw_address,int num_decoded_frames,double packet_loss_ratio)70 void LogMetricHfpPacketLossStats(const RawAddress& raw_address,
71                                  int num_decoded_frames,
72                                  double packet_loss_ratio) {
73   Address address = bluetooth::ToGdAddress(raw_address);
74   bluetooth::os::LogMetricHfpPacketLossStats(address, num_decoded_frames,
75                                              packet_loss_ratio);
76 }
77 
LogMetricReadRssiResult(const RawAddress & raw_address,uint16_t handle,uint32_t cmd_status,int8_t rssi)78 void LogMetricReadRssiResult(const RawAddress& raw_address, uint16_t handle,
79                              uint32_t cmd_status, int8_t rssi) {
80   Address address = bluetooth::ToGdAddress(raw_address);
81   bluetooth::os::LogMetricReadRssiResult(address, handle, cmd_status, rssi);
82 }
83 
LogMetricReadFailedContactCounterResult(const RawAddress & raw_address,uint16_t handle,uint32_t cmd_status,int32_t failed_contact_counter)84 void LogMetricReadFailedContactCounterResult(const RawAddress& raw_address,
85                                              uint16_t handle,
86                                              uint32_t cmd_status,
87                                              int32_t failed_contact_counter) {
88   Address address = bluetooth::ToGdAddress(raw_address);
89   bluetooth::os::LogMetricReadFailedContactCounterResult(
90       address, handle, cmd_status, failed_contact_counter);
91 }
92 
LogMetricReadTxPowerLevelResult(const RawAddress & raw_address,uint16_t handle,uint32_t cmd_status,int32_t transmit_power_level)93 void LogMetricReadTxPowerLevelResult(const RawAddress& raw_address,
94                                      uint16_t handle, uint32_t cmd_status,
95                                      int32_t transmit_power_level) {
96   Address address = bluetooth::ToGdAddress(raw_address);
97   bluetooth::os::LogMetricReadTxPowerLevelResult(address, handle, cmd_status,
98                                                  transmit_power_level);
99 }
100 
LogMetricSmpPairingEvent(const RawAddress & raw_address,uint16_t smp_cmd,android::bluetooth::DirectionEnum direction,uint16_t smp_fail_reason)101 void LogMetricSmpPairingEvent(const RawAddress& raw_address, uint16_t smp_cmd,
102                               android::bluetooth::DirectionEnum direction,
103                               uint16_t smp_fail_reason) {
104   Address address = bluetooth::ToGdAddress(raw_address);
105   bluetooth::os::LogMetricSmpPairingEvent(address, smp_cmd, direction,
106                                           smp_fail_reason);
107 }
108 
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)109 void LogMetricClassicPairingEvent(const RawAddress& raw_address,
110                                   uint16_t handle, uint32_t hci_cmd,
111                                   uint16_t hci_event, uint16_t cmd_status,
112                                   uint16_t reason_code, int64_t event_value) {
113   Address address = bluetooth::ToGdAddress(raw_address);
114   bluetooth::os::LogMetricClassicPairingEvent(address, handle, hci_cmd,
115                                               hci_event, cmd_status,
116                                               reason_code, event_value);
117 }
118 
LogMetricSdpAttribute(const RawAddress & raw_address,uint16_t protocol_uuid,uint16_t attribute_id,size_t attribute_size,const char * attribute_value)119 void LogMetricSdpAttribute(const RawAddress& raw_address,
120                            uint16_t protocol_uuid, uint16_t attribute_id,
121                            size_t attribute_size, const char* attribute_value) {
122   Address address = bluetooth::ToGdAddress(raw_address);
123   bluetooth::os::LogMetricSdpAttribute(address, protocol_uuid, attribute_id,
124                                        attribute_size, attribute_value);
125 }
126 
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)127 void LogMetricSocketConnectionState(
128     const RawAddress& raw_address, int port, int type,
129     android::bluetooth::SocketConnectionstateEnum connection_state,
130     int64_t tx_bytes, int64_t rx_bytes, int uid, int server_port,
131     android::bluetooth::SocketRoleEnum socket_role) {
132   Address address = bluetooth::ToGdAddress(raw_address);
133   bluetooth::os::LogMetricSocketConnectionState(
134       address, port, type, connection_state, tx_bytes, rx_bytes, uid,
135       server_port, socket_role);
136 }
137 
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)138 void LogMetricManufacturerInfo(
139     const RawAddress& raw_address,
140     android::bluetooth::AddressTypeEnum address_type,
141     android::bluetooth::DeviceInfoSrcEnum source_type,
142     const std::string& source_name, const std::string& manufacturer,
143     const std::string& model, const std::string& hardware_version,
144     const std::string& software_version) {
145   Address address = bluetooth::ToGdAddress(raw_address);
146   bluetooth::os::LogMetricManufacturerInfo(address, address_type, source_type,
147                                            source_name, manufacturer, model,
148                                            hardware_version, software_version);
149 }
150 
CountCounterMetrics(int32_t key,int64_t count)151 bool CountCounterMetrics(int32_t key, int64_t count) {
152   auto counter_metrics = GetCounterMetrics();
153   if (counter_metrics == nullptr) {
154     return false;
155   }
156   return counter_metrics->Count(key, count);
157 }
158 
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)159 void LogMetricBluetoothLEConnectionMetricEvent(
160     const RawAddress& raw_address,
161     android::bluetooth::le::LeConnectionOriginType origin_type,
162     android::bluetooth::le::LeConnectionType connection_type,
163     android::bluetooth::le::LeConnectionState transaction_state,
164     std::vector<std::pair<os::ArgumentType, int>> argument_list) {
165 
166   Address address = bluetooth::ToGdAddress(raw_address);
167   bluetooth::os::LogMetricBluetoothLEConnectionMetricEvent(address, origin_type, connection_type, transaction_state, argument_list);
168 }
169 
170 }  // namespace shim
171 }  // namespace bluetooth
172