OSDN Git Service

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