您可以新增 ConfigStore 項目 (亦即介面方法),
現有的介面類別如未定義介面類別,則必須新增
,這樣您才能為該類別新增 ConfigStore 項目。這個區段
我們採用 disableInitBlank
設定項目的範例
已將 healthd
新增至 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 };
- 為以下項目更新
Android.bp
和Android.mk
: ConfigStore 共用資料庫和標頭檔案,以納入新版介面 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
Android.bp
和Android.mk
:hardware/interfaces/configstore/1.0
。 - 產生 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
這個指令會建立兩個檔案:ChargerConfigs.h
和ChargerConfigs.cpp
,當地時間hardware/interfaces/configstore/1.0/default
。 - 開啟
.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
檔案,新增實作檔案 (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
- (選用) 新增資訊清單項目。如果該值區不存在,系統會採用預設值
「預設」ConfigStore 的執行個體名稱。例如,在
device/google/marlin/manifest.xml
:<hal format="hidl"> <name>android.hardware.configstore</name> ... <interface> <name>IChargerConfigs</name> <instance>default</instance> </interface> </hal>
- 視需要加入這項政策規則 (也就是如果用戶端沒有
對
hal_configstore
發出 hwbinder 呼叫的權限)。適用對象 例如在system/sepolicy/private/healthd.te
中:... // other rules binder_call(healthd, hal_configstore)
新增 ConfigStore 項目
如何新增 ConfigStore 項目:
- 開啟 HAL 檔案,然後新增項目所需的介面方法。(
有
.hal
個 ConfigStore 檔案位於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>
- 請在以下位置使用適當的範本函式存取 ConfigStore 項目:
android.hardware.configstore-utils
。例如,在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 服務,以便日後呼叫 例項化範本函式。 - 新增 ConfigStore 和
configstore-utils
的依附元件Android.mk
或Android.bp
中的程式庫例如,在system/core/healthd/Android.mk
:LOCAL_SHARED_LIBRARIES := \ android.hardware.configstore@1.0 \ android.hardware.configstore-utils \ ... (other libraries) \