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