Home | History | Annotate | Download | only in sensorservice
      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 #ifndef ANDROID_SENSOR_SERVICE_H
     18 #define ANDROID_SENSOR_SERVICE_H
     19 
     20 #include <stdint.h>
     21 #include <sys/types.h>
     22 
     23 #include <utils/Vector.h>
     24 #include <utils/SortedVector.h>
     25 #include <utils/KeyedVector.h>
     26 #include <utils/threads.h>
     27 #include <utils/RefBase.h>
     28 
     29 #include <binder/BinderService.h>
     30 
     31 #include <gui/Sensor.h>
     32 #include <gui/BitTube.h>
     33 #include <gui/ISensorServer.h>
     34 #include <gui/ISensorEventConnection.h>
     35 
     36 #include "SensorInterface.h"
     37 
     38 // ---------------------------------------------------------------------------
     39 
     40 #define DEBUG_CONNECTIONS   false
     41 // Max size is 1 MB which is enough to accept a batch of about 10k events.
     42 #define MAX_SOCKET_BUFFER_SIZE_BATCHED 1024 * 1024
     43 #define SOCKET_BUFFER_SIZE_NON_BATCHED 4 * 1024
     44 
     45 struct sensors_poll_device_t;
     46 struct sensors_module_t;
     47 
     48 namespace android {
     49 // ---------------------------------------------------------------------------
     50 
     51 class SensorService :
     52         public BinderService<SensorService>,
     53         public BnSensorServer,
     54         protected Thread
     55 {
     56     friend class BinderService<SensorService>;
     57 
     58     static const char* WAKE_LOCK_NAME;
     59 
     60     static char const* getServiceName() ANDROID_API { return "sensorservice"; }
     61     SensorService() ANDROID_API;
     62     virtual ~SensorService();
     63 
     64     virtual void onFirstRef();
     65 
     66     // Thread interface
     67     virtual bool threadLoop();
     68 
     69     // ISensorServer interface
     70     virtual Vector<Sensor> getSensorList();
     71     virtual sp<ISensorEventConnection> createSensorEventConnection();
     72     virtual status_t dump(int fd, const Vector<String16>& args);
     73 
     74 
     75     class SensorEventConnection : public BnSensorEventConnection {
     76         virtual ~SensorEventConnection();
     77         virtual void onFirstRef();
     78         virtual sp<BitTube> getSensorChannel() const;
     79         virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs,
     80                                        nsecs_t maxBatchReportLatencyNs, int reservedFlags);
     81         virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs);
     82         virtual status_t flush();
     83         // Count the number of flush complete events which are about to be dropped in the buffer.
     84         // Increment mPendingFlushEventsToSend in mSensorInfo. These flush complete events will be
     85         // sent separately before the next batch of events.
     86         void countFlushCompleteEventsLocked(sensors_event_t* scratch, int numEventsDropped);
     87 
     88         sp<SensorService> const mService;
     89         sp<BitTube> mChannel;
     90         uid_t mUid;
     91         mutable Mutex mConnectionLock;
     92 
     93         struct FlushInfo {
     94             // The number of flush complete events dropped for this sensor is stored here.
     95             // They are sent separately before the next batch of events.
     96             int mPendingFlushEventsToSend;
     97             // Every activate is preceded by a flush. Only after the first flush complete is
     98             // received, the events for the sensor are sent on that *connection*.
     99             bool mFirstFlushPending;
    100             FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {}
    101         };
    102         // protected by SensorService::mLock. Key for this vector is the sensor handle.
    103         KeyedVector<int, FlushInfo> mSensorInfo;
    104 
    105     public:
    106         SensorEventConnection(const sp<SensorService>& service, uid_t uid);
    107 
    108         status_t sendEvents(sensors_event_t const* buffer, size_t count,
    109                 sensors_event_t* scratch = NULL);
    110         bool hasSensor(int32_t handle) const;
    111         bool hasAnySensor() const;
    112         bool addSensor(int32_t handle);
    113         bool removeSensor(int32_t handle);
    114         void setFirstFlushPending(int32_t handle, bool value);
    115         void dump(String8& result);
    116 
    117         uid_t getUid() const { return mUid; }
    118     };
    119 
    120     class SensorRecord {
    121         SortedVector< wp<SensorEventConnection> > mConnections;
    122     public:
    123         SensorRecord(const sp<SensorEventConnection>& connection);
    124         bool addConnection(const sp<SensorEventConnection>& connection);
    125         bool removeConnection(const wp<SensorEventConnection>& connection);
    126         size_t getNumConnections() const { return mConnections.size(); }
    127     };
    128 
    129     SortedVector< wp<SensorEventConnection> > getActiveConnections() const;
    130     DefaultKeyedVector<int, SensorInterface*> getActiveVirtualSensors() const;
    131 
    132     String8 getSensorName(int handle) const;
    133     bool isVirtualSensor(int handle) const;
    134     void recordLastValue(const sensors_event_t* buffer, size_t count);
    135     static void sortEventBuffer(sensors_event_t* buffer, size_t count);
    136     Sensor registerSensor(SensorInterface* sensor);
    137     Sensor registerVirtualSensor(SensorInterface* sensor);
    138     status_t cleanupWithoutDisable(
    139             const sp<SensorEventConnection>& connection, int handle);
    140     status_t cleanupWithoutDisableLocked(
    141             const sp<SensorEventConnection>& connection, int handle);
    142     void cleanupAutoDisabledSensor(const sp<SensorEventConnection>& connection,
    143             sensors_event_t const* buffer, const int count);
    144 
    145     // constants
    146     Vector<Sensor> mSensorList;
    147     Vector<Sensor> mUserSensorListDebug;
    148     Vector<Sensor> mUserSensorList;
    149     DefaultKeyedVector<int, SensorInterface*> mSensorMap;
    150     Vector<SensorInterface *> mVirtualSensorList;
    151     status_t mInitCheck;
    152     size_t mSocketBufferSize;
    153 
    154     // protected by mLock
    155     mutable Mutex mLock;
    156     DefaultKeyedVector<int, SensorRecord*> mActiveSensors;
    157     DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors;
    158     SortedVector< wp<SensorEventConnection> > mActiveConnections;
    159 
    160     // The size of this vector is constant, only the items are mutable
    161     KeyedVector<int32_t, sensors_event_t> mLastEventSeen;
    162 
    163 public:
    164     void cleanupConnection(SensorEventConnection* connection);
    165     status_t enable(const sp<SensorEventConnection>& connection, int handle,
    166                     nsecs_t samplingPeriodNs,  nsecs_t maxBatchReportLatencyNs, int reservedFlags);
    167     status_t disable(const sp<SensorEventConnection>& connection, int handle);
    168     status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns);
    169     status_t flushSensor(const sp<SensorEventConnection>& connection, int handle);
    170 };
    171 
    172 // ---------------------------------------------------------------------------
    173 }; // namespace android
    174 
    175 #endif // ANDROID_SENSOR_SERVICE_H
    176