【HAL】hw_get_module分析:加载HAL层库,获取camx模块接口

博客介绍了Android Camera HAL模块的实现与调用。实现路径为android\\vendor\\qcom\\proprietary\\camx\\src\\core\\hal\\camxhal3entry.cpp,模块编译为camera.qcom.so。调用时通过hw_get_module接口获取入口结构体,还介绍了hw_get_module_by_class、hw_module_exists、load等步骤及相关操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.HAL module implementation

路径:android\vendor\qcom\proprietary\camx\src\core\hal\camxhal3entry.cpp

// Name of the hal_module_info
#define HAL_MODULE_INFO_SYM         HMI

// Name of the hal_module_info as a string
#define HAL_MODULE_INFO_SYM_AS_STR  "HMI"
CAMX_VISIBILITY_PUBLIC camera_module_t HAL_MODULE_INFO_SYM =
{
    .common =
    {
        .tag                = HARDWARE_MODULE_TAG,
        .module_api_version = CAMERA_MODULE_API_VERSION_CURRENT,
        .hal_api_version    = HARDWARE_HAL_API_VERSION,
        .id                 = CAMERA_HARDWARE_MODULE_ID,
        .name               = "QTI Camera HAL",
        .author             = "Qualcomm Technologies, Inc.",
        .methods            = &CamX::g_hwModuleMethods
    },
    .get_number_of_cameras  = CamX::get_number_of_cameras,
    .get_camera_info        = CamX::get_camera_info,
    .set_callbacks          = CamX::set_callbacks,
    .get_vendor_tag_ops     = CamX::get_vendor_tag_ops,
    .open_legacy            = CamX::open_legacy,
    .set_torch_mode         = CamX::set_torch_mode,
    .init                   = CamX::init
};

入口结构体名称为HAL_MODULE_INFO_SYM,根据其宏定义即HMI,camx模块被编译为camera.qcom.so,存放于设备路径:/vendor/lib64/hw/camera.qcom.so

2.调用

路径:android\hardware\interfaces\camera\provider\2.4\default\LegacyCameraProviderImpl_2_4.cpp

通过hw_get_module接口获取camera模块入口结构体。

#define CAMERA_HARDWARE_MODULE_ID "camera"
step1

注意:rawModule类型为camera_module_t,被强制转换为hw_module_t类型去获取结构体。原因是camera_module_t的第一个成员变量即为hw_module_t类型,所以地址相同,获取到该成员变量地址即获取整个结构体地址。

bool LegacyCameraProviderImpl_2_4::initialize() {
    camera_module_t *rawModule;
    int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
            (const hw_module_t **)&rawModule);
    if (err < 0) {
        ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err));
        return true;
    }
    ......
}
step2
int hw_get_module(const char *id, const struct hw_module_t **module)
{
    return hw_get_module_by_class(id, NULL, module);		// id为"camera"
}
step3:hw_get_module_by_class
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"
};

1、获取ro.hardware.camera属性,实测为空
2、遍历variant_keys中字符串的属性
在这里插入图片描述
获取ro.hardware属性的值为qcom,再调用hw_module_exists判断对应名称(camera.qcom)的so文件是否存在。
3、 Nothing found, try the default

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);		// name为"camera"

    /*
     * 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);	// 获取ro.hardware.camera属性,实测为空
    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);
}
step4:hw_module_exists

遍历各个路径下是否存在camera.qcom.so文件。一般在/vendor/lib64/hw下。

#define HAL_LIBRARY_PATH1 "/system/lib64/hw"
#define HAL_LIBRARY_PATH2 "/vendor/lib64/hw"
#define HAL_LIBRARY_PATH3 "/odm/lib64/hw"
/*
 * Check if a HAL with given name and subname exists, if so return 0, otherwise
 * otherwise return negative.  On success path will contain the path to the HAL.
 */
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 (path_in_path(path, HAL_LIBRARY_PATH3) && access(path, R_OK) == 0)
        return 0;

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

#ifndef __ANDROID_VNDK__
    snprintf(path, path_len, "%s/%s.%s.so",
             HAL_LIBRARY_PATH1, name, subname);
    if (path_in_path(path, HAL_LIBRARY_PATH1) && access(path, R_OK) == 0)
        return 0;
#endif

    return -ENOENT;
}
step5:load

dlopen camera.qcom.so,dlsym获取HAL_MODULE_INFO_SYM符号的地址,即camx模块的入口结构体地址。

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;
#ifdef __ANDROID_VNDK__
    const bool try_system = false;
#else
    const bool try_system = true;
#endif

    /*
     * 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
     */
    if (try_system &&
        strncmp(path, HAL_LIBRARY_PATH1, strlen(HAL_LIBRARY_PATH1)) == 0) {
        /* If the library is in system partition, no need to check
         * sphal namespace. Open it with dlopen.
         */
        handle = dlopen(path, RTLD_NOW);
    } else {
#if defined(__ANDROID_RECOVERY__)
        handle = dlopen(path, RTLD_NOW);
#else
        handle = android_load_sphal_library(path, RTLD_NOW);
#endif
    }
    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"
    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) {	// 检查id是否相同即“camera”
        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, hmi, handle);
    }

    *pHmi = hmi;

    return status;
}
<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` 的实际值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值