新增 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檔案以將實作檔案 ( modulename Configs.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. 如果需要,請新增 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) \