......
}
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
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;
}
CameraServer是通过dlopen的方式打开camera.qcom.so的,进而打开camx。
**3.dlopen / dlsym函数(动态链接库)**
**void \* dlopen( const char \* pathname, int mode);**
**功能 :**打开一个动态链接库
**包含头文件 :** #include <dlfcn.h> 编译时候要加入 -ldl (指定dl库)
**函数描述 :**使用dlopen函数以指定模式(mode)打开指定的动态连接库文件,并返回一个句柄给调
用进程。使用dlclose来卸载打开的库。
**返回值 :** 打开错误返回NULL 成功,返回库引用
**使用 : dlopen**
dlopen()是一个强大的库函数。该函数将打开一个新库,并把它装入内存。该函数主要用来加
载库中的符号,这些符号在编译的时候是不知道的。比如 Apache Web 服务器利用这个函数在运
行过程中加载模块,这为它提供了额外的能力。一个配置文件控制了加载模块的过程。这种机制使
得在系统中添加或者删除一个模块时,都 不需要重新编译了。
可以在自己的程序中使用 dlopen()。dlopen() 在 dlfcn.h 中定义,并在 dl 库中实现。它需要两个参
数:一个文件名和一个标志。文件名可以是我们学习过的库中的 soname。标志指明是否立刻计算
库的依赖性。如果设置为 RTLD\_NOW 的话,则立刻计算;如果设置的是 RTLD\_LAZY,则在需要
的时候才计算。另外,可以指定 RTLD\_GLOBAL,它使得那些在以后才加载的库可以获得其中的
符号。 当库被装入后,可以把 dlopen() 返回的句柄作为给 dlsym() 的第一个参数,以获得符号在
库中的地址。使用这个地址,就可以获得库中特定函数的指针,并且调用装载库中的相应函数。
**void\* dlsym(void\* handle,const char\* symbol)**
该函数handle参数是由dlopen打开动态链接库后返回的指针,symbol就是要求获取的函数的
## 最后
**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**
**深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。**
**因此收集整理了一份《2024年嵌入式&物联网开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
![img](https://img-blog.csdnimg.cn/img_convert/fbb2f9df99a33d4b26c11885780a757b.png)
![img](https://img-blog.csdnimg.cn/img_convert/0d8654735fb9cfdbff44a5f207b0a05c.jpeg)
![img](https://img-blog.csdnimg.cn/img_convert/bb6caba377da7a93774043d347a8af32.png)
![img](https://img-blog.csdnimg.cn/img_convert/66171e7fcc683ab56b6ccea9e0c6af33.png)
![img](https://img-blog.csdnimg.cn/img_convert/a7d04ee9423bb75f44fd1c7842ccd49c.png)
![img](https://img-blog.csdnimg.cn/img_convert/f4393cd7f63d70a6e3c97cef050f8fde.png)
![](https://img-blog.csdnimg.cn/img_convert/f7b0fcadf1fe0170132668a14228f29b.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!**
[**如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618654289)
**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**!!
5538180272)]
[外链图片转存中...(img-PjecMgOU-1715538180272)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!**
[**如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618654289)
**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**!!