หากต้องการกรอกฟังก์ชันเพื่อใช้ HAL ให้เรียกใช้ฟังก์ชัน _hidl_cb อีกครั้งพร้อมค่าอื่น (ขึ้นอยู่กับ Flag การสร้าง) ตัวอย่างเช่น คุณสามารถกรอกฟังก์ชันสําหรับ 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
}
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();
ตรวจสอบสิทธิ์ทดลองใช้ก่อนเปิดตัว
เพื่อให้มั่นใจว่าโมดูลเฟรมเวิร์กจะเข้าถึงบริการ HAL ได้ก่อนเปิดตัว คุณควรเริ่มบริการ HAL ที่กําหนดค่าไว้ให้เร็วที่สุดเท่าที่จะทำได้หลังจากที่ hwservicemanager พร้อมใช้งาน เนื่องจากบริการ HAL สำหรับการกําหนดค่าไม่ได้อ่านไฟล์ภายนอก จึงคาดว่าจะพร้อมใช้งานอย่างรวดเร็วหลังจากเปิดตัว
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-26 UTC"],[],[],null,["# Implement the service\n\n| **Warning:** Android 10 deprecates the ConfigStore HAL and replaces the HAL with system properties. For details, refer to [Configuring](/docs/core/architecture/configuration).\n\nTo prepare for the HAL implementation, you can generate basic ConfigStore\ninterface code, then modify it to meet your needs.\n\nGenerate interface code\n-----------------------\n\nTo generate boilerplate code for the interface, run `hidl-gen`.\nFor example, to generate code for `surfaceflinger`: \n\n```\nhidl-gen -o hardware/interfaces/configstore/1.0/default \\\n -Lc++-impl \\\n -randroid.hardware:hardware/interfaces \\\n -randroid.hidl:system/libhidl/transport \\\n android.hardware.config@1.0::ISurfaceFlingerConfigs\n```\n| **Note:** Don't run `hidl-gen` with `-Landroidbp-impl` as this generates `Android.bp`. The module must be built with `Android.mk` to access build flags.\n\nModify Android.mk\n-----------------\n\nNext, modify the `Android.mk` file to add the implementation file\n(`\u003cmodulename\u003eConfigs.cpp`) to `LOCAL_SRC_FILES` and\nto map build flags into macro definitions. For example, you can modify\n`surfaceflinger` in\n`hardware/interface/configstore/1.0/default/Android.mk`: \n\n```\nLOCAL_SRC_FILES += SurfaceFlingerConfigs.cpp\nifneq ($(NUM_FRAMEBUFFER_SURFACE_BUFFERS),)\n LOCAL_CFLAGS += -DNUM_FRAMEBUFFER_SURFACE_BUFFERS=$(NUM_FRAMEBUFFER_SURFACE_BUFFERS)\nendif\n\nifeq ($(TARGET_RUNNING_WITHOUT_SYNC_FRAMEWORK),true)\n LOCAL_CFLAGS += -DRUNNING_WITHOUT_SYNC_FRAMEWORK\nendif\n```\n\nIf `Android.mk` includes several `ifeq-endif` blocks,\nconsider moving your code into a new file (that is,\n`surfaceflinger.mk`) then include that file from\n`Android.mk`.\n\nImplement functions\n-------------------\n\nTo fill the functions to implement the HAL, call back the\n`_hidl_cb` function with different values (conditioned on build\nflags). For example, you can fill the functions for `surfaceflinger`\nin\n`hardware/interfaces/configstore/1.0/default/SurfaceFlingerConfigs.cpp`: \n\n```\nReturn\u003cvoid\u003e SurfaceFlingerConfigs::numFramebufferSurfaceBuffers(\n numFramebufferSurfaceBuffers_cb _hidl_cb) {\n #if NUM_FRAMEBUFFER_SURFACE_BUFFERS 2\n _hidl_cb(NumBuffers.TWO);\n #else if NUM_FRAMEBUFFER_SURFACE_BUFFERS 3\n _hidl_cb(NumBuffers.THREE);\n #else\n _hidl_cb(NumBuffers.USE_DEFAULT);\n #endif\n}\n\nReturn\u003cvoid\u003e SurfaceFlingerConfigs::runWithoutSyncFramework(\n runWithoutSyncFramework_cb _hidl_cb) {\n #ifdef RUNNING_WITHOUT_SYNC_FRAMEWORK\n _hidl_cb({true /* specified */, true /* value */});\n #else\n // when macro not defined, we can give any value to the second argument.\n // It will simply be ignored in the framework side.\n _hidl_cb({false /* specified */, false /* value */});\n #endif\n}\n```\n\nEnsure that the implementation doesn't contain a function named\n`HIDL_FETCH_`\u003cvar translate=\"no\"\u003einterface-name\u003c/var\u003e (for example,\n`HIDL_FETCH_ISurfaceFlingerConfigs`). This function is needed for\nHIDL passthrough mode, which is unused (and prohibited) by\n`configstore`. ConfigStore must always run in binderized mode.\n\nRegister as a service\n---------------------\n\nFinally, register all interface implementations to the\n`configstore` service. For example, you can register\n`surfaceflinger` implementations in\n`hardware/interfaces/configstore/1.0/default/service.cpp`: \n\n```\nconfigureRpcThreadpool(maxThreads, true);\nsp\u003cISurfaceFlingerConfigs\u003e surfaceFlingerConfigs = new SurfaceFlingerConfigs;\nstatus_t status = surfaceFlingerConfigs-\u003eregisterAsService();\n\nsp\u003cIBluetoothConfigs\u003e bluetoothConfigs = new BluetoothConfigs;\nstatus = bluetoothConfigs-\u003eregisterAsService();\n\n// register more interfaces here\njoinRpcThreadpool();\n```\n\nEnsure early access\n-------------------\n\nTo ensure that a framework module can get early access the HAL service, the\nconfig HAL service should start as early as possible, just after\n`hwservicemanager` is ready. As the config HAL service doesn't read\nexternal files, it's expected to be ready quickly after it's launched."]]