安卓HAL层 so库文件加载原理
来源:互联网 发布:mac pro apple care 编辑:程序博客网 时间:2024/05/22 06:39
本文分析代码基于安卓6.0
上层app通过jni调用hal层的hw_get_module函数获取硬件模块,这个函数是上层与hal打交道的入口
这里我们就具体来看看hw_get_module的实现
文件路径:
vim hardware/libhardware/hardware.c
打开该文件会发现,这个文件非常小,只有200多行
首先找到hw_get_module()
int hw_get_module(const char *id, const struct hw_module_t **module){ return hw_get_module_by_class(id, NULL, module);}
这里直接调用<pre name="code" class="cpp" style="color: rgb(51, 51, 51); font-size: 13.3333px; line-height: 24px;">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);}
文件的查找,主要集中在如下代码片段:
/* 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; } }这里将variant_keys[i]里的属性获取到,然后与上层传过来的id拼凑加上.so
大致如下:id.prop.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_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;
}
如果查找到相应文件,继续调用:
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)打开so库文件
使用 hmi = (struct hw_module_t *)dlsym(handle, sym)在得到的handle中获取hw_module_t
然后上层app就可以通过hw_module_t 里的函数接口访问驱动,进而访问硬件
- 安卓HAL层 so库文件加载原理
- 编译HAL层生成lib.so文件
- 安卓 hal层 C 文件中加调试堆栈
- 安卓so动态库加载代理实现,可以实现C层的类反射效果
- HAL so库加载机制---之一
- HAL so库加载机制---之二
- 厂商HAL层库文件 xxx.xxx.so添加到源码中编译到系统镜像中
- Android C层如何加载.so库文件
- cocos2dx 安卓加载so文件马上崩溃
- 安卓7.0 加载libsqlite.so文件失败
- 安卓7.0 加载libsqlite.so文件失败
- 北斗gps Android hal层so库代码
- 北斗gps Android hal层so库代码
- 安卓开发.SO文件
- 动态加载so库文件
- 动态加载so库文件
- Android studio中使用sqlcipher安卓7.0中.so文件加载失败问题。
- 4-安卓底层-HAL层-实现安卓控制LED
- Caffe:深度学习中 epoch,[batch size], iterations的区别
- Sublime Text : 创建工程
- maven-pom中配置多个远程仓库-编码gbk的不可映射字符
- idea中maven依赖图
- Java NIO和IO的区别
- 安卓HAL层 so库文件加载原理
- 推荐!手把手教你使用Git
- Servlet的llistener接口详解
- 锐捷面试_guozhijun20160704
- BZOJ4636 蒟蒻的数列
- ubuntu及linuxmint下电子词典首选:GoldenDict
- Struts小总结
- eclipse超级实用快捷键
- 我的实习(第一天)