1 /*
2  * Copyright (C) 2021 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_AIDL_SENSOR_HAL_WRAPPER_H
18 #define ANDROID_AIDL_SENSOR_HAL_WRAPPER_H
19 
20 #include "ISensorHalWrapper.h"
21 
22 #include <aidl/android/hardware/sensors/ISensors.h>
23 #include <fmq/AidlMessageQueue.h>
24 #include <sensor/SensorEventQueue.h>
25 
26 namespace android {
27 
28 class AidlSensorHalWrapper : public ISensorHalWrapper {
29 public:
30     AidlSensorHalWrapper();
31 
~AidlSensorHalWrapper()32     ~AidlSensorHalWrapper() override {
33         if (mEventQueueFlag != nullptr) {
34             ::android::hardware::EventFlag::deleteEventFlag(&mEventQueueFlag);
35             mEventQueueFlag = nullptr;
36         }
37         if (mWakeLockQueueFlag != nullptr) {
38             ::android::hardware::EventFlag::deleteEventFlag(&mWakeLockQueueFlag);
39             mWakeLockQueueFlag = nullptr;
40         }
41     }
42 
43     virtual bool connect(SensorDeviceCallback *callback) override;
44 
45     virtual void prepareForReconnect() override;
46 
47     virtual bool supportsPolling() override;
48 
49     virtual bool supportsMessageQueues() override;
50 
51     virtual ssize_t poll(sensors_event_t *buffer, size_t count) override;
52 
53     virtual ssize_t pollFmq(sensors_event_t *buffer, size_t count) override;
54 
55     virtual std::vector<sensor_t> getSensorsList() override;
56 
57     virtual status_t setOperationMode(SensorService::Mode mode) override;
58 
59     virtual status_t activate(int32_t sensorHandle, bool enabled) override;
60 
61     virtual status_t batch(int32_t sensorHandle, int64_t samplingPeriodNs,
62                            int64_t maxReportLatencyNs) override;
63 
64     virtual status_t flush(int32_t sensorHandle) override;
65 
66     virtual status_t injectSensorData(const sensors_event_t *event) override;
67 
68     virtual status_t registerDirectChannel(const sensors_direct_mem_t *memory,
69                                            int32_t *channelHandle) override;
70 
71     virtual status_t unregisterDirectChannel(int32_t channelHandle) override;
72 
73     virtual status_t configureDirectChannel(int32_t sensorHandle, int32_t channelHandle,
74                                             const struct sensors_direct_cfg_t *config) override;
75 
76     virtual void writeWakeLockHandled(uint32_t count) override;
77 
78 private:
79     std::shared_ptr<aidl::android::hardware::sensors::ISensors> mSensors;
80     std::shared_ptr<::aidl::android::hardware::sensors::ISensorsCallback> mCallback;
81     std::unique_ptr<::android::AidlMessageQueue<::aidl::android::hardware::sensors::Event,
82                                                 SynchronizedReadWrite>>
83             mEventQueue;
84     std::unique_ptr<::android::AidlMessageQueue<int, SynchronizedReadWrite>> mWakeLockQueue;
85     ::android::hardware::EventFlag *mEventQueueFlag;
86     ::android::hardware::EventFlag *mWakeLockQueueFlag;
87     SensorDeviceCallback *mSensorDeviceCallback;
88     std::array<::aidl::android::hardware::sensors::Event,
89                ::android::SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT>
90             mEventBuffer;
91 
92     ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
93 };
94 
95 } // namespace android
96 
97 #endif // ANDROID_AIDL_SENSOR_HAL_WRAPPER_H
98