From 4b985b6a8a86ec9289f38d07e95d784dde4b82c5 Mon Sep 17 00:00:00 2001 From: Yunmi Ha Date: Mon, 5 Apr 2021 20:10:04 +0900 Subject: [PATCH] Load HAL module before running API Change-Id: Ibe74a9b72c30ea96f61efb7110a8c897eeffd5c7 Signed-off-by: Yunmi Ha --- src/hal-api-sensor.cpp | 67 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/src/hal-api-sensor.cpp b/src/hal-api-sensor.cpp index e4b535a..01683b7 100644 --- a/src/hal-api-sensor.cpp +++ b/src/hal-api-sensor.cpp @@ -25,43 +25,58 @@ #define EXPORT __attribute__((visibility("default"))) #endif -static hal_backend_sensor_funcs *g_sensor_funcs = NULL; +static hal_backend_sensor_funcs *hal_sensor_funcs = NULL; +/* +-1 : failed to initialize +0 : not initialized +1 : succeeded to initialize +*/ +static int hal_initialized = 0; EXPORT -int hal_sensor_get_backend(void) { - int ret; +int hal_sensor_get_backend(void) +{ + int ret; - if (g_sensor_funcs) return 0; + if (hal_sensor_funcs) + return 0; - ret = hal_common_get_backend(HAL_MODULE_SENSOR, (void **)&g_sensor_funcs); - if (ret < 0) { - _E("Failed to get backend\n"); - return -EINVAL; - } + ret = hal_common_get_backend(HAL_MODULE_SENSOR, (void **)&hal_sensor_funcs); + if (ret < 0) { + _E("Failed to get backend"); + hal_initialized = -1; + return -ENODEV; + } - return 0; + hal_initialized = 1; + return 0; } EXPORT -int hal_sensor_put_backend(void) { - int ret; - - if (!g_sensor_funcs) return -EINVAL; +int hal_sensor_put_backend(void) +{ + if (!hal_sensor_funcs) + return -ENODEV; - ret = hal_common_put_backend(HAL_MODULE_SENSOR, (void *)g_sensor_funcs); - if (ret < 0) { - _E("Failed to put backend\n"); - return -EINVAL; - } - g_sensor_funcs = NULL; + hal_common_put_backend(HAL_MODULE_SENSOR, (void *)hal_sensor_funcs); + hal_sensor_funcs = NULL; + hal_initialized = 0; - return 0; + return 0; } EXPORT -int hal_sensor_create(sensor_device_t **devices) { - if (!g_sensor_funcs) { - return -ENOTSUP; - } - return g_sensor_funcs->create(devices); +int hal_sensor_create(sensor_device_t **devices) +{ + int ret; + + if (!hal_sensor_funcs && !hal_initialized) { + if ((ret = hal_sensor_get_backend()) < 0) + return ret; + } + + if (!hal_sensor_funcs || !hal_sensor_funcs->create) + return -ENODEV; + + return hal_sensor_funcs->create(devices); } -- 2.7.4