OSDN Git Service

[2472946] Sensor.TYPE_ORIENTATION returns inverted roll values
[android-x86/hardware-libhardware.git] / hardware.c
index ffc9a25..3a23b1f 100644 (file)
@@ -41,7 +41,6 @@
  * led.default.so
  */
 
-#define HAL_DEFAULT_VARIANT     "default"
 static const char *variant_keys[] = {
     "ro.hardware",  /* This goes first so that it can pick up a different
                        file on the emulator. */
@@ -49,26 +48,22 @@ static const char *variant_keys[] = {
     "ro.board.platform",
     "ro.arch"
 };
-#define HAL_VARIANT_KEYS_COUNT  (sizeof(variant_keys)/sizeof(variant_keys[0]))
+
+static const int HAL_VARIANT_KEYS_COUNT =
+    (sizeof(variant_keys)/sizeof(variant_keys[0]));
 
 /**
- * Load the file defined by the variant and if succesfull
+ * 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 *variant,
-                const struct hw_module_t **pHmi)
+        const char *path,
+        const struct hw_module_t **pHmi)
 {
     int status;
     void *handle;
     struct hw_module_t *hmi;
-    char path[PATH_MAX];
-
-    /* Construct the path. */
-    snprintf(path, sizeof(path), "%s/%s.%s.so", HAL_LIBRARY_PATH, id, variant);
-
-    LOGV("load: E id=%s path=%s", id, path);
 
     /*
      * load the symbols resolving undefined symbols before
@@ -78,7 +73,7 @@ static int load(const char *id,
     handle = dlopen(path, RTLD_NOW);
     if (handle == NULL) {
         char const *err_str = dlerror();
-        //LOGW("load: module=%s error=%s", path, err_str);
+        LOGE("load: module=%s\n%s", path, err_str?err_str:"unknown");
         status = -EINVAL;
         goto done;
     }
@@ -87,7 +82,6 @@ static int load(const char *id,
     const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
     hmi = (struct hw_module_t *)dlsym(handle, sym);
     if (hmi == NULL) {
-        char const *err_str = dlerror();
         LOGE("load: couldn't find symbol %s", sym);
         status = -EINVAL;
         goto done;
@@ -99,25 +93,26 @@ static int load(const char *id,
         status = -EINVAL;
         goto done;
     }
-    
+
     hmi->dso = handle;
 
     /* success */
     status = 0;
 
-done:
+    done:
     if (status != 0) {
         hmi = NULL;
         if (handle != NULL) {
             dlclose(handle);
             handle = NULL;
         }
+    } else {
+        LOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
+                id, path, *pHmi, handle);
     }
 
     *pHmi = hmi;
 
-    LOGV("load: X id=%s path=%s hmi=%p handle=%p status=%d",
-         id, path, *pHmi, handle, status);
     return status;
 }
 
@@ -127,6 +122,7 @@ int hw_get_module(const char *id, const struct hw_module_t **module)
     int i;
     const struct hw_module_t *hmi = NULL;
     char prop[PATH_MAX];
+    char path[PATH_MAX];
 
     /*
      * Here we rely on the fact that calling dlopen multiple times on
@@ -134,26 +130,32 @@ int hw_get_module(const char *id, const struct hw_module_t **module)
      * a new copy of the library).
      * We also assume that dlopen() is thread-safe.
      */
-    
-    LOGV("hal_module_info_get: Load module id=%s", id);
-
-    status = -EINVAL;
 
     /* Loop through the configuration variants looking for a module */
-    for (i = 0; (status != 0) && (i < HAL_VARIANT_KEYS_COUNT); i++) {
-        if (property_get(variant_keys[i], prop, NULL) == 0) {
+    for (i=0 ; i<HAL_VARIANT_KEYS_COUNT+1 ; i++) {
+        if (i < HAL_VARIANT_KEYS_COUNT) {
+            if (property_get(variant_keys[i], prop, NULL) == 0) {
+                continue;
+            }
+            snprintf(path, sizeof(path), "%s/%s.%s.so",
+                    HAL_LIBRARY_PATH, id, prop);
+        } else {
+            snprintf(path, sizeof(path), "%s/%s.default.so",
+                    HAL_LIBRARY_PATH, id);
+        }
+        if (access(path, R_OK)) {
             continue;
         }
-        status = load(id, prop, &hmi);
+        /* we found a library matching this id/variant */
+        break;
     }
 
-    /* Try default */
-    if (status != 0) {
-        status = load(id, HAL_DEFAULT_VARIANT, &hmi);
+    status = -ENOENT;
+    if (i < HAL_VARIANT_KEYS_COUNT+1) {
+        /* load the module, if this fails, we're doomed, and we should not try
+         * to load a different variant. */
+        status = load(id, path, module);
     }
-    
-    *module = hmi;
-    LOGV("hal_module_info_get: X id=%s hmi=%p status=%d", id, hmi, status);
 
     return status;
 }