2 * Copyright (C) 2013 Paul Kocialkowski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <hardware/sensors.h>
26 #include <hardware/hardware.h>
28 #define LOG_TAG "piranha_sensors"
29 #include <utils/Log.h>
31 #include "piranha_sensors.h"
37 struct sensor_t piranha_sensors[] = {
38 { "BMA254 Acceleration Sensor", "Bosch", 1, SENSOR_TYPE_ACCELEROMETER,
39 SENSOR_TYPE_ACCELEROMETER, 19.6f, 0.0383f, 0.13f, 10000, {}, },
40 { "YAS530 Magnetic Sensor", "Yamaha", 1, SENSOR_TYPE_MAGNETIC_FIELD,
41 SENSOR_TYPE_MAGNETIC_FIELD, 800.0f, 0.3f, 4.0f, 10000, {}, },
42 { "YAS Orientation Sensor", "Yamaha", 1, SENSOR_TYPE_ORIENTATION,
43 SENSOR_TYPE_ORIENTATION, 360.0f, 0.1f, 0.0f, 10000, {}, },
44 { "BH1721 Light Sensor", "ROHM", 1, SENSOR_TYPE_LIGHT,
45 SENSOR_TYPE_LIGHT, 0.0f, 0.0f, 0.0f, 0, {}, },
48 int piranha_sensors_count = sizeof(piranha_sensors) / sizeof(struct sensor_t);
50 struct piranha_sensors_handlers *piranha_sensors_handlers[] = {
57 int piranha_sensors_handlers_count = sizeof(piranha_sensors_handlers) /
58 sizeof(struct piranha_sensors_handlers *);
64 int piranha_sensors_activate(struct sensors_poll_device_t *dev, int handle, int enabled)
66 struct piranha_sensors_device *device;
69 LOGD("%s(%p, %d, %d)", __func__, dev, handle, enabled);
74 device = (struct piranha_sensors_device *) dev;
76 if (device->handlers == NULL || device->handlers_count <= 0)
79 for (i=0 ; i < device->handlers_count ; i++) {
80 if (device->handlers[i] == NULL)
83 if (device->handlers[i]->handle == handle) {
84 if (enabled && device->handlers[i]->activate != NULL)
85 return device->handlers[i]->activate(device->handlers[i]);
86 else if (!enabled && device->handlers[i]->deactivate != NULL)
87 return device->handlers[i]->deactivate(device->handlers[i]);
94 int piranha_sensors_set_delay(struct sensors_poll_device_t *dev, int handle, int64_t ns)
96 struct piranha_sensors_device *device;
99 LOGD("%s(%p, %d, %ld)", __func__, dev, handle, (long int) ns);
104 device = (struct piranha_sensors_device *) dev;
106 if (device->handlers == NULL || device->handlers_count <= 0)
109 for (i=0 ; i < device->handlers_count ; i++) {
110 if (device->handlers[i] == NULL)
113 if (device->handlers[i]->handle == handle && device->handlers[i]->set_delay != NULL)
114 return device->handlers[i]->set_delay(device->handlers[i], ns);
120 int piranha_sensors_poll(struct sensors_poll_device_t *dev,
121 struct sensors_event_t* data, int count)
123 struct piranha_sensors_device *device;
128 // LOGD("%s(%p, %p, %d)", __func__, dev, data, count);
133 device = (struct piranha_sensors_device *) dev;
135 if (device->handlers == NULL || device->handlers_count <= 0 || device->poll_fds == NULL)
138 memset(device->poll_fds, 0, device->handlers_count * sizeof(struct pollfd));
141 for (i=0 ; i < device->handlers_count ; i++) {
142 if (device->handlers[i] == NULL)
145 if (device->handlers[i]->activated && device->handlers[i]->poll_fd >= 0) {
146 device->poll_fds[index].fd = device->handlers[i]->poll_fd;
147 device->poll_fds[index].events = POLLIN;
152 // No sensor is enabled
159 for (c=0 ; c < count ; c++) {
160 rc = poll(device->poll_fds, index, 10);
164 for (i=0 ; i < index ; i++) {
165 for (j=0 ; j < device->handlers_count ; j++) {
166 if (device->handlers[j] == NULL)
169 if (device->poll_fds[i].revents & POLLIN && device->poll_fds[i].fd == device->handlers[j]->poll_fd && device->handlers[j]->get_data != NULL) {
170 rc = device->handlers[j]->get_data(device->handlers[j], &data[n]);
178 device->poll_fds[i].revents = 0;
192 int piranha_sensors_close(hw_device_t *device)
194 struct piranha_sensors_device *piranha_sensors_device;
197 LOGD("%s(%p)", __func__, device);
202 piranha_sensors_device = (struct piranha_sensors_device *) device;
204 if (piranha_sensors_device->poll_fds != NULL)
205 free(piranha_sensors_device->poll_fds);
207 for (i=0 ; i < piranha_sensors_device->handlers_count ; i++) {
208 if (piranha_sensors_device->handlers[i] == NULL || piranha_sensors_device->handlers[i]->deinit == NULL)
211 piranha_sensors_device->handlers[i]->deinit(piranha_sensors_device->handlers[i]);
219 int piranha_sensors_open(const struct hw_module_t* module, const char *id,
220 struct hw_device_t** device)
222 struct piranha_sensors_device *piranha_sensors_device;
225 LOGD("%s(%p, %s, %p)", __func__, module, id, device);
227 if (module == NULL || device == NULL)
230 piranha_sensors_device = (struct piranha_sensors_device *)
231 calloc(1, sizeof(struct piranha_sensors_device));
232 piranha_sensors_device->device.common.tag = HARDWARE_DEVICE_TAG;
233 piranha_sensors_device->device.common.version = 0;
234 piranha_sensors_device->device.common.module = (struct hw_module_t *) module;
235 piranha_sensors_device->device.common.close = piranha_sensors_close;
236 piranha_sensors_device->device.activate = piranha_sensors_activate;
237 piranha_sensors_device->device.setDelay = piranha_sensors_set_delay;
238 piranha_sensors_device->device.poll = piranha_sensors_poll;
240 piranha_sensors_device->handlers = piranha_sensors_handlers;
241 piranha_sensors_device->handlers_count = piranha_sensors_handlers_count;
242 piranha_sensors_device->poll_fds = (struct pollfd *)
243 malloc(piranha_sensors_handlers_count * sizeof(struct pollfd));
245 for (i=0 ; i < piranha_sensors_handlers_count ; i++) {
246 if (piranha_sensors_handlers[i] == NULL || piranha_sensors_handlers[i]->init == NULL)
249 piranha_sensors_handlers[i]->init(piranha_sensors_handlers[i], piranha_sensors_device);
252 *device = &(piranha_sensors_device->device.common);
257 int piranha_sensors_get_sensors_list(struct sensors_module_t* module,
258 const struct sensor_t **sensors_p)
260 LOGD("%s(%p, %p)", __func__, module, sensors_p);
262 if (sensors_p == NULL)
265 *sensors_p = piranha_sensors;
266 return piranha_sensors_count;
269 struct hw_module_methods_t piranha_sensors_module_methods = {
270 .open = piranha_sensors_open,
273 struct sensors_module_t HAL_MODULE_INFO_SYM = {
275 .tag = HARDWARE_MODULE_TAG,
278 .id = SENSORS_HARDWARE_MODULE_ID,
279 .name = "Piranha Sensors",
280 .author = "Paul Kocialkowski",
281 .methods = &piranha_sensors_module_methods,
283 .get_sensors_list = piranha_sensors_get_sensors_list,