• 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 
33 namespace android {
34 // ---------------------------------------------------------------------------
35 class BatteryService : public Singleton<BatteryService> {
36     static const int TRANSACTION_noteStartSensor = IBinder::FIRST_CALL_TRANSACTION + 3;
37     static const int TRANSACTION_noteStopSensor = IBinder::FIRST_CALL_TRANSACTION + 4;
38     static const String16 DESCRIPTOR;
39 
40     friend class Singleton<BatteryService>;
41     sp<IBinder> mBatteryStatService;
42 
BatteryService()43     BatteryService() {
44         const sp<IServiceManager> sm(defaultServiceManager());
45         if (sm != NULL) {
46             const String16 name("batteryinfo");
47             mBatteryStatService = sm->getService(name);
48         }
49     }
50 
noteStartSensor(int uid,int handle)51     status_t noteStartSensor(int uid, int handle) {
52         Parcel data, reply;
53         data.writeInterfaceToken(DESCRIPTOR);
54         data.writeInt32(uid);
55         data.writeInt32(handle);
56         status_t err = mBatteryStatService->transact(
57                 TRANSACTION_noteStartSensor, data, &reply, 0);
58         err = reply.readExceptionCode();
59         return err;
60     }
61 
noteStopSensor(int uid,int handle)62     status_t noteStopSensor(int uid, int handle) {
63         Parcel data, reply;
64         data.writeInterfaceToken(DESCRIPTOR);
65         data.writeInt32(uid);
66         data.writeInt32(handle);
67         status_t err = mBatteryStatService->transact(
68                 TRANSACTION_noteStopSensor, data, &reply, 0);
69         err = reply.readExceptionCode();
70         return err;
71     }
72 
73 public:
enableSensor(int handle)74     void enableSensor(int handle) {
75         if (mBatteryStatService != 0) {
76             int uid = IPCThreadState::self()->getCallingUid();
77             int64_t identity = IPCThreadState::self()->clearCallingIdentity();
78             noteStartSensor(uid, handle);
79             IPCThreadState::self()->restoreCallingIdentity(identity);
80         }
81     }
disableSensor(int handle)82     void disableSensor(int handle) {
83         if (mBatteryStatService != 0) {
84             int uid = IPCThreadState::self()->getCallingUid();
85             int64_t identity = IPCThreadState::self()->clearCallingIdentity();
86             noteStopSensor(uid, handle);
87             IPCThreadState::self()->restoreCallingIdentity(identity);
88         }
89     }
90 };
91 
92 const String16 BatteryService::DESCRIPTOR("com.android.internal.app.IBatteryStats");
93 
94 ANDROID_SINGLETON_STATIC_INSTANCE(BatteryService)
95 
96 // ---------------------------------------------------------------------------
97 
ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice)98 ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice)
99 
100 SensorDevice::SensorDevice()
101     :  mSensorDevice(0),
102        mSensorModule(0)
103 {
104     status_t err = hw_get_module(SENSORS_HARDWARE_MODULE_ID,
105             (hw_module_t const**)&mSensorModule);
106 
107     LOGE_IF(err, "couldn't load %s module (%s)",
108             SENSORS_HARDWARE_MODULE_ID, strerror(-err));
109 
110     if (mSensorModule) {
111         err = sensors_open(&mSensorModule->common, &mSensorDevice);
112 
113         LOGE_IF(err, "couldn't open device for module %s (%s)",
114                 SENSORS_HARDWARE_MODULE_ID, strerror(-err));
115 
116         if (mSensorDevice) {
117             sensor_t const* list;
118             ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
119             mActivationCount.setCapacity(count);
120             Info model;
121             for (size_t i=0 ; i<size_t(count) ; i++) {
122                 mActivationCount.add(list[i].handle, model);
123                 mSensorDevice->activate(mSensorDevice, list[i].handle, 0);
124             }
125         }
126     }
127 }
128 
dump(String8 & result,char * buffer,size_t SIZE)129 void SensorDevice::dump(String8& result, char* buffer, size_t SIZE)
130 {
131     if (!mSensorModule) return;
132     sensor_t const* list;
133     ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
134 
135     snprintf(buffer, SIZE, "%d h/w sensors:\n", int(count));
136     result.append(buffer);
137 
138     Mutex::Autolock _l(mLock);
139     for (size_t i=0 ; i<size_t(count) ; i++) {
140         snprintf(buffer, SIZE, "handle=0x%08x, active-count=%d\n",
141                 list[i].handle,
142                 mActivationCount.valueFor(list[i].handle).rates.size());
143         result.append(buffer);
144     }
145 }
146 
getSensorList(sensor_t const ** list)147 ssize_t SensorDevice::getSensorList(sensor_t const** list) {
148     if (!mSensorModule) return NO_INIT;
149     ssize_t count = mSensorModule->get_sensors_list(mSensorModule, list);
150     return count;
151 }
152 
initCheck() const153 status_t SensorDevice::initCheck() const {
154     return mSensorDevice && mSensorModule ? NO_ERROR : NO_INIT;
155 }
156 
poll(sensors_event_t * buffer,size_t count)157 ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) {
158     if (!mSensorDevice) return NO_INIT;
159     return mSensorDevice->poll(mSensorDevice, buffer, count);
160 }
161 
activate(void * ident,int handle,int enabled)162 status_t SensorDevice::activate(void* ident, int handle, int enabled)
163 {
164     if (!mSensorDevice) return NO_INIT;
165     status_t err(NO_ERROR);
166     bool actuateHardware = false;
167 
168     Info& info( mActivationCount.editValueFor(handle) );
169     if (enabled) {
170         Mutex::Autolock _l(mLock);
171         if (info.rates.indexOfKey(ident) < 0) {
172             info.rates.add(ident, DEFAULT_EVENTS_PERIOD);
173             actuateHardware = true;
174         } else {
175             // sensor was already activated for this ident
176         }
177     } else {
178         Mutex::Autolock _l(mLock);
179         if (info.rates.removeItem(ident) >= 0) {
180             if (info.rates.size() == 0) {
181                 actuateHardware = true;
182             }
183         } else {
184             // sensor wasn't enabled for this ident
185         }
186     }
187 
188     if (actuateHardware) {
189         err = mSensorDevice->activate(mSensorDevice, handle, enabled);
190         if (enabled) {
191             LOGE_IF(err, "Error activating sensor %d (%s)", handle, strerror(-err));
192             if (err == 0) {
193                 BatteryService::getInstance().enableSensor(handle);
194             }
195         } else {
196             if (err == 0) {
197                 BatteryService::getInstance().disableSensor(handle);
198             }
199         }
200     }
201 
202     if (!actuateHardware || enabled) {
203         Mutex::Autolock _l(mLock);
204         nsecs_t ns = info.rates.valueAt(0);
205         for (size_t i=1 ; i<info.rates.size() ; i++) {
206             if (info.rates.valueAt(i) < ns) {
207                 nsecs_t cur = info.rates.valueAt(i);
208                 if (cur < ns) {
209                     ns = cur;
210                 }
211             }
212         }
213         mSensorDevice->setDelay(mSensorDevice, handle, ns);
214     }
215 
216     return err;
217 }
218 
setDelay(void * ident,int handle,int64_t ns)219 status_t SensorDevice::setDelay(void* ident, int handle, int64_t ns)
220 {
221     if (!mSensorDevice) return NO_INIT;
222     Info& info( mActivationCount.editValueFor(handle) );
223     { // scope for lock
224         Mutex::Autolock _l(mLock);
225         ssize_t index = info.rates.indexOfKey(ident);
226         if (index < 0) return BAD_INDEX;
227         info.rates.editValueAt(index) = ns;
228         ns = info.rates.valueAt(0);
229         for (size_t i=1 ; i<info.rates.size() ; i++) {
230             nsecs_t cur = info.rates.valueAt(i);
231             if (cur < ns) {
232                 ns = cur;
233             }
234         }
235     }
236     return mSensorDevice->setDelay(mSensorDevice, handle, ns);
237 }
238 
239 // ---------------------------------------------------------------------------
240 }; // namespace android
241 
242