การเพิ่มคลาสและรายการ 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++ 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
  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 เพื่อเพิ่มไฟล์การใช้งาน ( 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
    
  7. (ไม่บังคับ) เพิ่มรายการรายการ หากไม่มีอยู่ ให้ใช้ชื่ออินสแตนซ์ "ดีฟอลต์" ของ 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);
    
    ในตัวอย่างนี้ รายการ ConfigStore disableInitBlank จะถูกดึงข้อมูลและจัดเก็บไว้ในตัวแปร (มีประโยชน์เมื่อจำเป็นต้องเข้าถึงตัวแปรหลายครั้ง) ค่าที่ดึงมาจาก ConfigStore จะถูกแคชไว้ภายในฟังก์ชันเทมเพลตที่สร้างอินสแตนซ์ เพื่อให้สามารถเรียกค้นได้อย่างรวดเร็วจากค่าที่แคชไว้ โดยไม่ต้องติดต่อกับบริการ ConfigStore เพื่อการเรียกฟังก์ชันเทมเพลตที่สร้างอินสแตนซ์ในภายหลัง
  3. เพิ่มการพึ่งพา 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) \