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