HAL框架

来源:互联网 发布:直通车助手软件 编辑:程序博客网 时间:2024/06/04 18:08

HAL层结构体

HAL只有三个struct结构:
hw_module_methods_t (1)
hw_module_t (2)
hw_device_t (3)

typedef struct hw_module_methods_t {//硬件模块方法列表的定义,这里只定义了一个open函数      /**并不是真正的操作设备,只是初始化一个hw_device_t结构体,这个结构体中含有设备处理函数,所以    是间接‘打开’设备*/      int (*open)(const struct hw_module_t* module, const char* id,             struct hw_device_t** device);  } hw_module_methods_t; 
typedef struct hw_module_t {      /** tag must be initialized to HARDWARE_MODULE_TAG */      uint32_t tag;      /** major version number for the module */      uint16_t version_major;      /** minor version number of the module */      uint16_t version_minor;      /** Identifier of module */      const char *id;      /** Name of this module */      const char *name;      /** Author/owner/implementor of the module */      const char *author;      /** Modules methods */      //模块方法列表,指向hw_module_methods_t*      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;
typedef struct hw_device_t {      /** tag must be initialized to HARDWARE_DEVICE_TAG */      uint32_t tag;      /** version number for hw_device_t */      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; 

HAL API

HAL框架提供了一个公用函数:

hw_get_module(const char *id,const struct hw_module_t** module);

功能:根据模块id(module_id)去朝找注册在当前系统中与id对应的硬件对象,然后载入(load)其相应的HAL层模块的.so
文件。载入以后从.so文件中查找”HMI”这个符号,如果在.so代码中有定义的函数名或者变量名为HMI,返回其地址(存在第二个参数当中)。

实例

结构体声明:

    struct led_module_t{        struct hw_module_t common;        int status;    };    struct led_device_t{        struct hw_device_t common;        int (*set_on)(struct led_device_t* dev);        int (*set_off)(struct led_device_t* dev);    };

实现:

static int led_device_close(struct hw_device_t *device){    struct led_deivce_t ldev=(struct led_device_t *)device;    if(ldev)        free(ldev);    return 0;}static int led_set_on(struct led_deivce_t *dev){    //调用HAL_Drive    return 0;}static int led_set_off(struct led_deivce_t *dev){    ////调用HAL_Drive    return 0;}static int led_open(const struct hw_module_t* module,const char *name,struct hw_device_t **device){    struct led_deivce_t *dev;    LOGD("led_dev_open");    dev=(struct led_deivce_t*)malloc(sizeof(struct led_deivce_t));    memset(dev,0,sizeof(struct led_deivce_t));    dev->common.tag=HARDWARE_DEVICE_TAG;    dev->common.version=0;    dev->common.module=(struct hw_module_t*)module;    dev->common.close=led_device_close;    dev->set_on=led_set_on;    dev->set_off=led_set_off;    *device=(struct hw_device_t*)dev;    return 0;}
const struct led_module_t HAL_MODULE_INFO_SYM={{HARDWARE_DEVICE_TAG,1,0,LED_HARDWARE_MODULE_ID,"TestLEDSTub","Test Project Team",&my_methods},-1};static struct hw_module_methods_t my_methods={led_open}

客户端调用流程

(1)hw_get_module(const char id,const struct hw_module_t* module) 获取到hw_module_t对象的指针。
(2)hw_module_t中保存了函数指针表,查找到hw_module_methods_t中的led_open函数。
(3)led_open会创建hw_device_t对象,最后调用hw_device_t对象的函数(led_set_on/led_set_off)操作设备。
led_set_on—->HAL_Driver(不开源)—->Linux_Driver(开源)

0 0
原创粉丝点击