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