SensorDevice.cpp revision 667102f6b072582fe497599e0b760f9fc94ceffa
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++) {
141667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian        const Info& info = mActivationCount.valueFor(list[i].handle);
142667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian        snprintf(buffer, SIZE, "handle=0x%08x, active-count=%d, rates(ms)={ ",
143f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                list[i].handle,
144667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian                info.rates.size());
145667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian        result.append(buffer);
146667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian        for (size_t j=0 ; j<info.rates.size() ; j++) {
147667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian            snprintf(buffer, SIZE, "%4.1f%s",
148667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian                    info.rates.valueAt(j) / 1e6f,
149667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian                    j<info.rates.size()-1 ? ", " : "");
150667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian            result.append(buffer);
151667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian        }
152667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian        snprintf(buffer, SIZE, " }, selected=%4.1f ms\n",  info.delay / 1e6f);
153f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        result.append(buffer);
154f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    }
155f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian}
156f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
157f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianssize_t SensorDevice::getSensorList(sensor_t const** list) {
158f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    if (!mSensorModule) return NO_INIT;
159f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    ssize_t count = mSensorModule->get_sensors_list(mSensorModule, list);
160f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    return count;
161f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian}
162f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
163f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianstatus_t SensorDevice::initCheck() const {
164f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    return mSensorDevice && mSensorModule ? NO_ERROR : NO_INIT;
165f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian}
166f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
167f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) {
168f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    if (!mSensorDevice) return NO_INIT;
169f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    return mSensorDevice->poll(mSensorDevice, buffer, count);
170f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian}
171f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
172f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianstatus_t SensorDevice::activate(void* ident, int handle, int enabled)
173f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{
174f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    if (!mSensorDevice) return NO_INIT;
175f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    status_t err(NO_ERROR);
176f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    bool actuateHardware = false;
177f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
178f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    Info& info( mActivationCount.editValueFor(handle) );
179a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian
180a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian
181a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian    LOGD_IF(DEBUG_CONNECTIONS,
182a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian            "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%d",
183a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian            ident, handle, enabled, info.rates.size());
184a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian
185f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    if (enabled) {
186f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        Mutex::Autolock _l(mLock);
187a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian        LOGD_IF(DEBUG_CONNECTIONS, "... index=%ld",
188a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian                info.rates.indexOfKey(ident));
189a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian
190f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        if (info.rates.indexOfKey(ident) < 0) {
191f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            info.rates.add(ident, DEFAULT_EVENTS_PERIOD);
192a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian            if (info.rates.size() == 1) {
193a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian                actuateHardware = true;
194a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian            }
19550b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian        } else {
19650b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian            // sensor was already activated for this ident
197f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        }
19850b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian    } else {
199f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        Mutex::Autolock _l(mLock);
200a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian        LOGD_IF(DEBUG_CONNECTIONS, "... index=%ld",
201a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian                info.rates.indexOfKey(ident));
202a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian
203a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian        ssize_t idx = info.rates.removeItem(ident);
204a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian        if (idx >= 0) {
20550b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian            if (info.rates.size() == 0) {
20650b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian                actuateHardware = true;
20750b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian            }
20850b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian        } else {
20950b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian            // sensor wasn't enabled for this ident
21050b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian        }
211f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    }
21250b66767f6c5635430483393e17d15969dfe2f05Mathias Agopian
213f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    if (actuateHardware) {
214a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian        LOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w");
215a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian
216f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        err = mSensorDevice->activate(mSensorDevice, handle, enabled);
217f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        if (enabled) {
218f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            LOGE_IF(err, "Error activating sensor %d (%s)", handle, strerror(-err));
219f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            if (err == 0) {
220f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                BatteryService::getInstance().enableSensor(handle);
221f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            }
222f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        } else {
223f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            if (err == 0) {
224f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                BatteryService::getInstance().disableSensor(handle);
225f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            }
226f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        }
227f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    }
228f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
229667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian    { // scope for the lock
230f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        Mutex::Autolock _l(mLock);
231667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian        nsecs_t ns = info.selectDelay();
232f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        mSensorDevice->setDelay(mSensorDevice, handle, ns);
233f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    }
234f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
235f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    return err;
236f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian}
237f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
238f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianstatus_t SensorDevice::setDelay(void* ident, int handle, int64_t ns)
239f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{
240f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    if (!mSensorDevice) return NO_INIT;
241667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian    Mutex::Autolock _l(mLock);
242f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    Info& info( mActivationCount.editValueFor(handle) );
243667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian    status_t err = info.setDelayForIdent(ident, ns);
244667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian    if (err < 0) return err;
245667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian    ns = info.selectDelay();
246667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian    return mSensorDevice->setDelay(mSensorDevice, handle, ns);
247667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian}
248984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
249667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian// ---------------------------------------------------------------------------
250984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian
251667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopianstatus_t SensorDevice::Info::setDelayForIdent(void* ident, int64_t ns)
252667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian{
253667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian    ssize_t index = rates.indexOfKey(ident);
254667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian    if (index < 0) {
255667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian        LOGE("Info::setDelayForIdent(ident=%p, ns=%lld) failed (%s)",
256667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian                ident, ns, strerror(-index));
257667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian        return BAD_INDEX;
258667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian    }
259667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian    rates.editValueAt(index) = ns;
260667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian    return NO_ERROR;
261667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian}
262667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian
263667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopiannsecs_t SensorDevice::Info::selectDelay()
264667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian{
265667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian    nsecs_t ns = rates.valueAt(0);
266667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian    for (size_t i=1 ; i<rates.size() ; i++) {
267667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian        nsecs_t cur = rates.valueAt(i);
268667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian        if (cur < ns) {
269667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian            ns = cur;
270667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian        }
271667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian    }
272667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian    delay = ns;
273667102f6b072582fe497599e0b760f9fc94ceffaMathias Agopian    return ns;
274f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian}
275f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
276f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian// ---------------------------------------------------------------------------
277f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian}; // namespace android
278f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
279