Android中HAL结构分析

一.文件位置

/hardware/libhardware/include/hardware/hardware.h

/hardware/libhardwarehardware.c

二.核心结构

struct hw_module_t;   

struct hw_module_methods_t;

struct hw_device_t;

三.核心结构分析

  1.struct hw_device_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;

其中tag 在设备注册的时候必须初始化,module主要用于加载的时候判断属于哪个模块。

2.struct hw_module_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.
 */
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;//模块的标示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;

硬件的模块主要是对linux设备文件的封装,包含设备号,设备方法,并为模块提供标示id。

3.struct hw_module_methods_t;

    对operation的封装,其函数的参数为,模块,模块id,设备。下面只提供了打开方法。

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;

四.函数分析

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

int hw_get_module(const char *id, const struct hw_module_t **module) 
{
    int status;
    int i;
    const struct hw_module_t *hmi = NULL;
    char prop[PATH_MAX];
    char path[PATH_MAX];

    /*
     * Here we rely on the fact that calling dlopen multiple times on
     * the same .so will simply increment a refcount (and not load
     * a new copy of the library).
     * We also assume that dlopen() is thread-safe.
     */

    /* Loop through the configuration variants looking for a module */
    for (i=0 ; i<HAL_VARIANT_KEYS_COUNT+1 ; i++) {//通过设备配置属性查找模块
        if (i < HAL_VARIANT_KEYS_COUNT) {
            if (property_get(variant_keys[i], prop, NULL) == 0) {
                continue;
            }
            snprintf(path, sizeof(path), "%s/%s.%s.so",
                    HAL_LIBRARY_PATH1, id, prop);
            if (access(path, R_OK) == 0) break;

            snprintf(path, sizeof(path), "%s/%s.%s.so",
                     HAL_LIBRARY_PATH2, id, prop);
            if (access(path, R_OK) == 0) break;
        } else {
            snprintf(path, sizeof(path), "%s/%s.default.so",
                     HAL_LIBRARY_PATH1, id);
            if (access(path, R_OK) == 0) break;
        }
    }

    status = -ENOENT;
    if (i < HAL_VARIANT_KEYS_COUNT+1) {
        /* load the module, if this fails, we're doomed, and we should not try
         * to load a different variant. */
        status = load(id, path, module);//加载
    }

    return status;
}
static const int HAL_VARIANT_KEYS_COUNT =
    (sizeof(variant_keys)/sizeof(variant_keys[0]));

/**
 * Load the file defined by the variant and if successful
 * return the dlopen handle and the hmi.
 * @return 0 = success, !0 = failure.
 */
static int load(const char *id,
        const char *path,
        const struct hw_module_t **pHmi)
{
    int status;
    void *handle;
    struct hw_module_t *hmi;

    /*
     * load the symbols resolving undefined symbols before
     * dlopen returns. Since RTLD_GLOBAL is not or'd in with
     * RTLD_NOW the external symbols will not be global
     */
    handle = dlopen(path, RTLD_NOW);
    if (handle == NULL) {
        char const *err_str = dlerror();
        LOGE("load: module=%s\n%s", path, err_str?err_str:"unknown");
        status = -EINVAL;
        goto done;
    }

    /* Get the address of the struct hal_module_info. */
    const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
    hmi = (struct hw_module_t *)dlsym(handle, sym);
    if (hmi == NULL) {
        LOGE("load: couldn't find symbol %s", sym);
        status = -EINVAL;
        goto done;
    }

    /* Check that the id matches */
    if (strcmp(id, hmi->id) != 0) {
        LOGE("load: id=%s != hmi->id=%s", id, hmi->id);
        status = -EINVAL;
        goto done;
    }

    hmi->dso = handle;

    /* success */
    status = 0;

    done:
    if (status != 0) {
        hmi = NULL;
        if (handle != NULL) {
            dlclose(handle);
            handle = NULL;
        }
    } else {
        LOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
                id, path, *pHmi, handle);
    }

    *pHmi = hmi;

    return status;
}
/**
 * There are a set of variant filename for modules. The form of the filename
 * is "<MODULE_ID>.variant.so" so for the led module the Dream variants 
 * of base "ro.product.board", "ro.board.platform" and "ro.arch" would be:
 *
 * led.trout.so
 * led.msm7k.so
 * led.ARMV6.so
 * led.default.so
 */

static const char *variant_keys[] = {
    "ro.hardware",  /* This goes first so that it can pick up a different
                       file on the emulator. */
    "ro.product.board",
    "ro.board.platform",
    "ro.arch"
};

static const int HAL_VARIANT_KEYS_COUNT =
    (sizeof(variant_keys)/sizeof(variant_keys[0]));

 hw_get_module的主要逻辑流程

1.在系统属性中查找硬件的定义

2.通过模块id查找模块路径

3.通过模块路径来加载模块so库

4.如果在系统属性中没有硬件的定义,则使用默认的so库




Android TIF (TV Input Framework) HAL层是Android系统专门用于电视输入设备的HAL层,它提供了与电视输入设备交互的接口。本文将从以下几个方面对Android TIF HAL层进行分析: 1. TIF HAL层的结构 TIF HAL层的结构主要包括以下几个部分: - TIF HAL层接口:包含了TIF HAL层与上层应用交互的接口,包括初始化、搜索电视节目、设置电视节目等接口。 - TIF HAL层实现:包含了TIF HAL层的具体实现,与具体的电视输入设备相关。 - TIF HAL层框架:包含了TIF HAL层的框架代码,用于管理TIF HAL层的实现。 2. TIF HAL层的初始化 TIF HAL层的初始化主要包括以下几个步骤: - 加载TIF HAL层库:系统在启动时会自动加载TIF HAL层库。 - 查找TIF HAL层接口:系统通过dlsym函数查找TIF HAL层接口。 - 初始化TIF HAL层实现:系统调用TIF HAL层接口的初始化函数初始化TIF HAL层实现。 3. TIF HAL层与电视输入设备的交互 TIF HAL层与电视输入设备的交互主要包括以下几个步骤: - 搜索电视节目:应用调用TIF HAL层接口的搜索电视节目函数,TIF HAL层实现会向电视输入设备发送搜索电视节目的指令,并接收电视输入设备返回的电视节目信息。 - 设置电视节目:应用调用TIF HAL层接口的设置电视节目函数,TIF HAL层实现会向电视输入设备发送设置电视节目的指令,并等待电视输入设备返回设置结果。 4. TIF HAL层的实现 TIF HAL层的具体实现与电视输入设备相关,不同的电视输入设备需要实现不同的TIF HAL层。TIF HAL层的实现需要遵循Android HAL层的规范,包括实现HAL层接口、定义HAL结构体等。 总的来说,Android TIF HAL层是一个用于电视输入设备的HAL层,它提供了与电视输入设备交互的接口,其具体实现与电视输入设备相关。在使用Android TIF HAL层时,需要遵循Android HAL层的规范,并根据实际的电视输入设备进行相应的实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值