• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 <stdint.h>
18 #include <math.h>
19 #include <sys/types.h>
20 
21 #include <utils/Atomic.h>
22 #include <utils/Errors.h>
23 #include <utils/Singleton.h>
24 
25 #include <binder/BinderService.h>
26 #include <binder/Parcel.h>
27 #include <binder/IServiceManager.h>
28 
29 #include <hardware/sensors.h>
30 
31 #include "SensorDevice.h"
32 #include "SensorService.h"
33 
34 namespace android {
35 // ---------------------------------------------------------------------------
36 
ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice)37 ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice)
38 
39 SensorDevice::SensorDevice()
40     :  mSensorDevice(0),
41        mSensorModule(0)
42 {
43     status_t err = hw_get_module(SENSORS_HARDWARE_MODULE_ID,
44             (hw_module_t const**)&mSensorModule);
45 
46     ALOGE_IF(err, "couldn't load %s module (%s)",
47             SENSORS_HARDWARE_MODULE_ID, strerror(-err));
48 
49     if (mSensorModule) {
50         err = sensors_open(&mSensorModule->common, &mSensorDevice);
51 
52         ALOGE_IF(err, "couldn't open device for module %s (%s)",
53                 SENSORS_HARDWARE_MODULE_ID, strerror(-err));
54 
55         if (mSensorDevice) {
56             sensor_t const* list;
57             ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
58             mActivationCount.setCapacity(count);
59             Info model;
60             for (size_t i=0 ; i<size_t(count) ; i++) {
61                 mActivationCount.add(list[i].handle, model);
62                 mSensorDevice->activate(mSensorDevice, list[i].handle, 0);
63             }
64         }
65     }
66 }
67 
dump(String8 & result,char * buffer,size_t SIZE)68 void SensorDevice::dump(String8& result, char* buffer, size_t SIZE)
69 {
70     if (!mSensorModule) return;
71     sensor_t const* list;
72     ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
73 
74     snprintf(buffer, SIZE, "%d h/w sensors:\n", int(count));
75     result.append(buffer);
76 
77     Mutex::Autolock _l(mLock);
78     for (size_t i=0 ; i<size_t(count) ; i++) {
79         const Info& info = mActivationCount.valueFor(list[i].handle);
80         snprintf(buffer, SIZE, "handle=0x%08x, active-count=%d, rates(ms)={ ",
81                 list[i].handle,
82                 info.rates.size());
83         result.append(buffer);
84         for (size_t j=0 ; j<info.rates.size() ; j++) {
85             snprintf(buffer, SIZE, "%4.1f%s",
86                     info.rates.valueAt(j) / 1e6f,
87                     j<info.rates.size()-1 ? ", " : "");
88             result.append(buffer);
89         }
90         snprintf(buffer, SIZE, " }, selected=%4.1f ms\n",  info.delay / 1e6f);
91         result.append(buffer);
92     }
93 }
94 
getSensorList(sensor_t const ** list)95 ssize_t SensorDevice::getSensorList(sensor_t const** list) {
96     if (!mSensorModule) return NO_INIT;
97     ssize_t count = mSensorModule->get_sensors_list(mSensorModule, list);
98     return count;
99 }
100 
initCheck() const101 status_t SensorDevice::initCheck() const {
102     return mSensorDevice && mSensorModule ? NO_ERROR : NO_INIT;
103 }
104 
poll(sensors_event_t * buffer,size_t count)105 ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) {
106     if (!mSensorDevice) return NO_INIT;
107     ssize_t c;
108     do {
109         c = mSensorDevice->poll(mSensorDevice, buffer, count);
110     } while (c == -EINTR);
111     return c;
112 }
113 
activate(void * ident,int handle,int enabled)114 status_t SensorDevice::activate(void* ident, int handle, int enabled)
115 {
116     if (!mSensorDevice) return NO_INIT;
117     status_t err(NO_ERROR);
118     bool actuateHardware = false;
119 
120     Info& info( mActivationCount.editValueFor(handle) );
121 
122 
123     ALOGD_IF(DEBUG_CONNECTIONS,
124             "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%d",
125             ident, handle, enabled, info.rates.size());
126 
127     if (enabled) {
128         Mutex::Autolock _l(mLock);
129         ALOGD_IF(DEBUG_CONNECTIONS, "... index=%ld",
130                 info.rates.indexOfKey(ident));
131 
132         if (info.rates.indexOfKey(ident) < 0) {
133             info.rates.add(ident, DEFAULT_EVENTS_PERIOD);
134             if (info.rates.size() == 1) {
135                 actuateHardware = true;
136             }
137         } else {
138             // sensor was already activated for this ident
139         }
140     } else {
141         Mutex::Autolock _l(mLock);
142         ALOGD_IF(DEBUG_CONNECTIONS, "... index=%ld",
143                 info.rates.indexOfKey(ident));
144 
145         ssize_t idx = info.rates.removeItem(ident);
146         if (idx >= 0) {
147             if (info.rates.size() == 0) {
148                 actuateHardware = true;
149             }
150         } else {
151             // sensor wasn't enabled for this ident
152         }
153     }
154 
155     if (actuateHardware) {
156         ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w");
157 
158         err = mSensorDevice->activate(mSensorDevice, handle, enabled);
159         ALOGE_IF(err, "Error %s sensor %d (%s)",
160                 enabled ? "activating" : "disabling",
161                 handle, strerror(-err));
162     }
163 
164     { // scope for the lock
165         Mutex::Autolock _l(mLock);
166         nsecs_t ns = info.selectDelay();
167         mSensorDevice->setDelay(mSensorDevice, handle, ns);
168     }
169 
170     return err;
171 }
172 
setDelay(void * ident,int handle,int64_t ns)173 status_t SensorDevice::setDelay(void* ident, int handle, int64_t ns)
174 {
175     if (!mSensorDevice) return NO_INIT;
176     Mutex::Autolock _l(mLock);
177     Info& info( mActivationCount.editValueFor(handle) );
178     status_t err = info.setDelayForIdent(ident, ns);
179     if (err < 0) return err;
180     ns = info.selectDelay();
181     return mSensorDevice->setDelay(mSensorDevice, handle, ns);
182 }
183 
184 // ---------------------------------------------------------------------------
185 
setDelayForIdent(void * ident,int64_t ns)186 status_t SensorDevice::Info::setDelayForIdent(void* ident, int64_t ns)
187 {
188     ssize_t index = rates.indexOfKey(ident);
189     if (index < 0) {
190         ALOGE("Info::setDelayForIdent(ident=%p, ns=%lld) failed (%s)",
191                 ident, ns, strerror(-index));
192         return BAD_INDEX;
193     }
194     rates.editValueAt(index) = ns;
195     return NO_ERROR;
196 }
197 
selectDelay()198 nsecs_t SensorDevice::Info::selectDelay()
199 {
200     nsecs_t ns = rates.valueAt(0);
201     for (size_t i=1 ; i<rates.size() ; i++) {
202         nsecs_t cur = rates.valueAt(i);
203         if (cur < ns) {
204             ns = cur;
205         }
206     }
207     delay = ns;
208     return ns;
209 }
210 
211 // ---------------------------------------------------------------------------
212 }; // namespace android
213 
214