From f860d77164b9ff4a9f8574ac1aadc6cf54c0942d Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Thu, 1 Jul 2021 14:36:12 +0900 Subject: [PATCH] callback: check feature before add/remove callback Change-Id: Id9ab9c9d68a54bcba444606ba7c62f46cc2c6d4c Signed-off-by: Youngjae Cho --- src/callback.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 6 deletions(-) diff --git a/src/callback.c b/src/callback.c index 91d9d25..f19d785 100644 --- a/src/callback.c +++ b/src/callback.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "callback.h" #include "battery.h" @@ -287,21 +288,71 @@ static int release_request(device_callback_e type) return -EINVAL; } +static int device_callback_check_feature_supported(device_callback_e type) +{ + static bool f_display = false; + static bool f_battery = false; + static bool f_flash = false; + + int retval; + bool supported; + + switch (type) { + case DEVICE_CALLBACK_BATTERY_CAPACITY: + case DEVICE_CALLBACK_BATTERY_LEVEL: + case DEVICE_CALLBACK_BATTERY_CHARGING: + if (f_battery) + return 0; + + retval = system_info_get_platform_bool("http://tizen.org/feature/battery", &supported); + if (retval != SYSTEM_INFO_ERROR_NONE || !supported) + return -ENOTSUP; + + f_battery = true; + return 0; + case DEVICE_CALLBACK_DISPLAY_STATE: + if (f_display) + return 0; + + retval = system_info_get_platform_bool("http://tizen.org/feature/display", &supported); + if (retval != SYSTEM_INFO_ERROR_NONE || !supported) + return -ENOTSUP; + retval = system_info_get_platform_bool("http://tizen.org/feature/display.state", &supported); + if (retval != SYSTEM_INFO_ERROR_NONE || !supported) + return -ENOTSUP; + + f_display = true; + return 0; + case DEVICE_CALLBACK_FLASH_BRIGHTNESS: + if (f_flash) + return 0; + + retval = system_info_get_platform_bool("http://tizen.org/feature/camera.back.flash", &supported); + if (retval != SYSTEM_INFO_ERROR_NONE || !supported) + return -ENOTSUP; + + f_flash = true; + return 0; + default: + return -EINVAL; + } +} + int device_add_callback(device_callback_e type, device_changed_cb cb, void *data) { struct device_cb_info *cb_info; GList *elem, *elem_next; int ret_request, n; - if (!is_feature_display_supported() && type == DEVICE_CALLBACK_DISPLAY_STATE) - return DEVICE_ERROR_NOT_SUPPORTED; - if (type < 0 || type >= DEVICE_CALLBACK_MAX) return DEVICE_ERROR_INVALID_PARAMETER; if (!cb) return DEVICE_ERROR_INVALID_PARAMETER; + if (device_callback_check_feature_supported(type) != 0) + return DEVICE_ERROR_NOT_SUPPORTED; + /* check if it is the first request */ n = SYS_G_LIST_LENGTH(device_cb_list[type]); if (n == 0) { @@ -335,15 +386,15 @@ int device_remove_callback(device_callback_e type, device_changed_cb cb) GList *elem, *elem_next; int ret_request, n; - if (!is_feature_display_supported() && type == DEVICE_CALLBACK_DISPLAY_STATE) - return DEVICE_ERROR_NOT_SUPPORTED; - if (type < 0 || type >= DEVICE_CALLBACK_MAX) return DEVICE_ERROR_INVALID_PARAMETER; if (!cb) return DEVICE_ERROR_INVALID_PARAMETER; + if (device_callback_check_feature_supported(type) != 0) + return DEVICE_ERROR_NOT_SUPPORTED; + /* search for the same element with callback */ SYS_G_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info) { if (cb_info->cb == cb) -- 2.7.4