OSDN Git Service

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