blob: 831f88402b3627dad026c31b7bad29ef4712d759 [file] [log] [blame]
Michael Bestas58e7cab2023-05-12 04:05:32 +03001/*
2 * Copyright (c) 2017-2020, The Linux Foundation. All rights reserved.
3 * Not a Contribution
4 */
5/*
6 * Copyright (C) 2016 The Android Open Source Project
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#define LOG_TAG "LocSvc_GnssInterface"
22#define LOG_NDEBUG 0
23
24#include <fstream>
25#include <log_util.h>
26#include <dlfcn.h>
27#include <cutils/properties.h>
28#include "Gnss.h"
29#include <LocationUtil.h>
30#include "battery_listener.h"
31#include "loc_misc_utils.h"
32
33typedef const GnssInterface* (getLocationInterface)();
34
35namespace android {
36namespace hardware {
37namespace gnss {
38namespace V1_0 {
39namespace implementation {
40
41static sp<Gnss> sGnss;
42void Gnss::GnssDeathRecipient::serviceDied(uint64_t cookie, const wp<IBase>& who) {
43 LOC_LOGE("%s] service died. cookie: %llu, who: %p",
44 __FUNCTION__, static_cast<unsigned long long>(cookie), &who);
45 if (mGnss != nullptr) {
Yingjie Wang044750d2020-09-04 11:51:54 +080046 mGnss->getGnssInterface()->resetNetworkInfo();
Michael Bestas58e7cab2023-05-12 04:05:32 +030047 mGnss->stop();
48 mGnss->cleanup();
49 }
50}
51
52void location_on_battery_status_changed(bool charging) {
53 LOC_LOGd("battery status changed to %s charging", charging ? "" : "not ");
54 if (sGnss != nullptr) {
55 sGnss->getGnssInterface()->updateBatteryStatus(charging);
56 }
57}
58Gnss::Gnss() {
59 ENTRY_LOG_CALLFLOW();
60 sGnss = this;
61 // initilize gnss interface at first in case needing notify battery status
62 sGnss->getGnssInterface()->initialize();
63 // register health client to listen on battery change
64 loc_extn_battery_properties_listener_init(location_on_battery_status_changed);
65 // clear pending GnssConfig
66 memset(&mPendingConfig, 0, sizeof(GnssConfig));
67
68 mGnssDeathRecipient = new GnssDeathRecipient(this);
69}
70
71Gnss::~Gnss() {
72 ENTRY_LOG_CALLFLOW();
73 if (mApi != nullptr) {
74 mApi->destroy();
75 mApi = nullptr;
76 }
77 sGnss = nullptr;
78}
79
80GnssAPIClient* Gnss::getApi() {
81 if (mApi == nullptr && (mGnssCbIface != nullptr || mGnssNiCbIface != nullptr)) {
82 mApi = new GnssAPIClient(mGnssCbIface, mGnssNiCbIface);
83 if (mApi == nullptr) {
84 LOC_LOGE("%s] faild to create GnssAPIClient", __FUNCTION__);
85 return mApi;
86 }
87
88 if (mPendingConfig.size == sizeof(GnssConfig)) {
89 // we have pending GnssConfig
90 mApi->gnssConfigurationUpdate(mPendingConfig);
91 // clear size to invalid mPendingConfig
92 mPendingConfig.size = 0;
93 if (mPendingConfig.assistanceServer.hostName != nullptr) {
94 free((void*)mPendingConfig.assistanceServer.hostName);
95 }
96 }
97 }
98 if (mApi == nullptr) {
99 LOC_LOGW("%s] GnssAPIClient is not ready", __FUNCTION__);
100 }
101 return mApi;
102}
103
104const GnssInterface* Gnss::getGnssInterface() {
105 static bool getGnssInterfaceFailed = false;
106 if (nullptr == mGnssInterface && !getGnssInterfaceFailed) {
107 void * libHandle = nullptr;
108 getLocationInterface* getter = (getLocationInterface*)
109 dlGetSymFromLib(libHandle, "libgnss.so", "getGnssInterface");
110
111 if (nullptr == getter) {
112 getGnssInterfaceFailed = true;
113 } else {
114 mGnssInterface = (GnssInterface*)(*getter)();
115 }
116 }
117 return mGnssInterface;
118}
119
120Return<bool> Gnss::setCallback(const sp<V1_0::IGnssCallback>& callback) {
121 ENTRY_LOG_CALLFLOW();
122 if (mGnssCbIface != nullptr) {
123 mGnssCbIface->unlinkToDeath(mGnssDeathRecipient);
124 }
125 mGnssCbIface = callback;
126 if (mGnssCbIface != nullptr) {
127 mGnssCbIface->linkToDeath(mGnssDeathRecipient, 0 /*cookie*/);
128 }
129
130 GnssAPIClient* api = getApi();
131 if (api != nullptr) {
132 api->gnssUpdateCallbacks(mGnssCbIface, mGnssNiCbIface);
133 api->gnssEnable(LOCATION_TECHNOLOGY_TYPE_GNSS);
134 api->requestCapabilities();
135 }
136 return true;
137}
138
139Return<bool> Gnss::setGnssNiCb(const sp<IGnssNiCallback>& callback) {
140 ENTRY_LOG_CALLFLOW();
141 mGnssNiCbIface = callback;
142 GnssAPIClient* api = getApi();
143 if (api != nullptr) {
144 api->gnssUpdateCallbacks(mGnssCbIface, mGnssNiCbIface);
145 }
146 return true;
147}
148
149Return<bool> Gnss::updateConfiguration(GnssConfig& gnssConfig) {
150 ENTRY_LOG_CALLFLOW();
151 GnssAPIClient* api = getApi();
152 if (api) {
153 api->gnssConfigurationUpdate(gnssConfig);
154 } else if (gnssConfig.flags != 0) {
155 // api is not ready yet, update mPendingConfig with gnssConfig
156 mPendingConfig.size = sizeof(GnssConfig);
157
158 if (gnssConfig.flags & GNSS_CONFIG_FLAGS_GPS_LOCK_VALID_BIT) {
159 mPendingConfig.flags |= GNSS_CONFIG_FLAGS_GPS_LOCK_VALID_BIT;
160 mPendingConfig.gpsLock = gnssConfig.gpsLock;
161 }
162 if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SUPL_VERSION_VALID_BIT) {
163 mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SUPL_VERSION_VALID_BIT;
164 mPendingConfig.suplVersion = gnssConfig.suplVersion;
165 }
166 if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SET_ASSISTANCE_DATA_VALID_BIT) {
167 mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SET_ASSISTANCE_DATA_VALID_BIT;
168 mPendingConfig.assistanceServer.size = sizeof(GnssConfigSetAssistanceServer);
169 mPendingConfig.assistanceServer.type = gnssConfig.assistanceServer.type;
170 if (mPendingConfig.assistanceServer.hostName != nullptr) {
171 free((void*)mPendingConfig.assistanceServer.hostName);
172 mPendingConfig.assistanceServer.hostName =
173 strdup(gnssConfig.assistanceServer.hostName);
174 }
175 mPendingConfig.assistanceServer.port = gnssConfig.assistanceServer.port;
176 }
177 if (gnssConfig.flags & GNSS_CONFIG_FLAGS_LPP_PROFILE_VALID_BIT) {
178 mPendingConfig.flags |= GNSS_CONFIG_FLAGS_LPP_PROFILE_VALID_BIT;
179 mPendingConfig.lppProfile = gnssConfig.lppProfile;
180 }
181 if (gnssConfig.flags & GNSS_CONFIG_FLAGS_LPPE_CONTROL_PLANE_VALID_BIT) {
182 mPendingConfig.flags |= GNSS_CONFIG_FLAGS_LPPE_CONTROL_PLANE_VALID_BIT;
183 mPendingConfig.lppeControlPlaneMask = gnssConfig.lppeControlPlaneMask;
184 }
185 if (gnssConfig.flags & GNSS_CONFIG_FLAGS_LPPE_USER_PLANE_VALID_BIT) {
186 mPendingConfig.flags |= GNSS_CONFIG_FLAGS_LPPE_USER_PLANE_VALID_BIT;
187 mPendingConfig.lppeUserPlaneMask = gnssConfig.lppeUserPlaneMask;
188 }
189 if (gnssConfig.flags & GNSS_CONFIG_FLAGS_AGLONASS_POSITION_PROTOCOL_VALID_BIT) {
190 mPendingConfig.flags |= GNSS_CONFIG_FLAGS_AGLONASS_POSITION_PROTOCOL_VALID_BIT;
191 mPendingConfig.aGlonassPositionProtocolMask = gnssConfig.aGlonassPositionProtocolMask;
192 }
193 if (gnssConfig.flags & GNSS_CONFIG_FLAGS_EM_PDN_FOR_EM_SUPL_VALID_BIT) {
194 mPendingConfig.flags |= GNSS_CONFIG_FLAGS_EM_PDN_FOR_EM_SUPL_VALID_BIT;
195 mPendingConfig.emergencyPdnForEmergencySupl = gnssConfig.emergencyPdnForEmergencySupl;
196 }
197 if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SUPL_EM_SERVICES_BIT) {
198 mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SUPL_EM_SERVICES_BIT;
199 mPendingConfig.suplEmergencyServices = gnssConfig.suplEmergencyServices;
200 }
201 if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SUPL_MODE_BIT) {
202 mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SUPL_MODE_BIT;
203 mPendingConfig.suplModeMask = gnssConfig.suplModeMask;
204 }
205 if (gnssConfig.flags & GNSS_CONFIG_FLAGS_BLACKLISTED_SV_IDS_BIT) {
206 mPendingConfig.flags |= GNSS_CONFIG_FLAGS_BLACKLISTED_SV_IDS_BIT;
207 mPendingConfig.blacklistedSvIds = gnssConfig.blacklistedSvIds;
208 }
209 }
210 return true;
211}
212
213Return<bool> Gnss::start() {
214 ENTRY_LOG_CALLFLOW();
215 bool retVal = false;
216 GnssAPIClient* api = getApi();
217 if (api) {
218 retVal = api->gnssStart();
219 }
220 return retVal;
221}
222
223Return<bool> Gnss::stop() {
224 ENTRY_LOG_CALLFLOW();
225 bool retVal = false;
226 GnssAPIClient* api = getApi();
227 if (api) {
228 retVal = api->gnssStop();
229 }
230 return retVal;
231}
232
233Return<void> Gnss::cleanup() {
234 ENTRY_LOG_CALLFLOW();
235
236 if (mApi != nullptr) {
237 mApi->gnssDisable();
238 }
239
240 return Void();
241}
242
243Return<bool> Gnss::injectLocation(double latitudeDegrees,
244 double longitudeDegrees,
245 float accuracyMeters) {
246 ENTRY_LOG_CALLFLOW();
247 const GnssInterface* gnssInterface = getGnssInterface();
248 if (nullptr != gnssInterface) {
249 gnssInterface->injectLocation(latitudeDegrees, longitudeDegrees, accuracyMeters);
250 return true;
251 } else {
252 return false;
253 }
254}
255
256Return<bool> Gnss::injectTime(int64_t timeMs, int64_t timeReferenceMs,
257 int32_t uncertaintyMs) {
258 ENTRY_LOG_CALLFLOW();
259 const GnssInterface* gnssInterface = getGnssInterface();
260 if ((nullptr != gnssInterface) && (gnssInterface->isSS5HWEnabled())) {
261 gnssInterface->injectTime(timeMs, timeReferenceMs, uncertaintyMs);
262 }
263 return true;
264}
265
266Return<void> Gnss::deleteAidingData(V1_0::IGnss::GnssAidingData aidingDataFlags) {
267 ENTRY_LOG_CALLFLOW();
268 GnssAPIClient* api = getApi();
269 if (api) {
270 api->gnssDeleteAidingData(aidingDataFlags);
271 }
272 return Void();
273}
274
275Return<bool> Gnss::setPositionMode(V1_0::IGnss::GnssPositionMode mode,
276 V1_0::IGnss::GnssPositionRecurrence recurrence,
277 uint32_t minIntervalMs,
278 uint32_t preferredAccuracyMeters,
279 uint32_t preferredTimeMs) {
280 ENTRY_LOG_CALLFLOW();
281 bool retVal = false;
282 GnssAPIClient* api = getApi();
283 if (api) {
284 retVal = api->gnssSetPositionMode(mode, recurrence, minIntervalMs,
285 preferredAccuracyMeters, preferredTimeMs);
286 }
287 return retVal;
288}
289
290Return<sp<V1_0::IAGnss>> Gnss::getExtensionAGnss() {
291 ENTRY_LOG_CALLFLOW();
292 mAGnssIface = new AGnss(this);
293 return mAGnssIface;
294}
295
296Return<sp<V1_0::IGnssNi>> Gnss::getExtensionGnssNi() {
297 ENTRY_LOG_CALLFLOW();
298 mGnssNi = new GnssNi(this);
299 return mGnssNi;
300}
301
302Return<sp<V1_0::IGnssMeasurement>> Gnss::getExtensionGnssMeasurement() {
303 ENTRY_LOG_CALLFLOW();
304 if (mGnssMeasurement == nullptr)
305 mGnssMeasurement = new GnssMeasurement();
306 return mGnssMeasurement;
307}
308
309Return<sp<V1_0::IGnssConfiguration>> Gnss::getExtensionGnssConfiguration() {
310 ENTRY_LOG_CALLFLOW();
311 mGnssConfig = new GnssConfiguration(this);
312 return mGnssConfig;
313}
314
315Return<sp<V1_0::IGnssGeofencing>> Gnss::getExtensionGnssGeofencing() {
316 ENTRY_LOG_CALLFLOW();
317 mGnssGeofencingIface = new GnssGeofencing();
318 return mGnssGeofencingIface;
319}
320
321Return<sp<V1_0::IGnssBatching>> Gnss::getExtensionGnssBatching() {
322 mGnssBatching = new GnssBatching();
323 return mGnssBatching;
324}
325
326Return<sp<V1_0::IGnssDebug>> Gnss::getExtensionGnssDebug() {
327 ENTRY_LOG_CALLFLOW();
328 mGnssDebug = new GnssDebug(this);
329 return mGnssDebug;
330}
331
332Return<sp<V1_0::IAGnssRil>> Gnss::getExtensionAGnssRil() {
333 mGnssRil = new AGnssRil(this);
334 return mGnssRil;
335}
336
337IGnss* HIDL_FETCH_IGnss(const char* hal) {
338 ENTRY_LOG_CALLFLOW();
339 IGnss* iface = nullptr;
340 iface = new Gnss();
341 if (iface == nullptr) {
342 LOC_LOGE("%s]: failed to get %s", __FUNCTION__, hal);
343 }
344 return iface;
345}
346
347} // namespace implementation
348} // namespace V1_0
349} // namespace gnss
350} // namespace hardware
351} // namespace android