--- /dev/null
+/*
+ * 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;
+};
--- /dev/null
+/*
+ * 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;
+}
*/
#include "sensor_loader.h"
+#include "sensor_device.h"
#include <dirent.h>
#include <dlfcn.h>
}
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");
}
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)
{