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_logs.h>
30 static bool get_event_num(const string &input_path, string &event_num)
32 const string event_prefix = "event";
34 struct dirent *dir_entry = NULL;
38 dir = opendir(input_path.c_str());
40 ERR("Failed to open dir: %s", input_path.c_str());
44 int prefix_size = event_prefix.size();
46 while (!find && (dir_entry = readdir(dir))) {
47 node_name = dir_entry->d_name;
49 if (node_name.compare(0, prefix_size, event_prefix) == 0) {
50 event_num = node_name.substr(prefix_size, node_name.size() - prefix_size);
61 static bool get_iio_node_info(const string& enable_node_name, const string& device_num, node_info &info)
63 const string base_dir = string("/sys/bus/iio/devices/iio:device") + device_num + string("/");
65 info.data_node_path = string("/dev/iio:device") + device_num;
66 info.enable_node_path = base_dir + enable_node_name;
67 info.interval_node_path = base_dir + string("sampling_frequency");
68 info.buffer_enable_node_path = base_dir + string("buffer/enable");
69 info.buffer_length_node_path = base_dir + string("buffer/length");
70 info.trigger_node_path = base_dir + string("trigger/current_trigger");
75 static bool get_sensorhub_iio_node_info(const string &interval_node_name, const string& device_num, node_info &info)
77 const string base_dir = string("/sys/bus/iio/devices/iio:device") + device_num + string("/");
78 const string hub_dir = "/sys/class/sensors/ssp_sensor/";
80 info.data_node_path = string("/dev/iio:device") + device_num;
81 info.enable_node_path = hub_dir + string("enable");
82 info.interval_node_path = hub_dir + interval_node_name;
83 info.buffer_enable_node_path = base_dir + string("buffer/enable");
84 info.buffer_length_node_path = base_dir + string("buffer/length");
88 static bool get_input_event_node_info(const string& device_num, node_info &info)
93 base_dir = string("/sys/class/input/input") + device_num + string("/");
95 if (!get_event_num(base_dir, event_num))
98 info.data_node_path = string("/dev/input/event") + event_num;
100 info.enable_node_path = base_dir + string("enable");
101 info.interval_node_path = base_dir + string("poll_delay");
105 static bool get_sensorhub_input_event_node_info(const string &interval_node_name, const string& device_num, node_info &info)
107 const string base_dir = "/sys/class/sensors/ssp_sensor/";
110 string input_dir = string("/sys/class/input/input") + device_num + string("/");
112 if (!get_event_num(input_dir, event_num))
115 info.data_node_path = string("/dev/input/event") + event_num;
116 info.enable_node_path = base_dir + string("enable");
117 info.interval_node_path = base_dir + interval_node_name;
121 static bool get_node_value(const string &node_path, int &value)
123 ifstream node(node_path, ifstream::binary);
133 static bool get_input_method(const string &key, int &method, string &device_num)
135 input_method_info input_info[2] = {
136 {INPUT_EVENT_METHOD, "/sys/class/input/", "input"},
137 {IIO_METHOD, "/sys/bus/iio/devices/", "iio:device"}
140 const int input_info_len = sizeof(input_info)/sizeof(input_info[0]);
142 string name_node, name;
145 struct dirent *dir_entry = NULL;
148 for (int i = 0; i < input_info_len; ++i) {
150 prefix_size = input_info[i].prefix.size();
152 dir = opendir(input_info[i].dir_path.c_str());
154 ERR("Failed to open dir: %s", input_info[i].dir_path.c_str());
160 while (!find && (dir_entry = readdir(dir))) {
161 d_name = string(dir_entry->d_name);
163 if (d_name.compare(0, prefix_size, input_info[i].prefix) == 0) {
164 name_node = input_info[i].dir_path + d_name + string("/name");
166 ifstream infile(name_node.c_str());
173 device_num = d_name.substr(prefix_size, d_name.size() - prefix_size);
175 method = input_info[i].method;
190 bool util::set_enable_node(const string &node_path, bool sensorhub_controlled, bool enable, int enable_bit)
192 int prev_status, status;
194 if (!get_node_value(node_path, prev_status)) {
195 ERR("Failed to get node: %s", node_path.c_str());
199 int _enable_bit = sensorhub_controlled ? enable_bit : 0;
202 status = prev_status | (1 << _enable_bit);
204 status = prev_status & (~(1 << _enable_bit));
206 if (!set_node_value(node_path, status)) {
207 ERR("Failed to set node: %s", node_path.c_str());
214 unsigned long long util::get_timestamp(void)
217 clock_gettime(CLOCK_MONOTONIC, &t);
218 return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
221 unsigned long long util::get_timestamp(timeval *t)
228 return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec);
231 bool util::is_sensorhub_controlled(const string &key)
233 string key_node = string("/sys/class/sensors/ssp_sensor/") + key;
235 if (access(key_node.c_str(), F_OK) == 0)
241 bool util::get_node_info(const node_info_query &query, node_info &info)
247 if (!get_input_method(query.key, method, device_num)) {
248 ERR("Failed to get input method for %s", query.key.c_str());
252 info.method = method;
254 if (method == IIO_METHOD) {
255 if (query.sensorhub_controlled)
256 ret = get_sensorhub_iio_node_info(query.sensorhub_interval_node_name, device_num, info);
258 ret = get_iio_node_info(query.iio_enable_node_name, device_num, info);
260 if (query.sensorhub_controlled)
261 ret = get_sensorhub_input_event_node_info(query.sensorhub_interval_node_name, device_num, info);
263 ret = get_input_event_node_info(device_num, info);
270 void util::show_node_info(node_info &info)
272 if (info.data_node_path.size())
273 INFO("Data node: %s", info.data_node_path.c_str());
274 if (info.enable_node_path.size())
275 INFO("Enable node: %s", info.enable_node_path.c_str());
276 if (info.interval_node_path.size())
277 INFO("Interval node: %s", info.interval_node_path.c_str());
278 if (info.buffer_enable_node_path.size())
279 INFO("Buffer enable node: %s", info.buffer_enable_node_path.c_str());
280 if (info.buffer_length_node_path.size())
281 INFO("Buffer length node: %s", info.buffer_length_node_path.c_str());
282 if (info.trigger_node_path.size())
283 INFO("Trigger node: %s", info.trigger_node_path.c_str());
286 bool util::set_node_value(const string &node_path, int value)
288 ofstream node(node_path, ofstream::binary);
298 bool util::set_node_value(const string &node_path, unsigned long long value)
300 ofstream node(node_path, ofstream::binary);