4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include <sensor_device_base.h>
30 sensor_device_base::sensor_device_base()
34 sensor_device_base::~sensor_device_base()
38 unsigned long long sensor_device_base::get_timestamp(void)
41 clock_gettime(CLOCK_MONOTONIC, &t);
42 return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
45 unsigned long long sensor_device_base::get_timestamp(timeval *t)
52 return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec);
55 bool sensor_device_base::is_sensorhub_controlled(const string &key)
57 string key_node = string("/sys/class/sensors/ssp_sensor/") + key;
59 if (access(key_node.c_str(), F_OK) == 0)
65 bool sensor_device_base::get_node_info(const node_info_query &query, node_info &info)
71 if (!get_input_method(query.key, method, device_num)) {
72 ERR("Failed to get input method for %s", query.key.c_str());
78 if (method == IIO_METHOD) {
79 if (query.sensorhub_controlled)
80 ret = get_sensorhub_iio_node_info(query.sensorhub_interval_node_name, device_num, info);
82 ret = get_iio_node_info(query.iio_enable_node_name, device_num, info);
84 if (query.sensorhub_controlled)
85 ret = get_sensorhub_input_event_node_info(query.sensorhub_interval_node_name, device_num, info);
87 ret = get_input_event_node_info(device_num, info);
94 void sensor_device_base::show_node_info(node_info &info)
96 if (info.data_node_path.size())
97 INFO("Data node: %s", info.data_node_path.c_str());
98 if (info.enable_node_path.size())
99 INFO("Enable node: %s", info.enable_node_path.c_str());
100 if (info.interval_node_path.size())
101 INFO("Interval node: %s", info.interval_node_path.c_str());
102 if (info.buffer_enable_node_path.size())
103 INFO("Buffer enable node: %s", info.buffer_enable_node_path.c_str());
104 if (info.buffer_length_node_path.size())
105 INFO("Buffer length node: %s", info.buffer_length_node_path.c_str());
106 if (info.trigger_node_path.size())
107 INFO("Trigger node: %s", info.trigger_node_path.c_str());
110 bool sensor_device_base::get_iio_node_info(const string& enable_node_name, const string& device_num, node_info &info)
112 const string base_dir = string("/sys/bus/iio/devices/iio:device") + device_num + string("/");
114 info.data_node_path = string("/dev/iio:device") + device_num;
115 info.enable_node_path = base_dir + enable_node_name;
116 info.interval_node_path = base_dir + string("sampling_frequency");
117 info.buffer_enable_node_path = base_dir + string("buffer/enable");
118 info.buffer_length_node_path = base_dir + string("buffer/length");
119 info.trigger_node_path = base_dir + string("trigger/current_trigger");
124 bool sensor_device_base::get_sensorhub_iio_node_info(const string &interval_node_name, const string& device_num, node_info &info)
126 const string base_dir = string("/sys/bus/iio/devices/iio:device") + device_num + string("/");
127 const string hub_dir = "/sys/class/sensors/ssp_sensor/";
129 info.data_node_path = string("/dev/iio:device") + device_num;
130 info.enable_node_path = hub_dir + string("enable");
131 info.interval_node_path = hub_dir + interval_node_name;
132 info.buffer_enable_node_path = base_dir + string("buffer/enable");
133 info.buffer_length_node_path = base_dir + string("buffer/length");
137 bool sensor_device_base::get_input_event_node_info(const string& device_num, node_info &info)
142 base_dir = string("/sys/class/input/input") + device_num + string("/");
144 if (!get_event_num(base_dir, event_num))
147 info.data_node_path = string("/dev/input/event") + event_num;
149 info.enable_node_path = base_dir + string("enable");
150 info.interval_node_path = base_dir + string("poll_delay");
154 bool sensor_device_base::get_sensorhub_input_event_node_info(const string &interval_node_name, const string& device_num, node_info &info)
156 const string base_dir = "/sys/class/sensors/ssp_sensor/";
159 string input_dir = string("/sys/class/input/input") + device_num + string("/");
161 if (!get_event_num(input_dir, event_num))
164 info.data_node_path = string("/dev/input/event") + event_num;
165 info.enable_node_path = base_dir + string("enable");
166 info.interval_node_path = base_dir + interval_node_name;
170 bool sensor_device_base::set_node_value(const string &node_path, int value)
172 ofstream node(node_path, ofstream::binary);
182 bool sensor_device_base::set_node_value(const string &node_path, unsigned long long value)
184 ofstream node(node_path, ofstream::binary);
195 bool sensor_device_base::get_node_value(const string &node_path, int &value)
197 ifstream node(node_path, ifstream::binary);
207 bool sensor_device_base::set_enable_node(const string &node_path, bool sensorhub_controlled, bool enable, int enable_bit)
209 int prev_status, status;
211 if (!get_node_value(node_path, prev_status)) {
212 ERR("Failed to get node: %s", node_path.c_str());
216 int _enable_bit = sensorhub_controlled ? enable_bit : 0;
219 status = prev_status | (1 << _enable_bit);
221 status = prev_status & (~(1 << _enable_bit));
223 if (!set_node_value(node_path, status)) {
224 ERR("Failed to set node: %s", node_path.c_str());
231 bool sensor_device_base::get_event_num(const string &input_path, string &event_num)
233 const string event_prefix = "event";
235 struct dirent *dir_entry = NULL;
239 dir = opendir(input_path.c_str());
241 ERR("Failed to open dir: %s", input_path.c_str());
245 int prefix_size = event_prefix.size();
247 while (!find && (dir_entry = readdir(dir))) {
248 node_name = dir_entry->d_name;
250 if (node_name.compare(0, prefix_size, event_prefix) == 0) {
251 event_num = node_name.substr(prefix_size, node_name.size() - prefix_size);
262 bool sensor_device_base::get_input_method(const string &key, int &method, string &device_num)
264 input_method_info input_info[2] = {
265 {INPUT_EVENT_METHOD, "/sys/class/input/", "input"},
266 {IIO_METHOD, "/sys/bus/iio/devices/", "iio:device"}
269 const int input_info_len = sizeof(input_info)/sizeof(input_info[0]);
271 string name_node, name;
274 struct dirent *dir_entry = NULL;
277 for (int i = 0; i < input_info_len; ++i) {
279 prefix_size = input_info[i].prefix.size();
281 dir = opendir(input_info[i].dir_path.c_str());
283 ERR("Failed to open dir: %s", input_info[i].dir_path.c_str());
289 while (!find && (dir_entry = readdir(dir))) {
290 d_name = string(dir_entry->d_name);
292 if (d_name.compare(0, prefix_size, input_info[i].prefix) == 0) {
293 name_node = input_info[i].dir_path + d_name + string("/name");
295 ifstream infile(name_node.c_str());
302 device_num = d_name.substr(prefix_size, d_name.size() - prefix_size);
304 method = input_info[i].method;