硬件适配层通用接口定义在hardware/libhardware/include/hardware/hardware.h。
typedef struct hw_module_t { /** tag must be initialized to HARDWARE_MODULE_TAG */ uint32_t tag; uint16_t module_api_version; #define version_major module_api_version uint16_t hal_api_version; #define version_minor hal_api_version /** Identifier of module */ const char *id; /** Name of this module */ const char *name; /** Author/owner/implementor of the module */ const char *author; /** Modules methods */ struct hw_module_methods_t* methods; /** module's dso */ void* dso; /** padding to 128 bytes, reserved for future use */ uint32_t reserved[32-7]; } hw_module_t; |
模块通用方法open,用来打开一个设备。
typedef struct hw_module_methods_t { /** Open a specific device */ int (*open)(const struct hw_module_t* module, const char* id, struct hw_device_t** device); } hw_module_methods_t; |
设备通用方法close,用来关闭该设备。
typedef struct hw_device_t { /** tag must be initialized to HARDWARE_DEVICE_TAG */ uint32_t tag; uint32_t version; /** reference to the module this device belongs to */ struct hw_module_t* module; /** padding reserved for future use */ uint32_t reserved[12]; /** Close this device */ int (*close)(struct hw_device_t* device); } hw_device_t; |
搜索模块
int hw_get_module(const char *id, const struct hw_module_t **module); int hw_get_module_by_class(const char *class_id, const char *inst, const struct hw_module_t **module); |
int hw_get_module(const char *id, const struct hw_module_t **module) { return hw_get_module_by_class(id, NULL, module); } |
- 搜索目录
- /vendor/lib/hw
- /system/lib/hw
- 搜索文件名称
- <class_id>.<inst>.<subname>.so (没有lib前缀)
- subname
- property_get "ro.hardware.<class_id>.<inst>"
- property_get "ro.hardware"
- property_get "ro.product.board"
- property_get "ro.board.platform"
- property_get "ro.arch"
- "default"
举例:
/vendor/lib/hw/lights.[ro.hardware.lights].so
/system/lib/hw/lights.[ro.hardware.lights].so
/vendor/lib/hw/lights.[ro.hardware].so
/system/lib/hw/lights.[ro.hardware].so
/vendor/lib/hw/lights.[ro.product.board].so
/system/lib/hw/lights.[ro.product.board].so
/vendor/lib/hw/lights.[ro.board.platform].so
/system/lib/hw/lights.[ro.board.platform].so
/vendor/lib/hw/lights.[ro.arch].so
/system/lib/hw/lights.[ro.arch].so
/system/lib/hw/lights.default.so