SensorService.cpp revision f001c92436b4a66eb7687286325ced7f10c9f917
1fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian/*
2fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * Copyright (C) 2010 The Android Open Source Project
3fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian *
4fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * you may not use this file except in compliance with the License.
6fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * You may obtain a copy of the License at
7fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian *
8fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian *
10fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * Unless required by applicable law or agreed to in writing, software
11fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * See the License for the specific language governing permissions and
14fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * limitations under the License.
15fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian */
16fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
17fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <stdint.h>
18f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include <math.h>
19fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <sys/types.h>
20fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
21fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/SortedVector.h>
22fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/KeyedVector.h>
23fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/threads.h>
24fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/Atomic.h>
25fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/Errors.h>
26fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/RefBase.h>
27451beee076cac09f817abae78a990dea108a9482Mathias Agopian#include <utils/Singleton.h>
28c4a930d1d5a432a1f302763ac55460d6e83fe7e0Mathias Agopian#include <utils/String16.h>
29fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
30fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <binder/BinderService.h>
31451beee076cac09f817abae78a990dea108a9482Mathias Agopian#include <binder/IServiceManager.h>
32fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
33fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <gui/ISensorServer.h>
34fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <gui/ISensorEventConnection.h>
35fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
36fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <hardware/sensors.h>
37fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
38fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include "SensorService.h"
39f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include "GravitySensor.h"
40f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include "LinearAccelerationSensor.h"
41f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include "RotationVectorSensor.h"
42fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
43fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopiannamespace android {
44fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// ---------------------------------------------------------------------------
45fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
46fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorService()
47fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    : Thread(false),
4850df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian      mDump("android.permission.DUMP"),
4950df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian      mInitCheck(NO_INIT)
50fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
51fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
52fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
53fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianvoid SensorService::onFirstRef()
54fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
5550df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian    LOGD("nuSensorService starting...");
5650df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian
57f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    SensorDevice& dev(SensorDevice::getInstance());
58fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
59f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    if (dev.initCheck() == NO_ERROR) {
60f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        uint32_t virtualSensorsNeeds =
61f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                (1<<SENSOR_TYPE_GRAVITY) |
62f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                (1<<SENSOR_TYPE_LINEAR_ACCELERATION) |
63f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                (1<<SENSOR_TYPE_ROTATION_VECTOR);
64f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        sensor_t const* list;
65f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        int count = dev.getSensorList(&list);
663560fb24b668675627934356f210d84d19bf4e56Mathias Agopian        mLastEventSeen.setCapacity(count);
6750df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian        for (int i=0 ; i<count ; i++) {
68f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            registerSensor( new HardwareSensor(list[i]) );
69f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            switch (list[i].type) {
70f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                case SENSOR_TYPE_GRAVITY:
71f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                case SENSOR_TYPE_LINEAR_ACCELERATION:
72f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                case SENSOR_TYPE_ROTATION_VECTOR:
73f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                    virtualSensorsNeeds &= ~(1<<list[i].type);
74f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                    break;
7550df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian            }
7650df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian        }
77fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
78f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        if (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) {
79f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            registerVirtualSensor( new GravitySensor(list, count) );
80f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        }
81f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        if (virtualSensorsNeeds & (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) {
82f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            registerVirtualSensor( new LinearAccelerationSensor(list, count) );
83f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        }
84f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {
85f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            registerVirtualSensor( new RotationVectorSensor(list, count) );
8650df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian        }
87f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
88f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        run("SensorService", PRIORITY_URGENT_DISPLAY);
89f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        mInitCheck = NO_ERROR;
90fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    }
91fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
92fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
93f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianvoid SensorService::registerSensor(SensorInterface* s)
94f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{
95f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    sensors_event_t event;
96f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    memset(&event, 0, sizeof(event));
97f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
98f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    const Sensor sensor(s->getSensor());
99f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    // add to the sensor list (returned to clients)
100f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    mSensorList.add(sensor);
101f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    // add to our handle->SensorInterface mapping
102f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    mSensorMap.add(sensor.getHandle(), s);
103f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    // create an entry in the mLastEventSeen array
104f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    mLastEventSeen.add(sensor.getHandle(), event);
105f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian}
106f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
107f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianvoid SensorService::registerVirtualSensor(SensorInterface* s)
108f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{
109f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    registerSensor(s);
110f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    mVirtualSensorList.add( s );
111f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian}
112f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
113fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::~SensorService()
114fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
115f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    for (size_t i=0 ; i<mSensorMap.size() ; i++)
116f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        delete mSensorMap.valueAt(i);
117fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
118fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
119fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::dump(int fd, const Vector<String16>& args)
120fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
121fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    const size_t SIZE = 1024;
122fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    char buffer[SIZE];
123fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    String8 result;
124fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    if (!mDump.checkCalling()) {
125fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        snprintf(buffer, SIZE, "Permission Denial: "
126fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian                "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
127fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian                IPCThreadState::self()->getCallingPid(),
128fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian                IPCThreadState::self()->getCallingUid());
129fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        result.append(buffer);
130fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    } else {
131fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        Mutex::Autolock _l(mLock);
1323560fb24b668675627934356f210d84d19bf4e56Mathias Agopian        snprintf(buffer, SIZE, "Sensor List:\n");
1333560fb24b668675627934356f210d84d19bf4e56Mathias Agopian        result.append(buffer);
1343560fb24b668675627934356f210d84d19bf4e56Mathias Agopian        for (size_t i=0 ; i<mSensorList.size() ; i++) {
1353560fb24b668675627934356f210d84d19bf4e56Mathias Agopian            const Sensor& s(mSensorList[i]);
1363560fb24b668675627934356f210d84d19bf4e56Mathias Agopian            const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
137f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            snprintf(buffer, SIZE, "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | last=<%5.1f,%5.1f,%5.1f>\n",
1383560fb24b668675627934356f210d84d19bf4e56Mathias Agopian                    s.getName().string(),
1393560fb24b668675627934356f210d84d19bf4e56Mathias Agopian                    s.getVendor().string(),
1403560fb24b668675627934356f210d84d19bf4e56Mathias Agopian                    s.getHandle(),
14124d72350f321c17b0bfe1ef3fd52d4070a1c02c3Mathias Agopian                    s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f,
1423560fb24b668675627934356f210d84d19bf4e56Mathias Agopian                    e.data[0], e.data[1], e.data[2]);
1433560fb24b668675627934356f210d84d19bf4e56Mathias Agopian            result.append(buffer);
1443560fb24b668675627934356f210d84d19bf4e56Mathias Agopian        }
145f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        SensorDevice::getInstance().dump(result, buffer, SIZE);
1463560fb24b668675627934356f210d84d19bf4e56Mathias Agopian
1477c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian        snprintf(buffer, SIZE, "%d active connections\n",
1487c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian                mActiveConnections.size());
149fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        result.append(buffer);
150fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        snprintf(buffer, SIZE, "Active sensors:\n");
151fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        result.append(buffer);
152fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
1535d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian            int handle = mActiveSensors.keyAt(i);
154f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n",
1555d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian                    getSensorName(handle).string(),
1565d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian                    handle,
157fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian                    mActiveSensors.valueAt(i)->getNumConnections());
158fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian            result.append(buffer);
159fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        }
160fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    }
161fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    write(fd, result.string(), result.size());
162fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    return NO_ERROR;
163fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
164fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
165fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::threadLoop()
166fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
167fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    LOGD("nuSensorService thread starting...");
168fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
169f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    const size_t numEventMax = 16 * (1 + mVirtualSensorList.size());
170f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    sensors_event_t buffer[numEventMax];
171f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    sensors_event_t scratch[numEventMax];
172f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    SensorDevice& device(SensorDevice::getInstance());
173f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    const size_t vcount = mVirtualSensorList.size();
174fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
175f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    ssize_t count;
176fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    do {
177f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        count = device.poll(buffer, numEventMax);
178fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        if (count<0) {
179fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian            LOGE("sensor poll failed (%s)", strerror(-count));
180fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian            break;
181fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        }
182fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
18394e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian        recordLastValue(buffer, count);
18494e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian
185f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        // handle virtual sensors
186f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        if (count && vcount) {
187f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
188f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                    getActiveVirtualSensors());
189f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            const size_t activeVirtualSensorCount = virtualSensors.size();
190f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            if (activeVirtualSensorCount) {
191f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                size_t k = 0;
192f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                for (size_t i=0 ; i<size_t(count) ; i++) {
193f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                    sensors_event_t const * const event = buffer;
194f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                    for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
195f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                        sensors_event_t out;
196f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                        if (virtualSensors.valueAt(j)->process(&out, event[i])) {
197f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                            buffer[count + k] = out;
198f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                            k++;
199f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                        }
200f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                    }
201f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                }
202f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                if (k) {
203f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                    // record the last synthesized values
204f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                    recordLastValue(&buffer[count], k);
205f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                    count += k;
206f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                    // sort the buffer by time-stamps
207f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                    sortEventBuffer(buffer, count);
208f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                }
209f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            }
210f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        }
211f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
212f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        // send our events to clients...
213fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        const SortedVector< wp<SensorEventConnection> > activeConnections(
214fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian                getActiveConnections());
215fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        size_t numConnections = activeConnections.size();
216f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        for (size_t i=0 ; i<numConnections ; i++) {
217f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            sp<SensorEventConnection> connection(
218f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                    activeConnections[i].promote());
219f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            if (connection != 0) {
220f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                connection->sendEvents(buffer, count, scratch);
221fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian            }
222fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        }
223fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    } while (count >= 0 || Thread::exitPending());
224fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
225fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    LOGW("Exiting SensorService::threadLoop!");
226fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    return false;
227fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
228fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
22994e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopianvoid SensorService::recordLastValue(
23094e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian        sensors_event_t const * buffer, size_t count)
23194e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian{
23294e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian    Mutex::Autolock _l(mLock);
23394e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian
23494e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian    // record the last event for each sensor
23594e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian    int32_t prev = buffer[0].sensor;
23694e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian    for (size_t i=1 ; i<count ; i++) {
23794e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian        // record the last event of each sensor type in this buffer
23894e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian        int32_t curr = buffer[i].sensor;
23994e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian        if (curr != prev) {
24094e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian            mLastEventSeen.editValueFor(prev) = buffer[i-1];
24194e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian            prev = curr;
24294e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian        }
24394e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian    }
24494e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian    mLastEventSeen.editValueFor(prev) = buffer[count-1];
24594e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian}
24694e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian
247f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianvoid SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
248f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{
249f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    struct compar {
250f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        static int cmp(void const* lhs, void const* rhs) {
251f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
252f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
253f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            return r->timestamp - l->timestamp;
254f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        }
255f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    };
256f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
257f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian}
258f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
259fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSortedVector< wp<SensorService::SensorEventConnection> >
260fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::getActiveConnections() const
261fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
262fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    Mutex::Autolock _l(mLock);
263fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    return mActiveConnections;
264fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
265fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
266f001c92436b4a66eb7687286325ced7f10c9f917Mathias AgopianDefaultKeyedVector<int, SensorInterface*>
267f001c92436b4a66eb7687286325ced7f10c9f917Mathias AgopianSensorService::getActiveVirtualSensors() const
268f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{
269f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    Mutex::Autolock _l(mLock);
270f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    return mActiveVirtualSensors;
271f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian}
272f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
2735d2707214dfb97bd8dfcc6620be36841d3c82420Mathias AgopianString8 SensorService::getSensorName(int handle) const {
2745d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian    size_t count = mSensorList.size();
2755d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian    for (size_t i=0 ; i<count ; i++) {
2765d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian        const Sensor& sensor(mSensorList[i]);
2775d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian        if (sensor.getHandle() == handle) {
2785d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian            return sensor.getName();
2795d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian        }
2805d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian    }
2815d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian    String8 result("unknown");
2825d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian    return result;
2835d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian}
2845d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian
285fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianVector<Sensor> SensorService::getSensorList()
286fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
287fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    return mSensorList;
288fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
289fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
290fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopiansp<ISensorEventConnection> SensorService::createSensorEventConnection()
291fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
292fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    sp<SensorEventConnection> result(new SensorEventConnection(this));
293fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    return result;
294fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
295fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
296fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianvoid SensorService::cleanupConnection(const wp<SensorEventConnection>& connection)
297fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
298fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    Mutex::Autolock _l(mLock);
2997c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian    size_t size = mActiveSensors.size();
3007c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian    for (size_t i=0 ; i<size ; ) {
3017c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian        SensorRecord* rec = mActiveSensors.valueAt(i);
3027c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian        if (rec && rec->removeConnection(connection)) {
303f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            int handle = mActiveSensors.keyAt(i);
304f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            SensorInterface* sensor = mSensorMap.valueFor( handle );
305f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            if (sensor) {
306f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                sensor->activate(connection.unsafe_get(), false);
307f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            }
3087c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian            mActiveSensors.removeItemsAt(i, 1);
309f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            mActiveVirtualSensors.removeItem(handle);
3107c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian            delete rec;
3117c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian            size--;
3127c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian        } else {
3137c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian            i++;
314fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        }
315fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    }
3167c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian    mActiveConnections.remove(connection);
317fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
318fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
319fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::enable(const sp<SensorEventConnection>& connection,
320fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        int handle)
321fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
32250df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian    if (mInitCheck != NO_ERROR)
32350df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian        return mInitCheck;
32450df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian
325fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    Mutex::Autolock _l(mLock);
326f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    SensorInterface* sensor = mSensorMap.valueFor(handle);
327f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE);
328f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    if (err == NO_ERROR) {
329f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        SensorRecord* rec = mActiveSensors.valueFor(handle);
330f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        if (rec == 0) {
331f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            rec = new SensorRecord(connection);
332f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            mActiveSensors.add(handle, rec);
333f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            if (sensor->isVirtual()) {
334f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                mActiveVirtualSensors.add(handle, sensor);
335f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            }
336f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        } else {
337f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            if (rec->addConnection(connection)) {
338f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                // this sensor is already activated, but we are adding a
339f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                // connection that uses it. Immediately send down the last
340f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                // known value of the requested sensor.
341f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                sensors_event_t scratch;
342f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                sensors_event_t& event(mLastEventSeen.editValueFor(handle));
343f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                if (event.version == sizeof(sensors_event_t)) {
344f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                    connection->sendEvents(&event, 1);
345f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                }
3463560fb24b668675627934356f210d84d19bf4e56Mathias Agopian            }
3473560fb24b668675627934356f210d84d19bf4e56Mathias Agopian        }
348f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        if (err == NO_ERROR) {
349f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            // connection now active
350f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            if (connection->addSensor(handle)) {
351f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                // the sensor was added (which means it wasn't already there)
352f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                // so, see if this connection becomes active
353f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                if (mActiveConnections.indexOf(connection) < 0) {
354f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                    mActiveConnections.add(connection);
355f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian                }
3567c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian            }
357fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        }
358fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    }
359fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    return err;
360fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
361fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
362fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::disable(const sp<SensorEventConnection>& connection,
363fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        int handle)
364fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
36550df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian    if (mInitCheck != NO_ERROR)
36650df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian        return mInitCheck;
36750df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian
368fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    status_t err = NO_ERROR;
369fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    Mutex::Autolock _l(mLock);
370fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    SensorRecord* rec = mActiveSensors.valueFor(handle);
371fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    if (rec) {
372fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        // see if this connection becomes inactive
373fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        connection->removeSensor(handle);
374fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        if (connection->hasAnySensor() == false) {
375fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian            mActiveConnections.remove(connection);
376fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        }
377fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        // see if this sensor becomes inactive
378fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        if (rec->removeConnection(connection)) {
379fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian            mActiveSensors.removeItem(handle);
380f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            mActiveVirtualSensors.removeItem(handle);
381fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian            delete rec;
382fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        }
383f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        SensorInterface* sensor = mSensorMap.valueFor(handle);
384f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
3857c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian    }
386fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    return err;
387fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
388fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
3897c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianstatus_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
390fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        int handle, nsecs_t ns)
391fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
39250df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian    if (mInitCheck != NO_ERROR)
39350df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian        return mInitCheck;
39450df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian
3951cd700015318727d6d42236ab6274f1949fb08baMathias Agopian    if (ns < 0)
3961cd700015318727d6d42236ab6274f1949fb08baMathias Agopian        return BAD_VALUE;
3971cd700015318727d6d42236ab6274f1949fb08baMathias Agopian
3987c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian    if (ns < MINIMUM_EVENTS_PERIOD)
3997c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian        ns = MINIMUM_EVENTS_PERIOD;
4001cd700015318727d6d42236ab6274f1949fb08baMathias Agopian
401f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    SensorInterface* sensor = mSensorMap.valueFor(handle);
402f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    if (!sensor) return BAD_VALUE;
403f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    return sensor->setDelay(connection.get(), handle, ns);
404fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
405fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
406fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// ---------------------------------------------------------------------------
407fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
408fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorRecord::SensorRecord(
409fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        const sp<SensorEventConnection>& connection)
410fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
411fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    mConnections.add(connection);
412fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
413fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
4147c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianbool SensorService::SensorRecord::addConnection(
415fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        const sp<SensorEventConnection>& connection)
416fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
417fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    if (mConnections.indexOf(connection) < 0) {
418fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        mConnections.add(connection);
4197c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian        return true;
420fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    }
4217c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian    return false;
422fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
423fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
424fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::SensorRecord::removeConnection(
425fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        const wp<SensorEventConnection>& connection)
426fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
427fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    ssize_t index = mConnections.indexOf(connection);
428fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    if (index >= 0) {
429fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        mConnections.removeItemsAt(index, 1);
430fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    }
431fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    return mConnections.size() ? false : true;
432fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
433fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
434fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// ---------------------------------------------------------------------------
435fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
436fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorEventConnection::SensorEventConnection(
437fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        const sp<SensorService>& service)
438fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    : mService(service), mChannel(new SensorChannel())
439fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
440fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
441fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
442fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorEventConnection::~SensorEventConnection()
443fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
444fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    mService->cleanupConnection(this);
445fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
446fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
447fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianvoid SensorService::SensorEventConnection::onFirstRef()
448fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
449fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
450fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
4517c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianbool SensorService::SensorEventConnection::addSensor(int32_t handle) {
45271d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian    Mutex::Autolock _l(mConnectionLock);
453f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    if (mSensorInfo.indexOf(handle) <= 0) {
454f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        mSensorInfo.add(handle);
4557c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian        return true;
456fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    }
4577c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian    return false;
458fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
459fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
4607c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianbool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
46171d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian    Mutex::Autolock _l(mConnectionLock);
462f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    if (mSensorInfo.remove(handle) >= 0) {
4637c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian        return true;
4647c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian    }
4657c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian    return false;
466fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
467fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
468fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
46971d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian    Mutex::Autolock _l(mConnectionLock);
470f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    return mSensorInfo.indexOf(handle) >= 0;
471fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
472fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
473fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::SensorEventConnection::hasAnySensor() const {
47471d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian    Mutex::Autolock _l(mConnectionLock);
4757c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian    return mSensorInfo.size() ? true : false;
4767c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian}
4777c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian
478fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::SensorEventConnection::sendEvents(
479cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian        sensors_event_t const* buffer, size_t numEvents,
480cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian        sensors_event_t* scratch)
481fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
482cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian    // filter out events not for this connection
4833560fb24b668675627934356f210d84d19bf4e56Mathias Agopian    size_t count = 0;
4843560fb24b668675627934356f210d84d19bf4e56Mathias Agopian    if (scratch) {
48571d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian        Mutex::Autolock _l(mConnectionLock);
4863560fb24b668675627934356f210d84d19bf4e56Mathias Agopian        size_t i=0;
4873560fb24b668675627934356f210d84d19bf4e56Mathias Agopian        while (i<numEvents) {
4883560fb24b668675627934356f210d84d19bf4e56Mathias Agopian            const int32_t curr = buffer[i].sensor;
489f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian            if (mSensorInfo.indexOf(curr) >= 0) {
4903560fb24b668675627934356f210d84d19bf4e56Mathias Agopian                do {
4913560fb24b668675627934356f210d84d19bf4e56Mathias Agopian                    scratch[count++] = buffer[i++];
4923560fb24b668675627934356f210d84d19bf4e56Mathias Agopian                } while ((i<numEvents) && (buffer[i].sensor == curr));
4933560fb24b668675627934356f210d84d19bf4e56Mathias Agopian            } else {
4943560fb24b668675627934356f210d84d19bf4e56Mathias Agopian                i++;
4953560fb24b668675627934356f210d84d19bf4e56Mathias Agopian            }
496cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian        }
4973560fb24b668675627934356f210d84d19bf4e56Mathias Agopian    } else {
4983560fb24b668675627934356f210d84d19bf4e56Mathias Agopian        scratch = const_cast<sensors_event_t *>(buffer);
4993560fb24b668675627934356f210d84d19bf4e56Mathias Agopian        count = numEvents;
500cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian    }
501fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
5023560fb24b668675627934356f210d84d19bf4e56Mathias Agopian    if (count == 0)
5033560fb24b668675627934356f210d84d19bf4e56Mathias Agopian        return 0;
5043560fb24b668675627934356f210d84d19bf4e56Mathias Agopian
505cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian    ssize_t size = mChannel->write(scratch, count*sizeof(sensors_event_t));
506fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    if (size == -EAGAIN) {
507fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        // the destination doesn't accept events anymore, it's probably
508fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        // full. For now, we just drop the events on the floor.
509fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        LOGW("dropping %d events on the floor", count);
510fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        return size;
511fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    }
512fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
513fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    LOGE_IF(size<0, "dropping %d events on the floor (%s)",
514fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian            count, strerror(-size));
515fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
5161e0b1e8491e5f6dc59faabe70cbfa942853150e0Jeff Brown    return size < 0 ? status_t(size) : status_t(NO_ERROR);
517fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
518fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
519fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopiansp<SensorChannel> SensorService::SensorEventConnection::getSensorChannel() const
520fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
521fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    return mChannel;
522fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
523fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
524fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::SensorEventConnection::enableDisable(
525fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        int handle, bool enabled)
526fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
527fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    status_t err;
528fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    if (enabled) {
529fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        err = mService->enable(this, handle);
530fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    } else {
531fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        err = mService->disable(this, handle);
532fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    }
533fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    return err;
534fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
535fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
536fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::SensorEventConnection::setEventRate(
537fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        int handle, nsecs_t ns)
538fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
5397c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian    return mService->setEventRate(this, handle, ns);
540fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}
541fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
542fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// ---------------------------------------------------------------------------
543fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}; // namespace android
544fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
545