• Android硬件抽象层(HAL)深入剖析(三)【转】


    前面分析了android HAL层是如何搜索硬件模块的动态共享库的,其实就是在"system/lib/hw/"或者"/vendor/lib/hw/"这两个路径下找到共享库modueid.variant.so后,通过调用load函数加载库。

    下面我们进入load函数,看看具体是如何实现加载共享库的。

    以下为load函数定义,同样在/hardware/libhardware/hardware.c中实现的。

    /**
     * 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)
    {//传入硬件模块id和库所在路径,获取到硬件模块结构体
        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
    %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) {//匹配解析出硬件模块的id和传入我们实际想要得到的模块id是否一致
            LOGE("load: id=%s != hmi->id=%s", id, hmi->id);
            status = -EINVAL;
            goto done;
        }
    
        hmi->dso = handle;  //将打开库得到句柄传给硬件模块的dso
    
        /* 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;//将得到的module的结果通过第三个参数传给hw_module_t
    
        return status;
    }

    可以看到load函数传入的几个参数,第一个参数就是需要加载的硬件模块对应动态库的硬件模块的id,

    第二个参数就是动态库存放的路径,就是在hw_get_module函数前部分搜索库得到的path,

    第三个参数就是我们需要得到的硬件模块结构体,通过它传给hw_get_module,hw_get_module函数在通过参数传给jni。

    第19行,首先调用dlopen打开共享库,该函数通过传入的库的路径找到库,并且打开它,传回一个操作句柄handle,然后再调用dlsym函数解析这个打开的库,下面第29行,得到库中包含的硬件模块结构体,并将它返回回来。所以硬件厂商或者硬件移植者都必须根据hal的这个架构去实现填充这个和自己硬件相关的硬件模块结构体hw_module_t,供使用。

    通过dlsym解析之后就得到了hw_module_t,随后第37行,将从库中解析得到的结构体中的id和传入的id做比较,看是否一致。

    如果一致则证明就是得到正确的硬件模块了。

    最后第60行,将hw_module_t结构体指针传给第三个参数,传给hw_get_module函数。

    到此,hw_get_module函数就得到了硬件模块结构体hw_module_t.

    有了hw_module_t,那么通过其内部的method open就能打开硬件模块对应的设备了,通过结构体中的一些方法就能操作硬件设备了。

  • 相关阅读:
    day14
    day13
    装饰器小题
    day12
    tes..
    1380 没有上司的舞会
    算法模板——KMP字符串匹配
    算法模板——Tarjan强连通分量
    3211: 花神游历各国
    1131: [POI2008]Sta
  • 原文地址:https://www.cnblogs.com/vijozsoft/p/10606838.html
Copyright © 2020-2023  润新知