#include <sys/stat.h>
#include <linux/input.h>
#include <sys/poll.h>
-#include "accel_sensor_device.h"
+#include <util.h>
+#include "accel.h"
#define GRAVITY 9.80665
#define G_TO_MG 1000
node_info_query query;
node_info info;
- query.sensorhub_controlled = m_sensorhub_controlled = is_sensorhub_controlled(sensorhub_interval_node_name);
+ query.sensorhub_controlled = m_sensorhub_controlled = util::is_sensorhub_controlled(sensorhub_interval_node_name);
query.sensor_type = "ACCEL";
query.key = "accelerometer_sensor";
query.iio_enable_node_name = "accel_enable";
query.sensorhub_interval_node_name = sensorhub_interval_node_name;
- if (!get_node_info(query, info)) {
+ if (!util::get_node_info(query, info)) {
ERR("Failed to get node info");
throw ENXIO;
}
- show_node_info(info);
+ util::show_node_info(info);
m_data_node = info.data_node_path;
m_enable_node = info.enable_node_path;
bool accel_sensor_device::enable(uint32_t id)
{
- set_enable_node(m_enable_node, m_sensorhub_controlled, true, SENSORHUB_ACCELEROMETER_ENABLE_BIT);
+ util::set_enable_node(m_enable_node, m_sensorhub_controlled, true, SENSORHUB_ACCELEROMETER_ENABLE_BIT);
set_interval(id, m_polling_interval);
m_fired_time = 0;
bool accel_sensor_device::disable(uint32_t id)
{
- set_enable_node(m_enable_node, m_sensorhub_controlled, false, SENSORHUB_ACCELEROMETER_ENABLE_BIT);
+ util::set_enable_node(m_enable_node, m_sensorhub_controlled, false, SENSORHUB_ACCELEROMETER_ENABLE_BIT);
INFO("Disable accelerometer sensor");
return true;
polling_interval_ns = ((unsigned long long)(val) * 1000llu * 1000llu);
- if (!set_node_value(m_interval_node, polling_interval_ns)) {
+ if (!util::set_node_value(m_interval_node, polling_interval_ns)) {
ERR("Failed to set polling resource: %s\n", m_interval_node.c_str());
return false;
}
}
} else if (accel_input.type == EV_SYN) {
syn = true;
- fired_time = sensor_device_base::get_timestamp(&accel_input.time);
+ fired_time = util::get_timestamp(&accel_input.time);
} else {
ERR("accel_input event[type = %d, code = %d] is unknown.", accel_input.type, accel_input.code);
return false;
+++ /dev/null
-/*
- * libsensord-share
- *
- * 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 _SENSOR_DEVICE_BASE_H_
-#define _SENSOR_DEVICE_BASE_H_
-#include <sys/time.h>
-#include <sensor_logs.h>
-#include <string>
-#include <sensor_hal.h>
-
-/*
-* As of Linux 3.4, there is a new EVIOCSCLOCKID ioctl to set the desired clock
-* Current kernel-headers package doesn't have it so we should define it here.
-*/
-
-#ifndef EVIOCSCLOCKID
-/* Set clockid to be used for timestamps */
-#define EVIOCSCLOCKID _IOW('E', 0xa0, int)
-#endif
-
-typedef struct {
- int method;
- std::string data_node_path;
- std::string enable_node_path;
- std::string interval_node_path;
- std::string buffer_enable_node_path;
- std::string buffer_length_node_path;
- std::string trigger_node_path;
-} node_info;
-
-typedef struct {
- bool sensorhub_controlled;
- std::string sensor_type;
- std::string key;
- std::string iio_enable_node_name;
- std::string sensorhub_interval_node_name;
-} node_info_query;
-
-enum input_method {
- IIO_METHOD = 0,
- INPUT_EVENT_METHOD = 1,
-};
-
-typedef struct {
- int method;
- std::string dir_path;
- std::string prefix;
-} input_method_info;
-
-class sensor_device_base : public sensor_device
-{
-public:
- sensor_device_base();
- virtual ~sensor_device_base();
-
-protected:
- bool set_enable_node(const std::string &node_path, bool sensorhub_controlled, bool enable, int enable_bit = 0);
-
- static unsigned long long get_timestamp(void);
- static unsigned long long get_timestamp(timeval *t);
- static bool is_sensorhub_controlled(const std::string &key);
- static bool get_node_info(const node_info_query &query, node_info &info);
- static void show_node_info(node_info &info);
- static bool set_node_value(const std::string &node_path, int value);
- static bool set_node_value(const std::string &node_path, unsigned long long value);
- static bool get_node_value(const std::string &node_path, int &value);
-private:
- static bool get_event_num(const std::string &node_path, std::string &event_num);
- static bool get_input_method(const std::string &key, int &method, std::string &device_num);
-
- static bool get_iio_node_info(const std::string& enable_node_name, const std::string& device_num, node_info &info);
- static bool get_sensorhub_iio_node_info(const std::string &interval_node_name, const std::string& device_num, node_info &info);
- static bool get_input_event_node_info(const std::string& device_num, node_info &info);
- static bool get_sensorhub_input_event_node_info(const std::string &interval_node_name, const std::string& device_num, node_info &info);
-};
-#endif /*_SENSOR_DEVICE_BASE_H_*/
*
*/
-#include <sensor_device_base.h>
#include <dirent.h>
#include <string.h>
#include <fstream>
+#include <util.h>
using std::ifstream;
using std::ofstream;
using std::fstream;
using std::string;
-sensor_device_base::sensor_device_base()
+static bool get_event_num(const string &input_path, string &event_num)
{
-}
-
-sensor_device_base::~sensor_device_base()
-{
-}
-
-unsigned long long sensor_device_base::get_timestamp(void)
-{
- struct timespec t;
- clock_gettime(CLOCK_MONOTONIC, &t);
- return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
-}
-
-unsigned long long sensor_device_base::get_timestamp(timeval *t)
-{
- if (!t) {
- ERR("t is NULL");
- return 0;
- }
-
- return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec);
-}
-
-bool sensor_device_base::is_sensorhub_controlled(const string &key)
-{
- string key_node = string("/sys/class/sensors/ssp_sensor/") + key;
-
- if (access(key_node.c_str(), F_OK) == 0)
- return true;
-
- return false;
-}
-
-bool sensor_device_base::get_node_info(const node_info_query &query, node_info &info)
-{
- bool ret = false;
- int method;
- string device_num;
+ const string event_prefix = "event";
+ DIR *dir = NULL;
+ struct dirent *dir_entry = NULL;
+ string node_name;
+ bool find = false;
- if (!get_input_method(query.key, method, device_num)) {
- ERR("Failed to get input method for %s", query.key.c_str());
+ dir = opendir(input_path.c_str());
+ if (!dir) {
+ ERR("Failed to open dir: %s", input_path.c_str());
return false;
}
- info.method = method;
+ int prefix_size = event_prefix.size();
- if (method == IIO_METHOD) {
- if (query.sensorhub_controlled)
- ret = get_sensorhub_iio_node_info(query.sensorhub_interval_node_name, device_num, info);
- else
- ret = get_iio_node_info(query.iio_enable_node_name, device_num, info);
- } else {
- if (query.sensorhub_controlled)
- ret = get_sensorhub_input_event_node_info(query.sensorhub_interval_node_name, device_num, info);
- else
- ret = get_input_event_node_info(device_num, info);
- }
+ while (!find && (dir_entry = readdir(dir))) {
+ node_name = dir_entry->d_name;
- return ret;
-}
+ if (node_name.compare(0, prefix_size, event_prefix) == 0) {
+ event_num = node_name.substr(prefix_size, node_name.size() - prefix_size);
+ find = true;
+ break;
+ }
+ }
+ closedir(dir);
-void sensor_device_base::show_node_info(node_info &info)
-{
- if (info.data_node_path.size())
- INFO("Data node: %s", info.data_node_path.c_str());
- if (info.enable_node_path.size())
- INFO("Enable node: %s", info.enable_node_path.c_str());
- if (info.interval_node_path.size())
- INFO("Interval node: %s", info.interval_node_path.c_str());
- if (info.buffer_enable_node_path.size())
- INFO("Buffer enable node: %s", info.buffer_enable_node_path.c_str());
- if (info.buffer_length_node_path.size())
- INFO("Buffer length node: %s", info.buffer_length_node_path.c_str());
- if (info.trigger_node_path.size())
- INFO("Trigger node: %s", info.trigger_node_path.c_str());
+ return find;
}
-bool sensor_device_base::get_iio_node_info(const string& enable_node_name, const string& device_num, node_info &info)
+static bool get_iio_node_info(const string& enable_node_name, const string& device_num, node_info &info)
{
const string base_dir = string("/sys/bus/iio/devices/iio:device") + device_num + string("/");
return true;
}
-bool sensor_device_base::get_sensorhub_iio_node_info(const string &interval_node_name, const string& device_num, node_info &info)
+static bool get_sensorhub_iio_node_info(const string &interval_node_name, const string& device_num, node_info &info)
{
const string base_dir = string("/sys/bus/iio/devices/iio:device") + device_num + string("/");
const string hub_dir = "/sys/class/sensors/ssp_sensor/";
return true;
}
-bool sensor_device_base::get_input_event_node_info(const string& device_num, node_info &info)
+static bool get_input_event_node_info(const string& device_num, node_info &info)
{
string base_dir;
string event_num;
return true;
}
-bool sensor_device_base::get_sensorhub_input_event_node_info(const string &interval_node_name, const string& device_num, node_info &info)
+static bool get_sensorhub_input_event_node_info(const string &interval_node_name, const string& device_num, node_info &info)
{
const string base_dir = "/sys/class/sensors/ssp_sensor/";
string event_num;
return true;
}
-bool sensor_device_base::set_node_value(const string &node_path, int value)
+static bool get_node_value(const string &node_path, int &value)
{
- ofstream node(node_path, ofstream::binary);
+ ifstream node(node_path, ifstream::binary);
if (!node)
return false;
- node << value;
+ node >> value;
return true;
}
-bool sensor_device_base::set_node_value(const string &node_path, unsigned long long value)
+static bool get_input_method(const string &key, int &method, string &device_num)
{
- ofstream node(node_path, ofstream::binary);
+ input_method_info input_info[2] = {
+ {INPUT_EVENT_METHOD, "/sys/class/input/", "input"},
+ {IIO_METHOD, "/sys/bus/iio/devices/", "iio:device"}
+ };
- if (!node)
- return false;
+ const int input_info_len = sizeof(input_info)/sizeof(input_info[0]);
+ size_t prefix_size;
+ string name_node, name;
+ string d_name;
+ DIR *dir = NULL;
+ struct dirent *dir_entry = NULL;
+ bool find = false;
- node << value;
+ for (int i = 0; i < input_info_len; ++i) {
- return true;
-}
+ prefix_size = input_info[i].prefix.size();
+ dir = opendir(input_info[i].dir_path.c_str());
+ if (!dir) {
+ ERR("Failed to open dir: %s", input_info[i].dir_path.c_str());
+ return false;
+ }
-bool sensor_device_base::get_node_value(const string &node_path, int &value)
-{
- ifstream node(node_path, ifstream::binary);
+ find = false;
- if (!node)
- return false;
+ while (!find && (dir_entry = readdir(dir))) {
+ d_name = string(dir_entry->d_name);
- node >> value;
+ if (d_name.compare(0, prefix_size, input_info[i].prefix) == 0) {
+ name_node = input_info[i].dir_path + d_name + string("/name");
- return true;
+ ifstream infile(name_node.c_str());
+ if (!infile)
+ continue;
+
+ infile >> name;
+
+ if (name == key) {
+ device_num = d_name.substr(prefix_size, d_name.size() - prefix_size);
+ find = true;
+ method = input_info[i].method;
+ break;
+ }
+ }
+ }
+
+ closedir(dir);
+
+ if (find)
+ break;
+ }
+
+ return find;
}
-bool sensor_device_base::set_enable_node(const string &node_path, bool sensorhub_controlled, bool enable, int enable_bit)
+bool util::set_enable_node(const string &node_path, bool sensorhub_controlled, bool enable, int enable_bit)
{
int prev_status, status;
return true;
}
-bool sensor_device_base::get_event_num(const string &input_path, string &event_num)
+unsigned long long util::get_timestamp(void)
{
- const string event_prefix = "event";
- DIR *dir = NULL;
- struct dirent *dir_entry = NULL;
- string node_name;
- bool find = false;
+ struct timespec t;
+ clock_gettime(CLOCK_MONOTONIC, &t);
+ return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
+}
- dir = opendir(input_path.c_str());
- if (!dir) {
- ERR("Failed to open dir: %s", input_path.c_str());
- return false;
+unsigned long long util::get_timestamp(timeval *t)
+{
+ if (!t) {
+ ERR("t is NULL");
+ return 0;
}
- int prefix_size = event_prefix.size();
-
- while (!find && (dir_entry = readdir(dir))) {
- node_name = dir_entry->d_name;
+ return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec);
+}
- if (node_name.compare(0, prefix_size, event_prefix) == 0) {
- event_num = node_name.substr(prefix_size, node_name.size() - prefix_size);
- find = true;
- break;
- }
- }
+bool util::is_sensorhub_controlled(const string &key)
+{
+ string key_node = string("/sys/class/sensors/ssp_sensor/") + key;
- closedir(dir);
+ if (access(key_node.c_str(), F_OK) == 0)
+ return true;
- return find;
+ return false;
}
-bool sensor_device_base::get_input_method(const string &key, int &method, string &device_num)
+bool util::get_node_info(const node_info_query &query, node_info &info)
{
- input_method_info input_info[2] = {
- {INPUT_EVENT_METHOD, "/sys/class/input/", "input"},
- {IIO_METHOD, "/sys/bus/iio/devices/", "iio:device"}
- };
+ bool ret = false;
+ int method;
+ string device_num;
- const int input_info_len = sizeof(input_info)/sizeof(input_info[0]);
- size_t prefix_size;
- string name_node, name;
- string d_name;
- DIR *dir = NULL;
- struct dirent *dir_entry = NULL;
- bool find = false;
+ if (!get_input_method(query.key, method, device_num)) {
+ ERR("Failed to get input method for %s", query.key.c_str());
+ return false;
+ }
- for (int i = 0; i < input_info_len; ++i) {
+ info.method = method;
- prefix_size = input_info[i].prefix.size();
+ if (method == IIO_METHOD) {
+ if (query.sensorhub_controlled)
+ ret = get_sensorhub_iio_node_info(query.sensorhub_interval_node_name, device_num, info);
+ else
+ ret = get_iio_node_info(query.iio_enable_node_name, device_num, info);
+ } else {
+ if (query.sensorhub_controlled)
+ ret = get_sensorhub_input_event_node_info(query.sensorhub_interval_node_name, device_num, info);
+ else
+ ret = get_input_event_node_info(device_num, info);
+ }
- dir = opendir(input_info[i].dir_path.c_str());
- if (!dir) {
- ERR("Failed to open dir: %s", input_info[i].dir_path.c_str());
- return false;
- }
+ return ret;
+}
- find = false;
- while (!find && (dir_entry = readdir(dir))) {
- d_name = string(dir_entry->d_name);
+void util::show_node_info(node_info &info)
+{
+ if (info.data_node_path.size())
+ INFO("Data node: %s", info.data_node_path.c_str());
+ if (info.enable_node_path.size())
+ INFO("Enable node: %s", info.enable_node_path.c_str());
+ if (info.interval_node_path.size())
+ INFO("Interval node: %s", info.interval_node_path.c_str());
+ if (info.buffer_enable_node_path.size())
+ INFO("Buffer enable node: %s", info.buffer_enable_node_path.c_str());
+ if (info.buffer_length_node_path.size())
+ INFO("Buffer length node: %s", info.buffer_length_node_path.c_str());
+ if (info.trigger_node_path.size())
+ INFO("Trigger node: %s", info.trigger_node_path.c_str());
+}
- if (d_name.compare(0, prefix_size, input_info[i].prefix) == 0) {
- name_node = input_info[i].dir_path + d_name + string("/name");
+bool util::set_node_value(const string &node_path, int value)
+{
+ ofstream node(node_path, ofstream::binary);
- ifstream infile(name_node.c_str());
- if (!infile)
- continue;
+ if (!node)
+ return false;
- infile >> name;
+ node << value;
- if (name == key) {
- device_num = d_name.substr(prefix_size, d_name.size() - prefix_size);
- find = true;
- method = input_info[i].method;
- break;
- }
- }
- }
+ return true;
+}
- closedir(dir);
+bool util::set_node_value(const string &node_path, unsigned long long value)
+{
+ ofstream node(node_path, ofstream::binary);
- if (find)
- break;
- }
+ if (!node)
+ return false;
- return find;
+ node << value;
+
+ return true;
}
+