4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include <linux/input.h>
23 #include "proxi_sensor_device.h"
25 #define MODEL_NAME "K2HH"
26 #define VENDOR "ST Microelectronics"
30 #define MIN_INTERVAL 1
32 #define MAX_BATCH_COUNT 0
34 static const sensor_properties_s proxi_properties = {
37 min_range : MIN_RANGE,
38 max_range : MAX_RANGE,
39 resolution : RESOLUTION,
40 min_interval : MIN_INTERVAL,
41 fifo_count : FIFO_COUNT,
42 max_batch_count : MAX_BATCH_COUNT,
45 static const sensor_handle_t handles[] = {
48 name: "Proximity Sensor",
49 type: SENSOR_DEVICE_PROXIMITY,
50 event_type: (SENSOR_DEVICE_PROXIMITY << 16) | 0x0001,
51 properties : proxi_properties
55 proxi_sensor_device::proxi_sensor_device()
59 , m_sensorhub_controlled(false)
61 const std::string sensorhub_interval_node_name = "prox_poll_delay";
63 node_info_query query;
66 query.sensorhub_controlled = m_sensorhub_controlled = is_sensorhub_controlled(sensorhub_interval_node_name);
67 query.sensor_type = "PROXI";
68 query.key = "proximity_sensor";
69 query.iio_enable_node_name = "proximity_enable";
70 query.sensorhub_interval_node_name = sensorhub_interval_node_name;
72 if (!get_node_info(query, info)) {
73 ERR("Failed to get node info");
79 m_data_node = info.data_node_path;
80 m_enable_node = info.enable_node_path;
82 if ((m_node_handle = open(m_data_node.c_str(), O_RDWR)) < 0) {
83 ERR("accel handle open fail for accel processor, error:%s\n", strerror(errno));
87 INFO("Proxi_sensor_device is created!\n");
90 proxi_sensor_device::~proxi_sensor_device()
95 INFO("Proxi_sensor_device is destroyed!\n");
98 bool proxi_sensor_device::get_sensors(std::vector<sensor_handle_t> &sensors)
100 int size = ARRAY_SIZE(handles);
102 for (int i = 0; i < size; ++i)
103 sensors.push_back(handles[i]);
108 bool proxi_sensor_device::enable(uint32_t id)
110 set_enable_node(m_enable_node, m_sensorhub_controlled, true, SENSORHUB_PROXIMITY_ENABLE_BIT);
113 INFO("Enable proximity sensor");
117 bool proxi_sensor_device::disable(uint32_t id)
119 set_enable_node(m_enable_node, m_sensorhub_controlled, false, SENSORHUB_PROXIMITY_ENABLE_BIT);
121 INFO("Disable proximity sensor");
125 int proxi_sensor_device::get_poll_fd()
127 return m_node_handle;
130 bool proxi_sensor_device::set_interval(uint32_t id, unsigned long interval_ms)
135 bool proxi_sensor_device::set_batch_latency(uint32_t id, unsigned long val)
140 bool proxi_sensor_device::set_command(uint32_t id, std::string command, std::string value)
145 bool proxi_sensor_device::is_data_ready(void)
148 ret = update_value();
152 bool proxi_sensor_device::update_value(void)
154 struct input_event proxi_event;
155 DBG("proxi event detection!");
157 int len = read(m_node_handle, &proxi_event, sizeof(proxi_event));
160 DBG("read(m_node_handle) is error:%s.\n", strerror(errno));
164 if ((proxi_event.type == EV_ABS) && (proxi_event.code == ABS_DISTANCE)) {
165 m_state = proxi_event.value;
166 m_fired_time = sensor_device_base::get_timestamp(&proxi_event.time);
168 DBG("m_state = %d, time = %lluus", m_state, m_fired_time);
176 bool proxi_sensor_device::get_sensor_data(uint32_t id, sensor_data_t &data)
178 data.accuracy = SENSOR_ACCURACY_UNDEFINED;
179 data.timestamp = m_fired_time;
180 data.value_count = 1;
181 data.values[0] = m_state;
186 int proxi_sensor_device::get_sensor_event(uint32_t id, sensor_event_t **event)
188 sensor_event_t *sensor_event;
189 sensor_event = (sensor_event_t *)malloc(sizeof(sensor_event_t));
191 sensor_event->data.accuracy = SENSOR_ACCURACY_GOOD;
192 sensor_event->data.timestamp = m_fired_time;
193 sensor_event->data.value_count = 1;
194 sensor_event->data.values[0] = m_state;
196 *event = sensor_event;
198 return sizeof(sensor_event_t);
201 bool proxi_sensor_device::get_properties(uint32_t id, sensor_properties_s &properties)
203 properties.name = MODEL_NAME;
204 properties.vendor = VENDOR;
205 properties.min_range = proxi_properties.min_range;
206 properties.max_range = proxi_properties.max_range;
207 properties.min_interval = proxi_properties.min_interval;
208 properties.resolution = proxi_properties.resolution;
209 properties.fifo_count = proxi_properties.fifo_count;
210 properties.max_batch_count = proxi_properties.max_batch_count;