sensord: add/change enums and types for avoiding build-break
[platform/core/system/sensord.git] / src / temperature / temperature_sensor_hal.cpp
1 /*
2  * temperature_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 <fstream>
25 #include <temperature_sensor_hal.h>
26 #include <sys/ioctl.h>
27 #include <iio_common.h>
28
29 using std::ifstream;
30
31 #define SENSOR_TYPE_TEMPERATURE         "TEMPERATURE"
32 #define ELEMENT_NAME                            "NAME"
33 #define ELEMENT_VENDOR                          "VENDOR"
34 #define ELEMENT_RAW_DATA_UNIT           "RAW_DATA_UNIT"
35
36 #define TEMP_INPUT_NAME                                 "temperature_sensor"
37 #define TEMP_IIO_ENABLE_NODE_NAME               "temp_enable"
38 #define TEMP_SENSORHUB_POLL_NODE_NAME   "temp_poll_delay"
39 #define INITIAL_TIME -1
40
41 temperature_sensor_hal::temperature_sensor_hal()
42 : m_temperature(0)
43 , m_node_handle(-1)
44 , m_polling_interval(POLL_1HZ_MS)
45 , m_fired_time(INITIAL_TIME)
46 {
47         const string sensorhub_interval_node_name = TEMP_SENSORHUB_POLL_NODE_NAME;
48         string file_name;
49         node_path_info_query query;
50         node_path_info info;
51         int input_method = IIO_METHOD;
52
53         if (!get_model_properties(SENSOR_TYPE_TEMPERATURE, m_model_id, input_method)) {
54                 ERR("Failed to find model_properties");
55                 throw ENXIO;
56         }
57
58         query.input_method = input_method;
59         query.sensorhub_controlled = is_sensorhub_controlled(sensorhub_interval_node_name);
60         query.sensor_type = SENSOR_TYPE_TEMPERATURE;
61         query.input_event_key = TEMP_INPUT_NAME;
62         query.iio_enable_node_name = TEMP_IIO_ENABLE_NODE_NAME;
63         query.sensorhub_interval_node_name = sensorhub_interval_node_name;
64
65         if (!get_node_path_info(query, info)) {
66                 ERR("Failed to get node info");
67                 throw ENXIO;
68         }
69
70         show_node_path_info(info);
71
72         m_data_node = info.data_node_path;
73         m_enable_node = info.enable_node_path;
74         m_interval_node = info.interval_node_path;
75
76         if(input_method == IIO_METHOD) {
77                 m_temperature_dir=info.base_dir;
78                 m_temp_node = m_temperature_dir + string(TEMP_RAW);
79                 INFO("m_temperature_dir = %s", m_temperature_dir.c_str());
80                 INFO("m_temp_node = %s", m_temp_node.c_str());
81         }
82         csensor_config &config = csensor_config::get_instance();
83
84         if (!config.get(SENSOR_TYPE_TEMPERATURE, m_model_id, ELEMENT_VENDOR, m_vendor)) {
85                 ERR("[VENDOR] is empty\n");
86                 throw ENXIO;
87         }
88
89         if (!config.get(SENSOR_TYPE_TEMPERATURE, m_model_id, ELEMENT_NAME, m_chip_name)) {
90                 ERR("[NAME] is empty\n");
91                 throw ENXIO;
92         }
93
94         double raw_data_unit;
95
96         if (!config.get(SENSOR_TYPE_TEMPERATURE, m_model_id, ELEMENT_RAW_DATA_UNIT, raw_data_unit)) {
97                 ERR("[RAW_DATA_UNIT] is empty\n");
98                 throw ENXIO;
99         }
100
101         m_raw_data_unit = (float)(raw_data_unit);
102
103         INFO("m_data_node = %s\n",m_data_node.c_str());
104
105         if ((m_node_handle = open(m_temp_node.c_str(),O_RDWR)) < 0) {
106                 ERR("Failed to open handle(%d)", m_node_handle);
107                 throw ENXIO;
108         }
109
110         INFO("m_data_node = %s\n",m_data_node.c_str());
111         INFO("m_raw_data_unit = %f\n", m_raw_data_unit);
112
113         file_name = m_temperature_dir + string(TEMP_SCALE);
114         if (!read_node_value<int>(file_name, m_temp_scale))
115                 throw ENXIO;
116
117         file_name = m_temperature_dir + string(TEMP_OFFSET);
118         if (!read_node_value<float>(file_name, m_temp_offset))
119                 throw ENXIO;
120
121         INFO("m_temp_offset %f",m_temp_offset);
122         INFO("m_temp_scale %d",m_temp_scale);
123         INFO("m_vendor = %s", m_vendor.c_str());
124         INFO("m_chip_name = %s", m_chip_name.c_str());
125         INFO("m_raw_data_unit = %f\n", m_raw_data_unit);
126         INFO("temperature_sensor_hal is created!\n");
127 }
128
129 temperature_sensor_hal::~temperature_sensor_hal()
130 {
131         close(m_node_handle);
132         m_node_handle = -1;
133
134         INFO("temperature_sensor_hal is destroyed!\n");
135 }
136
137 string temperature_sensor_hal::get_model_id(void)
138 {
139         return m_model_id;
140 }
141
142 sensor_type_t temperature_sensor_hal::get_type(void)
143 {
144         return TEMPERATURE_SENSOR;
145 }
146
147 bool temperature_sensor_hal::enable(void)
148 {
149         m_fired_time = INITIAL_TIME;
150         INFO("Temperature sensor real starting");
151         return true;
152 }
153
154 bool temperature_sensor_hal::disable(void)
155 {
156         INFO("Temperature sensor real stopping");
157         return true;
158 }
159
160 bool temperature_sensor_hal::set_interval(unsigned long val)
161 {
162         return true;
163
164 }
165
166 bool temperature_sensor_hal::update_value(bool wait)
167 {
168         int raw_temp_count;
169
170         if (!read_node_value<int>(m_temp_node, raw_temp_count))
171                 return false;
172         m_temperature = m_temp_offset + ((float)raw_temp_count)/((float)m_temp_scale);
173         INFO("m_temperature %f",m_temperature);
174         INFO("m_temp_offset %f",m_temp_offset);
175         INFO("raw_temp_count %d",raw_temp_count);
176         INFO("m_temp_scale %d",m_temp_scale);
177         return true;
178 }
179
180 bool temperature_sensor_hal::is_data_ready(bool wait)
181 {
182         bool ret;
183         ret = update_value(wait);
184         return ret;
185 }
186
187 int temperature_sensor_hal::get_sensor_data(sensor_data_t &data)
188 {
189         AUTOLOCK(m_value_mutex);
190         data.accuracy = SENSOR_ACCURACY_GOOD;
191         data.timestamp = m_fired_time ;
192         data.value_count = 1;
193         data.values[0] = (float) m_temperature;
194
195         return 0;
196 }
197
198
199 bool temperature_sensor_hal::get_properties(sensor_properties_s &properties)
200 {
201         properties.name = m_chip_name;
202         properties.vendor = m_vendor;
203         properties.min_range = -45;
204         properties.max_range = 130;
205         properties.min_interval = 1;
206         properties.resolution = 1;
207         properties.fifo_count = 0;
208         properties.max_batch_count = 0;
209
210         return true;
211 }
212
213 extern "C" void *create(void)
214 {
215         temperature_sensor_hal *inst;
216
217         try {
218                 inst = new temperature_sensor_hal();
219         } catch (int err) {
220                 ERR("temperature_sensor_hal class create fail , errno : %d , errstr : %s\n", err, strerror(err));
221                 return NULL;
222         }
223
224         return (void*)inst;
225 }
226
227 extern "C" void destroy(void *inst)
228 {
229         delete (temperature_sensor_hal*)inst;
230 }