zoukankan      html  css  js  c++  java
  • Android硬件抽象层(HAL)深入剖析(三)

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

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

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

     1 /**
     2  * Load the file defined by the variant and if successful
     3  * return the dlopen handle and the hmi.
     4  * @return 0 = success, !0 = failure.
     5  */
     6 static int load(const char *id,
     7         const char *path,
     8         const struct hw_module_t **pHmi)
     9 {//传入硬件模块id和库所在路径,获取到硬件模块结构体
    10     int status;
    11     void *handle;
    12     struct hw_module_t *hmi;
    13 
    14     /*
    15      * load the symbols resolving undefined symbols before
    16      * dlopen returns. Since RTLD_GLOBAL is not or'd in with
    17      * RTLD_NOW the external symbols will not be global
    18      */
    19     handle = dlopen(path, RTLD_NOW);//打开共享库
    20     if (handle == NULL) {
    21         char const *err_str = dlerror();
    22         LOGE("load: module=%s
    %s", path, err_str?err_str:"unknown");
    23         status = -EINVAL;
    24         goto done;
    25     }
    26 
    27     /* Get the address of the struct hal_module_info. */
    28     const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
    29     hmi = (struct hw_module_t *)dlsym(handle, sym);//解析共享库
    30     if (hmi == NULL) {
    31         LOGE("load: couldn't find symbol %s", sym);
    32         status = -EINVAL;
    33         goto done;
    34     }
    35 
    36     /* Check that the id matches */
    37     if (strcmp(id, hmi->id) != 0) {//匹配解析出硬件模块的id和传入我们实际想要得到的模块id是否一致
    38         LOGE("load: id=%s != hmi->id=%s", id, hmi->id);
    39         status = -EINVAL;
    40         goto done;
    41     }
    42 
    43     hmi->dso = handle;  //将打开库得到句柄传给硬件模块的dso
    44 
    45     /* success */
    46     status = 0;
    47 
    48     done:
    49     if (status != 0) {
    50         hmi = NULL;
    51         if (handle != NULL) {
    52             dlclose(handle);
    53             handle = NULL;
    54         }
    55     } else {
    56         LOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
    57                 id, path, *pHmi, handle);
    58     }
    59 
    60     *pHmi = hmi;//将得到的module的结果通过第三个参数传给hw_module_t
    61 
    62     return status;
    63 }

    可以看到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就能打开硬件模块对应的设备了,通过结构体中的一些方法就能操作硬件设备了。

  • 相关阅读:
    FFmpeg笔记-基本使用
    记一次下载大文件存在数据异常问题排查
    从零开始编写IntelliJ IDEA插件
    Guava LoadingCache不能缓存null值
    FFmpeg笔记--vcodec和-c:v,-acodec和-c:a的区别?
    说说maven依赖冲突,依赖调解,依赖传递和依赖范围
    记一次NoHttpResponseException问题排查
    JVM源码分析-类加载场景实例分析
    JVM源码分析-JVM源码编译与调试
    理解PHP的变量,值与引用的关系
  • 原文地址:https://www.cnblogs.com/microliang/p/3436147.html
Copyright © 2011-2022 走看看