OSDN Git Service

libsensors: fix building issues on Android 5.0
[android-x86/hardware-intel-libsensors.git] / 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, 10000, 0, 0,
42     SENSOR_STRING_TYPE_ACCELEROMETER, "", 0 , SENSOR_FLAG_CONTINUOUS_MODE, {}
43 };
44
45 const long HID_USAGE_SENSOR_UNITS_G = 0x1A;
46 const long HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD = (0x11, 0xE0);
47 const int retry_cnt = 10;
48
49 AccelSensor::AccelSensor(): SensorIIODev("accel_3d", "in_accel_scale", "in_accel_offset", "in_accel_", retry_cnt){
50     ALOGV(">>AccelSensor 3D: constructor!");
51     mPendingEvent.version = sizeof(sensors_event_t);
52     mPendingEvent.sensor = ID_A;
53     mPendingEvent.type = SENSOR_TYPE_ACCELEROMETER;
54     memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));
55
56     // CDD requires 120Hz, but HSB caps out at ~62Hz.  Use that as default.
57     sample_delay_min_ms = 16;
58
59     ALOGV("<<AccelSensor 3D: constructor!");
60 }
61
62 int AccelSensor::processEvent(unsigned char *raw_data, size_t raw_data_len){
63     struct accel_3d_sample *sample;
64
65     ALOGV(">>%s", __func__);
66
67     if (IsDeviceInitialized() == false){
68         ALOGE("Device was not initialized \n");
69         return  - 1;
70     } if (raw_data_len < sizeof(struct accel_3d_sample)){
71         ALOGE("Insufficient length \n");
72         return  - 1;
73     }
74
75     ALOGV("Accel:%2x:%2x:%2x:%2x:%2x:%2x",  *raw_data, *(raw_data + 1), *
76         (raw_data + 2), *(raw_data + 3), *(raw_data + 4), *(raw_data + 5));
77     sample = (struct accel_3d_sample*)raw_data;
78     mPendingEvent.data[0] = mPendingEvent.acceleration.x =
79         CONVERT_A_G_VTF16E14_X(GetChannelBytesUsedSize(CHANNEL_X), GetExponentValue(), sample->accel_x);
80     mPendingEvent.data[1] = mPendingEvent.acceleration.y =
81         CONVERT_A_G_VTF16E14_Y(GetChannelBytesUsedSize(CHANNEL_Y), GetExponentValue(), sample->accel_y);
82     mPendingEvent.data[2] = mPendingEvent.acceleration.z =
83         CONVERT_A_G_VTF16E14_Z(GetChannelBytesUsedSize(CHANNEL_Z), GetExponentValue(), sample->accel_z);
84
85     ALOGV("ACCEL 3D Sample %fm/s2 %fm/s2 %fm/s2\n", mPendingEvent.acceleration.x,
86         mPendingEvent.acceleration.y, mPendingEvent.acceleration.z);
87     ALOGV("<<%s", __func__);
88     return 0;
89 }