用戶端使用

您可以重構有條件編譯的程式碼,從 HAL 介面動態讀取值。例如:

#ifdef TARGET_FORCE_HWC_FOR_VIRTUAL_DISPLAYS
// some code fragment
#endif

架構程式碼隨後可根據其類型,呼叫 <configstore/Utils.h> 中定義的適當公用程式函式。

ConfigStore 範例

這個範例顯示讀取 TARGET_FORCE_HWC_FOR_VIRTUAL_DISPLAYS,在 ConfigStore HAL 中定義為 forceHwcForVirtualDisplays(),傳回型別為 OptionalBool

#include <configstore/Utils.h>
using namespace android::hardware::configstore;
using namespace android::hardware::configstore::V1_0;

static bool vsyncPhaseOffsetNs = getBool<ISurfaceFlingerConfigs,
        ISurfaceFlingerConfigs::forceHwcForVirtualDisplays>(false);

公用程式函式 (上例中的 getBool) 會與 configstore 服務聯絡,取得介面函式 Proxy 的控制代碼,然後透過 HIDL/hwbinder 叫用控制代碼,擷取值。

公用函式

<configstore/Utils.h> (configstore/1.0/include/configstore/Utils.h) 為每個原始傳回型別提供公用程式函式,包括 Optional[Bool|String|Int32|UInt32|Int64|UInt64],如下所示:

類型 函式 (省略範本參數)
OptionalBool bool getBool(const bool defValue)
OptionalInt32 int32_t getInt32(const int32_t defValue)
OptionalUInt32 uint32_t getUInt32(const uint32_t defValue)
OptionalInt64 int64_t getInt64(const int64_t defValue)
OptionalUInt64 uint64_t getUInt64(const uint64_t defValue)
OptionalString std::string getString(const std::string &defValue)

defValue 是預設值,當 HAL 實作未指定設定項目的值時,系統就會傳回這個值。每個函式都會採用兩個範本參數:

  • I 是介面類別名稱。
  • Func 是用於取得設定項目的成員函式指標。

由於設定值為唯讀且不會變更,公用程式函式會在內部快取設定值。後續呼叫會使用相同連結單元中的快取值,更有效率地提供服務。

使用 configstore-utils

ConfigStore HAL 的設計可向前相容於次要版本升級,也就是說,當 HAL 修訂完成,且部分架構程式碼使用新導入的項目時,仍可使用 /vendor 中次要版本較舊的 ConfigStore 服務。

為確保向前相容性,請確認導入方式符合下列規範:

  1. 如果只有舊版服務可用,新項目會使用預設值。範例:
    service = V1_1::IConfig::getService(); // null if V1_0 is installed
    value = DEFAULT_VALUE;
      if(service) {
        value = service->v1_1API(DEFAULT_VALUE);
      }
    
  2. 用戶端會使用包含 ConfigStore 項目在內的第一個介面。 範例:
    V1_1::IConfig::getService()->v1_0API(); // NOT ALLOWED
    
    V1_0::IConfig::getService()->v1_0API(); // OK
    
  3. 舊版介面可以擷取新版的服務。在以下範例中,如果安裝的版本是 v1_1,則必須為 getService() 傳回 v1_1 服務:
    V1_0::IConfig::getService()->v1_0API();
    

使用 configstore-utils 程式庫中的存取函式存取 ConfigStore 項目時,實作作業會確保 #1,編譯器錯誤則會確保 #2。因此,我們強烈建議您盡可能使用 configstore-utils