您可以為現有介面類別新增新的 ConfigStore 項目(即介面方法)。如果未定義介面類,則必須先新增一個新類,然後才能為該類新增 ConfigStore 項。本節使用將healthd
的disableInitBlank
配置項目新增至IChargerConfigs
介面類別的範例。
新增介面類
如果您要新增的介面方法沒有定義介面類,則必須先新增介面類,然後才能新增關聯的 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::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.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
進行 hwbinder 呼叫的權限)。例如,在system/sepolicy/private/healthd.te
中:... // other rules binder_call(healthd, hal_configstore)
新增新的 ConfigStore 項目
新增的 ConfigStore 項目:
- 開啟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); };
- 在對應的介面 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) \