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 #include <chrono>
19 #include <memory>
20 #include <mutex>
21 #include <aidl/android/hardware/gnss/BnGnss.h>
22 #include "GnssBatching.h"
23 #include "GnssConfiguration.h"
24 #include "GnssHwConn.h"
25 #include "IDataSink.h"
26 
27 namespace aidl {
28 namespace android {
29 namespace hardware {
30 namespace gnss {
31 namespace implementation {
32 
33 using ::aidl::android::hardware::gnss::measurement_corrections::IMeasurementCorrectionsInterface;
34 using ::aidl::android::hardware::gnss::visibility_control::IGnssVisibilityControl;
35 
36 struct Gnss : public BnGnss, public IDataSink {
37     Gnss();
38     ~Gnss();
39 
40     ndk::ScopedAStatus setCallback(const std::shared_ptr<IGnssCallback>& callback) override;
41     ndk::ScopedAStatus close() override;
42 
43     ndk::ScopedAStatus getExtensionPsds(std::shared_ptr<IGnssPsds>* iGnssPsds) override;
44     ndk::ScopedAStatus getExtensionGnssConfiguration(
45             std::shared_ptr<IGnssConfiguration>* iGnssConfiguration) override;
46     ndk::ScopedAStatus getExtensionGnssMeasurement(
47             std::shared_ptr<IGnssMeasurementInterface>* iGnssMeasurement) override;
48     ndk::ScopedAStatus getExtensionGnssPowerIndication(
49             std::shared_ptr<IGnssPowerIndication>* iGnssPowerIndication) override;
50     ndk::ScopedAStatus getExtensionGnssBatching(
51             std::shared_ptr<IGnssBatching>* iGnssBatching) override;
52     ndk::ScopedAStatus getExtensionGnssGeofence(
53             std::shared_ptr<IGnssGeofence>* iGnssGeofence) override;
54     ndk::ScopedAStatus getExtensionGnssNavigationMessage(
55             std::shared_ptr<IGnssNavigationMessageInterface>* iGnssNavigationMessage) override;
56     ndk::ScopedAStatus getExtensionAGnss(std::shared_ptr<IAGnss>* iAGnss) override;
57     ndk::ScopedAStatus getExtensionAGnssRil(std::shared_ptr<IAGnssRil>* iAGnssRil) override;
58     ndk::ScopedAStatus getExtensionGnssDebug(std::shared_ptr<IGnssDebug>* iGnssDebug) override;
59     ndk::ScopedAStatus getExtensionGnssVisibilityControl(
60             std::shared_ptr<IGnssVisibilityControl>* iGnssVisibilityControl) override;
61 
62     ndk::ScopedAStatus start() override;
63     ndk::ScopedAStatus stop() override;
64 
65     ndk::ScopedAStatus injectTime(int64_t timeMs, int64_t timeReferenceMs,
66                                   int uncertaintyMs) override;
67     ndk::ScopedAStatus injectLocation(const GnssLocation& location) override;
68     ndk::ScopedAStatus injectBestLocation(const GnssLocation& location) override;
69     ndk::ScopedAStatus deleteAidingData(GnssAidingData aidingDataFlags) override;
70     ndk::ScopedAStatus setPositionMode(const PositionModeOptions& options) override;
71 
72     ndk::ScopedAStatus getExtensionGnssAntennaInfo(
73             std::shared_ptr<IGnssAntennaInfo>* iGnssAntennaInfo) override;
74     ndk::ScopedAStatus getExtensionMeasurementCorrections(
75             std::shared_ptr<IMeasurementCorrectionsInterface>* iMeasurementCorrections)
76             override;
77 
78     ndk::ScopedAStatus startSvStatus() override;
79     ndk::ScopedAStatus stopSvStatus() override;
80     ndk::ScopedAStatus startNmea() override;
81     ndk::ScopedAStatus stopNmea() override;
82 
83     void onGnssStatusCb(IGnssCallback::GnssStatusValue) override;
84     void onGnssSvStatusCb(std::vector<IGnssCallback::GnssSvInfo>) override;
85     void onGnssNmeaCb(int64_t timestampMs, std::string nmea) override;
86     void onGnssLocationCb(GnssLocation location) override;
87 
88 private:
89     enum class SessionState {
90         OFF, STARTING, STARTED, STOPPED
91     };
92 
93     using Clock = std::chrono::steady_clock;
94 
95     double getRunningTime() const;
96     double getRunningTimeLocked(Clock::time_point now) const;
97     bool isWarmedUpLocked(Clock::time_point now) const;
98 
99     const std::shared_ptr<GnssBatching> mGnssBatching;
100     const std::shared_ptr<GnssConfiguration> mGnssConfiguration;
101 
102     std::shared_ptr<IGnssCallback> mCallback;        // protected by mMtx
103     std::optional<Clock::time_point> mStartT;        // protected by mMtx
104     int mRecurrence = -1;                            // protected by mMtx
105     Clock::duration mMinInterval;                    // protected by mMtx
106     Clock::time_point mFirstFix;                     // protected by mMtx
107     Clock::time_point mLastFix;                      // protected by mMtx
108     SessionState mSessionState = SessionState::OFF;  // protected by mMtx
109     bool mLowPowerMode = false;                      // protected by mMtx
110     bool mSendSvStatus = false;                      // protected by mMtx
111     bool mSendNmea = false;                          // protected by mMtx
112     mutable std::mutex mMtx;
113 
114     std::unique_ptr<GnssHwConn> mGnssHwConn;
115 };
116 
117 }  // namespace implementation
118 }  // namespace gnss
119 }  // namespace hardware
120 }  // namespace android
121 }  // namespace aidl
122