OSDN Git Service

Merge remote-tracking branch 'x86/kitkat-x86' into lollipop-x86
[android-x86/hardware-intel-libsensors.git] / HidSensor_Proximity.cpp
1 /*
2  * Copyright (C) 2014 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_Proximity.h"
28
29
30 struct prox_sample{
31     unsigned short presence;
32 } __packed;
33
34 const struct sensor_t ProximitySensor::sSensorInfo_proximity = {
35     "HID_SENSOR Proximity", "Intel", 1, SENSORS_PROXIMITY_HANDLE, SENSOR_TYPE_PROXIMITY,
36         5.0f, 1.0f, 0.75f, 0, 0, 0, {}
37     ,
38 };
39 const int retry_cnt = 10;
40
41 ProximitySensor::ProximitySensor(): SensorIIODev("prox", "in_proximity_scale", "in_proximity_offset", "in_proximity_", retry_cnt){
42     ALOGV(">>ProximitySensor 3D: constructor!");
43     mPendingEvent.version = sizeof(sensors_event_t);
44     mPendingEvent.sensor = ID_P;
45     mPendingEvent.type = SENSOR_TYPE_PROXIMITY;
46     memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));
47
48     // CDD is silent on Proximity requirements.  Fix default at 2Hz pending
49     // better numbers from somewhere.
50      sample_delay_min_ms = 500;
51
52     ALOGV("<<ProximitySensor 3D: constructor!");
53 }
54
55 int ProximitySensor::processEvent(unsigned char *raw_data, size_t raw_data_len){
56     struct prox_sample *sample;
57
58     ALOGV(">>%s", __func__);
59     if (IsDeviceInitialized() == false){
60         ALOGE("Device was not initialized \n");
61         return  - 1;
62     } if (raw_data_len < sizeof(struct prox_sample)){
63         ALOGE("Insufficient length \n");
64         return  - 1;
65     }
66     sample = (struct prox_sample*)raw_data;
67     if (sample->presence == 1)
68             mPendingEvent.distance = (float)0;
69     else
70             mPendingEvent.distance = (float)5;
71
72
73     ALOGE("Proximity %fm\n", mPendingEvent.distance);
74     ALOGV("<<%s", __func__);
75     return 0;
76 }