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/BnAGnss.h>
20 #include <aidl/android/hardware/gnss/BnAGnssRil.h>
21 #include <aidl/android/hardware/gnss/BnGnss.h>
22 #include <aidl/android/hardware/gnss/BnGnssAntennaInfo.h>
23 #include <aidl/android/hardware/gnss/BnGnssBatching.h>
24 #include <aidl/android/hardware/gnss/BnGnssConfiguration.h>
25 #include <aidl/android/hardware/gnss/BnGnssDebug.h>
26 #include <aidl/android/hardware/gnss/BnGnssMeasurementInterface.h>
27 #include <aidl/android/hardware/gnss/BnGnssPowerIndication.h>
28 #include <aidl/android/hardware/gnss/BnGnssPsds.h>
29 #include <aidl/android/hardware/gnss/measurement_corrections/BnMeasurementCorrectionsInterface.h>
30 #include <aidl/android/hardware/gnss/visibility_control/BnGnssVisibilityControl.h>
31 #include <atomic>
32 #include <mutex>
33 #include <thread>
34 #include "GnssConfiguration.h"
35 #include "GnssMeasurementInterface.h"
36 #include "GnssPowerIndication.h"
37 #include "Utils.h"
38 
39 namespace aidl::android::hardware::gnss {
40 
41 class Gnss : public BnGnss {
42   public:
43     Gnss();
~Gnss()44     ~Gnss() { stop(); };
45     ndk::ScopedAStatus setCallback(const std::shared_ptr<IGnssCallback>& callback) override;
46     ndk::ScopedAStatus start() override;
47     ndk::ScopedAStatus stop() override;
48     ndk::ScopedAStatus close() override;
49 
50     ndk::ScopedAStatus injectTime(int64_t timeMs, int64_t timeReferenceMs,
51                                   int uncertaintyMs) override;
52     ndk::ScopedAStatus injectLocation(const GnssLocation& location) override;
53     ndk::ScopedAStatus injectBestLocation(const GnssLocation& location) override;
54     ndk::ScopedAStatus deleteAidingData(GnssAidingData aidingDataFlags) override;
55     ndk::ScopedAStatus setPositionMode(const PositionModeOptions& options) override;
56     ndk::ScopedAStatus startSvStatus() override;
57     ndk::ScopedAStatus stopSvStatus() override;
58     ndk::ScopedAStatus startNmea() override;
59     ndk::ScopedAStatus stopNmea() override;
60 
61     ndk::ScopedAStatus getExtensionPsds(std::shared_ptr<IGnssPsds>* iGnssPsds) override;
62     ndk::ScopedAStatus getExtensionGnssConfiguration(
63             std::shared_ptr<IGnssConfiguration>* iGnssConfiguration) override;
64     ndk::ScopedAStatus getExtensionGnssPowerIndication(
65             std::shared_ptr<IGnssPowerIndication>* iGnssPowerIndication) override;
66     ndk::ScopedAStatus getExtensionGnssMeasurement(
67             std::shared_ptr<IGnssMeasurementInterface>* iGnssMeasurement) override;
68     ndk::ScopedAStatus getExtensionGnssBatching(
69             std::shared_ptr<IGnssBatching>* iGnssBatching) override;
70     ndk::ScopedAStatus getExtensionGnssGeofence(
71             std::shared_ptr<IGnssGeofence>* iGnssGeofence) override;
72     ndk::ScopedAStatus getExtensionGnssNavigationMessage(
73             std::shared_ptr<IGnssNavigationMessageInterface>* iGnssNavigationMessage) override;
74     ndk::ScopedAStatus getExtensionAGnss(std::shared_ptr<IAGnss>* iAGnss) override;
75     ndk::ScopedAStatus getExtensionAGnssRil(std::shared_ptr<IAGnssRil>* iAGnssRil) override;
76     ndk::ScopedAStatus getExtensionGnssDebug(std::shared_ptr<IGnssDebug>* iGnssDebug) override;
77     ndk::ScopedAStatus getExtensionGnssVisibilityControl(
78             std::shared_ptr<android::hardware::gnss::visibility_control::IGnssVisibilityControl>*
79                     iGnssVisibilityControl) override;
80     ndk::ScopedAStatus getExtensionGnssAntennaInfo(
81             std::shared_ptr<IGnssAntennaInfo>* iGnssAntennaInfo) override;
82     ndk::ScopedAStatus getExtensionMeasurementCorrections(
83             std::shared_ptr<android::hardware::gnss::measurement_corrections::
84                                     IMeasurementCorrectionsInterface>* iMeasurementCorrections)
85             override;
86 
87     void reportSvStatus() const;
88     void setGnssMeasurementEnabled(const bool enabled);
89     void setGnssMeasurementInterval(const long intervalMs);
90     std::shared_ptr<GnssLocation> getLastLocation() const;
91     std::shared_ptr<GnssConfiguration> mGnssConfiguration;
92     std::shared_ptr<GnssPowerIndication> mGnssPowerIndication;
93     std::shared_ptr<GnssMeasurementInterface> mGnssMeasurementInterface;
94 
95   private:
96     void reportLocation(const GnssLocation&);
97     void reportSvStatus(const std::vector<IGnssCallback::GnssSvInfo>& svInfoList) const;
98     void reportGnssStatusValue(const IGnssCallback::GnssStatusValue gnssStatusValue) const;
99     void reportNmea() const;
100     std::vector<IGnssCallback::GnssSvInfo> filterBlocklistedSatellites(
101             std::vector<IGnssCallback::GnssSvInfo> gnssSvInfoList) const;
102     std::unique_ptr<GnssLocation> getLocationFromHW();
103 
104     static std::shared_ptr<IGnssCallback> sGnssCallback;
105 
106     std::atomic<long> mMinIntervalMs;
107     std::atomic<long> mGnssMeasurementIntervalMs;
108     std::atomic<bool> mIsActive;
109     std::atomic<bool> mIsSvStatusActive;
110     std::atomic<bool> mIsNmeaActive;
111     std::atomic<bool> mFirstFixReceived;
112     std::atomic<bool> mGnssMeasurementEnabled;
113     std::shared_ptr<GnssLocation> mLastLocation;
114     std::thread mThread;
115     ::android::hardware::gnss::common::ThreadBlocker mThreadBlocker;
116 
117     mutable std::mutex mMutex;
118 };
119 
120 }  // namespace aidl::android::hardware::gnss
121