OSDN Git Service

libsensors: workaround sensors never detected
[android-x86/hardware-intel-libsensors.git] / bdw_wsb / BoardConfig.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
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <cutils/log.h>
20 #include <unistd.h>
21 #include <cutils/properties.h>
22
23 #include "common.h"
24
25 #include "BoardConfig.h"
26 #include "SensorConfig.h"
27 #include "HidSensor_Accel3D.h"
28 #include "HidSensor_Gyro3D.h"
29 #include "HidSensor_Compass3D.h"
30 #include "HidSensor_ALS.h"
31 #include "HidSensor_Proximity.h"
32 #include "HidSensor_Pressure.h"
33 #include "OrientationSensor.h"
34 #include "RotVecSensor.h"
35 #include "SynthCompassSensor.h"
36
37 static const struct sensor_t sSensorList[] = {
38     AccelSensor::sSensorInfo_accel3D,
39     GyroSensor::sSensorInfo_gyro3D,
40     SynthCompassSensor::sSensorInfo_compass,
41     ALSSensor::sSensorInfo_als,
42     ProximitySensor::sSensorInfo_proximity,
43     PressureSensor::sSensorInfo_pressure,
44     RotVecSensor::sSensorInfo_rotvec,
45     OrientationSensor::sSensorInfo_orientation,
46 };
47
48 const struct sensor_t* BoardConfig::sensorList()
49 {
50     return sSensorList;
51 }
52
53 int BoardConfig::sensorListSize()
54 {
55     int num_files(0);
56     DIR *dp(0);
57     struct dirent *dirp(0);
58     const char *key = "persist.sys.sensors.iio.present";
59     char value[PROPERTY_VALUE_MAX];
60
61     if (property_get(key, value, "")) {
62         if (strncmp(value, "1", 1) == 0) {
63             ALOGI("IIO sensor hub detected previously; assuming it is still attached.");
64             return ARRAY_SIZE(sSensorList);
65         } else {
66             ALOGI("IIO sensor hub not detected previously; assuming it still is not attached.");
67             return 0;
68         }
69     }
70
71     for (int i=0; i<250; i++) {
72         num_files = 0;
73         if ((dp = opendir("/sys/bus/iio/devices")) == NULL){
74             usleep(20000);
75             continue;
76         }
77         while ((dirp = readdir(dp)) != NULL){
78             num_files++;
79         }
80         closedir(dp);
81
82         if (num_files <= 2) {
83             usleep(20000);
84             continue;
85         }
86         ALOGI("Found IIO sensor hub.");
87         if (property_set(key, "1") != 0) {
88             ALOGE("Failed to set %s", key);
89         }
90         return ARRAY_SIZE(sSensorList);
91     }
92
93     ALOGI("Didn't find IIO sensor hub.");
94     if (property_set(key, "0") != 0) {
95         ALOGE("Failed to set %s", key);
96     }
97     return 0;
98 }
99
100 void BoardConfig::initSensors(SensorBase* sensors[])
101 {
102     sensors[accel] = new AccelSensor();
103     sensors[gyro] = new GyroSensor();
104     sensors[compass] = new CompassSensor();
105     sensors[light] = new ALSSensor();
106     sensors[proximity] = new ProximitySensor();
107     sensors[pressure] = new PressureSensor();
108     sensors[rotvec] = new RotVecSensor();
109     sensors[syncompass] = new SynthCompassSensor();
110     sensors[orientation] = new OrientationSensor();
111 }
112
113 int BoardConfig::handleToDriver(int handle)
114 {
115     switch (handle) {
116     case ID_A:
117         return accel;
118     case ID_M:
119         return compass;
120     case ID_PR:
121         return pressure;
122     case ID_T:
123         return -EINVAL;
124     case ID_GY:
125         return gyro;
126     case ID_L:
127         return light;
128     case ID_R:
129         return rotvec;
130     case ID_SC:
131         return syncompass;
132     case ID_O:
133         return orientation;
134     case ID_P:
135         return proximity;
136   default:
137         return -EINVAL;
138     }
139     return -EINVAL;
140 }