/* * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License") * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ #include #include #include #include #include #include static GSList *ua_sensors_list = NULL; /* ua_sensor_info_s */ static ua_feature_table_t feature_table[] = { {"tizen.org/feature/network.user_awareness", FALSE, FALSE}, /* UA_FEATURE_COMMON */ }; int _ua_check_supported_feature(ua_feature_t feature, bool *supported) { int ret; if (feature < UA_FEATURE_BASE || feature >= UA_FEATURE_MAX) { UA_ERR("Invalid parameter. feature [%d]", feature); return UA_ERROR_INVALID_PARAMETER; } if (TRUE == feature_table[feature].is_check) { *supported = feature_table[feature].value; return UA_ERROR_NONE; } ret = system_info_get_platform_bool(feature_table[feature].name, supported); if (SYSTEM_INFO_ERROR_NONE != ret) { UA_ERR("fail to get %s", feature_table[feature].name); return UA_ERROR_OPERATION_FAILED; } feature_table[feature].is_check = TRUE; feature_table[feature].value = *supported; return UA_ERROR_NONE; } ua_mac_type_e _to_ua_mac_type(uam_tech_type_e tech_type) { switch (tech_type) { case UAM_TECH_TYPE_BT: return UA_MAC_TYPE_BT; case UAM_TECH_TYPE_BLE: return UA_MAC_TYPE_BLE; case UAM_TECH_TYPE_WIFI: return UA_MAC_TYPE_WIFI; case UAM_TECH_TYPE_P2P: return UA_MAC_TYPE_P2P; default: return UA_MAC_TYPE_INVALID; } } int _ua_get_error_code(int ua_error) { switch (ua_error) { case UAM_ERROR_NONE: return UA_ERROR_NONE; case UAM_ERROR_INVALID_PARAMETER: return UA_ERROR_INVALID_PARAMETER; /* LCOV_EXCL_START */ case UAM_ERROR_RESOURCE_BUSY: return UA_ERROR_RESOURCE_BUSY; case UAM_ERROR_PERMISSION_DENIED: return UA_ERROR_PERMISSION_DENIED; case UAM_ERROR_TIMED_OUT: return UA_ERROR_TIMED_OUT; case UAM_ERROR_NOW_IN_PROGRESS: return UA_ERROR_NOW_IN_PROGRESS; case UAM_ERROR_NOT_SUPPORTED: return UA_ERROR_NOT_SUPPORTED; case UAM_ERROR_NOT_INITIALIZED: return UA_ERROR_NOT_INITIALIZED; case UAM_ERROR_NOT_IN_OPERATION: return UA_ERROR_NOT_IN_PROGRESS; case UAM_ERROR_ALREADY_DONE: return UA_ERROR_ALREADY_DONE; case UAM_ERROR_INTERNAL: return UA_ERROR_OPERATION_FAILED; /* LCOV_EXCL_STOP */ case UAM_ERROR_NOT_FOUND: return UA_ERROR_NOT_FOUND; /* LCOV_EXCL_START */ case UAM_ERROR_ALREADY_REGISTERED: return UA_ERROR_ALREADY_REGISTERED; case UAM_ERROR_DB_FAILED: return UA_ERROR_DB_FAILED; case UAM_ERROR_NOT_REGISTERED: return UA_ERROR_NOT_REGISTERED; /* LCOV_EXCL_STOP */ default: return UA_ERROR_OPERATION_FAILED; } } const char *_ua_get_error_string(ua_error_e err) { const char *err_str = NULL; switch (err) { case UA_ERROR_NONE: err_str = "UA_ERROR_NONE"; break; /* LCOV_EXCL_START */ case UA_ERROR_NOT_PERMITTED: err_str = "UA_ERROR_NOT_PERMITTED"; break; /* LCOV_EXCL_STOP */ case UA_ERROR_INVALID_PARAMETER: err_str = "UA_ERROR_INVALID_PARAMETER"; break; /* LCOV_EXCL_START */ case UA_ERROR_OUT_OF_MEMORY: err_str = "UA_ERROR_OUT_OF_MEMORY"; break; case UA_ERROR_PERMISSION_DENIED: err_str = "UA_ERROR_PERMISSION_DENIED"; break; case UA_ERROR_RESOURCE_BUSY: err_str = "UA_ERROR_RESOURCE_BUSY"; break; case UA_ERROR_TIMED_OUT: err_str = "UA_ERROR_TIMED_OUT"; break; case UA_ERROR_CANCELED: err_str = "UA_ERROR_CANCELED"; break; case UA_ERROR_NOW_IN_PROGRESS: err_str = "UA_ERROR_NOW_IN_PROGRESS"; break; case UA_ERROR_NOT_SUPPORTED: err_str = "UA_ERROR_NOT_SUPPORTED"; break; case UA_ERROR_NO_DATA: err_str = "UA_ERROR_NO_DATA"; break; case UA_ERROR_NOT_INITIALIZED: err_str = "UA_ERROR_NOT_INITIALIZED"; break; case UA_ERROR_NOT_IN_PROGRESS: err_str = "UA_ERROR_NOT_IN_PROGRESS"; break; case UA_ERROR_ALREADY_DONE: err_str = "UA_ERROR_ALREADY_DONE"; break; /* LCOV_EXCL_STOP */ case UA_ERROR_OPERATION_FAILED: err_str = "UA_ERROR_OPERATION_FAILED"; break; case UA_ERROR_NOT_FOUND: err_str = "UA_ERROR_NOT_FOUND"; break; /* LCOV_EXCL_START */ case UA_ERROR_ALREADY_REGISTERED: err_str = "UA_ERROR_ALREADY_REGISTERED"; break; case UA_ERROR_DB_FAILED: err_str = "UA_ERROR_DB_FAILED"; break; case UA_ERROR_NOT_REGISTERED: err_str = "UA_ERROR_NOT_REGISTERED"; break; default: err_str = "NOT Defined"; break; /* LCOV_EXCL_STOP */ } return err_str; } uam_tech_type_e _ua_to_uam_tech_type(ua_mac_type_e type) { switch (type) { case UA_MAC_TYPE_BT: return UAM_TECH_TYPE_BT; case UA_MAC_TYPE_BLE: return UAM_TECH_TYPE_BLE; case UA_MAC_TYPE_WIFI: return UAM_TECH_TYPE_WIFI; case UA_MAC_TYPE_P2P: return UAM_TECH_TYPE_P2P; default: return UA_MAC_TYPE_INVALID; } } ua_sensor_type_e _ua_dev_type_to_sensor(ua_mac_type_e type) { FUNC_ENTRY; switch (type) { case UA_MAC_TYPE_BT: return UA_SENSOR_BT; case UA_MAC_TYPE_BLE: return UA_SENSOR_BLE; case UA_MAC_TYPE_WIFI: return UA_SENSOR_WIFI; default: UA_WARN("Uncompatible to convert. Mac type [%u]", type); return UA_SENSOR_MAX; } FUNC_EXIT; } ua_sensor_type_e _uam_to_ua_sensor(uam_sensor_bitmask_e bitmask) { FUNC_ENTRY; switch (bitmask) { case UAM_SENSOR_BITMASK_BT: return UA_SENSOR_BT; case UAM_SENSOR_BITMASK_BLE: return UA_SENSOR_BLE; case UAM_SENSOR_BITMASK_WIFI: return UA_SENSOR_WIFI; case UAM_SENSOR_BITMASK_MOTION: return UA_SENSOR_MOTION; case UAM_SENSOR_BITMASK_LIGHT: return UA_SENSOR_LIGHT; case UAM_SENSOR_BITMASK_AUDIO: return UA_SENSOR_AUDIO; default: UA_WARN("Unknown sensor bitmask [0x%8.8X]", bitmask); return UA_SENSOR_MAX; } FUNC_EXIT; } static void __ua_free_ua_sensor_info_s(gpointer data) { FUNC_ENTRY; ua_sensor_info_s *sensor_info = (ua_sensor_info_s*)data; if (sensor_info == NULL) return; g_free(sensor_info->values); g_free(sensor_info); FUNC_EXIT; } void _ua_clear_sensor_list(void) { g_slist_free_full(ua_sensors_list, __ua_free_ua_sensor_info_s); ua_sensors_list = NULL; } static ua_sensor_info_s* __ua_allocate_ua_sensor_info() { ua_sensor_info_s *sensor_info; sensor_info = g_malloc0(sizeof(ua_sensor_info_s)); if (!sensor_info) { UA_ERR("g_malloc0 failed"); return NULL; } sensor_info->values = NULL; sensor_info->values = g_malloc0(UA_SENSOR_MAX_VALUES*sizeof(double)); if (!sensor_info->values) { UA_ERR("g_malloc0 failed"); g_free(sensor_info); return NULL; } return sensor_info; } static ua_sensor_info_s *__ua_get_sensor_info_from_list(ua_sensor_type_e bitmask) { GSList *l; ua_sensor_info_s *sensor; for (l = ua_sensors_list; NULL != l; l = g_slist_next(l)) { sensor = (ua_sensor_info_s *)l->data; if (bitmask == sensor->bitmask) { UA_INFO("Sensor info found, sensor bitmask[%d]", sensor->bitmask); return sensor; } } return NULL; } static int __ua_update_sensor_info( ua_sensor_info_s *sensor, ua_sensor_info_s *info) { FUNC_ENTRY; unsigned int i = 0; UA_VALIDATE_INPUT_PARAMETER(sensor); UA_VALIDATE_INPUT_PARAMETER(info); sensor->timestamp = info->timestamp; sensor->count = info->count; sensor->bitmask = info->bitmask; for (i = 0; i < UA_SENSOR_MAX_VALUES; i++) sensor->values[i] = info->values[i]; FUNC_EXIT; return UA_ERROR_NONE; } int _ua_free_sensor_info(ua_sensor_info_s *sensor_info) { retv_if(NULL == sensor_info, UA_ERROR_INVALID_PARAMETER); g_free(sensor_info->values); g_free(sensor_info); return UA_ERROR_NONE; } ua_sensor_info_s* _uam_to_ua_sensor_info(uam_sensor_info_s *info) { FUNC_ENTRY; ua_sensor_info_s *sensor_info = NULL; unsigned int i = 0; sensor_info = __ua_allocate_ua_sensor_info(); if (!sensor_info) return NULL; sensor_info->status = info->status; sensor_info->timestamp = info->timestamp; sensor_info->count = info->count; sensor_info->bitmask = _uam_to_ua_sensor(info->sensor_bitmask); for (i = 0; i < UA_SENSOR_MAX_VALUES; i++) sensor_info->values[i] = info->values[i]; return sensor_info; FUNC_EXIT; } int _ua_sensor_get_by_sensor_info( ua_sensor_info_s *info, ua_sensor_h *sensor_handle) { FUNC_ENTRY; int ret = UA_ERROR_NONE; ua_sensor_info_s *sensor; *sensor_handle = NULL; UA_VALIDATE_INPUT_PARAMETER(info); UA_VALIDATE_INPUT_PARAMETER(sensor_handle); sensor = __ua_get_sensor_info_from_list(info->bitmask); if (NULL != sensor) { ret = __ua_update_sensor_info(sensor, info); *sensor_handle = (ua_sensor_h)sensor; goto done; } sensor = __ua_allocate_ua_sensor_info(); if (!sensor) return UA_ERROR_OUT_OF_MEMORY; ret = __ua_update_sensor_info(sensor, info); /* Add sensor to list of sensors */ *sensor_handle = (ua_sensor_h)sensor; sensor->handle = *sensor_handle; ua_sensors_list = g_slist_append(ua_sensors_list, sensor); done: FUNC_EXIT; return ret; } GSList *_ua_sensor_get_sensor_handle_list( unsigned int bitmask) { FUNC_ENTRY; GSList *sensors = NULL; GSList *l; ua_sensor_info_s *sensor; for (l = ua_sensors_list; NULL != l; l = g_slist_next(l)) { sensor = (ua_sensor_info_s *)l->data; if (bitmask & sensor->bitmask) { UA_INFO("Sensor info found, sensor bitmask[%d]", sensor->bitmask); sensors = g_slist_append(sensors, sensor->handle); } } FUNC_EXIT; return sensors; } int ua_sensor_get_status( ua_sensor_h sensor_handle, ua_sensor_report_e *status) { FUNC_ENTRY; ua_sensor_info_s *sensor; UA_CHECK_SUPPORTED_FEATURE(UA_FEATURE_COMMON); UA_CHECK_INIT_STATUS(); UA_VALIDATE_INPUT_PARAMETER(sensor_handle); UA_INFO("Sensor Handle [%p]", sensor_handle); sensor = (ua_sensor_info_s *)sensor_handle; *status = sensor->status; FUNC_EXIT; return UA_ERROR_NONE; } int ua_sensor_get_timestamp( ua_sensor_h sensor_handle, unsigned long long *timestamp) { FUNC_ENTRY; ua_sensor_info_s *sensor; UA_CHECK_SUPPORTED_FEATURE(UA_FEATURE_COMMON); UA_CHECK_INIT_STATUS(); UA_VALIDATE_INPUT_PARAMETER(sensor_handle); UA_INFO("Sensor Handle [%p]", sensor_handle); sensor = (ua_sensor_info_s *)sensor_handle; *timestamp = sensor->timestamp; FUNC_EXIT; return UA_ERROR_NONE; } int ua_sensor_get_type( ua_sensor_h sensor_handle, ua_sensor_type_e *type) { FUNC_ENTRY; ua_sensor_info_s *sensor; UA_CHECK_SUPPORTED_FEATURE(UA_FEATURE_COMMON); UA_CHECK_INIT_STATUS(); UA_VALIDATE_INPUT_PARAMETER(sensor_handle); UA_INFO("Sensor Handle [%p]", sensor_handle); sensor = (ua_sensor_info_s *)sensor_handle; *type = sensor->bitmask; FUNC_EXIT; return UA_ERROR_NONE; }