1 /*
2  * Copyright (C) 2022 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 #pragma once
18 
19 #include <aidl/android/frameworks/sensorservice/BnSensorManager.h>
20 #include <jni.h>
21 #include <sensor/SensorManager.h>
22 #include <utils/Looper.h>
23 #include <mutex>
24 #include <thread>
25 
26 namespace android {
27 namespace frameworks {
28 namespace sensorservice {
29 namespace implementation {
30 
31 class SensorManagerAidl : public ::aidl::android::frameworks::sensorservice::BnSensorManager {
32 public:
33     explicit SensorManagerAidl(JavaVM* vm);
34     ~SensorManagerAidl();
35 
36     ::ndk::ScopedAStatus createAshmemDirectChannel(
37             const ::aidl::android::hardware::common::Ashmem& in_mem, int64_t in_size,
38             std::shared_ptr<::aidl::android::frameworks::sensorservice::IDirectReportChannel>*
39                     _aidl_return) override;
40     ::ndk::ScopedAStatus createEventQueue(
41             const std::shared_ptr<::aidl::android::frameworks::sensorservice::IEventQueueCallback>&
42                     in_callback,
43             std::shared_ptr<::aidl::android::frameworks::sensorservice::IEventQueue>* _aidl_return)
44             override;
45     ::ndk::ScopedAStatus createGrallocDirectChannel(
46             const ::ndk::ScopedFileDescriptor& in_buffer, int64_t in_size,
47             std::shared_ptr<::aidl::android::frameworks::sensorservice::IDirectReportChannel>*
48                     _aidl_return) override;
49     ::ndk::ScopedAStatus getDefaultSensor(
50             ::aidl::android::hardware::sensors::SensorType in_type,
51             ::aidl::android::hardware::sensors::SensorInfo* _aidl_return) override;
52     ::ndk::ScopedAStatus getSensorList(
53             std::vector<::aidl::android::hardware::sensors::SensorInfo>* _aidl_return) override;
54 
55 private:
56     // Block until ::android::SensorManager is initialized.
57     ::android::SensorManager& getInternalManager();
58     sp<Looper> getLooper();
59 
60     sp<Looper> mLooper;
61 
62     volatile bool mStopThread;
63     std::mutex mThreadMutex; // protects mPollThread
64     std::thread mPollThread;
65 
66     JavaVM* mJavaVm;
67 };
68 
69 } // namespace implementation
70 } // namespace sensorservice
71 } // namespace frameworks
72 } // namespace android
73