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>
27 #include <iio_common.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 #define INITIAL_TIME -1
40 #define GAUSS_TO_UTESLA(val) ((val) * 100.0f)
42 geo_sensor_hal::geo_sensor_hal()
43 : m_polling_interval(POLL_1HZ_MS)
48 , m_fired_time(INITIAL_TIME)
51 const string sensorhub_interval_node_name = "mag_poll_delay";
52 csensor_config &config = csensor_config::get_instance();
54 node_path_info_query query;
56 int input_method = IIO_METHOD;
58 if (!get_model_properties(SENSOR_TYPE_MAGNETIC, m_model_id, input_method)) {
59 ERR("Failed to find model_properties");
64 query.input_method = input_method;
65 query.sensorhub_controlled = m_sensorhub_controlled = is_sensorhub_controlled(sensorhub_interval_node_name);
66 query.sensor_type = SENSOR_TYPE_MAGNETIC;
67 query.input_event_key = "geomagnetic_sensor";
68 query.iio_enable_node_name = "geomagnetic_enable";
69 query.sensorhub_interval_node_name = sensorhub_interval_node_name;
71 if (!get_node_path_info(query, info)) {
72 ERR("Failed to get node info");
76 show_node_path_info(info);
78 m_data_node = info.data_node_path;
79 m_enable_node = info.enable_node_path;
80 m_interval_node = info.interval_node_path;
82 if (input_method == IIO_METHOD) {
83 m_geo_dir = info.base_dir;
84 m_x_node = m_geo_dir + string(X_RAW_VAL_NODE);
85 m_y_node = m_geo_dir + string(Y_RAW_VAL_NODE);
86 m_z_node = m_geo_dir + string(Z_RAW_VAL_NODE);
87 m_x_scale_node = m_geo_dir + string(X_SCALE_NODE);
88 m_y_scale_node = m_geo_dir + string(Y_SCALE_NODE);
89 m_z_scale_node = m_geo_dir + string(Z_SCALE_NODE);
90 INFO("Raw data node X: %s", m_x_node.c_str());
91 INFO("Raw data node Y: %s", m_y_node.c_str());
92 INFO("Raw data node Z: %s", m_z_node.c_str());
93 INFO("scale node X: %s", m_x_scale_node.c_str());
94 INFO("scale node Y: %s", m_y_scale_node.c_str());
95 INFO("scale node Z: %s", m_z_scale_node.c_str());
98 if (!config.get(SENSOR_TYPE_MAGNETIC, m_model_id, ELEMENT_VENDOR, m_vendor)) {
99 ERR("[VENDOR] is empty\n");
103 INFO("m_vendor = %s", m_vendor.c_str());
105 if (!config.get(SENSOR_TYPE_MAGNETIC, m_model_id, ELEMENT_NAME, m_chip_name)) {
106 ERR("[NAME] is empty\n");
112 INFO("m_chip_name = %s\n",m_chip_name.c_str());
113 INFO("m_raw_data_unit = %f\n", m_raw_data_unit);
114 INFO("geo_sensor_hal is created!\n");
118 geo_sensor_hal::~geo_sensor_hal()
120 if (m_node_handle > 0)
121 close(m_node_handle);
124 INFO("geo_sensor is destroyed!\n");
127 string geo_sensor_hal::get_model_id(void)
132 sensor_type_t geo_sensor_hal::get_type(void)
134 return GEOMAGNETIC_SENSOR;
137 bool geo_sensor_hal::enable(void)
139 m_fired_time = INITIAL_TIME;
140 INFO("Geo sensor real starting");
144 bool geo_sensor_hal::disable(void)
146 INFO("Geo sensor real stopping");
150 bool geo_sensor_hal::set_interval(unsigned long val)
152 INFO("Polling interval cannot be changed.");
157 bool geo_sensor_hal::update_value(void)
159 int raw_values[3] = {0,};
160 ifstream temp_handle;
162 if (!read_node_value<int>(m_x_node, raw_values[0]))
164 if (!read_node_value<int>(m_y_node, raw_values[1]))
166 if (!read_node_value<int>(m_z_node, raw_values[2]))
169 m_x = GAUSS_TO_UTESLA(raw_values[0] * m_x_scale);
170 m_y = GAUSS_TO_UTESLA(raw_values[1] * m_y_scale);
171 m_z = GAUSS_TO_UTESLA(raw_values[2] * m_z_scale);
173 m_fired_time = INITIAL_TIME;
174 INFO("x = %d, y = %d, z = %d, time = %lluus", raw_values[0], raw_values[1], raw_values[2], m_fired_time);
175 INFO("x = %f, y = %f, z = %f, time = %lluus", m_x, m_y, m_z, m_fired_time);
180 bool geo_sensor_hal::is_data_ready(bool wait)
183 ret = update_value();
187 int geo_sensor_hal::get_sensor_data(sensor_data_t &data)
189 data.accuracy = SENSOR_ACCURACY_GOOD;
190 data.timestamp = m_fired_time;
191 data.value_count = 3;
192 data.values[0] = (float)m_x;
193 data.values[1] = (float)m_y;
194 data.values[2] = (float)m_z;
198 bool geo_sensor_hal::get_properties(sensor_properties_s &properties)
200 properties.name = m_chip_name;
201 properties.vendor = m_vendor;
202 properties.min_range = m_min_range;
203 properties.max_range = m_max_range;
204 properties.min_interval = 1;
205 properties.resolution = m_raw_data_unit;
206 properties.fifo_count = 0;
207 properties.max_batch_count = 0;
211 bool geo_sensor_hal::init_resources(void)
213 ifstream temp_handle;
215 if (!read_node_value<double>(m_x_scale_node, m_x_scale)) {
216 ERR("Failed to read x scale node");
219 if (!read_node_value<double>(m_y_scale_node, m_y_scale)) {
220 ERR("Failed to read y scale node");
223 if (!read_node_value<double>(m_z_scale_node, m_z_scale)) {
224 ERR("Failed to read y scale node");
227 INFO("Scale Values: %f, %f, %f", m_x_scale, m_y_scale, m_z_scale);
231 extern "C" void *create(void)
233 geo_sensor_hal *inst;
236 inst = new geo_sensor_hal();
238 ERR("geo_sensor_hal class create fail , errno : %d , errstr : %s\n", err, strerror(err));
245 extern "C" void destroy(void *inst)
247 delete (geo_sensor_hal*)inst;