aboutsummaryrefslogtreecommitdiff
path: root/sensors/bst/hal/BstSensor.cpp
blob: 73618ede349967e83abf4f61ebf0a6b437df8f24 (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/*!
 * @section LICENSE
 *
 * (C) Copyright 2011~2014 Bosch Sensortec GmbH All Rights Reserved
 *
 * 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.
 *
 *------------------------------------------------------------------------------
 * Disclaimer
 *
 * Common: Bosch Sensortec products are developed for the consumer goods
 * industry. They may only be used within the parameters of the respective valid
 * product data sheet.  Bosch Sensortec products are provided with the express
 * understanding that there is no warranty of fitness for a particular purpose.
 * They are not fit for use in life-sustaining, safety or security sensitive
 * systems or any system or device that may lead to bodily harm or property
 * damage if the system or device malfunctions. In addition, Bosch Sensortec
 * products are not fit for use in products which interact with motor vehicle
 * systems.  The resale and/or use of products are at the purchaser's own risk
 * and his own responsibility. The examination of fitness for the intended use
 * is the sole responsibility of the Purchaser.
 *
 * The purchaser shall indemnify Bosch Sensortec from all third party claims,
 * including any claims for incidental, or consequential damages, arising from
 * any product use not covered by the parameters of the respective valid product
 * data sheet or not approved by Bosch Sensortec and reimburse Bosch Sensortec
 * for all costs in connection with such claims.
 *
 * The purchaser must monitor the market for the purchased products,
 * particularly with regard to product safety and inform Bosch Sensortec without
 * delay of all security relevant incidents.
 *
 * Engineering Samples are marked with an asterisk (*) or (e). Samples may vary
 * from the valid technical specifications of the product series. They are
 * therefore not intended or fit for resale to third parties or for use in end
 * products. Their sole purpose is internal client testing. The testing of an
 * engineering sample may in no way replace the testing of a product series.
 * Bosch Sensortec assumes no liability for the use of engineering samples. By
 * accepting the engineering samples, the Purchaser agrees to indemnify Bosch
 * Sensortec from all claims arising from the use of engineering samples.
 *
 * Special: This software module (hereinafter called "Software") and any
 * information on application-sheets (hereinafter called "Information") is
 * provided free of charge for the sole purpose to support your application
 * work. The Software and Information is subject to the following terms and
 * conditions:
 *
 * The Software is specifically designed for the exclusive use for Bosch
 * Sensortec products by personnel who have special experience and training. Do
 * not use this Software if you do not have the proper experience or training.
 *
 * This Software package is provided `` as is `` and without any expressed or
 * implied warranties, including without limitation, the implied warranties of
 * merchantability and fitness for a particular purpose.
 *
 * Bosch Sensortec and their representatives and agents deny any liability for
 * the functional impairment of this Software in terms of fitness, performance
 * and safety. Bosch Sensortec and their representatives and agents shall not be
 * liable for any direct or indirect damages or injury, except as otherwise
 * stipulated in mandatory applicable law.
 *
 * The Information provided is believed to be accurate and reliable. Bosch
 * Sensortec assumes no responsibility for the consequences of use of such
 * Information nor for any infringement of patents or other rights of third
 * parties which may result from its use.
 *
 * @file         BstSensor.cpp
 * @date         "Wed Jul 10 16:42:40 2013 +0800"
 * @commit       "0bdd1f5"
 *
 * @brief
 *
 * @detail
 *
 */


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

#include <linux/fs.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>


#include "BstSensor.h"
#include "BstSensorInfo.h"
#include "sensors.h"

#include "TargetPlatform.h"

#define GET_HANDLES_TRY_NUM         20

const int BstSensor::s_tab_id2handle[BST_SENSOR_NUM_MAX] = {
	BST_SENSOR_HANDLE_ACCELERATION,         /* 0 */
#ifdef __USECASE_TYPE_ACCEL_ONLY__
	-1,
	-1,
	-1,
	-1,
	-1,
	-1,
	-1,
	BST_SENSOR_HANDLE_GEST_FLIP,
#else
	BST_SENSOR_HANDLE_MAGNETIC_FIELD,       /* 1 */
	BST_SENSOR_HANDLE_GYROSCOPE,            /* 2 */
	BST_SENSOR_HANDLE_ORIENTATION,          /* 3 */
	BST_SENSOR_HANDLE_GRAVITY,              /* 4 */
	BST_SENSOR_HANDLE_LINEAR_ACCELERATION,  /* 5 */
	BST_SENSOR_HANDLE_ROTATION_VECTOR,      /* 6 */
	BST_SENSOR_HANDLE_PRESSURE,             /* 7 */
	BST_SENSOR_HANDLE_GEST_FLIP,            /* 8 */
#ifdef __UNSTANDARD_SENSOR_SUPPORT__
	BST_SENSOR_HANDLE_SW_SIGNIFICANT_MOTION,
#else
	-1,
#endif
#ifdef __UNCALIBRATED_VIRTUAL_SENSOR_SUPPORT__
	BST_SENSOR_HANDLE_GAME_ROTATION_VECTOR,
	BST_SENSOR_HANDLE_GYROSCOPE_UNCALIBRATED,
	BST_SENSOR_HANDLE_MAGNETIC_FIELD_UNCALIBRATED,
#endif
#if __HAL_VER__ >= __SENSORS_DEVICE_API_VERSION_1_1__
	BST_SENSOR_HANDLE_GEOMAGNETIC_ROTATION_VECTOR,
#endif
#if __HAL_VER__ >= __SENSORS_DEVICE_API_VERSION_1_0__
#ifdef __UNSTANDARD_SENSOR_SUPPORT__
	BST_SENSOR_HANDLE_STEP_COUNTER,
	BST_SENSOR_HANDLE_STEP_DETECTOR,
#else
	-1,
	-1,
#endif
#endif
#endif
	-1,                                     /* 11 */
	-1                                      /* 12 */
};


const int BstSensor::s_tab_handle2id[BST_SENSOR_NUM_MAX] = {
	ID_A,           /* 1 */
	ID_M,           /* 2 */
	ID_O,           /* 3 */
	ID_GY,          /* 4 */
	-1,             /* 5 */
	ID_PR,          /* 6 */
	-1,             /* 7 */
	-1,             /* 8 */
	ID_GR,          /* 9 */
	ID_LA,          /* 10 */
	ID_RV,          /* 11 */
	-1,             /* 12 */
	ID_GEST_FLIP,    /* 13 */
	ID_SGM,
#ifdef __UNCALIBRATED_VIRTUAL_SENSOR_SUPPORT__
	ID_GRV,          /* 9: game rotation vector */
	ID_GYU,          /* 10: gyro uncalibrated */
	ID_MU,          /* 11: magnetic uncalibrated */
#endif

#if __HAL_VER__ >= __SENSORS_DEVICE_API_VERSION_1_1__
	ID_GEOM_RV,
#endif
#if __HAL_VER__ >= __SENSORS_DEVICE_API_VERSION_1_0__
	ID_STC,
	ID_STD,
#endif
};

int8_t  handles[BST_SENSOR_NUM_MAX];
int     numOfHandles = 0;

int BstSensor::initStorage()
{
	char *path = NULL;
	int err = 0;
	struct stat st;
	int retry = 0;

	path = (char *)PATH_DIR_SENSOR_STORAGE;
	err = access(path, F_OK);
	if (!err) {
		st.st_mode = ~S_IFDIR;
		err = stat(path, &st);
		if (!S_ISDIR(st.st_mode)) {
			LOGE("<BST>" "a file of the same name exists");
			/* mark error explicitly for creating the dir */
			err = -ENOTDIR;
		}
	}

	while (0 !=err && retry < 4) {
		LOGE("<BST>" "create path of storage, err: %d, retry count %d", err, retry);
		unlink(path);
		err = mkdir(path, 0775);
		if (err) {
			LOGE("<BST>" "error creating storage dir");
		}
		retry++;
		usleep(1000);
	}

	return err;
}


int BstSensor::initIPC()
{
	int err = 0;
	char *filename;

	err = initStorage();
	if (err) {
		LOGE("initStorage return %d", err);
		return err;
	}

	filename = (char *)FIFO_CMD;
	err = access(filename, F_OK | R_OK | W_OK);
	if (err) {
		LOGE("<BST> " "can not access %s", filename);
		return err;
	}

	mCmdFd = open(filename, O_RDWR);
	if (mCmdFd < 0) {
		LOGE("<BST> " "error openning file: %s", filename);
		mCmdFd = -abs(errno);
		err = mCmdFd;
		return err;
	}

	filename = (char *)FIFO_DAT;
	err = access(filename, F_OK | R_OK | W_OK);
	if (err) {
		LOGE("<BST> " "can not access %s", filename);
		return err;
	}

	data_fd = open(filename, O_RDWR);
	if (data_fd < 0) {
		LOGE("<BST> " "error openning file: %s", filename);
		err = data_fd;
		return err;
	}

	data_name = filename;

	return err;
}

BstSensor::BstSensor()
	: SensorBase(NULL, NULL),
	mEnabled(0)
{
	struct exchange cmd;
	int             i = 0;
	int             ret = 0;
	int             fData = -1;

	do {
		if (i ++ > GET_HANDLES_TRY_NUM)
		{
			LOGE("initIPC failed:%d", ret);
			return;
		}
		ret = initIPC();
		if (!ret)
		{
			break;
		}
		sleep(1);
	} while(ret);

	/* if handles already initialized */
	if (numOfHandles > 0)
	{
		return;
	}

	fData = open(FIFO_DAT, O_RDONLY|O_NONBLOCK);
	if (fData == -1)
	{
		LOGE("open fifo data failed:%d", fData);
		return;
	}

	memset(&cmd, 0, sizeof(cmd));
	cmd.magic = CHANNEL_PKT_MAGIC_LIST;
	while (numOfHandles == 0)
	{
		int ret = 0;
		ret = write(mCmdFd, &cmd, sizeof(cmd));
		sleep(1);
		numOfHandles = read(fData, handles,
			BST_SENSOR_NUM_MAX);
		LOGD("BstSensor write ret %d read num = %d", ret, numOfHandles);
		if (i ++ > GET_HANDLES_TRY_NUM)
		{
			LOGE("reading num of handles time out\n");
			break;
		}
	}
	close(fData);
}

BstSensor::~BstSensor()
{
	/* mCmdFd is added by BstSensor, while
	 * data_fd is added by SensorBase
	 * so here, only close mCmdFd */
	if (mCmdFd >= 0) {
		close(mCmdFd);
	}
}


int BstSensor::id2handle(int32_t id)
{
	if ( (id <= ID_SENSOR_BASE_BST) ||
			(id >= ID_SENSOR_END_BST) ) {
		return -1;
	} else {
		return BstSensor::s_tab_id2handle[id - 1 - ID_SENSOR_BASE_BST];
	}
}


int BstSensor::handle2id(int32_t handle)
{
	if ( (handle - 1 < 0) || (handle - 1 >= BST_SENSOR_NUM_MAX) ) {
		LOGE("<BST> " "Invalid handle %d", handle);
		return -1;
	} else {
		return BstSensor::s_tab_handle2id[handle - 1];
	}
}


int BstSensor::enable(int32_t id, int enable)
{
	int err = 0;
	int handle;
	struct exchange cmd;
	int pos = (int)id;

	const struct sensor_t *s;

	handle = BstSensor::id2handle(id);
	if (-1 == handle) {
		return -EINVAL;
	}

	if (mCmdFd < 0) {
		LOGE("<BST> " "cannot tx cmd: %s",
				(char *)strerror(-mCmdFd));
		return mCmdFd;
	}

	enable = !!enable;


	s = BstSensorInfo::getSensor(id);
	LOGI("<BST> " "%s sensor <%s>",
			enable ? "enable" : "disable",
			(s != NULL) ? s->name : "unknown");

	cmd.magic = CHANNEL_PKT_MAGIC_CMD;
	cmd.command.cmd = SET_SENSOR_ACTIVE;
	cmd.command.code = handle;
	cmd.command.value = enable;
	cmd.ts = 0;
	err = write(mCmdFd, &cmd, sizeof(cmd));
	err = err < (int)sizeof(cmd) ? -1 : 0;

	if (!err) {
		if (enable) {
			mEnabled |= (1 << pos);
		} else {
			mEnabled &= ~(1 << pos);
		}
	}

	return err;
}


int BstSensor::setDelay(int32_t id, int64_t ns)
{
	int err = 0;
	int handle;
	struct exchange cmd;
	const struct sensor_t *s;

	handle = BstSensor::id2handle(id);
	if (-1 == handle) {
		return -EINVAL;
	}

	if (mCmdFd < 0) {
		LOGE("<BST> " "cannot tx cmd: %s",
				(char *)strerror(-mCmdFd));
		return mCmdFd;
	}

	cmd.magic = CHANNEL_PKT_MAGIC_CMD;

	cmd.command.cmd = SET_SENSOR_DELAY;
	cmd.command.code = handle;
	cmd.command.value = ns / SCALE_TIME_MS2NS;
	cmd.ts = 0;

	s = BstSensorInfo::getSensor(id);
	LOGI("<BST> " "set delay of <%s> to %jdms",
			(s != NULL) ? s->name : "unknown",
			ns / SCALE_TIME_MS2NS);

	err = write(mCmdFd, &cmd, sizeof(cmd));
	err = err < (int)sizeof(cmd) ? -1 : 0;

	if (!err) {
		mDelays[id - ID_SENSOR_BASE_BST] = ns / SCALE_TIME_MS2NS;
	}

	return err;
}


int BstSensor::readEvents(sensors_event_t *pdata, int count)
{
	int rslt;
	int err;
	struct exchange sensor_data;
	int sensor;
	sensors_event_t *pdata_cur;
#if __HAL_VER__ >= __SENSORS_DEVICE_API_VERSION_1_1__
	sensors_meta_data_event_t *p_flush_finish_event;
#endif

	if (count > 1) {
		count = 1;
	}
	rslt = 0;
	pdata_cur = pdata;
	while (rslt < count) {
#if 0
		fds.revents = 0;
		err = poll(&fds, 1, BST_DATA_POLL_TIMEOUT);
		if (err <= 0) {
			return rslt;
		}
#endif

		err = read(data_fd, &sensor_data, sizeof(sensor_data));
		if (err < (int)sizeof(sensor_data)) {
			LOGE("<BST> " "bad condition, stream needs sync");
			return rslt;
		}

		if (CHANNEL_PKT_MAGIC_DAT != sensor_data.magic) {
			LOGE("<BST> " "discard invalid data packet from stream");
			return rslt;
		}

#if __HAL_VER__ >= __SENSORS_DEVICE_API_VERSION_1_1__
		if(SENSOR_TYPE_META_DATA == sensor_data.data.type)
		{
			sensor = sensor_data.data.sensor;
			p_flush_finish_event = pdata_cur;
			p_flush_finish_event->version = META_DATA_VERSION;
			p_flush_finish_event->type = SENSOR_TYPE_META_DATA;
			p_flush_finish_event->meta_data.what = META_DATA_FLUSH_COMPLETE;
			p_flush_finish_event->meta_data.sensor = BstSensor::handle2id(sensor);
			PINFO("<BST> " "report flush finish event for sensor id: %d",
					p_flush_finish_event->meta_data.sensor);
			goto FLUSH_FINISH_EVENT_HANDLED;
		}
#endif
		sensor = sensor_data.data.sensor;
		pdata_cur->version = sizeof(*pdata_cur);
		pdata_cur->sensor = BstSensor::handle2id(sensor);
		pdata_cur->timestamp = sensor_data.ts;

		switch (pdata_cur->sensor) {
		case SENSORS_ACCELERATION_HANDLE:
			pdata_cur->acceleration.x = sensor_data.data.acceleration.x;
			pdata_cur->acceleration.y = sensor_data.data.acceleration.y;
			pdata_cur->acceleration.z = sensor_data.data.acceleration.z;
			pdata_cur->acceleration.status =
				sensor_data.data.status;
			pdata_cur->type = SENSOR_TYPE_ACCELEROMETER;
			break;
		case SENSORS_GYROSCOPE_HANDLE:
			pdata_cur->gyro.x = sensor_data.data.gyro.x;
			pdata_cur->gyro.y = sensor_data.data.gyro.y;
			pdata_cur->gyro.z = sensor_data.data.gyro.z;
			pdata_cur->gyro.status = sensor_data.data.status;
			pdata_cur->type = SENSOR_TYPE_GYROSCOPE;
			break;
		case SENSORS_MAGNETIC_FIELD_HANDLE:
			pdata_cur->magnetic.x = sensor_data.data.magnetic.x;
			pdata_cur->magnetic.y = sensor_data.data.magnetic.y;
			pdata_cur->magnetic.z = sensor_data.data.magnetic.z;
			pdata_cur->magnetic.status = sensor_data.data.status;
			pdata_cur->type = SENSOR_TYPE_MAGNETIC_FIELD;
			break;
		case SENSORS_ORIENTATION_HANDLE:
			pdata_cur->orientation.azimuth = sensor_data.data.orientation.azimuth;
			pdata_cur->orientation.pitch = sensor_data.data.orientation.pitch;
			pdata_cur->orientation.roll = sensor_data.data.orientation.roll;
			pdata_cur->orientation.status = sensor_data.data.status;
			pdata_cur->type = SENSOR_TYPE_ORIENTATION;
			break;
		case SENSORS_PRESSURE_HANDLE:
			pdata_cur->pressure = sensor_data.data.pressure;
			pdata_cur->type = SENSOR_TYPE_PRESSURE;
			break;
		case SENSORS_GRAVITY_HANDLE:
			pdata_cur->data[0] = sensor_data.data.data[0];
			pdata_cur->data[1] = sensor_data.data.data[1];
			pdata_cur->data[2] = sensor_data.data.data[2];
			pdata_cur->type = SENSOR_TYPE_GRAVITY;
			break;
		case SENSORS_LINEAR_ACCEL_HANDLE:
			pdata_cur->data[0] = sensor_data.data.data[0];
			pdata_cur->data[1] = sensor_data.data.data[1];
			pdata_cur->data[2] = sensor_data.data.data[2];
			pdata_cur->type = SENSOR_TYPE_LINEAR_ACCELERATION;
			break;
		case SENSORS_ROTATION_VECTOR_HANDLE:
			pdata_cur->data[0] = sensor_data.data.data[0];
			pdata_cur->data[1] = sensor_data.data.data[1];
			pdata_cur->data[2] = sensor_data.data.data[2];
		#if __HAL_VER__ >= __SENSORS_DEVICE_API_VERSION_1_0__
			pdata_cur->data[3] = sensor_data.data.data[3];
			pdata_cur->data[4] = sensor_data.data.data[4];
		#endif
			pdata_cur->type = SENSOR_TYPE_ROTATION_VECTOR;
			break;
		case SENSORS_GEST_FLIP_HANDLE:
			pdata_cur->data[0] = sensor_data.data.data[0];
			pdata_cur->data[1] = sensor_data.data.data[1];
			pdata_cur->data[2] = sensor_data.data.data[2];
			pdata_cur->type = BST_SENSOR_TYPE_GEST_FLIP;
			break;

	#ifdef __UNCALIBRATED_VIRTUAL_SENSOR_SUPPORT__
		case SENSORS_GAME_ROTATION_VECTOR_HANDLE:
			pdata_cur->data[0] = sensor_data.data.data[0];
			pdata_cur->data[1] = sensor_data.data.data[1];
			pdata_cur->data[2] = sensor_data.data.data[2];
			pdata_cur->data[3] = sensor_data.data.data[3];
			pdata_cur->data[4] = sensor_data.data.data[4];
			pdata_cur->type = SENSOR_TYPE_GAME_ROTATION_VECTOR;
			break;
		case SENSORS_GYROSCOPE_UNCALIBRATED_HANDLE:
			pdata_cur->uncalibrated_gyro.x_uncalib = sensor_data.data.uncalibrated_gyro.x_uncalib;
			pdata_cur->uncalibrated_gyro.y_uncalib = sensor_data.data.uncalibrated_gyro.y_uncalib;
			pdata_cur->uncalibrated_gyro.z_uncalib = sensor_data.data.uncalibrated_gyro.z_uncalib;
			pdata_cur->uncalibrated_gyro.x_bias = sensor_data.data.uncalibrated_gyro.x_bias;
			pdata_cur->uncalibrated_gyro.y_bias = sensor_data.data.uncalibrated_gyro.y_bias;
			pdata_cur->uncalibrated_gyro.z_bias = sensor_data.data.uncalibrated_gyro.z_bias;
			pdata_cur->type = SENSOR_TYPE_GYROSCOPE_UNCALIBRATED;
			break;
		case SENSORS_MAGNETIC_UNCALIBRATED_HANDLE:
			pdata_cur->uncalibrated_magnetic.x_uncalib = sensor_data.data.uncalibrated_magnetic.x_uncalib;
			pdata_cur->uncalibrated_magnetic.y_uncalib = sensor_data.data.uncalibrated_magnetic.y_uncalib;
			pdata_cur->uncalibrated_magnetic.z_uncalib = sensor_data.data.uncalibrated_magnetic.z_uncalib;
			pdata_cur->uncalibrated_magnetic.x_bias = sensor_data.data.uncalibrated_magnetic.x_bias;
			pdata_cur->uncalibrated_magnetic.y_bias = sensor_data.data.uncalibrated_magnetic.y_bias;
			pdata_cur->uncalibrated_magnetic.z_bias = sensor_data.data.uncalibrated_magnetic.z_bias;
			pdata_cur->type = SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED;
			break;
	#endif
#if __HAL_VER__ >= __SENSORS_DEVICE_API_VERSION_1_1__
		case SENSORS_GEOMAGNETIC_ROTATION_VECTOR_HANDLE:
			pdata_cur->data[0] = sensor_data.data.data[0];
			pdata_cur->data[1] = sensor_data.data.data[1];
			pdata_cur->data[2] = sensor_data.data.data[2];
			pdata_cur->data[3] = sensor_data.data.data[3];
			pdata_cur->data[4] = sensor_data.data.data[4];
			pdata_cur->type = SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR;
			break;
#endif
#if __HAL_VER__ >= __SENSORS_DEVICE_API_VERSION_1_0__
		case SENSORS_STEP_COUNTER_HANDLE:
		#if __HAL_VER__ >= __SENSORS_DEVICE_API_VERSION_1_1__
			pdata_cur->u64.step_counter = sensor_data.data.step_counter;
			pdata_cur->u64.data[0] = sensor_data.data.step_counter;
			pdata_cur->u64.data[1] = 0;
			pdata_cur->u64.data[2] = 0;
		#else
			pdata_cur->step_counter = sensor_data.data.step_counter;
			pdata_cur->data[0] = sensor_data.data.step_counter;
			pdata_cur->data[1] = 0;
			pdata_cur->data[2] = 0;
		#endif
			pdata_cur->type = SENSOR_TYPE_STEP_COUNTER;
			break;
		case SENSORS_STEP_DETECTOR_HANDLE:
			pdata_cur->data[0] = sensor_data.data.data[0];
			pdata_cur->data[1] = 0;
			pdata_cur->data[2] = 0;
			pdata_cur->type = SENSOR_TYPE_STEP_DETECTOR;
			break;
#endif
		case SENSORS_SW_SIGNIFICANT_MOTION_HANDLE:
			pdata_cur->data[0] = sensor_data.data.data[0];
			pdata_cur->data[1] = sensor_data.data.data[1];
			pdata_cur->data[2] = sensor_data.data.data[2];
			pdata_cur->type = BSTEXT_SENSOR_TYPE_SW_SGM;
			break;


		default:
			LOGE("<BST> " "Invalid data pkt");
			return rslt;
		}

#if __HAL_VER__ >= __SENSORS_DEVICE_API_VERSION_1_1__
FLUSH_FINISH_EVENT_HANDLED:
#endif
		rslt++;
		pdata_cur++;
	}

	return rslt;
}

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

	UNUSED_PARAM(len);
	for (i = 0; i < numOfHandles; i++)
	{
		for (j = 0; j < NUM_SENSORS; j++)
		{
			handlerID = id2handle(BstSensorInfo::g_bst_sensor_list[j].handle);
			if (handles[i] == handlerID)
			{
				memcpy(list++,
					&BstSensorInfo::g_bst_sensor_list[j],
					sizeof(struct sensor_t));
				num++;
			}
		}
	}

	LOGI("<BST> " "BST HAL version: %s",
			get_hal_version());
	LOGI("build date : %s", get_build_date());

	return num;
}

#if __HAL_VER__ >= __SENSORS_DEVICE_API_VERSION_1_1__
int BstSensor::flush(int id)
{
	int err = 0;
	int ret = 0;
	struct exchange cmd;
	const struct sensor_t *s;
	int handle;
	if (mCmdFd < 0) {
		PERR("<BST> " "cannot tx cmd: %s",
				(char *)strerror(-mCmdFd));
		return mCmdFd;
	}
	handle = BstSensor::id2handle(id);
	if (-1 == handle) {
		PERR("<BST> " "flush: cannot get handle for id %d", id);
		return 0;
	}
	cmd.magic = CHANNEL_PKT_MAGIC_CMD;
	cmd.command.cmd = SET_SENSOR_FLUSH;
	cmd.command.code = handle;
	s = BstSensorInfo::getSensor(id);
	PINFO("<BST> " "flush <%s>, id: %d",
			(s != NULL) ? s->name : "unknown", id);
	err = write(mCmdFd, &cmd, sizeof(cmd));
	ret = err < (int)sizeof(cmd) ? -1 : 0;
	return ret;
}

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

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