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.
- Utwórz plik interfejsu HAL. Wersja ConfigStore to 1.0, dlatego zdefiniuj interfejsy ConfigStore w
hardware/interfaces/configstore/1.0
. Na przykład whardware/interfaces/configstore/1.0/IChargerConfigs.hal
:package android.hardware.configstore@1.0; interface IChargerConfigs { // TO-BE-FILLED-BELOW };
- Zaktualizuj pliki
Android.bp
iAndroid.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
Android.bp
iAndroid.mk
whardware/interfaces/configstore/1.0
. - 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 plikiChargerConfigs.h
iChargerConfigs.cpp
whardware/interfaces/configstore/1.0/default
. - 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. - 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 }
- Zmodyfikuj plik
Android.mk
, dodając plik implementacji (modulenameConfigs.cpp
) do zbioru danychLOCAL_SRC_FILES
i mapując flagi kompilacji na definicje makr. Na przykład whardware/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
- (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>
- 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 wsystem/sepolicy/private/healthd.te
:... // other rules binder_call(healthd, hal_configstore)
Dodaj nowe elementy ConfigStore
Aby dodać nowy element ConfigStore:
- Otwórz plik HAL i dodaj wymaganą metodę interfejsu dla elementu. (Pliki
.hal
dla ConfigStore znajdują się whardware/interfaces/configstore/1.0
). Na przykład whardware/interfaces/configstore/1.0/IChargerConfigs.hal
:package android.hardware.configstore@1.0; interface IChargerConfigs { ... // Other interfaces disableInitBlank() generates(OptionalBool value); };
- Zaimplementuj tę metodę w odpowiednich plikach implementacji HAL interfejsu (
.h
i.cpp
). Umieść implementacje domyślne w plikuhardware/interfaces/configstore/1.0/default
. Na przykład w aplikacjihardware/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:
- 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>
- Uzyskaj dostęp do elementu ConfigStore za pomocą odpowiedniej funkcji szablonu w
android.hardware.configstore-utils
. Na przykład w zasadziesystem/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 ConfigStoredisableInitBlank
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. - Dodaj zależność od ConfigStore i biblioteki
configstore-utils
w taguAndroid.mk
lubAndroid.bp
. Na przykład wsystem/core/healthd/Android.mk
:LOCAL_SHARED_LIBRARIES := \ android.hardware.configstore@1.0 \ android.hardware.configstore-utils \ ... (other libraries) \