每一個模塊對應的動態連接庫的名字是遵循HAL的命名規範的,這樣廠商在開發HAL模塊只須要按照這個規範命名就能夠了。
shell
硬件抽象模塊的動態連接庫文件名命名規範定義在:/hardware/libhardware/hardware.c:
數組
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"
};
//後面會用到
static const int HAL_VARIANT_KEYS_COUNT =
(sizeof(variant_keys)/sizeof(variant_keys[0]));
複製代碼
HAL會按照variant_keys[]定義的屬性名稱的順序逐一來讀取屬性值prop,若值存在,則查找對應的<MODULE_ID>.<prop>.so是否存在。 bash
若是沒有讀取到任何屬性值,則使用<MODULE_ID>.default.so 做爲默認的動態連接庫文件名來加載硬件模塊。 在代碼中,獲取上面屬性數組裏面的屬性值的方法是經過property_get(prop_name, prop, NULL)來實現的,經過屬性名稱獲得對應的屬性值。 函數
咱們也能夠經過命令行來查看相應的屬性名稱的屬性值:
ui
adb shell
su
getprop ro.hardware
getprop ro.product.board
getprop ro.board.platform
getprop ro.arch
複製代碼
HAL模塊的存放路徑是有規範的。HAL規定了2個硬件模塊動態共享庫的存放路徑,定義在/hardware/libhardware/hardware.c:
this
/** Base path of the hal modules */
#if defined(__LP64__)
#define HAL_LIBRARY_PATH1 "/system/lib64/hw"
#define HAL_LIBRARY_PATH2 "/vendor/lib64/hw"
#else
#define HAL_LIBRARY_PATH1 "/system/lib/hw"
#define HAL_LIBRARY_PATH2 "/vendor/lib/hw"
#endif
複製代碼
因此,硬件模塊的共享庫必須放在/system/lib/hw 或者 /vendor/lib/hw 這2個路徑下的其中一個。 spa
HAL在加載所需的共享庫的時候,會先檢查HAL_LIBRARY_PATH2路徑下面是否存在目標庫;若是沒有,繼續檢查HAL_LIBRARY_PATH1路徑下面是否存在。具體實如今函數hw_module_exists, /hardware/libhardware/hardware.c,這個後面具體分析。
.net
應用打開HAL庫的入口函數爲hw_get_module,具體實如今文件hardware/libhardware/hardware.c,下面咱們具體來分析。
命令行
int hw_get_module(const char *id, const struct hw_module_t **module)
{
return hw_get_module_by_class(id, NULL, module);
}
複製代碼
hw_get_module實際上調用了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};
//根據id生成module name,這裏inst爲NULL
// 假如id爲gps,因此name爲gps
if (inst)
snprintf(name, PATH_MAX, "%s.%s", class_id, inst);
else
strlcpy(name, class_id, PATH_MAX);
// 獲得屬性名prop_name爲ro.hardware.gps
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; //存在,找到了
}
}
//逐一查詢variant_keys數組定義的屬性名稱對應的屬性值
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名稱爲default的共享庫
/* 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); //執行加載和解析共享庫的工做 } 複製代碼
下面看看hw_module_exists方法
// path:就是上面指定的兩個路徑
// name:其實對應上面提到的MODULE_ID,也就是模塊名稱
// subname: 對應從上面提到的屬性值prop
static int hw_module_exists(char *path, size_t path_len, const char *name,
const char *subname)
{
//檢查/vendor/lib/hw路徑下是否存在目標模塊
// 假如模塊名稱爲gps, 屬性值爲msm8974,因此模塊名爲:/vendor/lib/hw/gps.msm8974.so
snprintf(path, path_len, "%s/%s.%s.so",
HAL_LIBRARY_PATH2, name, subname);
if (access(path, R_OK) == 0)
return 0;
//檢查/system/lib/hw路徑下是否存在目標模塊
//假如模塊名稱爲gps, 屬性值爲msm8974,因此模塊名爲:/system/lib/hw/gps.msm8974.so
snprintf(path, path_len, "%s/%s.%s.so",
HAL_LIBRARY_PATH1, name, subname);
if (access(path, R_OK) == 0)
return 0;
return -ENOENT;
}
複製代碼
找到對應的HAL so庫以後,下面就是下載這個庫,下面看看load(class_id, path, module)方法。
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 */ //使用dlopen打開path定義的目標共享庫,獲得庫文件的句柄handle handle = dlopen(path, RTLD_NOW); if (handle == NULL) { //出錯,經過dlerror獲取錯誤信息 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" //使用dlsym找到符號爲「HMI」的地址,這裏應該是hw_module_t結構體的地址;而且賦給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 */ //檢查模塊id是否匹配 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); } //返回獲得的hw_module_t結構體的指針 *pHmi = hmi; return status; } 複製代碼
參考文獻:
https://blog.csdn.net/yangwen123/article/details/12040909
https://www.jianshu.com/p/0d155f267589