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 #include <log/log.h>
18 
19 #include "gnss.h"
20 #include "gnss_configuration.h"
21 #include "gnss_measurement.h"
22 #include "agnss.h"
23 
24 namespace {
25 constexpr char kGnssDeviceName[] = "Android Studio Emulator GPS";
26 };
27 
28 namespace goldfish {
29 
getExtensionGnssConfiguration_2_0()30 Return<sp<ahg20::IGnssConfiguration>> Gnss20::getExtensionGnssConfiguration_2_0() {
31     return new GnssConfiguration20();
32 }
33 
getExtensionGnssDebug_2_0()34 Return<sp<ahg20::IGnssDebug>> Gnss20::getExtensionGnssDebug_2_0() {
35     return nullptr;
36 }
37 
getExtensionAGnss_2_0()38 Return<sp<ahg20::IAGnss>> Gnss20::getExtensionAGnss_2_0() {
39     return new AGnss20();
40 }
41 
getExtensionAGnssRil_2_0()42 Return<sp<ahg20::IAGnssRil>> Gnss20::getExtensionAGnssRil_2_0() {
43     return nullptr;
44 }
45 
getExtensionGnssMeasurement_2_0()46 Return<sp<ahg20::IGnssMeasurement>> Gnss20::getExtensionGnssMeasurement_2_0() {
47     return new GnssMeasurement20();
48 }
49 
setCallback_2_0(const sp<ahg20::IGnssCallback> & callback)50 Return<bool> Gnss20::setCallback_2_0(const sp<ahg20::IGnssCallback>& callback) {
51     if (callback == nullptr) {
52         return false;
53     } else if (open()) {
54         using Caps = ahg20::IGnssCallback::Capabilities;
55         callback->gnssSetCapabilitiesCb_2_0(Caps::MEASUREMENTS | 0);
56         callback->gnssNameCb(kGnssDeviceName);
57         callback->gnssSetSystemInfoCb({.yearOfHw = 2020});
58 
59         m_dataSink.setCallback20(callback);
60         return true;
61     } else {
62         return false;
63     }
64 }
65 
getExtensionMeasurementCorrections()66 Return<sp<ahgmc10::IMeasurementCorrections>> Gnss20::getExtensionMeasurementCorrections() {
67     return nullptr;
68 }
69 
getExtensionVisibilityControl()70 Return<sp<ahgvc10::IGnssVisibilityControl>> Gnss20::getExtensionVisibilityControl() {
71     return nullptr;
72 }
73 
getExtensionGnssBatching_2_0()74 Return<sp<ahg20::IGnssBatching>> Gnss20::getExtensionGnssBatching_2_0() {
75     return nullptr;
76 }
77 
injectBestLocation_2_0(const ahg20::GnssLocation & location)78 Return<bool> Gnss20::injectBestLocation_2_0(const ahg20::GnssLocation& location) {
79     (void)location;
80     return true;
81 }
82 
setPositionMode_1_1(ahg10::IGnss::GnssPositionMode mode,ahg10::IGnss::GnssPositionRecurrence recurrence,uint32_t minIntervalMs,uint32_t preferredAccuracyMeters,uint32_t preferredTimeMs,bool lowPowerMode)83 Return<bool> Gnss20::setPositionMode_1_1(ahg10::IGnss::GnssPositionMode mode,
84                                          ahg10::IGnss::GnssPositionRecurrence recurrence,
85                                          uint32_t minIntervalMs, uint32_t preferredAccuracyMeters,
86                                          uint32_t preferredTimeMs, bool lowPowerMode) {
87     (void)mode;
88     (void)recurrence;
89     (void)minIntervalMs;
90     (void)preferredAccuracyMeters;
91     (void)preferredTimeMs;
92     (void)lowPowerMode;
93     return true;
94 }
95 
start()96 Return<bool> Gnss20::start() {
97     std::unique_lock<std::mutex> lock(m_gnssHwConnMtx);
98     if (m_gnssHwConn) {
99         m_dataSink.start();
100         return m_gnssHwConn->start();
101     } else {
102         return false;
103     }
104 }
105 
stop()106 Return<bool> Gnss20::stop() {
107     std::unique_lock<std::mutex> lock(m_gnssHwConnMtx);
108     if (m_gnssHwConn) {
109         return m_gnssHwConn->stop();
110     } else {
111         return false;
112     }
113 }
114 
cleanup()115 Return<void> Gnss20::cleanup() {
116     {
117         std::unique_lock<std::mutex> lock(m_gnssHwConnMtx);
118         m_gnssHwConn.reset();
119     }
120 
121     m_dataSink.cleanup();
122 
123     return {};
124 }
125 
injectTime(int64_t timeMs,int64_t timeReferenceMs,int32_t uncertaintyMs)126 Return<bool> Gnss20::injectTime(int64_t timeMs, int64_t timeReferenceMs,
127                                 int32_t uncertaintyMs) {
128     (void)timeMs;
129     (void)timeReferenceMs;
130     (void)uncertaintyMs;
131     return true;
132 }
133 
injectLocation(double latitudeDegrees,double longitudeDegrees,float accuracyMeters)134 Return<bool> Gnss20::injectLocation(double latitudeDegrees, double longitudeDegrees,
135                                     float accuracyMeters) {
136     (void)latitudeDegrees;
137     (void)longitudeDegrees;
138     (void)accuracyMeters;
139     return false;
140 }
141 
deleteAidingData(ahg10::IGnss::GnssAidingData aidingDataFlags)142 Return<void> Gnss20::deleteAidingData(ahg10::IGnss::GnssAidingData aidingDataFlags) {
143     (void)aidingDataFlags;
144     return {};
145 }
146 
getExtensionGnssGeofencing()147 Return<sp<ahg10::IGnssGeofencing>> Gnss20::getExtensionGnssGeofencing() {
148     return nullptr;
149 }
150 
getExtensionGnssNavigationMessage()151 Return<sp<ahg10::IGnssNavigationMessage>> Gnss20::getExtensionGnssNavigationMessage() {
152     return nullptr;
153 }
154 
getExtensionXtra()155 Return<sp<ahg10::IGnssXtra>> Gnss20::getExtensionXtra() {
156     return nullptr;
157 }
158 
159 ////////////////////////////////////////////////////////////////////////////////
open()160 bool Gnss20::open() {
161     std::unique_lock<std::mutex> lock(m_gnssHwConnMtx);
162     if (m_gnssHwConn) {
163         return true;
164     } else {
165         auto conn = std::make_unique<GnssHwConn>(&m_dataSink);
166         if (conn->ok()) {
167             m_gnssHwConn = std::move(conn);
168             return true;
169         } else {
170             return false;
171         }
172     }
173 }
174 
175 //// deprecated and old versions ///////////////////////////////////////////////
setCallback_1_1(const sp<ahg11::IGnssCallback> &)176 Return<bool> Gnss20::setCallback_1_1(const sp<ahg11::IGnssCallback>&) {
177     return false;
178 }
179 
getExtensionGnssMeasurement_1_1()180 Return<sp<ahg11::IGnssMeasurement>> Gnss20::getExtensionGnssMeasurement_1_1() {
181     return nullptr;
182 }
183 
getExtensionGnssConfiguration_1_1()184 Return<sp<ahg11::IGnssConfiguration>> Gnss20::getExtensionGnssConfiguration_1_1() {
185     return nullptr;
186 }
187 
setCallback(const sp<ahg10::IGnssCallback> &)188 Return<bool> Gnss20::setCallback(const sp<ahg10::IGnssCallback>&) {
189     return false;
190 }
191 
getExtensionGnssMeasurement()192 Return<sp<ahg10::IGnssMeasurement>> Gnss20::getExtensionGnssMeasurement() {
193     return nullptr;
194 }
195 
getExtensionAGnss()196 Return<sp<ahg10::IAGnss>> Gnss20::getExtensionAGnss() {
197     return nullptr;
198 }
199 
getExtensionGnssNi()200 Return<sp<ahg10::IGnssNi>> Gnss20::getExtensionGnssNi() {
201     return nullptr;
202 }
203 
getExtensionGnssDebug()204 Return<sp<ahg10::IGnssDebug>> Gnss20::getExtensionGnssDebug() {
205     return nullptr;
206 }
207 
getExtensionGnssBatching()208 Return<sp<ahg10::IGnssBatching>> Gnss20::getExtensionGnssBatching() {
209     return nullptr;
210 }
211 
injectBestLocation(const ahg10::GnssLocation &)212 Return<bool> Gnss20::injectBestLocation(const ahg10::GnssLocation&) {
213     return false;
214 }
215 
setPositionMode(ahg10::IGnss::GnssPositionMode,ahg10::IGnss::GnssPositionRecurrence,uint32_t,uint32_t,uint32_t)216 Return<bool> Gnss20::setPositionMode(ahg10::IGnss::GnssPositionMode,
217                                      ahg10::IGnss::GnssPositionRecurrence,
218                                      uint32_t, uint32_t, uint32_t) {
219     return false;
220 }
221 
getExtensionGnssConfiguration()222 Return<sp<ahg10::IGnssConfiguration>> Gnss20::getExtensionGnssConfiguration() {
223     return nullptr;
224 }
225 
getExtensionAGnssRil()226 Return<sp<ahg10::IAGnssRil>> Gnss20::getExtensionAGnssRil() {
227     return nullptr;
228 }
229 
230 }  // namespace goldfish
231