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