Use sensor_error_e of hal API
[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
61         retvm_if(sensors == NULL || sensors == nullptr, SENSOR_ERROR_INVALID_PARAMETER, "sensorhub_device:NULL interface");
62         size = manager->get_sensors(sensors);
63
64         return size;
65 }
66
67 bool sensorhub_device::enable(uint32_t id)
68 {
69         system_state_handler::get_instance().initialize();
70         retvm_if(id == 0 || id > MAX_ID, false, "%s:Invalid ID Received", SENSOR_NAME);
71         controller->enable();
72         sensorhub_sensor *sensor = manager->get_sensor(id);
73
74         if (!sensor) {
75                 ERR("Failed to enable sensor(0x%x)", id);
76                 return false;
77         }
78
79         return sensor->enable();
80 }
81
82 bool sensorhub_device::disable(uint32_t id)
83 {
84         system_state_handler::get_instance().finalize();
85         retvm_if(id == 0 || id > MAX_ID, false, "%s:Invalid ID Received", SENSOR_NAME);
86         controller->disable();
87         sensorhub_sensor *sensor = manager->get_sensor(id);
88
89         if (!sensor) {
90                 ERR("Failed to disable sensor(0x%x)", id);
91                 return false;
92         }
93
94         return sensor->disable();
95 }
96
97 bool sensorhub_device::set_interval(uint32_t id, unsigned long val)
98 {
99         sensorhub_sensor *sensor = NULL;
100         retvm_if(id == 0 || id > MAX_ID, false, "%s:Invalid ID Received", SENSOR_NAME);
101         retvm_if(val < 0, false, "%s:Invalid value Received", SENSOR_NAME);
102         sensor = manager->get_sensor(id);
103         if (!sensor) {
104                 ERR("Failed to set interval to sensor(0x%x)", id);
105                 return false;
106         }
107
108         return sensor->set_interval(val);
109 }
110
111 bool sensorhub_device::set_batch_latency(uint32_t id, unsigned long val)
112 {
113         sensorhub_sensor *sensor = NULL;
114         retvm_if(id == 0 || id > MAX_ID, false, "%s:Invalid ID Received", SENSOR_NAME);
115         sensor = manager->get_sensor(id);
116
117         if (!sensor) {
118                 ERR("Failed to set batch latency to sensor(0x%x)", id);
119                 return false;
120         }
121
122         return sensor->set_batch_latency(val);
123 }
124
125 bool sensorhub_device::set_attribute_int(uint32_t id, int32_t attribute, int32_t value)
126 {
127         int ret;
128
129         sensorhub_sensor *sensor = NULL;
130         retvm_if(id == 0 || id > MAX_ID, false, "%s:Invalid ID Received", SENSOR_NAME);
131         retvm_if(value < 0, false, "%s:Invalid value Received", SENSOR_NAME);
132         retvm_if(attribute < 0, false, "%s:Invalid attribute Received", SENSOR_NAME);
133
134         sensor = manager->get_sensor(id);
135         if (!sensor) {
136                 ERR("Failed to set attribute to sensor(0x%x)", id);
137                 return false;
138         }
139
140         ret = sensor->set_attribute_int(attribute, value);
141
142         if ((ret < 0) && (ret != -EBUSY)) {
143                 ERR("Failed to send sensorhub data");
144                 return false;
145         }
146
147         if (ret == -EBUSY) {
148                 WARN("Command is sent during sensorhub firmware update");
149                 return false;
150         }
151
152         return true;
153 }
154
155 bool sensorhub_device::set_attribute_str(uint32_t id, int32_t attribute, char *value, int value_len)
156 {
157         int ret;
158
159         sensorhub_sensor *sensor = NULL;
160         retvm_if(id == 0 || id > MAX_ID, false, "%s:Invalid ID Received", SENSOR_NAME);
161         retvm_if(value == NULL || value == nullptr, false, "%s:Invalid string Received", SENSOR_NAME);
162         retvm_if(value_len <= 0, false, "%s:Invalid value_len Received", SENSOR_NAME);
163         sensor = manager->get_sensor(id);
164
165         if (!sensor) {
166                 ERR("Failed to set attribute to sensor(0x%x)", id);
167                 return false;
168         }
169
170         ret = sensor->set_attribute_str(attribute, value, value_len);
171
172         if ((ret < 0) && (ret != -EBUSY)) {
173                 ERR("Failed to send sensorhub data");
174                 return false;
175         }
176
177         if (ret == -EBUSY) {
178                 WARN("Command is sent during sensorhub firmware update");
179                 return false;
180         }
181
182         return true;
183 }
184
185 int sensorhub_device::read_fd(uint32_t **ids)
186 {
187         sensorhub_data_t data;
188         retvm_if(ids == NULL || ids == nullptr, SENSOR_ERROR_INVALID_PARAMETER, "%s:NULL interface", SENSOR_NAME);
189         // step 1
190         if (!controller->read_fd(data))
191                 return 0;
192
193         // step 2
194         const char *hub_data = data.values;
195         int data_len = data.value_count;
196
197         // step 3
198         event_ids.clear();
199
200         while (data_len > 0) {
201                 DBG("Remaining data length: %d", data_len);
202                 int parsed = parse(hub_data, data_len);
203                 if (parsed < 0) {
204                         ERR("Parsing failed");
205                         break;
206                 }
207
208                 data_len -= parsed;
209                 hub_data += parsed;
210         }
211
212         // step 4
213         int size = event_ids.size();
214
215         if (event_ids.empty())
216                 return 0;
217
218         *ids = &event_ids[0];
219
220         return size;
221 }
222
223 int sensorhub_device::get_data(uint32_t id, sensor_data_t **data, int *length)
224 {
225         int remains = 1;
226         retvm_if(data == NULL || data == nullptr, SENSOR_ERROR_INVALID_PARAMETER, "%s:NULL data interface", SENSOR_NAME);
227         retvm_if(length == NULL || length == nullptr, SENSOR_ERROR_INVALID_PARAMETER, "%s:NULL length interface", SENSOR_NAME);
228         retvm_if(id == 0 || id > MAX_ID, SENSOR_ERROR_INVALID_PARAMETER, "%s:Invalid ID Received", SENSOR_NAME);
229
230         sensorhub_sensor *sensor = manager->get_sensor(id);
231         if (!sensor) {
232                 ERR("Failed to get data from sensor(0x%x)", id);
233                 return -1;
234         }
235
236         remains = sensor->get_data(data, length);
237
238         return remains;
239 }
240
241 bool sensorhub_device::flush(uint32_t id)
242 {
243         return false;
244 }
245
246 int sensorhub_device::parse(const char *hub_data, int data_len)
247 {
248         return parse_data(hub_data, data_len);
249 }
250
251 int sensorhub_device::parse_data(const char *hub_data, int data_len)
252 {
253         const char *cursor = hub_data;
254         int32_t libtype = 0;
255
256         sensorhub_sensor *sensor = manager->get_sensor(libtype);
257         if (!sensor) {
258                 ERR("Unknown Sensorhub lib type: %d", libtype);
259                 return -1;
260         }
261
262         event_ids.push_back(sensor->get_id());
263
264         return sensor->parse(cursor, data_len);
265 }
266
267 int sensorhub_device::parse_debug(const char *hub_data, int data_len)
268 {
269         return 0;
270 }
271