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 <mutex>
26 #include <string>
27 #include <unordered_map>
28 #include <vector>
29 
30 namespace android {
31 namespace SensorHalExt {
32 
33 class BaseDynamicSensorDaemon;
34 
35 class DynamicSensorManager : public SensorEventCallback {
36 public:
37     // handleBase is reserved for the dynamic sensor meta sensor.
38     // handleMax must be greater than handleBase + 1.
39     // This class has two operation mode depending on callback: 1) extension, 2) stand-alone.
40     // In extension mode, callback must not be nullptr. Sensor event generated will be submitted to
41     // buffer of primary sensor HAL implementation. In stand-alone mode, callback must be nullptr.
42     // Generated sensor events will be added into internal buffer waiting for poll() function to
43     // pick up.
44     //
45     static DynamicSensorManager* createInstance(
46             int handleBase, int handleCount, SensorEventCallback *callback);
47     virtual ~DynamicSensorManager();
48 
49     // calls to add or remove sensor, called from sensor daemon
50     bool registerSensor(sp<BaseSensorObject> sensor);
51     void unregisterSensor(sp<BaseSensorObject> sensor);
52 
53     // Determine if a sensor handle is in the range defined in constructor.
54     // It does not test if sensor handle is valid.
55     bool owns(int handle) const;
56 
57     // handles sensor hal requests.
58     int activate(int handle, bool enable);
59     int batch(int handle, nsecs_t sample_period, nsecs_t batch_period);
60     int setDelay(int handle, nsecs_t sample_period);
61     int flush(int handle);
62     int poll(sensors_event_t * data, int count);
63 
64     // SensorEventCallback
65     virtual int submitEvent(sp<BaseSensorObject>, const sensors_event_t &e) override;
66 
67     // get meta sensor struct
68     const sensor_t& getDynamicMetaSensor() const;
69 protected:
70     DynamicSensorManager(int handleBase, int handleMax, SensorEventCallback* callback);
71 private:
72     // a helper class used for generate connection and disconnection report
73     class ConnectionReport {
74     public:
ConnectionReport()75         ConnectionReport() {}
76         ConnectionReport(int handle, sp<BaseSensorObject> sensor);
77         ~ConnectionReport();
78         const sensors_event_t& generateConnectionEvent(int metaHandle);
79         static void fillDisconnectionEvent(sensors_event_t* event, int metaHandle, int handle);
80     private:
81         sensor_t mSensor;
82         std::string mName;
83         std::string mVendor;
84         std::string mPermission;
85         std::string mStringType;
86         sensors_event_t mEvent;
87         uint8_t mUuid[16];
88         bool mGenerated;
89         DISALLOW_EVIL_CONSTRUCTORS(ConnectionReport);
90     };
91 
92     // returns next available handle to use upon a new sensor connection, or -1 if we run out.
93     int getNextAvailableHandle();
94 
95     // TF:  int foo(sp<BaseSensorObject> obj);
96     template <typename TF>
operateSensor(int handle,TF f)97     int operateSensor(int handle, TF f) const {
98         std::lock_guard<std::mutex> lk(mLock);
99         const auto i = mMap.find(handle);
100         if (i == mMap.end()) {
101             return BAD_VALUE;
102         }
103         sp<BaseSensorObject> s = i->second.promote();
104         if (s == nullptr) {
105             // sensor object is already gone
106             return BAD_VALUE;
107         }
108         return f(s);
109     }
110 
111     // available sensor handle space
112     const std::pair<int, int> mHandleRange;
113     sensor_t mMetaSensor;
114 
115     // immutable pointer to event callback, used in extention mode.
116     SensorEventCallback * const mCallback;
117 
118     // RingBuffer used in standalone mode
119     static constexpr size_t kFifoSize = 4096; //4K events
120     mutable std::mutex mFifoLock;
121     RingBuffer mFifo;
122 
123     // mapping between handle and SensorObjects
124     mutable std::mutex mLock;
125     int mNextHandle;
126     std::unordered_map<int, wp<BaseSensorObject>> mMap;
127     std::unordered_map<void *, int> mReverseMap;
128     mutable std::unordered_map<int, ConnectionReport> mPendingReport;
129 
130     // daemons
131     std::vector<sp<BaseDynamicSensorDaemon>> mDaemonVector;
132 };
133 
134 } // namespace SensorHalExt
135 } // namespace android
136 
137 #endif // ANDROID_SENSORHAL_EXT_DYNAMIC_SENSOR_MANAGER_H
138