Merge "Allow to disable the rootcanal test console with a property"
diff --git a/audio_bluetooth_hw/device_port_proxy.cc b/audio_bluetooth_hw/device_port_proxy.cc
index 5b484b8..c7bf457 100644
--- a/audio_bluetooth_hw/device_port_proxy.cc
+++ b/audio_bluetooth_hw/device_port_proxy.cc
@@ -31,6 +31,8 @@
 namespace bluetooth {
 namespace audio {
 
+using android::base::StringPrintf;
+
 using ::android::bluetooth::audio::BluetoothAudioSessionControl;
 using ::android::hardware::bluetooth::audio::V2_0::BitsPerSample;
 using ::android::hardware::bluetooth::audio::V2_0::ChannelMode;
@@ -109,12 +111,11 @@
   auto control_result_cb = [port = this](uint16_t cookie, bool start_resp,
                                          const BluetoothAudioStatus& status) {
     if (!port->in_use()) {
-      LOG(ERROR) << ": BluetoothAudioPortOut is not in use";
+      LOG(ERROR) << "control_result_cb: BluetoothAudioPortOut is not in use";
       return;
     }
     if (port->cookie_ != cookie) {
-      LOG(ERROR) << "control_result_cb: proxy of device port (cookie=0x"
-                 << android::base::StringPrintf("%04hx", cookie)
+      LOG(ERROR) << "control_result_cb: proxy of device port (cookie=0x" << StringPrintf("%04hx", cookie)
                  << ") is corrupted";
       return;
     }
@@ -122,12 +123,11 @@
   };
   auto session_changed_cb = [port = this](uint16_t cookie) {
     if (!port->in_use()) {
-      LOG(ERROR) << ": BluetoothAudioPortOut is not in use";
+      LOG(ERROR) << "session_changed_cb: BluetoothAudioPortOut is not in use";
       return;
     }
     if (port->cookie_ != cookie) {
-      LOG(ERROR) << "session_changed_cb: proxy of device port (cookie=0x"
-                 << android::base::StringPrintf("%04hx", cookie)
+      LOG(ERROR) << "session_changed_cb: proxy of device port (cookie=0x" << StringPrintf("%04hx", cookie)
                  << ") is corrupted";
       return;
     }
@@ -138,8 +138,8 @@
       .session_changed_cb_ = session_changed_cb};
   cookie_ = BluetoothAudioSessionControl::RegisterControlResultCback(
       session_type_, cbacks);
-  LOG(INFO) << __func__ << ": session_type=" << toString(session_type_)
-            << ", cookie=0x" << android::base::StringPrintf("%04hx", cookie_);
+  LOG(INFO) << __func__ << ": session_type=" << toString(session_type_) << ", cookie=0x"
+            << StringPrintf("%04hx", cookie_);
 
   return (cookie_ != android::bluetooth::audio::kObserversCookieUndefined);
 }
@@ -149,29 +149,23 @@
     case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
     case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
     case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
-      LOG(VERBOSE)
-          << __func__
-          << "device=AUDIO_DEVICE_OUT_BLUETOOTH_A2DP (HEADPHONES/SPEAKER) (0x"
-          << android::base::StringPrintf("%08x", device) << ")";
+      LOG(VERBOSE) << __func__ << ": device=AUDIO_DEVICE_OUT_BLUETOOTH_A2DP (HEADPHONES/SPEAKER) (0x"
+                   << StringPrintf("%08x", device) << ")";
       session_type_ = SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH;
       break;
     case AUDIO_DEVICE_OUT_HEARING_AID:
-      LOG(VERBOSE) << __func__
-                   << "device=AUDIO_DEVICE_OUT_HEARING_AID (MEDIA/VOICE) (0x"
-                   << android::base::StringPrintf("%08x", device) << ")";
+      LOG(VERBOSE) << __func__ << ": device=AUDIO_DEVICE_OUT_HEARING_AID (MEDIA/VOICE) (0x"
+                   << StringPrintf("%08x", device) << ")";
       session_type_ = SessionType::HEARING_AID_SOFTWARE_ENCODING_DATAPATH;
       break;
     default:
-      LOG(ERROR) << __func__ << "unknown device=0x"
-                 << android::base::StringPrintf("%08x", device);
+      LOG(ERROR) << __func__ << ": unknown device=0x" << StringPrintf("%08x", device);
       return false;
   }
 
   if (!BluetoothAudioSessionControl::IsSessionReady(session_type_)) {
-    LOG(ERROR) << __func__ << "device=0x"
-               << android::base::StringPrintf("%08x", device)
-               << ", session_type=" << toString(session_type_)
-               << " is not ready";
+    LOG(ERROR) << __func__ << ": device=0x" << StringPrintf("%08x", device)
+               << ", session_type=" << toString(session_type_) << " is not ready";
     return false;
   }
   return true;
@@ -179,14 +173,13 @@
 
 void BluetoothAudioPortOut::TearDown() {
   if (!in_use()) {
-    LOG(ERROR) << __func__ << ": session_type=" << toString(session_type_)
-               << ", cookie=0x" << android::base::StringPrintf("%04hx", cookie_)
-               << " unknown monitor";
+    LOG(ERROR) << __func__ << ": session_type=" << toString(session_type_) << ", cookie=0x"
+               << StringPrintf("%04hx", cookie_) << " unknown monitor";
     return;
   }
 
-  LOG(INFO) << __func__ << ": session_type=" << toString(session_type_)
-            << ", cookie=0x" << android::base::StringPrintf("%04hx", cookie_);
+  LOG(INFO) << __func__ << ": session_type=" << toString(session_type_) << ", cookie=0x"
+            << StringPrintf("%04hx", cookie_);
   BluetoothAudioSessionControl::UnregisterControlResultCback(session_type_,
                                                              cookie_);
   cookie_ = android::bluetooth::audio::kObserversCookieUndefined;
@@ -200,9 +193,8 @@
   }
   std::unique_lock<std::mutex> port_lock(cv_mutex_);
   BluetoothStreamState previous_state = state_;
-  LOG(INFO) << "control_result_cb: session_type=" << toString(session_type_)
-            << ", cookie=0x" << android::base::StringPrintf("%04hx", cookie_)
-            << ", previous_state=" << previous_state
+  LOG(INFO) << "control_result_cb: session_type=" << toString(session_type_) << ", cookie=0x"
+            << StringPrintf("%04hx", cookie_) << ", previous_state=" << previous_state
             << ", status=" << toString(status);
 
   switch (previous_state) {
@@ -210,12 +202,10 @@
       if (status == BluetoothAudioStatus::SUCCESS) {
         state_ = BluetoothStreamState::STARTED;
       } else {
-        // Set to standby since the stack may be busy switching
-        LOG(ERROR) << "control_result_cb: status=" << toString(status)
-                   << " failure for session_type=" << toString(session_type_)
-                   << ", cookie=0x"
-                   << android::base::StringPrintf("%04hx", cookie_)
-                   << ", previous_state=" << previous_state;
+        // Set to standby since the stack may be busy switching between outputs
+        LOG(WARNING) << "control_result_cb: status=" << toString(status)
+                     << " failure for session_type=" << toString(session_type_) << ", cookie=0x"
+                     << StringPrintf("%04hx", cookie_) << ", previous_state=" << previous_state;
         state_ = BluetoothStreamState::STANDBY;
       }
       break;
@@ -223,20 +213,17 @@
       if (status == BluetoothAudioStatus::SUCCESS) {
         state_ = BluetoothStreamState::STANDBY;
       } else {
-        // This should never fail and we need to recover
-        LOG(FATAL) << "control_result_cb: status=" << toString(status)
-                   << " failure for session_type=" << toString(session_type_)
-                   << ", cookie=0x"
-                   << android::base::StringPrintf("%04hx", cookie_)
-                   << ", previous_state=" << previous_state;
+        // It will be failed if the headset is disconnecting, and set to disable
+        // to wait for re-init again
+        LOG(WARNING) << "control_result_cb: status=" << toString(status)
+                     << " failure for session_type=" << toString(session_type_) << ", cookie=0x"
+                     << StringPrintf("%04hx", cookie_) << ", previous_state=" << previous_state;
         state_ = BluetoothStreamState::DISABLED;
       }
       break;
     default:
       LOG(ERROR) << "control_result_cb: unexpected status=" << toString(status)
-                 << " for session_type=" << toString(session_type_)
-                 << ", cookie=0x"
-                 << android::base::StringPrintf("%04hx", cookie_)
+                 << " for session_type=" << toString(session_type_) << ", cookie=0x" << StringPrintf("%04hx", cookie_)
                  << ", previous_state=" << previous_state;
       return;
   }
@@ -251,9 +238,8 @@
   }
   std::unique_lock<std::mutex> port_lock(cv_mutex_);
   BluetoothStreamState previous_state = state_;
-  LOG(INFO) << "session_changed_cb: session_type=" << toString(session_type_)
-            << ", cookie=0x" << android::base::StringPrintf("%04hx", cookie_)
-            << ", previous_state=" << previous_state;
+  LOG(INFO) << "session_changed_cb: session_type=" << toString(session_type_) << ", cookie=0x"
+            << StringPrintf("%04hx", cookie_) << ", previous_state=" << previous_state;
   if (previous_state != BluetoothStreamState::DISABLED) {
     state_ = BluetoothStreamState::DISABLED;
   } else {
@@ -286,10 +272,8 @@
     return false;
   }
   const PcmParameters& pcm_cfg = hal_audio_cfg.pcmConfig();
-  LOG(VERBOSE) << __func__ << ": session_type=" << toString(session_type_)
-               << ", cookie=0x" << android::base::StringPrintf("%04hx", cookie_)
-               << ", state=" << state_ << ", PcmConfig=[" << toString(pcm_cfg)
-               << "]";
+  LOG(VERBOSE) << __func__ << ": session_type=" << toString(session_type_) << ", cookie=0x"
+               << StringPrintf("%04hx", cookie_) << ", state=" << state_ << ", PcmConfig=[" << toString(pcm_cfg) << "]";
   if (pcm_cfg.sampleRate == SampleRate::RATE_UNKNOWN ||
       pcm_cfg.channelMode == ChannelMode::UNKNOWN ||
       pcm_cfg.bitsPerSample == BitsPerSample::BITS_UNKNOWN) {
@@ -306,30 +290,24 @@
   std::unique_lock<std::mutex> port_lock(cv_mutex_);
   switch (state) {
     case BluetoothStreamState::STARTING:
-      LOG(VERBOSE) << __func__ << ": session_type=" << toString(session_type_)
-                   << ", cookie=0x"
-                   << android::base::StringPrintf("%04hx", cookie_)
-                   << " waiting for STARTED";
+      LOG(VERBOSE) << __func__ << ": session_type=" << toString(session_type_) << ", cookie=0x"
+                   << StringPrintf("%04hx", cookie_) << " waiting for STARTED";
       retval = internal_cv_.wait_for(
           port_lock, std::chrono::milliseconds(kMaxWaitingTimeMs),
           [this] { return this->state_ != BluetoothStreamState::STARTING; });
       retval = retval && state_ == BluetoothStreamState::STARTED;
       break;
     case BluetoothStreamState::SUSPENDING:
-      LOG(VERBOSE) << __func__ << ": session_type=" << toString(session_type_)
-                   << ", cookie=0x"
-                   << android::base::StringPrintf("%04hx", cookie_)
-                   << " waiting for SUSPENDED";
+      LOG(VERBOSE) << __func__ << ": session_type=" << toString(session_type_) << ", cookie=0x"
+                   << StringPrintf("%04hx", cookie_) << " waiting for SUSPENDED";
       retval = internal_cv_.wait_for(
           port_lock, std::chrono::milliseconds(kMaxWaitingTimeMs),
           [this] { return this->state_ != BluetoothStreamState::SUSPENDING; });
       retval = retval && state_ == BluetoothStreamState::STANDBY;
       break;
     default:
-      LOG(WARNING) << __func__ << ": session_type=" << toString(session_type_)
-                   << ", cookie=0x"
-                   << android::base::StringPrintf("%04hx", cookie_)
-                   << " waiting for KNOWN";
+      LOG(WARNING) << __func__ << ": session_type=" << toString(session_type_) << ", cookie=0x"
+                   << StringPrintf("%04hx", cookie_) << " waiting for KNOWN";
       return false;
   }
 
@@ -342,30 +320,25 @@
     return false;
   }
 
-  LOG(INFO) << __func__ << ": session_type=" << toString(session_type_)
-            << ", cookie=0x" << android::base::StringPrintf("%04hx", cookie_)
-            << ", state=" << state_ << " request";
+  LOG(INFO) << __func__ << ": session_type=" << toString(session_type_) << ", cookie=0x"
+            << StringPrintf("%04hx", cookie_) << ", state=" << state_ << " request";
   bool retval = false;
   if (state_ == BluetoothStreamState::STANDBY) {
     state_ = BluetoothStreamState::STARTING;
     if (BluetoothAudioSessionControl::StartStream(session_type_)) {
       retval = CondwaitState(BluetoothStreamState::STARTING);
     } else {
-      LOG(ERROR) << __func__ << ": session_type=" << toString(session_type_)
-                 << ", cookie=0x"
-                 << android::base::StringPrintf("%04hx", cookie_)
-                 << ", state=" << state_ << " Hal fails";
+      LOG(ERROR) << __func__ << ": session_type=" << toString(session_type_) << ", cookie=0x"
+                 << StringPrintf("%04hx", cookie_) << ", state=" << state_ << " Hal fails";
     }
   }
 
   if (retval) {
-    LOG(INFO) << __func__ << ": session_type=" << toString(session_type_)
-              << ", cookie=0x" << android::base::StringPrintf("%04hx", cookie_)
-              << ", state=" << state_ << " done";
+    LOG(INFO) << __func__ << ": session_type=" << toString(session_type_) << ", cookie=0x"
+              << StringPrintf("%04hx", cookie_) << ", state=" << state_ << " done";
   } else {
-    LOG(ERROR) << __func__ << ": session_type=" << toString(session_type_)
-               << ", cookie=0x" << android::base::StringPrintf("%04hx", cookie_)
-               << ", state=" << state_ << " failure";
+    LOG(ERROR) << __func__ << ": session_type=" << toString(session_type_) << ", cookie=0x"
+               << StringPrintf("%04hx", cookie_) << ", state=" << state_ << " failure";
   }
 
   return retval;  // false if any failure like timeout
@@ -377,30 +350,25 @@
     return false;
   }
 
-  LOG(INFO) << __func__ << ": session_type=" << toString(session_type_)
-            << ", cookie=0x" << android::base::StringPrintf("%04hx", cookie_)
-            << ", state=" << state_ << " request";
+  LOG(INFO) << __func__ << ": session_type=" << toString(session_type_) << ", cookie=0x"
+            << StringPrintf("%04hx", cookie_) << ", state=" << state_ << " request";
   bool retval = false;
   if (state_ == BluetoothStreamState::STARTED) {
     state_ = BluetoothStreamState::SUSPENDING;
     if (BluetoothAudioSessionControl::SuspendStream(session_type_)) {
       retval = CondwaitState(BluetoothStreamState::SUSPENDING);
     } else {
-      LOG(ERROR) << __func__ << ": session_type=" << toString(session_type_)
-                 << ", cookie=0x"
-                 << android::base::StringPrintf("%04hx", cookie_)
-                 << ", state=" << state_ << " Hal fails";
+      LOG(ERROR) << __func__ << ": session_type=" << toString(session_type_) << ", cookie=0x"
+                 << StringPrintf("%04hx", cookie_) << ", state=" << state_ << " Hal fails";
     }
   }
 
   if (retval) {
-    LOG(INFO) << __func__ << ": session_type=" << toString(session_type_)
-              << ", cookie=0x" << android::base::StringPrintf("%04hx", cookie_)
-              << ", state=" << state_ << " done";
+    LOG(INFO) << __func__ << ": session_type=" << toString(session_type_) << ", cookie=0x"
+              << StringPrintf("%04hx", cookie_) << ", state=" << state_ << " done";
   } else {
-    LOG(ERROR) << __func__ << ": session_type=" << toString(session_type_)
-               << ", cookie=0x" << android::base::StringPrintf("%04hx", cookie_)
-               << ", state=" << state_ << " failure";
+    LOG(ERROR) << __func__ << ": session_type=" << toString(session_type_) << ", cookie=0x"
+               << StringPrintf("%04hx", cookie_) << ", state=" << state_ << " failure";
   }
 
   return retval;  // false if any failure like timeout
@@ -411,14 +379,12 @@
     LOG(ERROR) << __func__ << ": BluetoothAudioPortOut is not in use";
     return;
   }
-  LOG(INFO) << __func__ << ": session_type=" << toString(session_type_)
-            << ", cookie=0x" << android::base::StringPrintf("%04hx", cookie_)
-            << ", state=" << state_ << " request";
+  LOG(INFO) << __func__ << ": session_type=" << toString(session_type_) << ", cookie=0x"
+            << StringPrintf("%04hx", cookie_) << ", state=" << state_ << " request";
   state_ = BluetoothStreamState::DISABLED;
   BluetoothAudioSessionControl::StopStream(session_type_);
-  LOG(INFO) << __func__ << ": session_type=" << toString(session_type_)
-            << ", cookie=0x" << android::base::StringPrintf("%04hx", cookie_)
-            << ", state=" << state_ << " done";
+  LOG(INFO) << __func__ << ": session_type=" << toString(session_type_) << ", cookie=0x"
+            << StringPrintf("%04hx", cookie_) << ", state=" << state_ << " done";
 }
 
 size_t BluetoothAudioPortOut::WriteData(const void* buffer,
@@ -437,14 +403,10 @@
   }
   bool retval = BluetoothAudioSessionControl::GetPresentationPosition(
       session_type_, delay_ns, bytes, timestamp);
-  LOG(VERBOSE) << __func__ << ": session_type=0x"
-               << android::base::StringPrintf("%02hhx", session_type_)
-               << ", cookie=0x" << android::base::StringPrintf("%04hx", cookie_)
-               << ", state=" << state_ << ", delay=" << *delay_ns
-               << "ns, data=" << *bytes
-               << " bytes, timestamp=" << timestamp->tv_sec << "."
-               << android::base::StringPrintf("%09ld", timestamp->tv_nsec)
-               << "s";
+  LOG(VERBOSE) << __func__ << ": session_type=0x" << StringPrintf("%02hhx", session_type_) << ", cookie=0x"
+               << StringPrintf("%04hx", cookie_) << ", state=" << state_ << ", delay=" << *delay_ns
+               << "ns, data=" << *bytes << " bytes, timestamp=" << timestamp->tv_sec << "."
+               << StringPrintf("%09ld", timestamp->tv_nsec) << "s";
 
   return retval;
 }
@@ -455,9 +417,8 @@
     LOG(ERROR) << __func__ << ": BluetoothAudioPortOut is not in use";
     return;
   }
-  LOG(DEBUG) << __func__ << ": session_type=" << toString(session_type_)
-             << ", cookie=0x" << android::base::StringPrintf("%04hx", cookie_)
-             << ", state=" << state_ << ", " << source_metadata->track_count
+  LOG(DEBUG) << __func__ << ": session_type=" << toString(session_type_) << ", cookie=0x"
+             << StringPrintf("%04hx", cookie_) << ", state=" << state_ << ", " << source_metadata->track_count
              << " track(s)";
   if (source_metadata->track_count == 0) return;
   BluetoothAudioSessionControl::UpdateTracksMetadata(session_type_,
diff --git a/audio_hal_interface/client_interface.cc b/audio_hal_interface/client_interface.cc
index cc1c96e..e83ae84 100644
--- a/audio_hal_interface/client_interface.cc
+++ b/audio_hal_interface/client_interface.cc
@@ -75,7 +75,7 @@
       auto hidl_retval =
           provider_->streamStarted(BluetoothAudioCtrlAckToHalStatus(ack));
       if (!hidl_retval.isOk()) {
-        LOG(FATAL) << __func__ << ": BluetoothAudioHal Failure";
+        LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description();
       }
     }
     return Void();
@@ -87,7 +87,7 @@
       auto hidl_retval =
           provider_->streamSuspended(BluetoothAudioCtrlAckToHalStatus(ack));
       if (!hidl_retval.isOk()) {
-        LOG(FATAL) << __func__ << ": BluetoothAudioHal Failure";
+        LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description();
       }
     }
     return Void();
@@ -200,7 +200,7 @@
   if (provider_ != nullptr) {
     auto hidl_retval = provider_->unlinkToDeath(death_recipient_);
     if (!hidl_retval.isOk()) {
-      LOG(ERROR) << __func__ << ": BluetoothAudioDeathRecipient Failure";
+      LOG(FATAL) << __func__ << ": BluetoothAudioDeathRecipient failure: " << hidl_retval.description();
     }
   }
 }
@@ -237,10 +237,10 @@
       sink_->GetSessionType(), getProviderCapabilities_cb);
   getProviderCapabilities_future.get();
   if (!hidl_retval.isOk()) {
-    LOG(FATAL) << __func__
-               << ": BluetoothAudioHal::getProviderCapabilities Failure";
+    LOG(FATAL) << __func__ << ": BluetoothAudioHal::getProviderCapabilities failure: " << hidl_retval.description();
     return;
-  } else if (capabilities_.empty()) {
+  }
+  if (capabilities_.empty()) {
     LOG(WARNING) << __func__
                  << ": SessionType=" << toString(sink_->GetSessionType())
                  << " Not supported by BluetoothAudioHal";
@@ -267,12 +267,12 @@
       providersFactory->openProvider(sink_->GetSessionType(), openProvider_cb);
   openProvider_future.get();
   if (!hidl_retval.isOk()) {
-    LOG(FATAL) << __func__ << ": BluetoothAudioHal::openProvider Failure";
+    LOG(FATAL) << __func__ << ": BluetoothAudioHal::openProvider failure: " << hidl_retval.description();
   }
   CHECK(provider_ != nullptr);
 
   if (!provider_->linkToDeath(death_recipient_, 0).isOk()) {
-    LOG(FATAL) << __func__ << ": BluetoothAudioDeathRecipient Failure";
+    LOG(FATAL) << __func__ << ": BluetoothAudioDeathRecipient failure: " << hidl_retval.description();
   }
 
   LOG(INFO) << "IBluetoothAudioProvidersFactory::openProvider() returned "
@@ -338,7 +338,7 @@
       stack_if, sink_->GetAudioConfiguration(), hidl_cb);
   hidl_startSession_future.get();
   if (!hidl_retval.isOk()) {
-    LOG(FATAL) << __func__ << ": BluetoothAudioHal Failure";
+    LOG(FATAL) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description();
     return -EPROTO;
   }
 
@@ -368,14 +368,15 @@
   if (provider_ == nullptr) {
     LOG(ERROR) << __func__ << ": BluetoothAudioHal nullptr";
     return;
-  } else if (ack == BluetoothAudioCtrlAck::PENDING) {
+  }
+  if (ack == BluetoothAudioCtrlAck::PENDING) {
     LOG(INFO) << __func__ << ": " << ack << " ignored";
     return;
   }
   BluetoothAudioStatus status = BluetoothAudioCtrlAckToHalStatus(ack);
   auto hidl_retval = provider_->streamStarted(status);
   if (!hidl_retval.isOk()) {
-    LOG(FATAL) << __func__ << ": BluetoothAudioHal Failure";
+    LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description();
   }
 }
 
@@ -384,14 +385,15 @@
   if (provider_ == nullptr) {
     LOG(ERROR) << __func__ << ": BluetoothAudioHal nullptr";
     return;
-  } else if (ack == BluetoothAudioCtrlAck::PENDING) {
+  }
+  if (ack == BluetoothAudioCtrlAck::PENDING) {
     LOG(INFO) << __func__ << ": " << ack << " ignored";
     return;
   }
   BluetoothAudioStatus status = BluetoothAudioCtrlAckToHalStatus(ack);
   auto hidl_retval = provider_->streamSuspended(status);
   if (!hidl_retval.isOk()) {
-    LOG(FATAL) << __func__ << ": BluetoothAudioHal Failure";
+    LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description();
   }
 }
 
@@ -410,7 +412,7 @@
   mDataMQ = nullptr;
   auto hidl_retval = provider_->endSession();
   if (!hidl_retval.isOk()) {
-    LOG(FATAL) << __func__ << ": BluetoothAudioHal Failure";
+    LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description();
     return -EPROTO;
   }
   return 0;
diff --git a/bta/hearing_aid/hearing_aid.cc b/bta/hearing_aid/hearing_aid.cc
index a96d9de..885c062 100644
--- a/bta/hearing_aid/hearing_aid.cc
+++ b/bta/hearing_aid/hearing_aid.cc
@@ -171,6 +171,24 @@
 g722_encode_state_t* encoder_state_left = nullptr;
 g722_encode_state_t* encoder_state_right = nullptr;
 
+inline void encoder_state_init() {
+  if (encoder_state_left != nullptr) {
+    LOG(WARNING) << __func__ << ": encoder already initialized";
+    return;
+  }
+  encoder_state_left = g722_encode_init(nullptr, 64000, G722_PACKED);
+  encoder_state_right = g722_encode_init(nullptr, 64000, G722_PACKED);
+}
+
+inline void encoder_state_release() {
+  if (encoder_state_left != nullptr) {
+    g722_encode_release(encoder_state_left);
+    encoder_state_left = nullptr;
+    g722_encode_release(encoder_state_right);
+    encoder_state_right = nullptr;
+  }
+}
+
 class HearingAidImpl : public HearingAid {
  private:
   // Keep track of whether the Audio Service has resumed audio playback
@@ -846,8 +864,7 @@
     VLOG(0) << __func__ << ": device=" << hearingDevice.address;
 
     if (encoder_state_left == nullptr) {
-      encoder_state_left = g722_encode_init(nullptr, 64000, G722_PACKED);
-      encoder_state_right = g722_encode_init(nullptr, 64000, G722_PACKED);
+      encoder_state_init();
       seq_counter = 0;
 
       // use the best codec avaliable for this pair of devices.
@@ -909,12 +926,8 @@
     audio_running = true;
 
     // TODO: shall we also reset the encoder ?
-    if (encoder_state_left != nullptr) {
-      g722_encode_release(encoder_state_left);
-      g722_encode_release(encoder_state_right);
-    }
-    encoder_state_left = g722_encode_init(nullptr, 64000, G722_PACKED);
-    encoder_state_right = g722_encode_init(nullptr, 64000, G722_PACKED);
+    encoder_state_release();
+    encoder_state_init();
     seq_counter = 0;
 
     for (auto& device : hearingDevices.devices) {
@@ -1002,12 +1015,7 @@
 
     if (left == nullptr && right == nullptr) {
       HearingAidAudioSource::Stop();
-      if (encoder_state_left != nullptr) {
-        g722_encode_release(encoder_state_left);
-        encoder_state_left = nullptr;
-        g722_encode_release(encoder_state_right);
-        encoder_state_right = nullptr;
-      }
+      encoder_state_release();
       current_volume = VOLUME_UNKNOWN;
       return;
     }
@@ -1360,6 +1368,8 @@
     }
 
     hearingDevices.devices.clear();
+
+    encoder_state_release();
   }
 
  private:
diff --git a/bta/hf_client/bta_hf_client_main.cc b/bta/hf_client/bta_hf_client_main.cc
index 9bb8ab5..fac61c7 100644
--- a/bta/hf_client/bta_hf_client_main.cc
+++ b/bta/hf_client/bta_hf_client_main.cc
@@ -329,6 +329,8 @@
     client_cb->state = BTA_HF_CLIENT_OPENING_ST;
     tBTA_HF_CLIENT_DATA msg;
     msg.hdr.layer_specific = client_cb->handle;
+    msg.api_open.bd_addr = client_cb->peer_addr;
+    msg.api_open.sec_mask = client_cb->cli_sec_mask;
     bta_hf_client_start_open(&msg);
   }
 }
@@ -749,19 +751,14 @@
     evt.bd_addr = client_cb->peer_addr;
     if (client_cb->state == BTA_HF_CLIENT_INIT_ST) {
       bta_hf_client_app_callback(BTA_HF_CLIENT_CLOSE_EVT, &evt);
+      APPL_TRACE_DEBUG("%s: marking CB handle %d to false", __func__, client_cb->handle);
+      client_cb->is_allocated = false;
     } else if (client_cb->state == BTA_HF_CLIENT_OPEN_ST) {
       evt.open.handle = client_cb->handle;
       bta_hf_client_app_callback(BTA_HF_CLIENT_OPEN_EVT, &evt);
     }
   }
 
-  /* if the next state is INIT then release the cb for future use */
-  if (client_cb->state == BTA_HF_CLIENT_INIT_ST) {
-    APPL_TRACE_DEBUG("%s: marking CB handle %d to false", __func__,
-                     client_cb->handle);
-    client_cb->is_allocated = false;
-  }
-
   VLOG(2) << __func__ << ": device " << client_cb->peer_addr
           << "state change: [" << bta_hf_client_state_str(in_state) << "] -> ["
           << bta_hf_client_state_str(client_cb->state) << "] after Event ["
diff --git a/btif/src/btif_rc.cc b/btif/src/btif_rc.cc
index f8cd625..9919a7b 100644
--- a/btif/src/btif_rc.cc
+++ b/btif/src/btif_rc.cc
@@ -1365,8 +1365,6 @@
 static uint8_t fill_attribute_id_array(
     uint8_t cmd_attribute_number, btrc_media_attr_t* cmd_attribute_id_array,
     size_t out_array_size, btrc_media_attr_t* out_attribute_id_array) {
-  /* Reset attribute array */
-  memset(out_attribute_id_array, 0, out_array_size);
   /* Default case for cmd_attribute_number == 0xFF, No attribute */
   uint8_t out_attribute_number = 0;
   if (cmd_attribute_number == 0) {
@@ -1438,7 +1436,7 @@
                            AVRC_STS_BAD_CMD, pavrc_cmd->cmd.opcode);
     } break;
     case AVRC_PDU_GET_ELEMENT_ATTR: {
-      btrc_media_attr_t element_attrs[BTRC_MAX_ELEM_ATTR_SIZE];
+      btrc_media_attr_t element_attrs[BTRC_MAX_ELEM_ATTR_SIZE] = {};
       uint8_t num_attr = fill_attribute_id_array(
           pavrc_cmd->get_elem_attrs.num_attr,
           (btrc_media_attr_t*)pavrc_cmd->get_elem_attrs.attrs,
@@ -1485,7 +1483,7 @@
     } break;
 
     case AVRC_PDU_GET_FOLDER_ITEMS: {
-      uint32_t attr_ids[BTRC_MAX_ELEM_ATTR_SIZE];
+      uint32_t attr_ids[BTRC_MAX_ELEM_ATTR_SIZE] = {0};
       uint8_t num_attr;
       num_attr = pavrc_cmd->get_items.attr_count;
 
@@ -1576,7 +1574,7 @@
     } break;
 
     case AVRC_PDU_GET_ITEM_ATTRIBUTES: {
-      btrc_media_attr_t item_attrs[BTRC_MAX_ELEM_ATTR_SIZE];
+      btrc_media_attr_t item_attrs[BTRC_MAX_ELEM_ATTR_SIZE] = {};
       uint8_t num_attr = fill_attribute_id_array(
           pavrc_cmd->get_attrs.attr_count,
           (btrc_media_attr_t*)pavrc_cmd->get_attrs.p_attr_list,
@@ -2045,7 +2043,7 @@
         } break;
 
         case AVRC_ITEM_MEDIA: {
-          tAVRC_ATTR_ENTRY attr_vals[BTRC_MAX_ELEM_ATTR_SIZE];
+          tAVRC_ATTR_ENTRY attr_vals[BTRC_MAX_ELEM_ATTR_SIZE] = {};
 
           memcpy(item.u.media.uid, cur_item->media.uid, sizeof(tAVRC_UID));
           item.u.media.type = cur_item->media.type;
diff --git a/stack/l2cap/l2c_fcr.cc b/stack/l2cap/l2c_fcr.cc
index 6e8e34c..857a0bf 100644
--- a/stack/l2cap/l2c_fcr.cc
+++ b/stack/l2cap/l2c_fcr.cc
@@ -835,7 +835,16 @@
   }
 
   if (p_ccb->is_first_seg) {
+    if (p_buf->len < sizeof(sdu_length)) {
+      L2CAP_TRACE_ERROR("%s: buffer length=%d too small. Need at least 2.",
+                        __func__, p_buf->len);
+      android_errorWriteWithInfoLog(0x534e4554, "120665616", -1, NULL, 0);
+      /* Discard the buffer */
+      osi_free(p_buf);
+      return;
+    }
     STREAM_TO_UINT16(sdu_length, p);
+
     /* Check the SDU Length with local MTU size */
     if (sdu_length > p_ccb->local_conn_cfg.mtu) {
       /* Discard the buffer */
@@ -844,6 +853,7 @@
     }
 
     p_buf->len -= sizeof(sdu_length);
+    p_buf->offset += sizeof(sdu_length);
 
     if (sdu_length < p_buf->len) {
       L2CAP_TRACE_ERROR("%s: Invalid sdu_length: %d", __func__, sdu_length);
@@ -863,7 +873,6 @@
     p_data->len = 0;
     p_ccb->ble_sdu_length = sdu_length;
     L2CAP_TRACE_DEBUG("%s SDU Length = %d", __func__, sdu_length);
-    p_buf->offset += sizeof(sdu_length);
     p_data->offset = 0;
 
   } else {