--- /dev/null
+/*
+ * accel_sensor_device
+ *
+ * Copyright (c) 2014 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 <fcntl.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <linux/input.h>
+#include <sys/poll.h>
+#include <util.h>
+#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 FIFO_COUNT 0
+#define MAX_BATCH_COUNT 0
+
+static const sensor_properties_s accel_properties = {
+ 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,
+ fifo_count : FIFO_COUNT,
+ max_batch_count : MAX_BATCH_COUNT,
+};
+
+static const sensor_handle_t handles[] = {
+ {
+ id: 0x1,
+ name: "Accelerometer",
+ type: SENSOR_DEVICE_ACCELEROMETER,
+ event_type: (SENSOR_DEVICE_ACCELEROMETER << 16) | 0x0001,
+ properties : accel_properties
+ },
+ {
+ id: 0x2,
+ name: "Accelerometer RAW",
+ type: SENSOR_DEVICE_ACCELEROMETER,
+ event_type: (SENSOR_DEVICE_ACCELEROMETER << 16) | 0x0002,
+ properties : accel_properties
+ }
+};
+
+accel_sensor_device::accel_sensor_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_RDWR)) < 0) {
+ ERR("accel handle open fail for accel processor, error:%s\n", strerror(errno));
+ throw ENXIO;
+ }
+
+ INFO("accel_sensor_device is created!\n");
+}
+
+accel_sensor_device::~accel_sensor_device()
+{
+ close(m_node_handle);
+ m_node_handle = -1;
+
+ INFO("accel_sensor_device is destroyed!\n");
+}
+
+bool accel_sensor_device::get_sensors(std::vector<sensor_handle_t> &sensors)
+{
+ int size = ARRAY_SIZE(handles);
+
+ for (int i = 0; i < size; ++i)
+ sensors.push_back(handles[i]);
+
+ return true;
+}
+
+bool accel_sensor_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_sensor_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;
+}
+
+int accel_sensor_device::get_poll_fd()
+{
+ return m_node_handle;
+}
+
+bool accel_sensor_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_sensor_device::set_batch_latency(uint32_t id, unsigned long val)
+{
+ return false;
+}
+
+bool accel_sensor_device::set_command(uint32_t id, std::string command, std::string value)
+{
+ return false;
+}
+
+bool accel_sensor_device::is_data_ready(void)
+{
+ bool ret;
+ ret = update_value_input_event();
+ return ret;
+}
+
+bool accel_sensor_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;
+}
+
+bool accel_sensor_device::get_sensor_data(uint32_t id, sensor_data_t &data)
+{
+ data.accuracy = SENSOR_ACCURACY_GOOD;
+ data.timestamp = m_fired_time;
+ data.value_count = 3;
+ data.values[0] = m_x;
+ data.values[1] = m_y;
+ data.values[2] = m_z;
+
+ raw_to_base(data);
+
+ return true;
+}
+
+int accel_sensor_device::get_sensor_event(uint32_t id, sensor_event_t **event)
+{
+ sensor_event_t *sensor_event;
+ sensor_event = (sensor_event_t *)malloc(sizeof(sensor_event_t));
+
+ sensor_event->data.accuracy = SENSOR_ACCURACY_GOOD;
+ sensor_event->data.timestamp = m_fired_time;
+ sensor_event->data.value_count = 3;
+ sensor_event->data.values[0] = m_x;
+ sensor_event->data.values[1] = m_y;
+ sensor_event->data.values[2] = m_z;
+
+ raw_to_base(sensor_event->data);
+
+ *event = sensor_event;
+
+ return sizeof(sensor_event_t);
+}
+
+void accel_sensor_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);
+}
+
+bool accel_sensor_device::get_properties(uint32_t id, sensor_properties_s &properties)
+{
+ properties.name = MODEL_NAME;
+ properties.vendor = VENDOR;
+ properties.min_range = accel_properties.min_range;
+ properties.max_range = accel_properties.max_range;
+ properties.min_interval = accel_properties.min_interval;
+ properties.resolution = accel_properties.resolution;
+ properties.fifo_count = accel_properties.fifo_count;
+ properties.max_batch_count = accel_properties.max_batch_count;
+ return true;
+}
--- /dev/null
+/*
+ * accel_sensor_device
+ *
+ * Copyright (c) 2014 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_SENSOR_DEVICE_H_
+#define _ACCEL_SENSOR_DEVICE_H_
+
+#include <sensor_hal.h>
+
+class accel_sensor_device : public sensor_device
+{
+public:
+ accel_sensor_device();
+ virtual ~accel_sensor_device();
+
+ int get_poll_fd(void);
+ bool get_sensors(std::vector<sensor_handle_t> &sensors);
+ bool enable(uint32_t id);
+ bool disable(uint32_t id);
+ bool set_interval(uint32_t id, unsigned long val);
+ bool set_batch_latency(uint32_t id, unsigned long val);
+ bool set_command(uint32_t id, std::string command, std::string value);
+ bool is_data_ready(void);
+ bool get_sensor_data(uint32_t id, sensor_data_t &data);
+ int get_sensor_event(uint32_t id, sensor_event_t **event);
+ bool get_properties(uint32_t id, sensor_properties_s &properties);
+
+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;
+
+ bool update_value_input_event(void);
+ void raw_to_base(sensor_data_t &data);
+};
+#endif /*_ACCEL_SENSOR_DEVICE_CLASS_H_*/
+++ /dev/null
-/*
- * accel_sensor_device
- *
- * Copyright (c) 2014 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 <fcntl.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <linux/input.h>
-#include <sys/poll.h>
-#include "accel_sensor_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 FIFO_COUNT 0
-#define MAX_BATCH_COUNT 0
-
-static const sensor_properties_s accel_properties = {
- 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,
- fifo_count : FIFO_COUNT,
- max_batch_count : MAX_BATCH_COUNT,
-};
-
-static const sensor_handle_t handles[] = {
- {
- id: 0x1,
- name: "Accelerometer",
- type: SENSOR_DEVICE_ACCELEROMETER,
- event_type: (SENSOR_DEVICE_ACCELEROMETER << 16) | 0x0001,
- properties : accel_properties
- },
- {
- id: 0x2,
- name: "Accelerometer RAW",
- type: SENSOR_DEVICE_ACCELEROMETER,
- event_type: (SENSOR_DEVICE_ACCELEROMETER << 16) | 0x0002,
- properties : accel_properties
- }
-};
-
-accel_sensor_device::accel_sensor_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 = 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 (!get_node_info(query, info)) {
- ERR("Failed to get node info");
- throw ENXIO;
- }
-
- 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_RDWR)) < 0) {
- ERR("accel handle open fail for accel processor, error:%s\n", strerror(errno));
- throw ENXIO;
- }
-
- INFO("accel_sensor_device is created!\n");
-}
-
-accel_sensor_device::~accel_sensor_device()
-{
- close(m_node_handle);
- m_node_handle = -1;
-
- INFO("accel_sensor_device is destroyed!\n");
-}
-
-bool accel_sensor_device::get_sensors(std::vector<sensor_handle_t> &sensors)
-{
- int size = ARRAY_SIZE(handles);
-
- for (int i = 0; i < size; ++i)
- sensors.push_back(handles[i]);
-
- return true;
-}
-
-bool accel_sensor_device::enable(uint32_t id)
-{
- 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_sensor_device::disable(uint32_t id)
-{
- set_enable_node(m_enable_node, m_sensorhub_controlled, false, SENSORHUB_ACCELEROMETER_ENABLE_BIT);
-
- INFO("Disable accelerometer sensor");
- return true;
-}
-
-int accel_sensor_device::get_poll_fd()
-{
- return m_node_handle;
-}
-
-bool accel_sensor_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 (!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_sensor_device::set_batch_latency(uint32_t id, unsigned long val)
-{
- return false;
-}
-
-bool accel_sensor_device::set_command(uint32_t id, std::string command, std::string value)
-{
- return false;
-}
-
-bool accel_sensor_device::is_data_ready(void)
-{
- bool ret;
- ret = update_value_input_event();
- return ret;
-}
-
-bool accel_sensor_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 = sensor_device_base::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;
-}
-
-bool accel_sensor_device::get_sensor_data(uint32_t id, sensor_data_t &data)
-{
- data.accuracy = SENSOR_ACCURACY_GOOD;
- data.timestamp = m_fired_time;
- data.value_count = 3;
- data.values[0] = m_x;
- data.values[1] = m_y;
- data.values[2] = m_z;
-
- raw_to_base(data);
-
- return true;
-}
-
-int accel_sensor_device::get_sensor_event(uint32_t id, sensor_event_t **event)
-{
- sensor_event_t *sensor_event;
- sensor_event = (sensor_event_t *)malloc(sizeof(sensor_event_t));
-
- sensor_event->data.accuracy = SENSOR_ACCURACY_GOOD;
- sensor_event->data.timestamp = m_fired_time;
- sensor_event->data.value_count = 3;
- sensor_event->data.values[0] = m_x;
- sensor_event->data.values[1] = m_y;
- sensor_event->data.values[2] = m_z;
-
- raw_to_base(sensor_event->data);
-
- *event = sensor_event;
-
- return sizeof(sensor_event_t);
-}
-
-void accel_sensor_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);
-}
-
-bool accel_sensor_device::get_properties(uint32_t id, sensor_properties_s &properties)
-{
- properties.name = MODEL_NAME;
- properties.vendor = VENDOR;
- properties.min_range = accel_properties.min_range;
- properties.max_range = accel_properties.max_range;
- properties.min_interval = accel_properties.min_interval;
- properties.resolution = accel_properties.resolution;
- properties.fifo_count = accel_properties.fifo_count;
- properties.max_batch_count = accel_properties.max_batch_count;
- return true;
-}
+++ /dev/null
-/*
- * accel_sensor_device
- *
- * Copyright (c) 2014 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_SENSOR_DEVICE_H_
-#define _ACCEL_SENSOR_DEVICE_H_
-
-#include <sensor_device_base.h>
-
-class accel_sensor_device : public sensor_device_base
-{
-public:
- accel_sensor_device();
- virtual ~accel_sensor_device();
-
- int get_poll_fd(void);
- bool get_sensors(std::vector<sensor_handle_t> &sensors);
- bool enable(uint32_t id);
- bool disable(uint32_t id);
- bool set_interval(uint32_t id, unsigned long val);
- bool set_batch_latency(uint32_t id, unsigned long val);
- bool set_command(uint32_t id, std::string command, std::string value);
- bool is_data_ready(void);
- bool get_sensor_data(uint32_t id, sensor_data_t &data);
- int get_sensor_event(uint32_t id, sensor_event_t **event);
- bool get_properties(uint32_t id, sensor_properties_s &properties);
-
-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;
-
- bool update_value_input_event(void);
- void raw_to_base(sensor_data_t &data);
-};
-#endif /*_ACCEL_SENSOR_DEVICE_CLASS_H_*/
#include <sensor_common.h>
-#include "accel/accel_sensor_device.h"
+#include "accel/accel.h"
//#include "gyro/gyro_sensor_device.h"
//#include "magnetic/geo_sensor_device.h"
-#include "proximity/proxi_sensor_device.h"
+#include "proximity/proxi.h"
//#include "light/light_sensor_device.h"
//#include "rotation_vector/rv_raw_sensor_device.h"
//#include "pressure/pressure_sensor_device.h"
--- /dev/null
+/*
+ * proxi_sensor_device
+ *
+ * Copyright (c) 2014 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 <fcntl.h>
+#include <sys/stat.h>
+#include <dirent.h>
+#include <linux/input.h>
+#include <util.h>
+#include "proxi.h"
+
+#define MODEL_NAME "K2HH"
+#define VENDOR "ST Microelectronics"
+#define MIN_RANGE 0
+#define MAX_RANGE 5
+#define RESOLUTION 1
+#define MIN_INTERVAL 1
+#define FIFO_COUNT 0
+#define MAX_BATCH_COUNT 0
+
+static const sensor_properties_s proxi_properties = {
+ name : MODEL_NAME,
+ vendor : VENDOR,
+ min_range : MIN_RANGE,
+ max_range : MAX_RANGE,
+ resolution : RESOLUTION,
+ min_interval : MIN_INTERVAL,
+ fifo_count : FIFO_COUNT,
+ max_batch_count : MAX_BATCH_COUNT,
+};
+
+static const sensor_handle_t handles[] = {
+ {
+ id: 0x1,
+ name: "Proximity Sensor",
+ type: SENSOR_DEVICE_PROXIMITY,
+ event_type: (SENSOR_DEVICE_PROXIMITY << 16) | 0x0001,
+ properties : proxi_properties
+ }
+};
+
+proxi_sensor_device::proxi_sensor_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_RDWR)) < 0) {
+ ERR("accel handle open fail for accel processor, error:%s\n", strerror(errno));
+ throw ENXIO;
+ }
+
+ INFO("Proxi_sensor_device is created!\n");
+}
+
+proxi_sensor_device::~proxi_sensor_device()
+{
+ close(m_node_handle);
+ m_node_handle = -1;
+
+ INFO("Proxi_sensor_device is destroyed!\n");
+}
+
+bool proxi_sensor_device::get_sensors(std::vector<sensor_handle_t> &sensors)
+{
+ int size = ARRAY_SIZE(handles);
+
+ for (int i = 0; i < size; ++i)
+ sensors.push_back(handles[i]);
+
+ return true;
+}
+
+bool proxi_sensor_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_sensor_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;
+}
+
+int proxi_sensor_device::get_poll_fd()
+{
+ return m_node_handle;
+}
+
+bool proxi_sensor_device::set_interval(uint32_t id, unsigned long interval_ms)
+{
+ return true;
+}
+
+bool proxi_sensor_device::set_batch_latency(uint32_t id, unsigned long val)
+{
+ return false;
+}
+
+bool proxi_sensor_device::set_command(uint32_t id, std::string command, std::string value)
+{
+ return false;
+}
+
+bool proxi_sensor_device::is_data_ready(void)
+{
+ bool ret;
+ ret = update_value();
+ return ret;
+}
+
+bool proxi_sensor_device::update_value(void)
+{
+ struct input_event proxi_event;
+ DBG("proxi event detection!");
+
+ int len = read(m_node_handle, &proxi_event, sizeof(proxi_event));
+
+ if (len == -1) {
+ DBG("read(m_node_handle) is error:%s.\n", strerror(errno));
+ 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;
+}
+
+bool proxi_sensor_device::get_sensor_data(uint32_t id, sensor_data_t &data)
+{
+ data.accuracy = SENSOR_ACCURACY_UNDEFINED;
+ data.timestamp = m_fired_time;
+ data.value_count = 1;
+ data.values[0] = m_state;
+
+ return true;
+}
+
+int proxi_sensor_device::get_sensor_event(uint32_t id, sensor_event_t **event)
+{
+ sensor_event_t *sensor_event;
+ sensor_event = (sensor_event_t *)malloc(sizeof(sensor_event_t));
+
+ sensor_event->data.accuracy = SENSOR_ACCURACY_GOOD;
+ sensor_event->data.timestamp = m_fired_time;
+ sensor_event->data.value_count = 1;
+ sensor_event->data.values[0] = m_state;
+
+ *event = sensor_event;
+
+ return sizeof(sensor_event_t);
+}
+
+bool proxi_sensor_device::get_properties(uint32_t id, sensor_properties_s &properties)
+{
+ properties.name = MODEL_NAME;
+ properties.vendor = VENDOR;
+ properties.min_range = proxi_properties.min_range;
+ properties.max_range = proxi_properties.max_range;
+ properties.min_interval = proxi_properties.min_interval;
+ properties.resolution = proxi_properties.resolution;
+ properties.fifo_count = proxi_properties.fifo_count;
+ properties.max_batch_count = proxi_properties.max_batch_count;
+ return true;
+}
--- /dev/null
+/*
+ * proxi_sensor_device
+ *
+ * Copyright (c) 2014 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_SENSOR_DEVICE_H_
+#define _PROXI_SENSOR_DEVICE_H_
+
+#include <sensor_hal.h>
+
+class proxi_sensor_device : public sensor_device
+{
+public:
+ proxi_sensor_device();
+ virtual ~proxi_sensor_device();
+
+ int get_poll_fd(void);
+ bool get_sensors(std::vector<sensor_handle_t> &sensors);
+ bool enable(uint32_t id);
+ bool disable(uint32_t id);
+ bool set_interval(uint32_t id, unsigned long ms_interval);
+ bool set_batch_latency(uint32_t id, unsigned long val);
+ bool set_command(uint32_t id, std::string command, std::string value);
+ bool is_data_ready(void);
+ bool get_sensor_data(uint32_t id, sensor_data_t &data);
+ int get_sensor_event(uint32_t id, sensor_event_t **event);
+ bool get_properties(uint32_t id, sensor_properties_s &properties);
+
+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;
+
+ bool update_value(void);
+};
+#endif /*_PROXI_SENSOR_DEVICE_H_*/
+++ /dev/null
-/*
- * proxi_sensor_device
- *
- * Copyright (c) 2014 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 <fcntl.h>
-#include <sys/stat.h>
-#include <dirent.h>
-#include <linux/input.h>
-#include "proxi_sensor_device.h"
-
-#define MODEL_NAME "K2HH"
-#define VENDOR "ST Microelectronics"
-#define MIN_RANGE 0
-#define MAX_RANGE 5
-#define RESOLUTION 1
-#define MIN_INTERVAL 1
-#define FIFO_COUNT 0
-#define MAX_BATCH_COUNT 0
-
-static const sensor_properties_s proxi_properties = {
- name : MODEL_NAME,
- vendor : VENDOR,
- min_range : MIN_RANGE,
- max_range : MAX_RANGE,
- resolution : RESOLUTION,
- min_interval : MIN_INTERVAL,
- fifo_count : FIFO_COUNT,
- max_batch_count : MAX_BATCH_COUNT,
-};
-
-static const sensor_handle_t handles[] = {
- {
- id: 0x1,
- name: "Proximity Sensor",
- type: SENSOR_DEVICE_PROXIMITY,
- event_type: (SENSOR_DEVICE_PROXIMITY << 16) | 0x0001,
- properties : proxi_properties
- }
-};
-
-proxi_sensor_device::proxi_sensor_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 = 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 (!get_node_info(query, info)) {
- ERR("Failed to get node info");
- throw ENXIO;
- }
-
- 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_RDWR)) < 0) {
- ERR("accel handle open fail for accel processor, error:%s\n", strerror(errno));
- throw ENXIO;
- }
-
- INFO("Proxi_sensor_device is created!\n");
-}
-
-proxi_sensor_device::~proxi_sensor_device()
-{
- close(m_node_handle);
- m_node_handle = -1;
-
- INFO("Proxi_sensor_device is destroyed!\n");
-}
-
-bool proxi_sensor_device::get_sensors(std::vector<sensor_handle_t> &sensors)
-{
- int size = ARRAY_SIZE(handles);
-
- for (int i = 0; i < size; ++i)
- sensors.push_back(handles[i]);
-
- return true;
-}
-
-bool proxi_sensor_device::enable(uint32_t id)
-{
- 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_sensor_device::disable(uint32_t id)
-{
- set_enable_node(m_enable_node, m_sensorhub_controlled, false, SENSORHUB_PROXIMITY_ENABLE_BIT);
-
- INFO("Disable proximity sensor");
- return true;
-}
-
-int proxi_sensor_device::get_poll_fd()
-{
- return m_node_handle;
-}
-
-bool proxi_sensor_device::set_interval(uint32_t id, unsigned long interval_ms)
-{
- return true;
-}
-
-bool proxi_sensor_device::set_batch_latency(uint32_t id, unsigned long val)
-{
- return false;
-}
-
-bool proxi_sensor_device::set_command(uint32_t id, std::string command, std::string value)
-{
- return false;
-}
-
-bool proxi_sensor_device::is_data_ready(void)
-{
- bool ret;
- ret = update_value();
- return ret;
-}
-
-bool proxi_sensor_device::update_value(void)
-{
- struct input_event proxi_event;
- DBG("proxi event detection!");
-
- int len = read(m_node_handle, &proxi_event, sizeof(proxi_event));
-
- if (len == -1) {
- DBG("read(m_node_handle) is error:%s.\n", strerror(errno));
- return false;
- }
-
- if ((proxi_event.type == EV_ABS) && (proxi_event.code == ABS_DISTANCE)) {
- m_state = proxi_event.value;
- m_fired_time = sensor_device_base::get_timestamp(&proxi_event.time);
-
- DBG("m_state = %d, time = %lluus", m_state, m_fired_time);
-
- return true;
- }
-
- return false;
-}
-
-bool proxi_sensor_device::get_sensor_data(uint32_t id, sensor_data_t &data)
-{
- data.accuracy = SENSOR_ACCURACY_UNDEFINED;
- data.timestamp = m_fired_time;
- data.value_count = 1;
- data.values[0] = m_state;
-
- return true;
-}
-
-int proxi_sensor_device::get_sensor_event(uint32_t id, sensor_event_t **event)
-{
- sensor_event_t *sensor_event;
- sensor_event = (sensor_event_t *)malloc(sizeof(sensor_event_t));
-
- sensor_event->data.accuracy = SENSOR_ACCURACY_GOOD;
- sensor_event->data.timestamp = m_fired_time;
- sensor_event->data.value_count = 1;
- sensor_event->data.values[0] = m_state;
-
- *event = sensor_event;
-
- return sizeof(sensor_event_t);
-}
-
-bool proxi_sensor_device::get_properties(uint32_t id, sensor_properties_s &properties)
-{
- properties.name = MODEL_NAME;
- properties.vendor = VENDOR;
- properties.min_range = proxi_properties.min_range;
- properties.max_range = proxi_properties.max_range;
- properties.min_interval = proxi_properties.min_interval;
- properties.resolution = proxi_properties.resolution;
- properties.fifo_count = proxi_properties.fifo_count;
- properties.max_batch_count = proxi_properties.max_batch_count;
- return true;
-}
+++ /dev/null
-/*
- * proxi_sensor_device
- *
- * Copyright (c) 2014 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_SENSOR_DEVICE_H_
-#define _PROXI_SENSOR_DEVICE_H_
-
-#include <sensor_device_base.h>
-
-class proxi_sensor_device : public sensor_device_base
-{
-public:
- proxi_sensor_device();
- virtual ~proxi_sensor_device();
-
- int get_poll_fd(void);
- bool get_sensors(std::vector<sensor_handle_t> &sensors);
- bool enable(uint32_t id);
- bool disable(uint32_t id);
- bool set_interval(uint32_t id, unsigned long ms_interval);
- bool set_batch_latency(uint32_t id, unsigned long val);
- bool set_command(uint32_t id, std::string command, std::string value);
- bool is_data_ready(void);
- bool get_sensor_data(uint32_t id, sensor_data_t &data);
- int get_sensor_event(uint32_t id, sensor_event_t **event);
- bool get_properties(uint32_t id, sensor_properties_s &properties);
-
-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;
-
- bool update_value(void);
-};
-#endif /*_PROXI_SENSOR_DEVICE_H_*/
+++ /dev/null
-/*
- * libsensord-share
- *
- * Copyright (c) 2014 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 <sensor_device_base.h>
-#include <dirent.h>
-#include <string.h>
-#include <fstream>
-
-using std::ifstream;
-using std::ofstream;
-using std::fstream;
-using std::string;
-
-sensor_device_base::sensor_device_base()
-{
-}
-
-sensor_device_base::~sensor_device_base()
-{
-}
-
-unsigned long long sensor_device_base::get_timestamp(void)
-{
- struct timespec t;
- clock_gettime(CLOCK_MONOTONIC, &t);
- return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
-}
-
-unsigned long long sensor_device_base::get_timestamp(timeval *t)
-{
- if (!t) {
- ERR("t is NULL");
- return 0;
- }
-
- return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec);
-}
-
-bool sensor_device_base::is_sensorhub_controlled(const string &key)
-{
- string key_node = string("/sys/class/sensors/ssp_sensor/") + key;
-
- if (access(key_node.c_str(), F_OK) == 0)
- return true;
-
- return false;
-}
-
-bool sensor_device_base::get_node_info(const node_info_query &query, node_info &info)
-{
- bool ret = false;
- int method;
- string device_num;
-
- if (!get_input_method(query.key, method, device_num)) {
- ERR("Failed to get input method for %s", query.key.c_str());
- return false;
- }
-
- info.method = method;
-
- if (method == IIO_METHOD) {
- if (query.sensorhub_controlled)
- ret = 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);
- } else {
- if (query.sensorhub_controlled)
- ret = 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 ret;
-}
-
-
-void sensor_device_base::show_node_info(node_info &info)
-{
- if (info.data_node_path.size())
- INFO("Data node: %s", info.data_node_path.c_str());
- if (info.enable_node_path.size())
- INFO("Enable node: %s", info.enable_node_path.c_str());
- if (info.interval_node_path.size())
- INFO("Interval node: %s", info.interval_node_path.c_str());
- if (info.buffer_enable_node_path.size())
- INFO("Buffer enable node: %s", info.buffer_enable_node_path.c_str());
- if (info.buffer_length_node_path.size())
- INFO("Buffer length node: %s", info.buffer_length_node_path.c_str());
- if (info.trigger_node_path.size())
- INFO("Trigger node: %s", info.trigger_node_path.c_str());
-}
-
-bool sensor_device_base::get_iio_node_info(const string& enable_node_name, const string& device_num, node_info &info)
-{
- const string base_dir = string("/sys/bus/iio/devices/iio:device") + device_num + string("/");
-
- info.data_node_path = string("/dev/iio:device") + device_num;
- info.enable_node_path = base_dir + enable_node_name;
- info.interval_node_path = base_dir + string("sampling_frequency");
- info.buffer_enable_node_path = base_dir + string("buffer/enable");
- info.buffer_length_node_path = base_dir + string("buffer/length");
- info.trigger_node_path = base_dir + string("trigger/current_trigger");
-
- return true;
-}
-
-bool sensor_device_base::get_sensorhub_iio_node_info(const string &interval_node_name, const string& device_num, node_info &info)
-{
- const string base_dir = string("/sys/bus/iio/devices/iio:device") + device_num + string("/");
- const string hub_dir = "/sys/class/sensors/ssp_sensor/";
-
- info.data_node_path = string("/dev/iio:device") + device_num;
- info.enable_node_path = hub_dir + string("enable");
- info.interval_node_path = hub_dir + interval_node_name;
- info.buffer_enable_node_path = base_dir + string("buffer/enable");
- info.buffer_length_node_path = base_dir + string("buffer/length");
- return true;
-}
-
-bool sensor_device_base::get_input_event_node_info(const string& device_num, node_info &info)
-{
- string base_dir;
- string event_num;
-
- base_dir = string("/sys/class/input/input") + device_num + string("/");
-
- if (!get_event_num(base_dir, event_num))
- return false;
-
- info.data_node_path = string("/dev/input/event") + event_num;
-
- info.enable_node_path = base_dir + string("enable");
- info.interval_node_path = base_dir + string("poll_delay");
- return true;
-}
-
-bool sensor_device_base::get_sensorhub_input_event_node_info(const string &interval_node_name, const string& device_num, node_info &info)
-{
- const string base_dir = "/sys/class/sensors/ssp_sensor/";
- string event_num;
-
- string input_dir = string("/sys/class/input/input") + device_num + string("/");
-
- if (!get_event_num(input_dir, event_num))
- return false;
-
- info.data_node_path = string("/dev/input/event") + event_num;
- info.enable_node_path = base_dir + string("enable");
- info.interval_node_path = base_dir + interval_node_name;
- return true;
-}
-
-bool sensor_device_base::set_node_value(const string &node_path, int value)
-{
- ofstream node(node_path, ofstream::binary);
-
- if (!node)
- return false;
-
- node << value;
-
- return true;
-}
-
-bool sensor_device_base::set_node_value(const string &node_path, unsigned long long value)
-{
- ofstream node(node_path, ofstream::binary);
-
- if (!node)
- return false;
-
- node << value;
-
- return true;
-}
-
-
-bool sensor_device_base::get_node_value(const string &node_path, int &value)
-{
- ifstream node(node_path, ifstream::binary);
-
- if (!node)
- return false;
-
- node >> value;
-
- return true;
-}
-
-bool sensor_device_base::set_enable_node(const string &node_path, bool sensorhub_controlled, bool enable, int enable_bit)
-{
- int prev_status, status;
-
- if (!get_node_value(node_path, prev_status)) {
- ERR("Failed to get node: %s", node_path.c_str());
- return false;
- }
-
- int _enable_bit = sensorhub_controlled ? enable_bit : 0;
-
- if (enable)
- status = prev_status | (1 << _enable_bit);
- else
- status = prev_status & (~(1 << _enable_bit));
-
- if (!set_node_value(node_path, status)) {
- ERR("Failed to set node: %s", node_path.c_str());
- return false;
- }
-
- return true;
-}
-
-bool sensor_device_base::get_event_num(const string &input_path, string &event_num)
-{
- const string event_prefix = "event";
- DIR *dir = NULL;
- struct dirent *dir_entry = NULL;
- string node_name;
- bool find = false;
-
- dir = opendir(input_path.c_str());
- if (!dir) {
- ERR("Failed to open dir: %s", input_path.c_str());
- return false;
- }
-
- int prefix_size = event_prefix.size();
-
- while (!find && (dir_entry = readdir(dir))) {
- node_name = 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;
- }
- }
-
- closedir(dir);
-
- return find;
-}
-
-bool sensor_device_base::get_input_method(const string &key, int &method, string &device_num)
-{
- input_method_info input_info[2] = {
- {INPUT_EVENT_METHOD, "/sys/class/input/", "input"},
- {IIO_METHOD, "/sys/bus/iio/devices/", "iio:device"}
- };
-
- const int input_info_len = sizeof(input_info)/sizeof(input_info[0]);
- size_t prefix_size;
- string name_node, name;
- string d_name;
- DIR *dir = NULL;
- struct dirent *dir_entry = NULL;
- bool find = false;
-
- for (int i = 0; i < input_info_len; ++i) {
-
- prefix_size = input_info[i].prefix.size();
-
- dir = opendir(input_info[i].dir_path.c_str());
- if (!dir) {
- ERR("Failed to open dir: %s", input_info[i].dir_path.c_str());
- return false;
- }
-
- find = false;
-
- while (!find && (dir_entry = readdir(dir))) {
- d_name = 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");
-
- ifstream infile(name_node.c_str());
- if (!infile)
- continue;
-
- infile >> name;
-
- if (name == key) {
- device_num = d_name.substr(prefix_size, d_name.size() - prefix_size);
- find = true;
- method = input_info[i].method;
- break;
- }
- }
- }
-
- closedir(dir);
-
- if (find)
- break;
- }
-
- return find;
-}
+++ /dev/null
-/*
- * libsensord-share
- *
- * Copyright (c) 2014 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_DEVICE_BASE_H_
-#define _SENSOR_DEVICE_BASE_H_
-#include <sys/time.h>
-#include <sensor_logs.h>
-#include <string>
-#include <sensor_hal.h>
-
-/*
-* As of Linux 3.4, there is a new EVIOCSCLOCKID ioctl to set the desired clock
-* Current kernel-headers package doesn't have it so we should define it here.
-*/
-
-#ifndef EVIOCSCLOCKID
-/* Set clockid to be used for timestamps */
-#define EVIOCSCLOCKID _IOW('E', 0xa0, int)
-#endif
-
-typedef struct {
- int method;
- std::string data_node_path;
- std::string enable_node_path;
- std::string interval_node_path;
- std::string buffer_enable_node_path;
- std::string buffer_length_node_path;
- std::string trigger_node_path;
-} node_info;
-
-typedef struct {
- bool sensorhub_controlled;
- std::string sensor_type;
- std::string key;
- std::string iio_enable_node_name;
- std::string sensorhub_interval_node_name;
-} node_info_query;
-
-enum input_method {
- IIO_METHOD = 0,
- INPUT_EVENT_METHOD = 1,
-};
-
-typedef struct {
- int method;
- std::string dir_path;
- std::string prefix;
-} input_method_info;
-
-class sensor_device_base : public sensor_device
-{
-public:
- sensor_device_base();
- virtual ~sensor_device_base();
-
-protected:
- bool set_enable_node(const std::string &node_path, bool sensorhub_controlled, bool enable, int enable_bit = 0);
-
- static unsigned long long get_timestamp(void);
- static unsigned long long get_timestamp(timeval *t);
- static bool is_sensorhub_controlled(const std::string &key);
- static bool get_node_info(const node_info_query &query, node_info &info);
- static void show_node_info(node_info &info);
- static bool set_node_value(const std::string &node_path, int value);
- static bool set_node_value(const std::string &node_path, unsigned long long value);
- static bool get_node_value(const std::string &node_path, int &value);
-private:
- static bool get_event_num(const std::string &node_path, std::string &event_num);
- static bool get_input_method(const std::string &key, int &method, std::string &device_num);
-
- static bool get_iio_node_info(const std::string& enable_node_name, const std::string& device_num, node_info &info);
- static bool get_sensorhub_iio_node_info(const std::string &interval_node_name, const std::string& device_num, node_info &info);
- static bool get_input_event_node_info(const std::string& device_num, node_info &info);
- static bool get_sensorhub_input_event_node_info(const std::string &interval_node_name, const std::string& device_num, node_info &info);
-};
-#endif /*_SENSOR_DEVICE_BASE_H_*/
--- /dev/null
+/*
+ * libsensord-share
+ *
+ * Copyright (c) 2014 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 <dirent.h>
+#include <string.h>
+#include <fstream>
+#include <util.h>
+
+using std::ifstream;
+using std::ofstream;
+using std::fstream;
+using std::string;
+
+static bool get_event_num(const string &input_path, string &event_num)
+{
+ const string event_prefix = "event";
+ DIR *dir = NULL;
+ struct dirent *dir_entry = NULL;
+ string node_name;
+ bool find = false;
+
+ dir = opendir(input_path.c_str());
+ if (!dir) {
+ ERR("Failed to open dir: %s", input_path.c_str());
+ return false;
+ }
+
+ int prefix_size = event_prefix.size();
+
+ while (!find && (dir_entry = readdir(dir))) {
+ node_name = 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;
+ }
+ }
+
+ closedir(dir);
+
+ return find;
+}
+
+static bool get_iio_node_info(const string& enable_node_name, const string& device_num, node_info &info)
+{
+ const string base_dir = string("/sys/bus/iio/devices/iio:device") + device_num + string("/");
+
+ info.data_node_path = string("/dev/iio:device") + device_num;
+ info.enable_node_path = base_dir + enable_node_name;
+ info.interval_node_path = base_dir + string("sampling_frequency");
+ info.buffer_enable_node_path = base_dir + string("buffer/enable");
+ info.buffer_length_node_path = base_dir + string("buffer/length");
+ info.trigger_node_path = base_dir + string("trigger/current_trigger");
+
+ return true;
+}
+
+static bool get_sensorhub_iio_node_info(const string &interval_node_name, const string& device_num, node_info &info)
+{
+ const string base_dir = string("/sys/bus/iio/devices/iio:device") + device_num + string("/");
+ const string hub_dir = "/sys/class/sensors/ssp_sensor/";
+
+ info.data_node_path = string("/dev/iio:device") + device_num;
+ info.enable_node_path = hub_dir + string("enable");
+ info.interval_node_path = hub_dir + interval_node_name;
+ info.buffer_enable_node_path = base_dir + string("buffer/enable");
+ info.buffer_length_node_path = base_dir + string("buffer/length");
+ return true;
+}
+
+static bool get_input_event_node_info(const string& device_num, node_info &info)
+{
+ string base_dir;
+ string event_num;
+
+ base_dir = string("/sys/class/input/input") + device_num + string("/");
+
+ if (!get_event_num(base_dir, event_num))
+ return false;
+
+ info.data_node_path = string("/dev/input/event") + event_num;
+
+ info.enable_node_path = base_dir + string("enable");
+ info.interval_node_path = base_dir + string("poll_delay");
+ return true;
+}
+
+static bool get_sensorhub_input_event_node_info(const string &interval_node_name, const string& device_num, node_info &info)
+{
+ const string base_dir = "/sys/class/sensors/ssp_sensor/";
+ string event_num;
+
+ string input_dir = string("/sys/class/input/input") + device_num + string("/");
+
+ if (!get_event_num(input_dir, event_num))
+ return false;
+
+ info.data_node_path = string("/dev/input/event") + event_num;
+ info.enable_node_path = base_dir + string("enable");
+ info.interval_node_path = base_dir + interval_node_name;
+ return true;
+}
+
+static bool get_node_value(const string &node_path, int &value)
+{
+ ifstream node(node_path, ifstream::binary);
+
+ if (!node)
+ return false;
+
+ node >> value;
+
+ return true;
+}
+
+static bool get_input_method(const string &key, int &method, string &device_num)
+{
+ input_method_info input_info[2] = {
+ {INPUT_EVENT_METHOD, "/sys/class/input/", "input"},
+ {IIO_METHOD, "/sys/bus/iio/devices/", "iio:device"}
+ };
+
+ const int input_info_len = sizeof(input_info)/sizeof(input_info[0]);
+ size_t prefix_size;
+ string name_node, name;
+ string d_name;
+ DIR *dir = NULL;
+ struct dirent *dir_entry = NULL;
+ bool find = false;
+
+ for (int i = 0; i < input_info_len; ++i) {
+
+ prefix_size = input_info[i].prefix.size();
+
+ dir = opendir(input_info[i].dir_path.c_str());
+ if (!dir) {
+ ERR("Failed to open dir: %s", input_info[i].dir_path.c_str());
+ return false;
+ }
+
+ find = false;
+
+ while (!find && (dir_entry = readdir(dir))) {
+ d_name = 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");
+
+ ifstream infile(name_node.c_str());
+ if (!infile)
+ continue;
+
+ infile >> name;
+
+ if (name == key) {
+ device_num = d_name.substr(prefix_size, d_name.size() - prefix_size);
+ find = true;
+ method = input_info[i].method;
+ break;
+ }
+ }
+ }
+
+ closedir(dir);
+
+ if (find)
+ break;
+ }
+
+ return find;
+}
+
+bool util::set_enable_node(const string &node_path, bool sensorhub_controlled, bool enable, int enable_bit)
+{
+ int prev_status, status;
+
+ if (!get_node_value(node_path, prev_status)) {
+ ERR("Failed to get node: %s", node_path.c_str());
+ return false;
+ }
+
+ int _enable_bit = sensorhub_controlled ? enable_bit : 0;
+
+ if (enable)
+ status = prev_status | (1 << _enable_bit);
+ else
+ status = prev_status & (~(1 << _enable_bit));
+
+ if (!set_node_value(node_path, status)) {
+ ERR("Failed to set node: %s", node_path.c_str());
+ return false;
+ }
+
+ return true;
+}
+
+unsigned long long util::get_timestamp(void)
+{
+ struct timespec t;
+ clock_gettime(CLOCK_MONOTONIC, &t);
+ return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
+}
+
+unsigned long long util::get_timestamp(timeval *t)
+{
+ if (!t) {
+ ERR("t is NULL");
+ return 0;
+ }
+
+ return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec);
+}
+
+bool util::is_sensorhub_controlled(const string &key)
+{
+ string key_node = string("/sys/class/sensors/ssp_sensor/") + key;
+
+ if (access(key_node.c_str(), F_OK) == 0)
+ return true;
+
+ return false;
+}
+
+bool util::get_node_info(const node_info_query &query, node_info &info)
+{
+ bool ret = false;
+ int method;
+ string device_num;
+
+ if (!get_input_method(query.key, method, device_num)) {
+ ERR("Failed to get input method for %s", query.key.c_str());
+ return false;
+ }
+
+ info.method = method;
+
+ if (method == IIO_METHOD) {
+ if (query.sensorhub_controlled)
+ ret = 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);
+ } else {
+ if (query.sensorhub_controlled)
+ ret = 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 ret;
+}
+
+
+void util::show_node_info(node_info &info)
+{
+ if (info.data_node_path.size())
+ INFO("Data node: %s", info.data_node_path.c_str());
+ if (info.enable_node_path.size())
+ INFO("Enable node: %s", info.enable_node_path.c_str());
+ if (info.interval_node_path.size())
+ INFO("Interval node: %s", info.interval_node_path.c_str());
+ if (info.buffer_enable_node_path.size())
+ INFO("Buffer enable node: %s", info.buffer_enable_node_path.c_str());
+ if (info.buffer_length_node_path.size())
+ INFO("Buffer length node: %s", info.buffer_length_node_path.c_str());
+ if (info.trigger_node_path.size())
+ INFO("Trigger node: %s", info.trigger_node_path.c_str());
+}
+
+bool util::set_node_value(const string &node_path, int value)
+{
+ ofstream node(node_path, ofstream::binary);
+
+ if (!node)
+ return false;
+
+ node << value;
+
+ return true;
+}
+
+bool util::set_node_value(const string &node_path, unsigned long long value)
+{
+ ofstream node(node_path, ofstream::binary);
+
+ if (!node)
+ return false;
+
+ node << value;
+
+ return true;
+}
+
--- /dev/null
+/*
+ * libsensord-share
+ *
+ * Copyright (c) 2014 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_UTIL_H_
+#define _SENSOR_UTIL_H_
+
+#include <sys/time.h>
+#include <sensor_logs.h>
+#include <string>
+#include <sensor_hal.h>
+
+typedef struct {
+ int method;
+ std::string data_node_path;
+ std::string enable_node_path;
+ std::string interval_node_path;
+ std::string buffer_enable_node_path;
+ std::string buffer_length_node_path;
+ std::string trigger_node_path;
+} node_info;
+
+typedef struct {
+ bool sensorhub_controlled;
+ std::string sensor_type;
+ std::string key;
+ std::string iio_enable_node_name;
+ std::string sensorhub_interval_node_name;
+} node_info_query;
+
+enum input_method {
+ IIO_METHOD = 0,
+ INPUT_EVENT_METHOD = 1,
+};
+
+typedef struct {
+ int method;
+ std::string dir_path;
+ std::string prefix;
+} input_method_info;
+
+namespace util {
+ bool set_enable_node(const std::string &node_path, bool sensorhub_controlled, bool enable, int enable_bit = 0);
+
+ unsigned long long get_timestamp(void);
+ unsigned long long get_timestamp(timeval *t);
+
+ bool is_sensorhub_controlled(const std::string &key);
+ bool get_node_info(const node_info_query &query, node_info &info);
+ void show_node_info(node_info &info);
+ bool set_node_value(const std::string &node_path, int value);
+ bool set_node_value(const std::string &node_path, unsigned long long value);
+}
+#endif /*_SENSOR_UTIL_H_*/