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 #pragma once
18 
19 #include <frameworks/proto_logging/stats/enums/bluetooth/enums.pb.h>
20 #include <frameworks/proto_logging/stats/enums/bluetooth/hci/enums.pb.h>
21 #include <frameworks/proto_logging/stats/enums/bluetooth/le/enums.pb.h>
22 
23 #include "os/metrics.h"
24 #include "types/raw_address.h"
25 
26 namespace bluetooth {
27 namespace shim {
28 
29 /**
30  * Log link layer connection event
31  *
32  * @param address Stack wide consistent Bluetooth address of this event,
33  *                nullptr if unknown
34  * @param connection_handle connection handle of this event,
35  *                          {@link kUnknownConnectionHandle} if unknown
36  * @param direction direction of this connection
37  * @param link_type type of the link
38  * @param hci_cmd HCI command opecode associated with this event, if any
39  * @param hci_event HCI event code associated with this event, if any
40  * @param hci_ble_event HCI BLE event code associated with this event, if any
41  * @param cmd_status Command status associated with this event, if any
42  * @param reason_code Reason code associated with this event, if any
43  */
44 void LogMetricLinkLayerConnectionEvent(
45     const RawAddress* address, uint32_t connection_handle,
46     android::bluetooth::DirectionEnum direction, uint16_t link_type,
47     uint32_t hci_cmd, uint16_t hci_event, uint16_t hci_ble_event,
48     uint16_t cmd_status, uint16_t reason_code);
49 
50 /**
51  * Log A2DP audio buffer underrun event
52  *
53  * @param address A2DP device associated with this event
54  * @param encoding_interval_millis encoding interval in milliseconds
55  * @param num_missing_pcm_bytes number of PCM bytes that cannot be read from
56  *                              the source
57  */
58 void LogMetricA2dpAudioUnderrunEvent(const RawAddress& address,
59                                      uint64_t encoding_interval_millis,
60                                      int num_missing_pcm_bytes);
61 
62 /**
63  * Log A2DP audio buffer overrun event
64  *
65  * @param address A2DP device associated with this event
66  * @param encoding_interval_millis encoding interval in milliseconds
67  * @param num_dropped_buffers number of encoded buffers dropped from Tx queue
68  * @param num_dropped_encoded_frames number of encoded frames dropped from Tx
69  *                                   queue
70  * @param num_dropped_encoded_bytes number of encoded bytes dropped from Tx
71  *                                  queue
72  */
73 void LogMetricA2dpAudioOverrunEvent(const RawAddress& address,
74                                     uint64_t encoding_interval_millis,
75                                     int num_dropped_buffers,
76                                     int num_dropped_encoded_frames,
77                                     int num_dropped_encoded_bytes);
78 
79 /**
80  * Log A2DP audio playback state changed event
81  *
82  * @param address A2DP device associated with this event
83  * @param playback_state A2DP audio playback state, on/off
84  * @param audio_coding_mode A2DP audio codec encoding mode, hw/sw
85  */
86 void LogMetricA2dpPlaybackEvent(const RawAddress& raw_address,
87                                 int playback_state, int audio_coding_mode);
88 
89 /**
90  * Log A2DP audio session metrics event
91  *
92  * @param address A2DP device associated with this session
93  * @param audio_duration_ms duration of the A2DP session
94  * @param media_timer_min_ms min time interval for the media timer
95  * @param media_timer_max_ms max time interval for the media timer
96  * @param media_timer_avg_ms avg time interval for the media timer
97  * @param total_scheduling_count total scheduling count
98  * @param buffer_overruns_max_count max count of Tx queue messages dropped
99                                     caused by buffer overruns
100  * @param buffer_overruns_total total count of Tx queue messages dropped
101                                 caused by buffer overruns
102  * @param buffer_underruns_average  avg number of bytes short in buffer
103                                     underruns
104  * @param buffer_underruns_count count of buffer underruns
105  * @param codec_index A2DP codec index (SBC=0, AAC=1, etc...)
106  * @param is_a2dp_offload if A2DP is offload
107  */
108 void LogMetricA2dpSessionMetricsEvent(
109     const RawAddress& address, int64_t audio_duration_ms,
110     int media_timer_min_ms, int media_timer_max_ms, int media_timer_avg_ms,
111     int total_scheduling_count, int buffer_overruns_max_count,
112     int buffer_overruns_total, float buffer_underruns_average,
113     int buffer_underruns_count, int64_t codec_index, bool is_a2dp_offload);
114 /**
115  * Log HFP audio capture packet loss statistics
116  *
117  * @param address HFP device associated with this stats
118  * @param num_decoded_frames number of decoded frames
119  * @param packet_loss_ratio ratio of packet loss frames
120  * @param codec_id codec ID of the packet (mSBC=2, LC3=3)
121  */
122 void LogMetricHfpPacketLossStats(const RawAddress& address,
123                                  int num_decoded_frames,
124                                  double packet_loss_ratio, uint16_t codec_id);
125 
126 /**
127  * Log Mmc transcode round-trip time statistics
128  *
129  * @param maximum_rtt maximum round-trip time in this session
130  * @param mean_rtt the average of round-trip time in this session
131  * @param num_requests the number of transcoding requests in the session
132  * @param codec_type codec type used in this session
133  */
134 void LogMetricMmcTranscodeRttStats(int maximum_rtt, double mean_rtt,
135                                    int num_requests, int codec_type);
136 
137 /**
138  * Log read RSSI result
139  *
140  * @param address device associated with this event
141  * @param handle connection handle of this event,
142  *               {@link kUnknownConnectionHandle} if unknown
143  * @param cmd_status command status from read RSSI command
144  * @param rssi rssi value in dBm
145  */
146 void LogMetricReadRssiResult(const RawAddress& address, uint16_t handle,
147                              uint32_t cmd_status, int8_t rssi);
148 
149 /**
150  * Log failed contact counter report
151  *
152  * @param address device associated with this event
153  * @param handle connection handle of this event,
154  *               {@link kUnknownConnectionHandle} if unknown
155  * @param cmd_status command status from read failed contact counter command
156  * @param failed_contact_counter Number of consecutive failed contacts for a
157  *                               connection corresponding to the Handle
158  */
159 void LogMetricReadFailedContactCounterResult(const RawAddress& address,
160                                              uint16_t handle,
161                                              uint32_t cmd_status,
162                                              int32_t failed_contact_counter);
163 
164 /**
165  * Log transmit power level for a particular device after read
166  *
167  * @param address device associated with this event
168  * @param handle connection handle of this event,
169  *               {@link kUnknownConnectionHandle} if unknown
170  * @param cmd_status command status from read failed contact counter command
171  * @param transmit_power_level transmit power level for connection to this
172  *                             device
173  */
174 void LogMetricReadTxPowerLevelResult(const RawAddress& address, uint16_t handle,
175                                      uint32_t cmd_status,
176                                      int32_t transmit_power_level);
177 
178 /**
179  * Logs when there is an event related to Bluetooth Security Manager Protocol
180  *
181  * @param address address of associated device
182  * @param smp_cmd SMP command code associated with this event
183  * @param direction direction of this SMP command
184  * @param smp_fail_reason SMP pairing failure reason code from SMP spec
185  */
186 void LogMetricSmpPairingEvent(const RawAddress& address, uint16_t smp_cmd,
187                               android::bluetooth::DirectionEnum direction,
188                               uint16_t smp_fail_reason);
189 
190 /**
191  * Logs there is an event related Bluetooth classic pairing
192  *
193  * @param address address of associated device
194  * @param handle connection handle of this event,
195  *               {@link kUnknownConnectionHandle} if unknown
196  * @param hci_cmd HCI command associated with this event
197  * @param hci_event HCI event associated with this event
198  * @param cmd_status Command status associated with this event
199  * @param reason_code Reason code associated with this event
200  * @param event_value A status value related to this specific event
201  */
202 void LogMetricClassicPairingEvent(const RawAddress& address, uint16_t handle,
203                                   uint32_t hci_cmd, uint16_t hci_event,
204                                   uint16_t cmd_status, uint16_t reason_code,
205                                   int64_t event_value);
206 
207 /**
208  * Logs when certain Bluetooth SDP attributes are discovered
209  *
210  * @param address address of associated device
211  * @param protocol_uuid 16 bit protocol UUID from Bluetooth Assigned Numbers
212  * @param attribute_id 16 bit attribute ID from Bluetooth Assigned Numbers
213  * @param attribute_size size of this attribute
214  * @param attribute_value pointer to the attribute data, must be larger than
215  *                        attribute_size
216  */
217 void LogMetricSdpAttribute(const RawAddress& address, uint16_t protocol_uuid,
218                            uint16_t attribute_id, size_t attribute_size,
219                            const char* attribute_value);
220 
221 /**
222  * Logs when there is a change in Bluetooth socket connection state
223  *
224  * @param address address of associated device, empty if this is a server port
225  * @param port port of this socket connection
226  * @param type type of socket
227  * @param connection_state socket connection state
228  * @param tx_bytes number of bytes transmitted
229  * @param rx_bytes number of bytes received
230  * @param server_port server port of this socket, if any. When both
231  *        |server_port| and |port| fields are populated, |port| must be spawned
232  *        by |server_port|
233  * @param socket_role role of this socket, server or connection
234  * @param uid socket owner's uid
235  */
236 void LogMetricSocketConnectionState(
237     const RawAddress& address, int port, int type,
238     android::bluetooth::SocketConnectionstateEnum connection_state,
239     int64_t tx_bytes, int64_t rx_bytes, int uid, int server_port,
240     android::bluetooth::SocketRoleEnum socket_role);
241 
242 /**
243  * Logs when a Bluetooth device's manufacturer information is learnt
244  *
245  * @param address address of associated device
246  * @param source_type where is this device info obtained from
247  * @param source_name name of the data source, internal or external
248  * @param manufacturer name of the manufacturer of this device
249  * @param model model of this device
250  * @param hardware_version hardware version of this device
251  * @param software_version software version of this device
252  */
253 void LogMetricManufacturerInfo(
254     const RawAddress& address, android::bluetooth::AddressTypeEnum address_type,
255     android::bluetooth::DeviceInfoSrcEnum source_type,
256     const std::string& source_name, const std::string& manufacturer,
257     const std::string& model, const std::string& hardware_version,
258     const std::string& software_version);
259 
260 bool CountCounterMetrics(int32_t key, int64_t count);
261 
262 void LogMetricBluetoothLEConnectionMetricEvent(
263     const RawAddress& raw_address,
264     android::bluetooth::le::LeConnectionOriginType origin_type,
265     android::bluetooth::le::LeConnectionType connection_type,
266     android::bluetooth::le::LeConnectionState transaction_state,
267     std::vector<std::pair<os::ArgumentType, int>> argument_list);
268 }  // namespace shim
269 }  // namespace bluetooth
270