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 #include <frameworks/proto_logging/stats/enums/bluetooth/enums.pb.h>
17 #include <frameworks/proto_logging/stats/enums/bluetooth/hci/enums.pb.h>
18
19 #include "common/strings.h"
20 #include "hci/hci_metrics_logging.h"
21 #include "os/metrics.h"
22 #include "storage/device.h"
23
24 namespace bluetooth {
25 namespace hci {
26
log_hci_event(std::unique_ptr<CommandView> & command_view,EventView event_view,storage::StorageModule * storage_module)27 void log_hci_event(
28 std::unique_ptr<CommandView>& command_view, EventView event_view, storage::StorageModule* storage_module) {
29 ASSERT(event_view.IsValid());
30 EventCode event_code = event_view.GetEventCode();
31 switch (event_code) {
32 case EventCode::COMMAND_COMPLETE: {
33 ASSERT(command_view->IsValid());
34 log_link_layer_connection_command_complete(event_view, command_view);
35 log_classic_pairing_command_complete(event_view, command_view);
36 break;
37 }
38 case EventCode::COMMAND_STATUS: {
39 ASSERT(command_view->IsValid());
40 CommandStatusView response_view = CommandStatusView::Create(event_view);
41 ASSERT(response_view.IsValid());
42 log_link_layer_connection_command_status(command_view, response_view.GetStatus());
43 log_classic_pairing_command_status(command_view, response_view.GetStatus());
44 break;
45 }
46 case EventCode::LE_META_EVENT: {
47 LeMetaEventView le_meta_event_view = LeMetaEventView::Create(event_view);
48 ASSERT(le_meta_event_view.IsValid());
49 log_link_layer_connection_event_le_meta(le_meta_event_view);
50 break;
51 }
52 default:
53 log_link_layer_connection_other_hci_event(event_view, storage_module);
54 log_classic_pairing_other_hci_event(event_view);
55 }
56 }
log_link_layer_connection_command_status(std::unique_ptr<CommandView> & command_view,ErrorCode status)57 void log_link_layer_connection_command_status(std::unique_ptr<CommandView>& command_view, ErrorCode status) {
58 // get op_code
59 ASSERT(command_view->IsValid());
60 OpCode op_code = command_view->GetOpCode();
61
62 // init parameters to log
63 Address address = Address::kEmpty;
64 uint32_t connection_handle = bluetooth::os::kUnknownConnectionHandle;
65 uint16_t reason = static_cast<uint16_t>(ErrorCode::UNKNOWN_HCI_COMMAND);
66 static uint16_t kUnknownBleEvt = android::bluetooth::hci::BLE_EVT_UNKNOWN;
67 uint16_t event_code = android::bluetooth::hci::EVT_COMMAND_STATUS;
68 android::bluetooth::DirectionEnum direction = android::bluetooth::DIRECTION_UNKNOWN;
69 uint16_t link_type = android::bluetooth::LINK_TYPE_UNKNOWN;
70
71 // get ConnectionManagementCommandView
72 ConnectionManagementCommandView connection_management_command_view =
73 ConnectionManagementCommandView::Create(AclCommandView::Create(*command_view));
74 ASSERT(connection_management_command_view.IsValid());
75 switch (op_code) {
76 case OpCode::CREATE_CONNECTION: {
77 auto create_connection_view = CreateConnectionView::Create(std::move(connection_management_command_view));
78 ASSERT(create_connection_view.IsValid());
79 address = create_connection_view.GetBdAddr();
80 direction = android::bluetooth::DIRECTION_OUTGOING;
81 link_type = android::bluetooth::LINK_TYPE_ACL;
82 break;
83 }
84 case OpCode::CREATE_CONNECTION_CANCEL: {
85 auto create_connection_cancel_view =
86 CreateConnectionCancelView::Create(std::move(connection_management_command_view));
87 ASSERT(create_connection_cancel_view.IsValid());
88 address = create_connection_cancel_view.GetBdAddr();
89 direction = android::bluetooth::DIRECTION_OUTGOING;
90 link_type = android::bluetooth::LINK_TYPE_ACL;
91 break;
92 }
93 case OpCode::DISCONNECT: {
94 auto disconnect_view = DisconnectView::Create(std::move(connection_management_command_view));
95 ASSERT(disconnect_view.IsValid());
96 connection_handle = disconnect_view.GetConnectionHandle();
97 reason = static_cast<uint16_t>(disconnect_view.GetReason());
98 break;
99 }
100 case OpCode::SETUP_SYNCHRONOUS_CONNECTION: {
101 auto setup_synchronous_connection_view = SetupSynchronousConnectionView::Create(
102 ScoConnectionCommandView::Create(std::move(connection_management_command_view)));
103 ASSERT(setup_synchronous_connection_view.IsValid());
104 connection_handle = setup_synchronous_connection_view.GetConnectionHandle();
105 direction = android::bluetooth::DIRECTION_OUTGOING;
106 break;
107 }
108 case OpCode::ENHANCED_SETUP_SYNCHRONOUS_CONNECTION: {
109 auto enhanced_setup_synchronous_connection_view = EnhancedSetupSynchronousConnectionView::Create(
110 ScoConnectionCommandView::Create(std::move(connection_management_command_view)));
111 ASSERT(enhanced_setup_synchronous_connection_view.IsValid());
112 connection_handle = enhanced_setup_synchronous_connection_view.GetConnectionHandle();
113 direction = android::bluetooth::DIRECTION_OUTGOING;
114 break;
115 }
116 case OpCode::ACCEPT_CONNECTION_REQUEST: {
117 auto accept_connection_request_view =
118 AcceptConnectionRequestView::Create(std::move(connection_management_command_view));
119 ASSERT(accept_connection_request_view.IsValid());
120 address = accept_connection_request_view.GetBdAddr();
121 direction = android::bluetooth::DIRECTION_INCOMING;
122 break;
123 }
124 case OpCode::ACCEPT_SYNCHRONOUS_CONNECTION: {
125 auto accept_synchronous_connection_view = AcceptSynchronousConnectionView::Create(
126 ScoConnectionCommandView::Create(std::move(connection_management_command_view)));
127 ASSERT(accept_synchronous_connection_view.IsValid());
128 address = accept_synchronous_connection_view.GetBdAddr();
129 direction = android::bluetooth::DIRECTION_INCOMING;
130 break;
131 }
132 case OpCode::ENHANCED_ACCEPT_SYNCHRONOUS_CONNECTION: {
133 auto enhanced_accept_synchronous_connection_view = EnhancedAcceptSynchronousConnectionView::Create(
134 ScoConnectionCommandView::Create(std::move(connection_management_command_view)));
135 ASSERT(enhanced_accept_synchronous_connection_view.IsValid());
136 address = enhanced_accept_synchronous_connection_view.GetBdAddr();
137 direction = android::bluetooth::DIRECTION_INCOMING;
138 break;
139 }
140 case OpCode::REJECT_CONNECTION_REQUEST: {
141 auto reject_connection_request_view =
142 RejectConnectionRequestView::Create(std::move(connection_management_command_view));
143 ASSERT(reject_connection_request_view.IsValid());
144 address = reject_connection_request_view.GetBdAddr();
145 reason = static_cast<uint16_t>(reject_connection_request_view.GetReason());
146 direction = android::bluetooth::DIRECTION_INCOMING;
147 break;
148 }
149 case OpCode::REJECT_SYNCHRONOUS_CONNECTION: {
150 auto reject_synchronous_connection_view = RejectSynchronousConnectionView::Create(
151 ScoConnectionCommandView::Create(std::move(connection_management_command_view)));
152 ASSERT(reject_synchronous_connection_view.IsValid());
153 address = reject_synchronous_connection_view.GetBdAddr();
154 reason = static_cast<uint16_t>(reject_synchronous_connection_view.GetReason());
155 direction = android::bluetooth::DIRECTION_INCOMING;
156 break;
157 }
158 case OpCode::LE_CREATE_CONNECTION: {
159 auto le_create_connection_view = LeCreateConnectionView::Create(
160 LeConnectionManagementCommandView::Create(std::move(connection_management_command_view)));
161 ASSERT(le_create_connection_view.IsValid());
162 uint8_t initiator_filter_policy = static_cast<uint8_t>(le_create_connection_view.GetInitiatorFilterPolicy());
163 if (initiator_filter_policy != 0x00 && status == ErrorCode::SUCCESS) {
164 return;
165 }
166 address = le_create_connection_view.GetPeerAddress();
167 direction = android::bluetooth::DIRECTION_INCOMING;
168 link_type = android::bluetooth::LINK_TYPE_ACL;
169 break;
170 }
171 case OpCode::LE_EXTENDED_CREATE_CONNECTION: {
172 auto le_extended_create_connection_view = LeExtendedCreateConnectionView::Create(
173 LeConnectionManagementCommandView::Create(std::move(connection_management_command_view)));
174 ASSERT(le_extended_create_connection_view.IsValid());
175 uint8_t initiator_filter_policy =
176 static_cast<uint8_t>(le_extended_create_connection_view.GetInitiatorFilterPolicy());
177 if (initiator_filter_policy != 0x00 && status == ErrorCode::SUCCESS) {
178 return;
179 }
180 address = le_extended_create_connection_view.GetPeerAddress();
181 direction = android::bluetooth::DIRECTION_OUTGOING;
182 link_type = android::bluetooth::LINK_TYPE_ACL;
183 break;
184 }
185 case OpCode::LE_CREATE_CONNECTION_CANCEL: {
186 auto le_create_connection_cancel_view = LeCreateConnectionCancelView::Create(
187 LeConnectionManagementCommandView::Create(std::move(connection_management_command_view)));
188 ASSERT(le_create_connection_cancel_view.IsValid());
189 if (status == ErrorCode::SUCCESS) {
190 return;
191 }
192 direction = android::bluetooth::DIRECTION_OUTGOING;
193 link_type = android::bluetooth::LINK_TYPE_ACL;
194 break;
195 }
196 case OpCode::LE_CLEAR_CONNECT_LIST: {
197 auto le_clear_connect_list_view = LeClearConnectListView::Create(
198 LeConnectionManagementCommandView::Create(std::move(connection_management_command_view)));
199 ASSERT(le_clear_connect_list_view.IsValid());
200 direction = android::bluetooth::DIRECTION_INCOMING;
201 link_type = android::bluetooth::LINK_TYPE_ACL;
202 break;
203 }
204 case OpCode::LE_ADD_DEVICE_TO_CONNECT_LIST: {
205 auto le_add_device_to_connect_list_view = LeAddDeviceToConnectListView::Create(
206 LeConnectionManagementCommandView::Create(std::move(connection_management_command_view)));
207 ASSERT(le_add_device_to_connect_list_view.IsValid());
208 address = le_add_device_to_connect_list_view.GetAddress();
209 direction = android::bluetooth::DIRECTION_INCOMING;
210 link_type = android::bluetooth::LINK_TYPE_ACL;
211 break;
212 }
213 case OpCode::LE_REMOVE_DEVICE_FROM_CONNECT_LIST: {
214 auto le_remove_device_from_connect_list_view = LeRemoveDeviceFromConnectListView::Create(
215 LeConnectionManagementCommandView::Create(std::move(connection_management_command_view)));
216 ASSERT(le_remove_device_from_connect_list_view.IsValid());
217 address = le_remove_device_from_connect_list_view.GetAddress();
218 direction = android::bluetooth::DIRECTION_INCOMING;
219 link_type = android::bluetooth::LINK_TYPE_ACL;
220 break;
221 }
222 default:
223 return;
224 }
225 os::LogMetricLinkLayerConnectionEvent(
226 &address,
227 connection_handle,
228 direction,
229 link_type,
230 static_cast<uint32_t>(op_code),
231 static_cast<uint16_t>(event_code),
232 kUnknownBleEvt,
233 static_cast<uint16_t>(status),
234 static_cast<uint16_t>(reason));
235 }
236
log_link_layer_connection_command_complete(EventView event_view,std::unique_ptr<CommandView> & command_view)237 void log_link_layer_connection_command_complete(EventView event_view, std::unique_ptr<CommandView>& command_view) {
238 CommandCompleteView command_complete_view = CommandCompleteView::Create(std::move(event_view));
239 ASSERT(command_complete_view.IsValid());
240 OpCode op_code = command_complete_view.GetCommandOpCode();
241
242 // init parameters to log
243 Address address = Address::kEmpty;
244 uint32_t connection_handle = bluetooth::os::kUnknownConnectionHandle;
245 ErrorCode status = ErrorCode::UNKNOWN_HCI_COMMAND;
246 ErrorCode reason = ErrorCode::UNKNOWN_HCI_COMMAND;
247 static uint16_t kUnknownBleEvt = android::bluetooth::hci::BLE_EVT_UNKNOWN;
248 uint16_t event_code = android::bluetooth::hci::EVT_COMMAND_COMPLETE;
249 android::bluetooth::DirectionEnum direction = android::bluetooth::DIRECTION_UNKNOWN;
250 uint16_t link_type = android::bluetooth::LINK_TYPE_UNKNOWN;
251
252 // get ConnectionManagementCommandView
253 ConnectionManagementCommandView connection_management_command_view =
254 ConnectionManagementCommandView::Create(AclCommandView::Create(*command_view));
255 ASSERT(connection_management_command_view.IsValid());
256
257 switch (op_code) {
258 case OpCode::LE_CLEAR_CONNECT_LIST: {
259 auto le_clear_connect_list_view = LeClearConnectListView::Create(
260 LeConnectionManagementCommandView::Create(std::move(connection_management_command_view)));
261 ASSERT(le_clear_connect_list_view.IsValid());
262 direction = android::bluetooth::DIRECTION_INCOMING;
263 link_type = android::bluetooth::LINK_TYPE_ACL;
264 break;
265 }
266 case OpCode::LE_ADD_DEVICE_TO_CONNECT_LIST: {
267 auto le_add_device_to_connect_list_view = LeAddDeviceToConnectListView::Create(
268 LeConnectionManagementCommandView::Create(std::move(connection_management_command_view)));
269 ASSERT(le_add_device_to_connect_list_view.IsValid());
270 address = le_add_device_to_connect_list_view.GetAddress();
271 direction = android::bluetooth::DIRECTION_INCOMING;
272 link_type = android::bluetooth::LINK_TYPE_ACL;
273 break;
274 }
275 case OpCode::LE_REMOVE_DEVICE_FROM_CONNECT_LIST: {
276 auto le_remove_device_from_connect_list_view = LeRemoveDeviceFromConnectListView::Create(
277 LeConnectionManagementCommandView::Create(std::move(connection_management_command_view)));
278 ASSERT(le_remove_device_from_connect_list_view.IsValid());
279 address = le_remove_device_from_connect_list_view.GetAddress();
280 direction = android::bluetooth::DIRECTION_INCOMING;
281 link_type = android::bluetooth::LINK_TYPE_ACL;
282 break;
283 }
284 case OpCode::CREATE_CONNECTION_CANCEL: {
285 auto create_connection_cancel_complete_view =
286 CreateConnectionCancelCompleteView::Create(std::move(command_complete_view));
287 ASSERT(create_connection_cancel_complete_view.IsValid());
288 address = create_connection_cancel_complete_view.GetBdAddr();
289 direction = android::bluetooth::DIRECTION_OUTGOING;
290 link_type = android::bluetooth::LINK_TYPE_ACL;
291 status = create_connection_cancel_complete_view.GetStatus();
292 break;
293 }
294 case OpCode::LE_CREATE_CONNECTION_CANCEL: {
295 auto le_create_connection_cancel_complete_view =
296 LeCreateConnectionCancelCompleteView::Create(std::move(command_complete_view));
297 ASSERT(le_create_connection_cancel_complete_view.IsValid());
298 direction = android::bluetooth::DIRECTION_OUTGOING;
299 link_type = android::bluetooth::LINK_TYPE_ACL;
300 status = le_create_connection_cancel_complete_view.GetStatus();
301 break;
302 }
303 default:
304 return;
305 }
306 os::LogMetricLinkLayerConnectionEvent(
307 &address,
308 connection_handle,
309 direction,
310 link_type,
311 static_cast<uint32_t>(op_code),
312 static_cast<uint16_t>(event_code),
313 kUnknownBleEvt,
314 static_cast<uint16_t>(status),
315 static_cast<uint16_t>(reason));
316 }
317
log_link_layer_connection_other_hci_event(EventView packet,storage::StorageModule * storage_module)318 void log_link_layer_connection_other_hci_event(EventView packet, storage::StorageModule* storage_module) {
319 EventCode event_code = packet.GetEventCode();
320 Address address = Address::kEmpty;
321 uint32_t connection_handle = bluetooth::os::kUnknownConnectionHandle;
322 android::bluetooth::DirectionEnum direction = android::bluetooth::DIRECTION_UNKNOWN;
323 uint16_t link_type = android::bluetooth::LINK_TYPE_UNKNOWN;
324 ErrorCode status = ErrorCode::UNKNOWN_HCI_COMMAND;
325 ErrorCode reason = ErrorCode::UNKNOWN_HCI_COMMAND;
326 uint32_t cmd = android::bluetooth::hci::CMD_UNKNOWN;
327 switch (event_code) {
328 case EventCode::CONNECTION_COMPLETE: {
329 auto connection_complete_view = ConnectionCompleteView::Create(std::move(packet));
330 ASSERT(connection_complete_view.IsValid());
331 address = connection_complete_view.GetBdAddr();
332 connection_handle = connection_complete_view.GetConnectionHandle();
333 link_type = static_cast<uint16_t>(connection_complete_view.GetLinkType());
334 status = connection_complete_view.GetStatus();
335
336 // besides log link layer connection events, also log remote device manufacturer info
337 log_remote_device_information(address, connection_handle, status, storage_module);
338 break;
339 }
340 case EventCode::CONNECTION_REQUEST: {
341 auto connection_request_view = ConnectionRequestView::Create(std::move(packet));
342 ASSERT(connection_request_view.IsValid());
343 address = connection_request_view.GetBdAddr();
344 link_type = static_cast<uint16_t>(connection_request_view.GetLinkType());
345 direction = android::bluetooth::DIRECTION_INCOMING;
346 break;
347 }
348 case EventCode::DISCONNECTION_COMPLETE: {
349 auto disconnection_complete_view = DisconnectionCompleteView::Create(std::move(packet));
350 ASSERT(disconnection_complete_view.IsValid());
351 status = disconnection_complete_view.GetStatus();
352 connection_handle = disconnection_complete_view.GetConnectionHandle();
353 reason = disconnection_complete_view.GetReason();
354 break;
355 }
356 case EventCode::SYNCHRONOUS_CONNECTION_COMPLETE: {
357 auto synchronous_connection_complete_view = SynchronousConnectionCompleteView::Create(std::move(packet));
358 ASSERT(synchronous_connection_complete_view.IsValid());
359 connection_handle = synchronous_connection_complete_view.GetConnectionHandle();
360 address = synchronous_connection_complete_view.GetBdAddr();
361 link_type = static_cast<uint16_t>(synchronous_connection_complete_view.GetLinkType());
362 status = synchronous_connection_complete_view.GetStatus();
363 break;
364 }
365 case EventCode::SYNCHRONOUS_CONNECTION_CHANGED: {
366 auto synchronous_connection_changed_view = SynchronousConnectionChangedView::Create(std::move(packet));
367 ASSERT(synchronous_connection_changed_view.IsValid());
368 status = synchronous_connection_changed_view.GetStatus();
369 connection_handle = synchronous_connection_changed_view.GetConnectionHandle();
370 break;
371 }
372 default:
373 return;
374 }
375 os::LogMetricLinkLayerConnectionEvent(
376 &address,
377 connection_handle,
378 direction,
379 link_type,
380 static_cast<uint32_t>(cmd),
381 static_cast<uint16_t>(event_code),
382 android::bluetooth::hci::BLE_EVT_UNKNOWN,
383 static_cast<uint16_t>(status),
384 static_cast<uint16_t>(reason));
385 }
386
log_link_layer_connection_event_le_meta(LeMetaEventView le_meta_event_view)387 void log_link_layer_connection_event_le_meta(LeMetaEventView le_meta_event_view) {
388 SubeventCode leEvt = le_meta_event_view.GetSubeventCode();
389 auto le_connection_complete_view = LeConnectionCompleteView::Create(std::move(le_meta_event_view));
390 if (!le_connection_complete_view.IsValid()) {
391 // function is called for all le meta events. Only need to process le connection complete.
392 return;
393 }
394 ASSERT(le_connection_complete_view.IsValid());
395 // init parameters to log
396 EventCode event_code = EventCode::LE_META_EVENT;
397 Address address = le_connection_complete_view.GetPeerAddress();
398 uint32_t connection_handle = le_connection_complete_view.GetConnectionHandle();
399 android::bluetooth::DirectionEnum direction = android::bluetooth::DIRECTION_UNKNOWN;
400 uint16_t link_type = android::bluetooth::LINK_TYPE_ACL;
401 ErrorCode status = le_connection_complete_view.GetStatus();
402 ErrorCode reason = ErrorCode::UNKNOWN_HCI_COMMAND;
403 uint32_t cmd = android::bluetooth::hci::CMD_UNKNOWN;
404
405 os::LogMetricLinkLayerConnectionEvent(
406 &address,
407 connection_handle,
408 direction,
409 link_type,
410 static_cast<uint32_t>(cmd),
411 static_cast<uint16_t>(event_code),
412 static_cast<uint16_t>(leEvt),
413 static_cast<uint16_t>(status),
414 static_cast<uint16_t>(reason));
415 }
416
log_classic_pairing_other_hci_event(EventView packet)417 void log_classic_pairing_other_hci_event(EventView packet) {
418 EventCode event_code = packet.GetEventCode();
419 Address address = Address::kEmpty;
420 uint32_t cmd = android::bluetooth::hci::CMD_UNKNOWN;
421 ErrorCode status = ErrorCode::UNKNOWN_HCI_COMMAND;
422 ErrorCode reason = ErrorCode::UNKNOWN_HCI_COMMAND;
423 uint32_t connection_handle = bluetooth::os::kUnknownConnectionHandle;
424 int64_t value = 0;
425
426 switch (event_code) {
427 case EventCode::IO_CAPABILITY_REQUEST: {
428 IoCapabilityRequestView io_capability_request_view = IoCapabilityRequestView::Create(std::move(packet));
429 ASSERT(io_capability_request_view.IsValid());
430 address = io_capability_request_view.GetBdAddr();
431 break;
432 }
433 case EventCode::IO_CAPABILITY_RESPONSE: {
434 IoCapabilityResponseView io_capability_response_view = IoCapabilityResponseView::Create(std::move(packet));
435 ASSERT(io_capability_response_view.IsValid());
436 address = io_capability_response_view.GetBdAddr();
437 break;
438 }
439 case EventCode::LINK_KEY_REQUEST: {
440 LinkKeyRequestView link_key_request_view = LinkKeyRequestView::Create(std::move(packet));
441 ASSERT(link_key_request_view.IsValid());
442 address = link_key_request_view.GetBdAddr();
443 break;
444 }
445 case EventCode::LINK_KEY_NOTIFICATION: {
446 LinkKeyNotificationView link_key_notification_view = LinkKeyNotificationView::Create(std::move(packet));
447 ASSERT(link_key_notification_view.IsValid());
448 address = link_key_notification_view.GetBdAddr();
449 break;
450 }
451 case EventCode::USER_PASSKEY_REQUEST: {
452 UserPasskeyRequestView user_passkey_request_view = UserPasskeyRequestView::Create(std::move(packet));
453 ASSERT(user_passkey_request_view.IsValid());
454 address = user_passkey_request_view.GetBdAddr();
455 break;
456 }
457 case EventCode::USER_PASSKEY_NOTIFICATION: {
458 UserPasskeyNotificationView user_passkey_notification_view = UserPasskeyNotificationView::Create(std::move(packet));
459 ASSERT(user_passkey_notification_view.IsValid());
460 address = user_passkey_notification_view.GetBdAddr();
461 break;
462 }
463 case EventCode::USER_CONFIRMATION_REQUEST: {
464 UserConfirmationRequestView user_confirmation_request_view = UserConfirmationRequestView::Create(std::move(packet));
465 ASSERT(user_confirmation_request_view.IsValid());
466 address = user_confirmation_request_view.GetBdAddr();
467 break;
468 }
469 case EventCode::KEYPRESS_NOTIFICATION: {
470 KeypressNotificationView keypress_notification_view = KeypressNotificationView::Create(std::move(packet));
471 ASSERT(keypress_notification_view.IsValid());
472 address = keypress_notification_view.GetBdAddr();
473 break;
474 }
475 case EventCode::REMOTE_OOB_DATA_REQUEST: {
476 RemoteOobDataRequestView remote_oob_data_request_view = RemoteOobDataRequestView::Create(std::move(packet));
477 ASSERT(remote_oob_data_request_view.IsValid());
478 address = remote_oob_data_request_view.GetBdAddr();
479 break;
480 }
481 case EventCode::SIMPLE_PAIRING_COMPLETE: {
482 SimplePairingCompleteView simple_pairing_complete_view = SimplePairingCompleteView::Create(std::move(packet));
483 ASSERT(simple_pairing_complete_view.IsValid());
484 address = simple_pairing_complete_view.GetBdAddr();
485 status = simple_pairing_complete_view.GetStatus();
486 break;
487 }
488 case EventCode::REMOTE_NAME_REQUEST_COMPLETE: {
489 RemoteNameRequestCompleteView remote_name_request_complete_view = RemoteNameRequestCompleteView::Create(std::move(packet));
490 ASSERT(remote_name_request_complete_view.IsValid());
491 address = remote_name_request_complete_view.GetBdAddr();
492 status = remote_name_request_complete_view.GetStatus();
493 break;
494 }
495 case EventCode::AUTHENTICATION_COMPLETE: {
496 AuthenticationCompleteView authentication_complete_view = AuthenticationCompleteView::Create(std::move(packet));
497 ASSERT(authentication_complete_view.IsValid());
498 status = authentication_complete_view.GetStatus();
499 connection_handle = authentication_complete_view.GetConnectionHandle();
500 break;
501 }
502 case EventCode::ENCRYPTION_CHANGE: {
503 EncryptionChangeView encryption_change_view = EncryptionChangeView::Create(std::move(packet));
504 ASSERT(encryption_change_view.IsValid());
505 status = encryption_change_view.GetStatus();
506 connection_handle = encryption_change_view.GetConnectionHandle();
507 value = static_cast<int64_t>(encryption_change_view.GetEncryptionEnabled());
508 break;
509 }
510 default:
511 return;
512 }
513 os::LogMetricClassicPairingEvent(
514 address,
515 connection_handle,
516 static_cast<uint32_t>(cmd),
517 static_cast<uint16_t>(event_code),
518 static_cast<uint16_t>(status),
519 static_cast<uint16_t>(reason),
520 value);
521 }
522
log_classic_pairing_command_status(std::unique_ptr<CommandView> & command_view,ErrorCode status)523 void log_classic_pairing_command_status(std::unique_ptr<CommandView>& command_view, ErrorCode status) {
524 // get op_code
525 ASSERT(command_view->IsValid());
526 OpCode op_code = command_view->GetOpCode();
527
528 // init parameters
529 Address address = Address::kEmpty;
530 ErrorCode reason = ErrorCode::UNKNOWN_HCI_COMMAND;
531 uint32_t connection_handle = bluetooth::os::kUnknownConnectionHandle;
532 int64_t value = 0;
533 uint16_t event_code = android::bluetooth::hci::EVT_COMMAND_STATUS;
534
535 // create SecurityCommandView
536 SecurityCommandView security_command_view = SecurityCommandView::Create(*command_view);
537 ASSERT(security_command_view.IsValid());
538
539 // create ConnectionManagementCommandView
540 ConnectionManagementCommandView connection_management_command_view =
541 ConnectionManagementCommandView::Create(AclCommandView::Create(*command_view));
542 ASSERT(connection_management_command_view.IsValid());
543
544 // create DiscoveryCommandView
545 DiscoveryCommandView discovery_command_view = DiscoveryCommandView::Create(*command_view);
546 ASSERT(discovery_command_view.IsValid());
547
548 switch (op_code) {
549 case OpCode::READ_LOCAL_OOB_DATA: {
550 ReadLocalOobDataView read_local_oob_data_view = ReadLocalOobDataView::Create(std::move(security_command_view));
551 ASSERT(read_local_oob_data_view.IsValid());
552 break;
553 }
554 case OpCode::WRITE_SIMPLE_PAIRING_MODE: {
555 WriteSimplePairingModeView write_simple_pairing_mode_view
556 = WriteSimplePairingModeView::Create(std::move(security_command_view));
557 ASSERT(write_simple_pairing_mode_view.IsValid());
558 value = static_cast<int64_t>(write_simple_pairing_mode_view.GetSimplePairingMode());
559 break;
560 }
561 case OpCode::WRITE_SECURE_CONNECTIONS_HOST_SUPPORT: {
562 WriteSecureConnectionsHostSupportView write_secure_connections_host_support_view
563 = WriteSecureConnectionsHostSupportView::Create(std::move(security_command_view));
564 ASSERT(write_secure_connections_host_support_view.IsValid());
565 value = static_cast<int64_t>(write_secure_connections_host_support_view.GetSecureConnectionsHostSupport());
566 break;
567 }
568 case OpCode::AUTHENTICATION_REQUESTED: {
569 AuthenticationRequestedView authentication_requested_view
570 = AuthenticationRequestedView::Create(std::move(connection_management_command_view));
571 ASSERT(authentication_requested_view.IsValid());
572 connection_handle = authentication_requested_view.GetConnectionHandle();
573 break;
574 }
575 case OpCode::SET_CONNECTION_ENCRYPTION: {
576 SetConnectionEncryptionView set_connection_encryption_view
577 = SetConnectionEncryptionView::Create(std::move(connection_management_command_view));
578 ASSERT(set_connection_encryption_view.IsValid());
579 connection_handle = set_connection_encryption_view.GetConnectionHandle();
580 value = static_cast<int64_t>(set_connection_encryption_view.GetEncryptionEnable());
581 break;
582 }
583 case OpCode::DELETE_STORED_LINK_KEY: {
584 DeleteStoredLinkKeyView delete_stored_link_key_view
585 = DeleteStoredLinkKeyView::Create(std::move(security_command_view));
586 ASSERT(delete_stored_link_key_view.IsValid());
587 address = delete_stored_link_key_view.GetBdAddr();
588 value = static_cast<int64_t>(delete_stored_link_key_view.GetDeleteAllFlag());
589 break;
590 }
591 case OpCode::REMOTE_NAME_REQUEST: {
592 RemoteNameRequestView remote_name_request_view = RemoteNameRequestView::Create(std::move(discovery_command_view));
593 ASSERT(remote_name_request_view.IsValid());
594 address = remote_name_request_view.GetBdAddr();
595 break;
596 }
597 case OpCode::REMOTE_NAME_REQUEST_CANCEL: {
598 RemoteNameRequestCancelView remote_name_request_cancel_view
599 = RemoteNameRequestCancelView::Create(std::move(discovery_command_view));
600 ASSERT(remote_name_request_cancel_view.IsValid());
601 address = remote_name_request_cancel_view.GetBdAddr();
602 break;
603 }
604 case OpCode::LINK_KEY_REQUEST_REPLY: {
605 LinkKeyRequestReplyView link_key_request_reply_view
606 = LinkKeyRequestReplyView::Create(std::move(security_command_view));
607 ASSERT(link_key_request_reply_view.IsValid());
608 address = link_key_request_reply_view.GetBdAddr();
609 break;
610 }
611 case OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY: {
612 LinkKeyRequestNegativeReplyView link_key_request_negative_reply_view
613 = LinkKeyRequestNegativeReplyView::Create(std::move(security_command_view));
614 ASSERT(link_key_request_negative_reply_view.IsValid());
615 address = link_key_request_negative_reply_view.GetBdAddr();
616 break;
617 }
618 case OpCode::IO_CAPABILITY_REQUEST_REPLY: {
619 IoCapabilityRequestReplyView io_capability_request_reply_view
620 = IoCapabilityRequestReplyView::Create(std::move(security_command_view));
621 ASSERT(io_capability_request_reply_view.IsValid());
622 address = io_capability_request_reply_view.GetBdAddr();
623 break;
624 }
625 case OpCode::USER_CONFIRMATION_REQUEST_REPLY: {
626 UserConfirmationRequestReplyView user_confirmation_request_reply
627 = UserConfirmationRequestReplyView::Create(std::move(security_command_view));
628 ASSERT(user_confirmation_request_reply.IsValid());
629 address = user_confirmation_request_reply.GetBdAddr();
630 break;
631 }
632 case OpCode::USER_CONFIRMATION_REQUEST_NEGATIVE_REPLY: {
633 UserConfirmationRequestNegativeReplyView user_confirmation_request_negative_reply
634 = UserConfirmationRequestNegativeReplyView::Create(std::move(security_command_view));
635 ASSERT(user_confirmation_request_negative_reply.IsValid());
636 address = user_confirmation_request_negative_reply.GetBdAddr();
637 break;
638 }
639 case OpCode::USER_PASSKEY_REQUEST_REPLY: {
640 UserPasskeyRequestReplyView user_passkey_request_reply
641 = UserPasskeyRequestReplyView::Create(std::move(security_command_view));
642 ASSERT(user_passkey_request_reply.IsValid());
643 address = user_passkey_request_reply.GetBdAddr();
644 break;
645 }
646 case OpCode::USER_PASSKEY_REQUEST_NEGATIVE_REPLY: {
647 UserPasskeyRequestNegativeReplyView user_passkey_request_negative_reply
648 = UserPasskeyRequestNegativeReplyView::Create(std::move(security_command_view));
649 ASSERT(user_passkey_request_negative_reply.IsValid());
650 address = user_passkey_request_negative_reply.GetBdAddr();
651 break;
652 }
653 case OpCode::REMOTE_OOB_DATA_REQUEST_REPLY: {
654 RemoteOobDataRequestReplyView remote_oob_data_request_reply_view
655 = RemoteOobDataRequestReplyView::Create(std::move(security_command_view));
656 ASSERT(remote_oob_data_request_reply_view.IsValid());
657 address = remote_oob_data_request_reply_view.GetBdAddr();
658 break;
659 }
660 case OpCode::REMOTE_OOB_DATA_REQUEST_NEGATIVE_REPLY: {
661 RemoteOobDataRequestNegativeReplyView remote_oob_data_request_negative_reply_view
662 = RemoteOobDataRequestNegativeReplyView::Create(std::move(security_command_view));
663 ASSERT(remote_oob_data_request_negative_reply_view.IsValid());
664 address = remote_oob_data_request_negative_reply_view.GetBdAddr();
665 break;
666 }
667 case OpCode::IO_CAPABILITY_REQUEST_NEGATIVE_REPLY: {
668 IoCapabilityRequestNegativeReplyView io_capability_request_negative_reply_view
669 = IoCapabilityRequestNegativeReplyView::Create(std::move(security_command_view));
670 ASSERT(io_capability_request_negative_reply_view.IsValid());
671 address = io_capability_request_negative_reply_view.GetBdAddr();
672 reason = io_capability_request_negative_reply_view.GetReason();
673 break;
674 }
675 default:
676 return;
677 }
678 os::LogMetricClassicPairingEvent(
679 address,
680 connection_handle,
681 static_cast<uint32_t>(op_code),
682 static_cast<uint16_t>(event_code),
683 static_cast<uint16_t>(status),
684 static_cast<uint16_t>(reason),
685 value);
686 }
687
log_classic_pairing_command_complete(EventView event_view,std::unique_ptr<CommandView> & command_view)688 void log_classic_pairing_command_complete(EventView event_view, std::unique_ptr<CommandView>& command_view) {
689
690 // get op_code
691 CommandCompleteView command_complete_view = CommandCompleteView::Create(std::move(event_view));
692 ASSERT(command_complete_view.IsValid());
693 OpCode op_code = command_complete_view.GetCommandOpCode();
694
695 // init parameters
696 Address address = Address::kEmpty;
697 ErrorCode status = ErrorCode::UNKNOWN_HCI_COMMAND;
698 ErrorCode reason = ErrorCode::UNKNOWN_HCI_COMMAND;
699 uint32_t connection_handle = bluetooth::os::kUnknownConnectionHandle;
700 int64_t value = 0;
701 EventCode event_code = EventCode::COMMAND_COMPLETE;
702
703 // get ConnectionManagementCommandView
704 ConnectionManagementCommandView connection_management_command_view =
705 ConnectionManagementCommandView::Create(AclCommandView::Create(*command_view));
706 ASSERT(connection_management_command_view.IsValid());
707
708 // create SecurityCommandView
709 SecurityCommandView security_command_view = SecurityCommandView::Create(*command_view);
710 ASSERT(security_command_view.IsValid());
711
712 switch (op_code) {
713 case OpCode::DELETE_STORED_LINK_KEY: {
714 auto delete_stored_link_key_complete_view = DeleteStoredLinkKeyCompleteView::Create(std::move(command_complete_view));
715 ASSERT(delete_stored_link_key_complete_view.IsValid());
716 status = delete_stored_link_key_complete_view.GetStatus();
717 break;
718 }
719 case OpCode::READ_LOCAL_OOB_DATA: {
720 auto read_local_oob_data_complete_view = ReadLocalOobDataCompleteView::Create(std::move(command_complete_view));
721 ASSERT(read_local_oob_data_complete_view.IsValid());
722 status = read_local_oob_data_complete_view.GetStatus();
723 break;
724 }
725 case OpCode::WRITE_SIMPLE_PAIRING_MODE: {
726 auto write_simple_pairing_mode_complete_view = WriteSimplePairingModeCompleteView::Create(std::move(command_complete_view));
727 ASSERT(write_simple_pairing_mode_complete_view.IsValid());
728 status = write_simple_pairing_mode_complete_view.GetStatus();
729 break;
730 }
731 case OpCode::WRITE_SECURE_CONNECTIONS_HOST_SUPPORT: {
732 auto write_secure_connections_host_support_complete_view = WriteSecureConnectionsHostSupportCompleteView::Create(std::move(command_complete_view));
733 ASSERT(write_secure_connections_host_support_complete_view.IsValid());
734 status = write_secure_connections_host_support_complete_view.GetStatus();
735 break;
736 }
737 case OpCode::READ_ENCRYPTION_KEY_SIZE: {
738 auto read_encryption_key_size_complete_view = ReadEncryptionKeySizeCompleteView::Create(std::move(command_complete_view));
739 ASSERT(read_encryption_key_size_complete_view.IsValid());
740 status = read_encryption_key_size_complete_view.GetStatus();
741 connection_handle = read_encryption_key_size_complete_view.GetConnectionHandle();
742 value = read_encryption_key_size_complete_view.GetKeySize();
743 break;
744 }
745 case OpCode::LINK_KEY_REQUEST_REPLY: {
746 auto link_key_request_reply_complete_view = LinkKeyRequestReplyCompleteView::Create(std::move(command_complete_view));
747 ASSERT(link_key_request_reply_complete_view.IsValid());
748 status = link_key_request_reply_complete_view.GetStatus();
749 auto link_key_request_reply_view = LinkKeyRequestReplyView::Create(std::move(security_command_view));
750 ASSERT(link_key_request_reply_view.IsValid());
751 address = link_key_request_reply_view.GetBdAddr();
752 break;
753 }
754 case OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY: {
755 auto link_key_request_negative_reply_complete_view = LinkKeyRequestNegativeReplyCompleteView::Create(std::move(command_complete_view));
756 ASSERT(link_key_request_negative_reply_complete_view.IsValid());
757 status = link_key_request_negative_reply_complete_view.GetStatus();
758 auto link_key_request_negative_reply_view = LinkKeyRequestNegativeReplyView::Create(std::move(security_command_view));
759 ASSERT(link_key_request_negative_reply_view.IsValid());
760 address = link_key_request_negative_reply_view.GetBdAddr();
761 break;
762 }
763 case OpCode::IO_CAPABILITY_REQUEST_REPLY: {
764 auto io_capability_request_reply_complete_view = IoCapabilityRequestReplyCompleteView::Create(std::move(command_complete_view));
765 ASSERT(io_capability_request_reply_complete_view.IsValid());
766 status = io_capability_request_reply_complete_view.GetStatus();
767 auto io_capability_request_reply_view = IoCapabilityRequestReplyView::Create(std::move(security_command_view));
768 ASSERT(io_capability_request_reply_view.IsValid());
769 address = io_capability_request_reply_view.GetBdAddr();
770 break;
771 }
772 case OpCode::IO_CAPABILITY_REQUEST_NEGATIVE_REPLY: {
773 auto io_capability_request_negative_reply_complete_view = IoCapabilityRequestNegativeReplyCompleteView::Create(std::move(command_complete_view));
774 ASSERT(io_capability_request_negative_reply_complete_view.IsValid());
775 status = io_capability_request_negative_reply_complete_view.GetStatus();
776 auto io_capability_request_negative_reply_view = IoCapabilityRequestNegativeReplyView::Create(std::move(security_command_view));
777 ASSERT(io_capability_request_negative_reply_view.IsValid());
778 address = io_capability_request_negative_reply_view.GetBdAddr();
779 break;
780 }
781 case OpCode::USER_CONFIRMATION_REQUEST_REPLY: {
782 auto user_confirmation_request_reply_complete_view = UserConfirmationRequestReplyCompleteView::Create(std::move(command_complete_view));
783 ASSERT(user_confirmation_request_reply_complete_view.IsValid());
784 status = user_confirmation_request_reply_complete_view.GetStatus();
785 auto user_confirmation_request_reply_view = UserConfirmationRequestReplyView::Create(std::move(security_command_view));
786 ASSERT(user_confirmation_request_reply_view.IsValid());
787 address = user_confirmation_request_reply_view.GetBdAddr();
788 break;
789 }
790 case OpCode::USER_CONFIRMATION_REQUEST_NEGATIVE_REPLY: {
791 auto user_confirmation_request_negative_reply_complete_view = UserConfirmationRequestNegativeReplyCompleteView::Create(std::move(command_complete_view));
792 ASSERT(user_confirmation_request_negative_reply_complete_view.IsValid());
793 status = user_confirmation_request_negative_reply_complete_view.GetStatus();
794 auto user_confirmation_request_negative_reply_view = UserConfirmationRequestNegativeReplyView::Create(std::move(security_command_view));
795 ASSERT(user_confirmation_request_negative_reply_view.IsValid());
796 address = user_confirmation_request_negative_reply_view.GetBdAddr();
797 break;
798 }
799 case OpCode::USER_PASSKEY_REQUEST_REPLY: {
800 auto user_passkey_request_reply_complete_view = UserPasskeyRequestReplyCompleteView::Create(std::move(command_complete_view));
801 ASSERT(user_passkey_request_reply_complete_view.IsValid());
802 status = user_passkey_request_reply_complete_view.GetStatus();
803 auto user_passkey_request_reply_view = UserPasskeyRequestReplyView::Create(std::move(security_command_view));
804 ASSERT(user_passkey_request_reply_view.IsValid());
805 address = user_passkey_request_reply_view.GetBdAddr();
806 break;
807 }
808 case OpCode::USER_PASSKEY_REQUEST_NEGATIVE_REPLY: {
809 auto user_passkey_request_negative_reply_complete_view = UserPasskeyRequestNegativeReplyCompleteView::Create(std::move(command_complete_view));
810 ASSERT(user_passkey_request_negative_reply_complete_view.IsValid());
811 status = user_passkey_request_negative_reply_complete_view.GetStatus();
812 auto user_passkey_request_negative_reply_view = UserPasskeyRequestNegativeReplyView::Create(std::move(security_command_view));
813 ASSERT(user_passkey_request_negative_reply_view.IsValid());
814 address = user_passkey_request_negative_reply_view.GetBdAddr();
815 break;
816 }
817 case OpCode::REMOTE_OOB_DATA_REQUEST_REPLY: {
818 auto remote_oob_data_request_reply_complete_view = RemoteOobDataRequestReplyCompleteView::Create(std::move(command_complete_view));
819 ASSERT(remote_oob_data_request_reply_complete_view.IsValid());
820 status = remote_oob_data_request_reply_complete_view.GetStatus();
821 auto remote_oob_data_request_reply_view = RemoteOobDataRequestReplyView::Create(std::move(security_command_view));
822 ASSERT(remote_oob_data_request_reply_view.IsValid());
823 address = remote_oob_data_request_reply_view.GetBdAddr();
824 break;
825 }
826 case OpCode::REMOTE_OOB_DATA_REQUEST_NEGATIVE_REPLY: {
827 auto remote_oob_data_request_negative_reply_complete_view = RemoteOobDataRequestNegativeReplyCompleteView::Create(std::move(command_complete_view));
828 ASSERT(remote_oob_data_request_negative_reply_complete_view.IsValid());
829 status = remote_oob_data_request_negative_reply_complete_view.GetStatus();
830 auto remote_oob_data_request_negative_reply_view = RemoteOobDataRequestNegativeReplyView::Create(std::move(security_command_view));
831 ASSERT(remote_oob_data_request_negative_reply_view.IsValid());
832 address = remote_oob_data_request_negative_reply_view.GetBdAddr();
833 break;
834 }
835 default:
836 return;
837 }
838 os::LogMetricClassicPairingEvent(
839 address,
840 connection_handle,
841 static_cast<uint32_t>(op_code),
842 static_cast<uint16_t>(event_code),
843 static_cast<uint16_t>(status),
844 static_cast<uint16_t>(reason),
845 value);
846 }
847
log_remote_device_information(const Address & address,uint32_t connection_handle,ErrorCode status,storage::StorageModule * storage_module)848 void log_remote_device_information(
849 const Address& address, uint32_t connection_handle, ErrorCode status, storage::StorageModule* storage_module) {
850 if (address.IsEmpty()) {
851 return;
852 }
853 const storage::Device device = storage_module->GetDeviceByLegacyKey(address);
854 // log ManufacturerInfo
855 std::stringstream sdp_di_vendor_id_source;
856 // [N - native]::SDP::[DIP - Device ID Profile]
857 sdp_di_vendor_id_source << "N:SDP::DIP::" << common::ToHexString(device.GetSdpDiVendorIdSource().value_or(0)).c_str();
858 os::LogMetricManufacturerInfo(
859 address,
860 android::bluetooth::DeviceInfoSrcEnum::DEVICE_INFO_INTERNAL,
861 sdp_di_vendor_id_source.str(),
862 common::ToHexString(device.GetSdpDiManufacturer().value_or(0)).c_str(),
863 common::ToHexString(device.GetSdpDiModel().value_or(0)).c_str(),
864 common::ToHexString(device.GetSdpDiHardwareVersion().value_or(0)).c_str(),
865 "");
866
867 // log RemoteVersionInfo
868 os::LogMetricRemoteVersionInfo(
869 connection_handle,
870 static_cast<uint16_t>(status),
871 device.GetLmpVersion().value_or(-1),
872 device.GetManufacturerCode().value_or(-1),
873 device.GetLmpSubVersion().value_or(-1));
874 }
875
876 } // namespace hci
877 } // namespace bluetooth