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.
23 #include <linux/input.h>
24 #include <csensor_config.h>
25 #include <light_sensor_hal.h>
26 #include <sys/ioctl.h>
31 #define SENSOR_TYPE_LIGHT "LIGHT"
32 #define ELEMENT_NAME "NAME"
33 #define ELEMENT_VENDOR "VENDOR"
34 #define ELEMENT_RAW_DATA_UNIT "RAW_DATA_UNIT"
35 #define ELEMENT_RESOLUTION "RESOLUTION"
36 #define ATTR_VALUE "value"
37 #define INITIAL_TIME -1
39 #define INVALID_VALUE -1
40 #define INITIAL_VALUE -1
42 light_sensor_hal::light_sensor_hal()
43 : m_polling_interval(POLL_1HZ_MS)
44 , m_adc(INVALID_VALUE)
45 , m_fired_time(INITIAL_TIME)
46 , m_node_handle(INITIAL_VALUE)
48 const string sensorhub_interval_node_name = "light_poll_delay";
49 csensor_config &config = csensor_config::get_instance();
51 node_info_query query;
54 if (!find_model_id(SENSOR_TYPE_LIGHT, m_model_id)) {
55 ERR("Failed to find model id");
60 query.sensorhub_controlled = m_sensorhub_controlled = is_sensorhub_controlled(sensorhub_interval_node_name);
61 query.sensor_type = SENSOR_TYPE_LIGHT;
62 query.key = "light_sensor";
63 query.iio_enable_node_name = "light_enable";
64 query.sensorhub_interval_node_name = sensorhub_interval_node_name;
66 if (!get_node_info(query, info)) {
67 ERR("Failed to get node info");
73 m_data_node = info.data_node_path;
74 m_enable_node = info.enable_node_path;
75 m_interval_node = info.interval_node_path;
77 if (!config.get(SENSOR_TYPE_LIGHT, m_model_id, ELEMENT_VENDOR, m_vendor)) {
78 ERR("[VENDOR] is empty\n");
82 INFO("m_vendor = %s", m_vendor.c_str());
84 if (!config.get(SENSOR_TYPE_LIGHT, m_model_id, ELEMENT_NAME, m_chip_name)) {
85 ERR("[NAME] is empty\n");
89 INFO("m_chip_name = %s\n",m_chip_name.c_str());
91 if ((m_node_handle = open(m_data_node.c_str(),O_RDWR)) < 0) {
92 ERR("Failed to open handle(%d)", m_node_handle);
96 int clockId = CLOCK_MONOTONIC;
97 if (ioctl(m_node_handle, EVIOCSCLOCKID, &clockId) != 0)
98 ERR("Fail to set monotonic timestamp for %s", m_data_node.c_str());
100 INFO("light_sensor_hal is created!\n");
103 light_sensor_hal::~light_sensor_hal()
105 close(m_node_handle);
108 INFO("light_sensor_hal is destroyed!\n");
111 string light_sensor_hal::get_model_id(void)
117 sensor_hal_type_t light_sensor_hal::get_type(void)
119 return SENSOR_HAL_TYPE_LIGHT;
122 bool light_sensor_hal::enable(void)
126 set_enable_node(m_enable_node, m_sensorhub_controlled, true, SENSORHUB_LIGHT_ENABLE_BIT);
127 set_interval(m_polling_interval);
130 INFO("Light sensor real starting");
134 bool light_sensor_hal::disable(void)
138 set_enable_node(m_enable_node, m_sensorhub_controlled, false, SENSORHUB_LIGHT_ENABLE_BIT);
140 INFO("Light sensor real stopping");
144 bool light_sensor_hal::set_interval(unsigned long val)
146 unsigned long long polling_interval_ns;
150 polling_interval_ns = ((unsigned long long)(val) * 1000llu * 1000llu);
152 if (!set_node_value(m_interval_node, polling_interval_ns)) {
153 ERR("Failed to set polling resource: %s\n", m_interval_node.c_str());
157 INFO("Interval is changed from %dms to %dms]", m_polling_interval, val);
158 m_polling_interval = val;
162 bool light_sensor_hal::update_value(void)
164 unsigned short int adc = INITIAL_VALUE;
166 struct input_event light_event;
167 DBG("light event detection!");
169 int len = read(m_node_handle, &light_event, sizeof(light_event));
171 DBG("read(m_node_handle) is error:%s.\n", strerror(errno));
175 if (light_event.type == EV_ABS && light_event.code == ABS_MISC) {
176 adc = light_event.value;
177 } else if (light_event.type == EV_REL && light_event.code == REL_MISC) {
178 adc = light_event.value - BIAS;
179 } else if (light_event.type == EV_REL && light_event.code == REL_RX) {
180 adc = light_event.value - BIAS;
182 DBG("light input event[type = %d, code = %d] is unknown.", light_event.type, light_event.code);
186 DBG("read event, len : %d, type : %x, code : %x, value : %x",
187 len, light_event.type, light_event.code, light_event.value);
189 DBG("update_value, adc : %d", adc);
191 AUTOLOCK(m_value_mutex);
193 m_fired_time = sensor_hal_base::get_timestamp(&light_event.time);
198 bool light_sensor_hal::is_data_ready(void)
201 ret = update_value();
205 int light_sensor_hal::get_sensor_data(sensor_data_t &data)
207 AUTOLOCK(m_value_mutex);
208 data.accuracy = SENSOR_ACCURACY_GOOD;
209 data.timestamp = m_fired_time;
210 data.value_count = 1;
211 data.values[0] = (float) m_adc;
216 bool light_sensor_hal::get_properties(sensor_properties_s &properties)
218 properties.name = m_chip_name;
219 properties.vendor = m_vendor;
220 properties.min_range = 0;
221 properties.max_range = 65535;
222 properties.min_interval = 1;
223 properties.resolution = 1;
224 properties.fifo_count = 0;
225 properties.max_batch_count = 0;