1 /* 2 * Copyright (C) 2017 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_SENSORHAL_EXT_DYNAMIC_SENSOR_MANAGER_H 18 #define ANDROID_SENSORHAL_EXT_DYNAMIC_SENSOR_MANAGER_H 19 20 #include "SensorEventCallback.h" 21 #include "RingBuffer.h" 22 #include <hardware/sensors.h> 23 #include <utils/RefBase.h> 24 25 #include <future> 26 #include <mutex> 27 #include <queue> 28 #include <string> 29 #include <unordered_map> 30 #include <vector> 31 32 namespace android { 33 namespace SensorHalExt { 34 35 class BaseDynamicSensorDaemon; 36 37 class DynamicSensorManager : public SensorEventCallback { 38 public: 39 // handleBase is reserved for the dynamic sensor meta sensor. 40 // handleMax must be greater than handleBase + 1. 41 // This class has two operation mode depending on callback: 1) extension, 2) stand-alone. 42 // In extension mode, callback must not be nullptr. Sensor event generated will be submitted to 43 // buffer of primary sensor HAL implementation. In stand-alone mode, callback must be nullptr. 44 // Generated sensor events will be added into internal buffer waiting for poll() function to 45 // pick up. 46 // 47 static DynamicSensorManager* createInstance( 48 int handleBase, int handleCount, SensorEventCallback *callback); 49 virtual ~DynamicSensorManager(); 50 51 // calls to add or remove sensor, called from sensor daemon 52 bool registerSensor(sp<BaseSensorObject> sensor); 53 void unregisterSensor(sp<BaseSensorObject> sensor); 54 55 // Determine if a sensor handle is in the range defined in constructor. 56 // It does not test if sensor handle is valid. 57 bool owns(int handle) const; 58 59 // handles sensor hal requests. 60 int activate(int handle, bool enable); 61 int batch(int handle, nsecs_t sample_period, nsecs_t batch_period); 62 int setDelay(int handle, nsecs_t sample_period); 63 int flush(int handle); 64 int poll(sensors_event_t * data, int count); 65 66 // SensorEventCallback 67 virtual int submitEvent(sp<BaseSensorObject>, const sensors_event_t &e) override; 68 69 // get meta sensor struct 70 const sensor_t& getDynamicMetaSensor() const; 71 protected: 72 DynamicSensorManager(int handleBase, int handleMax, SensorEventCallback* callback); 73 private: 74 // a helper class used for generate connection and disconnection report 75 class ConnectionReport { 76 public: ConnectionReport()77 ConnectionReport() {} 78 ConnectionReport(int handle, sp<BaseSensorObject> sensor); 79 ~ConnectionReport(); 80 const sensors_event_t& generateConnectionEvent(int metaHandle); 81 static void fillDisconnectionEvent(sensors_event_t* event, int metaHandle, int handle); 82 private: 83 sensor_t mSensor; 84 std::string mName; 85 std::string mVendor; 86 std::string mPermission; 87 std::string mStringType; 88 sensors_event_t mEvent; 89 uint8_t mUuid[16]; 90 bool mGenerated; 91 DISALLOW_EVIL_CONSTRUCTORS(ConnectionReport); 92 }; 93 94 // returns next available handle to use upon a new sensor connection, or -1 if we run out. 95 int getNextAvailableHandle(); 96 97 // Runs a sensor function with a timeout. On timeout, function could still 98 // be running, so any function parameter or closure lifetimes should match 99 // the function's lifetime. 100 using OperateSensorFunc = std::function<int(sp<BaseSensorObject>)>; 101 int operateSensor(int handle, OperateSensorFunc sensorFunc); 102 int operateSensor(int handle, OperateSensorFunc sensorFunc, 103 uint64_t sensorOpIndex); 104 105 // available sensor handle space 106 const std::pair<int, int> mHandleRange; 107 sensor_t mMetaSensor; 108 bool mMetaSensorActive = false; 109 110 // immutable pointer to event callback, used in extention mode. 111 SensorEventCallback * const mCallback; 112 113 // RingBuffer used in standalone mode 114 static constexpr size_t kFifoSize = 4096; //4K events 115 mutable std::mutex mFifoLock; 116 RingBuffer mFifo; 117 118 // mapping between handle and SensorObjects 119 mutable std::mutex mLock; 120 int mNextHandle; 121 std::unordered_map<int, wp<BaseSensorObject>> mMap; 122 std::unordered_map<void *, int> mReverseMap; 123 mutable std::unordered_map<int, ConnectionReport> mPendingReport; 124 125 // daemons 126 std::vector<sp<BaseDynamicSensorDaemon>> mDaemonVector; 127 128 // Sensor operation queue. Calls to the sensor HAL should complete within ~1 129 // second, but to permit delayed replies due to sniff mode, etc., we use a 130 // slightly longer timeout here. 131 static constexpr std::chrono::milliseconds 132 kSensorOpTimeout = std::chrono::milliseconds(1600); 133 std::mutex mSensorOpQueueLock; 134 std::queue<std::pair<uint64_t, std::shared_future<int>>> mSensorOpQueue; 135 uint64_t mNextSensorOpIndex = 0; 136 }; 137 138 } // namespace SensorHalExt 139 } // namespace android 140 141 #endif // ANDROID_SENSORHAL_EXT_DYNAMIC_SENSOR_MANAGER_H 142