OSDN Git Service

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