From: kibak.yoon Date: Wed, 11 May 2016 09:45:00 +0000 (+0900) Subject: sensor-hal-tm1: sync with sensor-hal-tw1 HAL X-Git-Tag: submit/tizen_mobile/20160523.074427^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1e5ebe281ff2cb3b47262c5f1919c653211c8922;p=platform%2Fadaptation%2Ftm1%2Fsensor-hal-tm1.git sensor-hal-tm1: sync with sensor-hal-tw1 HAL - clean up the code for maintenance. Change-Id: Id0012c3341c31b4778a22bfa25cf6b44c07b2cf4 Signed-off-by: kibak.yoon --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 8810c29..390f4a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -PROJECT(sensor-hal CXX) +PROJECT(sensor-hal-tm1 CXX) INCLUDE(GNUInstallDirs) - SET(ACCEL "ON") SET(PROXIMITY "ON") SET(SENSORHUB "OFF") @@ -18,13 +17,9 @@ MESSAGE("FLAGS: ${CMAKE_EXE_LINKER_FLAGS}") #ADD_DEFINITIONS(-Wall -g -D_DEBUG) INCLUDE(FindPkgConfig) -PKG_CHECK_MODULES(hal_pkgs REQUIRED dlog glib-2.0 gio-2.0 gobject-2.0 vconf) - -FOREACH(flag ${hal_pkgs_LDFLAGS}) - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}") -ENDFOREACH(flag) +PKG_CHECK_MODULES(HAL_PKGS REQUIRED dlog glib-2.0 gio-2.0 gobject-2.0 vconf) -FOREACH(flag ${hal_pkgs_CFLAGS}) +FOREACH(flag ${HAL_PKGS_CFLAGS}) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}") ENDFOREACH(flag) @@ -50,6 +45,7 @@ ENDIF() MESSAGE("Sources: ${SRCS}") ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS}) -TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${hal_pkgs_LDFLAGS}) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${HAL_PKGS_LDFLAGS}) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE.APLv2 DESTINATION share/license RENAME ${PROJECT_NAME}) INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR}/sensor) diff --git a/packaging/sensor-hal-tm1.spec b/packaging/sensor-hal-tm1.spec index 460c8f3..38d8b88 100644 --- a/packaging/sensor-hal-tm1.spec +++ b/packaging/sensor-hal-tm1.spec @@ -46,3 +46,4 @@ rm -rf %{buildroot} %files %manifest packaging/%{name}.manifest %{_libdir}/sensor/*.so +%{_datadir}/license/sensor-hal-tm1 diff --git a/src/accel/accel.cpp b/src/accel/accel.cpp deleted file mode 100644 index f0c690c..0000000 --- a/src/accel/accel.cpp +++ /dev/null @@ -1,267 +0,0 @@ -/* - * Copyright (c) 2016 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. - * - */ - -#include -#include -#include -#include -#include -#include -#include - -#include "accel.h" - -#define GRAVITY 9.80665 -#define G_TO_MG 1000 -#define RAW_DATA_TO_G_UNIT(X) (((float)(X))/((float)G_TO_MG)) -#define RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(X) (GRAVITY * (RAW_DATA_TO_G_UNIT(X))) - -#define MIN_RANGE(RES) (-((1 << (RES))/2)) -#define MAX_RANGE(RES) (((1 << (RES))/2)-1) - -#define MODEL_NAME "K2HH" -#define VENDOR "ST Microelectronics" -#define RESOLUTION 16 -#define RAW_DATA_UNIT 0.122 -#define MIN_INTERVAL 1 -#define MAX_BATCH_COUNT 0 - -#define SENSORHUB_ACCELEROMETER_ENABLE_BIT 0 - -static const sensor_info_t sensor_info = { - id: 0x1, - name: "Accelerometer", - type: SENSOR_DEVICE_ACCELEROMETER, - event_type: (SENSOR_DEVICE_ACCELEROMETER << 16) | 0x0001, - model_name: MODEL_NAME, - vendor: VENDOR, - min_range: MIN_RANGE(RESOLUTION) * RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(RAW_DATA_UNIT), - max_range: MAX_RANGE(RESOLUTION) * RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(RAW_DATA_UNIT), - resolution: RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(RAW_DATA_UNIT), - min_interval: MIN_INTERVAL, - max_batch_count: MAX_BATCH_COUNT, - wakeup_supported: false -}; - -std::vector accel_device::event_ids; - -accel_device::accel_device() -: m_node_handle(-1) -, m_x(-1) -, m_y(-1) -, m_z(-1) -, m_polling_interval(0) -, m_fired_time(0) -, m_sensorhub_controlled(false) -{ - const std::string sensorhub_interval_node_name = "accel_poll_delay"; - - node_info_query query; - node_info info; - - 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 (!util::get_node_info(query, info)) { - ERR("Failed to get node info"); - throw ENXIO; - } - - util::show_node_info(info); - - m_data_node = info.data_node_path; - m_enable_node = info.enable_node_path; - m_interval_node = info.interval_node_path; - - if ((m_node_handle = open(m_data_node.c_str(), O_RDONLY)) < 0) { - _ERRNO(errno, _E, "accel handle open fail for accel processor"); - throw ENXIO; - } - - INFO("accel_device is created!\n"); -} - -accel_device::~accel_device() -{ - close(m_node_handle); - m_node_handle = -1; - - INFO("accel_device is destroyed!\n"); -} - -int accel_device::get_poll_fd() -{ - return m_node_handle; -} - -int accel_device::get_sensors(const sensor_info_t **sensors) -{ - *sensors = &sensor_info; - - return 1; -} - -bool accel_device::enable(uint32_t id) -{ - 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; - INFO("Enable accelerometer sensor"); - return true; -} - -bool accel_device::disable(uint32_t id) -{ - util::set_enable_node(m_enable_node, m_sensorhub_controlled, false, SENSORHUB_ACCELEROMETER_ENABLE_BIT); - - INFO("Disable accelerometer sensor"); - return true; -} - -bool accel_device::set_interval(uint32_t id, unsigned long val) -{ - unsigned long long polling_interval_ns; - - polling_interval_ns = ((unsigned long long)(val) * 1000llu * 1000llu); - - 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; - } - - INFO("Interval is changed from %dms to %dms]", m_polling_interval, val); - m_polling_interval = val; - return true; -} - -bool accel_device::update_value_input_event(void) -{ - int accel_raw[3] = {0,}; - bool x,y,z; - int read_input_cnt = 0; - const int INPUT_MAX_BEFORE_SYN = 10; - unsigned long long fired_time = 0; - bool syn = false; - - x = y = z = false; - - struct input_event accel_input; - DBG("accel event detection!"); - - while ((syn == false) && (read_input_cnt < INPUT_MAX_BEFORE_SYN)) { - int len = read(m_node_handle, &accel_input, sizeof(accel_input)); - if (len != sizeof(accel_input)) { - ERR("accel_file read fail, read_len = %d\n",len); - return false; - } - - ++read_input_cnt; - - if (accel_input.type == EV_REL) { - switch (accel_input.code) { - case REL_X: - accel_raw[0] = (int)accel_input.value; - x = true; - break; - case REL_Y: - accel_raw[1] = (int)accel_input.value; - y = true; - break; - case REL_Z: - accel_raw[2] = (int)accel_input.value; - z = true; - break; - default: - ERR("accel_input event[type = %d, code = %d] is unknown.", accel_input.type, accel_input.code); - return false; - break; - } - } else if (accel_input.type == EV_SYN) { - syn = true; - 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; - } - } - - if (syn == false) { - ERR("EV_SYN didn't come until %d inputs had come", read_input_cnt); - return false; - } - - if (x) - m_x = accel_raw[0]; - if (y) - m_y = accel_raw[1]; - if (z) - m_z = accel_raw[2]; - - m_fired_time = fired_time; - - DBG("m_x = %d, m_y = %d, m_z = %d, time = %lluus", m_x, m_y, m_z, m_fired_time); - - return true; -} - -int accel_device::read_fd(uint32_t **ids) -{ - if (!update_value_input_event()) { - DBG("Failed to update value"); - return false; - } - - event_ids.clear(); - event_ids.push_back(sensor_info.id); - - *ids = &event_ids[0]; - - return event_ids.size(); -} - -int accel_device::get_data(uint32_t id, sensor_data_t **data, int *length) -{ - int remains = 1; - sensor_data_t *sensor_data; - sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t)); - - sensor_data->accuracy = SENSOR_ACCURACY_GOOD; - sensor_data->timestamp = m_fired_time; - sensor_data->value_count = 3; - sensor_data->values[0] = m_x; - sensor_data->values[1] = m_y; - sensor_data->values[2] = m_z; - - raw_to_base(sensor_data); - - *data = sensor_data; - *length = sizeof(sensor_data_t); - - return --remains; -} - -void accel_device::raw_to_base(sensor_data_t *data) -{ - data->value_count = 3; - data->values[0] = RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(data->values[0] * RAW_DATA_UNIT); - data->values[1] = RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(data->values[1] * RAW_DATA_UNIT); - data->values[2] = RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(data->values[2] * RAW_DATA_UNIT); -} diff --git a/src/accel/accel.h b/src/accel/accel.h deleted file mode 100644 index 75a5d99..0000000 --- a/src/accel/accel.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2016 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 _ACCEL_DEVICE_H_ -#define _ACCEL_DEVICE_H_ - -#include -#include -#include - -class accel_device : public sensor_device { -public: - accel_device(); - virtual ~accel_device(); - - int get_poll_fd(void); - int get_sensors(const sensor_info_t **sensors); - - bool enable(uint32_t id); - bool disable(uint32_t id); - - bool set_interval(uint32_t id, unsigned long val); - - int read_fd(uint32_t **ids); - int get_data(uint32_t id, sensor_data_t **data, int *length); - -private: - int m_node_handle; - int m_x; - int m_y; - int m_z; - unsigned long m_polling_interval; - unsigned long long m_fired_time; - bool m_sensorhub_controlled; - - std::string m_data_node; - std::string m_enable_node; - std::string m_interval_node; - - static std::vector event_ids; - - bool update_value_input_event(void); - void raw_to_base(sensor_data_t *data); -}; -#endif /*_ACCEL_DEVICE_H_*/ diff --git a/src/accel/accel_device.cpp b/src/accel/accel_device.cpp new file mode 100644 index 0000000..ca696db --- /dev/null +++ b/src/accel/accel_device.cpp @@ -0,0 +1,349 @@ +/* + * Copyright (c) 2016 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. + * + */ + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include "accel_device.h" + +#define GRAVITY 9.80665 +#define G_TO_MG 1000 +#define RAW_DATA_TO_G_UNIT(X) (((float)(X))/((float)G_TO_MG)) +#define RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(X) (GRAVITY * (RAW_DATA_TO_G_UNIT(X))) + +#define MIN_RANGE(RES) (-((1 << (RES))/2)) +#define MAX_RANGE(RES) (((1 << (RES))/2)-1) + +#define MODEL_NAME "K2HH" +#define VENDOR "ST Microelectronics" +#define RESOLUTION 16 +#define RAW_DATA_UNIT 0.122 +#define MIN_INTERVAL 1 +#define MAX_BATCH_COUNT 0 + +#define SENSOR_NAME "SENSOR_ACCELEROMETER" +#define SENSOR_TYPE_ACCEL "ACCEL" + +#define INPUT_NAME "accelerometer_sensor" +#define ACCEL_SENSORHUB_POLL_NODE_NAME "accel_poll_delay" + +static sensor_info_t sensor_info = { + id: 0x1, + name: SENSOR_NAME, + type: SENSOR_DEVICE_ACCELEROMETER, + event_type: (SENSOR_DEVICE_ACCELEROMETER << SENSOR_EVENT_SHIFT) | RAW_DATA_EVENT, + model_name: MODEL_NAME, + vendor: VENDOR, + min_range: MIN_RANGE(RESOLUTION) * RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(RAW_DATA_UNIT), + max_range: MAX_RANGE(RESOLUTION) * RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(RAW_DATA_UNIT), + resolution: RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(RAW_DATA_UNIT), + min_interval: MIN_INTERVAL, + max_batch_count: MAX_BATCH_COUNT, + wakeup_supported: false +}; + +accel_device::accel_device() +: m_node_handle(-1) +, m_x(-1) +, m_y(-1) +, m_z(-1) +, m_polling_interval(1000) +, m_fired_time(0) +, m_sensorhub_controlled(false) +{ + const std::string sensorhub_interval_node_name = ACCEL_SENSORHUB_POLL_NODE_NAME; + + node_info_query query; + node_info info; + + query.sensorhub_controlled = m_sensorhub_controlled = util::is_sensorhub_controlled(sensorhub_interval_node_name); + query.sensor_type = SENSOR_TYPE_ACCEL; + query.key = INPUT_NAME; + query.iio_enable_node_name = "accel_enable"; + query.sensorhub_interval_node_name = sensorhub_interval_node_name; + + if (!util::get_node_info(query, info)) { + _E("Failed to get node info"); + throw ENXIO; + } + + util::show_node_info(info); + + m_method = info.method; + m_data_node = info.data_node_path; + m_enable_node = info.enable_node_path; + m_interval_node = info.interval_node_path; + + m_node_handle = open(m_data_node.c_str(), O_RDONLY); + + if (m_node_handle < 0) { + _ERRNO(errno, _E, "accel handle open fail for accel processor"); + throw ENXIO; + } + + if (m_method == INPUT_EVENT_METHOD) { + if (!util::set_monotonic_clock(m_node_handle)) + throw ENXIO; + + update_value = [=]() { + return this->update_value_input_event(); + }; + } else { + if (!info.buffer_length_node_path.empty()) + util::set_node_value(info.buffer_length_node_path, 480); + + if (!info.buffer_enable_node_path.empty()) + util::set_node_value(info.buffer_enable_node_path, 1); + + update_value = [=]() { + return this->update_value_iio(); + }; + } + + _I("accel_sensor is created!"); +} + +accel_device::~accel_device() +{ + close(m_node_handle); + m_node_handle = -1; + + _I("accel_sensor is destroyed!"); +} + +int accel_device::get_poll_fd() +{ + return m_node_handle; +} + +int accel_device::get_sensors(const sensor_info_t **sensors) +{ + *sensors = &sensor_info; + + return 1; +} + +bool accel_device::enable(uint32_t id) +{ + 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; + _I("Enable accelerometer sensor"); + return true; +} + +bool accel_device::disable(uint32_t id) +{ + util::set_enable_node(m_enable_node, m_sensorhub_controlled, false, SENSORHUB_ACCELEROMETER_ENABLE_BIT); + + _I("Disable accelerometer sensor"); + return true; +} + +bool accel_device::set_interval(uint32_t id, unsigned long val) +{ + unsigned long long polling_interval_ns; + + polling_interval_ns = ((unsigned long long)(val) * 1000llu * 1000llu); + + if (!util::set_node_value(m_interval_node, polling_interval_ns)) { + _E("Failed to set polling resource: %s", m_interval_node.c_str()); + return false; + } + + _I("Interval is changed from %dms to %dms", m_polling_interval, val); + m_polling_interval = val; + return true; +} + +bool accel_device::update_value_input_event(void) +{ + int accel_raw[3] = {0,}; + bool x,y,z; + int read_input_cnt = 0; + const int INPUT_MAX_BEFORE_SYN = 10; + unsigned long long fired_time = 0; + bool syn = false; + + x = y = z = false; + + struct input_event accel_input; + _D("accel event detection!"); + + while ((syn == false) && (read_input_cnt < INPUT_MAX_BEFORE_SYN)) { + int len = read(m_node_handle, &accel_input, sizeof(accel_input)); + if (len != sizeof(accel_input)) { + _E("accel_file read fail, read_len = %d",len); + return false; + } + + ++read_input_cnt; + + if (accel_input.type == EV_REL) { + switch (accel_input.code) { + case REL_X: + accel_raw[0] = (int)accel_input.value; + x = true; + break; + case REL_Y: + accel_raw[1] = (int)accel_input.value; + y = true; + break; + case REL_Z: + accel_raw[2] = (int)accel_input.value; + z = true; + break; + default: + _E("accel_input event[type = %d, code = %d] is unknown.", accel_input.type, accel_input.code); + return false; + break; + } + } else if (accel_input.type == EV_SYN) { + syn = true; + fired_time = util::get_timestamp(&accel_input.time); + } else { + _E("accel_input event[type = %d, code = %d] is unknown.", accel_input.type, accel_input.code); + return false; + } + } + + if (syn == false) { + _E("EV_SYN didn't come until %d inputs had come", read_input_cnt); + return false; + } + + if (x) + m_x = accel_raw[0]; + if (y) + m_y = accel_raw[1]; + if (z) + m_z = accel_raw[2]; + + m_fired_time = fired_time; + + _D("m_x = %d, m_y = %d, m_z = %d, time = %lluus", m_x, m_y, m_z, m_fired_time); + + return true; +} + +bool accel_device::update_value_iio(void) +{ + struct { + int16_t x; + int16_t y; + int16_t z; + int64_t timestamp; + } __attribute__((packed)) data; + + struct pollfd pfd; + + pfd.fd = m_node_handle; + pfd.events = POLLIN | POLLERR; + pfd.revents = 0; + + int ret = poll(&pfd, 1, -1); + + if (ret == -1) { + _ERRNO(errno, _E, "Failed to poll from m_node_handle:%d", m_node_handle); + return false; + } else if (!ret) { + _E("poll timeout m_node_handle:%d", m_node_handle); + return false; + } + + if (pfd.revents & POLLERR) { + _E("poll exception occurred! m_node_handle:%d", m_node_handle); + return false; + } + + if (!(pfd.revents & POLLIN)) { + _E("poll nothing to read! m_node_handle:%d, pfd.revents = %d", m_node_handle, pfd.revents); + return false; + } + + int len = read(m_node_handle, &data, sizeof(data)); + + if (len != sizeof(data)) { + _E("Failed to read data, m_node_handle:%d read_len:%d", m_node_handle, len); + return false; + } + + m_x = data.x; + m_y = data.y; + m_z = data.z; + m_fired_time = data.timestamp; + + _D("m_x = %d, m_y = %d, m_z = %d, time = %lluus", m_x, m_y, m_z, m_fired_time); + + return true; +} + +int accel_device::read_fd(uint32_t **ids) +{ + if (!update_value()) { + _D("Failed to update value"); + return false; + } + + event_ids.clear(); + event_ids.push_back(sensor_info.id); + + *ids = &event_ids[0]; + + return event_ids.size(); +} + +int accel_device::get_data(uint32_t id, sensor_data_t **data, int *length) +{ + int remains = 1; + sensor_data_t *sensor_data; + sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t)); + retvm_if(!sensor_data, -ENOMEM, "Memory allocation failed"); + + sensor_data->accuracy = SENSOR_ACCURACY_GOOD; + sensor_data->timestamp = m_fired_time; + sensor_data->value_count = 3; + sensor_data->values[0] = m_x; + sensor_data->values[1] = m_y; + sensor_data->values[2] = m_z; + + raw_to_base(sensor_data); + + *data = sensor_data; + *length = sizeof(sensor_data_t); + + return --remains; +} + +void accel_device::raw_to_base(sensor_data_t *data) +{ + data->value_count = 3; + data->values[0] = RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(data->values[0] * RAW_DATA_UNIT); + data->values[1] = RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(data->values[1] * RAW_DATA_UNIT); + data->values[2] = RAW_DATA_TO_METRE_PER_SECOND_SQUARED_UNIT(data->values[2] * RAW_DATA_UNIT); +} diff --git a/src/accel/accel_device.h b/src/accel/accel_device.h new file mode 100644 index 0000000..36c958d --- /dev/null +++ b/src/accel/accel_device.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2016 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 _ACCEL_DEVICE_H_ +#define _ACCEL_DEVICE_H_ + +#include +#include +#include +#include + +class accel_device : public sensor_device { +public: + accel_device(); + virtual ~accel_device(); + + int get_poll_fd(void); + int get_sensors(const sensor_info_t **sensors); + + bool enable(uint32_t id); + bool disable(uint32_t id); + + bool set_interval(uint32_t id, unsigned long val); + + int read_fd(uint32_t **ids); + int get_data(uint32_t id, sensor_data_t **data, int *length); + +private: + int m_node_handle; + int m_x; + int m_y; + int m_z; + unsigned long m_polling_interval; + unsigned long long m_fired_time; + bool m_sensorhub_controlled; + + int m_method; + std::string m_data_node; + std::string m_enable_node; + std::string m_interval_node; + + std::function update_value; + + std::vector event_ids; + + bool update_value_input_event(void); + bool update_value_iio(void); + + void raw_to_base(sensor_data_t *data); +}; +#endif /* _ACCEL_DEVICE_H_ */ diff --git a/src/create.cpp b/src/create.cpp index 6ed9bbd..2e9a2d5 100644 --- a/src/create.cpp +++ b/src/create.cpp @@ -19,8 +19,8 @@ #include #include -#include "accel/accel.h" -#include "proxi/proxi.h" +#include "accel/accel_device.h" +#include "proxi/proxi_device.h" #include "sensorhub/sensorhub.h" static std::vector devs; @@ -32,7 +32,7 @@ void create_sensor(const char *name) try { instance = new _sensor; } catch (std::exception &e) { - ERR("Failed to create %s sensor device (%s)", name, e.what()); + ERR("Failed to create %s sensor device, exception: %s", name, e.what()); return; } catch (int err) { _ERRNO(err, _E, "Failed to create %s sensor device", name); @@ -45,18 +45,15 @@ void create_sensor(const char *name) extern "C" int create(sensor_device_t **devices) { #ifdef ENABLE_ACCEL - create_sensor("Accel"); + create_sensor("Accelerometer"); #endif - #ifdef ENABLE_PROXIMITY create_sensor("Proximity"); #endif - #ifdef ENABLE_SENSORHUB create_sensor("Sensorhub"); #endif *devices = &devs[0]; - return devs.size(); } diff --git a/src/macro.h b/src/macro.h index 64446af..132b547 100644 --- a/src/macro.h +++ b/src/macro.h @@ -20,5 +20,6 @@ #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#define MIN(x, y) ((x) < (y) ? (x) : (y)) + #endif /* __MACRO_H__ */ -//! End of a file diff --git a/src/proxi/proxi.cpp b/src/proxi/proxi.cpp deleted file mode 100644 index aa930f2..0000000 --- a/src/proxi/proxi.cpp +++ /dev/null @@ -1,184 +0,0 @@ -/* - * Copyright (c) 2016 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. - * - */ - -#include -#include -#include -#include -#include -#include -#include - -#include "proxi.h" - -#define MODEL_NAME "IMS1911" -#define VENDOR "ITM" -#define MIN_RANGE 0 -#define MAX_RANGE 5 -#define RESOLUTION 1 -#define MIN_INTERVAL 1 -#define MAX_BATCH_COUNT 0 - -#define SENSORHUB_PROXIMITY_ENABLE_BIT 7 - -#define RAW_DATA_TO_DISTANCE(x) ((x) * 5) - -static const sensor_info_t sensor_info = { - id: 0x1, - name: "Proximity Sensor", - type: SENSOR_DEVICE_PROXIMITY, - event_type: (SENSOR_DEVICE_PROXIMITY << 16) | 0x0001, - model_name: MODEL_NAME, - vendor: VENDOR, - min_range: MIN_RANGE, - max_range: MAX_RANGE, - resolution: RESOLUTION, - min_interval: MIN_INTERVAL, - max_batch_count: MAX_BATCH_COUNT, - wakeup_supported: false -}; - -std::vector proxi_device::event_ids; - -proxi_device::proxi_device() -: m_node_handle(-1) -, m_state(-1) -, m_fired_time(0) -, m_sensorhub_controlled(false) -{ - const std::string sensorhub_interval_node_name = "prox_poll_delay"; - - node_info_query query; - node_info info; - - query.sensorhub_controlled = m_sensorhub_controlled = util::is_sensorhub_controlled(sensorhub_interval_node_name); - query.sensor_type = "PROXI"; - query.key = "proximity_sensor"; - query.iio_enable_node_name = "proximity_enable"; - query.sensorhub_interval_node_name = sensorhub_interval_node_name; - - if (!util::get_node_info(query, info)) { - ERR("Failed to get node info"); - throw ENXIO; - } - - util::show_node_info(info); - - m_data_node = info.data_node_path; - m_enable_node = info.enable_node_path; - - if ((m_node_handle = open(m_data_node.c_str(), O_RDONLY)) < 0) { - _ERRNO(errno, _E, "proxi handle open fail for proxi device"); - throw ENXIO; - } - - INFO("proxi_device is created!"); -} - -proxi_device::~proxi_device() -{ - close(m_node_handle); - m_node_handle = -1; - - INFO("proxi_device is destroyed!"); -} - -int proxi_device::get_poll_fd() -{ - return m_node_handle; -} - -int proxi_device::get_sensors(const sensor_info_t **sensors) -{ - *sensors = &sensor_info; - - return 1; -} - -bool proxi_device::enable(uint32_t id) -{ - util::set_enable_node(m_enable_node, m_sensorhub_controlled, true, SENSORHUB_PROXIMITY_ENABLE_BIT); - - m_fired_time = 0; - INFO("Enable proximity sensor"); - return true; -} - -bool proxi_device::disable(uint32_t id) -{ - util::set_enable_node(m_enable_node, m_sensorhub_controlled, false, SENSORHUB_PROXIMITY_ENABLE_BIT); - - INFO("Disable proximity sensor"); - return true; -} - -bool proxi_device::update_value_input_event(void) -{ - struct input_event proxi_event; - DBG("proxi event detection!"); - - int len = read(m_node_handle, &proxi_event, sizeof(proxi_event)); - - if (len == -1) { - _ERRNO(errno, _D, "Failed to read from m_node_handle[%d]", m_node_handle); - return false; - } - - if ((proxi_event.type == EV_ABS) && (proxi_event.code == ABS_DISTANCE)) { - m_state = proxi_event.value; - m_fired_time = util::get_timestamp(&proxi_event.time); - - DBG("m_state = %d, time = %lluus", m_state, m_fired_time); - - return true; - } - - return false; -} - -int proxi_device::read_fd(uint32_t **ids) -{ - if (!update_value_input_event()) { - DBG("Failed to update value"); - return false; - } - - event_ids.clear(); - event_ids.push_back(sensor_info.id); - - *ids = &event_ids[0]; - - return event_ids.size(); -} - -int proxi_device::get_data(uint32_t id, sensor_data_t **data, int *length) -{ - int remains = 1; - sensor_data_t *sensor_data; - sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t)); - - sensor_data->accuracy = SENSOR_ACCURACY_GOOD; - sensor_data->timestamp = m_fired_time; - sensor_data->value_count = 1; - sensor_data->values[0] = RAW_DATA_TO_DISTANCE(m_state); - - *data = sensor_data; - *length = sizeof(sensor_data_t); - - return --remains; -} - diff --git a/src/proxi/proxi.h b/src/proxi/proxi.h deleted file mode 100644 index 1946304..0000000 --- a/src/proxi/proxi.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2016 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 _PROXI_DEVICE_H_ -#define _PROXI_DEVICE_H_ - -#include -#include -#include - -class proxi_device : public sensor_device -{ -public: - proxi_device(); - virtual ~proxi_device(); - - int get_poll_fd(void); - int get_sensors(const sensor_info_t **sensors); - - bool enable(uint32_t id); - bool disable(uint32_t id); - - int read_fd(uint32_t **ids); - int get_data(uint32_t id, sensor_data_t **data, int *length); - -private: - int m_node_handle; - unsigned int m_state; - unsigned long long m_fired_time; - bool m_sensorhub_controlled; - - std::string m_data_node; - std::string m_enable_node; - - static std::vector event_ids; - - bool update_value_input_event(void); -}; -#endif /*_PROXI_DEVICE_H_*/ diff --git a/src/proxi/proxi_device.cpp b/src/proxi/proxi_device.cpp new file mode 100644 index 0000000..211db23 --- /dev/null +++ b/src/proxi/proxi_device.cpp @@ -0,0 +1,206 @@ +/* + * Copyright (c) 2016 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. + * + */ + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include "proxi_device.h" + +#define MODEL_NAME "IMS1911" +#define VENDOR "ITM" +#define MIN_RANGE 0 +#define MAX_RANGE 5 +#define RESOLUTION 1 +#define MIN_INTERVAL 1 +#define MAX_BATCH_COUNT 0 + +#define SENSOR_NAME "SENSOR_PROXIMITY" +#define SENSOR_TYPE_PROXI "PROXI" + +#define INPUT_NAME "proximity_sensor" +#define PROXI_SENSORHUB_POLL_NODE_NAME "prox_poll_delay" + +#define RAW_DATA_TO_DISTANCE(x) ((x) * 5) + +static sensor_info_t sensor_info = { + id: 0x1, + name: SENSOR_NAME, + type: SENSOR_DEVICE_PROXIMITY, + event_type: (SENSOR_DEVICE_PROXIMITY << SENSOR_EVENT_SHIFT) | RAW_DATA_EVENT, + model_name: MODEL_NAME, + vendor: VENDOR, + min_range: MIN_RANGE, + max_range: MAX_RANGE, + resolution: RESOLUTION, + min_interval: MIN_INTERVAL, + max_batch_count: MAX_BATCH_COUNT, + wakeup_supported: false +}; + +proxi_device::proxi_device() +: m_node_handle(-1) +, m_state(PROXIMITY_NODE_STATE_FAR) +, m_fired_time(0) +, m_sensorhub_controlled(false) +{ + const std::string sensorhub_interval_node_name = PROXI_SENSORHUB_POLL_NODE_NAME; + + node_info_query query; + node_info info; + + query.sensorhub_controlled = m_sensorhub_controlled = util::is_sensorhub_controlled(sensorhub_interval_node_name); + query.sensor_type = SENSOR_TYPE_PROXI; + query.key = INPUT_NAME; + query.iio_enable_node_name = "proximity_enable"; + query.sensorhub_interval_node_name = sensorhub_interval_node_name; + + if (!util::get_node_info(query, info)) { + _E("Failed to get node info"); + throw ENXIO; + } + + util::show_node_info(info); + + m_method = info.method; + m_data_node = info.data_node_path; + m_enable_node = info.enable_node_path; + + m_node_handle = open(m_data_node.c_str(), O_RDONLY); + + if (m_node_handle < 0) { + _ERRNO(errno, _E, "proxi handle open fail for proxi device"); + throw ENXIO; + } + + if (m_method == INPUT_EVENT_METHOD) { + if (!util::set_monotonic_clock(m_node_handle)) + throw ENXIO; + + update_value = [=]() { + return this->update_value_input_event(); + }; + } + + _I("Proxi_sensor_hal is created!"); +} + +proxi_device::~proxi_device() +{ + close(m_node_handle); + m_node_handle = -1; + + _I("Proxi_sensor_hal is destroyed!"); +} + +int proxi_device::get_poll_fd(void) +{ + return m_node_handle; +} + +int proxi_device::get_sensors(const sensor_info_t **sensors) +{ + *sensors = &sensor_info; + + return 1; +} + +bool proxi_device::enable(uint32_t id) +{ + util::set_enable_node(m_enable_node, m_sensorhub_controlled, true, SENSORHUB_PROXIMITY_ENABLE_BIT); + + m_fired_time = 0; + INFO("Enable proxi sensor"); + return true; +} + +bool proxi_device::disable(uint32_t id) +{ + util::set_enable_node(m_enable_node, m_sensorhub_controlled, false, SENSORHUB_PROXIMITY_ENABLE_BIT); + + INFO("Disable proxi sensor"); + return true; +} + +bool proxi_device::update_value_input_event(void) +{ + struct input_event proxi_event; + _I("proxi event detection!"); + + int len = read(m_node_handle, &proxi_event, sizeof(proxi_event)); + + if (len == -1) { + _ERRNO(errno, _E, "Failed to read from m_node_handle"); + return false; + } + + _D("read event, len : %d , type : %x , code : %x , value : %x", len, proxi_event.type, proxi_event.code, proxi_event.value); + if ((proxi_event.type != EV_ABS) || (proxi_event.code != ABS_DISTANCE)) + return false; + + if (proxi_event.value != PROXIMITY_NODE_STATE_FAR && proxi_event.value != PROXIMITY_NODE_STATE_NEAR) { + _E("PROXIMITY_STATE Unknown: %d",proxi_event.value); + return false; + } + + m_state = proxi_event.value; + m_fired_time = util::get_timestamp(&proxi_event.time); + + return true; +} + +int proxi_device::read_fd(uint32_t **ids) +{ + if (!update_value()) { + DBG("Failed to update value"); + return false; + } + + event_ids.clear(); + event_ids.push_back(sensor_info.id); + + *ids = &event_ids[0]; + + return event_ids.size(); +} + +int proxi_device::get_data(uint32_t id, sensor_data_t **data, int *length) +{ + int remains = 1; + sensor_data_t *sensor_data; + sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t)); + retvm_if(!sensor_data, -ENOMEM, "Memory allocation failed"); + + sensor_data->accuracy = SENSOR_ACCURACY_GOOD; + sensor_data->timestamp = m_fired_time; + sensor_data->value_count = 1; + sensor_data->values[0] = RAW_DATA_TO_DISTANCE(m_state); + + *data = sensor_data; + *length = sizeof(sensor_data_t); + + return --remains; +} diff --git a/src/proxi/proxi_device.h b/src/proxi/proxi_device.h new file mode 100644 index 0000000..2ddb5e4 --- /dev/null +++ b/src/proxi/proxi_device.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2016 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 _PROXI_DEVICE_H_ +#define _PROXI_DEVICE_H_ + +#include +#include +#include +#include + +class proxi_device : public sensor_device { +public: + enum proxi_node_state_event_t { + PROXIMITY_NODE_STATE_NEAR = 0, + PROXIMITY_NODE_STATE_FAR = 1, + PROXIMITY_NODE_STATE_UNKNOWN = 2, + }; + + proxi_device(); + virtual ~proxi_device(); + + int get_poll_fd(void); + int get_sensors(const sensor_info_t **sensors); + + bool enable(uint32_t id); + bool disable(uint32_t id); + + int read_fd(uint32_t **ids); + int get_data(uint32_t id, sensor_data_t **data, int *length); + +private: + int m_node_handle; + unsigned int m_state; + unsigned long long m_fired_time; + bool m_sensorhub_controlled; + + int m_method; + std::string m_data_node; + std::string m_enable_node; + + std::function update_value; + + std::vector event_ids; + + bool update_value_input_event(void); +}; + +#endif /* _PROXI_DEVICE_H_ */ diff --git a/src/sensor_common.h b/src/sensor_common.h new file mode 100644 index 0000000..b549c96 --- /dev/null +++ b/src/sensor_common.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2016 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_COMMON_H_ +#define _SENSOR_COMMON_H_ + +#define SENSOR_EVENT_SHIFT 16 +#define RAW_DATA_EVENT 0x0001 + +#define UNKNOWN_NAME "UNKNOWN" + +enum sensorhub_enable_bit { + SENSORHUB_ACCELEROMETER_ENABLE_BIT = 0, + SENSORHUB_GYROSCOPE_ENABLE_BIT, + SENSORHUB_GEOMAGNETIC_UNCALIB_ENABLE_BIT, + SENSORHUB_GEOMAGNETIC_RAW_ENABLE_BIT, + SENSORHUB_GEOMAGNETIC_ENABLE_BIT, + SENSORHUB_PRESSURE_ENABLE_BIT, + SENSORHUB_GESTURE_ENABLE_BIT, + SENSORHUB_PROXIMITY_ENABLE_BIT, + SENSORHUB_TEMPERATURE_HUMIDITY_ENABLE_BIT, + SENSORHUB_LIGHT_ENABLE_BIT, + SENSORHUB_PROXIMITY_RAW_ENABLE_BIT, + SENSORHUB_ORIENTATION_ENABLE_BIT, + SENSORHUB_STEP_DETECTOR_ENABLE_BIT = 12, + SENSORHUB_SIG_MOTION_ENABLE_BIT, + SENSORHUB_GYRO_UNCALIB_ENABLE_BIT, + SENSORHUB_GAME_ROTATION_VECTOR_ENABLE_BIT = 15, + SENSORHUB_ROTATION_VECTOR_ENABLE_BIT, + SENSORHUB_STEP_COUNTER_ENABLE_BIT, + SENSORHUB_HRM_RAW_ENABLE_BIT, + SENSORHUB_HRM_RAW_FAC_ENABLE_BIT, + SENSORHUB_HRM_LIB_ENABLE_BIT, + SENSORHUB_TILT_MOTION, + SENSORHUB_UV_SENSOR, + SENSORHUB_PIR_ENABLE_BIT, + SENSORHUB_PIR_RAW_ENABLE_BIT, + SENSORHUB_GSR_ENABLE_BIT = 25, + SENSORHUB_ENABLE_BIT_MAX, +}; + +#endif /* _SENSOR_COMMON_H_ */ diff --git a/src/sensor_log.h b/src/sensor_log.h index 395abb1..8e593e2 100644 --- a/src/sensor_log.h +++ b/src/sensor_log.h @@ -20,33 +20,17 @@ #include -#if !defined(NAME_MAX) +#ifndef NAME_MAX #define NAME_MAX 256 #endif -#define SENSOR_TYPE_SHIFT 16 - -enum sf_log_type { - SF_LOG_PRINT_FILE = 1, - SF_LOG_SYSLOG = 2, - SF_LOG_DLOG = 3, -}; - -enum sf_priority_type { - SF_LOG_ERR = 1, - SF_LOG_DBG = 2, - SF_LOG_INFO = 3, - SF_LOG_WARN = 4, -}; - -#define MICROSECONDS(tv) ((tv.tv_sec * 1000000ll) + tv.tv_usec) - -//for new log system - dlog #ifdef LOG_TAG #undef LOG_TAG #endif #define LOG_TAG "SENSOR" +#define LOG_DUMP(fp, fmt, arg...) do { if (fp) fprintf(fp, fmt, ##arg); else _E(fmt, ##arg); } while(0) + #ifdef _DEBUG #define DBG SLOGD #else @@ -71,64 +55,64 @@ enum sf_priority_type { tag(fmt" (%s[%d])", ##arg, error, errno); \ } while (0) -#if defined(_DEBUG) -# define warn_if(expr, fmt, arg...) do { \ +#ifdef _DEBUG +#define warn_if(expr, fmt, arg...) do { \ if(expr) { \ - DBG("(%s) -> " fmt, #expr, ##arg); \ + _D("(%s) -> " fmt, #expr, ##arg); \ } \ } while (0) -# define ret_if(expr) do { \ +#define ret_if(expr) do { \ if(expr) { \ - DBG("(%s) -> %s() return", #expr, __FUNCTION__); \ + _D("(%s) -> %s() return", #expr, __FUNCTION__); \ return; \ } \ } while (0) -# define retv_if(expr, val) do { \ +#define retv_if(expr, val) do { \ if(expr) { \ - DBG("(%s) -> %s() return", #expr, __FUNCTION__); \ + _D("(%s) -> %s() return", #expr, __FUNCTION__); \ return (val); \ } \ } while (0) -# define retm_if(expr, fmt, arg...) do { \ +#define retm_if(expr, fmt, arg...) do { \ if(expr) { \ - ERR(fmt, ##arg); \ - DBG("(%s) -> %s() return", #expr, __FUNCTION__); \ + _E(fmt, ##arg); \ + _D("(%s) -> %s() return", #expr, __FUNCTION__); \ return; \ } \ } while (0) -# define retvm_if(expr, val, fmt, arg...) do { \ +#define retvm_if(expr, val, fmt, arg...) do { \ if(expr) { \ - ERR(fmt, ##arg); \ - DBG("(%s) -> %s() return", #expr, __FUNCTION__); \ + _E(fmt, ##arg); \ + _D("(%s) -> %s() return", #expr, __FUNCTION__); \ return (val); \ } \ } while (0) #else -# define warn_if(expr, fmt, arg...) do { \ +#define warn_if(expr, fmt, arg...) do { \ if(expr) { \ - ERR(fmt, ##arg); \ + _E(fmt, ##arg); \ } \ } while (0) -# define ret_if(expr) do { \ +#define ret_if(expr) do { \ if(expr) { \ return; \ } \ } while (0) -# define retv_if(expr, val) do { \ +#define retv_if(expr, val) do { \ if(expr) { \ return (val); \ } \ } while (0) -# define retm_if(expr, fmt, arg...) do { \ +#define retm_if(expr, fmt, arg...) do { \ if(expr) { \ - ERR(fmt, ##arg); \ + _E(fmt, ##arg); \ return; \ } \ } while (0) -# define retvm_if(expr, val, fmt, arg...) do { \ +#define retvm_if(expr, val, fmt, arg...) do { \ if(expr) { \ - ERR(fmt, ##arg); \ + _E(fmt, ##arg); \ return (val); \ } \ } while (0) diff --git a/src/util.cpp b/src/util.cpp index 19790f9..5b60a16 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -27,12 +27,14 @@ using std::ofstream; using std::fstream; using std::string; +#define PREFIX_EVENT "event" + static bool get_event_num(const string &input_path, string &event_num) { - const string event_prefix = "event"; + const string event_prefix = PREFIX_EVENT; DIR *dir = NULL; struct dirent dir_entry; - struct dirent *result; + struct dirent *result = NULL; std::string node_name; int error; bool find = false; @@ -56,11 +58,12 @@ static bool get_event_num(const string &input_path, string &event_num) node_name = std::string(dir_entry.d_name); - 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; - } + if (node_name.compare(0, prefix_size, event_prefix) != 0) + continue; + + event_num = node_name.substr(prefix_size, node_name.size() - prefix_size); + find = true; + break; } closedir(dir); @@ -153,7 +156,7 @@ static bool get_input_method(const string &key, int &method, string &device_num) std::string d_name; DIR *dir = NULL; struct dirent dir_entry; - struct dirent *result; + struct dirent *result = NULL; int error; bool find = false; @@ -180,22 +183,24 @@ static bool get_input_method(const string &key, int &method, string &device_num) d_name = std::string(dir_entry.d_name); - if (d_name.compare(0, prefix_size, input_info[i].prefix) == 0) { - name_node = input_info[i].dir_path + d_name + string("/name"); + if (d_name.compare(0, prefix_size, input_info[i].prefix) != 0) + continue; + + name_node = input_info[i].dir_path + d_name + string("/name"); - ifstream infile(name_node.c_str()); - if (!infile) - continue; + ifstream infile(name_node.c_str()); + if (!infile) + continue; - infile >> name; + infile >> name; + + if (name != key) + continue; - if (name == key) { - device_num = d_name.substr(prefix_size, d_name.size() - prefix_size); - find = true; - method = input_info[i].method; - break; - } - } + device_num = d_name.substr(prefix_size, d_name.size() - prefix_size); + find = true; + method = input_info[i].method; + break; } closedir(dir); @@ -207,6 +212,18 @@ static bool get_input_method(const string &key, int &method, string &device_num) return find; } +bool util::set_monotonic_clock(int fd) +{ +#ifdef EVIOCSCLOCKID + int clockId = CLOCK_MONOTONIC; + if (ioctl(fd, EVIOCSCLOCKID, &clockId) != 0) { + _E("Fail to set monotonic timestamp for fd[%d]", fd); + return false; + } +#endif + return true; +} + bool util::set_enable_node(const string &node_path, bool sensorhub_controlled, bool enable, int enable_bit) { int prev_status, status; @@ -260,7 +277,6 @@ bool util::is_sensorhub_controlled(const string &key) bool util::get_node_info(const node_info_query &query, node_info &info) { - bool ret = false; int method; string device_num; @@ -273,20 +289,17 @@ bool util::get_node_info(const node_info_query &query, node_info &info) if (method == IIO_METHOD) { if (query.sensorhub_controlled) - ret = get_sensorhub_iio_node_info(query.sensorhub_interval_node_name, device_num, info); + return 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); + return 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); + return get_sensorhub_input_event_node_info(query.sensorhub_interval_node_name, device_num, info); else - ret = get_input_event_node_info(device_num, info); + return get_input_event_node_info(device_num, info); } - - return ret; } - void util::show_node_info(node_info &info) { if (info.data_node_path.size()) diff --git a/src/util.h b/src/util.h index 70c600a..d799b22 100644 --- a/src/util.h +++ b/src/util.h @@ -51,6 +51,8 @@ typedef struct { } input_method_info; namespace util { + bool set_monotonic_clock(int fd); + bool set_enable_node(const std::string &node_path, bool sensorhub_controlled, bool enable, int enable_bit = 0); unsigned long long get_timestamp(void);