aboutsummaryrefslogtreecommitdiff
path: root/sensors/bst/hal/BstSensorExt.cpp
blob: b87f60bd0596374a4e4ce1a03feb2665195888de (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <poll.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/select.h>

#include "BstSensorExt.h"

#include "TargetPlatform.h"

/*****************************************************************************/
const struct sensor_t BstSensorExt::g_bstext_sensor_list[] =
{
#ifdef __SIGNIFICANT_MOTION_SENSOR_SUPPORT__
	{ "BOSCH Significant Motion Sensor",
	  "Bosch Sensortec, GmbH",
	  1, SENSORS_SIGNIFICANT_MOTION_HANDLE,
	  SENSOR_TYPE_SIGNIFICANT_MOTION, 1.0f, 1.0f, 0.0f, -1,
#if __HAL_VER__ >= __SENSORS_DEVICE_API_VERSION_1_1__
         0,0,
#endif
#if __HAL_VER__ >= __SENSORS_DEVICE_API_VERSION_1_3__
         SENSOR_STRING_TYPE_SIGNIFICANT_MOTION,
         "",
         -1,
         0,
#endif
	  {}},
#endif

#ifdef __DOUBLE_TAP_SENSOR_SUPPORT__
	{ "BOSCH Double Tap Sensor",
	  "Bosch Sensortec ,GmbH",
	  1, SENSORS_DOUBLE_TAP_HANDLE,
	  BSTEXT_SENSOR_TYPE_DOUBLE_TAP, 2.0f, 1.0f, 0.0f, 0,
#if __HAL_VER__ >= __SENSORS_DEVICE_API_VERSION_1_1__
	 0,0,
#endif
#if __HAL_VER__ >= __SENSORS_DEVICE_API_VERSION_1_3__
         "bosch.double.tap.sensor",
         "",
         -1,
         0,
#endif
	  {}},
#endif
};


BstSensorExt::BstSensorExt()
    : SensorBase(NULL, BST_SENSOR_EXT_INPUT_DEV),
      mInputReader(4)
{
	mPendingEvent.version = sizeof(sensors_event_t);
	memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));
	memset(input_sysfs_path, 0, sizeof(input_sysfs_path));
	input_sysfs_path_len = 0;
#ifdef __DOUBLE_TAP_SENSOR_SUPPORT__
	mDtapTimes = 0;
#endif

}

BstSensorExt::~BstSensorExt()
{
#ifdef __SIGNIFICANT_MOTION_SENSOR_SUPPORT__
	enable(ID_SMT, 0);
#endif
#ifdef __DOUBLE_TAP_SENSOR_SUPPORT__
	enable(ID_DTAP, 0);
#endif
}

int BstSensorExt::enableSensor(int en, char * sensor_path, char * enable_mode)
{
	int ret = 0;
	int fd;
	char buf[2];
	strncpy(input_sysfs_path, sensor_path, strlen(sensor_path));
	input_sysfs_path_len = strlen(input_sysfs_path);
	strncpy(&input_sysfs_path[input_sysfs_path_len], enable_mode, strlen(enable_mode));
	fd = open(input_sysfs_path, O_RDWR);
	if (fd >= 0)
	{
		buf[1] = '\0';
		if (en)
			buf[0] = '1';
		else
			buf[0] = '0';
		write(fd, buf, sizeof(buf));
		close(fd);
		ret = 0;
	}
	else
	{
		ret = -EIO;
	}
	return ret;
}

int BstSensorExt::enable(int32_t id, int en)
{
	int ret = 0;

	PDEBUG("BstSensorExt: id=%d, en=%d", id, en);
#ifdef __SIGNIFICANT_MOTION_SENSOR_SUPPORT__
	if (ID_SMT == id)
	{
		ret = BstSensorExt::enableSensor(en, (char *)INPUT_SYSFS_PATH_SIGNIFICANT_MOTION_SENSOR,
								(char *)INPUT_SYSFS_PATH_SIGNIFICANT_MOTION_SENSOR_ENABLE_NODE);
	}
	else
#endif
#ifdef __DOUBLE_TAP_SENSOR_SUPPORT__
	if (ID_DTAP == id)
	{
		ret = BstSensorExt::enableSensor(en, (char *)INPUT_SYSFS_PATH_DOUBLE_TAP_SENSOR,
								(char *)INPUT_SYSFS_PATH_DOUBLE_TAP_SENSOR_ENABLE_NODE);
	}
	else
#endif
	{
		PERR("sensor id %d not found", id);
	}

	return ret;
}


int BstSensorExt::readEvents(sensors_event_t* data, int count)
{
	if (count < 1)
		return -EINVAL;

	ssize_t n = mInputReader.fill(data_fd);
	if (n < 0)
		return n;

	int numEventReceived = 0;
	input_event const* event;

	while (count && mInputReader.readEvent(&event))
	{
		PDEBUG("%s,event.type is %d,event value is %d,event code is %d",__FUNCTION__,event->type,event->value,event->code);
		if(event->type == EV_REL)
		{
			switch(event->code)
			{	case SINGLE_TAP_INTERRUPT:
					break;
				case LOW_G_INTERRUPT:
					break;
				case HIGH_G_INTERRUPT:
					break;

			#ifdef __SIGNIFICANT_MOTION_SENSOR_SUPPORT__
				case SIGNIFICANT_MOTION_INTERRUPT:
					// significant motion sensor is one-shot, and disabled in driver.
					mPendingEvent.sensor = ID_SMT;
					mPendingEvent.type = SENSOR_TYPE_SIGNIFICANT_MOTION;
					mPendingEvent.timestamp = timevalToNano(event->time);
					break;
			#else
				case SLOP_INTERRUPT:
					break;
			#endif

			#ifdef __DOUBLE_TAP_SENSOR_SUPPORT__
				case DOUBLE_TAP_INTERRUPT:
					mPendingEvent.sensor = ID_DTAP;
					mPendingEvent.type = BSTEXT_SENSOR_TYPE_DOUBLE_TAP;
					mPendingEvent.timestamp = timevalToNano(event->time);
					mPendingEvent.data[0] = mDtapTimes;
					mDtapTimes = (mDtapTimes >= 99) ? 0: mDtapTimes+1;
					break;
			#endif

				default:
					break;

			}
			*data++ = mPendingEvent;
			count--;
			numEventReceived++;
		}

		mInputReader.next();
	}

	return numEventReceived;
}

int BstSensorExt::checkSensorAvailable(int sensor_id)
{
	int fd;
	int ret = 0;
	char sysfs_path[PATH_MAX];
	int sysfs_path_len;
	char *p_path;
	char *p_enable_node;

	switch (sensor_id)
	{
	#ifdef __SIGNIFICANT_MOTION_SENSOR_SUPPORT__
		case BSTEXT_SID_SMT:
			p_path = (char*)INPUT_SYSFS_PATH_SIGNIFICANT_MOTION_SENSOR;
			p_enable_node = (char*)INPUT_SYSFS_PATH_SIGNIFICANT_MOTION_SENSOR_ENABLE_NODE;
			break;
	#endif

	#ifdef __DOUBLE_TAP_SENSOR_SUPPORT__
		case BSTEXT_SID_DTAP:
			p_path = (char*)INPUT_SYSFS_PATH_DOUBLE_TAP_SENSOR;
			p_enable_node = (char*)INPUT_SYSFS_PATH_DOUBLE_TAP_SENSOR_ENABLE_NODE;
			break;
	#endif

		default:
			return 0;
	}

	if (sensor_id < NUM_BSTEXT_SENSORS)
	{
		strcpy(sysfs_path, p_path);
		sysfs_path_len = strlen(sysfs_path);
		strcpy(&sysfs_path[sysfs_path_len], p_enable_node);

		fd = open(sysfs_path, O_RDONLY);
		if (fd >= 0)
		{
			close(fd);
			ret = 0;
		}
		else
			ret = -EIO;
	}

	PINFO("BstSensorExt::checkSensorAvailable, id, ret= %d, %d", sensor_id, ret);
	return ret;
}


int BstSensorExt::getSensorList(struct sensor_t *list, int len)
{
	int             num = 0;
	int j;

	UNUSED_PARAM(len);
	for (j = 0; j < NUM_BSTEXT_SENSORS; j++)
	{
		if ( BstSensorExt::checkSensorAvailable(j) == 0)
		{
			memcpy(list++,
				&BstSensorExt::g_bstext_sensor_list[j],
				sizeof(struct sensor_t));
			num++;
		}
	}

	return num;
}

const struct sensor_t * BstSensorExt::getSensor(int handle)
{
	const struct sensor_t *s = NULL;
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(g_bstext_sensor_list); i++) {
		if (g_bstext_sensor_list[i].handle == handle) {
			break;
		}
	}

	if (i < ARRAY_SIZE(g_bstext_sensor_list)) {
		s = (const struct sensor_t *) g_bstext_sensor_list + i;
	}

	return s;
}

#if __HAL_VER__ >= __SENSORS_DEVICE_API_VERSION_1_1__
int BstSensorExt::flush(int id)
{
	PDEBUG("flush id %d", id);

	UNUSED_PARAM(id);
	return -EINVAL;
}

int BstSensorExt::batch(int id, int flags, int64_t period_ns, int64_t timeout)
{
	int ret = 0;

	PDEBUG("batch id %d, flags %d, period_ns %lld, timeout %lld", id, flags, period_ns, timeout);

	UNUSED_PARAM(id);
	UNUSED_PARAM(flags);
	UNUSED_PARAM(period_ns);
	UNUSED_PARAM(timeout);
	//ret = BstSensorExt::setDelay(id, period_ns);
	return ret;
}
#endif