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 <frameworks/proto_logging/stats/enums/bluetooth/enums.pb.h>
18 #include <frameworks/proto_logging/stats/enums/bluetooth/hci/enums.pb.h>
19 
20 #include "btif/include/btif_metrics_logging.h"
21 #include "common/metric_id_allocator.h"
22 #include "common/metrics.h"
23 #include "main/shim/metrics_api.h"
24 #include "main/shim/shim.h"
25 #include "types/raw_address.h"
26 
log_a2dp_audio_underrun_event(const RawAddress & address,uint64_t encoding_interval_millis,int num_missing_pcm_bytes)27 void log_a2dp_audio_underrun_event(const RawAddress& address,
28                                    uint64_t encoding_interval_millis,
29                                    int num_missing_pcm_bytes) {
30   if (bluetooth::shim::is_any_gd_enabled()) {
31     bluetooth::shim::LogMetricA2dpAudioUnderrunEvent(
32         address, encoding_interval_millis, num_missing_pcm_bytes);
33   } else {
34     bluetooth::common::LogA2dpAudioUnderrunEvent(
35         address, encoding_interval_millis, num_missing_pcm_bytes);
36   }
37 }
38 
log_a2dp_audio_overrun_event(const RawAddress & address,uint64_t encoding_interval_millis,int num_dropped_buffers,int num_dropped_encoded_frames,int num_dropped_encoded_bytes)39 void log_a2dp_audio_overrun_event(const RawAddress& address,
40                                   uint64_t encoding_interval_millis,
41                                   int num_dropped_buffers,
42                                   int num_dropped_encoded_frames,
43                                   int num_dropped_encoded_bytes) {
44   if (bluetooth::shim::is_any_gd_enabled()) {
45     bluetooth::shim::LogMetricA2dpAudioOverrunEvent(
46         address, encoding_interval_millis, num_dropped_buffers,
47         num_dropped_encoded_frames, num_dropped_encoded_bytes);
48   } else {
49     bluetooth::common::LogA2dpAudioOverrunEvent(
50         address, encoding_interval_millis, num_dropped_buffers,
51         num_dropped_encoded_frames, num_dropped_encoded_bytes);
52   }
53 }
54 
log_a2dp_playback_event(const RawAddress & address,int playback_state,int audio_coding_mode)55 void log_a2dp_playback_event(const RawAddress& address, int playback_state,
56                              int audio_coding_mode) {
57   if (bluetooth::shim::is_any_gd_enabled()) {
58     bluetooth::shim::LogMetricA2dpPlaybackEvent(address, playback_state,
59                                                 audio_coding_mode);
60   } else {
61     bluetooth::common::LogA2dpPlaybackEvent(address, playback_state,
62                                             audio_coding_mode);
63   }
64 }
65 
log_read_rssi_result(const RawAddress & address,uint16_t handle,uint32_t cmd_status,int8_t rssi)66 void log_read_rssi_result(const RawAddress& address, uint16_t handle,
67                           uint32_t cmd_status, int8_t rssi) {
68   if (bluetooth::shim::is_any_gd_enabled()) {
69     bluetooth::shim::LogMetricReadRssiResult(address, handle, cmd_status, rssi);
70   } else {
71     bluetooth::common::LogReadRssiResult(address, handle, cmd_status, rssi);
72   }
73 }
74 
log_read_failed_contact_counter_result(const RawAddress & address,uint16_t handle,uint32_t cmd_status,int32_t failed_contact_counter)75 void log_read_failed_contact_counter_result(const RawAddress& address,
76                                             uint16_t handle,
77                                             uint32_t cmd_status,
78                                             int32_t failed_contact_counter) {
79   if (bluetooth::shim::is_any_gd_enabled()) {
80     bluetooth::shim::LogMetricReadFailedContactCounterResult(
81         address, handle, cmd_status, failed_contact_counter);
82   } else {
83     bluetooth::common::LogReadFailedContactCounterResult(
84         address, handle, cmd_status, failed_contact_counter);
85   }
86 }
87 
log_read_tx_power_level_result(const RawAddress & address,uint16_t handle,uint32_t cmd_status,int32_t transmit_power_level)88 void log_read_tx_power_level_result(const RawAddress& address, uint16_t handle,
89                                     uint32_t cmd_status,
90                                     int32_t transmit_power_level) {
91   if (bluetooth::shim::is_any_gd_enabled()) {
92     bluetooth::shim::LogMetricReadTxPowerLevelResult(
93         address, handle, cmd_status, transmit_power_level);
94   } else {
95     bluetooth::common::LogReadTxPowerLevelResult(address, handle, cmd_status,
96                                                  transmit_power_level);
97   }
98 }
99 
log_socket_connection_state(const RawAddress & 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)100 void log_socket_connection_state(
101     const RawAddress& address, int port, int type,
102     android::bluetooth::SocketConnectionstateEnum connection_state,
103     int64_t tx_bytes, int64_t rx_bytes, int uid, int server_port,
104     android::bluetooth::SocketRoleEnum socket_role) {
105   if (bluetooth::shim::is_any_gd_enabled()) {
106     bluetooth::shim::LogMetricSocketConnectionState(
107         address, port, type, connection_state, tx_bytes, rx_bytes, uid,
108         server_port, socket_role);
109   } else {
110     bluetooth::common::LogSocketConnectionState(
111         address, port, type, connection_state, tx_bytes, rx_bytes, uid,
112         server_port, socket_role);
113   }
114 }
115 
init_metric_id_allocator(const std::unordered_map<RawAddress,int> & paired_device_map,bluetooth::shim::CallbackLegacy save_device_callback,bluetooth::shim::CallbackLegacy forget_device_callback)116 bool init_metric_id_allocator(
117     const std::unordered_map<RawAddress, int>& paired_device_map,
118     bluetooth::shim::CallbackLegacy save_device_callback,
119     bluetooth::shim::CallbackLegacy forget_device_callback) {
120   if (bluetooth::shim::is_any_gd_enabled()) {
121     return bluetooth::shim::InitMetricIdAllocator(
122         paired_device_map, std::move(save_device_callback),
123         std::move(forget_device_callback));
124   }
125   return bluetooth::common::MetricIdAllocator::GetInstance().Init(
126       paired_device_map, std::move(save_device_callback),
127       std::move(forget_device_callback));
128 }
129 
close_metric_id_allocator()130 bool close_metric_id_allocator() {
131   if (bluetooth::shim::is_any_gd_enabled()) {
132     return bluetooth::shim::CloseMetricIdAllocator();
133   }
134   return bluetooth::common::MetricIdAllocator::GetInstance().Close();
135 }
136 
allocate_metric_id_from_metric_id_allocator(const RawAddress & address)137 int allocate_metric_id_from_metric_id_allocator(const RawAddress& address) {
138   if (bluetooth::shim::is_any_gd_enabled()) {
139     return bluetooth::shim::AllocateIdFromMetricIdAllocator(address);
140   }
141   return bluetooth::common::MetricIdAllocator::GetInstance().AllocateId(
142       address);
143 }
144 
save_metric_id_from_metric_id_allocator(const RawAddress & address)145 int save_metric_id_from_metric_id_allocator(const RawAddress& address) {
146   if (bluetooth::shim::is_any_gd_enabled()) {
147     return bluetooth::shim::SaveDeviceOnMetricIdAllocator(address);
148   }
149   return bluetooth::common::MetricIdAllocator::GetInstance().SaveDevice(
150       address);
151 }
152 
forget_device_from_metric_id_allocator(const RawAddress & address)153 void forget_device_from_metric_id_allocator(const RawAddress& address) {
154   if (bluetooth::shim::is_any_gd_enabled()) {
155     bluetooth::shim::ForgetDeviceFromMetricIdAllocator(address);
156   } else {
157     bluetooth::common::MetricIdAllocator::GetInstance().ForgetDevice(address);
158   }
159 }
160 
is_valid_id_from_metric_id_allocator(const int id)161 bool is_valid_id_from_metric_id_allocator(const int id) {
162   if (bluetooth::shim::is_any_gd_enabled()) {
163     return bluetooth::shim::IsValidIdFromMetricIdAllocator(id);
164   }
165   return bluetooth::common::MetricIdAllocator::IsValidId(id);
166 }
167