226cdadde7f51a946acf82df95344432e6ef4aeb
[platform/adaptation/tm1/sensor-hal-tm1.git] / src / proxi / proxi.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <linux/input.h>
23 #include <util.h>
24 #include <sensor_logs.h>
25
26 #include "proxi.h"
27
28 #define MODEL_NAME "IMS1911"
29 #define VENDOR "ITM"
30 #define MIN_RANGE 0
31 #define MAX_RANGE 5
32 #define RESOLUTION 1
33 #define MIN_INTERVAL 1
34 #define MAX_BATCH_COUNT 0
35
36 #define SENSORHUB_PROXIMITY_ENABLE_BIT 7
37
38 static const sensor_info_t sensor_info = {
39         id: 0x1,
40         name: "Proximity Sensor",
41         type: SENSOR_DEVICE_PROXIMITY,
42         event_type: (SENSOR_DEVICE_PROXIMITY << 16) | 0x0001,
43         model_name: MODEL_NAME,
44         vendor: VENDOR,
45         min_range: MIN_RANGE,
46         max_range: MAX_RANGE,
47         resolution: RESOLUTION,
48         min_interval: MIN_INTERVAL,
49         max_batch_count: MAX_BATCH_COUNT,
50         wakeup_supported: false
51 };
52
53 std::vector<uint32_t> proxi_device::event_ids;
54
55 proxi_device::proxi_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 = 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;
71
72         if (!util::get_node_info(query, info)) {
73                 ERR("Failed to get node info");
74                 throw ENXIO;
75         }
76
77         util::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", strerror(errno));
84                 throw ENXIO;
85         }
86
87         INFO("proxi_device is created!");
88 }
89
90 proxi_device::~proxi_device()
91 {
92         close(m_node_handle);
93         m_node_handle = -1;
94
95         INFO("proxi_device is destroyed!");
96 }
97
98 int proxi_device::get_poll_fd()
99 {
100         return m_node_handle;
101 }
102
103 int proxi_device::get_sensors(const sensor_info_t **sensors)
104 {
105         *sensors = &sensor_info;
106
107         return 1;
108 }
109
110 bool proxi_device::enable(uint32_t id)
111 {
112         util::set_enable_node(m_enable_node, m_sensorhub_controlled, true, SENSORHUB_PROXIMITY_ENABLE_BIT);
113
114         m_fired_time = 0;
115         INFO("Enable proximity sensor");
116         return true;
117 }
118
119 bool proxi_device::disable(uint32_t id)
120 {
121         util::set_enable_node(m_enable_node, m_sensorhub_controlled, false, SENSORHUB_PROXIMITY_ENABLE_BIT);
122
123         INFO("Disable proximity sensor");
124         return true;
125 }
126
127 bool proxi_device::set_interval(uint32_t id, unsigned long interval_ms)
128 {
129         return true;
130 }
131
132 bool proxi_device::set_batch_latency(uint32_t id, unsigned long val)
133 {
134         return false;
135 }
136
137 bool proxi_device::set_attribute_int(uint32_t id, int32_t attribute, int32_t value)
138 {
139         return false;
140 }
141
142 bool proxi_device::set_attribute_str(uint32_t id, int32_t attribute, char *value, int value_len)
143 {
144         return false;
145 }
146
147 bool proxi_device::update_value_input_event(void)
148 {
149         struct input_event proxi_event;
150         DBG("proxi event detection!");
151
152         int len = read(m_node_handle, &proxi_event, sizeof(proxi_event));
153
154         if (len == -1) {
155                 DBG("read(m_node_handle) is error:%s", strerror(errno));
156                 return false;
157         }
158
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);
162
163                 DBG("m_state = %d, time = %lluus", m_state, m_fired_time);
164
165                 return true;
166         }
167
168         return false;
169 }
170
171 int proxi_device::read_fd(uint32_t **ids)
172 {
173         if (!update_value_input_event()) {
174                 DBG("Failed to update value");
175                 return false;
176         }
177
178         event_ids.clear();
179         event_ids.push_back(sensor_info.id);
180
181         *ids = &event_ids[0];
182
183         return event_ids.size();
184 }
185
186 int proxi_device::get_data(uint32_t id, sensor_data_t **data, int *length)
187 {
188         int remains = 1;
189         sensor_data_t *sensor_data;
190         sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t));
191
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;
196
197         *data = sensor_data;
198         *length = sizeof(sensor_data_t);
199
200         return --remains;
201 }
202
203 bool proxi_device::flush(uint32_t id)
204 {
205         return false;
206 }