Implement the service

To prepare for the HAL implementation, you can generate basic ConfigStore interface code, then modify it to meet your needs.

Generate interface code

To generate boilerplate code for the interface, run hidl-gen. For example, to generate code for surfaceflinger:

hidl-gen -o hardware/interfaces/configstore/1.0/default \
    -Lc++-impl \
    -randroid.hardware:hardware/interfaces \
    -randroid.hidl:system/libhidl/transport \
    android.hardware.config@1.0::ISurfaceFlingerConfigs

Modify Android.mk

Next, modify the Android.mk file to add the implementation file (<modulename>Configs.cpp) to LOCAL_SRC_FILES and to map build flags into macro definitions. For example, you can modify surfaceflinger in hardware/interface/configstore/1.0/default/Android.mk:

LOCAL_SRC_FILES += SurfaceFlingerConfigs.cpp
ifneq ($(NUM_FRAMEBUFFER_SURFACE_BUFFERS),)
    LOCAL_CFLAGS += -DNUM_FRAMEBUFFER_SURFACE_BUFFERS=$(NUM_FRAMEBUFFER_SURFACE_BUFFERS)
endif

ifeq ($(TARGET_RUNNING_WITHOUT_SYNC_FRAMEWORK),true)
    LOCAL_CFLAGS += -DRUNNING_WITHOUT_SYNC_FRAMEWORK
endif

If Android.mk includes several ifeq-endif blocks, consider moving your code into a new file (that is, surfaceflinger.mk) then include that file from Android.mk.

Implement functions

To fill the functions to implement the HAL, call back the _hidl_cb function with different values (conditioned on build flags). For example, you can fill the functions for surfaceflinger in hardware/interfaces/configstore/1.0/default/SurfaceFlingerConfigs.cpp:

Return<void> SurfaceFlingerConfigs::numFramebufferSurfaceBuffers(
        numFramebufferSurfaceBuffers_cb _hidl_cb) {
    #if NUM_FRAMEBUFFER_SURFACE_BUFFERS 2
    _hidl_cb(NumBuffers.TWO);
    #else if NUM_FRAMEBUFFER_SURFACE_BUFFERS 3
    _hidl_cb(NumBuffers.THREE);
    #else
    _hidl_cb(NumBuffers.USE_DEFAULT);
    #endif
}

Return<void> SurfaceFlingerConfigs::runWithoutSyncFramework(
        runWithoutSyncFramework_cb _hidl_cb) {
    #ifdef RUNNING_WITHOUT_SYNC_FRAMEWORK
    _hidl_cb({true /* specified */, true /* value */});
    #else
    // when macro not defined, we can give any value to the second argument.
    // It will simply be ignored in the framework side.
    _hidl_cb({false /* specified */, false /* value */});
    #endif
}

Ensure that the implementation doesn't contain a function named HIDL_FETCH_interface-name (for example, HIDL_FETCH_ISurfaceFlingerConfigs). This function is needed for HIDL passthrough mode, which is unused (and prohibited) by configstore. ConfigStore must always run in binderized mode.

Register as a service

Finally, register all interface implementations to the configstore service. For example, you can register surfaceflinger implementations in hardware/interfaces/configstore/1.0/default/service.cpp:

configureRpcThreadpool(maxThreads, true);
sp<ISurfaceFlingerConfigs> surfaceFlingerConfigs = new SurfaceFlingerConfigs;
status_t status = surfaceFlingerConfigs->registerAsService();

sp<IBluetoothConfigs> bluetoothConfigs = new BluetoothConfigs;
status = bluetoothConfigs->registerAsService();

// register more interfaces here
joinRpcThreadpool();

Ensure early access

To ensure that a framework module can get early access the HAL service, the config HAL service should start as early as possible, just after hwservicemanager is ready. As the config HAL service doesn't read external files, it's expected to be ready quickly after it's launched.