2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include <sensor_log.h>
30 #define PREFIX_EVENT "event"
32 static bool get_event_num(const string &input_path, string &event_num)
34 const string event_prefix = PREFIX_EVENT;
36 struct dirent dir_entry;
37 struct dirent *result = NULL;
38 std::string node_name;
42 dir = opendir(input_path.c_str());
44 ERR("Failed to open dir: %s", input_path.c_str());
48 int prefix_size = event_prefix.size();
51 error = readdir_r(dir, &dir_entry, &result);
59 node_name = std::string(dir_entry.d_name);
61 if (node_name.compare(0, prefix_size, event_prefix) != 0)
64 event_num = node_name.substr(prefix_size, node_name.size() - prefix_size);
74 static bool get_iio_node_info(const string& enable_node_name, const string& device_num, node_info &info)
76 const string base_dir = string("/sys/bus/iio/devices/iio:device") + device_num + string("/");
78 info.data_node_path = string("/dev/iio:device") + device_num;
79 info.enable_node_path = base_dir + enable_node_name;
80 info.interval_node_path = base_dir + string("sampling_frequency");
81 info.buffer_enable_node_path = base_dir + string("buffer/enable");
82 info.buffer_length_node_path = base_dir + string("buffer/length");
83 info.trigger_node_path = base_dir + string("trigger/current_trigger");
88 static bool get_sensorhub_iio_node_info(const string &interval_node_name, const string& device_num, node_info &info)
90 const string base_dir = string("/sys/bus/iio/devices/iio:device") + device_num + string("/");
91 const string hub_dir = "/sys/class/sensors/ssp_sensor/";
93 info.data_node_path = string("/dev/iio:device") + device_num;
94 info.enable_node_path = hub_dir + string("enable");
95 info.interval_node_path = hub_dir + interval_node_name;
96 info.buffer_enable_node_path = base_dir + string("buffer/enable");
97 info.buffer_length_node_path = base_dir + string("buffer/length");
101 static bool get_input_event_node_info(const string& device_num, node_info &info)
106 base_dir = string("/sys/class/input/input") + device_num + string("/");
108 if (!get_event_num(base_dir, event_num))
111 info.data_node_path = string("/dev/input/event") + event_num;
113 info.enable_node_path = base_dir + string("enable");
114 info.interval_node_path = base_dir + string("poll_delay");
118 static bool get_sensorhub_input_event_node_info(const string &interval_node_name, const string& device_num, node_info &info)
120 const string base_dir = "/sys/class/sensors/ssp_sensor/";
123 string input_dir = string("/sys/class/input/input") + device_num + string("/");
125 if (!get_event_num(input_dir, event_num))
128 info.data_node_path = string("/dev/input/event") + event_num;
129 info.enable_node_path = base_dir + string("enable");
130 info.interval_node_path = base_dir + interval_node_name;
134 static bool get_node_value(const string &node_path, int &value)
136 ifstream node(node_path, ifstream::binary);
146 static bool get_input_method(const string &key, int &method, string &device_num)
148 input_method_info input_info[2] = {
149 {INPUT_EVENT_METHOD, "/sys/class/input/", "input"},
150 {IIO_METHOD, "/sys/bus/iio/devices/", "iio:device"}
153 const int input_info_len = sizeof(input_info)/sizeof(input_info[0]);
155 std::string name_node, name;
158 struct dirent dir_entry;
159 struct dirent *result = NULL;
163 for (int i = 0; i < input_info_len; ++i) {
165 prefix_size = input_info[i].prefix.size();
167 dir = opendir(input_info[i].dir_path.c_str());
169 ERR("Failed to open dir: %s", input_info[i].dir_path.c_str());
176 error = readdir_r(dir, &dir_entry, &result);
184 d_name = std::string(dir_entry.d_name);
186 if (d_name.compare(0, prefix_size, input_info[i].prefix) != 0)
189 name_node = input_info[i].dir_path + d_name + string("/name");
191 ifstream infile(name_node.c_str());
200 device_num = d_name.substr(prefix_size, d_name.size() - prefix_size);
202 method = input_info[i].method;
215 bool util::set_monotonic_clock(int fd)
218 int clockId = CLOCK_MONOTONIC;
219 if (ioctl(fd, EVIOCSCLOCKID, &clockId) != 0) {
220 _E("Fail to set monotonic timestamp for fd[%d]", fd);
227 bool util::set_enable_node(const string &node_path, bool sensorhub_controlled, bool enable, int enable_bit)
229 int prev_status, status;
231 if (!get_node_value(node_path, prev_status)) {
232 ERR("Failed to get node: %s", node_path.c_str());
236 int _enable_bit = sensorhub_controlled ? enable_bit : 0;
239 status = prev_status | (1 << _enable_bit);
241 status = prev_status & (~(1 << _enable_bit));
243 if (!set_node_value(node_path, status)) {
244 ERR("Failed to set node: %s", node_path.c_str());
251 unsigned long long util::get_timestamp(void)
254 clock_gettime(CLOCK_MONOTONIC, &t);
255 return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
258 unsigned long long util::get_timestamp(timeval *t)
265 return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec);
268 bool util::is_sensorhub_controlled(const string &key)
270 string key_node = string("/sys/class/sensors/ssp_sensor/") + key;
272 if (access(key_node.c_str(), F_OK) == 0)
278 bool util::get_node_info(const node_info_query &query, node_info &info)
283 if (!get_input_method(query.key, method, device_num)) {
284 ERR("Failed to get input method for %s", query.key.c_str());
288 info.method = method;
290 if (method == IIO_METHOD) {
291 if (query.sensorhub_controlled)
292 return get_sensorhub_iio_node_info(query.sensorhub_interval_node_name, device_num, info);
294 return get_iio_node_info(query.iio_enable_node_name, device_num, info);
296 if (query.sensorhub_controlled)
297 return get_sensorhub_input_event_node_info(query.sensorhub_interval_node_name, device_num, info);
299 return get_input_event_node_info(device_num, info);
303 void util::show_node_info(node_info &info)
305 if (info.data_node_path.size())
306 INFO("Data node: %s", info.data_node_path.c_str());
307 if (info.enable_node_path.size())
308 INFO("Enable node: %s", info.enable_node_path.c_str());
309 if (info.interval_node_path.size())
310 INFO("Interval node: %s", info.interval_node_path.c_str());
311 if (info.buffer_enable_node_path.size())
312 INFO("Buffer enable node: %s", info.buffer_enable_node_path.c_str());
313 if (info.buffer_length_node_path.size())
314 INFO("Buffer length node: %s", info.buffer_length_node_path.c_str());
315 if (info.trigger_node_path.size())
316 INFO("Trigger node: %s", info.trigger_node_path.c_str());
319 bool util::set_node_value(const string &node_path, int value)
321 ofstream node(node_path, ofstream::binary);
331 bool util::set_node_value(const string &node_path, unsigned long long value)
333 ofstream node(node_path, ofstream::binary);