Home | History | Annotate | Download | only in sensorservicehidl
      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_FRAMEWORKS_SENSORSERVICE_V1_0_SENSORMANAGER_H
     18 #define ANDROID_FRAMEWORKS_SENSORSERVICE_V1_0_SENSORMANAGER_H
     19 
     20 #include <jni.h>
     21 
     22 #include <mutex>
     23 #include <thread>
     24 
     25 #include <android/frameworks/sensorservice/1.0/ISensorManager.h>
     26 #include <android/frameworks/sensorservice/1.0/types.h>
     27 #include <hidl/MQDescriptor.h>
     28 #include <hidl/Status.h>
     29 #include <sensor/SensorManager.h>
     30 #include <utils/Looper.h>
     31 
     32 namespace android {
     33 namespace frameworks {
     34 namespace sensorservice {
     35 namespace V1_0 {
     36 namespace implementation {
     37 
     38 using ::android::hardware::sensors::V1_0::SensorType;
     39 using ::android::hardware::hidl_handle;
     40 using ::android::hardware::hidl_memory;
     41 using ::android::hardware::Return;
     42 using ::android::sp;
     43 
     44 struct SensorManager final : public ISensorManager {
     45 
     46     SensorManager(JavaVM* vm);
     47     ~SensorManager();
     48 
     49     // Methods from ::android::frameworks::sensorservice::V1_0::ISensorManager follow.
     50     Return<void> getSensorList(getSensorList_cb _hidl_cb) override;
     51     Return<void> getDefaultSensor(SensorType type, getDefaultSensor_cb _hidl_cb) override;
     52     Return<void> createAshmemDirectChannel(const hidl_memory& mem, uint64_t size, createAshmemDirectChannel_cb _hidl_cb) override;
     53     Return<void> createGrallocDirectChannel(const hidl_handle& buffer, uint64_t size, createGrallocDirectChannel_cb _hidl_cb) override;
     54     Return<void> createEventQueue(const sp<IEventQueueCallback> &callback, createEventQueue_cb _hidl_cb);
     55 
     56 private:
     57     // Block until ::android::SensorManager is initialized.
     58     ::android::SensorManager& getInternalManager();
     59     sp<Looper> getLooper();
     60 
     61     std::mutex mInternalManagerMutex;
     62     ::android::SensorManager* mInternalManager = nullptr; // does not own
     63     sp<Looper> mLooper;
     64 
     65     volatile bool mStopThread;
     66     std::mutex mThreadMutex; //protects mPollThread
     67     std::thread mPollThread;
     68 
     69     JavaVM* mJavaVm;
     70 };
     71 
     72 }  // namespace implementation
     73 }  // namespace V1_0
     74 }  // namespace sensorservice
     75 }  // namespace frameworks
     76 }  // namespace android
     77 
     78 #endif  // ANDROID_FRAMEWORKS_SENSORSERVICE_V1_0_SENSORMANAGER_H
     79