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