您可以為現有的接口類添加新的 ConfigStore 項(即接口方法)。如果未定義接口類,則必須先添加一個新類,然後才能為該類添加 ConfigStore 項。本節使用將healthd添加到IChargerConfigs接口類的disableInitBlank配置項的示例。
添加接口類
如果沒有為要添加的接口方法定義接口類,則必須先添加接口類,然後才能添加關聯的 ConfigStore 項。
- 創建一個 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 }; - 更新 ConfigStore 共享庫和頭文件的
Android.bp和Android.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::IChargerConfigshidl-gen -o hardware/interfaces/configstore/1.0/default -Landroidbp -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport android.hardware.configstore@1.0::IChargerConfigshardware/interfaces/configstore/1.0中的Android.bp和Android.mk。 - 生成用於實現服務器代碼的 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.h和ChargerConfigs.cpp。 - 打開
.h和.cpp實現文件並刪除與函數HIDL_FETCH_ name相關的代碼(例如HIDL_FETCH_IChargerConfigs)。 HIDL 直通模式需要此功能,ConfigStore 不使用此功能。 - 將實現註冊到 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 } - 修改
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
- (可選)添加清單條目。如果不存在,則默認為 ConfigStore 的“默認”實例名稱。例如,在
device/google/marlin/manifest.xml中:<hal format="hidl"> <name>android.hardware.configstore</name> ... <interface> <name>IChargerConfigs</name> <instance>default</instance> </interface> </hal> - 如果需要,添加 sepolicy 規則(也就是說,如果客戶端沒有權限對 hal_configstore 進行
hal_configstore調用)。例如,在system/sepolicy/private/healthd.te:... // other rules binder_call(healthd, hal_configstore)
添加新的 ConfigStore 項
添加新的 ConfigStore 項:
- 打開HAL文件,為item添加所需的接口方法。 (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); }; - 在相應的接口 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 項:
- 包括所需的頭文件。例如,在
system/core/healthd/healthd.cpp:#include <android/hardware/configstore/1.0/IChargerConfigs.h> #include <configstore/Utils.h>
- 使用
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 服務以稍後調用實例化模板函數。 - 在
Android.mk或Android.bp中添加對 ConfigStore 和configstore-utils庫的依賴。例如,在system/core/healthd/Android.mk中:LOCAL_SHARED_LIBRARIES := \ android.hardware.configstore@1.0 \ android.hardware.configstore-utils \ ... (other libraries) \