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