sensord: change the type of hal id to int32_t
[platform/core/system/sensord.git] / src / server / physical_sensor.cpp
1 /*
2  * libsensord-share
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 <physical_sensor.h>
21 #include <sensor_event_queue.h>
22
23 #define UNKNOWN_NAME "UNKNOWN_SENSOR"
24
25 cmutex physical_sensor::m_mutex;
26
27 physical_sensor::physical_sensor()
28 : m_sensor_device(NULL)
29 {
30 }
31
32 physical_sensor::~physical_sensor()
33 {
34 }
35
36 void physical_sensor::set_sensor_handle(sensor_handle_t handle)
37 {
38         m_handle.id = handle.id;
39         m_handle.name = handle.name;
40         m_handle.type = handle.type;
41         m_handle.event_type = handle.event_type;
42         m_handle.model_name = handle.model_name;
43         m_handle.vendor = handle.vendor;
44         m_handle.min_range = handle.min_range;
45         m_handle.max_range = handle.max_range;
46         m_handle.resolution = handle.resolution;
47         m_handle.min_interval = handle.min_interval;
48         m_handle.max_batch_count = handle.max_batch_count;
49         m_handle.wakeup_supported = handle.wakeup_supported;
50 }
51
52 void physical_sensor::set_sensor_device(sensor_device *device)
53 {
54         m_sensor_device = device;
55 }
56
57 sensor_type_t physical_sensor::get_type(void)
58 {
59         return static_cast<sensor_type_t>(m_handle.type);
60 }
61
62 unsigned int physical_sensor::get_event_type(void)
63 {
64         return m_handle.event_type;
65 }
66
67 const char* physical_sensor::get_name(void)
68 {
69         if (!m_handle.name)
70                 return UNKNOWN_NAME;
71
72         return m_handle.name;
73 }
74
75 uint32_t physical_sensor::get_hal_id(void)
76 {
77         return m_handle.id;
78 }
79
80 int physical_sensor::get_poll_fd()
81 {
82         AUTOLOCK(m_mutex);
83
84         if (!m_sensor_device)
85                 return -1;
86
87         return m_sensor_device->get_poll_fd();
88 }
89
90 bool physical_sensor::read_fd(std::vector<uint32_t> &ids)
91 {
92         AUTOLOCK(m_mutex);
93         int size;
94         uint32_t *_ids;
95
96         if (!m_sensor_device)
97                 return false;
98
99         size = m_sensor_device->read_fd(&_ids);
100
101         for (int i = 0; i < size; ++i)
102                 ids.push_back(_ids[i]);
103
104         return true;
105 }
106
107 int physical_sensor::get_data(sensor_data_t **data, int *length)
108 {
109         AUTOLOCK(m_mutex);
110
111         if (!m_sensor_device)
112                 return -1;
113
114         int remains = 0;
115         remains = m_sensor_device->get_data(m_handle.id, data, length);
116
117         if (*length < 0) {
118                 ERR("Failed to get sensor event");
119                 return -1;
120         }
121
122         return remains;
123 }
124
125 bool physical_sensor::flush(void)
126 {
127         AUTOLOCK(m_mutex);
128
129         if (!m_sensor_device)
130                 return false;
131
132         return m_sensor_device->flush(m_handle.id);
133 }
134
135 bool physical_sensor::set_interval(unsigned long interval)
136 {
137         AUTOLOCK(m_mutex);
138
139         if (!m_sensor_device)
140                 return false;
141
142         INFO("Polling interval is set to %dms", interval);
143
144         return m_sensor_device->set_interval(m_handle.id, interval);
145 }
146
147 bool physical_sensor::set_batch_latency(unsigned long latency)
148 {
149         AUTOLOCK(m_mutex);
150
151         if (!m_sensor_device)
152                 return false;
153
154         INFO("Polling interval is set to %dms", latency);
155
156         return m_sensor_device->set_batch_latency(m_handle.id, latency);
157 }
158
159 int physical_sensor::set_attribute(int32_t attribute, int32_t value)
160 {
161         AUTOLOCK(m_mutex);
162
163         if (!m_sensor_device)
164                 return false;
165
166         return m_sensor_device->set_attribute(m_handle.id, attribute, value);
167 }
168
169 int physical_sensor::set_attribute(char *attribute, char *value, int value_len)
170 {
171         AUTOLOCK(m_mutex);
172
173         if (!m_sensor_device)
174                 return false;
175
176         return m_sensor_device->set_attribute_str(m_handle.id, attribute, value, value_len);
177 }
178
179 bool physical_sensor::set_wakeup(int wakeup)
180 {
181         return false;
182 }
183
184 bool physical_sensor::on_start()
185 {
186         AUTOLOCK(m_mutex);
187
188         if (!m_sensor_device)
189                 return false;
190
191         return m_sensor_device->enable(m_handle.id);
192 }
193
194 bool physical_sensor::on_stop()
195 {
196         AUTOLOCK(m_mutex);
197
198         if (!m_sensor_device)
199                 return false;
200
201         return m_sensor_device->disable(m_handle.id);
202 }
203
204 bool physical_sensor::get_sensor_info(sensor_info &info)
205 {
206         info.set_type(get_type());
207         info.set_id(get_id());
208         info.set_privilege(SENSOR_PRIVILEGE_PUBLIC); // FIXME
209         info.set_name(m_handle.model_name);
210         info.set_vendor(m_handle.vendor);
211         info.set_min_range(m_handle.min_range);
212         info.set_max_range(m_handle.max_range);
213         info.set_resolution(m_handle.resolution);
214         info.set_min_interval(m_handle.min_interval);
215         info.set_fifo_count(0); // FIXME
216         info.set_max_batch_count(m_handle.max_batch_count);
217         info.set_supported_event(get_event_type());
218         info.set_wakeup_supported(m_handle.wakeup_supported);
219
220         return true;
221 }
222