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