1 /******************************************************************************
2  *
3  *  Copyright 2021 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #pragma once
20 
21 #include <frameworks/proto_logging/stats/enums/bluetooth/enums.pb.h>
22 #include <frameworks/proto_logging/stats/enums/bluetooth/hci/enums.pb.h>
23 #include <frameworks/proto_logging/stats/enums/bluetooth/le/enums.pb.h>
24 
25 #include <vector>
26 
27 #include "hci/address.h"
28 
29 namespace bluetooth {
30 
31 namespace os {
32 /**
33  * Unknown connection handle for metrics purpose
34  */
35 static const uint32_t kUnknownConnectionHandle = 0xFFFF;
36 
37 /**
38  * Log link layer connection event
39  *
40  * @param address Stack wide consistent Bluetooth address of this event,
41  *                nullptr if unknown
42  * @param connection_handle connection handle of this event,
43  *                          {@link kUnknownConnectionHandle} if unknown
44  * @param direction direction of this connection
45  * @param link_type type of the link
46  * @param hci_cmd HCI command opecode associated with this event, if any
47  * @param hci_event HCI event code associated with this event, if any
48  * @param hci_ble_event HCI BLE event code associated with this event, if any
49  * @param cmd_status Command status associated with this event, if any
50  * @param reason_code Reason code associated with this event, if any
51  */
52 void LogMetricLinkLayerConnectionEvent(const hci::Address* address, uint32_t connection_handle,
53                                        android::bluetooth::DirectionEnum direction,
54                                        uint16_t link_type, uint32_t hci_cmd, uint16_t hci_event,
55                                        uint16_t hci_ble_event, uint16_t cmd_status,
56                                        uint16_t reason_code);
57 
58 /**
59  * Logs when Bluetooth controller failed to reply with command status within
60  * a timeout period after receiving an HCI command from the host
61  *
62  * @param hci_cmd opcode of HCI command that caused this timeout
63  */
64 void LogMetricHciTimeoutEvent(uint32_t hci_cmd);
65 
66 /**
67  * Logs when we receive Bluetooth Read Remote Version Information Complete
68  * Event from the remote device, as documented by the Bluetooth Core HCI
69  * specification
70  *
71  * Reference: 5.0 Core Specification, Vol 2, Part E, Page 1118
72  *
73  * @param handle handle of associated ACL connection
74  * @param status HCI command status of this event
75  * @param version version code from read remote version complete event
76  * @param manufacturer_name manufacturer code from read remote version complete
77  *                          event
78  * @param subversion subversion code from read remote version complete event
79  */
80 void LogMetricRemoteVersionInfo(uint16_t handle, uint8_t status, uint8_t version,
81                                 uint16_t manufacturer_name, uint16_t subversion);
82 
83 /**
84  * Log A2DP audio buffer underrun event
85  *
86  * @param address A2DP device associated with this event
87  * @param encoding_interval_millis encoding interval in milliseconds
88  * @param num_missing_pcm_bytes number of PCM bytes that cannot be read from
89  *                              the source
90  */
91 void LogMetricA2dpAudioUnderrunEvent(const hci::Address& address, uint64_t encoding_interval_millis,
92                                      int num_missing_pcm_bytes);
93 
94 /**
95  * Log A2DP audio buffer overrun event
96  *
97  * @param address A2DP device associated with this event
98  * @param encoding_interval_millis encoding interval in milliseconds
99  * @param num_dropped_buffers number of encoded buffers dropped from Tx queue
100  * @param num_dropped_encoded_frames number of encoded frames dropped from Tx
101  *                                   queue
102  * @param num_dropped_encoded_bytes number of encoded bytes dropped from Tx
103  *                                  queue
104  */
105 void LogMetricA2dpAudioOverrunEvent(const hci::Address& address, uint64_t encoding_interval_millis,
106                                     int num_dropped_buffers, int num_dropped_encoded_frames,
107                                     int num_dropped_encoded_bytes);
108 
109 /**
110  * Log A2DP audio playback state changed event
111  *
112  * @param address A2DP device associated with this event
113  * @param playback_state A2DP audio playback state, on/off
114  * @param audio_coding_mode A2DP audio codec encoding mode, hw/sw
115  */
116 void LogMetricA2dpPlaybackEvent(const hci::Address& address, int playback_state,
117                                 int audio_coding_mode);
118 
119 /**
120  * Log A2DP audio session metrics
121  *
122  * @param address A2DP device associated with this session
123  * @param audio_duration_ms duration of the A2DP session
124  * @param media_timer_min_ms min time interval for the media timer
125  * @param media_timer_max_ms max time interval for the media timer
126  * @param media_timer_avg_ms avg time interval for the media timer
127  * @param total_scheduling_count total scheduling count
128  * @param buffer_overruns_max_count max count of Tx queue messages dropped
129                                     caused by buffer overruns
130  * @param buffer_overruns_total total count of Tx queue messages dropped
131                                 caused by buffer overruns
132  * @param buffer_underruns_average  avg number of bytes short in buffer
133                                     underruns
134  * @param buffer_underruns_count count of buffer underruns
135  * @param codec_index A2DP codec index (SBC=0, AAC=1, etc...)
136  * @param is_a2dp_offload if A2DP is offload
137  */
138 void LogMetricA2dpSessionMetricsEvent(const hci::Address& address, int64_t audio_duration_ms,
139                                       int media_timer_min_ms, int media_timer_max_ms,
140                                       int media_timer_avg_ms, int total_scheduling_count,
141                                       int buffer_overruns_max_count, int buffer_overruns_total,
142                                       float buffer_underruns_average, int buffer_underruns_count,
143                                       int64_t codec_index, bool is_a2dp_offload);
144 
145 /**
146  * Log HFP audio capture packet loss statistics
147  *
148  * @param address HFP device associated with this stats
149  * @param num_decoded_frames number of decoded frames
150  * @param packet_loss_ratio ratio of packet loss frames
151  * @param codec_id codec ID of the packet (mSBC=2, LC3=3)
152  */
153 void LogMetricHfpPacketLossStats(const hci::Address& address, int num_decoded_frames,
154                                  double packet_loss_ratio, uint16_t codec_id);
155 
156 /**
157  * Log Mmc transcode round-trip time statistics
158  *
159  * @param maximum_rtt maximum round-trip time in this session
160  * @param mean_rtt the average of round-trip time in this session
161  * @param num_requests the number of transcoding requests in the session
162  * @param codec_type codec type used in this session
163  */
164 void LogMetricMmcTranscodeRttStats(int maximum_rtt, double mean_rtt, int num_requests,
165                                    int codec_type);
166 
167 /**
168  * Log read RSSI result
169  *
170  * @param address device associated with this event
171  * @param handle connection handle of this event,
172  *               {@link kUnknownConnectionHandle} if unknown
173  * @param cmd_status command status from read RSSI command
174  * @param rssi rssi value in dBm
175  */
176 void LogMetricReadRssiResult(const hci::Address& address, uint16_t handle, uint32_t cmd_status,
177                              int8_t rssi);
178 
179 /**
180  * Log failed contact counter report
181  *
182  * @param address device associated with this event
183  * @param handle connection handle of this event,
184  *               {@link kUnknownConnectionHandle} if unknown
185  * @param cmd_status command status from read failed contact counter command
186  * @param failed_contact_counter Number of consecutive failed contacts for a
187  *                               connection corresponding to the Handle
188  */
189 void LogMetricReadFailedContactCounterResult(const hci::Address& address, uint16_t handle,
190                                              uint32_t cmd_status, int32_t failed_contact_counter);
191 
192 /**
193  * Log transmit power level for a particular device after read
194  *
195  * @param address device associated with this event
196  * @param handle connection handle of this event,
197  *               {@link kUnknownConnectionHandle} if unknown
198  * @param cmd_status command status from read failed contact counter command
199  * @param transmit_power_level transmit power level for connection to this
200  *                             device
201  */
202 void LogMetricReadTxPowerLevelResult(const hci::Address& address, uint16_t handle,
203                                      uint32_t cmd_status, int32_t transmit_power_level);
204 
205 /**
206  * Logs when there is an event related to Bluetooth Security Manager Protocol
207  *
208  * @param address address of associated device
209  * @param smp_cmd SMP command code associated with this event
210  * @param direction direction of this SMP command
211  * @param smp_fail_reason SMP pairing failure reason code from SMP spec
212  */
213 void LogMetricSmpPairingEvent(const hci::Address& address, uint16_t smp_cmd,
214                               android::bluetooth::DirectionEnum direction,
215                               uint16_t smp_fail_reason);
216 
217 /**
218  * Logs there is an event related Bluetooth classic pairing
219  *
220  * @param address address of associated device
221  * @param handle connection handle of this event,
222  *               {@link kUnknownConnectionHandle} if unknown
223  * @param hci_cmd HCI command associated with this event
224  * @param hci_event HCI event associated with this event
225  * @param cmd_status Command status associated with this event
226  * @param reason_code Reason code associated with this event
227  * @param event_value A status value related to this specific event
228  */
229 void LogMetricClassicPairingEvent(const hci::Address& address, uint16_t handle, uint32_t hci_cmd,
230                                   uint16_t hci_event, uint16_t cmd_status, uint16_t reason_code,
231                                   int64_t event_value);
232 
233 /**
234  * Logs when certain Bluetooth SDP attributes are discovered
235  *
236  * @param address address of associated device
237  * @param protocol_uuid 16 bit protocol UUID from Bluetooth Assigned Numbers
238  * @param attribute_id 16 bit attribute ID from Bluetooth Assigned Numbers
239  * @param attribute_size size of this attribute
240  * @param attribute_value pointer to the attribute data, must be larger than
241  *                        attribute_size
242  */
243 void LogMetricSdpAttribute(const hci::Address& address, uint16_t protocol_uuid,
244                            uint16_t attribute_id, size_t attribute_size,
245                            const char* attribute_value);
246 
247 /**
248  * Logs when there is a change in Bluetooth socket connection state
249  *
250  * @param address address of associated device, empty if this is a server port
251  * @param port port of this socket connection
252  * @param type type of socket
253  * @param connection_state socket connection state
254  * @param tx_bytes number of bytes transmitted
255  * @param rx_bytes number of bytes received
256  * @param server_port server port of this socket, if any. When both
257  *        |server_port| and |port| fields are populated, |port| must be spawned
258  *        by |server_port|
259  * @param socket_role role of this socket, server or connection
260  * @param uid socket owner's uid
261  */
262 void LogMetricSocketConnectionState(const hci::Address& address, int port, int type,
263                                     android::bluetooth::SocketConnectionstateEnum connection_state,
264                                     int64_t tx_bytes, int64_t rx_bytes, int uid, int server_port,
265                                     android::bluetooth::SocketRoleEnum socket_role);
266 
267 /**
268  * Logs when a Bluetooth device's manufacturer information is learnt
269  *
270  * @param address address of associated device
271  * @param source_type where is this device info obtained from
272  * @param source_name name of the data source, internal or external
273  * @param manufacturer name of the manufacturer of this device
274  * @param model model of this device
275  * @param hardware_version hardware version of this device
276  * @param software_version software version of this device
277  */
278 void LogMetricManufacturerInfo(const hci::Address& address,
279                                android::bluetooth::AddressTypeEnum address_type,
280                                android::bluetooth::DeviceInfoSrcEnum source_type,
281                                const std::string& source_name, const std::string& manufacturer,
282                                const std::string& model, const std::string& hardware_version,
283                                const std::string& software_version);
284 
285 /**
286  * Logs when received Bluetooth HAL crash reason report.
287  *
288  * @param address current connected address.
289  * @param error_code the crash reason from bluetooth hal
290  * @param vendor_error_code the vendor crash reason from bluetooth firmware
291  */
292 void LogMetricBluetoothHalCrashReason(const hci::Address& address, uint32_t error_code,
293                                       uint32_t vendor_error_code);
294 
295 void LogMetricBluetoothLocalSupportedFeatures(uint32_t page_num, uint64_t features);
296 
297 void LogMetricBluetoothLocalVersions(uint32_t lmp_manufacturer_name, uint8_t lmp_version,
298                                      uint32_t lmp_subversion, uint8_t hci_version,
299                                      uint32_t hci_revision);
300 
301 void LogMetricBluetoothDisconnectionReasonReported(uint32_t reason, const hci::Address& address,
302                                                    uint32_t connection_handle);
303 
304 void LogMetricBluetoothRemoteSupportedFeatures(const hci::Address& address, uint32_t page,
305                                                uint64_t features, uint32_t connection_handle);
306 
307 void LogMetricBluetoothCodePathCounterMetrics(int32_t key, int64_t count);
308 
309 using android::bluetooth::le::LeAclConnectionState;
310 using android::bluetooth::le::LeConnectionOriginType;
311 using android::bluetooth::le::LeConnectionState;
312 using android::bluetooth::le::LeConnectionType;
313 // Adding options
314 struct LEConnectionSessionOptions {
315   // Contains the state of the LE-ACL Connection
316   LeAclConnectionState acl_connection_state = LeAclConnectionState::LE_ACL_UNSPECIFIED;
317   // Origin of the transaction
318   LeConnectionOriginType origin_type = LeConnectionOriginType::ORIGIN_UNSPECIFIED;
319   // Connection Type
320   LeConnectionType transaction_type = LeConnectionType::CONNECTION_TYPE_UNSPECIFIED;
321   // Transaction State
322   LeConnectionState transaction_state = LeConnectionState::STATE_UNSPECIFIED;
323   // Latency of the entire transaction
324   int64_t latency = 0;
325   // Address of the remote device
326   hci::Address remote_address = hci::Address::kEmpty;
327   // UID associated with the device
328   int app_uid = 0;
329   // Latency of the ACL Transaction
330   int64_t acl_latency = 0;
331   // Contains the error code associated with the ACL Connection if failed
332   android::bluetooth::hci::StatusEnum status = android::bluetooth::hci::StatusEnum::STATUS_UNKNOWN;
333   // Cancelled connection
334   bool is_cancelled = false;
335 };
336 
337 // Upload LE Session
338 void LogMetricBluetoothLEConnection(os::LEConnectionSessionOptions session_options);
339 
340 /**
341  * Logs a Bluetooth Event
342  *
343  * @param address address of associated device
344  * @param event_type type of event where this is getting logged from
345  * @param state state associated with the event
346  */
347 void LogMetricBluetoothEvent(const hci::Address& address, android::bluetooth::EventType event_type,
348                              android::bluetooth::State state);
349 }  // namespace os
350    //
351 }  // namespace bluetooth
352