/***每一個硬件都通過hw_module_t來描述,我們稱之為一個硬件對象。你可以去"繼承"這個hw_module_t*然后擴展自己的屬性,硬件對象必須定義為一個固定的名字HMI,即:Hardware Module Information的簡寫*每個硬件對象里都封裝了一個函數指針open用于打開硬件,我們理解為硬件對象的open方法,open調用后*返回這個硬件對應的操作接口集合*/
typedef struct hw_module_t {/** tag must be initialized to HARDWARE_MODULE_TAG */uint32_t tag; //該值必須聲明為HARDWARE_MODULE_TAG/*** The API version of the implemented module. The module owner is* responsible for updating the version when a module interface has* changed.** The derived modules such as gralloc and audio own and manage this field.* The module user must interpret the version field to decide whether or* not to inter-operate with the supplied module implementation.* For example, SurfaceFlinger is responsible for making sure that* it knows how to manage different versions of the gralloc-module API,* and AudioFlinger must know how to do the same for audio-module API.** The module API version should include a major and a minor component.* For example, version 1.0 could be represented as 0x0100. This format* implies that versions 0x0100-0x01ff are all API-compatible.** In the future, libhardware will expose a hw_get_module_version()* (or equivalent) function that will take minimum/maximum supported* versions as arguments and would be able to reject modules with* versions outside of the supplied range.*/uint16_t module_api_version;
#define version_major module_api_version/*** version_major/version_minor defines are supplied here for temporary* source code compatibility. They will be removed in the next version.* ALL clients must convert to the new version format.*//*** The API version of the HAL module interface. This is meant to* version the hw_module_t, hw_module_methods_t, and hw_device_t* structures and definitions.** The HAL interface owns this field. Module users/implementations* must NOT rely on this value for version information.** Presently, 0 is the only valid value.*/uint16_t hal_api_version;
#define version_minor hal_api_version/** Identifier of module */const char *id; //硬件id名,唯一標識module/** Name of this module */const char *name; //硬件module的名字/** Author/owner/implementor of the module */const char *author; //作者/** Modules methods *///指向封裝有open函數指針的結構體struct hw_module_methods_t* methods; /** module's dso */void* dso;/** padding to 128 bytes, reserved for future use *///128字節補齊uint32_t reserved[32-7];} hw_module_t;//硬件對象的open方法描述結構體,它里面只有一個元素: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;/*** Every device data structure must begin with hw_device_t* followed by module specific public methods and attributes.*///硬件對象hw_module_t的open方法返回該硬件的Operation interface,它由hw_device_t結構體來描述//我們稱之為該硬件的操作接口
typedef struct hw_device_t {/** tag must be initialized to HARDWARE_DEVICE_TAG */uint32_t tag; //必須賦值為HARDWARE_DEVICE_TAG/*** Version of the module-specific device API. This value is used by* the derived-module user to manage different device implementations.** The module user is responsible for checking the module_api_version* and device version fields to ensure that the user is capable of* communicating with the specific module implementation.** One module can support multiple devices with different versions. This* can be useful when a device interface changes in an incompatible way* but it is still necessary to support older implementations at the same* time. One such example is the Camera 2.0 API.** This field is interpreted by the module user and is ignored by the* HAL interface itself.*/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;??? 上述三個結構體之間關系緊密,每個硬件對象都由hw_module_t來描述,只要我們拿到了這個硬件對象,就可以調用它的open方法,返回這個硬件對象的硬件操作接口,然后就可以通過這些硬件操作接口來間接操作硬件。只不過,open方法被hw_module_methods_t結構封裝了一次,硬件操作接口被hw_device_t封裝了一次而已。下面這張圖可以反映出它們三者的關系:
//HAL Stub對象固定的名字
#define HAL_MODULE_INFO_SYM HMI/*** Name of the hal_module_info as a string*///字符串形式的名字
#define HAL_MODULE_INFO_SYM_AS_STR "HMI"/*** Get the module info associated with a module by id.** @return: 0 == success, <0 == error and *module == NULL*///通過硬件名來獲得硬件HAL Stub對象
int hw_get_module(const char *id, const struct hw_module_t **module); 用戶程序通過硬件的id名來拿到硬件,下面我們以android平臺下驅動的開發及測試框架概述(二)一文的例子來分析:
????? 在聲明結構體freg_module_t時,其名字統一定義為了HMI,而這么做的目的就是為了通過dlsym來查找Freg HAL Stub源碼生成的so庫里的”HMI”符號。現在很明顯了,我們寫的HAL Stub代碼最終要編譯so庫文件,并且庫文件名為:freg.default.so(當然可以設置四個系統屬性之一來指定名字為:freg.屬性值.so),并且庫的所在目錄為:/system/lib/hw/。