เพิ่มคลาสและรายการ ConfigStore

คุณเพิ่มรายการ ConfigStore ใหม่ (ซึ่งก็คือเมธอดอินเทอร์เฟซ) สำหรับคลาสอินเทอร์เฟซที่มีอยู่ได้ หากไม่ได้กำหนดคลาสอินเทอร์เฟซไว้ คุณต้องเพิ่มคลาสใหม่ก่อนจึงจะเพิ่มรายการ ConfigStore สำหรับคลาสนั้นได้ ส่วนนี้ใช้ตัวอย่างของรายการการกำหนดค่า disableInitBlank สำหรับ healthd ที่เพิ่มลงในคลาสอินเทอร์เฟซ IChargerConfigs

เพิ่มคลาสอินเทอร์เฟซ

หากไม่ได้กำหนดคลาสอินเทอร์เฟซไว้สำหรับเมธอดอินเทอร์เฟซที่ต้องการเพิ่ม คุณต้องเพิ่มคลาสอินเทอร์เฟซก่อนจึงจะเพิ่มรายการ ConfigStore ที่เชื่อมโยงได้

  1. สร้างไฟล์อินเทอร์เฟซ 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
    };
    
  2. อัปเดต 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
  3. สร้างสตับ 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
    
    คำสั่งนี้จะสร้างไฟล์ 2 ไฟล์ ได้แก่ ChargerConfigs.h และ ChargerConfigs.cpp ใน hardware/interfaces/configstore/1.0/default
  4. เปิดไฟล์การใช้งาน .h และ .cpp และนำโค้ดที่เกี่ยวข้องกับฟังก์ชัน HIDL_FETCH_name ออก (เช่น HIDL_FETCH_IChargerConfigs) ฟังก์ชันนี้จำเป็นสำหรับโหมดส่งผ่าน HIDL ซึ่ง ConfigStore ไม่ได้ใช้
  5. ลงทะเบียนการใช้งานกับบริการ 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
    }
    
  6. แก้ไขไฟล์ 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
    
  7. (ไม่บังคับ) เพิ่มรายการไฟล์ Manifest หากไม่มี ให้ใช้ชื่ออินสแตนซ์ "เริ่มต้น" ของ ConfigStore ตามค่าเริ่มต้น เช่นใน device/google/marlin/manifest.xml
        <hal format="hidl">
            <name>android.hardware.configstore</name>
            ...
            <interface>
                <name>IChargerConfigs</name>
                <instance>default</instance>
            </interface>
        </hal>
    
  8. เพิ่มกฎ Sepolicy หากจำเป็น (กล่าวคือ หากไคลเอ็นต์ไม่มีสิทธิ์เรียกใช้ Hwbinder ไปยัง hal_configstore) ตัวอย่างเช่นใน system/sepolicy/private/healthd.te
    ... // other rules
    binder_call(healthd, hal_configstore)
    

เพิ่มรายการ ConfigStore ใหม่

วิธีเพิ่มรายการ ConfigStore ใหม่มีดังนี้

  1. เปิดไฟล์ 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);
    };
    
  2. นำเมธอดนี้ไปใช้ในไฟล์การติดตั้งใช้งาน 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

  1. ใส่ไฟล์ส่วนหัวที่จำเป็น เช่นใน system/core/healthd/healthd.cpp
    #include <android/hardware/configstore/1.0/IChargerConfigs.h>
    #include <configstore/Utils.h>
    
  2. เข้าถึงรายการ 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);
    
    ในตัวอย่างนี้ ระบบจะดึงและจัดเก็บรายการ disableInitBlank ใน ConfigStore ไว้ในตัวแปร (มีประโยชน์เมื่อต้องเข้าถึงตัวแปรหลายครั้ง) ค่าที่ดึงจาก ConfigStore จะแคชภายในฟังก์ชันเทมเพลตที่ไม่ระบุตัวตนเพื่อให้ดึงค่าจากค่าที่แคชไว้ได้อย่างรวดเร็วโดยไม่ต้องติดต่อบริการ ConfigStore สำหรับการเรียกฟังก์ชันเทมเพลตที่ยืนยันในภายหลัง
  3. เพิ่มทรัพยากร Dependency ใน 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) \