Android中hw_get_module函数分析

本文解析了Android系统中HAL层模块的加载过程,包括hw_get_module()及hw_get_module_by_class()函数的工作原理,介绍了如何通过ID和实例名称定位并加载正确的HAL库。

该函数定义在hardware/libhardware/hardware.c文件中,定义如下:

int hw_get_module(const char *id, const struct hw_module_t **module)
{
    return hw_get_module_by_class(id, NULL, module);
}

hw_get_module()函数利用HAL层注册信息id,获取相应的模块。

hw_get_module_by_class()函数利用HAL层注册信息id和name,获取相应的模块,主要用于id相同、name不同,即获取相同功能但厂家不同的硬件库。

模块注册信息如下:

这个id是hal层注册时加入的,例如sensor的hal的定义
struct sensors_module_t HAL_MODULE_INFO_SYM = {
    .common = {
        .tag = HARDWARE_MODULE_TAG,
        .version_major = 1,
        .version_minor = 0,
        .id = SENSORS_HARDWARE_MODULE_ID,
        .name = "Sunwave",
        .author = "The Android Open Source Project",
        .methods = &sensors_module_methods,
    },
    .get_sensors_list = sensors__get_sensors_list,
};
#define SENSORS_HARDWARE_MODULE_ID "fingerprint"
//hw_module_t是硬件模块结构,是HAL层的灵魂

-------------------------------------------------------------------------------------------------

下面分析一下,hw_get_module_by_class()函数。

int hw_get_module_by_class(const char *class_id, const char *inst,
                           const struct hw_module_t **module)
{
    int i = 0;
    char prop[PATH_MAX] = {0};
    char path[PATH_MAX] = {0};
    char name[PATH_MAX] = {0};
    char prop_name[PATH_MAX] = {0};


    if (inst)
        snprintf(name, PATH_MAX, "%s.%s", class_id, inst);
    else
        strlcpy(name, class_id, 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.
     */

    /* First try a property specific to the class and possibly instance */
    snprintf(prop_name, sizeof(prop_name), "ro.hardware.%s", name);
    if (property_get(prop_name, prop, NULL) > 0) {
        if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
            goto found;
        }
    }

    /* Loop through the configuration variants looking for a module */
    for (i=0 ; i<HAL_VARIANT_KEYS_COUNT; i++) {
        if (property_get(variant_keys[i], prop, NULL) == 0) {
            continue;
        }
        if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
            goto found;
        }
    }

    /* Nothing found, try the default */
    if (hw_module_exists(path, sizeof(path), name, "default") == 0) {
        goto found;
    }

    return -ENOENT;

found:
    /* load the module, if this fails, we're doomed, and we should not try
     * to load a different variant. */
    return load(class_id, path, module);
}

1.

    if (inst)
        snprintf(name, PATH_MAX, "%s.%s", class_id, inst);
    else
        strlcpy(name, class_id, PATH_MAX);

判断inst是否存在,如果存在,将name 赋值为class_id.inst。主要在多个型号时使用。

例如,class_id =fingerprint,inst = sunwave,则name = fingerprint.sunwave

如果不存在,name=class_id,即fingerprint。

2.

    snprintf(prop_name, sizeof(prop_name), "ro.hardware.%s", name);
    if (property_get(prop_name, prop, NULL) > 0) {
        if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
            goto found;
        }
    }

将prop_name赋值为ro.hardware.%name。或者为ro.hardware.fingerprint,或者为ro.hardware.fingerprint.sunwave。

if (property_get(prop_name, prop, NULL) > 0) //查看是否有该属性

如果有,查看该so库是否存在。如果存在则加载。

property_get()函数,如果获取到值,返回获取到值prop的大小。

3.

    /* Loop through the configuration variants looking for a module */
    for (i=0 ; i<HAL_VARIANT_KEYS_COUNT; i++) {
        if (property_get(variant_keys[i], prop, NULL) == 0) {
            continue;
        }
        if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
            goto found;
        }
    }

然后在配置变量中查找,属性值是否存在,配置的变量如下:

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"
};

如果查找到,则查看name.prop.so库是否存在。例如,fingerprint.sunwave.so。

4.

    /* Nothing found, try the default */
    if (hw_module_exists(path, sizeof(path), name, "default") == 0) {
        goto found;
    }

如果都不存在,则将prop=default。查看name.defaule.so库是否存在。例如:fingerprint.default.so库。

----------------------------------------------------------------------------------------------------------

然后分析,hw_module_exists()函数。

static int hw_module_exists(char *path, size_t path_len, const char *name,
                            const char *subname)
{
    snprintf(path, path_len, "%s/%s.%s.so",
             HAL_LIBRARY_PATH3, name, subname);
    if (access(path, R_OK) == 0)
        return 0;

    snprintf(path, path_len, "%s/%s.%s.so",
             HAL_LIBRARY_PATH2, name, subname);
    if (access(path, R_OK) == 0)
        return 0;

    snprintf(path, path_len, "%s/%s.%s.so",
             HAL_LIBRARY_PATH1, name, subname);
    if (access(path, R_OK) == 0)
        return 0;

    return -ENOENT;
}
/** Base path of the hal modules */
#if defined(__LP64__)
#define HAL_LIBRARY_PATH1 "/system/lib64/hw"
#define HAL_LIBRARY_PATH2 "/vendor/lib64/hw"
#define HAL_LIBRARY_PATH3 "/odm/lib64/hw"
#else
#define HAL_LIBRARY_PATH1 "/system/lib/hw"
#define HAL_LIBRARY_PATH2 "/vendor/lib/hw"
#define HAL_LIBRARY_PATH3 "/odm/lib/hw"
#endif

该函数会在三个路径判断库是否存在。获取path参数,供load函数使用。

-------------------------------------------------------------------------------------------------------------

最后,分析 load(class_id, path, module);函数

static int load(const char *id,
        const char *path,
        const struct hw_module_t **pHmi)
{
    int status = -EINVAL;
    void *handle = NULL;
    struct hw_module_t *hmi = NULL;

    /*
     * 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();
        ALOGE("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) {
        ALOGE("load: couldn't find symbol %s", sym);
        status = -EINVAL;
        goto done;
    }

    /* Check that the id matches */
    if (strcmp(id, hmi->id) != 0) {
        ALOGE("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 {
        ALOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
                id, path, *pHmi, handle);
    }

    *pHmi = hmi;

    return status;
}
handle = dlopen(path, RTLD_NOW);
 hmi = (struct hw_module_t *)dlsym(handle, sym);

主要有两个函数,

dlopen()函数根据path以指定模式打开指定的动态链接库文件,并返回一个句柄给dlsym()的调用进程。

dlsym()根据动态链接库操作句柄与符号,返回符号对应的地址,不但可以获取函数地址,也可以获取变量地址。

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

校核id是否正确。

*pHmi = hmi;

赋值hw_module_t结构体指针,即hw_get_module()函数的指针参数。

<think>好的,我现在需要解决的问题是用户询问的两个`hw_get_module`函数调用的区别。这两个调用分别是: 1. `hw_get_module("chao_hal", (hw_module_t const **)&module);` 2. `hw_get_module(HARDWARE_MODULE_ID, &hw_module);` 首先,我需要回忆一下Android HAL(硬件抽象层)的相关知识。`hw_get_module`是Android系统中用于加载硬件模块的函数,属于HAL层的一部分。它的作用是根据给定的模块ID查找并加载对应的共享库,然后返回对应的模块结构体。 接下来,我需要分析这两个调用的不同之处。首先看参数部分。第一个参数是模块ID,第二个参数是用于接收模块指针的指针。 第一个调用中的模块ID是字符串"chao_hal",而第二个调用使用的是宏定义`HARDWARE_MODULE_ID`。通常,`HARDWARE_MODULE_ID`是一个预定义的宏,比如在某个硬件模块的头文件中定义的,比如`#define HARDWARE_MODULE_ID "audio"`或者其他具体的模块名称。所以这里的不同可能在于模块ID的不同,一个是特定的"chao_hal",另一个是标准模块ID。 然后是第二个参数的类型转换问题。在第一个调用中,用户显式地将`module`的地址转换为`hw_module_t const **`类型,而第二个调用直接传递了`&hw_module`,这里假设`hw_module`的类型是`hw_module_t*`,所以`&hw_module`的类型应该是`hw_module_t**`,而函数期望的是`const hw_module_t**`类型。这时候可能需要考虑类型转换是否必要,或者是否有潜在的类型不匹配问题。 接下来,我需要考虑这两个调用在功能上的区别。使用不同的模块ID会导致加载不同的硬件模块。例如,如果"chao_hal"是一个自定义或第三方开发的HAL模块,而`HARDWARE_MODULE_ID`是系统标准定义的模块ID(如gralloc、audio等),那么这两个调用分别加载了不同的模块。 另外,类型转换的问题可能需要进一步分析。在C语言中,如果`module`变量的类型是`hw_module_t*`,那么`&module`的类型就是`hw_module_t**`。而函数`hw_get_module`的第二个参数类型应该是`const hw_module_t**`,所以在第一个调用中需要进行强制类型转换,将`hw_module_t**`转换为`const hw_module_t**`。而第二个调用中,如果`hw_module`的类型是`hw_module_t*`,那么`&hw_module`的类型是`hw_module_t**`,同样需要转换为`const hw_module_t**`。但用户提供的第二个调用没有显式转换,可能是因为在上下文中`hw_module`已经声明为`const hw_module_t*`类型,或者编译器能够隐式转换,或者可能存在某种警告或错误。 总结起来,这两个调用的主要区别在于加载的模块ID不同,以及可能的类型转换处理。模块ID的不同导致加载不同的硬件模块,而类型转换可能涉及代码规范或编译器严格性的问题。 可能还需要查阅Android的官方文档或源码来确认`hw_get_module`的具体参数类型和用法。例如,在Android源码中,`hw_get_module`的原型通常是: ```c int hw_get_module(const char *id, const struct hw_module_t **module); ``` 因此,第二个参数需要是指向`const hw_module_t*`的指针。如果调用者的变量不是const的,就需要进行强制类型转换。例如,第一个调用中的`&module`如果原本是`hw_module_t**`类型,就需要转换为`const hw_module_t**`,而第二个调用中的`&hw_module`如果其类型是`const hw_module_t**`则不需要转换,否则可能需要转换。 因此,用户的两个调用可能在模块ID的选择和const正确性处理上存在差异,导致不同的加载结果和潜在的编译器警告或错误。</think>这两个调用在功能及参数处理上有以下区别: --- ### **1. 模块ID差异** - **`"chao_hal"`** 明确指定加载一个名为 `"chao_hal"` 的自定义或第三方硬件模块。这通常是开发者自行实现的 HAL(硬件抽象层)模块,用于特定硬件或功能的扩展。 - **`HARDWARE_MODULE_ID`** 使用预定义的宏,代表系统标准模块ID(如 `"gralloc"`、`"audio"` 等)。该宏在对应模块的头文件中定义,指向 Android 官方支持的硬件模块。 --- ### **2. 参数类型差异** - **第一个调用** `(hw_module_t const **)&module` 包含显式的类型转换,目的是将 `module` 的指针强制转换为 `const hw_module_t**` 类型。 **原因**:`hw_get_module` 的第二个参数类型是 `const hw_module_t**`,若调用者定义的 `module` 是非 `const` 的(如 `hw_module_t* module;`),则需要强制转换以避免编译器警告。 - **第二个调用** `&hw_module` 未显式转换,可能因为 `hw_module` 已定义为 `const hw_module_t*` 类型,或编译器隐式处理了类型兼容性。若未正确定义,可能引发编译警告。 --- ### **3. 用途差异** - **`hw_get_module("chao_hal", ...)`** 用于加载自定义模块,常见于需要扩展非标准硬件功能(如厂商定制传感器、外设驱动等)。 - **`hw_get_module(HARDWARE_MODULE_ID, ...)`** 用于加载 Android 标准硬件模块,例如音频、显示等官方支持的硬件接口。 --- ### **示例代码对比** ```c // 自定义模块加载(需类型转换) hw_module_t *module; // 非 const 类型 hw_get_module("chao_hal", (const hw_module_t**)&module); // 标准模块加载(假设 HARDWARE_MODULE_ID 已定义) const hw_module_t *hw_module; // 正确类型,无需转换 hw_get_module(HARDWARE_MODULE_ID, &hw_module); ``` --- ### **关键总结** | 调用方式 | 模块类型 | 参数处理 | 典型场景 | |-------------------------------------|----------------|------------------------|------------------------| | `hw_get_module("chao_hal", ...)` | 自定义模块 | 需显式类型转换 | 厂商/第三方扩展硬件 | | `hw_get_module(HARDWARE_MODULE_ID, ...)` | 标准模块 | 类型可能隐式兼容 | Android 官方硬件支持 | --- **建议**: 1. 优先使用 `const hw_module_t*` 类型变量,避免强制转换。 2. 自定义模块需确保实现符合 HAL 接口规范(如 `struct hw_module_methods_t`)。 3. 标准模块应查阅对应头文件(如 `hardware/audio.h`)以确认 `HARDWARE_MODULE_ID` 的实际值。
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值