From: SangYoun Kwak Date: Wed, 11 Dec 2024 09:06:55 +0000 (+0900) Subject: Use class sensor_device moved from hal-api-sensor X-Git-Tag: accepted/tizen/9.0/unified/20250116.154302~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=18178ba8009c4941867a516a12e80946cd8eaba5;p=platform%2Fcore%2Fsystem%2Fsensord.git Use class sensor_device moved from hal-api-sensor Since the usage of the hal-api-sensor is changed, sensord should be changed according to the change of hal-pai-sensor. Below are change of hal-api-sensor: * No C++ class in the header file * Sensor device creation function does not return sensor_device object but a unique ID for that sensor device object in the hal backend. * All methods of sensor_device is implemented as a separated C functions, which has same name as the original method name but has a prefix "hal_sensor". Also those functions should be called with sensor ID which is passed from creation function to distinghush sensor devices. According to those changes, sensord is also changed: * Create a sensor_device compatible class to emulate the original sensor_device class. * Some minor changes according to the removal of some typedefs. Change-Id: Ia1892d87d578a720ad63e228dedb5a0d58f91037 Signed-off-by: SangYoun Kwak --- diff --git a/include/physical_sensor.h b/include/physical_sensor.h index fd1e609e..7b114459 100644 --- a/include/physical_sensor.h +++ b/include/physical_sensor.h @@ -27,6 +27,8 @@ #include +#include "sensor_device.h" + #ifndef SENSOR_VERSION #define PHYSICAL_SENSOR_VERSION(maj, min) \ ((((maj) & 0xFFFF) << 24) | ((min) & 0xFFFF)) diff --git a/include/sensor_device.h b/include/sensor_device.h new file mode 100644 index 00000000..3e59c746 --- /dev/null +++ b/include/sensor_device.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#pragma once + +#include + +#include + +class sensor_device { +public: + sensor_device(uint64_t sensor_device_id); + ~sensor_device(); + + int get_poll_fd(void); + int get_sensors(const sensor_info_t **sensors); + + bool enable(uint32_t id); + bool disable(uint32_t id); + + int read_fd(uint32_t **ids); + int get_data(uint32_t id, sensor_data_t **data, int *length); + + bool set_interval(uint32_t id, unsigned long val); + bool set_batch_latency(uint32_t id, unsigned long val); + bool set_attribute_int(uint32_t id, int32_t attribute, int32_t value); + bool set_attribute_str(uint32_t id, int32_t attribute, const char *value, int len) ; + bool get_attribute_int(uint32_t id, int32_t attribute, int32_t *value); + bool get_attribute_str(uint32_t id, int32_t attribute, char **value, int *len); + + bool flush(uint32_t id); + +private: + uint64_t sensor_device_id; +}; diff --git a/src/server/physical_sensor_handler.h b/src/server/physical_sensor_handler.h index 67436040..f9619119 100644 --- a/src/server/physical_sensor_handler.h +++ b/src/server/physical_sensor_handler.h @@ -26,6 +26,7 @@ #include "sensor_handler.h" #include "sensor_observer.h" +#include "sensor_device.h" namespace sensor { diff --git a/src/server/sensor_device.cpp b/src/server/sensor_device.cpp new file mode 100644 index 00000000..e231f154 --- /dev/null +++ b/src/server/sensor_device.cpp @@ -0,0 +1,156 @@ +/* + * sensord + * + * Copyright (c) 2025 Samsung Electronics Co., Ltd. + * + * 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 "sensor_log.h" +#include "sensor_device.h" + +sensor_device::sensor_device(uint64_t sensor_device_id): + sensor_device_id(sensor_device_id) { } + +sensor_device::~sensor_device() +{ + hal_sensor_delete(&sensor_device_id, 1); +} + +int sensor_device::get_poll_fd(void) +{ + int poll_fd = 0; + int ret = 0; + + ret = hal_sensor_get_poll_fd(sensor_device_id, &poll_fd); + if (ret < 0) + return ret; + + return poll_fd; +} + +int sensor_device::get_sensors(const sensor_info_t **sensors) +{ + size_t sensors_len = 0; + int ret = 0; + + ret = hal_sensor_get_sensors(sensor_device_id, sensors, &sensors_len); + if (ret < 0) + return ret; + + if (sensors_len > (size_t)INT_MAX) + return -EINVAL; + + return sensors_len; +} + +bool sensor_device::enable(uint32_t id) +{ + return hal_sensor_enable(sensor_device_id, id) == 0; +} + +bool sensor_device::disable(uint32_t id) +{ + return hal_sensor_disable(sensor_device_id, id) == 0; +} + +int sensor_device::read_fd(uint32_t **ids) +{ + size_t ids_len = 0; + int ret = 0; + + ret = hal_sensor_read_fd(sensor_device_id, ids, &ids_len); + if (ret < 0) + return ret; + + if (ids_len > (size_t)INT_MAX) + return -EINVAL; + + return ids_len; +} + +int sensor_device::get_data(uint32_t id, sensor_data_t **data, int *length) +{ + size_t data_length = 0; + int ret = 0; + + ret = hal_sensor_get_data(sensor_device_id, id, data, &data_length); + if (ret < 0) + return ret; + + if (data_length > (size_t)INT_MAX) + return -EINVAL; + *length = data_length; + + return 0; +} + +bool sensor_device::set_interval(uint32_t id, unsigned long val) +{ + return hal_sensor_set_interval(sensor_device_id, id, val) == 0; +} + +bool sensor_device::set_batch_latency(uint32_t id, unsigned long val) +{ + return hal_sensor_set_batch_latency(sensor_device_id, id, val) == 0; +} + +bool sensor_device::set_attribute_int(uint32_t id, int32_t attribute, int32_t value) +{ + return hal_sensor_set_attribute_int(sensor_device_id, id, attribute, value) == 0; +} + +bool sensor_device::set_attribute_str(uint32_t id, int32_t attribute, const char *value, int len) +{ + if (len < 0) + return false; + + return hal_sensor_set_attribute_str(sensor_device_id, id, attribute, value, len) == 0; +} + +bool sensor_device::get_attribute_int(uint32_t id, int32_t attribute, int32_t *value) +{ + return hal_sensor_get_attribute_int(sensor_device_id, id, attribute, value) == 0; +} + +bool sensor_device::get_attribute_str(uint32_t id, int32_t attribute, char **value, int *len) +{ + char *temp_value = nullptr; + size_t temp_value_len = 0; + int ret = 0; + + ret = hal_sensor_get_attribute_str(sensor_device_id, id, attribute, &temp_value, &temp_value_len); + if (ret < 0) + return false; + + if (temp_value_len > (size_t)INT_MAX) { + delete[] value; + return false; + } + + *value = temp_value; + *len = temp_value_len; + + return true; +} + +bool sensor_device::flush(uint32_t id) +{ + return hal_sensor_flush(sensor_device_id, id) == 0; +} diff --git a/src/server/sensor_loader.cpp b/src/server/sensor_loader.cpp index 4daaa3c9..ab037942 100644 --- a/src/server/sensor_loader.cpp +++ b/src/server/sensor_loader.cpp @@ -18,6 +18,7 @@ */ #include "sensor_loader.h" +#include "sensor_device.h" #include #include @@ -46,17 +47,22 @@ sensor_loader::~sensor_loader() { } void sensor_loader::load_hal(device_sensor_registry_t &devices) { - void **results = nullptr; + uint64_t *sensor_device_ids = nullptr; + size_t size = 0; + int ret = 0; - int size = hal_sensor_create(&results); - if (size <= 0 || !results) { - _E("Failed to get sensor from hal sensor backend"); + ret = hal_sensor_create(&sensor_device_ids, &size); + if (ret < 0 || !sensor_device_ids) { + _E("Failed to get sensor from hal sensor backend: ret(%d)", ret); return; } - for (int i = 0; i < size; ++i) { - devices.emplace_back(static_cast(results[i])); + for (size_t i = 0; i < size; ++i) { + devices.emplace_back(new sensor_device(sensor_device_ids[i])); } + + free(sensor_device_ids); + _I("Success to load sensor from hal sensor backend"); } @@ -87,6 +93,9 @@ void sensor_loader::unload(void) dlclose(it->second); } +typedef void * sensor_device_t; +typedef int (*create_t)(sensor_device_t **devices); + template bool sensor_loader::load(const std::string &dir_path, std::vector> &sensors) { diff --git a/src/server/sensor_loader.h b/src/server/sensor_loader.h index 559d696a..a10901f2 100644 --- a/src/server/sensor_loader.h +++ b/src/server/sensor_loader.h @@ -28,6 +28,8 @@ #include #include +#include "sensor_device.h" + namespace sensor { typedef std::vector> device_sensor_registry_t;