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