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 
24 #include "hci/address.h"
25 
26 namespace bluetooth {
27 
28 namespace os {
29 /**
30  * Unknown connection handle for metrics purpose
31  */
32 static const uint32_t kUnknownConnectionHandle = 0xFFFF;
33 
34 /**
35  * Log link layer connection event
36  *
37  * @param address Stack wide consistent Bluetooth address of this event,
38  *                nullptr if unknown
39  * @param connection_handle connection handle of this event,
40  *                          {@link kUnknownConnectionHandle} if unknown
41  * @param direction direction of this connection
42  * @param link_type type of the link
43  * @param hci_cmd HCI command opecode associated with this event, if any
44  * @param hci_event HCI event code associated with this event, if any
45  * @param hci_ble_event HCI BLE event code associated with this event, if any
46  * @param cmd_status Command status associated with this event, if any
47  * @param reason_code Reason code associated with this event, if any
48  */
49 void LogMetricLinkLayerConnectionEvent(
50     const hci::Address* address,
51     uint32_t connection_handle,
52     android::bluetooth::DirectionEnum direction,
53     uint16_t link_type,
54     uint32_t hci_cmd,
55     uint16_t hci_event,
56     uint16_t hci_ble_event,
57     uint16_t cmd_status,
58     uint16_t reason_code);
59 
60 /**
61  * Logs when Bluetooth controller failed to reply with command status within
62  * a timeout period after receiving an HCI command from the host
63  *
64  * @param hci_cmd opcode of HCI command that caused this timeout
65  */
66 void LogMetricHciTimeoutEvent(uint32_t hci_cmd);
67 
68 /**
69  * Logs when we receive Bluetooth Read Remote Version Information Complete
70  * Event from the remote device, as documented by the Bluetooth Core HCI
71  * specification
72  *
73  * Reference: 5.0 Core Specification, Vol 2, Part E, Page 1118
74  *
75  * @param handle handle of associated ACL connection
76  * @param status HCI command status of this event
77  * @param version version code from read remote version complete event
78  * @param manufacturer_name manufacturer code from read remote version complete
79  *                          event
80  * @param subversion subversion code from read remote version complete event
81  */
82 void LogMetricRemoteVersionInfo(
83     uint16_t handle, uint8_t status, uint8_t version, uint16_t manufacturer_name, uint16_t subversion);
84 
85 /**
86  * Log A2DP audio buffer underrun event
87  *
88  * @param address A2DP device associated with this event
89  * @param encoding_interval_millis encoding interval in milliseconds
90  * @param num_missing_pcm_bytes number of PCM bytes that cannot be read from
91  *                              the source
92  */
93 void LogMetricA2dpAudioUnderrunEvent(
94     const hci::Address& address, uint64_t encoding_interval_millis, int num_missing_pcm_bytes);
95 
96 /**
97  * Log A2DP audio buffer overrun event
98  *
99  * @param address A2DP device associated with this event
100  * @param encoding_interval_millis encoding interval in milliseconds
101  * @param num_dropped_buffers number of encoded buffers dropped from Tx queue
102  * @param num_dropped_encoded_frames number of encoded frames dropped from Tx
103  *                                   queue
104  * @param num_dropped_encoded_bytes number of encoded bytes dropped from Tx
105  *                                  queue
106  */
107 void LogMetricA2dpAudioOverrunEvent(
108     const hci::Address& address,
109     uint64_t encoding_interval_millis,
110     int num_dropped_buffers,
111     int num_dropped_encoded_frames,
112     int num_dropped_encoded_bytes);
113 
114 /**
115  * Log A2DP audio playback state changed event
116  *
117  * @param address A2DP device associated with this event
118  * @param playback_state A2DP audio playback state, on/off
119  * @param audio_coding_mode A2DP audio codec encoding mode, hw/sw
120  */
121 void LogMetricA2dpPlaybackEvent(const hci::Address& address, int playback_state, int audio_coding_mode);
122 
123 /**
124  * Log read RSSI result
125  *
126  * @param address device associated with this event
127  * @param handle connection handle of this event,
128  *               {@link kUnknownConnectionHandle} if unknown
129  * @param cmd_status command status from read RSSI command
130  * @param rssi rssi value in dBm
131  */
132 void LogMetricReadRssiResult(const hci::Address& address, uint16_t handle, uint32_t cmd_status, int8_t rssi);
133 
134 /**
135  * Log failed contact counter report
136  *
137  * @param address device associated with this event
138  * @param handle connection handle of this event,
139  *               {@link kUnknownConnectionHandle} if unknown
140  * @param cmd_status command status from read failed contact counter command
141  * @param failed_contact_counter Number of consecutive failed contacts for a
142  *                               connection corresponding to the Handle
143  */
144 void LogMetricReadFailedContactCounterResult(
145     const hci::Address& address, uint16_t handle, uint32_t cmd_status, int32_t failed_contact_counter);
146 
147 /**
148  * Log transmit power level for a particular device after read
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 transmit_power_level transmit power level for connection to this
155  *                             device
156  */
157 void LogMetricReadTxPowerLevelResult(
158     const hci::Address& address, uint16_t handle, uint32_t cmd_status, int32_t transmit_power_level);
159 
160 /**
161  * Logs when there is an event related to Bluetooth Security Manager Protocol
162  *
163  * @param address address of associated device
164  * @param smp_cmd SMP command code associated with this event
165  * @param direction direction of this SMP command
166  * @param smp_fail_reason SMP pairing failure reason code from SMP spec
167  */
168 void LogMetricSmpPairingEvent(
169     const hci::Address& address, uint8_t smp_cmd, android::bluetooth::DirectionEnum direction, uint8_t smp_fail_reason);
170 
171 /**
172  * Logs there is an event related Bluetooth classic pairing
173  *
174  * @param address address of associated device
175  * @param handle connection handle of this event,
176  *               {@link kUnknownConnectionHandle} if unknown
177  * @param hci_cmd HCI command associated with this event
178  * @param hci_event HCI event associated with this event
179  * @param cmd_status Command status associated with this event
180  * @param reason_code Reason code associated with this event
181  * @param event_value A status value related to this specific event
182  */
183 void LogMetricClassicPairingEvent(
184     const hci::Address& address,
185     uint16_t handle,
186     uint32_t hci_cmd,
187     uint16_t hci_event,
188     uint16_t cmd_status,
189     uint16_t reason_code,
190     int64_t event_value);
191 
192 /**
193  * Logs when certain Bluetooth SDP attributes are discovered
194  *
195  * @param address address of associated device
196  * @param protocol_uuid 16 bit protocol UUID from Bluetooth Assigned Numbers
197  * @param attribute_id 16 bit attribute ID from Bluetooth Assigned Numbers
198  * @param attribute_size size of this attribute
199  * @param attribute_value pointer to the attribute data, must be larger than
200  *                        attribute_size
201  */
202 void LogMetricSdpAttribute(
203     const hci::Address& address,
204     uint16_t protocol_uuid,
205     uint16_t attribute_id,
206     size_t attribute_size,
207     const char* attribute_value);
208 
209 /**
210  * Logs when there is a change in Bluetooth socket connection state
211  *
212  * @param address address of associated device, empty if this is a server port
213  * @param port port of this socket connection
214  * @param type type of socket
215  * @param connection_state socket connection state
216  * @param tx_bytes number of bytes transmitted
217  * @param rx_bytes number of bytes received
218  * @param server_port server port of this socket, if any. When both
219  *        |server_port| and |port| fields are populated, |port| must be spawned
220  *        by |server_port|
221  * @param socket_role role of this socket, server or connection
222  * @param uid socket owner's uid
223  */
224 void LogMetricSocketConnectionState(
225     const hci::Address& address,
226     int port,
227     int type,
228     android::bluetooth::SocketConnectionstateEnum connection_state,
229     int64_t tx_bytes,
230     int64_t rx_bytes,
231     int uid,
232     int server_port,
233     android::bluetooth::SocketRoleEnum socket_role);
234 
235 /**
236  * Logs when a Bluetooth device's manufacturer information is learnt
237  *
238  * @param address address of associated device
239  * @param source_type where is this device info obtained from
240  * @param source_name name of the data source, internal or external
241  * @param manufacturer name of the manufacturer of this device
242  * @param model model of this device
243  * @param hardware_version hardware version of this device
244  * @param software_version software version of this device
245  */
246 void LogMetricManufacturerInfo(
247     const hci::Address& address,
248     android::bluetooth::DeviceInfoSrcEnum source_type,
249     const std::string& source_name,
250     const std::string& manufacturer,
251     const std::string& model,
252     const std::string& hardware_version,
253     const std::string& software_version);
254 
255 /**
256  * Logs when received Bluetooth HAL crash reason report.
257  *
258  * @param address current connected address.
259  * @param error_code the crash reason from bluetooth hal
260  * @param vendor_error_code the vendor crash reason from bluetooth firmware
261  */
262 void LogMetricBluetoothHalCrashReason(
263     const hci::Address& address,
264     uint32_t error_code,
265     uint32_t vendor_error_code);
266 }  // namespace os
267 
268 }  // namespace bluetooth
269