Dodawanie klas i elementów ConfigStore

Możesz dodać nowe elementy ConfigStore (czyli metody interfejsu) dla istniejącej klasy interfejsu. Jeśli klasa interfejsu nie jest zdefiniowana, przed dodaniem elementu ConfigStore dla tej klasy należy dodać nową klasę. W tej sekcji wykorzystano przykład elementu konfiguracji disableInitBlank dla healthd dodanego do klasy interfejsu IChargerConfigs .

Dodawanie klas interfejsu

Jeśli dla metody interfejsu, którą chcesz dodać, nie zdefiniowano żadnej klasy interfejsu, musisz dodać klasę interfejsu, zanim będzie można dodać powiązane elementy ConfigStore.

  1. Utwórz plik interfejsu HAL. Wersja ConfigStore to 1.0, więc zdefiniuj interfejsy ConfigStore w hardware/interfaces/configstore/1.0 . Na przykład w hardware/interfaces/configstore/1.0/IChargerConfigs.hal :
    package android.hardware.configstore@1.0;
    
    interface IChargerConfigs {
        // TO-BE-FILLED-BELOW
    };
    
  2. Zaktualizuj pliki Android.bp i Android.mk dla bibliotek współdzielonych i plików nagłówkowych ConfigStore, aby uwzględnić nowy interfejs HAL. Na przykład:
    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
    
    Te polecenia aktualizują pliki Android.bp i Android.mk w hardware/interfaces/configstore/1.0 .
  3. Wygeneruj kod pośredniczący C++ do implementacji kodu serwera. Na przykład:
    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
    
    To polecenie tworzy dwa pliki, ChargerConfigs.h i ChargerConfigs.cpp , w hardware/interfaces/configstore/1.0/default .
  4. Otwórz pliki implementacyjne .h i .cpp i usuń kod związany z HIDL_FETCH_ name (na przykład HIDL_FETCH_IChargerConfigs ). Ta funkcja jest potrzebna w trybie przekazywania HIDL, który nie jest używany przez ConfigStore.
  5. Zarejestruj wdrożenie w usłudze ConfigStore. Na przykład w 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. Zmodyfikuj plik Android.mk , aby dodać plik implementacyjny ( modulename Configs.cpp ) do LOCAL_SRC_FILES i zamapować flagi kompilacji na definicje makr. Na przykład w 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. (Opcjonalnie) Dodaj wpis manifestu. Jeśli nie istnieje, użyj domyślnej nazwy instancji ConfigStore. Na przykład w device/google/marlin/manifest.xml :
        <hal format="hidl">
            <name>android.hardware.configstore</name>
            ...
            <interface>
                <name>IChargerConfigs</name>
                <instance>default</instance>
            </interface>
        </hal>
    
  8. W razie potrzeby dodaj regułę sepolicy (to znaczy, jeśli klient nie ma uprawnień do wykonywania wywołań hwbinder do hal_configstore ). Na przykład w system/sepolicy/private/healthd.te :
    ... // other rules
    binder_call(healthd, hal_configstore)
    

Dodawanie nowych elementów ConfigStore

Aby dodać nowy element ConfigStore:

  1. Otwórz plik HAL i dodaj wymaganą metodę interfejsu dla elementu. (Pliki .hal dla ConfigStore znajdują się w hardware/interfaces/configstore/1.0 .) Na przykład w hardware/interfaces/configstore/1.0/IChargerConfigs.hal :
    package android.hardware.configstore@1.0;
    
    interface IChargerConfigs {
        ... // Other interfaces
        disableInitBlank() generates(OptionalBool value);
    };
    
  2. Zaimplementuj metodę w odpowiednich plikach implementacji interfejsu HAL ( .h i .cpp ). Umieść domyślne implementacje w hardware/interfaces/configstore/1.0/default . Na przykład w hardware/interfaces/configstore/1.0/default/ChargerConfigs.h :
    struct ChargerConfigs : public IChargerConfigs {
        ... // Other interfaces
        Return<void> disableInitBlank(disableInitBlank_cb _hidl_cb) override;
    };
    
    Oraz w 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();
    }
    

Korzystanie z elementów ConfigStore

Aby użyć elementu ConfigStore:

  1. Dołącz wymagane pliki nagłówkowe. Na przykład w system/core/healthd/healthd.cpp :
    #include <android/hardware/configstore/1.0/IChargerConfigs.h>
    #include <configstore/Utils.h>
    
  2. Uzyskaj dostęp do elementu ConfigStore przy użyciu odpowiedniej funkcji szablonu w android.hardware.configstore-utils . Na przykład w 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);
    
    W tym przykładzie element disableInitBlank magazynu ConfigStore jest pobierany i zapisywany w zmiennej (przydatne, gdy trzeba uzyskać wielokrotny dostęp do zmiennej). Wartość pobrana z magazynu ConfigStore jest buforowana w utworzonej funkcji szablonu, dzięki czemu można ją szybko pobrać z wartości w pamięci podręcznej bez konieczności kontaktowania się z usługą ConfigStore w celu późniejszych wywołań funkcji szablonu utworzonej instancji.
  3. Dodaj zależność od ConfigStore i biblioteki configstore-utils w Android.mk lub Android.bp . Na przykład w system/core/healthd/Android.mk :
    LOCAL_SHARED_LIBRARIES := \
        android.hardware.configstore@1.0 \
        android.hardware.configstore-utils \
        ... (other libraries) \