signal_init();
- sensor_loader::get_instance().load_sensors();
+ sensor_loader::get_instance().load();
server::get_instance().run();
{
}
-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)
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()
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");
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)
_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)
_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)
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)
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)
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()
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)
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;
}
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 */
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;
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")
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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_ */
+
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;
}
#include <unordered_set>
#include <algorithm>
+#include <accel_sensor.h>
#ifdef ENABLE_AUTO_ROTATION
#include <auto_rotation_sensor.h>
#endif
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;
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;
}
}
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)
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);
#ifdef _DEBUG
#define DBG SLOGD
#else
-#define _D(...) do{} while(0)
+#define DBG(...) do{} while(0)
#endif
#define ERR SLOGE