Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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/hardware/gnss/BnGnssMeasurementCallback.h> |
| 20 | #include <aidl/android/hardware/gnss/BnGnssMeasurementInterface.h> |
| 21 | #include <atomic> |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 22 | #include <future> |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 23 | #include <mutex> |
| 24 | #include <thread> |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 25 | #include "Utils.h" |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 26 | |
| 27 | namespace aidl::android::hardware::gnss { |
Zhanghao | d32f378 | 2023-03-21 05:41:55 +0000 | [diff] [blame] | 28 | class Gnss; |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 29 | |
| 30 | struct GnssMeasurementInterface : public BnGnssMeasurementInterface { |
| 31 | public: |
| 32 | GnssMeasurementInterface(); |
| 33 | ~GnssMeasurementInterface(); |
| 34 | ndk::ScopedAStatus setCallback(const std::shared_ptr<IGnssMeasurementCallback>& callback, |
Shinru Han | 4abab50 | 2020-12-09 15:07:18 +0800 | [diff] [blame] | 35 | const bool enableFullTracking, |
| 36 | const bool enableCorrVecOutputs) override; |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 37 | ndk::ScopedAStatus close() override; |
Yu-Han Yang | 11c0c24 | 2021-12-15 11:22:09 -0800 | [diff] [blame] | 38 | ndk::ScopedAStatus setCallbackWithOptions( |
| 39 | const std::shared_ptr<IGnssMeasurementCallback>& callback, |
| 40 | const Options& options) override; |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 41 | void setLocationInterval(const int intervalMs); |
| 42 | void setLocationEnabled(const bool enabled); |
Zhanghao | d32f378 | 2023-03-21 05:41:55 +0000 | [diff] [blame] | 43 | void setGnssInterface(const std::shared_ptr<Gnss>& gnss); |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 44 | |
| 45 | private: |
Yu-Han Yang | c5d4f36 | 2022-11-04 22:55:32 +0000 | [diff] [blame] | 46 | void start(const bool enableCorrVecOutputs, const bool enableFullTracking); |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 47 | void stop(); |
| 48 | void reportMeasurement(const GnssData&); |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 49 | void waitForStoppingThreads(); |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 50 | |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 51 | std::atomic<long> mIntervalMs; |
| 52 | std::atomic<long> mLocationIntervalMs; |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 53 | std::atomic<bool> mIsActive; |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 54 | std::atomic<bool> mLocationEnabled; |
Yu-Han Yang | e28dd79 | 2023-10-15 17:47:23 +0000 | [diff] [blame] | 55 | std::vector<std::thread> mThreads; |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 56 | std::vector<std::future<void>> mFutures; |
| 57 | ::android::hardware::gnss::common::ThreadBlocker mThreadBlocker; |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 58 | |
| 59 | // Guarded by mMutex |
| 60 | static std::shared_ptr<IGnssMeasurementCallback> sCallback; |
| 61 | |
| 62 | // Synchronization lock for sCallback |
| 63 | mutable std::mutex mMutex; |
Zhanghao | d32f378 | 2023-03-21 05:41:55 +0000 | [diff] [blame] | 64 | |
| 65 | std::shared_ptr<Gnss> mGnss; |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | } // namespace aidl::android::hardware::gnss |