คุณสามารถเพิ่มรายการ ConfigStore ใหม่ (นั่นคือ วิธีการอินเทอร์เฟซ) สำหรับคลาสอินเทอร์เฟซที่มีอยู่ หากไม่ได้กำหนดคลาสอินเทอร์เฟซ คุณต้องเพิ่มคลาสใหม่ก่อนจึงจะสามารถเพิ่มรายการ ConfigStore สำหรับคลาสนั้นได้ ส่วนนี้ใช้ตัวอย่างของรายการการกำหนดค่า disableInitBlank
สำหรับ healthd
ที่ถูกเพิ่มไปยังคลาสอินเทอร์เฟซ IChargerConfigs
การเพิ่มคลาสอินเทอร์เฟซ
หากไม่มีการกำหนดคลาสอินเทอร์เฟซสำหรับวิธีอินเทอร์เฟซที่คุณต้องการเพิ่ม คุณต้องเพิ่มคลาสอินเทอร์เฟซก่อนจึงจะสามารถเพิ่มรายการ ConfigStore ที่เกี่ยวข้องได้
- สร้างไฟล์อินเตอร์เฟส HAL เวอร์ชัน ConfigStore คือ 1.0 ดังนั้น ให้กำหนดอินเทอร์เฟซ ConfigStore ใน
hardware/interfaces/configstore/1.0
ตัวอย่างเช่น ใน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++ stub สำหรับการนำโค้ดเซิร์ฟเวอร์ไปใช้ ตัวอย่างเช่น:
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
เพื่อเพิ่มไฟล์การใช้งาน (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 หากจำเป็น (นั่นคือ หากไคลเอ็นต์ไม่มีสิทธิ์ในการเรียก hwbinder ไปที่
hal_configstore
) ตัวอย่างเช่น ใน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);
ในตัวอย่างนี้ รายการ ConfigStoredisableInitBlank
จะถูกดึงข้อมูลและจัดเก็บไว้ในตัวแปร (มีประโยชน์เมื่อจำเป็นต้องเข้าถึงตัวแปรหลายครั้ง) ค่าที่ดึงมาจาก 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) \