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