diff options
-rw-r--r-- | src/hal-api-sensor.cpp | 111 |
1 files changed, 99 insertions, 12 deletions
diff --git a/src/hal-api-sensor.cpp b/src/hal-api-sensor.cpp index 01683b7..7dbbd99 100644 --- a/src/hal-api-sensor.cpp +++ b/src/hal-api-sensor.cpp @@ -13,19 +13,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +#include <stdlib.h> +#include <vector> #include <dlfcn.h> #include <hal/hal-common.h> #include "hal-api-sensor-log.h" #include "hal-sensor-interface.h" +#include "hal-sensor-types.h" #include "hal-sensor.h" #ifndef EXPORT #define EXPORT __attribute__((visibility("default"))) #endif -static hal_backend_sensor_funcs *hal_sensor_funcs = NULL; +static hal_backend_sensor_funcs **hal_sensor_funcs = NULL; +static char **hal_sensor_names = NULL; +static int hal_sensor_count = 0; /* -1 : failed to initialize 0 : not initialized @@ -33,33 +37,100 @@ static hal_backend_sensor_funcs *hal_sensor_funcs = NULL; */ static int hal_initialized = 0; +static std::vector<sensor_device_t> hal_backend_devs; + +#define FREE(x) \ + do { if (x) { free(x); x = NULL; } } while(0) + EXPORT int hal_sensor_get_backend(void) { - int ret; + int ret_getnames, ret_getbackend; + int i; + int loaded_backend = 0; if (hal_sensor_funcs) return 0; - ret = hal_common_get_backend(HAL_MODULE_SENSOR, (void **)&hal_sensor_funcs); - if (ret < 0) { - _E("Failed to get backend"); - hal_initialized = -1; + hal_sensor_count = hal_common_get_backend_count(HAL_MODULE_SENSOR); + if (hal_sensor_count < 0) { + _E("Failed to get backend count."); + return -ENODEV; + } else if (hal_sensor_count == 0) { + _E("There is no hal-backend-sensor library."); + return -ENODEV; + } + + hal_sensor_names = (char **)malloc(sizeof(char *) * hal_sensor_count); + if (!hal_sensor_names) { + _E("Failed to allocate memory."); + hal_sensor_count = 0; return -ENODEV; } + for (i = 0; i < hal_sensor_count; i++) { + hal_sensor_names[i] = (char *)malloc(sizeof(char) * 256); + if (!hal_sensor_names[i]) { + _E("Failed to allocate memory."); + goto FREE_MEMORY; + } + } + + ret_getnames = hal_common_get_backend_library_names(HAL_MODULE_SENSOR, hal_sensor_names, hal_sensor_count); + if (ret_getnames < 0) { + _E("Failed to get backend library names."); + goto FREE_MEMORY; + } + + hal_sensor_funcs = (hal_backend_sensor_funcs **)malloc(sizeof(hal_backend_sensor_funcs *) * hal_sensor_count); + if (!hal_sensor_funcs) { + _E("Failed to allocate memory."); + goto FREE_MEMORY; + } + + for(i = 0; i < hal_sensor_count; i++) { + ret_getbackend = hal_common_get_backend_with_library_name(HAL_MODULE_SENSOR, (void **)&hal_sensor_funcs[i], hal_sensor_names[i]); + if (ret_getbackend < 0) { + _E("Failed to get backend (%s)", hal_sensor_names[i]); + } else + loaded_backend++; + } + + if (loaded_backend == 0) { + FREE(hal_sensor_funcs); + goto FREE_MEMORY; + } + hal_initialized = 1; return 0; + +FREE_MEMORY: + for(i = 0; i < hal_sensor_count; i++) + FREE(hal_sensor_names[i]); + + FREE(hal_sensor_names); + + hal_sensor_count = 0; + hal_initialized = -1; + return -ENODEV; } EXPORT int hal_sensor_put_backend(void) { + int i = 0; if (!hal_sensor_funcs) return -ENODEV; - hal_common_put_backend(HAL_MODULE_SENSOR, (void *)hal_sensor_funcs); - hal_sensor_funcs = NULL; + for (i = 0; i < hal_sensor_count; i++) { + if (hal_sensor_funcs[i]) { + hal_common_put_backend_with_library_name(HAL_MODULE_SENSOR, (void *)hal_sensor_funcs[i], hal_sensor_names[i]); + } + FREE(hal_sensor_names[i]); + } + FREE(hal_sensor_names); + FREE(hal_sensor_funcs); + hal_initialized = 0; return 0; @@ -68,15 +139,31 @@ int hal_sensor_put_backend(void) EXPORT int hal_sensor_create(sensor_device_t **devices) { - int ret; + int ret, ret_create; + int i, j; + sensor_device_t *device; if (!hal_sensor_funcs && !hal_initialized) { if ((ret = hal_sensor_get_backend()) < 0) return ret; } - if (!hal_sensor_funcs || !hal_sensor_funcs->create) + if (!hal_sensor_funcs || (hal_sensor_count == 0)) return -ENODEV; - return hal_sensor_funcs->create(devices); + if (hal_backend_devs.empty()) { + for (i = 0; i < hal_sensor_count; i++) { + if (!hal_sensor_funcs[i] || !hal_sensor_funcs[i]->create) + continue; + + ret_create = hal_sensor_funcs[i]->create(&device); + for (j = 0; j < ret_create; j++) + { + hal_backend_devs.push_back(device[j]); + } + } + } + + *devices = &hal_backend_devs[0]; + return hal_backend_devs.size(); } |