summaryrefslogtreecommitdiffstats
path: root/sensors/piranha_sensors.c
blob: bfb9f94a746ec0377bcd66c9aa4dce4d5ab7c430 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <errno.h>
#include <poll.h>
#include <sys/select.h>
#include <hardware/sensors.h>
#include <hardware/hardware.h>

#define LOG_TAG "piranha_sensors"
#include <utils/Log.h>

#include "piranha_sensors.h"

/*
 * Sensors list
 */

struct sensor_t piranha_sensors[] = {
	{ "BMA250 Acceleration Sensor", "Bosch", 1, SENSOR_TYPE_ACCELEROMETER,
		SENSOR_TYPE_ACCELEROMETER, 2 * GRAVITY_EARTH, GRAVITY_EARTH / 256.0f, 0.13f, 10000, {}, },
	{ "YAS530 Magnetic Sensor", "Yamaha", 1, SENSOR_TYPE_MAGNETIC_FIELD,
		SENSOR_TYPE_MAGNETIC_FIELD, 800.0f, 0.3f, 4.0f, 10000, {}, },
	{ "YAS Orientation Sensor", "Yamaha", 1, SENSOR_TYPE_ORIENTATION,
		SENSOR_TYPE_ORIENTATION, 360.0f, 0.1f, 0.0f, 10000, {}, },
	{ "GP2A Light Sensor", "Sharp", 1, SENSOR_TYPE_LIGHT,
		SENSOR_TYPE_LIGHT, 10240.0f, 1.0f, 0.75f, 0, {}, },
	{ "GP2A Proximity Sensor", "Sharp", 1, SENSOR_TYPE_PROXIMITY,
		SENSOR_TYPE_PROXIMITY, 5.0f, 5.0f, 0.75f, 0, {}, },
};

int piranha_sensors_count = sizeof(piranha_sensors) / sizeof(struct sensor_t);

struct piranha_sensors_handlers *piranha_sensors_handlers[] = {
	&bma250,
	&yas530,
	&yas_orientation,
	&gp2a_light,
	&gp2a_proximity,
};

int piranha_sensors_handlers_count = sizeof(piranha_sensors_handlers) /
	sizeof(struct piranha_sensors_handlers *);

/*
 * Piranha Sensors
 */

int piranha_sensors_activate(struct sensors_poll_device_t *dev, int handle,
	int enabled)
{
	struct piranha_sensors_device *device;
	int i;

	ALOGD("%s(%p, %d, %d)", __func__, dev, handle, enabled);

	if (dev == NULL)
		return -EINVAL;

	device = (struct piranha_sensors_device *) dev;

	if (device->handlers == NULL || device->handlers_count <= 0)
		return -EINVAL;

	for (i = 0; i < device->handlers_count; i++) {
		if (device->handlers[i] == NULL)
			continue;

		if (device->handlers[i]->handle == handle) {
			if (enabled && device->handlers[i]->activate != NULL) {
				device->handlers[i]->needed |= PIRANHA_SENSORS_NEEDED_API;
				if (device->handlers[i]->needed == PIRANHA_SENSORS_NEEDED_API)
					return device->handlers[i]->activate(device->handlers[i]);
				else
					return 0;
			} else if (!enabled && device->handlers[i]->deactivate != NULL) {
				device->handlers[i]->needed &= ~PIRANHA_SENSORS_NEEDED_API;
				if (device->handlers[i]->needed == 0)
					return device->handlers[i]->deactivate(device->handlers[i]);
				else
					return 0;
			}
		}
	}

	return -1;
}

int piranha_sensors_set_delay(struct sensors_poll_device_t *dev, int handle,
	int64_t ns)
{
	struct piranha_sensors_device *device;
	int i;

	ALOGD("%s(%p, %d, %ld)", __func__, dev, handle, (long int) ns);

	if (dev == NULL)
		return -EINVAL;

	device = (struct piranha_sensors_device *) dev;

	if (device->handlers == NULL || device->handlers_count <= 0)
		return -EINVAL;

	for (i = 0; i < device->handlers_count; i++) {
		if (device->handlers[i] == NULL)
			continue;

		if (device->handlers[i]->handle == handle && device->handlers[i]->set_delay != NULL)
			return device->handlers[i]->set_delay(device->handlers[i], (long int) ns);
	}

	return 0;
}

int piranha_sensors_poll(struct sensors_poll_device_t *dev,
	struct sensors_event_t* data, int count)
{
	struct piranha_sensors_device *device;
	int i, j;
	int c, n;
	int poll_rc, rc;

//	ALOGD("%s(%p, %p, %d)", __func__, dev, data, count);

	if (dev == NULL)
		return -EINVAL;

	device = (struct piranha_sensors_device *) dev;

	if (device->handlers == NULL || device->handlers_count <= 0 ||
		device->poll_fds == NULL || device->poll_fds_count <= 0)
		return -EINVAL;

	n = 0;

	do {
		poll_rc = poll(device->poll_fds, device->poll_fds_count, n > 0 ? 0 : -1);
		if (poll_rc < 0)
			return -1;

		for (i = 0; i < device->poll_fds_count; i++) {
			if (!(device->poll_fds[i].revents & POLLIN))
				continue;

			for (j = 0; j < device->handlers_count; j++) {
				if (device->handlers[j] == NULL || device->handlers[j]->poll_fd != device->poll_fds[i].fd || device->handlers[j]->get_data == NULL)
					continue;

				rc = device->handlers[j]->get_data(device->handlers[j], &data[n]);
				if (rc < 0) {
					device->poll_fds[i].revents = 0;
					poll_rc = -1;
				} else {
					n++;
					count--;
				}
			}
		}
	} while ((poll_rc > 0 || n < 1) && count > 0);

	return n;
}

/*
 * Interface
 */

int piranha_sensors_close(hw_device_t *device)
{
	struct piranha_sensors_device *piranha_sensors_device;
	int i;

	ALOGD("%s(%p)", __func__, device);

	if (device == NULL)
		return -EINVAL;

	piranha_sensors_device = (struct piranha_sensors_device *) device;

	if (piranha_sensors_device->poll_fds != NULL)
		free(piranha_sensors_device->poll_fds);

	for (i = 0; i < piranha_sensors_device->handlers_count; i++) {
		if (piranha_sensors_device->handlers[i] == NULL || piranha_sensors_device->handlers[i]->deinit == NULL)
			continue;

		piranha_sensors_device->handlers[i]->deinit(piranha_sensors_device->handlers[i]);
	}

	free(device);

	return 0;
}

int piranha_sensors_open(const struct hw_module_t* module, const char *id,
	struct hw_device_t** device)
{
	struct piranha_sensors_device *piranha_sensors_device;
	int p, i;

	ALOGD("%s(%p, %s, %p)", __func__, module, id, device);

	if (module == NULL || device == NULL)
		return -EINVAL;

	piranha_sensors_device = (struct piranha_sensors_device *)
		calloc(1, sizeof(struct piranha_sensors_device));
	piranha_sensors_device->device.common.tag = HARDWARE_DEVICE_TAG;
	piranha_sensors_device->device.common.version = 0;
	piranha_sensors_device->device.common.module = (struct hw_module_t *) module;
	piranha_sensors_device->device.common.close = piranha_sensors_close;
	piranha_sensors_device->device.activate = piranha_sensors_activate;
	piranha_sensors_device->device.setDelay = piranha_sensors_set_delay;
	piranha_sensors_device->device.poll = piranha_sensors_poll;
	piranha_sensors_device->handlers = piranha_sensors_handlers;
	piranha_sensors_device->handlers_count = piranha_sensors_handlers_count;
	piranha_sensors_device->poll_fds = (struct pollfd *)
		calloc(1, piranha_sensors_handlers_count * sizeof(struct pollfd));

	p = 0;
	for (i = 0; i < piranha_sensors_handlers_count; i++) {
		if (piranha_sensors_handlers[i] == NULL || piranha_sensors_handlers[i]->init == NULL)
			continue;

		piranha_sensors_handlers[i]->init(piranha_sensors_handlers[i], piranha_sensors_device);
		if (piranha_sensors_handlers[i]->poll_fd >= 0) {
			piranha_sensors_device->poll_fds[p].fd = piranha_sensors_handlers[i]->poll_fd;
			piranha_sensors_device->poll_fds[p].events = POLLIN;
			p++;
		}
	}

	piranha_sensors_device->poll_fds_count = p;

	*device = &(piranha_sensors_device->device.common);

	return 0;
}

int piranha_sensors_get_sensors_list(struct sensors_module_t* module,
	const struct sensor_t **sensors_p)
{
	ALOGD("%s(%p, %p)", __func__, module, sensors_p);

	if (sensors_p == NULL)
		return -EINVAL;

	*sensors_p = piranha_sensors;
	return piranha_sensors_count;
}

struct hw_module_methods_t piranha_sensors_module_methods = {
	.open = piranha_sensors_open,
};

struct sensors_module_t HAL_MODULE_INFO_SYM = {
	.common = {
		.tag = HARDWARE_MODULE_TAG,
		.version_major = 1,
		.version_minor = 0,
		.id = SENSORS_HARDWARE_MODULE_ID,
		.name = "Piranha Sensors",
		.author = "Paul Kocialkowski",
		.methods = &piranha_sensors_module_methods,
	},
	.get_sensors_list = piranha_sensors_get_sensors_list,
};