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   bluetooth::shim::LogMetricA2dpAudioUnderrunEvent(
31       address, encoding_interval_millis, num_missing_pcm_bytes);
32 }
33 
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)34 void log_a2dp_audio_overrun_event(const RawAddress& address,
35                                   uint64_t encoding_interval_millis,
36                                   int num_dropped_buffers,
37                                   int num_dropped_encoded_frames,
38                                   int num_dropped_encoded_bytes) {
39   bluetooth::shim::LogMetricA2dpAudioOverrunEvent(
40       address, encoding_interval_millis, num_dropped_buffers,
41       num_dropped_encoded_frames, num_dropped_encoded_bytes);
42 }
43 
log_a2dp_playback_event(const RawAddress & address,int playback_state,int audio_coding_mode)44 void log_a2dp_playback_event(const RawAddress& address, int playback_state,
45                              int audio_coding_mode) {
46     bluetooth::shim::LogMetricA2dpPlaybackEvent(address, playback_state,
47                                                 audio_coding_mode);
48 }
49 
log_a2dp_session_metrics_event(const RawAddress & 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)50 void log_a2dp_session_metrics_event(
51     const RawAddress& address, int64_t audio_duration_ms,
52     int media_timer_min_ms, int media_timer_max_ms, int media_timer_avg_ms,
53     int total_scheduling_count, int buffer_overruns_max_count,
54     int buffer_overruns_total, float buffer_underruns_average,
55     int buffer_underruns_count, int64_t codec_index, bool is_a2dp_offload) {
56     bluetooth::shim::LogMetricA2dpSessionMetricsEvent(
57         address, audio_duration_ms, media_timer_min_ms, media_timer_max_ms,
58         media_timer_avg_ms, total_scheduling_count, buffer_overruns_max_count,
59         buffer_overruns_total, buffer_underruns_average, buffer_underruns_count,
60         codec_index, is_a2dp_offload);
61 }
62 
log_read_rssi_result(const RawAddress & address,uint16_t handle,uint32_t cmd_status,int8_t rssi)63 void log_read_rssi_result(const RawAddress& address, uint16_t handle,
64                           uint32_t cmd_status, int8_t rssi) {
65   bluetooth::shim::LogMetricReadRssiResult(address, handle, cmd_status, rssi);
66 }
67 
log_read_failed_contact_counter_result(const RawAddress & address,uint16_t handle,uint32_t cmd_status,int32_t failed_contact_counter)68 void log_read_failed_contact_counter_result(const RawAddress& address,
69                                             uint16_t handle,
70                                             uint32_t cmd_status,
71                                             int32_t failed_contact_counter) {
72   bluetooth::shim::LogMetricReadFailedContactCounterResult(
73       address, handle, cmd_status, failed_contact_counter);
74 }
75 
log_read_tx_power_level_result(const RawAddress & address,uint16_t handle,uint32_t cmd_status,int32_t transmit_power_level)76 void log_read_tx_power_level_result(const RawAddress& address, uint16_t handle,
77                                     uint32_t cmd_status,
78                                     int32_t transmit_power_level) {
79   bluetooth::shim::LogMetricReadTxPowerLevelResult(address, handle, cmd_status,
80                                                    transmit_power_level);
81 }
82 
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)83 void log_socket_connection_state(
84     const RawAddress& address, int port, int type,
85     android::bluetooth::SocketConnectionstateEnum connection_state,
86     int64_t tx_bytes, int64_t rx_bytes, int uid, int server_port,
87     android::bluetooth::SocketRoleEnum socket_role) {
88   bluetooth::shim::LogMetricSocketConnectionState(
89       address, port, type, connection_state, tx_bytes, rx_bytes, uid,
90       server_port, socket_role);
91 }
92 
log_counter_metrics_btif(android::bluetooth::CodePathCounterKeyEnum key,int64_t value)93 void log_counter_metrics_btif(android::bluetooth::CodePathCounterKeyEnum key,
94                               int64_t value) {
95   bluetooth::shim::CountCounterMetrics(key, value);
96 }
97 
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)98 bool init_metric_id_allocator(
99     const std::unordered_map<RawAddress, int>& paired_device_map,
100     bluetooth::shim::CallbackLegacy save_device_callback,
101     bluetooth::shim::CallbackLegacy forget_device_callback) {
102   return bluetooth::shim::InitMetricIdAllocator(
103       paired_device_map, std::move(save_device_callback),
104       std::move(forget_device_callback));
105 }
106 
close_metric_id_allocator()107 bool close_metric_id_allocator() {
108   return bluetooth::shim::CloseMetricIdAllocator();
109 }
110 
allocate_metric_id_from_metric_id_allocator(const RawAddress & address)111 int allocate_metric_id_from_metric_id_allocator(const RawAddress& address) {
112   return bluetooth::shim::AllocateIdFromMetricIdAllocator(address);
113 }
114 
save_metric_id_from_metric_id_allocator(const RawAddress & address)115 int save_metric_id_from_metric_id_allocator(const RawAddress& address) {
116   return bluetooth::shim::SaveDeviceOnMetricIdAllocator(address);
117 }
118 
forget_device_from_metric_id_allocator(const RawAddress & address)119 void forget_device_from_metric_id_allocator(const RawAddress& address) {
120   bluetooth::shim::ForgetDeviceFromMetricIdAllocator(address);
121 }
122 
is_valid_id_from_metric_id_allocator(const int id)123 bool is_valid_id_from_metric_id_allocator(const int id) {
124   return bluetooth::shim::IsValidIdFromMetricIdAllocator(id);
125 }
126