fc944a31ce6f1cb2279562c3664bbb980036226e
[platform/hal/backend/tm1/sensor-tm1.git] / src / sensorhub / sensorhub.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <algorithm>
19 #include <sensor_log.h>
20
21 #include "sensorhub.h"
22 #include "sensorhub_controller.h"
23 #include "sensorhub_manager.h"
24 #include "system_state.h"
25
26 #define MAX_ID 0x3
27
28 sensorhub_device::sensorhub_device()
29 {
30         controller = &sensorhub_controller::get_instance();
31         if (!controller) {
32                 ERR("Failed to allocated memory");
33                 throw;
34         }
35
36         manager = &sensorhub_manager::get_instance();
37         if (!manager) {
38                 ERR("Failed to allocated memory");
39                 throw;
40         }
41         manager->set_controller(controller);
42         system_state_handler::get_instance().set_controller(controller);
43
44         INFO("sensorhub_device is created!");
45 }
46
47 sensorhub_device::~sensorhub_device()
48 {
49         INFO("sensorhub_device is destroyed!");
50 }
51
52 int sensorhub_device::get_poll_fd(void)
53 {
54         return controller->get_poll_fd();
55 }
56
57 int sensorhub_device::get_sensors(const sensor_info_t **sensors)
58 {
59         int size;
60         size = manager->get_sensors(sensors);
61
62         return size;
63 }
64
65 bool sensorhub_device::enable(uint32_t id)
66 {
67         system_state_handler::get_instance().initialize();
68         retvm_if(id == 0 || id > MAX_ID, false, "%s:Invalid ID Received", SENSOR_NAME);
69         controller->enable();
70         sensorhub_sensor *sensor = manager->get_sensor(id);
71
72         if (!sensor) {
73                 ERR("Failed to enable sensor(0x%x)", id);
74                 return false;
75         }
76
77         return sensor->enable();
78 }
79
80 bool sensorhub_device::disable(uint32_t id)
81 {
82         system_state_handler::get_instance().finalize();
83         retvm_if(id == 0 || id > MAX_ID, false, "%s:Invalid ID Received", SENSOR_NAME);
84         controller->disable();
85         sensorhub_sensor *sensor = manager->get_sensor(id);
86
87         if (!sensor) {
88                 ERR("Failed to disable sensor(0x%x)", id);
89                 return false;
90         }
91
92         return sensor->disable();
93 }
94
95 bool sensorhub_device::set_interval(uint32_t id, unsigned long val)
96 {
97         sensorhub_sensor *sensor = NULL;
98         retvm_if(id == 0 || id > MAX_ID, false, "%s:Invalid ID Received", SENSOR_NAME);
99         retvm_if(val < 0, false, "%s:Invalid value Received", SENSOR_NAME);
100         sensor = manager->get_sensor(id);
101         if (!sensor) {
102                 ERR("Failed to set interval to sensor(0x%x)", id);
103                 return false;
104         }
105
106         return sensor->set_interval(val);
107 }
108
109 bool sensorhub_device::set_batch_latency(uint32_t id, unsigned long val)
110 {
111         sensorhub_sensor *sensor = NULL;
112         retvm_if(id == 0 || id > MAX_ID, false, "%s:Invalid ID Received", SENSOR_NAME);
113         sensor = manager->get_sensor(id);
114
115         if (!sensor) {
116                 ERR("Failed to set batch latency to sensor(0x%x)", id);
117                 return false;
118         }
119
120         return sensor->set_batch_latency(val);
121 }
122
123 bool sensorhub_device::set_attribute_int(uint32_t id, int32_t attribute, int32_t value)
124 {
125         int ret;
126
127         sensorhub_sensor *sensor = NULL;
128         retvm_if(id == 0 || id > MAX_ID, false, "%s:Invalid ID Received", SENSOR_NAME);
129         retvm_if(value < 0, false, "%s:Invalid value Received", SENSOR_NAME);
130         retvm_if(attribute < 0, false, "%s:Invalid attribute Received", SENSOR_NAME);
131
132         sensor = manager->get_sensor(id);
133         if (!sensor) {
134                 ERR("Failed to set attribute to sensor(0x%x)", id);
135                 return false;
136         }
137
138         ret = sensor->set_attribute_int(attribute, value);
139
140         if ((ret < 0) && (ret != -EBUSY)) {
141                 ERR("Failed to send sensorhub data");
142                 return false;
143         }
144
145         if (ret == -EBUSY) {
146                 WARN("Command is sent during sensorhub firmware update");
147                 return false;
148         }
149
150         return true;
151 }
152
153 bool sensorhub_device::set_attribute_str(uint32_t id, int32_t attribute, char *value, int value_len)
154 {
155         int ret;
156
157         sensorhub_sensor *sensor = NULL;
158         retvm_if(id == 0 || id > MAX_ID, false, "%s:Invalid ID Received", SENSOR_NAME);
159         retvm_if(value == NULL || value == nullptr, false, "%s:Invalid string Received", SENSOR_NAME);
160         retvm_if(value_len <= 0, false, "%s:Invalid value_len Received", SENSOR_NAME);
161         sensor = manager->get_sensor(id);
162
163         if (!sensor) {
164                 ERR("Failed to set attribute to sensor(0x%x)", id);
165                 return false;
166         }
167
168         ret = sensor->set_attribute_str(attribute, value, value_len);
169
170         if ((ret < 0) && (ret != -EBUSY)) {
171                 ERR("Failed to send sensorhub data");
172                 return false;
173         }
174
175         if (ret == -EBUSY) {
176                 WARN("Command is sent during sensorhub firmware update");
177                 return false;
178         }
179
180         return true;
181 }
182
183 int sensorhub_device::read_fd(uint32_t **ids)
184 {
185         sensorhub_data_t data;
186         retvm_if(ids == NULL || ids == nullptr, SENSOR_ERROR_INVALID_PARAMETER, "%s:NULL interface", SENSOR_NAME);
187         // step 1
188         if (!controller->read_fd(data))
189                 return 0;
190
191         // step 2
192         const char *hub_data = data.values;
193         int data_len = data.value_count;
194
195         // step 3
196         event_ids.clear();
197
198         while (data_len > 0) {
199                 DBG("Remaining data length: %d", data_len);
200                 int parsed = parse(hub_data, data_len);
201                 if (parsed < 0) {
202                         ERR("Parsing failed");
203                         break;
204                 }
205
206                 data_len -= parsed;
207                 hub_data += parsed;
208         }
209
210         // step 4
211         int size = event_ids.size();
212
213         if (event_ids.empty())
214                 return 0;
215
216         *ids = &event_ids[0];
217
218         return size;
219 }
220
221 int sensorhub_device::get_data(uint32_t id, sensor_data_t **data, int *length)
222 {
223         int remains = 1;
224         retvm_if(data == NULL || data == nullptr, SENSOR_ERROR_INVALID_PARAMETER, "%s:NULL data interface", SENSOR_NAME);
225         retvm_if(length == NULL || length == nullptr, SENSOR_ERROR_INVALID_PARAMETER, "%s:NULL length interface", SENSOR_NAME);
226         retvm_if(id == 0 || id > MAX_ID, SENSOR_ERROR_INVALID_PARAMETER, "%s:Invalid ID Received", SENSOR_NAME);
227
228         sensorhub_sensor *sensor = manager->get_sensor(id);
229         if (!sensor) {
230                 ERR("Failed to get data from sensor(0x%x)", id);
231                 return -1;
232         }
233
234         remains = sensor->get_data(data, length);
235
236         return remains;
237 }
238
239 bool sensorhub_device::flush(uint32_t id)
240 {
241         return false;
242 }
243
244 int sensorhub_device::parse(const char *hub_data, int data_len)
245 {
246         return parse_data(hub_data, data_len);
247 }
248
249 int sensorhub_device::parse_data(const char *hub_data, int data_len)
250 {
251         const char *cursor = hub_data;
252         int32_t libtype = 0;
253
254         sensorhub_sensor *sensor = manager->get_sensor(libtype);
255         if (!sensor) {
256                 ERR("Unknown Sensorhub lib type: %d", libtype);
257                 return -1;
258         }
259
260         event_ids.push_back(sensor->get_id());
261
262         return sensor->parse(cursor, data_len);
263 }
264
265 int sensorhub_device::parse_debug(const char *hub_data, int data_len)
266 {
267         return 0;
268 }
269