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 <csensor_config.h>
24 #include <geo_sensor_hal.h>
25 #include <sys/ioctl.h>
31 #define SENSOR_TYPE_MAGNETIC "MAGNETIC"
32 #define ELEMENT_NAME "NAME"
33 #define ELEMENT_VENDOR "VENDOR"
34 #define ELEMENT_RAW_DATA_UNIT "RAW_DATA_UNIT"
35 #define ELEMENT_MIN_RANGE "MIN_RANGE"
36 #define ELEMENT_MAX_RANGE "MAX_RANGE"
37 #define ATTR_VALUE "value"
39 geo_sensor_hal::geo_sensor_hal()
46 , m_polling_interval(POLL_1HZ_MS)
48 const string sensorhub_interval_node_name = "mag_poll_delay";
49 csensor_config &config = csensor_config::get_instance();
51 node_info_query query;
54 if (!find_model_id(SENSOR_TYPE_MAGNETIC, m_model_id)) {
55 ERR("Failed to find model id");
59 query.sensorhub_controlled = m_sensorhub_controlled = is_sensorhub_controlled(sensorhub_interval_node_name);
60 query.sensor_type = SENSOR_TYPE_MAGNETIC;
61 query.key = "geomagnetic_sensor";
62 query.iio_enable_node_name = "geomagnetic_enable";
63 query.sensorhub_interval_node_name = sensorhub_interval_node_name;
65 bool error = get_node_info(query, info);
67 query.key = "magnetic_sensor";
68 error |= get_node_info(query, info);
71 ERR("Failed to get node info");
77 m_data_node = info.data_node_path;
78 m_enable_node = info.enable_node_path;
79 m_interval_node = info.interval_node_path;
81 if (!config.get(SENSOR_TYPE_MAGNETIC, m_model_id, ELEMENT_VENDOR, m_vendor)) {
82 ERR("[VENDOR] is empty\n");
86 INFO("m_vendor = %s", m_vendor.c_str());
88 if (!config.get(SENSOR_TYPE_MAGNETIC, m_model_id, ELEMENT_NAME, m_chip_name)) {
89 ERR("[NAME] is empty\n");
93 INFO("m_chip_name = %s\n",m_chip_name.c_str());
97 if (!config.get(SENSOR_TYPE_MAGNETIC, m_model_id, ELEMENT_MIN_RANGE, min_range)) {
98 ERR("[MIN_RANGE] is empty\n");
102 m_min_range = (float)min_range;
103 INFO("m_min_range = %f\n",m_min_range);
107 if (!config.get(SENSOR_TYPE_MAGNETIC, m_model_id, ELEMENT_MAX_RANGE, max_range)) {
108 ERR("[MAX_RANGE] is empty\n");
112 m_max_range = (float)max_range;
113 INFO("m_max_range = %f\n",m_max_range);
115 double raw_data_unit;
117 if (!config.get(SENSOR_TYPE_MAGNETIC, m_model_id, ELEMENT_RAW_DATA_UNIT, raw_data_unit)) {
118 ERR("[RAW_DATA_UNIT] is empty\n");
122 m_raw_data_unit = (float)(raw_data_unit);
124 if ((m_node_handle = open(m_data_node.c_str(),O_RDWR)) < 0) {
125 ERR("Failed to open handle(%d)", m_node_handle);
129 int clockId = CLOCK_MONOTONIC;
130 if (ioctl(m_node_handle, EVIOCSCLOCKID, &clockId) != 0)
131 ERR("Fail to set monotonic timestamp for %s", m_data_node.c_str());
133 INFO("m_raw_data_unit = %f\n", m_raw_data_unit);
134 INFO("geo_sensor_hal is created!\n");
137 geo_sensor_hal::~geo_sensor_hal()
139 close(m_node_handle);
142 INFO("geo_sensor is destroyed!\n");
145 string geo_sensor_hal::get_model_id(void)
150 sensor_hal_type_t geo_sensor_hal::get_type(void)
152 return SENSOR_HAL_TYPE_GEOMAGNETIC;
155 bool geo_sensor_hal::enable(void)
159 set_enable_node(m_enable_node, m_sensorhub_controlled, true, SENSORHUB_GEOMAGNETIC_ENABLE_BIT);
160 set_interval(m_polling_interval);
163 INFO("Geo sensor real starting");
167 bool geo_sensor_hal::disable(void)
171 set_enable_node(m_enable_node, m_sensorhub_controlled, false, SENSORHUB_GEOMAGNETIC_ENABLE_BIT);
173 INFO("Geo sensor real stopping");
177 bool geo_sensor_hal::set_interval(unsigned long val)
179 unsigned long long polling_interval_ns;
183 polling_interval_ns = ((unsigned long long)(val) * 1000llu * 1000llu);
185 if (!set_node_value(m_interval_node, polling_interval_ns)) {
186 ERR("Failed to set polling resource: %s\n", m_interval_node.c_str());
190 INFO("Interval is changed from %dms to %dms]", m_polling_interval, val);
191 m_polling_interval = val;
195 bool geo_sensor_hal::update_value(void)
197 int geo_raw[4] = {0,};
199 int read_input_cnt = 0;
200 const int INPUT_MAX_BEFORE_SYN = 10;
201 unsigned long long fired_time = 0;
204 x = y = z = hdst = false;
206 struct input_event geo_input;
207 DBG("geo event detection!");
209 while ((syn == false) && (read_input_cnt < INPUT_MAX_BEFORE_SYN)) {
210 int len = read(m_node_handle, &geo_input, sizeof(geo_input));
211 if (len != sizeof(geo_input)) {
212 ERR("geo_file read fail, read_len = %d\n",len);
218 if (geo_input.type == EV_REL) {
219 switch (geo_input.code) {
221 geo_raw[0] = (int)geo_input.value;
225 geo_raw[1] = (int)geo_input.value;
229 geo_raw[2] = (int)geo_input.value;
233 geo_raw[3] = (int)geo_input.value;
237 ERR("geo_input event[type = %d, code = %d] is unknown.", geo_input.type, geo_input.code);
241 } else if (geo_input.type == EV_SYN) {
243 fired_time = sensor_hal_base::get_timestamp(&geo_input.time);
245 ERR("geo_input event[type = %d, code = %d] is unknown.", geo_input.type, geo_input.code);
250 AUTOLOCK(m_value_mutex);
259 m_hdst = geo_raw[3] - 1; /* accuracy bias: -1 */
261 m_fired_time = fired_time;
263 DBG("m_x = %d, m_y = %d, m_z = %d, m_hdst = %d, time = %lluus", m_x, m_y, m_z, m_hdst, m_fired_time);
268 bool geo_sensor_hal::is_data_ready(void)
271 ret = update_value();
275 int geo_sensor_hal::get_sensor_data(sensor_data_t &data)
277 data.accuracy = (m_hdst == 1) ? 0 : m_hdst; /* hdst 0 and 1 are needed to calibrate */
278 data.timestamp = m_fired_time;
279 data.value_count = 3;
280 data.values[0] = (float)m_x;
281 data.values[1] = (float)m_y;
282 data.values[2] = (float)m_z;
286 bool geo_sensor_hal::get_properties(sensor_properties_s &properties)
288 properties.name = m_chip_name;
289 properties.vendor = m_vendor;
290 properties.min_range = m_min_range;
291 properties.max_range = m_max_range;
292 properties.min_interval = 1;
293 properties.resolution = m_raw_data_unit;
294 properties.fifo_count = 0;
295 properties.max_batch_count = 0;