Use class sensor_device moved from hal-api-sensor 30/316930/10
authorSangYoun Kwak <sy.kwak@samsung.com>
Wed, 11 Dec 2024 09:06:55 +0000 (18:06 +0900)
committerSangYoun Kwak <sy.kwak@samsung.com>
Fri, 10 Jan 2025 06:51:35 +0000 (15:51 +0900)
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 <sy.kwak@samsung.com>
include/physical_sensor.h
include/sensor_device.h [new file with mode: 0644]
src/server/physical_sensor_handler.h
src/server/sensor_device.cpp [new file with mode: 0644]
src/server/sensor_loader.cpp
src/server/sensor_loader.h

index fd1e609e5d90a4eaef102342cb006131b85a2d45..7b114459b266e25dbf7311f809fa99ba7d1400d8 100644 (file)
@@ -27,6 +27,8 @@
 
 #include <hal/hal-sensor-types.h>
 
+#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 (file)
index 0000000..3e59c74
--- /dev/null
@@ -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 <stdint.h>
+
+#include <hal/hal-sensor.h>
+
+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;
+};
index 6743604090d2950456777fa5d0f4c11a1e78138d..f9619119d2e782bc55db89b7ac3f2e5d0f1dff38 100644 (file)
@@ -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 (file)
index 0000000..e231f15
--- /dev/null
@@ -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 <climits>
+
+#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;
+}
index 4daaa3c99b46e738ec89be5b99c3a9f2991a889b..ab037942493d07f1174766572edb4bddee2f6b2a 100644 (file)
@@ -18,6 +18,7 @@
  */
 
 #include "sensor_loader.h"
+#include "sensor_device.h"
 
 #include <dirent.h>
 #include <dlfcn.h>
@@ -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<sensor_device *>(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<typename T>
 bool sensor_loader::load(const std::string &dir_path, std::vector<std::shared_ptr<T>> &sensors)
 {
index 559d696a237764cdf71cd59ebcc564c9969ab1dd..a10901f21474f7cf26e3f03ac598db4efd881b6d 100644 (file)
@@ -28,6 +28,8 @@
 #include <memory>
 #include <map>
 
+#include "sensor_device.h"
+
 namespace sensor {
 
 typedef std::vector<std::shared_ptr<sensor_device>> device_sensor_registry_t;