2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <sys/types.h>
22 #include <linux/input.h>
24 #include <sensor_logs.h>
28 #define MODEL_NAME "IMS1911"
33 #define MIN_INTERVAL 1
34 #define MAX_BATCH_COUNT 0
36 #define SENSORHUB_PROXIMITY_ENABLE_BIT 7
38 static const sensor_info_t sensor_info = {
40 name: "Proximity Sensor",
41 type: SENSOR_DEVICE_PROXIMITY,
42 event_type: (SENSOR_DEVICE_PROXIMITY << 16) | 0x0001,
43 model_name: MODEL_NAME,
47 resolution: RESOLUTION,
48 min_interval: MIN_INTERVAL,
49 max_batch_count: MAX_BATCH_COUNT,
50 wakeup_supported: false
53 std::vector<uint32_t> proxi_device::event_ids;
55 proxi_device::proxi_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 = util::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 (!util::get_node_info(query, info)) {
73 ERR("Failed to get node info");
77 util::show_node_info(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", strerror(errno));
87 INFO("proxi_device is created!");
90 proxi_device::~proxi_device()
95 INFO("proxi_device is destroyed!");
98 int proxi_device::get_poll_fd()
100 return m_node_handle;
103 int proxi_device::get_sensors(const sensor_info_t **sensors)
105 *sensors = &sensor_info;
110 bool proxi_device::enable(uint32_t id)
112 util::set_enable_node(m_enable_node, m_sensorhub_controlled, true, SENSORHUB_PROXIMITY_ENABLE_BIT);
115 INFO("Enable proximity sensor");
119 bool proxi_device::disable(uint32_t id)
121 util::set_enable_node(m_enable_node, m_sensorhub_controlled, false, SENSORHUB_PROXIMITY_ENABLE_BIT);
123 INFO("Disable proximity sensor");
127 bool proxi_device::set_interval(uint32_t id, unsigned long interval_ms)
132 bool proxi_device::set_batch_latency(uint32_t id, unsigned long val)
137 bool proxi_device::set_attribute_int(uint32_t id, int32_t attribute, int32_t value)
142 bool proxi_device::set_attribute_str(uint32_t id, int32_t attribute, char *value, int value_len)
147 bool proxi_device::update_value_input_event(void)
149 struct input_event proxi_event;
150 DBG("proxi event detection!");
152 int len = read(m_node_handle, &proxi_event, sizeof(proxi_event));
155 DBG("read(m_node_handle) is error:%s", strerror(errno));
159 if ((proxi_event.type == EV_ABS) && (proxi_event.code == ABS_DISTANCE)) {
160 m_state = proxi_event.value;
161 m_fired_time = util::get_timestamp(&proxi_event.time);
163 DBG("m_state = %d, time = %lluus", m_state, m_fired_time);
171 int proxi_device::read_fd(uint32_t **ids)
173 if (!update_value_input_event()) {
174 DBG("Failed to update value");
179 event_ids.push_back(sensor_info.id);
181 *ids = &event_ids[0];
183 return event_ids.size();
186 int proxi_device::get_data(uint32_t id, sensor_data_t **data, int *length)
189 sensor_data_t *sensor_data;
190 sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t));
192 sensor_data->accuracy = SENSOR_ACCURACY_GOOD;
193 sensor_data->timestamp = m_fired_time;
194 sensor_data->value_count = 1;
195 sensor_data->values[0] = m_state;
198 *length = sizeof(sensor_data_t);
203 bool proxi_device::flush(uint32_t id)