blob: d2737e5206b2390848cc221c14f56dc15e36d0fc [file] [log] [blame]
Yu-Han Yang04832302020-11-20 09:51:18 -08001/*
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 Yang19a32b62022-04-27 09:57:01 -070022#include <future>
Yu-Han Yang04832302020-11-20 09:51:18 -080023#include <mutex>
24#include <thread>
Yu-Han Yang19a32b62022-04-27 09:57:01 -070025#include "Utils.h"
Yu-Han Yang04832302020-11-20 09:51:18 -080026
27namespace aidl::android::hardware::gnss {
28
29struct GnssMeasurementInterface : public BnGnssMeasurementInterface {
30 public:
31 GnssMeasurementInterface();
32 ~GnssMeasurementInterface();
33 ndk::ScopedAStatus setCallback(const std::shared_ptr<IGnssMeasurementCallback>& callback,
Shinru Han4abab502020-12-09 15:07:18 +080034 const bool enableFullTracking,
35 const bool enableCorrVecOutputs) override;
Yu-Han Yang04832302020-11-20 09:51:18 -080036 ndk::ScopedAStatus close() override;
Yu-Han Yang11c0c242021-12-15 11:22:09 -080037 ndk::ScopedAStatus setCallbackWithOptions(
38 const std::shared_ptr<IGnssMeasurementCallback>& callback,
39 const Options& options) override;
Yu-Han Yang19a32b62022-04-27 09:57:01 -070040 void setLocationInterval(const int intervalMs);
41 void setLocationEnabled(const bool enabled);
Yu-Han Yang04832302020-11-20 09:51:18 -080042
43 private:
Yu-Han Yangc5d4f362022-11-04 22:55:32 +000044 void start(const bool enableCorrVecOutputs, const bool enableFullTracking);
Yu-Han Yang04832302020-11-20 09:51:18 -080045 void stop();
46 void reportMeasurement(const GnssData&);
Yu-Han Yang19a32b62022-04-27 09:57:01 -070047 void waitForStoppingThreads();
Yu-Han Yang04832302020-11-20 09:51:18 -080048
Yu-Han Yang19a32b62022-04-27 09:57:01 -070049 std::atomic<long> mIntervalMs;
50 std::atomic<long> mLocationIntervalMs;
Yu-Han Yang04832302020-11-20 09:51:18 -080051 std::atomic<bool> mIsActive;
Yu-Han Yang19a32b62022-04-27 09:57:01 -070052 std::atomic<bool> mLocationEnabled;
Yu-Han Yang04832302020-11-20 09:51:18 -080053 std::thread mThread;
Yu-Han Yang19a32b62022-04-27 09:57:01 -070054 std::vector<std::future<void>> mFutures;
55 ::android::hardware::gnss::common::ThreadBlocker mThreadBlocker;
Yu-Han Yang04832302020-11-20 09:51:18 -080056
57 // Guarded by mMutex
58 static std::shared_ptr<IGnssMeasurementCallback> sCallback;
59
60 // Synchronization lock for sCallback
61 mutable std::mutex mMutex;
62};
63
64} // namespace aidl::android::hardware::gnss