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