sensord: add/change enums and types for avoiding build-break
[platform/core/system/sensord.git] / src / geo / geo_sensor_hal.cpp
1 /*
2  * geo_sensor_hal
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <dirent.h>
22 #include <linux/input.h>
23 #include <csensor_config.h>
24 #include <geo_sensor_hal.h>
25 #include <sys/ioctl.h>
26 #include <fstream>
27 #include <iio_common.h>
28
29 using std::ifstream;
30
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"
38
39 #define INITIAL_TIME                    -1
40 #define GAUSS_TO_UTESLA(val)    ((val) * 100.0f)
41
42 geo_sensor_hal::geo_sensor_hal()
43 : m_polling_interval(POLL_1HZ_MS)
44 , m_x(0)
45 , m_y(0)
46 , m_z(0)
47 , m_hdst(0)
48 , m_fired_time(INITIAL_TIME)
49 , m_node_handle(-1)
50 {
51         const string sensorhub_interval_node_name = "mag_poll_delay";
52         csensor_config &config = csensor_config::get_instance();
53
54         node_path_info_query query;
55         node_path_info info;
56         int input_method = IIO_METHOD;
57
58         if (!get_model_properties(SENSOR_TYPE_MAGNETIC, m_model_id, input_method)) {
59                 ERR("Failed to find model_properties");
60                 throw ENXIO;
61
62         }
63
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;
70
71         if (!get_node_path_info(query, info)) {
72                 ERR("Failed to get node info");
73                 throw ENXIO;
74         }
75
76         show_node_path_info(info);
77
78         m_data_node = info.data_node_path;
79         m_enable_node = info.enable_node_path;
80         m_interval_node = info.interval_node_path;
81
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());
96         }
97
98         if (!config.get(SENSOR_TYPE_MAGNETIC, m_model_id, ELEMENT_VENDOR, m_vendor)) {
99                 ERR("[VENDOR] is empty\n");
100                 throw ENXIO;
101         }
102
103         INFO("m_vendor = %s", m_vendor.c_str());
104
105         if (!config.get(SENSOR_TYPE_MAGNETIC, m_model_id, ELEMENT_NAME, m_chip_name)) {
106                 ERR("[NAME] is empty\n");
107                 throw ENXIO;
108         }
109
110         init_resources();
111
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");
115
116 }
117
118 geo_sensor_hal::~geo_sensor_hal()
119 {
120         if (m_node_handle > 0)
121                 close(m_node_handle);
122         m_node_handle = -1;
123
124         INFO("geo_sensor is destroyed!\n");
125 }
126
127 string geo_sensor_hal::get_model_id(void)
128 {
129         return m_model_id;
130 }
131
132 sensor_type_t geo_sensor_hal::get_type(void)
133 {
134         return GEOMAGNETIC_SENSOR;
135 }
136
137 bool geo_sensor_hal::enable(void)
138 {
139         m_fired_time = INITIAL_TIME;
140         INFO("Geo sensor real starting");
141         return true;
142 }
143
144 bool geo_sensor_hal::disable(void)
145 {
146         INFO("Geo sensor real stopping");
147         return true;
148 }
149
150 bool geo_sensor_hal::set_interval(unsigned long val)
151 {
152         INFO("Polling interval cannot be changed.");
153         return true;
154
155 }
156
157 bool geo_sensor_hal::update_value(void)
158 {
159         int raw_values[3] = {0,};
160         ifstream temp_handle;
161
162         if (!read_node_value<int>(m_x_node, raw_values[0]))
163                 return false;
164         if (!read_node_value<int>(m_y_node, raw_values[1]))
165                 return false;
166         if (!read_node_value<int>(m_z_node, raw_values[2]))
167                 return false;
168
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);
172
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);
176
177         return true;
178 }
179
180 bool geo_sensor_hal::is_data_ready(bool wait)
181 {
182         bool ret;
183         ret = update_value();
184         return ret;
185 }
186
187 int geo_sensor_hal::get_sensor_data(sensor_data_t &data)
188 {
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;
195         return 0;
196 }
197
198 bool geo_sensor_hal::get_properties(sensor_properties_s &properties)
199 {
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;
208         return true;
209 }
210
211 bool geo_sensor_hal::init_resources(void)
212 {
213         ifstream temp_handle;
214
215         if (!read_node_value<double>(m_x_scale_node, m_x_scale)) {
216                 ERR("Failed to read x scale node");
217                 return false;
218         }
219         if (!read_node_value<double>(m_y_scale_node, m_y_scale)) {
220                 ERR("Failed to read y scale node");
221                 return false;
222         }
223         if (!read_node_value<double>(m_z_scale_node, m_z_scale)) {
224                 ERR("Failed to read y scale node");
225                 return false;
226         }
227         INFO("Scale Values: %f, %f, %f", m_x_scale, m_y_scale, m_z_scale);
228         return true;
229 }
230
231 extern "C" void *create(void)
232 {
233         geo_sensor_hal *inst;
234
235         try {
236                 inst = new geo_sensor_hal();
237         } catch (int err) {
238                 ERR("geo_sensor_hal class create fail , errno : %d , errstr : %s\n", err, strerror(err));
239                 return NULL;
240         }
241
242         return (void*)inst;
243 }
244
245 extern "C" void destroy(void *inst)
246 {
247         delete (geo_sensor_hal*)inst;
248 }