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