Use flushcommand before BTConfigStore close

When LAZY HAL is enabled, flushcommands function is called
before dereferencing btConfigStore. This will decrement the
HIDL usage count by 1.

Change-Id: Iba5422080e948a1492a22112d1549095a550342a
diff --git a/system_bt_ext/btconfigstore/bt_configstore.cc b/system_bt_ext/btconfigstore/bt_configstore.cc
index 903520c..ffb82c7 100644
--- a/system_bt_ext/btconfigstore/bt_configstore.cc
+++ b/system_bt_ext/btconfigstore/bt_configstore.cc
@@ -49,12 +49,15 @@
 
 #include <hwbinder/ProcessState.h>
 #include <string.h>
+#include <hwbinder/IPCThreadState.h>
+#include <cutils/properties.h>
 
 /* max platform record must be equal to the predefined max num
    of platform in bt_configstore.conf */
 #define MAX_PLATFORM_PROP_RECORD 12
 #define BT_CONFIG_STORE_PATH "/etc/bluetooth/bt_configstore.conf"
 
+using android::hardware::IPCThreadState;
 using ::vendor::qti::hardware::btconfigstore::V1_0::IBTConfigStore;
 using ::vendor::qti::hardware::btconfigstore::V1_0::VendorProperty;
 using ::vendor::qti::hardware::btconfigstore::V1_0::AddOnFeaturesList;
@@ -79,6 +82,7 @@
 static bt_soc_type_t convertSocNameToBTSocType(const char * name);
 static const char * convertPropTypeToStringFormat(uint32_t propType);
 
+const bool IsLazyHalSupported(property_get_bool("ro.vendor.bt.enablelazyhal", false));
 
 EXPORT_SYMBOL bt_configstore_interface_t btConfigStoreInterface = {
     sizeof(btConfigStoreInterface),
@@ -89,6 +93,7 @@
     convertPropTypeToStringFormat,
 };
 
+
 /*******************************************************************************
 **
 ** Function         getVendorProperties
@@ -104,6 +109,8 @@
 *******************************************************************************/
 static bool getVendorProperties(uint32_t vPropType, std::vector<vendor_property_t> &vPropList)
 {
+  bool status = false;
+
   LOG_INFO(LOG_TAG, "%s ", __func__);
 
   if (btConfigStore == nullptr)
@@ -131,18 +138,23 @@
         LOG_INFO(LOG_TAG, "prop type: %s, prop_value: %s",
             convertPropTypeToStringFormat(vProp.type), vProp.value);
       }
-      return true;
+      status = true;
     }
   } else {
     LOG_WARN(LOG_TAG,"%s btConfigStore hal interface is null", __func__);
     if (btConfigStoreLoadProperties(vPropType, vPropList)){
       LOG_INFO(LOG_TAG, "Properties are successfully read from %s",
           BT_CONFIG_STORE_PATH);
-      return true;
+
+      status = true;
     }
   }
 
-  return false;
+  if (IsLazyHalSupported && btConfigStore != nullptr)
+    IPCThreadState::self()->flushCommands();
+
+  btConfigStore = nullptr;
+  return status;
 }
 
 /*******************************************************************************
@@ -160,6 +172,8 @@
 *******************************************************************************/
 static bool setVendorProperty(uint32_t type, const char * value)
 {
+  bool status = false;
+
   LOG_INFO(LOG_TAG, "%s ", __func__);
 
   if (btConfigStore == nullptr)
@@ -173,12 +187,17 @@
     LOG_INFO(LOG_TAG, "%s:: halResult = %d", __func__, halResult);
 
     if (halResult == Result::SUCCESS){
-      return true;
+      status = true;
     }
   } else {
     LOG_WARN(LOG_TAG, "%s btConfigStore is null", __func__);
   }
-  return false;
+
+  if (IsLazyHalSupported && btConfigStore != nullptr)
+    IPCThreadState::self()->flushCommands();
+
+  btConfigStore = nullptr;
+  return status;
 }
 
 /*******************************************************************************
@@ -196,6 +215,8 @@
 *******************************************************************************/
 static bool getAddOnFeatures(add_on_features_list_t *features_list)
 {
+  bool status = false;
+
   LOG_INFO(LOG_TAG, "%s ", __func__);
 
   if (btConfigStore == nullptr)
@@ -232,13 +253,17 @@
           "%s:: product_id = %d, version = %d, feat_mask_len = %d features data: %s",
           __func__, features_list->product_id, features_list->rsp_version,
           features_list->feat_mask_len, features);
-      return true;
+      status = true;
     }
   } else {
     LOG_WARN(LOG_TAG, "%s add feature is not avaliable", __func__);
   }
 
-  return false;
+  if (IsLazyHalSupported && btConfigStore != nullptr)
+    IPCThreadState::self()->flushCommands();
+
+  btConfigStore = nullptr;
+  return status;
 }
 
 /*******************************************************************************