• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
33 typedef const GnssInterface* (getLocationInterface)();
34 
35 #define IMAGES_INFO_FILE "/sys/devices/soc0/images"
36 #define DELIMITER ";"
37 
38 namespace android {
39 namespace hardware {
40 namespace gnss {
41 namespace V2_1 {
42 namespace implementation {
43 
44 using ::android::hardware::gnss::visibility_control::V1_0::implementation::GnssVisibilityControl;
45 using ::android::hardware::gnss::measurement_corrections::V1_1::
46         implementation::MeasurementCorrections;
47 static sp<Gnss> sGnss;
getVersionString()48 static std::string getVersionString() {
49     static std::string version;
50     if (!version.empty())
51         return version;
52 
53     char value[PROPERTY_VALUE_MAX] = {0};
54     property_get("ro.hardware", value, "unknown");
55     version.append(value).append(DELIMITER);
56 
57     std::ifstream in(IMAGES_INFO_FILE);
58     std::string s;
59     while(getline(in, s)) {
60         std::size_t found = s.find("CRM:");
61         if (std::string::npos == found) {
62             continue;
63         }
64 
65         // skip over space characters after "CRM:"
66         const char* substr = s.c_str();
67         found += 4;
68         while (0 != substr[found] && isspace(substr[found])) {
69             found++;
70         }
71         if (s.find("11:") != found) {
72             continue;
73         }
74         s.erase(0, found + 3);
75 
76         found = s.find_first_of("\r\n");
77         if (std::string::npos != found) {
78             s.erase(s.begin() + found, s.end());
79         }
80         version.append(s).append(DELIMITER);
81     }
82     return version;
83 }
84 
serviceDied(uint64_t cookie,const wp<IBase> & who)85 void Gnss::GnssDeathRecipient::serviceDied(uint64_t cookie, const wp<IBase>& who) {
86     LOC_LOGE("%s] service died. cookie: %llu, who: %p",
87             __FUNCTION__, static_cast<unsigned long long>(cookie), &who);
88     if (mGnss != nullptr) {
89         mGnss->cleanup();
90     }
91 }
92 
location_on_battery_status_changed(bool charging)93 void location_on_battery_status_changed(bool charging) {
94     LOC_LOGd("battery status changed to %s charging", charging ? "" : "not");
95     if (sGnss != nullptr) {
96         sGnss->getGnssInterface()->updateBatteryStatus(charging);
97     }
98 }
Gnss()99 Gnss::Gnss() {
100     ENTRY_LOG_CALLFLOW();
101     sGnss = this;
102     // initilize gnss interface at first in case needing notify battery status
103     sGnss->getGnssInterface()->initialize();
104     // register health client to listen on battery change
105     loc_extn_battery_properties_listener_init(location_on_battery_status_changed);
106     // clear pending GnssConfig
107     memset(&mPendingConfig, 0, sizeof(GnssConfig));
108     mGnssDeathRecipient = new GnssDeathRecipient(this);
109 }
110 
~Gnss()111 Gnss::~Gnss() {
112     ENTRY_LOG_CALLFLOW();
113     if (mApi != nullptr) {
114         delete mApi;
115         mApi = nullptr;
116     }
117     sGnss = nullptr;
118 }
119 
getApi()120 GnssAPIClient* Gnss::getApi() {
121     if (mApi != nullptr) {
122         return mApi;
123     }
124 
125     if (mGnssCbIface_2_1 != nullptr) {
126         mApi = new GnssAPIClient(mGnssCbIface_2_1);
127     } else if (mGnssCbIface_2_0 != nullptr) {
128         mApi = new GnssAPIClient(mGnssCbIface_2_0);
129     } else if (mGnssCbIface_1_1 != nullptr) {
130         mApi = new GnssAPIClient(mGnssCbIface_1_1, mGnssNiCbIface);
131     } else if (mGnssCbIface != nullptr) {
132         mApi = new GnssAPIClient(mGnssCbIface, mGnssNiCbIface);
133     } else {
134         LOC_LOGW("%s] GnssAPIClient is not ready", __FUNCTION__);
135         return mApi;
136     }
137 
138     if (mPendingConfig.size == sizeof(GnssConfig)) {
139         // we have pending GnssConfig
140         mApi->gnssConfigurationUpdate(mPendingConfig);
141         // clear size to invalid mPendingConfig
142         mPendingConfig.size = 0;
143         if (mPendingConfig.assistanceServer.hostName != nullptr) {
144             free((void*)mPendingConfig.assistanceServer.hostName);
145         }
146     }
147 
148     return mApi;
149 }
150 
getGnssInterface()151 const GnssInterface* Gnss::getGnssInterface() {
152     static bool getGnssInterfaceFailed = false;
153     if (mGnssInterface == nullptr && !getGnssInterfaceFailed) {
154         void * libHandle = nullptr;
155         getLocationInterface* getter = (getLocationInterface*)
156                 dlGetSymFromLib(libHandle, "libgnss.so", "getGnssInterface");
157         if (NULL == getter) {
158             getGnssInterfaceFailed = true;
159         } else {
160             mGnssInterface = (GnssInterface*)(*getter)();
161         }
162     }
163     return mGnssInterface;
164 }
165 
setCallback(const sp<V1_0::IGnssCallback> & callback)166 Return<bool> Gnss::setCallback(const sp<V1_0::IGnssCallback>& callback)  {
167     ENTRY_LOG_CALLFLOW();
168 
169     // In case where previous call to setCallback_1_1/setCallback_2_0/setCallback_2_1, then
170     // we need to cleanup these interfaces/callbacks here since we no longer
171     // do so in cleanup() function to keep callbacks around after cleanup()
172     if (mApi != nullptr) {
173         mApi->gnssUpdateCallbacks_2_0(nullptr);
174         mApi->gnssUpdateCallbacks_2_1(nullptr);
175     }
176     if (mGnssCbIface_1_1 != nullptr) {
177         mGnssCbIface_1_1->unlinkToDeath(mGnssDeathRecipient);
178         mGnssCbIface_1_1 = nullptr;
179     }
180     if (mGnssCbIface_2_0 != nullptr) {
181         mGnssCbIface_2_0->unlinkToDeath(mGnssDeathRecipient);
182         mGnssCbIface_2_0 = nullptr;
183     }
184     if (mGnssCbIface_2_1 != nullptr) {
185         mGnssCbIface_2_1->unlinkToDeath(mGnssDeathRecipient);
186         mGnssCbIface_2_1 = nullptr;
187     }
188 
189 
190     if (mGnssCbIface != nullptr) {
191         mGnssCbIface->unlinkToDeath(mGnssDeathRecipient);
192     }
193     mGnssCbIface = callback;
194     if (mGnssCbIface != nullptr) {
195         mGnssCbIface->linkToDeath(mGnssDeathRecipient, 0 /*cookie*/);
196     }
197 
198     GnssAPIClient* api = getApi();
199     if (api != nullptr) {
200         api->gnssUpdateCallbacks(mGnssCbIface, mGnssNiCbIface);
201         api->gnssEnable(LOCATION_TECHNOLOGY_TYPE_GNSS);
202         api->requestCapabilities();
203     }
204     return true;
205 }
206 
setGnssNiCb(const sp<IGnssNiCallback> & callback)207 Return<bool> Gnss::setGnssNiCb(const sp<IGnssNiCallback>& callback) {
208     ENTRY_LOG_CALLFLOW();
209     mGnssNiCbIface = callback;
210     GnssAPIClient* api = getApi();
211     if (api != nullptr) {
212         api->gnssUpdateCallbacks(mGnssCbIface, mGnssNiCbIface);
213     }
214     return true;
215 }
216 
updateConfiguration(GnssConfig & gnssConfig)217 Return<bool> Gnss::updateConfiguration(GnssConfig& gnssConfig) {
218     ENTRY_LOG_CALLFLOW();
219     GnssAPIClient* api = getApi();
220     if (api) {
221         api->gnssConfigurationUpdate(gnssConfig);
222     } else if (gnssConfig.flags != 0) {
223         // api is not ready yet, update mPendingConfig with gnssConfig
224         mPendingConfig.size = sizeof(GnssConfig);
225 
226         if (gnssConfig.flags & GNSS_CONFIG_FLAGS_GPS_LOCK_VALID_BIT) {
227             mPendingConfig.flags |= GNSS_CONFIG_FLAGS_GPS_LOCK_VALID_BIT;
228             mPendingConfig.gpsLock = gnssConfig.gpsLock;
229         }
230         if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SUPL_VERSION_VALID_BIT) {
231             mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SUPL_VERSION_VALID_BIT;
232             mPendingConfig.suplVersion = gnssConfig.suplVersion;
233         }
234         if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SET_ASSISTANCE_DATA_VALID_BIT) {
235             mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SET_ASSISTANCE_DATA_VALID_BIT;
236             mPendingConfig.assistanceServer.size = sizeof(GnssConfigSetAssistanceServer);
237             mPendingConfig.assistanceServer.type = gnssConfig.assistanceServer.type;
238             if (mPendingConfig.assistanceServer.hostName != nullptr) {
239                 free((void*)mPendingConfig.assistanceServer.hostName);
240                 mPendingConfig.assistanceServer.hostName =
241                     strdup(gnssConfig.assistanceServer.hostName);
242             }
243             mPendingConfig.assistanceServer.port = gnssConfig.assistanceServer.port;
244         }
245         if (gnssConfig.flags & GNSS_CONFIG_FLAGS_LPP_PROFILE_VALID_BIT) {
246             mPendingConfig.flags |= GNSS_CONFIG_FLAGS_LPP_PROFILE_VALID_BIT;
247             mPendingConfig.lppProfile = gnssConfig.lppProfile;
248         }
249         if (gnssConfig.flags & GNSS_CONFIG_FLAGS_LPPE_CONTROL_PLANE_VALID_BIT) {
250             mPendingConfig.flags |= GNSS_CONFIG_FLAGS_LPPE_CONTROL_PLANE_VALID_BIT;
251             mPendingConfig.lppeControlPlaneMask = gnssConfig.lppeControlPlaneMask;
252         }
253         if (gnssConfig.flags & GNSS_CONFIG_FLAGS_LPPE_USER_PLANE_VALID_BIT) {
254             mPendingConfig.flags |= GNSS_CONFIG_FLAGS_LPPE_USER_PLANE_VALID_BIT;
255             mPendingConfig.lppeUserPlaneMask = gnssConfig.lppeUserPlaneMask;
256         }
257         if (gnssConfig.flags & GNSS_CONFIG_FLAGS_AGLONASS_POSITION_PROTOCOL_VALID_BIT) {
258             mPendingConfig.flags |= GNSS_CONFIG_FLAGS_AGLONASS_POSITION_PROTOCOL_VALID_BIT;
259             mPendingConfig.aGlonassPositionProtocolMask = gnssConfig.aGlonassPositionProtocolMask;
260         }
261         if (gnssConfig.flags & GNSS_CONFIG_FLAGS_EM_PDN_FOR_EM_SUPL_VALID_BIT) {
262             mPendingConfig.flags |= GNSS_CONFIG_FLAGS_EM_PDN_FOR_EM_SUPL_VALID_BIT;
263             mPendingConfig.emergencyPdnForEmergencySupl = gnssConfig.emergencyPdnForEmergencySupl;
264         }
265         if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SUPL_EM_SERVICES_BIT) {
266             mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SUPL_EM_SERVICES_BIT;
267             mPendingConfig.suplEmergencyServices = gnssConfig.suplEmergencyServices;
268         }
269         if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SUPL_MODE_BIT) {
270             mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SUPL_MODE_BIT;
271             mPendingConfig.suplModeMask = gnssConfig.suplModeMask;
272         }
273         if (gnssConfig.flags & GNSS_CONFIG_FLAGS_BLACKLISTED_SV_IDS_BIT) {
274             mPendingConfig.flags |= GNSS_CONFIG_FLAGS_BLACKLISTED_SV_IDS_BIT;
275             mPendingConfig.blacklistedSvIds = gnssConfig.blacklistedSvIds;
276         }
277         if (gnssConfig.flags & GNSS_CONFIG_FLAGS_EMERGENCY_EXTENSION_SECONDS_BIT) {
278             mPendingConfig.flags |= GNSS_CONFIG_FLAGS_EMERGENCY_EXTENSION_SECONDS_BIT;
279             mPendingConfig.emergencyExtensionSeconds = gnssConfig.emergencyExtensionSeconds;
280         }
281     }
282     return true;
283 }
284 
start()285 Return<bool> Gnss::start()  {
286     ENTRY_LOG_CALLFLOW();
287     bool retVal = false;
288     GnssAPIClient* api = getApi();
289     if (api) {
290         retVal = api->gnssStart();
291     }
292     return retVal;
293 }
294 
stop()295 Return<bool> Gnss::stop()  {
296     ENTRY_LOG_CALLFLOW();
297     bool retVal = false;
298     GnssAPIClient* api = getApi();
299     if (api) {
300         retVal = api->gnssStop();
301     }
302     return retVal;
303 }
304 
cleanup()305 Return<void> Gnss::cleanup()  {
306     ENTRY_LOG_CALLFLOW();
307 
308     if (mApi != nullptr) {
309         mApi->gnssStop();
310         mApi->gnssDisable();
311     }
312 
313     return Void();
314 }
315 
injectLocation(double latitudeDegrees,double longitudeDegrees,float accuracyMeters)316 Return<bool> Gnss::injectLocation(double latitudeDegrees,
317                                   double longitudeDegrees,
318                                   float accuracyMeters)  {
319     ENTRY_LOG_CALLFLOW();
320     const GnssInterface* gnssInterface = getGnssInterface();
321     if (nullptr != gnssInterface) {
322         gnssInterface->injectLocation(latitudeDegrees, longitudeDegrees, accuracyMeters);
323         return true;
324     } else {
325         return false;
326     }
327 }
328 
injectTime(int64_t timeMs,int64_t timeReferenceMs,int32_t uncertaintyMs)329 Return<bool> Gnss::injectTime(int64_t timeMs, int64_t timeReferenceMs,
330                               int32_t uncertaintyMs) {
331     return true;
332 }
333 
deleteAidingData(V1_0::IGnss::GnssAidingData aidingDataFlags)334 Return<void> Gnss::deleteAidingData(V1_0::IGnss::GnssAidingData aidingDataFlags)  {
335     ENTRY_LOG_CALLFLOW();
336     GnssAPIClient* api = getApi();
337     if (api) {
338         api->gnssDeleteAidingData(aidingDataFlags);
339     }
340     return Void();
341 }
342 
setPositionMode(V1_0::IGnss::GnssPositionMode mode,V1_0::IGnss::GnssPositionRecurrence recurrence,uint32_t minIntervalMs,uint32_t preferredAccuracyMeters,uint32_t preferredTimeMs)343 Return<bool> Gnss::setPositionMode(V1_0::IGnss::GnssPositionMode mode,
344                                    V1_0::IGnss::GnssPositionRecurrence recurrence,
345                                    uint32_t minIntervalMs,
346                                    uint32_t preferredAccuracyMeters,
347                                    uint32_t preferredTimeMs)  {
348     ENTRY_LOG_CALLFLOW();
349     bool retVal = false;
350     GnssAPIClient* api = getApi();
351     if (api) {
352         retVal = api->gnssSetPositionMode(mode, recurrence, minIntervalMs,
353                 preferredAccuracyMeters, preferredTimeMs);
354     }
355     return retVal;
356 }
357 
getExtensionAGnss()358 Return<sp<V1_0::IAGnss>> Gnss::getExtensionAGnss()  {
359     ENTRY_LOG_CALLFLOW();
360     // deprecated function. Must return nullptr to pass VTS
361     return nullptr;
362 }
363 
getExtensionGnssNi()364 Return<sp<V1_0::IGnssNi>> Gnss::getExtensionGnssNi()  {
365     ENTRY_LOG_CALLFLOW();
366     // deprecated function. Must return nullptr to pass VTS
367     return nullptr;
368 }
369 
getExtensionGnssMeasurement()370 Return<sp<V1_0::IGnssMeasurement>> Gnss::getExtensionGnssMeasurement() {
371     ENTRY_LOG_CALLFLOW();
372     if (mGnssMeasurement == nullptr) {
373         mGnssMeasurement = new GnssMeasurement();
374     }
375     return mGnssMeasurement;
376 }
377 
getExtensionGnssConfiguration()378 Return<sp<V1_0::IGnssConfiguration>> Gnss::getExtensionGnssConfiguration()  {
379     ENTRY_LOG_CALLFLOW();
380     if (mGnssConfig == nullptr) {
381         mGnssConfig = new GnssConfiguration(this);
382     }
383     return mGnssConfig;
384 }
385 
getExtensionGnssGeofencing()386 Return<sp<V1_0::IGnssGeofencing>> Gnss::getExtensionGnssGeofencing()  {
387     ENTRY_LOG_CALLFLOW();
388     if (mGnssGeofencingIface == nullptr) {
389         mGnssGeofencingIface = new GnssGeofencing();
390     }
391     return mGnssGeofencingIface;
392 }
393 
getExtensionGnssBatching()394 Return<sp<V1_0::IGnssBatching>> Gnss::getExtensionGnssBatching()  {
395     ENTRY_LOG_CALLFLOW();
396     if (mGnssBatching == nullptr) {
397         mGnssBatching = new GnssBatching();
398     }
399     return mGnssBatching;
400 }
401 
getExtensionGnssDebug()402 Return<sp<V1_0::IGnssDebug>> Gnss::getExtensionGnssDebug() {
403     ENTRY_LOG_CALLFLOW();
404     if (mGnssDebug == nullptr) {
405         mGnssDebug = new GnssDebug(this);
406     }
407     return mGnssDebug;
408 }
409 
getExtensionAGnssRil()410 Return<sp<V1_0::IAGnssRil>> Gnss::getExtensionAGnssRil() {
411     ENTRY_LOG_CALLFLOW();
412     if (mGnssRil == nullptr) {
413         mGnssRil = new AGnssRil(this);
414     }
415     return mGnssRil;
416 }
417 
418 // Methods from ::android::hardware::gnss::V1_1::IGnss follow.
setCallback_1_1(const sp<V1_1::IGnssCallback> & callback)419 Return<bool> Gnss::setCallback_1_1(const sp<V1_1::IGnssCallback>& callback) {
420     ENTRY_LOG_CALLFLOW();
421     auto r = callback->gnssNameCb(getVersionString());
422     if (!r.isOk()) {
423         LOC_LOGE("%s] Error from gnssNameCb description=%s",
424                 __func__, r.description().c_str());
425     }
426 
427     // In case where previous call to setCallback/setCallback_2_0/setCallback_2_1, then
428     // we need to cleanup these interfaces/callbacks here since we no longer
429     // do so in cleanup() function to keep callbacks around after cleanup()
430     if (mApi != nullptr) {
431         mApi->gnssUpdateCallbacks_2_0(nullptr);
432         mApi->gnssUpdateCallbacks_2_1(nullptr);
433     }
434     if (mGnssCbIface != nullptr) {
435         mGnssCbIface->unlinkToDeath(mGnssDeathRecipient);
436         mGnssCbIface = nullptr;
437     }
438     if (mGnssCbIface_2_0 != nullptr) {
439         mGnssCbIface_2_0->unlinkToDeath(mGnssDeathRecipient);
440         mGnssCbIface_2_0 = nullptr;
441     }
442     if (mGnssCbIface_2_1 != nullptr) {
443         mGnssCbIface_2_1->unlinkToDeath(mGnssDeathRecipient);
444         mGnssCbIface_2_1 = nullptr;
445     }
446 
447 
448     if (mGnssCbIface_1_1 != nullptr) {
449         mGnssCbIface_1_1->unlinkToDeath(mGnssDeathRecipient);
450     }
451     mGnssCbIface_1_1 = callback;
452     if (mGnssCbIface_1_1 != nullptr) {
453         mGnssCbIface_1_1->linkToDeath(mGnssDeathRecipient, 0 /*cookie*/);
454     }
455 
456     const GnssInterface* gnssInterface = getGnssInterface();
457     if (nullptr != gnssInterface) {
458         OdcpiRequestCallback cb = [this](const OdcpiRequestInfo& odcpiRequest) {
459             odcpiRequestCb(odcpiRequest);
460         };
461         gnssInterface->odcpiInit(cb);
462     }
463 
464     GnssAPIClient* api = getApi();
465     if (api != nullptr) {
466         api->gnssUpdateCallbacks(mGnssCbIface_1_1, mGnssNiCbIface);
467         api->gnssEnable(LOCATION_TECHNOLOGY_TYPE_GNSS);
468         api->requestCapabilities();
469     }
470 
471     return true;
472 }
473 
setPositionMode_1_1(V1_0::IGnss::GnssPositionMode mode,V1_0::IGnss::GnssPositionRecurrence recurrence,uint32_t minIntervalMs,uint32_t preferredAccuracyMeters,uint32_t preferredTimeMs,bool lowPowerMode)474 Return<bool> Gnss::setPositionMode_1_1(V1_0::IGnss::GnssPositionMode mode,
475         V1_0::IGnss::GnssPositionRecurrence recurrence,
476         uint32_t minIntervalMs,
477         uint32_t preferredAccuracyMeters,
478         uint32_t preferredTimeMs,
479         bool lowPowerMode) {
480     ENTRY_LOG_CALLFLOW();
481     bool retVal = false;
482     GnssAPIClient* api = getApi();
483     if (api) {
484         GnssPowerMode powerMode = lowPowerMode?
485                 GNSS_POWER_MODE_M4 : GNSS_POWER_MODE_M2;
486         retVal = api->gnssSetPositionMode(mode, recurrence, minIntervalMs,
487                 preferredAccuracyMeters, preferredTimeMs, powerMode, minIntervalMs);
488     }
489     return retVal;
490 }
491 
getExtensionGnssMeasurement_1_1()492 Return<sp<V1_1::IGnssMeasurement>> Gnss::getExtensionGnssMeasurement_1_1() {
493     ENTRY_LOG_CALLFLOW();
494 #ifdef GNSS_HIDL_LEGACY_MEASURMENTS
495     return nullptr;
496 #else
497     if (mGnssMeasurement == nullptr)
498         mGnssMeasurement = new GnssMeasurement();
499     return mGnssMeasurement;
500 #endif
501 }
502 
getExtensionGnssConfiguration_1_1()503 Return<sp<V1_1::IGnssConfiguration>> Gnss::getExtensionGnssConfiguration_1_1() {
504     ENTRY_LOG_CALLFLOW();
505     if (mGnssConfig == nullptr)
506         mGnssConfig = new GnssConfiguration(this);
507     return mGnssConfig;
508 }
509 
injectBestLocation(const GnssLocation & gnssLocation)510 Return<bool> Gnss::injectBestLocation(const GnssLocation& gnssLocation) {
511     ENTRY_LOG_CALLFLOW();
512     const GnssInterface* gnssInterface = getGnssInterface();
513     if (nullptr != gnssInterface) {
514         Location location = {};
515         convertGnssLocation(gnssLocation, location);
516         gnssInterface->odcpiInject(location);
517     }
518     return true;
519 }
520 
odcpiRequestCb(const OdcpiRequestInfo & request)521 void Gnss::odcpiRequestCb(const OdcpiRequestInfo& request) {
522     ENTRY_LOG_CALLFLOW();
523 
524     if (mGnssCbIface_2_1 != nullptr) {
525         // For emergency mode, request DBH (Device based hybrid) location
526         // Mark Independent from GNSS flag to false.
527         if (ODCPI_REQUEST_TYPE_START == request.type) {
528             LOC_LOGd("gnssRequestLocationCb_2_1 isUserEmergency = %d", request.isEmergencyMode);
529             auto r = mGnssCbIface_2_1->gnssRequestLocationCb_2_0(!request.isEmergencyMode,
530                                                                  request.isEmergencyMode);
531             if (!r.isOk()) {
532                 LOC_LOGe("Error invoking gnssRequestLocationCb_2_0 %s", r.description().c_str());
533             }
534         } else {
535             LOC_LOGv("Unsupported ODCPI request type: %d", request.type);
536         }
537     } else if (mGnssCbIface_2_0 != nullptr) {
538         // For emergency mode, request DBH (Device based hybrid) location
539         // Mark Independent from GNSS flag to false.
540         if (ODCPI_REQUEST_TYPE_START == request.type) {
541             LOC_LOGd("gnssRequestLocationCb_2_0 isUserEmergency = %d", request.isEmergencyMode);
542             auto r = mGnssCbIface_2_0->gnssRequestLocationCb_2_0(!request.isEmergencyMode,
543                                                                  request.isEmergencyMode);
544             if (!r.isOk()) {
545                 LOC_LOGe("Error invoking gnssRequestLocationCb_2_0 %s", r.description().c_str());
546             }
547         } else {
548             LOC_LOGv("Unsupported ODCPI request type: %d", request.type);
549         }
550     } else if (mGnssCbIface_1_1 != nullptr) {
551         // For emergency mode, request DBH (Device based hybrid) location
552         // Mark Independent from GNSS flag to false.
553         if (ODCPI_REQUEST_TYPE_START == request.type) {
554             auto r = mGnssCbIface_1_1->gnssRequestLocationCb(!request.isEmergencyMode);
555             if (!r.isOk()) {
556                 LOC_LOGe("Error invoking gnssRequestLocationCb %s", r.description().c_str());
557             }
558         } else {
559             LOC_LOGv("Unsupported ODCPI request type: %d", request.type);
560         }
561     } else {
562         LOC_LOGe("ODCPI request not supported.");
563     }
564 }
565 
566 // Methods from ::android::hardware::gnss::V2_0::IGnss follow.
setCallback_2_0(const sp<V2_0::IGnssCallback> & callback)567 Return<bool> Gnss::setCallback_2_0(const sp<V2_0::IGnssCallback>& callback) {
568     ENTRY_LOG_CALLFLOW();
569     auto r = callback->gnssNameCb(getVersionString());
570     if (!r.isOk()) {
571         LOC_LOGE("%s] Error from gnssNameCb description=%s",
572                 __func__, r.description().c_str());
573     }
574 
575     // In case where previous call to setCallback/setCallback_1_1/setCallback_2_1, then
576     // we need to cleanup these interfaces/callbacks here since we no longer
577     // do so in cleanup() function to keep callbacks around after cleanup()
578     if (mApi != nullptr) {
579         mApi->gnssUpdateCallbacks(nullptr, nullptr);
580         mApi->gnssUpdateCallbacks_2_1(nullptr);
581     }
582     mGnssNiCbIface = nullptr;
583     if (mGnssCbIface != nullptr) {
584         mGnssCbIface->unlinkToDeath(mGnssDeathRecipient);
585         mGnssCbIface = nullptr;
586     }
587     if (mGnssCbIface_1_1 != nullptr) {
588         mGnssCbIface_1_1->unlinkToDeath(mGnssDeathRecipient);
589         mGnssCbIface_1_1 = nullptr;
590     }
591     if (mGnssCbIface_2_1 != nullptr) {
592         mGnssCbIface_2_1->unlinkToDeath(mGnssDeathRecipient);
593         mGnssCbIface_2_1 = nullptr;
594     }
595 
596 
597     if (mGnssCbIface_2_0 != nullptr) {
598         mGnssCbIface_2_0->unlinkToDeath(mGnssDeathRecipient);
599     }
600     mGnssCbIface_2_0 = callback;
601     if (mGnssCbIface_2_0 != nullptr) {
602         mGnssCbIface_2_0->linkToDeath(mGnssDeathRecipient, 0 /*cookie*/);
603     }
604 
605     const GnssInterface* gnssInterface = getGnssInterface();
606     if (nullptr != gnssInterface) {
607         OdcpiRequestCallback cb = [this](const OdcpiRequestInfo& odcpiRequest) {
608             odcpiRequestCb(odcpiRequest);
609         };
610         gnssInterface->odcpiInit(cb);
611     }
612 
613     GnssAPIClient* api = getApi();
614     if (api != nullptr) {
615         api->gnssUpdateCallbacks_2_0(mGnssCbIface_2_0);
616         api->gnssEnable(LOCATION_TECHNOLOGY_TYPE_GNSS);
617         api->requestCapabilities();
618     }
619 
620     return true;
621 }
622 
getExtensionAGnss_2_0()623 Return<sp<V2_0::IAGnss>> Gnss::getExtensionAGnss_2_0() {
624     ENTRY_LOG_CALLFLOW();
625     if (mAGnssIface_2_0 == nullptr) {
626         mAGnssIface_2_0 = new AGnss(this);
627     }
628     return mAGnssIface_2_0;
629 }
getExtensionAGnssRil_2_0()630 Return<sp<V2_0::IAGnssRil>> Gnss::getExtensionAGnssRil_2_0() {
631 
632     if (mGnssRil == nullptr) {
633         mGnssRil = new AGnssRil(this);
634     }
635     return mGnssRil;
636 }
637 
getExtensionGnssConfiguration_2_0()638 Return<sp<V2_0::IGnssConfiguration>> Gnss::getExtensionGnssConfiguration_2_0() {
639     ENTRY_LOG_CALLFLOW();
640     if (mGnssConfig == nullptr) {
641         mGnssConfig = new GnssConfiguration(this);
642     }
643     return mGnssConfig;
644 }
getExtensionGnssMeasurement_2_0()645 Return<sp<V2_0::IGnssMeasurement>> Gnss::getExtensionGnssMeasurement_2_0() {
646     ENTRY_LOG_CALLFLOW();
647 #ifdef GNSS_HIDL_LEGACY_MEASURMENTS
648     return nullptr;
649 #else
650     if (mGnssMeasurement == nullptr)
651         mGnssMeasurement = new GnssMeasurement();
652     return mGnssMeasurement;
653 #endif
654 }
655 
656 Return<sp<IMeasurementCorrectionsV1_0>>
getExtensionMeasurementCorrections()657         Gnss::getExtensionMeasurementCorrections() {
658     //return nullptr;
659     ENTRY_LOG_CALLFLOW();
660     if (mGnssMeasCorr == nullptr) {
661          mGnssMeasCorr = new MeasurementCorrections(this);
662     }
663     return mGnssMeasCorr;
664 }
665 
666 Return<sp<IMeasurementCorrectionsV1_1>>
getExtensionMeasurementCorrections_1_1()667         Gnss::getExtensionMeasurementCorrections_1_1() {
668     ENTRY_LOG_CALLFLOW();
669     if (mGnssMeasCorr == nullptr) {
670         mGnssMeasCorr = new MeasurementCorrections(this);
671     }
672     return mGnssMeasCorr;
673 }
674 
675 Return<sp<::android::hardware::gnss::visibility_control::V1_0::IGnssVisibilityControl>>
getExtensionVisibilityControl()676         Gnss::getExtensionVisibilityControl() {
677     ENTRY_LOG_CALLFLOW();
678     if (mVisibCtrl == nullptr) {
679         mVisibCtrl = new GnssVisibilityControl(this);
680     }
681     return mVisibCtrl;
682 }
683 
injectBestLocation_2_0(const V2_0::GnssLocation & gnssLocation)684 Return<bool> Gnss::injectBestLocation_2_0(const V2_0::GnssLocation& gnssLocation) {
685     ENTRY_LOG_CALLFLOW();
686     const GnssInterface* gnssInterface = getGnssInterface();
687     if (nullptr != gnssInterface) {
688         Location location = {};
689         convertGnssLocation(gnssLocation, location);
690         gnssInterface->odcpiInject(location);
691     }
692     return true;
693 }
694 
getExtensionGnssDebug_2_0()695 Return<sp<V2_0::IGnssDebug>> Gnss::getExtensionGnssDebug_2_0() {
696     ENTRY_LOG_CALLFLOW();
697     if (mGnssDebug == nullptr) {
698         mGnssDebug = new GnssDebug(this);
699     }
700     return mGnssDebug;
701 }
702 
getExtensionGnssBatching_2_0()703 Return<sp<V2_0::IGnssBatching>> Gnss::getExtensionGnssBatching_2_0() {
704     return nullptr;
705 }
706 
707 // Methods from ::android::hardware::gnss::V2_1::IGnss follow.
setCallback_2_1(const sp<V2_1::IGnssCallback> & callback)708 Return<bool> Gnss::setCallback_2_1(const sp<V2_1::IGnssCallback>& callback) {
709     ENTRY_LOG_CALLFLOW();
710     auto r = callback->gnssNameCb(getVersionString());
711     if (!r.isOk()) {
712         LOC_LOGE("%s] Error from gnssNameCb description=%s",
713                 __func__, r.description().c_str());
714     }
715 
716     // In case where previous call to setCallback/setCallback_1_1/setCallback_2_0, then
717     // we need to cleanup these interfaces/callbacks here since we no longer
718     // do so in cleanup() function to keep callbacks around after cleanup()
719     if (mApi != nullptr) {
720         mApi->gnssUpdateCallbacks(nullptr, nullptr);
721         mApi->gnssUpdateCallbacks_2_0(nullptr);
722     }
723     mGnssNiCbIface = nullptr;
724     if (mGnssCbIface != nullptr) {
725         mGnssCbIface->unlinkToDeath(mGnssDeathRecipient);
726         mGnssCbIface = nullptr;
727     }
728     if (mGnssCbIface_1_1 != nullptr) {
729         mGnssCbIface_1_1->unlinkToDeath(mGnssDeathRecipient);
730         mGnssCbIface_1_1 = nullptr;
731     }
732     if (mGnssCbIface_2_0 != nullptr) {
733         mGnssCbIface_2_0->unlinkToDeath(mGnssDeathRecipient);
734         mGnssCbIface_2_0 = nullptr;
735     }
736     if (mGnssCbIface_2_1 != nullptr) {
737         mGnssCbIface_2_1->unlinkToDeath(mGnssDeathRecipient);
738     }
739     mGnssCbIface_2_1 = callback;
740     if (mGnssCbIface_2_1 != nullptr) {
741         mGnssCbIface_2_1->linkToDeath(mGnssDeathRecipient, 0 /*cookie*/);
742     }
743 
744     const GnssInterface* gnssInterface = getGnssInterface();
745     if (gnssInterface != nullptr) {
746         OdcpiRequestCallback cb = [this](const OdcpiRequestInfo& odcpiRequest) {
747             odcpiRequestCb(odcpiRequest);
748         };
749         gnssInterface->odcpiInit(cb);
750     }
751 
752     GnssAPIClient* api = getApi();
753     if (api != nullptr) {
754         api->gnssUpdateCallbacks_2_1(mGnssCbIface_2_1);
755         api->gnssEnable(LOCATION_TECHNOLOGY_TYPE_GNSS);
756         api->requestCapabilities();
757     }
758 
759     return true;
760 }
getExtensionGnssMeasurement_2_1()761 Return<sp<V2_1::IGnssMeasurement>> Gnss::getExtensionGnssMeasurement_2_1() {
762     ENTRY_LOG_CALLFLOW();
763     if (mGnssMeasurement == nullptr) {
764         mGnssMeasurement = new GnssMeasurement();
765     }
766     return mGnssMeasurement;
767 }
getExtensionGnssConfiguration_2_1()768 Return<sp<V2_1::IGnssConfiguration>> Gnss::getExtensionGnssConfiguration_2_1() {
769     ENTRY_LOG_CALLFLOW();
770     if (mGnssConfig == nullptr) {
771         mGnssConfig = new GnssConfiguration(this);
772     }
773     return mGnssConfig;
774 }
775 
getExtensionGnssAntennaInfo()776 Return<sp<V2_1::IGnssAntennaInfo>> Gnss::getExtensionGnssAntennaInfo() {
777     ENTRY_LOG_CALLFLOW();
778     if (mGnssAntennaInfo == nullptr) {
779         mGnssAntennaInfo = new GnssAntennaInfo(this);
780     }
781     return mGnssAntennaInfo;
782 }
783 
HIDL_FETCH_IGnss(const char * hal)784 V1_0::IGnss* HIDL_FETCH_IGnss(const char* hal) {
785     ENTRY_LOG_CALLFLOW();
786     V1_0::IGnss* iface = nullptr;
787     iface = new Gnss();
788     if (iface == nullptr) {
789         LOC_LOGE("%s]: failed to get %s", __FUNCTION__, hal);
790     }
791     return iface;
792 }
793 
794 }  // namespace implementation
795 }  // namespace V2_1
796 }  // namespace gnss
797 }  // namespace hardware
798 }  // namespace android
799