sensord: add/change enums and types for avoiding build-break
[platform/core/system/sensord.git] / src / light / light_sensor.cpp
1 /*
2  * sensord
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
20 #include <common.h>
21 #include <sf_common.h>
22 #include <light_sensor.h>
23 #include <sensor_plugin_loader.h>
24 #include <algorithm>
25
26 using std::bind1st;
27 using std::mem_fun;
28
29 #define SENSOR_NAME "LIGHT_SENSOR"
30
31 #define INITIAL_VALUE -1
32 const int light_sensor::m_light_level[] = {0, 1, 165, 288, 497, 869, 1532, 2692, 4692, 8280, 21428, 65535, 137852};
33
34 light_sensor::light_sensor()
35 : m_sensor_hal(NULL)
36 , m_level(INITIAL_VALUE)
37 {
38         m_name = string(SENSOR_NAME);
39
40         vector<unsigned int> supported_events = {
41                 LIGHT_EVENT_CHANGE_LEVEL,
42                 LIGHT_EVENT_LEVEL_DATA_REPORT_ON_TIME,
43                 LIGHT_EVENT_LUX_DATA_REPORT_ON_TIME,
44         };
45
46         for_each(supported_events.begin(), supported_events.end(),
47                         bind1st(mem_fun(&sensor_base::register_supported_event), this));
48
49         physical_sensor::set_poller(light_sensor::working, this);
50 }
51
52 light_sensor::~light_sensor()
53 {
54         INFO("light_sensor is destroyed!");
55 }
56
57 bool light_sensor::init()
58 {
59         m_sensor_hal = sensor_plugin_loader::get_instance().get_sensor_hal(LIGHT_SENSOR);
60
61         if (!m_sensor_hal) {
62                 ERR("cannot load sensor_hal[%s]", sensor_base::get_name());
63                 return false;
64         }
65
66         INFO("%s is created!", sensor_base::get_name());
67         return true;
68 }
69
70 sensor_type_t light_sensor::get_type(void)
71 {
72         return LIGHT_SENSOR;
73 }
74
75 bool light_sensor::working(void *inst)
76 {
77         light_sensor *sensor = (light_sensor *)inst;
78         return sensor->process_event();
79 }
80
81 bool light_sensor::process_event(void)
82 {
83         sensor_event_t event;
84         int level;
85
86         if (!m_sensor_hal->is_data_ready(true))
87                 return true;
88
89         m_sensor_hal->get_sensor_data(event.data);
90         level = (int) adc_to_light_level((int)event.data.values[0]);
91
92         AUTOLOCK(m_client_info_mutex);
93
94         if (get_client_cnt(LIGHT_EVENT_LUX_DATA_REPORT_ON_TIME)) {
95                 event.event_type = LIGHT_EVENT_LUX_DATA_REPORT_ON_TIME;
96                 push(event);
97         }
98
99         if (get_client_cnt(LIGHT_EVENT_LEVEL_DATA_REPORT_ON_TIME)) {
100                 event.event_type = LIGHT_EVENT_LEVEL_DATA_REPORT_ON_TIME;
101                 raw_to_level(event.data);
102                 push(event);
103         }
104
105         if (m_level != level) {
106                 m_level = level;
107
108                 if (get_client_cnt(LIGHT_EVENT_CHANGE_LEVEL)) {
109                         event.event_type = LIGHT_EVENT_CHANGE_LEVEL;
110                         raw_to_level(event.data);
111                         push(event);
112                 }
113         }
114
115         return true;
116 }
117
118 int light_sensor::adc_to_light_level(int adc)
119 {
120         int level_cnt = sizeof(m_light_level) / sizeof(m_light_level[0]) - 1;
121
122         for (int i = 0; i < level_cnt; ++i) {
123                 if (adc >= m_light_level[i] && adc < m_light_level[i + 1])
124                         return i;
125         }
126
127         return -1;
128 }
129
130 bool light_sensor::on_start(void)
131 {
132         AUTOLOCK(m_mutex);
133
134         if (!m_sensor_hal->enable()) {
135                 ERR("m_sensor_hal start fail");
136                 return false;
137         }
138
139         return start_poll();
140 }
141
142 bool light_sensor::on_stop(void)
143 {
144         AUTOLOCK(m_mutex);
145
146         if (!m_sensor_hal->disable()) {
147                 ERR("m_sensor_hal stop fail");
148                 return false;
149         }
150
151         return stop_poll();
152 }
153
154 bool light_sensor::get_properties(const unsigned int type, sensor_properties_s &properties)
155 {
156         m_sensor_hal->get_properties(properties);
157
158         if (type == LIGHT_LUX_DATA_SET)
159                 return 0;
160
161         if (type == LIGHT_BASE_DATA_SET) {
162                 properties.min_range = 0;
163                 properties.max_range = sizeof(m_light_level) / sizeof(m_light_level[0]) - 1;
164                 properties.resolution = 1;
165                 return 0;
166         }
167
168         return -1;
169 }
170
171 int light_sensor::get_sensor_data(const unsigned int type, sensor_data_t &data)
172 {
173         int ret;
174         ret = m_sensor_hal->get_sensor_data(data);
175
176         if (ret < 0)
177                 return -1;
178
179         if (type == LIGHT_LUX_DATA_SET)
180                 return 0;
181
182         if (type == LIGHT_BASE_DATA_SET) {
183                 raw_to_level(data);
184                 return 0;
185         }
186
187         return -1;
188 }
189
190 bool light_sensor::set_interval(unsigned long interval)
191 {
192         AUTOLOCK(m_mutex);
193
194         INFO("Polling interval is set to %dms", interval);
195         return m_sensor_hal->set_interval(interval);
196 }
197
198 void light_sensor::raw_to_level(sensor_data_t &data)
199 {
200         data.values[0] = (int) adc_to_light_level((int)data.values[0]);
201         data.values_num = 1;
202 }
203
204 extern "C" void *create(void)
205 {
206         light_sensor *inst;
207
208         try {
209                 inst = new light_sensor();
210         } catch (int err) {
211                 ERR("Failed to create light_sensor class, errno : %d, errstr : %s", err, strerror(err));
212                 return NULL;
213         }
214
215         return (void *)inst;
216 }
217
218 extern "C" void destroy(void *inst)
219 {
220         delete (light_sensor *)inst;
221 }