SensorDevice.cpp revision a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0
1f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian/* 2f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * Copyright (C) 2010 The Android Open Source Project 3f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * 4f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License"); 5f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * you may not use this file except in compliance with the License. 6f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * You may obtain a copy of the License at 7f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * 8f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * http://www.apache.org/licenses/LICENSE-2.0 9f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * 10f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * Unless required by applicable law or agreed to in writing, software 11f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS, 12f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * See the License for the specific language governing permissions and 14f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian * limitations under the License. 15f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian */ 16f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 17f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include <stdint.h> 18f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include <math.h> 19f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include <sys/types.h> 20f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 21f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include <utils/Atomic.h> 22f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include <utils/Errors.h> 23f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include <utils/Singleton.h> 24f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 25f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include <binder/BinderService.h> 26f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include <binder/Parcel.h> 27f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include <binder/IServiceManager.h> 28f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 29f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include <hardware/sensors.h> 30f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 31f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include "SensorDevice.h" 32a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian#include "SensorService.h" 33f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 34f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopiannamespace android { 35f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian// --------------------------------------------------------------------------- 36f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianclass BatteryService : public Singleton<BatteryService> { 37f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian static const int TRANSACTION_noteStartSensor = IBinder::FIRST_CALL_TRANSACTION + 3; 38f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian static const int TRANSACTION_noteStopSensor = IBinder::FIRST_CALL_TRANSACTION + 4; 39f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian static const String16 DESCRIPTOR; 40f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 41f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian friend class Singleton<BatteryService>; 42f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sp<IBinder> mBatteryStatService; 43f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 44f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian BatteryService() { 45f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const sp<IServiceManager> sm(defaultServiceManager()); 46f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (sm != NULL) { 47f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const String16 name("batteryinfo"); 48f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mBatteryStatService = sm->getService(name); 49f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 50f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 51f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 52f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian status_t noteStartSensor(int uid, int handle) { 53f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian Parcel data, reply; 54f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian data.writeInterfaceToken(DESCRIPTOR); 55f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian data.writeInt32(uid); 56f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian data.writeInt32(handle); 57f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian status_t err = mBatteryStatService->transact( 58f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian TRANSACTION_noteStartSensor, data, &reply, 0); 59f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian err = reply.readExceptionCode(); 60f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return err; 61f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 62f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 63f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian status_t noteStopSensor(int uid, int handle) { 64f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian Parcel data, reply; 65f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian data.writeInterfaceToken(DESCRIPTOR); 66f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian data.writeInt32(uid); 67f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian data.writeInt32(handle); 68f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian status_t err = mBatteryStatService->transact( 69f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian TRANSACTION_noteStopSensor, data, &reply, 0); 70f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian err = reply.readExceptionCode(); 71f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return err; 72f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 73f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 74f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianpublic: 75f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian void enableSensor(int handle) { 76f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mBatteryStatService != 0) { 77f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian int uid = IPCThreadState::self()->getCallingUid(); 78f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian int64_t identity = IPCThreadState::self()->clearCallingIdentity(); 79f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian noteStartSensor(uid, handle); 80f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian IPCThreadState::self()->restoreCallingIdentity(identity); 81f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 82f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 83f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian void disableSensor(int handle) { 84f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mBatteryStatService != 0) { 85f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian int uid = IPCThreadState::self()->getCallingUid(); 86f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian int64_t identity = IPCThreadState::self()->clearCallingIdentity(); 87f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian noteStopSensor(uid, handle); 88f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian IPCThreadState::self()->restoreCallingIdentity(identity); 89f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 90f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 91f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian}; 92f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 93f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianconst String16 BatteryService::DESCRIPTOR("com.android.internal.app.IBatteryStats"); 94f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 95f001c92436b4a66eb7687286325ced7f10c9f917Mathias AgopianANDROID_SINGLETON_STATIC_INSTANCE(BatteryService) 96f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 97f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian// --------------------------------------------------------------------------- 98f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 99f001c92436b4a66eb7687286325ced7f10c9f917Mathias AgopianANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice) 100f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 101f001c92436b4a66eb7687286325ced7f10c9f917Mathias AgopianSensorDevice::SensorDevice() 102f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian : mSensorDevice(0), 103f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mSensorModule(0) 104f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 105f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian status_t err = hw_get_module(SENSORS_HARDWARE_MODULE_ID, 106f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian (hw_module_t const**)&mSensorModule); 107f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 108f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian LOGE_IF(err, "couldn't load %s module (%s)", 109f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SENSORS_HARDWARE_MODULE_ID, strerror(-err)); 110f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 111f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mSensorModule) { 112f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian err = sensors_open(&mSensorModule->common, &mSensorDevice); 113f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 114f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian LOGE_IF(err, "couldn't open device for module %s (%s)", 115f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SENSORS_HARDWARE_MODULE_ID, strerror(-err)); 116f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 117f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mSensorDevice) { 118f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensor_t const* list; 119f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list); 120f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActivationCount.setCapacity(count); 121f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian Info model; 122f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t i=0 ; i<size_t(count) ; i++) { 123f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActivationCount.add(list[i].handle, model); 124f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mSensorDevice->activate(mSensorDevice, list[i].handle, 0); 125f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 126f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 127f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 128f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 129f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 130f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianvoid SensorDevice::dump(String8& result, char* buffer, size_t SIZE) 131f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 132f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (!mSensorModule) return; 133f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensor_t const* list; 134f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list); 135f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 136f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian snprintf(buffer, SIZE, "%d h/w sensors:\n", int(count)); 137f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian result.append(buffer); 138f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 139f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian Mutex::Autolock _l(mLock); 140f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t i=0 ; i<size_t(count) ; i++) { 14150b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian snprintf(buffer, SIZE, "handle=0x%08x, active-count=%d\n", 142f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian list[i].handle, 143f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActivationCount.valueFor(list[i].handle).rates.size()); 144f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian result.append(buffer); 145f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 146f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 147f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 148f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianssize_t SensorDevice::getSensorList(sensor_t const** list) { 149f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (!mSensorModule) return NO_INIT; 150f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian ssize_t count = mSensorModule->get_sensors_list(mSensorModule, list); 151f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return count; 152f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 153f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 154f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianstatus_t SensorDevice::initCheck() const { 155f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return mSensorDevice && mSensorModule ? NO_ERROR : NO_INIT; 156f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 157f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 158f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) { 159f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (!mSensorDevice) return NO_INIT; 160f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return mSensorDevice->poll(mSensorDevice, buffer, count); 161f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 162f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 163f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianstatus_t SensorDevice::activate(void* ident, int handle, int enabled) 164f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 165f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (!mSensorDevice) return NO_INIT; 166f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian status_t err(NO_ERROR); 167f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian bool actuateHardware = false; 168f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 169f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian Info& info( mActivationCount.editValueFor(handle) ); 170a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian 171a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian 172a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian LOGD_IF(DEBUG_CONNECTIONS, 173a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%d", 174a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian ident, handle, enabled, info.rates.size()); 175a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian 176f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (enabled) { 177f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian Mutex::Autolock _l(mLock); 178a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian LOGD_IF(DEBUG_CONNECTIONS, "... index=%ld", 179a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian info.rates.indexOfKey(ident)); 180a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian 181f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (info.rates.indexOfKey(ident) < 0) { 182f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian info.rates.add(ident, DEFAULT_EVENTS_PERIOD); 183a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian if (info.rates.size() == 1) { 184a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian actuateHardware = true; 185a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian } 18650b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian } else { 18750b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian // sensor was already activated for this ident 188f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 18950b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian } else { 190f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian Mutex::Autolock _l(mLock); 191a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian LOGD_IF(DEBUG_CONNECTIONS, "... index=%ld", 192a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian info.rates.indexOfKey(ident)); 193a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian 194a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian ssize_t idx = info.rates.removeItem(ident); 195a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian if (idx >= 0) { 19650b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian if (info.rates.size() == 0) { 19750b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian actuateHardware = true; 19850b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian } 19950b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian } else { 20050b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian // sensor wasn't enabled for this ident 20150b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian } 202f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 20350b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian 204f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (actuateHardware) { 205a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian LOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w"); 206a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian 207f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian err = mSensorDevice->activate(mSensorDevice, handle, enabled); 208f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (enabled) { 209f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian LOGE_IF(err, "Error activating sensor %d (%s)", handle, strerror(-err)); 210f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (err == 0) { 211f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian BatteryService::getInstance().enableSensor(handle); 212f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 213f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } else { 214f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (err == 0) { 215f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian BatteryService::getInstance().disableSensor(handle); 216f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 217f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 218f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 219f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 220f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (!actuateHardware || enabled) { 221f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian Mutex::Autolock _l(mLock); 222f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian nsecs_t ns = info.rates.valueAt(0); 223f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t i=1 ; i<info.rates.size() ; i++) { 224f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (info.rates.valueAt(i) < ns) { 225f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian nsecs_t cur = info.rates.valueAt(i); 226f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (cur < ns) { 227f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian ns = cur; 228f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 229f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 230f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 231f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mSensorDevice->setDelay(mSensorDevice, handle, ns); 232f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 233f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 234f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return err; 235f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 236f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 237f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianstatus_t SensorDevice::setDelay(void* ident, int handle, int64_t ns) 238f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 239f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (!mSensorDevice) return NO_INIT; 240f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian Info& info( mActivationCount.editValueFor(handle) ); 241f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian { // scope for lock 242f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian Mutex::Autolock _l(mLock); 243f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian ssize_t index = info.rates.indexOfKey(ident); 244f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (index < 0) return BAD_INDEX; 245f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian info.rates.editValueAt(index) = ns; 246f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian ns = info.rates.valueAt(0); 247f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t i=1 ; i<info.rates.size() ; i++) { 248f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian nsecs_t cur = info.rates.valueAt(i); 249f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (cur < ns) { 250f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian ns = cur; 251f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 252f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 253f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 254f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return mSensorDevice->setDelay(mSensorDevice, handle, ns); 255f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 256f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 257f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian// --------------------------------------------------------------------------- 258f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian}; // namespace android 259f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 260