sensord: add/change enums and types for avoiding build-break
[platform/core/system/sensord.git] / src / proxi / proxi_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 <proxi_sensor.h>
23 #include <sensor_plugin_loader.h>
24
25 #define SENSOR_NAME "PROXI_SENSOR"
26
27 proxi_sensor::proxi_sensor()
28 : m_sensor_hal(NULL)
29 , m_state(PROXIMITY_STATE_FAR)
30 {
31         m_name = string(SENSOR_NAME);
32
33         register_supported_event(PROXIMITY_EVENT_CHANGE_STATE);
34         register_supported_event(PROXIMITY_EVENT_STATE_REPORT_ON_TIME);
35         register_supported_event(PROXIMITY_EVENT_DISTANCE_DATA_REPORT_ON_TIME);
36
37         physical_sensor::set_poller(proxi_sensor::working, this);
38 }
39
40 proxi_sensor::~proxi_sensor()
41 {
42         INFO("proxi_sensor is destroyed!\n");
43 }
44
45 bool proxi_sensor::init()
46 {
47         m_sensor_hal = sensor_plugin_loader::get_instance().get_sensor_hal(PROXIMITY_SENSOR);
48
49         if (!m_sensor_hal) {
50                 ERR("cannot load sensor_hal[%s]", sensor_base::get_name());
51                 return false;
52         }
53
54         INFO("%s is created!\n", sensor_base::get_name());
55         return true;
56 }
57
58 sensor_type_t proxi_sensor::get_type(void)
59 {
60         return PROXIMITY_SENSOR;
61 }
62
63 bool proxi_sensor::working(void *inst)
64 {
65         proxi_sensor *sensor = (proxi_sensor*)inst;
66         return sensor->process_event();
67 }
68
69 bool proxi_sensor::process_event(void)
70 {
71         sensor_event_t event;
72         int state;
73
74         if (!m_sensor_hal->is_data_ready(true))
75                 return true;
76
77         m_sensor_hal->get_sensor_data(event.data);
78
79         AUTOLOCK(m_client_info_mutex);
80         AUTOLOCK(m_mutex);
81
82         event.sensor_id = get_id();
83         if (get_client_cnt(PROXIMITY_EVENT_DISTANCE_DATA_REPORT_ON_TIME)) {
84                 event.event_type = PROXIMITY_EVENT_DISTANCE_DATA_REPORT_ON_TIME;
85                 raw_to_base(event.data);
86                 push(event);
87         }
88
89         state = event.data.values[0];
90
91         if (m_state != state) {
92                 AUTOLOCK(m_value_mutex);
93                 m_state = state;
94
95                 if (get_client_cnt(PROXIMITY_EVENT_CHANGE_STATE)) {
96                         event.event_type = PROXIMITY_EVENT_CHANGE_STATE;
97                         raw_to_state(event.data);
98                         push(event);
99                 }
100         }
101
102         return true;
103 }
104
105 bool proxi_sensor::on_start(void)
106 {
107         if (!m_sensor_hal->enable()) {
108                 ERR("m_sensor_hal start fail\n");
109                 return false;
110         }
111
112         return start_poll();
113 }
114
115 bool proxi_sensor::on_stop(void)
116 {
117         if (!m_sensor_hal->disable()) {
118                 ERR("m_sensor_hal stop fail\n");
119                 return false;
120         }
121
122         return stop_poll();
123 }
124
125 bool proxi_sensor::get_properties(sensor_properties_s &properties)
126 {
127         m_sensor_hal->get_properties(properties);
128
129         properties.min_range = properties.min_range * 5;
130         properties.max_range = properties.max_range * 5;
131
132         return true;
133 }
134
135 int proxi_sensor::get_sensor_data(unsigned int type, sensor_data_t &data)
136 {
137         int state;
138
139         if ((type != PROXIMITY_BASE_DATA_SET) && (type != PROXIMITY_DISTANCE_DATA_SET))
140                 return -1;
141
142         state = m_sensor_hal->get_sensor_data(data);
143
144         if (state < 0) {
145                 ERR("m_sensor_hal get struct_data fail\n");
146                 return -1;
147         }
148
149         if (type == PROXIMITY_DISTANCE_DATA_SET) {
150                 raw_to_base(data);
151                 return 0;
152         }
153
154         return 0;
155 }
156
157 void proxi_sensor::raw_to_base(sensor_data_t &data)
158 {
159         data.values[0] = (float)((PROXIMITY_STATE_NEAR - data.values[0]) * 5);
160 }
161
162 void proxi_sensor::raw_to_state(sensor_data_t &data)
163 {
164         data.value_count = 1;
165 }
166
167 extern "C" void *create(void)
168 {
169         proxi_sensor *inst;
170
171         try {
172                 inst = new proxi_sensor();
173         } catch (int err) {
174                 ERR("proxi_sensor class create fail , errno : %d , errstr : %s\n", err, strerror(err));
175                 return NULL;
176         }
177
178         return (void*)inst;
179 }
180
181 extern "C" void destroy(void *inst)
182 {
183         delete (proxi_sensor*)inst;
184 }