sensord: add physical_sensor_handler class 46/123646/1
authorkibak.yoon <kibak.yoon@samsung.com>
Thu, 6 Apr 2017 09:43:32 +0000 (18:43 +0900)
committerkibak.yoon <kibak.yoon@samsung.com>
Thu, 6 Apr 2017 09:43:32 +0000 (18:43 +0900)
- physical_sensor_handler provides:
  - controls for operating sensor HAL(sensor_device)
  - controls for operation policy

Change-Id: I4c5d6b2d99847485ffd8beec1a9634b20ce45e71
Signed-off-by: kibak.yoon <kibak.yoon@samsung.com>
src/server/physical_sensor_handler.cpp [new file with mode: 0644]
src/server/physical_sensor_handler.h [new file with mode: 0644]

diff --git a/src/server/physical_sensor_handler.cpp b/src/server/physical_sensor_handler.cpp
new file mode 100644 (file)
index 0000000..00cf361
--- /dev/null
@@ -0,0 +1,286 @@
+/*
+ * sensord
+ *
+ * Copyright (c) 2017 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 "physical_sensor_handler.h"
+
+#include <sensor_log.h>
+#include <command_types.h>
+#include <message.h>
+#include <algorithm>
+
+using namespace sensor;
+
+physical_sensor_handler::physical_sensor_handler(const sensor_info &info,
+               sensor_device *device, int hal_id,
+               physical_sensor *sensor)
+: m_info(info)
+, m_device(device)
+, m_sensor(sensor)
+, m_hal_id(hal_id)
+{
+}
+
+physical_sensor_handler::~physical_sensor_handler()
+{
+}
+
+const sensor_info &physical_sensor_handler::get_sensor_info(void)
+{
+       return m_info;
+}
+
+int physical_sensor_handler::get_hal_id(void)
+{
+       return m_hal_id;
+}
+
+int physical_sensor_handler::get_poll_fd(void)
+{
+       retv_if(!m_device, -EINVAL);
+
+       return m_device->get_poll_fd();
+}
+
+int physical_sensor_handler::read_fd(std::vector<uint32_t> &ids)
+{
+       int size;
+       uint32_t *_ids;
+
+       retv_if(!m_device, -EINVAL);
+
+       size = m_device->read_fd(&_ids);
+       retv_if(size == 0, -ENODATA);
+
+       for (int i = 0; i < size; ++i)
+               ids.push_back(_ids[i]);
+
+       return OP_SUCCESS;
+}
+
+int physical_sensor_handler::on_event(const sensor_data_t *data, int32_t len, int32_t remains)
+{
+       retv_if(!m_device, -EINVAL);
+
+       if (m_sensor) {
+               int ret = m_sensor->on_event(const_cast<sensor_data_t *>(data), len, remains);
+               retv_if(ret <= OP_ERROR, ret);
+       }
+
+       return OP_SUCCESS;
+}
+int physical_sensor_handler::start(sensor_observer *ob)
+{
+       retv_if(!m_device, -EINVAL);
+
+       int policy = OP_DEFAULT;
+
+       if (m_sensor) {
+               policy = m_sensor->start(ob);
+               retv_if(policy <= OP_ERROR, policy);
+       }
+
+       add_observer(ob);
+
+       if (policy == OP_DEFAULT) {
+               if (observer_count() > 1)
+                       return OP_SUCCESS; /* already started */
+       }
+
+       return m_device->enable(m_hal_id);
+}
+
+int physical_sensor_handler::stop(sensor_observer *ob)
+{
+       retv_if(!m_device, -EINVAL);
+
+       int policy = OP_DEFAULT;
+
+       if (m_sensor) {
+               policy = m_sensor->stop(ob);
+               retv_if(policy <= OP_ERROR, policy);
+       }
+
+       remove_observer(ob);
+
+       if (policy == OP_DEFAULT) {
+               if (observer_count() >= 1)
+                       return OP_SUCCESS; /* already stopped */
+       }
+
+       return m_device->disable(m_hal_id);
+}
+
+int physical_sensor_handler::get_min_interval(void)
+{
+       int interval;
+       std::vector<int> temp;
+
+       for (auto it = m_interval_map.begin(); it != m_interval_map.end(); ++it)
+               if (it->second > 0)
+                   temp.push_back(it->second);
+
+       if (temp.empty())
+               return m_info.get_min_interval();
+
+       interval = *std::min_element(temp.begin(), temp.end());
+
+       if (interval < m_info.get_min_interval())
+               return m_info.get_min_interval();
+
+       return interval;
+}
+
+int physical_sensor_handler::set_interval(sensor_observer *ob, int32_t interval)
+{
+       retv_if(!m_device, -EINVAL);
+
+       bool ret = false;
+       int _interval = interval;
+       int policy = OP_DEFAULT;
+
+       if (m_sensor) {
+               policy = m_sensor->set_interval(ob, interval);
+               retv_if(policy <= OP_ERROR, policy);
+       }
+
+       m_interval_map[ob] = interval;
+
+       if (policy == OP_DEFAULT)
+               _interval = get_min_interval();
+
+       ret = m_device->set_interval(m_hal_id, _interval);
+
+       return (ret ? OP_SUCCESS : OP_SUCCESS);
+}
+
+int physical_sensor_handler::get_min_batch_latency(void)
+{
+       int batch_latency;
+       std::vector<int> temp;
+
+       for (auto it = m_batch_latency_map.begin(); it != m_batch_latency_map.end(); ++it)
+               if (it->second > 0)
+                   temp.push_back(it->second);
+
+       if (temp.empty())
+               return 0;
+
+       batch_latency = *std::min_element(temp.begin(), temp.end());
+
+       return batch_latency;
+}
+
+int physical_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency)
+{
+       retv_if(!m_device, -EINVAL);
+
+       bool ret = false;
+       int _latency = latency;
+       int policy = OP_DEFAULT;
+
+       if (m_sensor) {
+               policy = m_sensor->set_batch_latency(ob, latency);
+               retv_if(policy <= OP_ERROR, policy);
+       }
+
+       m_batch_latency_map[ob] = _latency;
+
+       if (_latency <= latency)
+               return OP_SUCCESS;
+
+       ret = m_device->set_batch_latency(m_hal_id, _latency);
+
+       return (ret ? OP_SUCCESS : OP_SUCCESS);
+}
+
+int physical_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value)
+{
+       retv_if(!m_device, -EINVAL);
+
+       bool ret = false;
+       int policy = OP_DEFAULT;
+
+       if (m_sensor) {
+               policy = m_sensor->set_attribute(ob, attr, value);
+               retv_if(policy <= OP_ERROR, policy);
+       }
+
+       /*
+        * TODO: default policy for attribute?
+       if (policy == OP_DEFAULT) {
+               if (observer_count() > 1)
+                       return OP_SUCCESS;
+       }
+       */
+
+       ret = m_device->set_attribute_int(m_hal_id, attr, value);
+
+       return (ret ? OP_SUCCESS : OP_SUCCESS);
+}
+
+int physical_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len)
+{
+       retv_if(!m_device, -EINVAL);
+
+       bool ret = false;
+       int policy = OP_DEFAULT;
+
+       if (m_sensor) {
+               policy = m_sensor->set_attribute(ob, attr, value, len);
+               retv_if(policy <= OP_ERROR, policy);
+       }
+
+       /*
+        * TODO: default policy for attribute?
+       if (policy == OP_DEFAULT) {
+               if (observer_count() > 1)
+                       return OP_SUCCESS;
+       }
+       */
+
+       ret = m_device->set_attribute_str(m_hal_id, attr, const_cast<char *>(value), len);
+
+       return (ret ? OP_SUCCESS : OP_SUCCESS);
+}
+
+int physical_sensor_handler::flush(sensor_observer *ob)
+{
+       retv_if(!m_device, -EINVAL);
+       int ret = false;
+
+       if (m_sensor) {
+               ret = m_sensor->flush(ob);
+               retv_if(ret <= OP_ERROR, ret);
+       }
+
+       ret = m_device->flush(m_hal_id);
+
+       return (ret ? OP_SUCCESS : OP_ERROR);
+}
+
+int physical_sensor_handler::get_data(sensor_data_t **data, int *len)
+{
+       retv_if(!m_device, -EINVAL);
+
+       int remains = m_device->get_data(m_hal_id, data, len);
+       retvm_if(*len <= 0, OP_ERROR, "Failed to get sensor event");
+
+       return remains;
+}
+
diff --git a/src/server/physical_sensor_handler.h b/src/server/physical_sensor_handler.h
new file mode 100644 (file)
index 0000000..1983dbd
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * sensord
+ *
+ * Copyright (c) 2014 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.
+ *
+ */
+
+#ifndef __PHYSICAL_SENSOR_HANDLER_H__
+#define __PHYSICAL_SENSOR_HANDLER_H__
+
+#include <sensor_types.h>
+#include <physical_sensor.h>
+#include <unordered_map>
+
+#include "sensor_handler.h"
+#include "sensor_observer.h"
+
+namespace sensor {
+
+class physical_sensor_handler : public sensor_handler {
+public:
+       physical_sensor_handler(const sensor_info &info,
+                       sensor_device *device, int hal_id,
+                       physical_sensor *sensor);
+       virtual ~physical_sensor_handler();
+
+       /* functions for sensor device(HAL) */
+       int get_hal_id(void);
+       int get_poll_fd(void);
+       int read_fd(std::vector<uint32_t> &hal_ids);
+       int on_event(const sensor_data_t *data, int32_t len, int32_t remains);
+
+       /* sensor interface */
+       const sensor_info &get_sensor_info(void);
+
+       int start(sensor_observer *ob);
+       int stop(sensor_observer *ob);
+
+       int set_interval(sensor_observer *ob, int32_t interval);
+       int set_batch_latency(sensor_observer *ob, int32_t latency);
+       int set_attribute(sensor_observer *ob, int32_t attr, int32_t value);
+       int set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len);
+       int flush(sensor_observer *ob);
+       int get_data(sensor_data_t **data, int *len);
+
+private:
+       int get_min_interval(void);
+       int get_min_batch_latency(void);
+
+       sensor_info m_info;
+       sensor_device *m_device;
+       physical_sensor *m_sensor;
+       int m_hal_id;
+
+       std::unordered_map<sensor_observer *, int> m_interval_map;
+       std::unordered_map<sensor_observer *, int> m_batch_latency_map;
+};
+
+}
+
+#endif /* __PHYSICAL_SENSOR_HANDLER_H__ */