Dodawanie klas i elementów ConfigStore

Możesz dodać nowe elementy ConfigStore (metody interfejsu) dla istniejącej klasy interfejsu. Jeśli klasa interfejsu nie jest zdefiniowana, musisz dodać nową klasę, zanim będzie można dodać do niej element ConfigStore. W tej sekcji używamy przykładowego elementu konfiguracji disableInitBlank dla healthd dodawanego do klasy interfejsu IChargerConfigs.

Dodaj klasy interfejsu

Jeśli dla metody interfejsu, którą chcesz dodać, nie ma zdefiniowanej klasy interfejsu, musisz dodać klasę interfejsu, zanim dodasz powiązane elementy ConfigStore.

  1. Utwórz plik interfejsu HAL. Wersja ConfigStore to 1.0, dlatego 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 współdzielonej biblioteki i plików nagłówka 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ą Android.bp i Android.mk w hardware/interfaces/configstore/1.0.
  3. Wygeneruj atrapę 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 2 pliki ChargerConfigs.h i ChargerConfigs.cpp w hardware/interfaces/configstore/1.0/default.
  4. Otwórz pliki implementacji .h i .cpp i usuń kod związany z funkcją HIDL_FETCH_name (np. HIDL_FETCH_IChargerConfigs). Ta funkcja jest potrzebna w trybie przekazywania HIDL, który nie jest używany w ConfigStore.
  5. Zarejestruj implementację 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, dodając plik implementacji (modulenameConfigs.cpp) do zbioru danych LOCAL_SRC_FILES i mapując 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 (tzn. jeśli klient nie ma uprawnień do wykonywania wywołań hwbinder dla interfejsu hal_configstore). Na przykład w system/sepolicy/private/healthd.te:
    ... // other rules
    binder_call(healthd, hal_configstore)
    

Dodaj nowe elementy 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 tę metodę w odpowiednich plikach implementacji HAL interfejsu (.h i .cpp). Umieść implementacje domyślne w pliku hardware/interfaces/configstore/1.0/default. Na przykład w aplikacji hardware/interfaces/configstore/1.0/default/ChargerConfigs.h:
    struct ChargerConfigs : public IChargerConfigs {
        ... // Other interfaces
        Return<void> disableInitBlank(disableInitBlank_cb _hidl_cb) override;
    };
    
    a 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();
    }
    

Używanie elementów ConfigStore

Aby użyć elementu ConfigStore:

  1. Dołącz wymagane pliki nagłówka. 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 za pomocą odpowiedniej funkcji szablonu w android.hardware.configstore-utils. Na przykład w zasadzie 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 ConfigStore disableInitBlank jest pobierany i zapisywany w zmiennej (przydatne, gdy trzeba wielokrotnie uzyskać do niej dostęp). Wartość pobierana z ConfigStore jest przechowywana w pamięci podręcznej w funkcji bezpośredniego szablonu, dzięki czemu można ją szybko pobrać z wartości zapisanej w pamięci podręcznej bez kontaktowania się z usługą ConfigStore w celu późniejszego wywoływania funkcji instancjonowanego szablonu.
  3. Dodaj zależność od ConfigStore i biblioteki configstore-utils w tagu 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) \