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