summaryrefslogtreecommitdiff
path: root/modules/sensors/dynamic_sensor/DynamicSensorManager.h
diff options
context:
space:
mode:
authorPeng Xu <pengxu@google.com>2017-01-20 14:24:39 -0800
committerPeng Xu <pengxu@google.com>2017-03-07 20:50:56 -0800
commit57d8af7f5ecbdbadb043b05ac5874989ef33fdad (patch)
treea315a9abcecd5d6adb6603e22efb2ead32ee6262 /modules/sensors/dynamic_sensor/DynamicSensorManager.h
parent2e643b42ad6fea21d0be2adf21f4d6832205528a (diff)
Dynamic sensor manager module - framework
Library to handle dynamic sensor connection. There are two way to use this: as hal extension or standalone hal module. In hal extension mode: add libdynamic_sensor_ext in dependency of hal, instantiate DynamicSensorManager with appropriate parameters. Then for all sensor requests, if the handle is owned by dynamic sensor manager, forward the request. In standalone mode, add sensor.dynamic_sensor_hal into device make file. Usually, this also means multihal is necessary. Add sensor.dynamic_sensor_hal into multihal configuration file. This CL implements the dynamic sensor manager framework. Sensor daemon will be added in follow up CL. Test: test compile (functionality test done in a follow up CL) Change-Id: I3b96ee135d8dbe3e199af01bed4b61637358803e
Diffstat (limited to 'modules/sensors/dynamic_sensor/DynamicSensorManager.h')
-rw-r--r--modules/sensors/dynamic_sensor/DynamicSensorManager.h137
1 files changed, 137 insertions, 0 deletions
diff --git a/modules/sensors/dynamic_sensor/DynamicSensorManager.h b/modules/sensors/dynamic_sensor/DynamicSensorManager.h
new file mode 100644
index 00000000..b8fd4721
--- /dev/null
+++ b/modules/sensors/dynamic_sensor/DynamicSensorManager.h
@@ -0,0 +1,137 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_SENSORHAL_EXT_DYNAMIC_SENSOR_MANAGER_H
+#define ANDROID_SENSORHAL_EXT_DYNAMIC_SENSOR_MANAGER_H
+
+#include "SensorEventCallback.h"
+#include "RingBuffer.h"
+#include <hardware/sensors.h>
+#include <utils/RefBase.h>
+
+#include <mutex>
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+namespace android {
+namespace SensorHalExt {
+
+class BaseDynamicSensorDaemon;
+
+class DynamicSensorManager : public SensorEventCallback {
+public:
+ // handleBase is reserved for the dynamic sensor meta sensor.
+ // handleMax must be greater than handleBase + 1.
+ // This class has two operation mode depending on callback: 1) extension, 2) stand-alone.
+ // In extension mode, callback must not be nullptr. Sensor event generated will be submitted to
+ // buffer of primary sensor HAL implementation. In stand-alone mode, callback must be nullptr.
+ // Generated sensor events will be added into internal buffer waiting for poll() function to
+ // pick up.
+ //
+ static DynamicSensorManager* createInstance(
+ int handleBase, int handleCount, SensorEventCallback *callback);
+ virtual ~DynamicSensorManager();
+
+ // calls to add or remove sensor, called from sensor daemon
+ bool registerSensor(sp<BaseSensorObject> sensor);
+ void unregisterSensor(sp<BaseSensorObject> sensor);
+
+ // Determine if a sensor handle is in the range defined in constructor.
+ // It does not test if sensor handle is valid.
+ bool owns(int handle) const;
+
+ // handles sensor hal requests.
+ int activate(int handle, bool enable);
+ int batch(int handle, nsecs_t sample_period, nsecs_t batch_period);
+ int setDelay(int handle, nsecs_t sample_period);
+ int flush(int handle);
+ int poll(sensors_event_t * data, int count);
+
+ // SensorEventCallback
+ virtual int submitEvent(sp<BaseSensorObject>, const sensors_event_t &e) override;
+
+ // get meta sensor struct
+ const sensor_t& getDynamicMetaSensor() const;
+protected:
+ DynamicSensorManager(int handleBase, int handleMax, SensorEventCallback* callback);
+private:
+ // a helper class used for generate connection and disconnection report
+ class ConnectionReport {
+ public:
+ ConnectionReport() {}
+ ConnectionReport(int handle, sp<BaseSensorObject> sensor);
+ ~ConnectionReport();
+ const sensors_event_t& generateConnectionEvent(int metaHandle);
+ static void fillDisconnectionEvent(sensors_event_t* event, int metaHandle, int handle);
+ private:
+ sensor_t mSensor;
+ std::string mName;
+ std::string mVendor;
+ std::string mPermission;
+ std::string mStringType;
+ sensors_event_t mEvent;
+ uint8_t mUuid[16];
+ bool mGenerated;
+ DISALLOW_EVIL_CONSTRUCTORS(ConnectionReport);
+ };
+
+ // returns next available handle to use upon a new sensor connection, or -1 if we run out.
+ int getNextAvailableHandle();
+
+ // TF: int foo(sp<BaseSensorObject> obj);
+ template <typename TF>
+ int operateSensor(int handle, TF f) const {
+ std::lock_guard<std::mutex> lk(mLock);
+ const auto i = mMap.find(handle);
+ if (i == mMap.end()) {
+ return BAD_VALUE;
+ }
+ sp<BaseSensorObject> s = i->second.promote();
+ if (s == nullptr) {
+ // sensor object is already gone
+ return BAD_VALUE;
+ }
+ return f(s);
+ }
+
+ // available sensor handle space
+ const std::pair<int, int> mHandleRange;
+ sensor_t mMetaSensor;
+
+ // immutable pointer to event callback, used in extention mode.
+ SensorEventCallback * const mCallback;
+
+ // RingBuffer used in standalone mode
+ static constexpr size_t kFifoSize = 4096; //4K events
+ mutable std::mutex mFifoLock;
+ RingBuffer mFifo;
+
+ // mapping between handle and SensorObjects
+ mutable std::mutex mLock;
+ int mNextHandle;
+ std::unordered_map<int, wp<BaseSensorObject> > mMap;
+ std::unordered_map<void *, int> mReverseMap;
+ mutable std::unordered_map<int, ConnectionReport> mPendingReport;
+
+ // daemons
+ std::vector<sp<BaseDynamicSensorDaemon>> mDaemonVector;
+};
+
+} // namespace SensorHalExt
+} // namespace android
+
+#endif // ANDROID_SENSORHAL_EXT_DYNAMIC_SENSOR_MANAGER_H