29410a6ecc957339e94359917c4639b1bba6db2a
[platform/adaptation/tm1/sensor-hal-tm1.git] / src / plugins / proxi / proxi_sensor_device.cpp
1 /*
2  * proxi_sensor_device
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <dirent.h>
22 #include <linux/input.h>
23 #include <proxi_sensor_device.h>
24
25 #define MODEL_NAME "K2HH"
26 #define VENDOR "ST Microelectronics"
27 #define MIN_RANGE 0
28 #define MAX_RANGE 5
29 #define RESOLUTION 1
30 #define MIN_INTERVAL 1
31 #define FIFO_COUNT 0
32 #define MAX_BATCH_COUNT 0
33
34 static const sensor_properties_s proxi_properties = {
35         name : MODEL_NAME,
36         vendor : VENDOR,
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,
43 };
44
45 static const sensor_handle_t handles[] = {
46         {
47                 id: 0x1,
48                 name: "Proximity Sensor",
49                 type: SENSOR_DEVICE_PROXIMITY,
50                 event_type: (SENSOR_DEVICE_PROXIMITY << 16) | 0x0001,
51                 properties : proxi_properties
52         }
53 };
54
55 proxi_sensor_device::proxi_sensor_device()
56 : m_node_handle(-1)
57 , m_state(-1)
58 , m_fired_time(0)
59 , m_sensorhub_controlled(false)
60 {
61         const std::string sensorhub_interval_node_name = "prox_poll_delay";
62
63         node_info_query query;
64         node_info info;
65
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;
71
72         if (!get_node_info(query, info)) {
73                 ERR("Failed to get node info");
74                 throw ENXIO;
75         }
76
77         show_node_info(info);
78
79         m_data_node = info.data_node_path;
80         m_enable_node = info.enable_node_path;
81
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));
84                 throw ENXIO;
85         }
86
87         INFO("Proxi_sensor_device is created!\n");
88 }
89
90 proxi_sensor_device::~proxi_sensor_device()
91 {
92         close(m_node_handle);
93         m_node_handle = -1;
94
95         INFO("Proxi_sensor_device is destroyed!\n");
96 }
97
98 bool proxi_sensor_device::get_sensors(std::vector<sensor_handle_t> &sensors)
99 {
100         int size = ARRAY_SIZE(handles);
101
102         for (int i = 0; i < size; ++i)
103                 sensors.push_back(handles[i]);
104
105         return true;
106 }
107
108 bool proxi_sensor_device::enable(uint32_t id)
109 {
110         set_enable_node(m_enable_node, m_sensorhub_controlled, true, SENSORHUB_PROXIMITY_ENABLE_BIT);
111
112         m_fired_time = 0;
113         INFO("Enable proximity sensor");
114         return true;
115 }
116
117 bool proxi_sensor_device::disable(uint32_t id)
118 {
119         set_enable_node(m_enable_node, m_sensorhub_controlled, false, SENSORHUB_PROXIMITY_ENABLE_BIT);
120
121         INFO("Disable proximity sensor");
122         return true;
123 }
124
125 int proxi_sensor_device::get_poll_fd()
126 {
127         return m_node_handle;
128 }
129
130 bool proxi_sensor_device::set_interval(uint32_t id, unsigned long interval_ms)
131 {
132         return true;
133 }
134
135 bool proxi_sensor_device::set_batch_latency(uint32_t id, unsigned long val)
136 {
137         return false;
138 }
139
140 bool proxi_sensor_device::set_command(uint32_t id, std::string command, std::string value)
141 {
142         return false;
143 }
144
145 bool proxi_sensor_device::is_data_ready(void)
146 {
147         bool ret;
148         ret = update_value();
149         return ret;
150 }
151
152 bool proxi_sensor_device::update_value(void)
153 {
154         struct input_event proxi_event;
155         DBG("proxi event detection!");
156
157         int len = read(m_node_handle, &proxi_event, sizeof(proxi_event));
158
159         if (len == -1) {
160                 DBG("read(m_node_handle) is error:%s.\n", strerror(errno));
161                 return false;
162         }
163
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);
167
168                 DBG("m_state = %d, time = %lluus", m_state, m_fired_time);
169
170                 return true;
171         }
172
173         return false;
174 }
175
176 bool proxi_sensor_device::get_sensor_data(uint32_t id, sensor_data_t &data)
177 {
178         data.accuracy = SENSOR_ACCURACY_UNDEFINED;
179         data.timestamp = m_fired_time;
180         data.value_count = 1;
181         data.values[0] = m_state;
182
183         return true;
184 }
185
186 int proxi_sensor_device::get_sensor_event(uint32_t id, sensor_event_t **event)
187 {
188         sensor_event_t *sensor_event;
189         sensor_event = (sensor_event_t *)malloc(sizeof(sensor_event_t));
190
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;
195
196         *event = sensor_event;
197
198         return sizeof(sensor_event_t);
199 }
200
201 bool proxi_sensor_device::get_properties(uint32_t id, sensor_properties_s &properties)
202 {
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;
211         return true;
212 }