• 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_connect_list_view = LeClearFilterAcceptListView::Create(
197           LeConnectionManagementCommandView::Create(std::move(connection_management_command_view)));
198       ASSERT(le_clear_connect_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_connect_list_view = LeClearFilterAcceptListView::Create(
377           LeConnectionManagementCommandView::Create(std::move(connection_management_command_view)));
378       ASSERT(le_clear_connect_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_connect_list_view = LeClearFilterAcceptListView::Create(
439           LeConnectionManagementCommandView::Create(std::move(connection_management_command_view)));
440       ASSERT(le_clear_connect_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   auto le_connection_complete_view = LeConnectionCompleteView::Create(std::move(le_meta_event_view));
578   if (!le_connection_complete_view.IsValid()) {
579     // function is called for all le meta events. Only need to process le connection complete.
580     return;
581   }
582   ASSERT(le_connection_complete_view.IsValid());
583   // init parameters to log
584   EventCode event_code = EventCode::LE_META_EVENT;
585   Address address = le_connection_complete_view.GetPeerAddress();
586   uint32_t connection_handle = le_connection_complete_view.GetConnectionHandle();
587   android::bluetooth::DirectionEnum direction = android::bluetooth::DIRECTION_UNKNOWN;
588   uint16_t link_type = android::bluetooth::LINK_TYPE_ACL;
589   ErrorCode status = le_connection_complete_view.GetStatus();
590   ErrorCode reason = ErrorCode::UNKNOWN_HCI_COMMAND;
591   uint32_t cmd = android::bluetooth::hci::CMD_UNKNOWN;
592 
593   os::LogMetricLinkLayerConnectionEvent(
594       &address,
595       connection_handle,
596       direction,
597       link_type,
598       static_cast<uint32_t>(cmd),
599       static_cast<uint16_t>(event_code),
600       static_cast<uint16_t>(leEvt),
601       static_cast<uint16_t>(status),
602       static_cast<uint16_t>(reason));
603 
604   if (status != ErrorCode::SUCCESS && status != ErrorCode::UNKNOWN_CONNECTION) {
605     // ERROR CODE 0x02, unknown connection identifier, means connection attempt was cancelled by host, so probably no
606     // need to log it.
607     common::LogConnectionAdminAuditEvent("Connecting", address, status);
608   }
609 }
610 
log_classic_pairing_other_hci_event(EventView packet)611 void log_classic_pairing_other_hci_event(EventView packet) {
612   EventCode event_code = packet.GetEventCode();
613   Address address = Address::kEmpty;
614   uint32_t cmd = android::bluetooth::hci::CMD_UNKNOWN;
615   ErrorCode status = ErrorCode::UNKNOWN_HCI_COMMAND;
616   ErrorCode reason = ErrorCode::UNKNOWN_HCI_COMMAND;
617   uint32_t connection_handle = bluetooth::os::kUnknownConnectionHandle;
618   int64_t value = 0;
619 
620   switch (event_code) {
621     case EventCode::IO_CAPABILITY_REQUEST: {
622       IoCapabilityRequestView io_capability_request_view = IoCapabilityRequestView::Create(std::move(packet));
623       ASSERT(io_capability_request_view.IsValid());
624       address = io_capability_request_view.GetBdAddr();
625       break;
626     }
627     case EventCode::IO_CAPABILITY_RESPONSE: {
628       IoCapabilityResponseView io_capability_response_view = IoCapabilityResponseView::Create(std::move(packet));
629       ASSERT(io_capability_response_view.IsValid());
630       address = io_capability_response_view.GetBdAddr();
631       break;
632     }
633     case EventCode::LINK_KEY_REQUEST: {
634       LinkKeyRequestView link_key_request_view = LinkKeyRequestView::Create(std::move(packet));
635       ASSERT(link_key_request_view.IsValid());
636       address = link_key_request_view.GetBdAddr();
637       break;
638     }
639     case EventCode::LINK_KEY_NOTIFICATION: {
640       LinkKeyNotificationView link_key_notification_view = LinkKeyNotificationView::Create(std::move(packet));
641       ASSERT(link_key_notification_view.IsValid());
642       address = link_key_notification_view.GetBdAddr();
643       break;
644     }
645     case EventCode::USER_PASSKEY_REQUEST: {
646       UserPasskeyRequestView user_passkey_request_view = UserPasskeyRequestView::Create(std::move(packet));
647       ASSERT(user_passkey_request_view.IsValid());
648       address = user_passkey_request_view.GetBdAddr();
649       break;
650     }
651     case EventCode::USER_PASSKEY_NOTIFICATION: {
652       UserPasskeyNotificationView user_passkey_notification_view = UserPasskeyNotificationView::Create(std::move(packet));
653       ASSERT(user_passkey_notification_view.IsValid());
654       address = user_passkey_notification_view.GetBdAddr();
655       break;
656     }
657     case EventCode::USER_CONFIRMATION_REQUEST: {
658       UserConfirmationRequestView user_confirmation_request_view = UserConfirmationRequestView::Create(std::move(packet));
659       ASSERT(user_confirmation_request_view.IsValid());
660       address = user_confirmation_request_view.GetBdAddr();
661       break;
662     }
663     case EventCode::KEYPRESS_NOTIFICATION: {
664       KeypressNotificationView keypress_notification_view = KeypressNotificationView::Create(std::move(packet));
665       ASSERT(keypress_notification_view.IsValid());
666       address = keypress_notification_view.GetBdAddr();
667       break;
668     }
669     case EventCode::REMOTE_OOB_DATA_REQUEST: {
670       RemoteOobDataRequestView remote_oob_data_request_view = RemoteOobDataRequestView::Create(std::move(packet));
671       if (!remote_oob_data_request_view.IsValid()) {
672         LOG_WARN("remote_oob_data_request_view not valid");
673         return;
674       }
675       address = remote_oob_data_request_view.GetBdAddr();
676       break;
677     }
678     case EventCode::SIMPLE_PAIRING_COMPLETE: {
679       SimplePairingCompleteView simple_pairing_complete_view = SimplePairingCompleteView::Create(std::move(packet));
680       ASSERT(simple_pairing_complete_view.IsValid());
681       address = simple_pairing_complete_view.GetBdAddr();
682       status = simple_pairing_complete_view.GetStatus();
683       break;
684     }
685     case EventCode::REMOTE_NAME_REQUEST_COMPLETE: {
686       RemoteNameRequestCompleteView remote_name_request_complete_view = RemoteNameRequestCompleteView::Create(std::move(packet));
687       if (!remote_name_request_complete_view.IsValid()) {
688         LOG_WARN("remote_name_request_complete_view not valid");
689         return;
690       }
691       address = remote_name_request_complete_view.GetBdAddr();
692       status = remote_name_request_complete_view.GetStatus();
693       break;
694     }
695     case EventCode::AUTHENTICATION_COMPLETE: {
696       AuthenticationCompleteView authentication_complete_view = AuthenticationCompleteView::Create(std::move(packet));
697       ASSERT(authentication_complete_view.IsValid());
698       status = authentication_complete_view.GetStatus();
699       connection_handle = authentication_complete_view.GetConnectionHandle();
700       break;
701     }
702     case EventCode::ENCRYPTION_CHANGE: {
703       EncryptionChangeView encryption_change_view = EncryptionChangeView::Create(std::move(packet));
704       ASSERT(encryption_change_view.IsValid());
705       status = encryption_change_view.GetStatus();
706       connection_handle = encryption_change_view.GetConnectionHandle();
707       value = static_cast<int64_t>(encryption_change_view.GetEncryptionEnabled());
708       break;
709     }
710     default:
711       return;
712   }
713   os::LogMetricClassicPairingEvent(
714       address,
715       connection_handle,
716       static_cast<uint32_t>(cmd),
717       static_cast<uint16_t>(event_code),
718       static_cast<uint16_t>(status),
719       static_cast<uint16_t>(reason),
720       value);
721 }
722 
log_classic_pairing_command_status(std::unique_ptr<CommandView> & command_view,ErrorCode status)723 void log_classic_pairing_command_status(std::unique_ptr<CommandView>& command_view, ErrorCode status) {
724   // get op_code
725   ASSERT(command_view->IsValid());
726   OpCode op_code = command_view->GetOpCode();
727 
728   // init parameters
729   Address address = Address::kEmpty;
730   ErrorCode reason = ErrorCode::UNKNOWN_HCI_COMMAND;
731   uint32_t connection_handle = bluetooth::os::kUnknownConnectionHandle;
732   int64_t value = 0;
733   uint16_t event_code = android::bluetooth::hci::EVT_COMMAND_STATUS;
734 
735   // create SecurityCommandView
736   SecurityCommandView security_command_view = SecurityCommandView::Create(*command_view);
737   ASSERT(security_command_view.IsValid());
738 
739   // create ConnectionManagementCommandView
740   ConnectionManagementCommandView connection_management_command_view =
741       ConnectionManagementCommandView::Create(AclCommandView::Create(*command_view));
742   ASSERT(connection_management_command_view.IsValid());
743 
744   // create DiscoveryCommandView
745   DiscoveryCommandView discovery_command_view = DiscoveryCommandView::Create(*command_view);
746   ASSERT(discovery_command_view.IsValid());
747 
748   switch (op_code) {
749     case OpCode::READ_LOCAL_OOB_DATA: {
750       ReadLocalOobDataView read_local_oob_data_view = ReadLocalOobDataView::Create(std::move(security_command_view));
751       ASSERT(read_local_oob_data_view.IsValid());
752       break;
753     }
754     case OpCode::WRITE_SIMPLE_PAIRING_MODE: {
755       WriteSimplePairingModeView write_simple_pairing_mode_view
756       = WriteSimplePairingModeView::Create(std::move(security_command_view));
757       ASSERT(write_simple_pairing_mode_view.IsValid());
758       value = static_cast<int64_t>(write_simple_pairing_mode_view.GetSimplePairingMode());
759       break;
760     }
761     case OpCode::WRITE_SECURE_CONNECTIONS_HOST_SUPPORT: {
762       WriteSecureConnectionsHostSupportView write_secure_connections_host_support_view
763       = WriteSecureConnectionsHostSupportView::Create(std::move(security_command_view));
764       ASSERT(write_secure_connections_host_support_view.IsValid());
765       value = static_cast<int64_t>(write_secure_connections_host_support_view.GetSecureConnectionsHostSupport());
766       break;
767     }
768     case OpCode::AUTHENTICATION_REQUESTED: {
769       AuthenticationRequestedView authentication_requested_view
770       = AuthenticationRequestedView::Create(std::move(connection_management_command_view));
771       ASSERT(authentication_requested_view.IsValid());
772       connection_handle = authentication_requested_view.GetConnectionHandle();
773       break;
774     }
775     case OpCode::SET_CONNECTION_ENCRYPTION: {
776       SetConnectionEncryptionView set_connection_encryption_view
777       = SetConnectionEncryptionView::Create(std::move(connection_management_command_view));
778       ASSERT(set_connection_encryption_view.IsValid());
779       connection_handle = set_connection_encryption_view.GetConnectionHandle();
780       value = static_cast<int64_t>(set_connection_encryption_view.GetEncryptionEnable());
781       break;
782     }
783     case OpCode::DELETE_STORED_LINK_KEY: {
784       DeleteStoredLinkKeyView delete_stored_link_key_view
785       = DeleteStoredLinkKeyView::Create(std::move(security_command_view));
786       ASSERT(delete_stored_link_key_view.IsValid());
787       address = delete_stored_link_key_view.GetBdAddr();
788       value = static_cast<int64_t>(delete_stored_link_key_view.GetDeleteAllFlag());
789       break;
790     }
791     case OpCode::REMOTE_NAME_REQUEST: {
792       RemoteNameRequestView remote_name_request_view = RemoteNameRequestView::Create(std::move(discovery_command_view));
793       ASSERT(remote_name_request_view.IsValid());
794       address = remote_name_request_view.GetBdAddr();
795       break;
796     }
797     case OpCode::REMOTE_NAME_REQUEST_CANCEL: {
798       RemoteNameRequestCancelView remote_name_request_cancel_view
799       = RemoteNameRequestCancelView::Create(std::move(discovery_command_view));
800       ASSERT(remote_name_request_cancel_view.IsValid());
801       address = remote_name_request_cancel_view.GetBdAddr();
802       break;
803     }
804     case OpCode::LINK_KEY_REQUEST_REPLY: {
805       LinkKeyRequestReplyView link_key_request_reply_view
806       = LinkKeyRequestReplyView::Create(std::move(security_command_view));
807       ASSERT(link_key_request_reply_view.IsValid());
808       address = link_key_request_reply_view.GetBdAddr();
809       break;
810     }
811     case OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY: {
812       LinkKeyRequestNegativeReplyView link_key_request_negative_reply_view
813       = LinkKeyRequestNegativeReplyView::Create(std::move(security_command_view));
814       ASSERT(link_key_request_negative_reply_view.IsValid());
815       address = link_key_request_negative_reply_view.GetBdAddr();
816       break;
817     }
818     case OpCode::IO_CAPABILITY_REQUEST_REPLY: {
819       IoCapabilityRequestReplyView io_capability_request_reply_view
820       = IoCapabilityRequestReplyView::Create(std::move(security_command_view));
821       ASSERT(io_capability_request_reply_view.IsValid());
822       address = io_capability_request_reply_view.GetBdAddr();
823       break;
824     }
825     case OpCode::USER_CONFIRMATION_REQUEST_REPLY: {
826       UserConfirmationRequestReplyView user_confirmation_request_reply
827       = UserConfirmationRequestReplyView::Create(std::move(security_command_view));
828       ASSERT(user_confirmation_request_reply.IsValid());
829       address = user_confirmation_request_reply.GetBdAddr();
830       break;
831     }
832     case OpCode::USER_CONFIRMATION_REQUEST_NEGATIVE_REPLY: {
833       UserConfirmationRequestNegativeReplyView user_confirmation_request_negative_reply
834       = UserConfirmationRequestNegativeReplyView::Create(std::move(security_command_view));
835       ASSERT(user_confirmation_request_negative_reply.IsValid());
836       address = user_confirmation_request_negative_reply.GetBdAddr();
837       break;
838     }
839     case OpCode::USER_PASSKEY_REQUEST_REPLY: {
840       UserPasskeyRequestReplyView user_passkey_request_reply
841       = UserPasskeyRequestReplyView::Create(std::move(security_command_view));
842       ASSERT(user_passkey_request_reply.IsValid());
843       address = user_passkey_request_reply.GetBdAddr();
844       break;
845     }
846     case OpCode::USER_PASSKEY_REQUEST_NEGATIVE_REPLY: {
847       UserPasskeyRequestNegativeReplyView user_passkey_request_negative_reply
848       = UserPasskeyRequestNegativeReplyView::Create(std::move(security_command_view));
849       ASSERT(user_passkey_request_negative_reply.IsValid());
850       address = user_passkey_request_negative_reply.GetBdAddr();
851       break;
852     }
853     case OpCode::REMOTE_OOB_DATA_REQUEST_REPLY: {
854       RemoteOobDataRequestReplyView remote_oob_data_request_reply_view
855       = RemoteOobDataRequestReplyView::Create(std::move(security_command_view));
856       if (!remote_oob_data_request_reply_view.IsValid()) {
857         LOG_WARN("remote_oob_data_request_reply_view is not valid.");
858         return;
859       }
860       address = remote_oob_data_request_reply_view.GetBdAddr();
861       break;
862     }
863     case OpCode::REMOTE_OOB_DATA_REQUEST_NEGATIVE_REPLY: {
864       RemoteOobDataRequestNegativeReplyView remote_oob_data_request_negative_reply_view
865       = RemoteOobDataRequestNegativeReplyView::Create(std::move(security_command_view));
866       if (!remote_oob_data_request_negative_reply_view.IsValid()) {
867         LOG_WARN("remote_oob_data_request_negative_reply_view is not valid.");
868         return;
869       }
870       address = remote_oob_data_request_negative_reply_view.GetBdAddr();
871       break;
872     }
873     case OpCode::IO_CAPABILITY_REQUEST_NEGATIVE_REPLY: {
874       IoCapabilityRequestNegativeReplyView io_capability_request_negative_reply_view
875       = IoCapabilityRequestNegativeReplyView::Create(std::move(security_command_view));
876       ASSERT(io_capability_request_negative_reply_view.IsValid());
877       address = io_capability_request_negative_reply_view.GetBdAddr();
878       reason = io_capability_request_negative_reply_view.GetReason();
879       break;
880     }
881     default:
882       return;
883   }
884   os::LogMetricClassicPairingEvent(
885       address,
886       connection_handle,
887       static_cast<uint32_t>(op_code),
888       static_cast<uint16_t>(event_code),
889       static_cast<uint16_t>(status),
890       static_cast<uint16_t>(reason),
891       value);
892 }
893 
log_classic_pairing_command_complete(EventView event_view,std::unique_ptr<CommandView> & command_view)894 void log_classic_pairing_command_complete(EventView event_view, std::unique_ptr<CommandView>& command_view) {
895 
896   // get op_code
897   CommandCompleteView command_complete_view = CommandCompleteView::Create(std::move(event_view));
898   ASSERT(command_complete_view.IsValid());
899   OpCode op_code = command_complete_view.GetCommandOpCode();
900 
901   // init parameters
902   Address address = Address::kEmpty;
903   ErrorCode status = ErrorCode::UNKNOWN_HCI_COMMAND;
904   ErrorCode reason = ErrorCode::UNKNOWN_HCI_COMMAND;
905   uint32_t connection_handle = bluetooth::os::kUnknownConnectionHandle;
906   int64_t value = 0;
907   EventCode event_code = EventCode::COMMAND_COMPLETE;
908 
909   // get ConnectionManagementCommandView
910   ConnectionManagementCommandView connection_management_command_view =
911       ConnectionManagementCommandView::Create(AclCommandView::Create(*command_view));
912   ASSERT(connection_management_command_view.IsValid());
913 
914   // create SecurityCommandView
915   SecurityCommandView security_command_view = SecurityCommandView::Create(*command_view);
916   ASSERT(security_command_view.IsValid());
917 
918   switch (op_code) {
919     case OpCode::DELETE_STORED_LINK_KEY: {
920       auto delete_stored_link_key_complete_view = DeleteStoredLinkKeyCompleteView::Create(std::move(command_complete_view));
921       ASSERT(delete_stored_link_key_complete_view.IsValid());
922       status = delete_stored_link_key_complete_view.GetStatus();
923       break;
924     }
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