• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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_HARDWARE_SENSORS_V2_0_SENSORS_H
18 #define ANDROID_HARDWARE_SENSORS_V2_0_SENSORS_H
19 
20 #include "Sensor.h"
21 
22 #include <android/hardware/sensors/2.0/ISensors.h>
23 #include <fmq/MessageQueue.h>
24 #include <hardware_legacy/power.h>
25 #include <hidl/MQDescriptor.h>
26 #include <hidl/Status.h>
27 
28 #include <atomic>
29 #include <memory>
30 #include <thread>
31 
32 namespace android {
33 namespace hardware {
34 namespace sensors {
35 namespace V2_0 {
36 namespace implementation {
37 
38 using ::android::sp;
39 using ::android::hardware::EventFlag;
40 using ::android::hardware::hidl_array;
41 using ::android::hardware::hidl_memory;
42 using ::android::hardware::hidl_string;
43 using ::android::hardware::hidl_vec;
44 using ::android::hardware::MessageQueue;
45 using ::android::hardware::MQDescriptor;
46 using ::android::hardware::Return;
47 using ::android::hardware::Void;
48 
49 struct Sensors : public ISensors, public ISensorsEventCallback {
50     using Event = ::android::hardware::sensors::V1_0::Event;
51     using OperationMode = ::android::hardware::sensors::V1_0::OperationMode;
52     using RateLevel = ::android::hardware::sensors::V1_0::RateLevel;
53     using Result = ::android::hardware::sensors::V1_0::Result;
54     using SharedMemInfo = ::android::hardware::sensors::V1_0::SharedMemInfo;
55 
56     Sensors();
57     virtual ~Sensors();
58 
59     // Methods from ::android::hardware::sensors::V2_0::ISensors follow.
60     Return<void> getSensorsList(getSensorsList_cb _hidl_cb) override;
61 
62     Return<Result> setOperationMode(OperationMode mode) override;
63 
64     Return<Result> activate(int32_t sensorHandle, bool enabled) override;
65 
66     Return<Result> initialize(
67         const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor,
68         const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
69         const sp<ISensorsCallback>& sensorsCallback) override;
70 
71     Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs,
72                          int64_t maxReportLatencyNs) override;
73 
74     Return<Result> flush(int32_t sensorHandle) override;
75 
76     Return<Result> injectSensorData(const Event& event) override;
77 
78     Return<void> registerDirectChannel(const SharedMemInfo& mem,
79                                        registerDirectChannel_cb _hidl_cb) override;
80 
81     Return<Result> unregisterDirectChannel(int32_t channelHandle) override;
82 
83     Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
84                                     configDirectReport_cb _hidl_cb) override;
85 
86     void postEvents(const std::vector<Event>& events, bool wakeup) override;
87 
88    private:
89     /**
90      * Add a new sensor
91      */
92     template <class SensorType>
AddSensorSensors93     void AddSensor() {
94         std::shared_ptr<SensorType> sensor =
95                 std::make_shared<SensorType>(mNextHandle++ /* sensorHandle */, this /* callback */);
96         mSensors[sensor->getSensorInfo().sensorHandle] = sensor;
97     }
98 
99     /**
100      * Utility function to delete the Event Flag
101      */
102     void deleteEventFlag();
103 
104     /**
105      * Function to read the Wake Lock FMQ and release the wake lock when appropriate
106      */
107     void readWakeLockFMQ();
108 
109     static void startReadWakeLockThread(Sensors* sensors);
110 
111     /**
112      * Responsible for acquiring and releasing a wake lock when there are unhandled WAKE_UP events
113      */
114     void updateWakeLock(int32_t eventsWritten, int32_t eventsHandled);
115 
116     using EventMessageQueue = MessageQueue<Event, kSynchronizedReadWrite>;
117     using WakeLockMessageQueue = MessageQueue<uint32_t, kSynchronizedReadWrite>;
118 
119     /**
120      * The Event FMQ where sensor events are written
121      */
122     std::unique_ptr<EventMessageQueue> mEventQueue;
123 
124     /**
125      * The Wake Lock FMQ that is read to determine when the framework has handled WAKE_UP events
126      */
127     std::unique_ptr<WakeLockMessageQueue> mWakeLockQueue;
128 
129     /**
130      * Event Flag to signal to the framework when sensor events are available to be read
131      */
132     EventFlag* mEventQueueFlag;
133 
134     /**
135      * Callback for asynchronous events, such as dynamic sensor connections.
136      */
137     sp<ISensorsCallback> mCallback;
138 
139     /**
140      * A map of the available sensors
141      */
142     std::map<int32_t, std::shared_ptr<Sensor>> mSensors;
143 
144     /**
145      * The next available sensor handle
146      */
147     int32_t mNextHandle;
148 
149     /**
150      * Lock to protect writes to the FMQs
151      */
152     std::mutex mWriteLock;
153 
154     /**
155      * Lock to protect acquiring and releasing the wake lock
156      */
157     std::mutex mWakeLockLock;
158 
159     /**
160      * Track the number of WAKE_UP events that have not been handled by the framework
161      */
162     uint32_t mOutstandingWakeUpEvents;
163 
164     /**
165      * A thread to read the Wake Lock FMQ
166      */
167     std::thread mWakeLockThread;
168 
169     /**
170      * Flag to indicate that the Wake Lock Thread should continue to run
171      */
172     std::atomic_bool mReadWakeLockQueueRun;
173 
174     /**
175      * Track the time when the wake lock should automatically be released
176      */
177     int64_t mAutoReleaseWakeLockTime;
178 
179     /**
180      * Flag to indicate if a wake lock has been acquired
181      */
182     bool mHasWakeLock;
183 };
184 
185 }  // namespace implementation
186 }  // namespace V2_0
187 }  // namespace sensors
188 }  // namespace hardware
189 }  // namespace android
190 
191 #endif  // ANDROID_HARDWARE_SENSORS_V2_0_SENSORS_H
192