Home | History | Annotate | Download | only in sensor
      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 #pragma once
     18 
     19 #include <stdint.h>
     20 #include <sys/types.h>
     21 
     22 #include <utils/Errors.h>
     23 #include <utils/RefBase.h>
     24 #include <utils/Timers.h>
     25 #include <utils/Mutex.h>
     26 
     27 #include <sensor/BitTube.h>
     28 
     29 // ----------------------------------------------------------------------------
     30 #define WAKE_UP_SENSOR_EVENT_NEEDS_ACK (1U << 31)
     31 struct ALooper;
     32 struct ASensorEvent;
     33 
     34 // Concrete types for the NDK
     35 struct ASensorEventQueue {
     36     ALooper* looper;
     37 };
     38 
     39 // ----------------------------------------------------------------------------
     40 namespace android {
     41 // ----------------------------------------------------------------------------
     42 
     43 class ISensorEventConnection;
     44 class Sensor;
     45 class Looper;
     46 
     47 // ----------------------------------------------------------------------------
     48 
     49 class SensorEventQueue : public ASensorEventQueue, public RefBase
     50 {
     51 public:
     52 
     53     enum { MAX_RECEIVE_BUFFER_EVENT_COUNT = 256 };
     54 
     55     /**
     56      * Typical sensor delay (sample period) in microseconds.
     57      */
     58     // Fastest sampling, system will bound it to minDelay
     59     static constexpr int32_t SENSOR_DELAY_FASTEST = 0;
     60     // Typical sample period for game, 50Hz;
     61     static constexpr int32_t SENSOR_DELAY_GAME = 20000;
     62     // Typical sample period for UI, 15Hz
     63     static constexpr int32_t SENSOR_DELAY_UI = 66667;
     64     // Default sensor sample period
     65     static constexpr int32_t SENSOR_DELAY_NORMAL = 200000;
     66 
     67     SensorEventQueue(const sp<ISensorEventConnection>& connection);
     68     virtual ~SensorEventQueue();
     69     virtual void onFirstRef();
     70 
     71     int getFd() const;
     72 
     73     static ssize_t write(const sp<BitTube>& tube,
     74             ASensorEvent const* events, size_t numEvents);
     75 
     76     ssize_t read(ASensorEvent* events, size_t numEvents);
     77 
     78     status_t waitForEvent() const;
     79     status_t wake() const;
     80 
     81     status_t enableSensor(Sensor const* sensor) const;
     82     status_t enableSensor(Sensor const* sensor, int32_t samplingPeriodUs) const;
     83     status_t disableSensor(Sensor const* sensor) const;
     84     status_t setEventRate(Sensor const* sensor, nsecs_t ns) const;
     85 
     86     // these are here only to support SensorManager.java and HIDL Frameworks SensorManager.
     87     status_t enableSensor(int32_t handle, int32_t samplingPeriodUs, int64_t maxBatchReportLatencyUs,
     88                           int reservedFlags) const;
     89     status_t disableSensor(int32_t handle) const;
     90     status_t flush() const;
     91     // Send an ack for every wake_up sensor event that is set to WAKE_UP_SENSOR_EVENT_NEEDS_ACK.
     92     void sendAck(const ASensorEvent* events, int count);
     93 
     94     status_t injectSensorEvent(const ASensorEvent& event);
     95 private:
     96     sp<Looper> getLooper() const;
     97     sp<ISensorEventConnection> mSensorEventConnection;
     98     sp<BitTube> mSensorChannel;
     99     mutable Mutex mLock;
    100     mutable sp<Looper> mLooper;
    101     ASensorEvent* mRecBuffer;
    102     size_t mAvailable;
    103     size_t mConsumed;
    104     uint32_t mNumAcksToSend;
    105 };
    106 
    107 // ----------------------------------------------------------------------------
    108 }; // namespace android
    109