Bạn có thể thêm các mục ConfigStore mới (tức là các phương thức giao diện) cho một lớp giao diện hiện có. Nếu lớp giao diện chưa được xác định, bạn phải thêm một lớp mới trước khi có thể thêm mục ConfigStore cho lớp đó. Phần này sử dụng ví dụ về một mục cấu hình disableInitBlank
cho healthd
được thêm vào lớp giao diện IChargerConfigs
.
Thêm lớp giao diện
Nếu không có lớp giao diện nào được xác định cho phương thức giao diện mà bạn muốn thêm, thì bạn phải thêm lớp giao diện trước khi có thể thêm các mục ConfigStore liên kết.
- Tạo tệp giao diện HAL. Phiên bản ConfigStore là 1.0, vì vậy, hãy xác định giao diện ConfigStore trong
hardware/interfaces/configstore/1.0
. Ví dụ: tronghardware/interfaces/configstore/1.0/IChargerConfigs.hal
:package android.hardware.configstore@1.0; interface IChargerConfigs { // TO-BE-FILLED-BELOW };
- Cập nhật
Android.bp
vàAndroid.mk
cho thư viện dùng chung ConfigStore và các tệp tiêu đề để bao gồm giao diện HAL mới. Ví dụ: Các lệnh này cập nhậthidl-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
vàAndroid.mk
tronghardware/interfaces/configstore/1.0
. - Tạo mã giả lập C++ để triển khai mã máy chủ. Ví dụ:
Lệnh này tạo hai tệp là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
vàChargerConfigs.cpp
tronghardware/interfaces/configstore/1.0/default
. - Mở các tệp triển khai
.h
và.cpp
rồi xoá mã liên quan đến hàmHIDL_FETCH_name
(ví dụ:HIDL_FETCH_IChargerConfigs
). Hàm này cần thiết cho chế độ truyền qua HIDL mà ConfigStore không sử dụng. - Đăng ký quá trình triển khai với dịch vụ ConfigStore. Ví dụ: trong
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 }
- Sửa đổi tệp
Android.mk
để thêm tệp triển khai (modulenameConfigs.cpp
) vàoLOCAL_SRC_FILES
và liên kết cờ bản dựng vào các định nghĩa macro. Ví dụ: tronghardware/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
- (Không bắt buộc) Thêm mục nhập tệp kê khai. Nếu không tồn tại, giá trị mặc định sẽ là tên thực thể "mặc định" của ConfigStore. Ví dụ: trong
device/google/marlin/manifest.xml
:<hal format="hidl"> <name>android.hardware.configstore</name> ... <interface> <name>IChargerConfigs</name> <instance>default</instance> </interface> </hal>
- Thêm quy tắc sepolicy nếu cần (tức là nếu ứng dụng không có quyền thực hiện lệnh gọi hwbinder đến
hal_configstore
). Ví dụ: trongsystem/sepolicy/private/healthd.te
:... // other rules binder_call(healthd, hal_configstore)
Thêm các mục ConfigStore mới
Cách thêm một mục ConfigStore mới:
- Mở tệp HAL và thêm phương thức giao diện bắt buộc cho mục đó. (Các tệp
.hal
cho ConfigStore nằm tronghardware/interfaces/configstore/1.0
.) Ví dụ: tronghardware/interfaces/configstore/1.0/IChargerConfigs.hal
:package android.hardware.configstore@1.0; interface IChargerConfigs { ... // Other interfaces disableInitBlank() generates(OptionalBool value); };
- Triển khai phương thức trong các tệp triển khai HAL giao diện tương ứng (
.h
và.cpp
). Đặt các phương thức triển khai mặc định tronghardware/interfaces/configstore/1.0/default
. Ví dụ: tronghardware/interfaces/configstore/1.0/default/ChargerConfigs.h
: Và trongstruct 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(); }
Sử dụng các mục ConfigStore
Cách sử dụng mục ConfigStore:
- Thêm các tệp tiêu đề bắt buộc. Ví dụ: trong
system/core/healthd/healthd.cpp
:#include <android/hardware/configstore/1.0/IChargerConfigs.h> #include <configstore/Utils.h>
- Truy cập vào mục ConfigStore bằng hàm mẫu thích hợp trong
android.hardware.configstore-utils
. Ví dụ: trongsystem/core/healthd/healthd.cpp
: Trong ví dụ này, mục ConfigStoreusing namespace android::hardware::configstore; using namespace android::hardware::configstore::V1_0; static int64_t disableInitBlank = getBool< IChargerConfigs, &IChargerConfigs::disableInitBlank>(false);
disableInitBlank
được truy xuất và lưu trữ vào một biến (rất hữu ích khi cần truy cập vào biến nhiều lần). Giá trị được truy xuất từ ConfigStore được lưu vào bộ nhớ đệm bên trong hàm mẫu được tạo bản sao để có thể truy xuất nhanh từ giá trị được lưu vào bộ nhớ đệm mà không cần liên hệ với dịch vụ ConfigStore cho các lệnh gọi sau đến hàm mẫu được tạo bản sao. - Thêm phần phụ thuộc trên ConfigStore và thư viện
configstore-utils
trongAndroid.mk
hoặcAndroid.bp
. Ví dụ: trongsystem/core/healthd/Android.mk
:LOCAL_SHARED_LIBRARIES := \ android.hardware.configstore@1.0 \ android.hardware.configstore-utils \ ... (other libraries) \