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_hal.h>
24 #include <csensor_config.h>
29 cmutex sensor_hal::m_shared_mutex;
31 sensor_hal::sensor_hal()
35 sensor_hal::~sensor_hal()
39 bool sensor_hal::init(void *data)
44 bool sensor_hal::set_interval(unsigned long val)
49 long sensor_hal::set_command(unsigned int cmd, long val)
54 int sensor_hal::send_sensorhub_data(const char* data, int data_len)
59 int sensor_hal::get_sensor_data(sensor_data_t &data)
64 int sensor_hal::get_sensor_data(sensorhub_data_t &data)
69 unsigned long long sensor_hal::get_timestamp(void)
72 clock_gettime(CLOCK_MONOTONIC, &t);
73 return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
76 unsigned long long sensor_hal::get_timestamp(timeval *t)
83 return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec);
86 bool sensor_hal::is_sensorhub_controlled(const string &key)
88 string key_node = string("/sys/class/sensors/ssp_sensor/") + key;
90 if (access(key_node.c_str(), F_OK) == 0)
96 bool sensor_hal::get_node_info(const node_info_query &query, node_info &info)
102 if (!get_input_method(query.key, method, device_num)) {
103 ERR("Failed to get input method for %s", query.key.c_str());
107 info.method = method;
109 if (method == IIO_METHOD) {
110 if (query.sensorhub_controlled)
111 ret = get_sensorhub_iio_node_info(query.sensorhub_interval_node_name, device_num, info);
113 ret = get_iio_node_info(query.iio_enable_node_name, device_num, info);
115 if (query.sensorhub_controlled)
116 ret = get_sensorhub_input_event_node_info(query.sensorhub_interval_node_name, device_num, info);
118 ret = get_input_event_node_info(device_num, info);
125 void sensor_hal::show_node_info(node_info &info)
127 if (info.data_node_path.size())
128 INFO("Data node: %s", info.data_node_path.c_str());
129 if (info.enable_node_path.size())
130 INFO("Enable node: %s", info.enable_node_path.c_str());
131 if (info.interval_node_path.size())
132 INFO("Interval node: %s", info.interval_node_path.c_str());
133 if (info.buffer_enable_node_path.size())
134 INFO("Buffer enable node: %s", info.buffer_enable_node_path.c_str());
135 if (info.buffer_length_node_path.size())
136 INFO("Buffer length node: %s", info.buffer_length_node_path.c_str());
137 if (info.trigger_node_path.size())
138 INFO("Trigger node: %s", info.trigger_node_path.c_str());
141 bool sensor_hal::get_iio_node_info(const string& enable_node_name, const string& device_num, node_info &info)
143 const string base_dir = string("/sys/bus/iio/devices/iio:device") + device_num + string("/");
145 info.data_node_path = string("/dev/iio:device") + device_num;
146 info.enable_node_path = base_dir + enable_node_name;
147 info.interval_node_path = base_dir + string("sampling_frequency");
148 info.buffer_enable_node_path = base_dir + string("buffer/enable");
149 info.buffer_length_node_path = base_dir + string("buffer/length");
150 info.trigger_node_path = base_dir + string("trigger/current_trigger");
155 bool sensor_hal::get_sensorhub_iio_node_info(const string &interval_node_name, const string& device_num, node_info &info)
157 const string base_dir = string("/sys/bus/iio/devices/iio:device") + device_num + string("/");
158 const string hub_dir = "/sys/class/sensors/ssp_sensor/";
160 info.data_node_path = string("/dev/iio:device") + device_num;
161 info.enable_node_path = hub_dir + string("enable");
162 info.interval_node_path = hub_dir + interval_node_name;
163 info.buffer_enable_node_path = base_dir + string("buffer/enable");
164 info.buffer_length_node_path = base_dir + string("buffer/length");
168 bool sensor_hal::get_input_event_node_info(const string& device_num, node_info &info)
173 base_dir = string("/sys/class/input/input") + device_num + string("/");
175 if (!get_event_num(base_dir, event_num))
178 info.data_node_path = string("/dev/input/event") + event_num;
180 info.enable_node_path = base_dir + string("enable");
181 info.interval_node_path = base_dir + string("poll_delay");
185 bool sensor_hal::get_sensorhub_input_event_node_info(const string &interval_node_name, const string& device_num, node_info &info)
187 const string base_dir = "/sys/class/sensors/ssp_sensor/";
190 string input_dir = string("/sys/class/input/input") + device_num + string("/");
192 if (!get_event_num(input_dir, event_num))
195 info.data_node_path = string("/dev/input/event") + event_num;
196 info.enable_node_path = base_dir + string("enable");
197 info.interval_node_path = base_dir + interval_node_name;
201 bool sensor_hal::set_node_value(const string &node_path, int value)
203 fstream node(node_path, fstream::out);
213 bool sensor_hal::set_node_value(const string &node_path, unsigned long long value)
215 fstream node(node_path, fstream::out);
226 bool sensor_hal::get_node_value(const string &node_path, int &value)
228 fstream node(node_path, fstream::in);
238 bool sensor_hal::set_enable_node(const string &node_path, bool sensorhub_controlled, bool enable, int enable_bit)
240 int prev_status, status;
242 AUTOLOCK(m_shared_mutex);
244 if (!get_node_value(node_path, prev_status)) {
245 ERR("Failed to get node: %s", node_path.c_str());
249 int _enable_bit = sensorhub_controlled ? enable_bit : 0;
252 status = prev_status | (1 << _enable_bit);
254 status = prev_status & (~(1 << _enable_bit));
256 if (!set_node_value(node_path, status)) {
257 ERR("Failed to set node: %s", node_path.c_str());
265 bool sensor_hal::find_model_id(const string &sensor_type, string &model_id)
267 string dir_path = "/sys/class/sensors/";
268 string name_node, name;
271 struct dirent *dir_entry = NULL;
274 dir = opendir(dir_path.c_str());
276 DBG("Failed to open dir: %s", dir_path.c_str());
280 while (!find && (dir_entry = readdir(dir))) {
281 d_name = string(dir_entry->d_name);
283 if ((d_name != ".") && (d_name != "..") && (dir_entry->d_ino != 0)) {
284 name_node = dir_path + d_name + string("/name");
286 ifstream infile(name_node.c_str());
293 if (csensor_config::get_instance().is_supported(sensor_type, name)) {
306 bool sensor_hal::get_event_num(const string &input_path, string &event_num)
308 const string event_prefix = "event";
310 struct dirent *dir_entry = NULL;
314 dir = opendir(input_path.c_str());
316 ERR("Failed to open dir: %s", input_path.c_str());
320 int prefix_size = event_prefix.size();
322 while (!find && (dir_entry = readdir(dir))) {
323 node_name = dir_entry->d_name;
325 if (node_name.compare(0, prefix_size, event_prefix) == 0) {
326 event_num = node_name.substr(prefix_size, node_name.size() - prefix_size);
337 bool sensor_hal::get_input_method(const string &key, int &method, string &device_num)
339 input_method_info input_info[2] = {
340 {INPUT_EVENT_METHOD, "/sys/class/input/", "input"},
341 {IIO_METHOD, "/sys/bus/iio/devices/", "iio:device"}
344 const int input_info_len = sizeof(input_info)/sizeof(input_info[0]);
346 string name_node, name;
349 struct dirent *dir_entry = NULL;
352 for (int i = 0; i < input_info_len; ++i) {
354 prefix_size = input_info[i].prefix.size();
356 dir = opendir(input_info[i].dir_path.c_str());
358 ERR("Failed to open dir: %s", input_info[i].dir_path.c_str());
364 while (!find && (dir_entry = readdir(dir))) {
365 d_name = string(dir_entry->d_name);
367 if (d_name.compare(0, prefix_size, input_info[i].prefix) == 0) {
368 name_node = input_info[i].dir_path + d_name + string("/name");
370 ifstream infile(name_node.c_str());
377 device_num = d_name.substr(prefix_size, d_name.size() - prefix_size);
379 method = input_info[i].method;