新增 ConfigStore 類別和項目

您可以為現有的介面類別新增 ConfigStore 項目 (即介面方法)。如果未定義介面類別,您必須先新增類別,才能為該類別新增 ConfigStore 項目。本節以 disableInitBlank 設定項目為例,說明如何將 healthd 新增至 IChargerConfigs 介面類別。

新增介面類別

如果想新增的介面方法未定義任何介面類別,您必須先新增介面類別,才能新增相關聯的 ConfigStore 項目。

  1. 建立 HAL 介面檔案。ConfigStore 版本為 1.0,因此請在 hardware/interfaces/configstore/1.0 中定義 ConfigStore 介面。舉例來說,在 hardware/interfaces/configstore/1.0/IChargerConfigs.hal 中:
    package android.hardware.configstore@1.0;
    
    interface IChargerConfigs {
        // TO-BE-FILLED-BELOW
    };
    
  2. 更新 ConfigStore 共用程式庫和標頭檔案的 Android.bpAndroid.mk,納入新的介面 HAL。例如:
    hidl-gen -o hardware/interfaces/configstore/1.0/default -Lmakefile -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport android.hardware.configstore@1.0::IChargerConfigs
    hidl-gen -o hardware/interfaces/configstore/1.0/default -Landroidbp -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport android.hardware.configstore@1.0::IChargerConfigs
    
    這些指令會更新 hardware/interfaces/configstore/1.0 中的 Android.bpAndroid.mk
  3. 產生 C++ 虛設常式,用於實作伺服器程式碼。例如:
    hidl-gen -o hardware/interfaces/configstore/1.0/default -Lc++-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport android.hardware.configstore@1.0::IChargerConfigs
    
    這個指令會在 hardware/interfaces/configstore/1.0/default 中建立 ChargerConfigs.hChargerConfigs.cpp 這兩個檔案。
  4. 開啟 .h.cpp 實作檔案,並移除與 HIDL_FETCH_name 函式相關的程式碼 (例如 HIDL_FETCH_IChargerConfigs)。這個函式是 HIDL 直通模式的必要函式,但 ConfigStore 不會使用這種模式。
  5. 向 ConfigStore 服務註冊實作項目。例如,在 hardware/interfaces/configstore/1.0/default/service.cpp 中:
    #include <android/hardware/configstore/1.0/IChargerConfigs.h>
    #include "ChargerConfigs.h"
    
    using android::hardware::configstore::V1_0::IChargerConfigs;
    using android::hardware::configstore::V1_0::implementation::ChargerConfigs;
    
    int main() {
        ... // other code
        sp<IChargerConfigs> chargerConfigs = new ChargerConfigs;
        status = chargerConfigs->registerAsService();
        LOG_ALWAYS_FATAL_IF(status != OK, "Could not register IChargerConfigs");
        ... // other code
    }
    
  6. 修改 Android.mk 檔案,將實作檔案 (modulenameConfigs.cpp) 新增至 LOCAL_SRC_FILES,並將建構標記對應至巨集定義。例如,在 hardware/interfaces/configstore/1.0/default/Android.mk 中:
    LOCAL_SRC_FILES += ChargerConfigs.cpp
    
    ifeq ($(strip $(BOARD_CHARGER_DISABLE_INIT_BLANK)),true)
    LOCAL_CFLAGS += -DCHARGER_DISABLE_INIT_BLANK
    endif
    
  7. (選用) 新增資訊清單項目。如果不存在,則預設為 ConfigStore 的「default」執行個體名稱。例如,在 device/google/marlin/manifest.xml 中:
        <hal format="hidl">
            <name>android.hardware.configstore</name>
            ...
            <interface>
                <name>IChargerConfigs</name>
                <instance>default</instance>
            </interface>
        </hal>
    
  8. 視需要新增 sepolicy 規則 (也就是說,如果用戶端沒有權限可對 hal_configstore 進行 hwbinder 呼叫)。舉例來說,在 system/sepolicy/private/healthd.te 中:
    ... // other rules
    binder_call(healthd, hal_configstore)
    

新增 ConfigStore 項目

如要新增 ConfigStore 項目,請按照下列步驟操作:

  1. 開啟 HAL 檔案,然後新增項目的必要介面方法。(ConfigStore 的 .hal 檔案位於 hardware/interfaces/configstore/1.0 中)。舉例來說,在 hardware/interfaces/configstore/1.0/IChargerConfigs.hal 中:
    package android.hardware.configstore@1.0;
    
    interface IChargerConfigs {
        ... // Other interfaces
        disableInitBlank() generates(OptionalBool value);
    };
    
  2. 在對應的介面 HAL 實作檔案 (.h.cpp) 中實作方法。將預設實作放在 hardware/interfaces/configstore/1.0/default 中。 例如,在 hardware/interfaces/configstore/1.0/default/ChargerConfigs.h 中:
    struct ChargerConfigs : public IChargerConfigs {
        ... // Other interfaces
        Return<void> disableInitBlank(disableInitBlank_cb _hidl_cb) override;
    };
    
    hardware/interfaces/configstore/1.0/default/ChargerConfigs.cpp
    Return<void> ChargerConfigs::disableInitBlank(disableInitBlank_cb _hidl_cb) {
        bool value = false;
    #ifdef CHARGER_DISABLE_INIT_BLANK
        value = true;
    #endif
        _hidl_cb({true, value});
        return Void();
    }
    

使用 ConfigStore 項目

如要使用 ConfigStore 項目:

  1. 加入必要的標頭檔案。例如,在 system/core/healthd/healthd.cpp 中:
    #include <android/hardware/configstore/1.0/IChargerConfigs.h>
    #include <configstore/Utils.h>
    
  2. android.hardware.configstore-utils 中使用適當的範本函式存取 ConfigStore 項目。例如,在 system/core/healthd/healthd.cpp 中:
    using namespace android::hardware::configstore;
    using namespace android::hardware::configstore::V1_0;
    
    static int64_t disableInitBlank = getBool<
            IChargerConfigs,
            &IChargerConfigs::disableInitBlank>(false);
    
    在這個範例中,系統會擷取 ConfigStore 項目 disableInitBlank 並儲存至變數 (如果需要多次存取變數,這就很有用)。從 ConfigStore 擷取的值會快取在例項化的範本函式中,因此後續呼叫例項化的範本函式時,可以從快取值快速擷取,不必連線至 ConfigStore 服務。
  3. Android.mkAndroid.bp 中,新增對 ConfigStore 和 configstore-utils 程式庫的依附元件。例如,在 system/core/healthd/Android.mk 中:
    LOCAL_SHARED_LIBRARIES := \
        android.hardware.configstore@1.0 \
        android.hardware.configstore-utils \
        ... (other libraries) \