OSDN Git Service

libsensors: retry more and deal with sensor hub quirk
[android-x86/hardware-intel-libsensors.git] / common / libsensors / HidSensor_Accel3D.cpp
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
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 #include <fcntl.h>
17 #include <errno.h>
18 #include <math.h>
19 #include <poll.h>
20 #include <unistd.h>
21 #include <dirent.h>
22 #include <sys/select.h>
23 #include <cutils/log.h>
24
25 #include "common.h"
26 #include "SensorConfig.h"
27 #include "HidSensor_Accel3D.h"
28
29 #define CHANNEL_X 0
30 #define CHANNEL_Y 1
31 #define CHANNEL_Z 2
32
33 struct accel_3d_sample{
34     unsigned int accel_x;
35     unsigned int accel_y;
36     unsigned int accel_z;
37 } __packed;
38
39 const struct sensor_t AccelSensor::sSensorInfo_accel3D = {
40     "HID_SENSOR Accelerometer 3D", "Intel", 1, SENSORS_ACCELERATION_HANDLE,
41         SENSOR_TYPE_ACCELEROMETER, RANGE_A, RESOLUTION_A, 0.23f, 20000, {}
42 };
43
44 const long HID_USAGE_SENSOR_UNITS_G = 0x1A;
45 const long HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD = (0x11, 0xE0);
46 const int retry_cnt = 10;
47
48 AccelSensor::AccelSensor(): SensorIIODev("accel_3d", "in_accel_scale", "in_accel_offset", "in_accel_", retry_cnt){
49     ALOGV(">>AccelSensor 3D: constructor!");
50     mPendingEvent.version = sizeof(sensors_event_t);
51     mPendingEvent.sensor = ID_A;
52     mPendingEvent.type = SENSOR_TYPE_ACCELEROMETER;
53     memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));
54     ALOGV("<<AccelSensor 3D: constructor!");
55 }
56
57 int AccelSensor::processEvent(unsigned char *raw_data, size_t raw_data_len){
58     struct accel_3d_sample *sample;
59
60     ALOGV(">>%s", __func__);
61
62     if (IsDeviceInitialized() == false){
63         ALOGE("Device was not initialized \n");
64         return  - 1;
65     } if (raw_data_len < sizeof(struct accel_3d_sample)){
66         ALOGE("Insufficient length \n");
67         return  - 1;
68     }
69
70     ALOGV("Accel:%2x:%2x:%2x:%2x:%2x:%2x",  *raw_data, *(raw_data + 1), *
71         (raw_data + 2), *(raw_data + 3), *(raw_data + 4), *(raw_data + 5));
72     sample = (struct accel_3d_sample*)raw_data;
73     mPendingEvent.data[0] = mPendingEvent.acceleration.x =
74         CONVERT_A_G_VTF16E14_X(GetChannelBytesUsedSize(CHANNEL_X), GetExponentValue(), sample->accel_x);
75     mPendingEvent.data[1] = mPendingEvent.acceleration.y =
76         CONVERT_A_G_VTF16E14_Y(GetChannelBytesUsedSize(CHANNEL_Y), GetExponentValue(), sample->accel_y);
77     mPendingEvent.data[2] = mPendingEvent.acceleration.z =
78         CONVERT_A_G_VTF16E14_Z(GetChannelBytesUsedSize(CHANNEL_Z), GetExponentValue(), sample->accel_z);
79
80     ALOGV("ACCEL 3D Sample %fm/s2 %fm/s2 %fm/s2\n", mPendingEvent.acceleration.x,
81         mPendingEvent.acceleration.y, mPendingEvent.acceleration.z);
82     ALOGV("<<%s", __func__);
83     return 0;
84 }