Metrics: Add metrics item for profile usage

Item A: Add messages to log profile connections
* Add ProfileUsageStats message in bluetooth.proto
* Add BluetoothProfileId enum to represent each profile
* Add num_times_connected item to log how many times a profile is
  connected

Item B: Add messages to log headset profile connections for HSP and HFP
* Add HeadsetProfileConnectionStats message in bluetooth.proto
* Add HeadsetProfileType enum to represent each headset profile type
* Add num_times_connected item to log how many times each headset
  profile is connected

* Add unit tests in metrics_tests
* Remove "clear" flag in various metrics dumping methods to make sure
  that we clean up metrics every time we dump

Bug: 77476285
Test: make, net_test_osi, BtFunhausMetricsTest,
      adb shell dumpsys bluetooth_manager --proto-bin
Change-Id: Ib18948c50ddb98ab7472f7b51a9bb98d153071c7
Merged-In: Ib18948c50ddb98ab7472f7b51a9bb98d153071c7
diff --git a/osi/test/metrics_test.cc b/osi/test/metrics_test.cc
index 15f6ad9..2c9762f 100644
--- a/osi/test/metrics_test.cc
+++ b/osi/test/metrics_test.cc
@@ -50,6 +50,8 @@
 using bluetooth::metrics::BluetoothMetricsProto::ScanEvent_ScanEventType;
 using bluetooth::metrics::BluetoothMetricsProto::WakeEvent;
 using bluetooth::metrics::BluetoothMetricsProto::WakeEvent_WakeEventType;
+using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType;
+using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileConnectionStats;
 using system_bt_osi::BluetoothMetricsLogger;
 using system_bt_osi::A2dpSessionMetrics;
 
@@ -408,7 +410,7 @@
   BluetoothMetricsLogger::GetInstance()->LogPairEvent(
       35, 12345, 42, system_bt_osi::DEVICE_TYPE_BREDR);
   std::string msg_str;
-  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str, true);
+  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
 }
 
@@ -420,7 +422,7 @@
   BluetoothMetricsLogger::GetInstance()->LogWakeEvent(
       system_bt_osi::WAKE_EVENT_ACQUIRED, "TEST_REQ", "TEST_NAME", 12345);
   std::string msg_str;
-  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str, true);
+  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
 }
 
@@ -437,7 +439,7 @@
         "TEST_REQ", "TEST_NAME", i);
   }
   std::string msg_str;
-  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str, true);
+  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
 }
 
@@ -451,7 +453,7 @@
   BluetoothMetricsLogger::GetInstance()->LogScanEvent(
       false, "TEST_INITIATOR", system_bt_osi::SCAN_TECH_TYPE_BREDR, 42, 123456);
   std::string msg_str;
-  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str, true);
+  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
 }
 
@@ -469,7 +471,7 @@
   BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
       system_bt_osi::DISCONNECT_REASON_UNKNOWN, 133456);
   std::string msg_str;
-  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str, true);
+  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
 }
 
@@ -486,7 +488,7 @@
       system_bt_osi::CONNECTION_TECHNOLOGY_TYPE_LE, time_get_os_boottime_ms());
   sleep_ms(1000);
   std::string msg_str;
-  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str, true);
+  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
 }
 
@@ -513,7 +515,7 @@
       system_bt_osi::CONNECTION_TECHNOLOGY_TYPE_LE, 0);
   sleep_ms(2000);
   std::string msg_str;
-  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str, true);
+  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
 }
 
@@ -579,7 +581,7 @@
   BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
       system_bt_osi::DISCONNECT_REASON_UNKNOWN, 133456);
   std::string msg_str;
-  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str, true);
+  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
 }
 
@@ -635,7 +637,7 @@
   BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1);
   sleep_ms(1000);
   std::string msg_str;
-  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str, true);
+  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
   ClearLog();
   info = MakeDeviceInfo(
@@ -655,7 +657,7 @@
   BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
       system_bt_osi::DISCONNECT_REASON_UNKNOWN, 0);
   msg_str.clear();
-  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str, true);
+  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
 }
 
@@ -714,7 +716,7 @@
   BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2);
   sleep_ms(1000);
   std::string msg_str;
-  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str, true);
+  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
 }
 
@@ -777,7 +779,7 @@
       BTM_COD_MAJOR_AUDIO_TEST, system_bt_osi::DEVICE_TYPE_BREDR);
   sleep_ms(1000);
   std::string msg_str;
-  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str, true);
+  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
   ClearLog();
   info = MakeDeviceInfo(
@@ -798,7 +800,94 @@
   BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
       system_bt_osi::DISCONNECT_REASON_UNKNOWN, 0);
   msg_str.clear();
-  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str, true);
+  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
 }
+
+TEST_F(BluetoothMetricsLoggerTest, LogHeadsetProfileRfcConnectionTest) {
+  BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
+      BTA_HSP_SERVICE_ID);
+  BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
+      BTA_HFP_SERVICE_ID);
+  BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
+      BTA_HFP_SERVICE_ID);
+  std::string msg_str;
+  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
+  BluetoothLog* metrics = BluetoothLog::default_instance().New();
+  metrics->ParseFromString(msg_str);
+  EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 2);
+  bool hfp_correct = false;
+  bool hsp_correct = false;
+  for (const HeadsetProfileConnectionStats& headset_profile_connection_stats :
+       metrics->headset_profile_connection_stats()) {
+    switch (headset_profile_connection_stats.headset_profile_type()) {
+      case HeadsetProfileType::HFP:
+        EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 2);
+        hfp_correct = true;
+        break;
+      case HeadsetProfileType::HSP:
+        EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 1);
+        hsp_correct = true;
+        break;
+      default:
+        FAIL();
+    }
+  }
+  EXPECT_TRUE(hfp_correct);
+  EXPECT_TRUE(hsp_correct);
+  metrics->clear_headset_profile_connection_stats();
+  EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 0);
+  msg_str.clear();
+  // Verify that dump after clean up result in an empty list
+  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
+  metrics->ParseFromString(msg_str);
+  EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 0);
+  delete metrics;
+}
+
+TEST_F(BluetoothMetricsLoggerTest, LogHeadsetProfileRfcConnectionErrorTest) {
+  BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
+      BTA_HSP_SERVICE_ID);
+  BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
+      BTA_HFP_SERVICE_ID);
+  BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
+      BTA_BIP_SERVICE_ID);
+  BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
+      BTA_HSP_SERVICE_ID);
+  std::string msg_str;
+  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
+  BluetoothLog* metrics = BluetoothLog::default_instance().New();
+  metrics->ParseFromString(msg_str);
+  EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 3);
+  bool hfp_correct = false;
+  bool hsp_correct = false;
+  bool unknown_correct = false;
+  for (const HeadsetProfileConnectionStats& headset_profile_connection_stats :
+       metrics->headset_profile_connection_stats()) {
+    switch (headset_profile_connection_stats.headset_profile_type()) {
+      case HeadsetProfileType::HFP:
+        EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 1);
+        hfp_correct = true;
+        break;
+      case HeadsetProfileType::HSP:
+        EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 2);
+        hsp_correct = true;
+        break;
+      default:
+        EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 1);
+        unknown_correct = true;
+        break;
+    }
+  }
+  EXPECT_TRUE(hfp_correct);
+  EXPECT_TRUE(hsp_correct);
+  EXPECT_TRUE(unknown_correct);
+  metrics->clear_headset_profile_connection_stats();
+  EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 0);
+  // Verify that dump after clean up result in an empty list
+  BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
+  metrics->ParseFromString(msg_str);
+  EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 0);
+  delete metrics;
+}
 }