新增 ConfigStore 類別和項目

您可為現有的介面類別新增 ConfigStore 項目 (即介面方法)。如未定義介面類別,則必須先新增類別,才能為該類別新增 ConfigStore 項目。本節會使用將 healthddisableInitBlank 設定項目範例新增至 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 的「預設」執行個體名稱。例如,在 device/google/marlin/manifest.xml 中:
        <hal format="hidl">
            <name>android.hardware.configstore</name>
            ...
            <interface>
                <name>IChargerConfigs</name>
                <instance>default</instance>
            </interface>
        </hal>
    
  8. 視需要新增加密規則 (也就是用戶端沒有向 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) \