Merge "sensord: add the sensord plugin logic for flexibility" into devel/tizen_3.0
authorMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 15 Feb 2016 08:58:45 +0000 (00:58 -0800)
committerGerrit Code Review <gerrit@review.vlan103.tizen.org>
Mon, 15 Feb 2016 08:58:45 +0000 (00:58 -0800)
src/server/main.cpp
src/server/physical_sensor.cpp
src/server/physical_sensor.h
src/server/plugins/CMakeLists.txt
src/server/plugins/accel/accel_sensor.cpp [new file with mode: 0644]
src/server/plugins/accel/accel_sensor.h [new file with mode: 0644]
src/server/plugins/auto_rotation/auto_rotation_sensor.cpp
src/server/sensor_loader.cpp
src/server/sensor_loader.h
src/shared/sensor_logs.h

index bcf0bb2..6bda15a 100644 (file)
@@ -56,7 +56,7 @@ int main(int argc, char *argv[])
 
        signal_init();
 
-       sensor_loader::get_instance().load_sensors();
+       sensor_loader::get_instance().load();
 
        server::get_instance().run();
 
index 7d17901..b230e1c 100644 (file)
@@ -33,20 +33,9 @@ physical_sensor::~physical_sensor()
 {
 }
 
-void physical_sensor::set_sensor_handle(sensor_handle_t handle)
-{
-       m_handle.id = handle.id;
-       m_handle.name = handle.name;
-       m_handle.type = handle.type;
-       m_handle.event_type = handle.event_type;
-       m_handle.model_name = handle.model_name;
-       m_handle.vendor = handle.vendor;
-       m_handle.min_range = handle.min_range;
-       m_handle.max_range = handle.max_range;
-       m_handle.resolution = handle.resolution;
-       m_handle.min_interval = handle.min_interval;
-       m_handle.max_batch_count = handle.max_batch_count;
-       m_handle.wakeup_supported = handle.wakeup_supported;
+void physical_sensor::set_sensor_handle(const sensor_handle_t *handle)
+{
+       m_handle = handle;
 }
 
 void physical_sensor::set_sensor_device(sensor_device *device)
@@ -56,25 +45,25 @@ void physical_sensor::set_sensor_device(sensor_device *device)
 
 sensor_type_t physical_sensor::get_type(void)
 {
-       return static_cast<sensor_type_t>(m_handle.type);
+       return static_cast<sensor_type_t>(m_handle->type);
 }
 
 unsigned int physical_sensor::get_event_type(void)
 {
-       return m_handle.event_type;
+       return m_handle->event_type;
 }
 
 const char* physical_sensor::get_name(void)
 {
-       if (!m_handle.name)
+       if (!m_handle->name)
                return UNKNOWN_NAME;
 
-       return m_handle.name;
+       return m_handle->name;
 }
 
 uint32_t physical_sensor::get_hal_id(void)
 {
-       return m_handle.id;
+       return m_handle->id;
 }
 
 int physical_sensor::get_poll_fd()
@@ -112,7 +101,7 @@ int physical_sensor::get_data(sensor_data_t **data, int *length)
                return -1;
 
        int remains = 0;
-       remains = m_sensor_device->get_data(m_handle.id, data, length);
+       remains = m_sensor_device->get_data(m_handle->id, data, length);
 
        if (*length < 0) {
                _E("Failed to get sensor event");
@@ -129,7 +118,7 @@ bool physical_sensor::flush(void)
        if (!m_sensor_device)
                return false;
 
-       return m_sensor_device->flush(m_handle.id);
+       return m_sensor_device->flush(m_handle->id);
 }
 
 bool physical_sensor::set_interval(unsigned long interval)
@@ -141,7 +130,7 @@ bool physical_sensor::set_interval(unsigned long interval)
 
        _I("Polling interval is set to %dms", interval);
 
-       return m_sensor_device->set_interval(m_handle.id, interval);
+       return m_sensor_device->set_interval(m_handle->id, interval);
 }
 
 bool physical_sensor::set_batch_latency(unsigned long latency)
@@ -153,7 +142,7 @@ bool physical_sensor::set_batch_latency(unsigned long latency)
 
        _I("Polling interval is set to %dms", latency);
 
-       return m_sensor_device->set_batch_latency(m_handle.id, latency);
+       return m_sensor_device->set_batch_latency(m_handle->id, latency);
 }
 
 int physical_sensor::set_attribute(int32_t attribute, int32_t value)
@@ -163,7 +152,7 @@ int physical_sensor::set_attribute(int32_t attribute, int32_t value)
        if (!m_sensor_device)
                return false;
 
-       return m_sensor_device->set_attribute_int(m_handle.id, attribute, value);
+       return m_sensor_device->set_attribute_int(m_handle->id, attribute, value);
 }
 
 int physical_sensor::set_attribute(int32_t attribute, char *value, int value_len)
@@ -173,7 +162,7 @@ int physical_sensor::set_attribute(int32_t attribute, char *value, int value_len
        if (!m_sensor_device)
                return false;
 
-       return m_sensor_device->set_attribute_str(m_handle.id, attribute, value, value_len);
+       return m_sensor_device->set_attribute_str(m_handle->id, attribute, value, value_len);
 }
 
 bool physical_sensor::set_wakeup(int wakeup)
@@ -188,7 +177,7 @@ bool physical_sensor::on_start()
        if (!m_sensor_device)
                return false;
 
-       return m_sensor_device->enable(m_handle.id);
+       return m_sensor_device->enable(m_handle->id);
 }
 
 bool physical_sensor::on_stop()
@@ -198,7 +187,7 @@ bool physical_sensor::on_stop()
        if (!m_sensor_device)
                return false;
 
-       return m_sensor_device->disable(m_handle.id);
+       return m_sensor_device->disable(m_handle->id);
 }
 
 bool physical_sensor::get_sensor_info(sensor_info &info)
@@ -206,16 +195,16 @@ bool physical_sensor::get_sensor_info(sensor_info &info)
        info.set_type(get_type());
        info.set_id(get_id());
        info.set_privilege(SENSOR_PRIVILEGE_PUBLIC); // FIXME
-       info.set_name(m_handle.model_name);
-       info.set_vendor(m_handle.vendor);
-       info.set_min_range(m_handle.min_range);
-       info.set_max_range(m_handle.max_range);
-       info.set_resolution(m_handle.resolution);
-       info.set_min_interval(m_handle.min_interval);
+       info.set_name(m_handle->model_name);
+       info.set_vendor(m_handle->vendor);
+       info.set_min_range(m_handle->min_range);
+       info.set_max_range(m_handle->max_range);
+       info.set_resolution(m_handle->resolution);
+       info.set_min_interval(m_handle->min_interval);
        info.set_fifo_count(0); // FIXME
-       info.set_max_batch_count(m_handle.max_batch_count);
+       info.set_max_batch_count(m_handle->max_batch_count);
        info.set_supported_event(get_event_type());
-       info.set_wakeup_supported(m_handle.wakeup_supported);
+       info.set_wakeup_supported(m_handle->wakeup_supported);
 
        return true;
 }
index 32cfa38..7722929 100644 (file)
@@ -29,7 +29,7 @@ public:
        virtual ~physical_sensor();
 
        /* setting module */
-       void set_sensor_handle(sensor_handle_t handle);
+       void set_sensor_handle(const sensor_handle_t *handle);
        void set_sensor_device(sensor_device *device);
 
        /* module info */
@@ -46,7 +46,7 @@ public:
 private:
        static cmutex m_mutex;
 
-       sensor_handle_t m_handle;
+       const sensor_handle_t *m_handle;
        sensor_device *m_sensor_device;
        uint32_t hal_id;
 
index db69cf0..432e427 100644 (file)
@@ -16,6 +16,9 @@ SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
 
 include_directories(${CMAKE_CURRENT_SOURCE_DIR})
 
+SET(DIR_INCLUDE "${DIR_INCLUDE} ${CMAKE_CURRENT_SOURCE_DIR}/accel")
+list (APPEND PLUGIN_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/accel/accel_sensor.cpp")
+
 IF("${AUTO_ROTATION}" STREQUAL "ON")
 list (APPEND DIR_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/auto_rotation")
 list (APPEND PLUGIN_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/auto_rotation/auto_rotation_alg.cpp")
diff --git a/src/server/plugins/accel/accel_sensor.cpp b/src/server/plugins/accel/accel_sensor.cpp
new file mode 100644 (file)
index 0000000..0c71bc8
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * sensord
+ *
+ * Copyright (c) 2016 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 <sensor_common.h>
+#include <sensor_logs.h>
+#include <accel_sensor.h>
+
+accel_sensor::accel_sensor()
+{
+       _E("accel_sensor is created : 0x%x", this);
+}
+
+accel_sensor::~accel_sensor()
+{
+
+}
+
+sensor_type_t accel_sensor::get_type(void)
+{
+       return ACCELEROMETER_SENSOR;
+}
diff --git a/src/server/plugins/accel/accel_sensor.h b/src/server/plugins/accel/accel_sensor.h
new file mode 100644 (file)
index 0000000..309f91b
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * sensord
+ *
+ * Copyright (c) 2016 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 _ACCEL_SENSOR_H_
+#define _ACCEL_SENSOR_H_
+
+#include <physical_sensor.h>
+
+class accel_sensor : public physical_sensor {
+public:
+       accel_sensor();
+       ~accel_sensor();
+
+       sensor_type_t get_type(void);
+};
+
+#endif /* _ACCEL_SENSOR_H_ */
+
index e819c2b..68e4b80 100644 (file)
@@ -186,7 +186,7 @@ void auto_rotation_sensor::synthesize(const sensor_event_t& event)
 
        push(rotation_event);
 
-       _D("Rotation: %d, ACC[0]: %f, ACC[1]: %f, ACC[2]: %f", rotation, event.data.values[0], event.data.values[1], event.data.values[2]);
+       _D("Rotation: %d, ACC[0]: %f, ACC[1]: %f, ACC[2]: %f", rotation, event.data->values[0], event.data->values[1], event.data->values[2]);
        return;
 }
 
index 7a90ffb..a2bee31 100644 (file)
@@ -29,6 +29,7 @@
 #include <unordered_set>
 #include <algorithm>
 
+#include <accel_sensor.h>
 #ifdef ENABLE_AUTO_ROTATION
 #include <auto_rotation_sensor.h>
 #endif
@@ -50,15 +51,47 @@ sensor_loader& sensor_loader::get_instance()
        return inst;
 }
 
-bool sensor_loader::load_devices(const string &path, vector<sensor_device_t> &devices, void* &handle)
+bool sensor_loader::load(void)
+{
+       std::vector<string> device_plugin_paths;
+       std::vector<string> unique_device_plugin_paths;
+
+       get_paths_from_dir(string(DEVICE_PLUGINS_DIR_PATH), device_plugin_paths);
+
+       std::unordered_set<string> s;
+       auto unique = [&s](vector<string> &paths, const string &path) {
+               if (s.insert(path).second)
+                       paths.push_back(path);
+       };
+
+       for_each(device_plugin_paths.begin(), device_plugin_paths.end(),
+               [&](const string &path) {
+                       unique(unique_device_plugin_paths, path);
+               }
+       );
+
+       for_each(unique_device_plugin_paths.begin(), unique_device_plugin_paths.end(),
+               [&](const string &path) {
+                       void *handle;
+                       load_sensor_devices(path, handle);
+               }
+       );
+
+       create_sensors();
+       show_sensor_info();
+
+       return true;
+}
+
+bool sensor_loader::load_sensor_devices(const string &path, void* &handle)
 {
-       int size;
        sensor_device_t *_devices = NULL;
+       sensor_device *device;
+       const sensor_handle_t *handles;
 
        _I("load device: [%s]", path.c_str());
 
        void *_handle = dlopen(path.c_str(), RTLD_NOW);
-
        if (!_handle) {
                _E("Failed to dlopen(%s), dlerror : %s", path.c_str(), dlerror());
                return false;
@@ -67,129 +100,95 @@ bool sensor_loader::load_devices(const string &path, vector<sensor_device_t> &de
        dlerror();
 
        create_t create_devices = (create_t) dlsym(_handle, "create");
-
        if (!create_devices) {
                _E("Failed to find symbols in %s", path.c_str());
                dlclose(_handle);
                return false;
        }
 
-       size = create_devices(&_devices);
-
+       int device_size = create_devices(&_devices);
        if (!_devices) {
                _E("Failed to create devices, path is %s\n", path.c_str());
                dlclose(_handle);
                return false;
        }
 
-       devices.clear();
+       for (int i = 0; i < device_size; ++i) {
+               device = static_cast<sensor_device *>(_devices[i]);
 
-       for (int i = 0; i < size; ++i)
-               devices.push_back(_devices[i]);
+               int handle_size = device->get_sensors(&handles);
+               for (int j = 0; j < handle_size; ++j)
+                       m_devices[&handles[j]] = device;
+       }
 
        handle = _handle;
 
        delete _devices;
-
        return true;
 }
 
-physical_sensor* sensor_loader::create_sensor(sensor_handle_t handle, sensor_device *device)
+void sensor_loader::create_sensors(void)
 {
-       int32_t index;
-       physical_sensor *sensor;
-
-       index = (int32_t) (m_sensors.count((sensor_type_t)handle.type));
-
-       sensor = new(std::nothrow) physical_sensor();
-       if (!sensor) {
-               _E("Memory allocation failed[%s]", handle.name);
-               return NULL;
-       }
-
-       sensor->set_id((int64_t)handle.type << SENSOR_TYPE_SHIFT | index);
-       sensor->set_sensor_handle(handle);
-       sensor->set_sensor_device(device);
+       create_physical_sensors<accel_sensor>(ACCELEROMETER_SENSOR);
+       create_physical_sensors<physical_sensor>(UNKNOWN_SENSOR);
 
-       return sensor;
+       create_virtual_sensors<auto_rotation_sensor>("Auto Rotation");
 }
 
-bool sensor_loader::load_physical_sensors(std::vector<sensor_device_t> &devices)
+template<typename _sensor>
+void sensor_loader::create_physical_sensors(sensor_type_t type)
 {
-       int size;
-       sensor_device *device;
-       const sensor_handle_t *handles;
+       int32_t index;
+       const sensor_handle_t *handle;
        physical_sensor *sensor;
+       sensor_device *device;
 
-       for (void *device_ptr : devices) {
-               device = static_cast<sensor_device *>(device_ptr);
+       sensor_device_map_t::iterator it = m_devices.begin();
 
-               size = device->get_sensors(&handles);
+       for (sensor_device_map_t::iterator it = m_devices.begin(); it != m_devices.end(); ++it) {
+               handle = it->first;
+               device = it->second;
+               if (m_devices[handle] == NULL)
+                       continue;
 
-               for (int i = 0; i < size; ++i) {
-                       sensor = create_sensor(handles[i], device);
-                       if (!sensor)
+               if (type != UNKNOWN_SENSOR) {
+                       if (type != (sensor_type_t)(handle->type))
                                continue;
-
-                       std::shared_ptr<sensor_base> sensor_ptr(sensor);
-                       m_sensors.insert(std::make_pair((sensor_type_t)(handles[i].type), sensor_ptr));
-
-                       _I("inserted [%s] sensor", sensor->get_name());
                }
-       }
-
-       return true;
-}
-
-bool sensor_loader::load_sensors(void)
-{
-       vector<string> device_plugin_paths;
-       vector<string> unique_device_plugin_paths;
 
-       get_paths_from_dir(string(DEVICE_PLUGINS_DIR_PATH), device_plugin_paths);
+               sensor = reinterpret_cast<physical_sensor *>(create_sensor<_sensor>());
 
-       std::unordered_set<string> s;
-       auto unique = [&s](vector<string> &paths, const string &path) {
-               if (s.insert(path).second)
-                       paths.push_back(path);
-       };
-
-       for_each(device_plugin_paths.begin(), device_plugin_paths.end(),
-               [&](const string &path) {
-                       unique(unique_device_plugin_paths, path);
+               if (!sensor) {
+                       _E("Memory allocation failed[%s]", handle->name);
+                       return;
                }
-       );
 
-       for_each(unique_device_plugin_paths.begin(), unique_device_plugin_paths.end(),
-               [&](const string &path) {
-                       void *handle;
-                       std::vector<sensor_device_t> devices;
+               index = (int32_t) (m_sensors.count(type));
 
-                       load_devices(path, devices, handle);
-                       load_physical_sensors(devices);
-               }
-       );
+               sensor->set_id(((int64_t)handle->type << SENSOR_TYPE_SHIFT) | index);
+               sensor->set_sensor_handle(handle);
+               sensor->set_sensor_device(device);
 
-       load_virtual_sensors();
+               std::shared_ptr<sensor_base> sensor_ptr(sensor);
+               m_sensors.insert(std::make_pair(type, sensor_ptr));
 
-       show_sensor_info();
-       return true;
+               _I("created [%s] sensor", sensor->get_name());
+
+               m_devices[handle] = NULL;
+       }
+       return;
 }
 
 template <typename _sensor>
-void sensor_loader::load_virtual_sensor(const char *name)
+void sensor_loader::create_virtual_sensors(const char *name)
 {
+       int32_t index;
        sensor_type_t type;
-       int16_t index;
-       virtual_sensor *instance = NULL;
+       virtual_sensor *instance;
 
-       try {
-               instance = new _sensor;
-       } catch (std::exception &e) {
-               _E("Failed to create %s sensor, exception: %s", name, e.what());
-               return;
-       } catch (int err) {
-               _E("Failed to create %s sensor err: %d, cause: %s", name, err, strerror(err));
+       instance = reinterpret_cast<virtual_sensor *>(create_sensor<_sensor>());
+       if (!instance) {
+               _E("Memory allocation failed[%s]", name);
                return;
        }
 
@@ -200,18 +199,32 @@ void sensor_loader::load_virtual_sensor(const char *name)
        }
 
        std::shared_ptr<sensor_base> sensor(instance);
-
        type = sensor->get_type();
-       index = (int16_t)(m_sensors.count(type));
+       index = (int32_t)(m_sensors.count(type));
 
        sensor->set_id((int64_t)type << SENSOR_TYPE_SHIFT | index);
 
        m_sensors.insert(std::make_pair(type, sensor));
+
+       _I("created [%s] sensor", sensor->get_name());
 }
 
-void sensor_loader::load_virtual_sensors(void)
+template <typename _sensor>
+sensor_base* sensor_loader::create_sensor(void)
 {
-       load_virtual_sensor<auto_rotation_sensor>("Auto Rotation");
+       sensor_base *instance = NULL;
+
+       try {
+               instance = new _sensor;
+       } catch (std::exception &e) {
+               _E("Failed to create sensor, exception: %s", e.what());
+               return NULL;
+       } catch (int err) {
+               _E("Failed to create sensor err: %d, cause: %s", err, strerror(err));
+               return NULL;
+       }
+
+       return instance;
 }
 
 void sensor_loader::show_sensor_info(void)
index 9a6a18d..d10d216 100644 (file)
 class sensor_base;
 
 typedef std::multimap<sensor_type_t, std::shared_ptr<sensor_base>> sensor_map_t;
+typedef std::map<const sensor_handle_t *, sensor_device *> sensor_device_map_t;
 
 class sensor_loader {
 private:
        sensor_loader();
 
-       bool load_devices(const std::string &path, std::vector<sensor_device_t> &devices, void* &handle);
+       bool load_sensor_devices(const std::string &path, void* &handle);
 
-       physical_sensor* create_sensor(sensor_handle_t handle, sensor_device *device);
-       bool load_physical_sensors(std::vector<sensor_device_t> &devices);
-
-       template <typename _sensor> void load_virtual_sensor(const char *name);
-       void load_virtual_sensors(void);
+       void create_sensors(void);
+       template <typename _sensor> void create_physical_sensors(sensor_type_t type);
+       template <typename _sensor> void create_virtual_sensors(const char *name);
+       template <typename _sensor> sensor_base* create_sensor(void);
 
        void show_sensor_info(void);
        bool get_paths_from_dir(const std::string &dir_path, std::vector<std::string> &plugin_paths);
 
        sensor_map_t m_sensors;
+       sensor_device_map_t m_devices;
 public:
        static sensor_loader& get_instance();
-       bool load_sensors(void);
+       bool load(void);
 
        sensor_base* get_sensor(sensor_type_t type);
        sensor_base* get_sensor(sensor_id_t id);
index 2b78532..c414264 100644 (file)
@@ -36,7 +36,7 @@
 #ifdef _DEBUG
 #define DBG SLOGD
 #else
-#define _D(...) do{} while(0)
+#define DBG(...) do{} while(0)
 #endif
 
 #define ERR SLOGE