Thêm các lớp và mục ConfigStore

Bạn có thể thêm các mục ConfigStore mới (nghĩa là các phương thức giao diện) cho lớp giao diện hiện có. Nếu lớp giao diện không đượ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ục cấu hình disableInitBlank dành cho healthd được thêm vào lớp giao diện IChargerConfigs .

Thêm các 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, 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 quan.

  1. Tạo một 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ụ: trong hardware/interfaces/configstore/1.0/IChargerConfigs.hal :
    package android.hardware.configstore@1.0;
    
    interface IChargerConfigs {
        // TO-BE-FILLED-BELOW
    };
    
  2. Cập nhật Android.bpAndroid.mk cho thư viện chia sẻ ConfigStore và các tệp tiêu đề để bao gồm giao diện HAL mới. Ví dụ:
    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
    
    Các lệnh này cập nhật Android.bpAndroid.mk trong hardware/interfaces/configstore/1.0 .
  3. Tạo sơ khai C++ để triển khai mã máy chủ. Ví dụ:
    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
    
    Lệnh này tạo hai tệp, ChargerConfigs.hChargerConfigs.cpp , trong hardware/interfaces/configstore/1.0/default .
  4. Mở file triển khai .h.cpp và xóa mã liên quan đến HIDL_FETCH_ name (ví dụ: HIDL_FETCH_IChargerConfigs ). Chức năng này cần thiết cho chế độ truyền qua HIDL, chế độ này không được ConfigStore sử dụng.
  5. Đăng ký 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
    }
    
  6. Sửa đổi tệp Android.mk để thêm tệp triển khai ( modulename Configs.cpp ) vào LOCAL_SRC_FILES và ánh xạ các cờ xây dựng thành các định nghĩa macro. Ví dụ: trong 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. (Tùy chọn) Thêm mục nhập bảng kê khai. Nếu nó không tồn tại, hãy đặt mặc định thành tên phiên bản "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>
    
  8. Thêm quy tắc sepolicy nếu cần (nghĩa là nếu máy khách không có quyền thực hiện lệnh gọi hwbinder tới hal_configstore ). Ví dụ: trong system/sepolicy/private/healthd.te :
    ... // other rules
    binder_call(healthd, hal_configstore)
    

Thêm các mục ConfigStore mới

Để thêm một mục ConfigStore mới:

  1. Mở tệp HAL và thêm phương thức giao diện cần thiết cho mục. (Các tệp .hal cho ConfigStore nằm trong hardware/interfaces/configstore/1.0 .) Ví dụ: trong hardware/interfaces/configstore/1.0/IChargerConfigs.hal :
    package android.hardware.configstore@1.0;
    
    interface IChargerConfigs {
        ... // Other interfaces
        disableInitBlank() generates(OptionalBool value);
    };
    
  2. Triển khai phương thức trong các tệp triển khai HAL giao diện tương ứng ( .h.cpp ). Đặt các triển khai mặc định trong hardware/interfaces/configstore/1.0/default . Ví dụ: trong hardware/interfaces/configstore/1.0/default/ChargerConfigs.h :
    struct ChargerConfigs : public IChargerConfigs {
        ... // Other interfaces
        Return<void> disableInitBlank(disableInitBlank_cb _hidl_cb) override;
    };
    
    Và trong 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

Để sử dụng mục ConfigStore:

  1. Bao gồm các tập tin tiêu đề cần thiết. Ví dụ: trong system/core/healthd/healthd.cpp :
    #include <android/hardware/configstore/1.0/IChargerConfigs.h>
    #include <configstore/Utils.h>
    
  2. Truy cập mục ConfigStore bằng chức năng mẫu thích hợp trong android.hardware.configstore-utils . Ví dụ: trong 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);
    
    Trong ví dụ này, mục ConfigStore disableInitBlank được truy xuất và lưu trữ vào một biến (hữu ích khi biến đó cần được truy cập 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 khởi tạo để có thể truy xuất nhanh chóng từ giá trị được lưu trong bộ nhớ đệm mà không cần liên hệ với dịch vụ ConfigStore để thực hiện các lệnh gọi sau này đến hàm mẫu được khởi tạo.
  3. Thêm phần phụ thuộc vào ConfigStore và thư viện configstore-utils trong Android.mk hoặc Android.bp . Ví dụ: trong system/core/healthd/Android.mk :
    LOCAL_SHARED_LIBRARIES := \
        android.hardware.configstore@1.0 \
        android.hardware.configstore-utils \
        ... (other libraries) \