Home | History | Annotate | Download | only in gui
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #define LOG_TAG "Sensors"
     18 
     19 #include <stdint.h>
     20 #include <sys/types.h>
     21 
     22 #include <utils/Errors.h>
     23 #include <utils/RefBase.h>
     24 #include <utils/Looper.h>
     25 
     26 #include <gui/Sensor.h>
     27 #include <gui/BitTube.h>
     28 #include <gui/SensorEventQueue.h>
     29 #include <gui/ISensorEventConnection.h>
     30 
     31 #include <android/sensor.h>
     32 
     33 // ----------------------------------------------------------------------------
     34 namespace android {
     35 // ----------------------------------------------------------------------------
     36 
     37 SensorEventQueue::SensorEventQueue(const sp<ISensorEventConnection>& connection)
     38     : mSensorEventConnection(connection), mRecBuffer(NULL), mAvailable(0), mConsumed(0) {
     39     mRecBuffer = new ASensorEvent[MAX_RECEIVE_BUFFER_EVENT_COUNT];
     40 }
     41 
     42 SensorEventQueue::~SensorEventQueue() {
     43     delete [] mRecBuffer;
     44 }
     45 
     46 void SensorEventQueue::onFirstRef()
     47 {
     48     mSensorChannel = mSensorEventConnection->getSensorChannel();
     49 }
     50 
     51 int SensorEventQueue::getFd() const
     52 {
     53     return mSensorChannel->getFd();
     54 }
     55 
     56 
     57 ssize_t SensorEventQueue::write(const sp<BitTube>& tube,
     58         ASensorEvent const* events, size_t numEvents) {
     59     return BitTube::sendObjects(tube, events, numEvents);
     60 }
     61 
     62 ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents) {
     63     if (mAvailable == 0) {
     64         ssize_t err = BitTube::recvObjects(mSensorChannel,
     65                 mRecBuffer, MAX_RECEIVE_BUFFER_EVENT_COUNT);
     66         if (err < 0) {
     67             return err;
     68         }
     69         mAvailable = err;
     70         mConsumed = 0;
     71     }
     72     size_t count = numEvents < mAvailable ? numEvents : mAvailable;
     73     memcpy(events, mRecBuffer + mConsumed, count*sizeof(ASensorEvent));
     74     mAvailable -= count;
     75     mConsumed += count;
     76     return count;
     77 }
     78 
     79 sp<Looper> SensorEventQueue::getLooper() const
     80 {
     81     Mutex::Autolock _l(mLock);
     82     if (mLooper == 0) {
     83         mLooper = new Looper(true);
     84         mLooper->addFd(getFd(), getFd(), ALOOPER_EVENT_INPUT, NULL, NULL);
     85     }
     86     return mLooper;
     87 }
     88 
     89 status_t SensorEventQueue::waitForEvent() const
     90 {
     91     const int fd = getFd();
     92     sp<Looper> looper(getLooper());
     93 
     94     int events;
     95     int32_t result;
     96     do {
     97         result = looper->pollOnce(-1, NULL, &events, NULL);
     98         if (result == ALOOPER_POLL_ERROR) {
     99             ALOGE("SensorEventQueue::waitForEvent error (errno=%d)", errno);
    100             result = -EPIPE; // unknown error, so we make up one
    101             break;
    102         }
    103         if (events & ALOOPER_EVENT_HANGUP) {
    104             // the other-side has died
    105             ALOGE("SensorEventQueue::waitForEvent error HANGUP");
    106             result = -EPIPE; // unknown error, so we make up one
    107             break;
    108         }
    109     } while (result != fd);
    110 
    111     return  (result == fd) ? status_t(NO_ERROR) : result;
    112 }
    113 
    114 status_t SensorEventQueue::wake() const
    115 {
    116     sp<Looper> looper(getLooper());
    117     looper->wake();
    118     return NO_ERROR;
    119 }
    120 
    121 status_t SensorEventQueue::enableSensor(Sensor const* sensor) const {
    122     return mSensorEventConnection->enableDisable(sensor->getHandle(), true, 0, 0, false);
    123 }
    124 
    125 status_t SensorEventQueue::disableSensor(Sensor const* sensor) const {
    126     return mSensorEventConnection->enableDisable(sensor->getHandle(), false, 0, 0, false);
    127 }
    128 
    129 status_t SensorEventQueue::enableSensor(int32_t handle, int32_t samplingPeriodUs,
    130                                         int maxBatchReportLatencyUs, int reservedFlags) const {
    131     return mSensorEventConnection->enableDisable(handle, true, us2ns(samplingPeriodUs),
    132                                                  us2ns(maxBatchReportLatencyUs), reservedFlags);
    133 }
    134 
    135 status_t SensorEventQueue::flush() const {
    136     return mSensorEventConnection->flush();
    137 }
    138 
    139 status_t SensorEventQueue::disableSensor(int32_t handle) const {
    140     return mSensorEventConnection->enableDisable(handle, false, 0, 0, false);
    141 }
    142 
    143 status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const {
    144     return mSensorEventConnection->setEventRate(sensor->getHandle(), ns);
    145 }
    146 
    147 // ----------------------------------------------------------------------------
    148 }; // namespace android
    149 
    150