Gnss.cpp 23 KB

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