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