硬件抽象层模块的加载

在HAL层,由函数hw_get_module负责HAL层模块的加载

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]));//variant_keys数组的大小
//id为输入参数,表示要加载的硬件抽象层模块ID
//module为输出参数,如果加载成功,则其指向一个自定义的HAL模块结构体
//函数的返回值为一个整数,0表示加载成功,小于0表示加载失败
//主要通过另外一个函数hw_get_module_by_class来实现
int hw_get_module(const char *id, const struct hw_module_t **module)
{
    return hw_get_module_by_class(id, NULL, module);
}
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)
        //sprintf函数功能

最多从源串中拷贝size1个字符到目标串中,然后再在后面加一个0。所以如果目标串的大小为size的话,将不会溢出。

函数返回值:

若成功则返回欲写入的字符串长度,若出错则返回负值。

        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);

int property_get(const char *key, char *value, const char *default_value);

int property_set(const char *key, const char *value);

    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); } /**  * 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) {     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; } /*  * 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 (access(path, R_OK) == 0)         return 0;     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; }


猜你喜欢

转载自blog.csdn.net/qq_28449863/article/details/80756728
今日推荐