Metric: Add logging for link layer connection events

* Logs when there is an event affecting Bluetooth device's link
  layer connection. Triggerred when there is a related HCI command
  or event
* Users of this metrics can deduce Bluetooth device's connection state
  from these events
* HCI commands are logged before the command is sent, after receiving,
  command status, and after receiving command complete
  event comes back
* HCI events are logged when they arrive
* Simplifies logic in btu_hcif_hdl_command_status() since p_cmd is never
  null and we always ignore parameter total length field
* Call btm_identity_addr_to_random_pseudo() when parsing command status
  packet for LE create connection, extended create connection,
  add/remove/clear whitelist commands

DETAILS:

* Bluetooth classic commands:
  - CMD_CREATE_CONNECTION
  - CMD_DISCONNECT
  - CMD_CREATE_CONNECTION_CANCEL
  - CMD_ACCEPT_CONNECTION_REQUEST
  - CMD_REJECT_CONNECTION_REQUEST
  - CMD_SETUP_ESCO_CONNECTION
  - CMD_ACCEPT_ESCO_CONNECTION
  - CMD_REJECT_ESCO_CONNECTION
  - CMD_ENH_SETUP_ESCO_CONNECTION
  - CMD_ENH_ACCEPT_ESCO_CONNECTION
* Bluetooth low energy commands:
  - CMD_BLE_CREATE_LL_CONN [Only logged when there is an error or initiator filter policy is 0x00]
  - CMD_BLE_CREATE_CONN_CANCEL [Only logged when there is an error]
  - CMD_BLE_EXTENDED_CREATE_CONNECTION [Only logged on error or when initiator filter policy is 0x00]
  - CMD_BLE_CLEAR_WHITE_LIST
  - CMD_BLE_ADD_WHITE_LIST
  - CMD_BLE_REMOVE_WHITE_LIST
* Bluetooth classic events:
  - EVT_CONNECTION_COMP
  - EVT_CONNECTION_REQUEST
  - EVT_DISCONNECTION_COMP
  - EVT_ESCO_CONNECTION_COMP
  - EVT_ESCO_CONNECTION_CHANGED
* Bluetooth low energy meta events:
  - BLE_EVT_CONN_COMPLETE_EVT
  - BLE_EVT_ENHANCED_CONN_COMPLETE_EVT

Bug: 112969790
Test: make and test drive statsd
Change-Id: Ib843dfa95bb6448c41dac261dabcf17947efda06
diff --git a/system/common/metrics.cc b/system/common/metrics.cc
index ace021b..976b5cf 100644
--- a/system/common/metrics.cc
+++ b/system/common/metrics.cc
@@ -29,11 +29,13 @@
 #include <base/base64.h>
 #include <base/logging.h>
 #include <include/hardware/bt_av.h>
+#include <statslog.h>
 
 #include "bluetooth/metrics/bluetooth.pb.h"
 #include "osi/include/osi.h"
 #include "stack/include/btm_api_types.h"
 
+#include "address_obfuscator.h"
 #include "leaky_bonded_queue.h"
 #include "metrics.h"
 #include "time_util.h"
@@ -567,6 +569,34 @@
   pimpl_->scan_event_queue_->Clear();
 }
 
+void LogLinkLayerConnectionEvent(const RawAddress* address,
+                                 uint32_t connection_handle,
+                                 android::bluetooth::DirectionEnum direction,
+                                 uint32_t link_type, uint32_t hci_cmd,
+                                 uint32_t hci_event, uint32_t hci_ble_event,
+                                 uint32_t cmd_status, uint32_t reason_code) {
+  std::string obfuscated_id;
+  if (address != nullptr) {
+    obfuscated_id = AddressObfuscator::GetInstance()->Obfuscate(*address);
+  }
+  // nullptr and size 0 represent missing value for obfuscated_id
+  android::util::BytesField bytes_field(
+      address != nullptr ? obfuscated_id.c_str() : nullptr,
+      address != nullptr ? obfuscated_id.size() : 0);
+  int ret = android::util::stats_write(
+      android::util::BLUETOOTH_LINK_LAYER_CONNECTION_EVENT, bytes_field,
+      connection_handle, direction, link_type, hci_cmd, hci_event,
+      hci_ble_event, cmd_status, reason_code);
+  if (ret < 0) {
+    LOG(WARNING) << __func__ << ": failed to log status 0x"
+                 << loghex(cmd_status) << ", reason 0x" << loghex(reason_code)
+                 << " from cmd 0x" << loghex(hci_cmd) << ", event 0x"
+                 << loghex(hci_event) << ", ble_event 0x"
+                 << loghex(hci_ble_event) << " for " << address << ", handle "
+                 << connection_handle << ", error " << ret;
+  }
+}
+
 }  // namespace common
 
 }  // namespace bluetooth