硬件访问服务AIDL HAL

JNI 向上提供本地函数, 向下加载HAL文件并调用HAL的函数
HAL 负责访问驱动程序执行硬件操作

在Android 5 中 HAL module 框架主要分为如下三个结构体

hardware/libhardware/include/hardware/hardware.h 文件中定义

struct hw_module_t
struct hw_module_methods_t
struct hw_device_t
/**
 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
 * and the fields of this data structure must begin with hw_module_t
 * followed by module specific information.
* 大概的意思是
硬件层必须导出 HAL_MODULE_INFO_SYM 且 一个成员变量必须是 hw_module_t
*/ typedef struct hw_module_t { /** tag must be initialized to HARDWARE_MODULE_TAG */ uint32_t 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; /** 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; #ifdef __LP64__ uint64_t reserved[32-7]; #else /** padding to 128 bytes, reserved for future use */ uint32_t reserved[32-7]; #endif } hw_module_t;

说明:1.硬件抽象层必须自定义一个硬件模块结构体 且第一个成员变量必须是 hw_module_t

   2.必须存在一个导出符号HAL_MODULE_INFO_SYM

        3.tag成员值必须设置为HARDWARE_MODULE_TAG

          4.methods定义了硬件抽象层模块的操作方法列表  类型是 hw_module_methods_t

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;

说明:module  硬件所在模块

           id设备Id

           device 打开的设备

/**
 * Every device data structure must begin with hw_device_t
 * followed by module specific public methods and attributes.
 */
typedef struct hw_device_t {
    /** tag must be initialized to HARDWARE_DEVICE_TAG */
    uint32_t 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 */
#ifdef __LP64__
    uint64_t reserved[12];
#else
    uint32_t reserved[12];
#endif

    /** Close this device */
    int (*close)(struct hw_device_t* device);

} hw_device_t;

说明:1.每一个硬件设备必须自定义一个设备结构体 且第一个成员变量必须是hw_device_t

           2.结构体hw_device_t 成员tag必须设置值为HARDWARE_DEVICE_TAG

           3.成员close 是一个函数指针 用来关闭一个硬件设备

调用梗概:

  1. 模块名==>文件名 

    hw_get_module_by_class("led", NULL)
    name = "led"
    property_get xxx是某个属性
    hw_module_exists 判断是否存在led.xxx.so

  

  2. 加载
    dlopen(filename)
    dlsym("HMI") 从SO文件中获得名为HMI的hw_module_t结构体
    strcmp(id, hmi->id) 判断名字是否一致(hmi->id, "led")

hw_module_exists(char *path, size_t path_len, const char *name,const char *subname)

  它用来判断"name"."subname".so文件是否存在
  查找的目录:
    a. HAL_LIBRARY_PATH 环境变量
    b. /vendor/lib/hw
    c. /system/lib/hw

  

JNI 怎么使用HAL

  a. hw_get_module 获得一个hw_module_t结构体

  b. 调用 module->methods->open(module, device_name, &device)
    获得一个hw_device_t结构体
    并且把hw_device_t结构体转换为设备自定义的结构体

struct led_device_t{
    struct hw_device_t common;
    int (*led_open)(struct led_device_t* dev);
    int (*led_ctrl)(struct led_device_t * dev,int which,int status);
};

HAL 怎么编写

  a. 实现一个名为HMI的hw_module_t结构体
  b. 实现一个open函数, 它会根据name返回一个设备自定义的结构体
    这个设备自定义的结构体的第1个成员是 hw_device_t结构体
    还可以定义设备相关的成员

dlopen()

原文地址:https://www.cnblogs.com/qingducx/p/5189184.html