You can add new ConfigStore items (that is, interface methods) for an
existing interface class. If the interface class isn't defined, you must add a
new class before you can add a ConfigStore item for that class. This section
uses the example of a disableInitBlank
configuration item for
healthd
being added to the IChargerConfigs
interface
class.
Add interface classes
If no interface class is defined for the interface method that you want to add, you must add the interface class before you can add the associated ConfigStore items.
- Create a HAL interface file. The ConfigStore version is 1.0, so define
ConfigStore interfaces in
hardware/interfaces/configstore/1.0
. For example, inhardware/interfaces/configstore/1.0/IChargerConfigs.hal
:package android.hardware.configstore@1.0; interface IChargerConfigs { // TO-BE-FILLED-BELOW };
- Update
Android.bp
andAndroid.mk
for the ConfigStore shared library and header files to include the new interface HAL. For example: These commands updatehidl-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
andAndroid.mk
inhardware/interfaces/configstore/1.0
. - Generate the C++ stub for implementing the server code. For example:
This command creates two files,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
ChargerConfigs.h
andChargerConfigs.cpp
, inhardware/interfaces/configstore/1.0/default
. - Open the
.h
and.cpp
implementation files and remove code related to the functionHIDL_FETCH_name
(for example,HIDL_FETCH_IChargerConfigs
). This function is needed for HIDL passthrough mode, which isn't used by ConfigStore. - Register the implementation to the ConfigStore service. For example, in
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 }
- Modify the
Android.mk
file to add the implementation file (modulenameConfigs.cpp
) toLOCAL_SRC_FILES
and to map build flags into macro definitions. For example, inhardware/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
- (Optional) Add a manifest entry. If it doesn't exist, default to the
"default" instance name of ConfigStore. For example, in
device/google/marlin/manifest.xml
:<hal format="hidl"> <name>android.hardware.configstore</name> ... <interface> <name>IChargerConfigs</name> <instance>default</instance> </interface> </hal>
- Add the sepolicy rule if needed (that is, if the client doesn't have
permissions for making hwbinder calls to
hal_configstore
). For example, insystem/sepolicy/private/healthd.te
:... // other rules binder_call(healthd, hal_configstore)
Add new ConfigStore items
To add a new ConfigStore item:
- Open the HAL file and add the required interface method for the item. (The
.hal
files for ConfigStore reside inhardware/interfaces/configstore/1.0
.) For example, inhardware/interfaces/configstore/1.0/IChargerConfigs.hal
:package android.hardware.configstore@1.0; interface IChargerConfigs { ... // Other interfaces disableInitBlank() generates(OptionalBool value); };
- Implement the method in the corresponding interface HAL implementation files
(
.h
and.cpp
). Place the default implementations inhardware/interfaces/configstore/1.0/default
. For example, inhardware/interfaces/configstore/1.0/default/ChargerConfigs.h
: And instruct 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(); }
Use ConfigStore items
To use a ConfigStore item:
- Include the required header files. For example, in
system/core/healthd/healthd.cpp
:#include <android/hardware/configstore/1.0/IChargerConfigs.h> #include <configstore/Utils.h>
- Access the ConfigStore item using the appropriate template function in
android.hardware.configstore-utils
. For example, insystem/core/healthd/healthd.cpp
: In this example, the ConfigStore itemusing namespace android::hardware::configstore; using namespace android::hardware::configstore::V1_0; static int64_t disableInitBlank = getBool< IChargerConfigs, &IChargerConfigs::disableInitBlank>(false);
disableInitBlank
is retrieved and stored to a variable (useful when the variable needs to be accessed multiple times). The value retrieved from the ConfigStore is cached inside the instantiated template function so that it can be retrieved quickly from the cached value without contacting the ConfigStore service for later calls to the instantiated template function. - Add the dependency on ConfigStore and the
configstore-utils
library inAndroid.mk
orAndroid.bp
. For example, insystem/core/healthd/Android.mk
:LOCAL_SHARED_LIBRARIES := \ android.hardware.configstore@1.0 \ android.hardware.configstore-utils \ ... (other libraries) \