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 <sys/ioctl.h>
25 #include <linux/input.h>
27 #include <geo_sensor_hal.h>
28 #include <iio_common.h>
32 using config::CConfig;
34 #define SENSOR_TYPE_MAGNETIC "MAGNETIC"
35 #define ELEMENT_NAME "NAME"
36 #define ELEMENT_VENDOR "VENDOR"
37 #define ATTR_VALUE "value"
39 #define SENSOR_MIN_RANGE -1200
40 #define SENSOR_MAX_RANGE 1200
41 #define INITIAL_TIME 0
42 #define INITIAL_VALUE -1
43 #define GAUSS_TO_UTESLA(val) ((val) * 100)
45 geo_sensor_hal::geo_sensor_hal()
49 , m_polling_interval(POLL_1HZ_MS)
50 , m_fired_time(INITIAL_TIME)
51 , m_sensorhub_supported(false)
55 ERR("check_hw_node() fail");
59 CConfig &config = CConfig::get_instance();
61 if (!config.get(SENSOR_TYPE_MAGNETIC, m_model_id, ELEMENT_VENDOR, m_vendor))
63 ERR("[VENDOR] is empty");
67 INFO("m_vendor = %s", m_vendor.c_str());
69 if (!config.get(SENSOR_TYPE_MAGNETIC, m_model_id, ELEMENT_NAME, m_chip_name))
71 ERR("[NAME] is empty");
75 INFO("m_chip_name = %s", m_chip_name.c_str());
77 if (!init_resources())
80 INFO("geo_sensor_hal is created!");
83 geo_sensor_hal::~geo_sensor_hal()
85 INFO("geo_sensor_hal is destroyed!");
88 bool geo_sensor_hal::init_resources(void)
92 if (!read_node_value<double>(m_x_scale_node, m_x_scale))
95 if (!read_node_value<double>(m_y_scale_node, m_y_scale))
98 if (!read_node_value<double>(m_z_scale_node, m_z_scale))
101 INFO("Scale Values: %f, %f, %f", m_x_scale, m_y_scale, m_z_scale);
105 string geo_sensor_hal::get_model_id(void)
110 sensor_type_t geo_sensor_hal::get_type(void)
112 return GEOMAGNETIC_SENSOR;
115 bool geo_sensor_hal::enable(void)
117 INFO("Resource already enabled. Enable not supported.");
121 bool geo_sensor_hal::disable(void)
123 INFO("Disable not supported.");
127 bool geo_sensor_hal::set_interval(unsigned long val)
129 INFO("Polling interval cannot be changed.");
133 bool geo_sensor_hal::update_value(void)
135 int raw_values[3] = {0,};
136 ifstream temp_handle;
138 if (!read_node_value<int>(m_x_node, raw_values[0]))
141 if (!read_node_value<int>(m_y_node, raw_values[1]))
144 if (!read_node_value<int>(m_z_node, raw_values[2]))
147 m_x = GAUSS_TO_UTESLA(raw_values[0] * m_x_scale);
148 m_y = GAUSS_TO_UTESLA(raw_values[1] * m_y_scale);
149 m_z = GAUSS_TO_UTESLA(raw_values[2] * m_z_scale);
151 m_fired_time = INITIAL_TIME;
152 INFO("x = %d, y = %d, z = %d, time = %lluus", raw_values[0], raw_values[0], raw_values[0], m_fired_time);
157 bool geo_sensor_hal::is_data_ready(bool wait)
160 ret = update_value();
164 int geo_sensor_hal::get_sensor_data(sensor_data_t &data)
166 data.data_unit_idx = SENSOR_UNIT_MICRO_TESLA;
167 data.timestamp = m_fired_time;
169 data.values[0] = (float)m_x;
170 data.values[1] = (float)m_y;
171 data.values[2] = (float)m_z;
175 bool geo_sensor_hal::get_properties(sensor_properties_t &properties)
177 properties.sensor_unit_idx = SENSOR_UNIT_MICRO_TESLA;
178 properties.sensor_min_range = SENSOR_MIN_RANGE;
179 properties.sensor_max_range = SENSOR_MAX_RANGE;
180 snprintf(properties.sensor_name, sizeof(properties.sensor_name), "%s", m_chip_name.c_str());
181 snprintf(properties.sensor_vendor, sizeof(properties.sensor_vendor), "%s", m_vendor.c_str());
182 properties.sensor_resolution = 1;
186 bool geo_sensor_hal::is_sensorhub_supported(void)
191 bool geo_sensor_hal::check_hw_node(void)
195 DIR *main_dir = NULL;
196 struct dirent *dir_entry = NULL;
197 bool find_node = false;
199 INFO("======================start check_hw_node=============================");
201 m_sensorhub_supported = is_sensorhub_supported();
202 main_dir = opendir(IIO_DIR);
206 ERR("Could not open IIO directory\n");
212 dir_entry = readdir(main_dir);
213 if(dir_entry == NULL)
216 if ((strncasecmp(dir_entry->d_name , ".", 1 ) != 0) && (strncasecmp(dir_entry->d_name , "..", 2 ) != 0) && (dir_entry->d_ino != 0))
218 file_name = string(IIO_DIR) + string(dir_entry->d_name) + string(NAME_NODE);
219 ifstream infile(file_name.c_str());
226 if (CConfig::get_instance().is_supported(SENSOR_TYPE_MAGNETIC, hw_name) == true)
228 m_name = m_model_id = hw_name;
229 INFO("m_model_id = %s", m_model_id.c_str());
231 string temp = string(IIO_DIR) + string(dir_entry->d_name);
233 m_x_node = temp + string(X_RAW_VAL_NODE);
234 m_y_node = temp + string(Y_RAW_VAL_NODE);
235 m_z_node = temp + string(Z_RAW_VAL_NODE);
236 m_x_scale_node = temp + string(X_SCALE_NODE);
237 m_y_scale_node = temp + string(Y_SCALE_NODE);
238 m_z_scale_node = temp + string(Z_SCALE_NODE);
250 extern "C" void *create(void)
252 geo_sensor_hal *inst;
256 inst = new geo_sensor_hal();
260 ERR("Failed to create geo_sensor_hal class, errno : %d, errstr : %s", err, strerror(err));
267 extern "C" void destroy(void *inst)
269 delete (geo_sensor_hal *)inst;