Gnss.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. }
  413. GnssAPIClient* api = getApi();
  414. if (api != nullptr) {
  415. api->gnssUpdateCallbacks(mGnssCbIface_1_1, mGnssNiCbIface);
  416. api->gnssEnable(LOCATION_TECHNOLOGY_TYPE_GNSS);
  417. api->requestCapabilities();
  418. }
  419. return true;
  420. }
  421. Return<bool> Gnss::setPositionMode_1_1(V1_0::IGnss::GnssPositionMode mode,
  422. V1_0::IGnss::GnssPositionRecurrence recurrence,
  423. uint32_t minIntervalMs,
  424. uint32_t preferredAccuracyMeters,
  425. uint32_t preferredTimeMs,
  426. bool lowPowerMode) {
  427. ENTRY_LOG_CALLFLOW();
  428. bool retVal = false;
  429. GnssAPIClient* api = getApi();
  430. if (api) {
  431. GnssPowerMode powerMode = lowPowerMode?
  432. GNSS_POWER_MODE_M4 : GNSS_POWER_MODE_M2;
  433. retVal = api->gnssSetPositionMode(mode, recurrence, minIntervalMs,
  434. preferredAccuracyMeters, preferredTimeMs, powerMode, minIntervalMs);
  435. }
  436. return retVal;
  437. }
  438. Return<sp<V1_1::IGnssMeasurement>> Gnss::getExtensionGnssMeasurement_1_1() {
  439. ENTRY_LOG_CALLFLOW();
  440. #ifdef GNSS_HIDL_LEGACY_MEASURMENTS
  441. return nullptr;
  442. #else
  443. if (mGnssMeasurement == nullptr)
  444. mGnssMeasurement = new GnssMeasurement(mGnssMeasurement);
  445. return mGnssMeasurement;
  446. #endif
  447. }
  448. Return<sp<V1_1::IGnssConfiguration>> Gnss::getExtensionGnssConfiguration_1_1() {
  449. ENTRY_LOG_CALLFLOW();
  450. if (mGnssConfig == nullptr)
  451. mGnssConfig = new GnssConfiguration(this);
  452. return mGnssConfig;
  453. }
  454. Return<bool> Gnss::injectBestLocation(const GnssLocation& gnssLocation) {
  455. ENTRY_LOG_CALLFLOW();
  456. const GnssInterface* gnssInterface = getGnssInterface();
  457. if (nullptr != gnssInterface) {
  458. Location location = {};
  459. convertGnssLocation(gnssLocation, location);
  460. location.techMask |= LOCATION_TECHNOLOGY_HYBRID_BIT;
  461. gnssInterface->odcpiInject(location);
  462. }
  463. return true;
  464. }
  465. void Gnss::odcpiRequestCb(const OdcpiRequestInfo& request) {
  466. ENTRY_LOG_CALLFLOW();
  467. if (ODCPI_REQUEST_TYPE_STOP == request.type) {
  468. return;
  469. }
  470. if (mGnssCbIface_2_0 != nullptr) {
  471. // For emergency mode, request DBH (Device based hybrid) location
  472. // Mark Independent from GNSS flag to false.
  473. if (ODCPI_REQUEST_TYPE_START == request.type) {
  474. LOC_LOGd("gnssRequestLocationCb_2_0 isUserEmergency = %d", request.isEmergencyMode);
  475. auto r = mGnssCbIface_2_0->gnssRequestLocationCb_2_0(!request.isEmergencyMode,
  476. request.isEmergencyMode);
  477. if (!r.isOk()) {
  478. LOC_LOGe("Error invoking gnssRequestLocationCb_2_0 %s", r.description().c_str());
  479. }
  480. } else {
  481. LOC_LOGv("Unsupported ODCPI request type: %d", request.type);
  482. }
  483. } else if (mGnssCbIface_1_1 != nullptr) {
  484. // For emergency mode, request DBH (Device based hybrid) location
  485. // Mark Independent from GNSS flag to false.
  486. if (ODCPI_REQUEST_TYPE_START == request.type) {
  487. auto r = mGnssCbIface_1_1->gnssRequestLocationCb(!request.isEmergencyMode);
  488. if (!r.isOk()) {
  489. LOC_LOGe("Error invoking gnssRequestLocationCb %s", r.description().c_str());
  490. }
  491. } else {
  492. LOC_LOGv("Unsupported ODCPI request type: %d", request.type);
  493. }
  494. } else {
  495. LOC_LOGe("ODCPI request not supported.");
  496. }
  497. }
  498. // Methods from ::android::hardware::gnss::V2_0::IGnss follow.
  499. Return<bool> Gnss::setCallback_2_0(const sp<V2_0::IGnssCallback>& callback) {
  500. ENTRY_LOG_CALLFLOW();
  501. auto r = callback->gnssNameCb(getVersionString());
  502. if (!r.isOk()) {
  503. LOC_LOGE("%s] Error from gnssNameCb description=%s",
  504. __func__, r.description().c_str());
  505. }
  506. // In case where previous call to setCallback or setCallback_1_1, then
  507. // we need to cleanup these interfaces/callbacks here since we no longer
  508. // do so in cleanup() function to keep callbacks around after cleanup()
  509. if (mApi != nullptr) {
  510. mApi->gnssUpdateCallbacks(nullptr, nullptr);
  511. }
  512. mGnssNiCbIface = nullptr;
  513. if (mGnssCbIface != nullptr) {
  514. mGnssCbIface->unlinkToDeath(mGnssDeathRecipient);
  515. mGnssCbIface = nullptr;
  516. }
  517. if (mGnssCbIface_1_1 != nullptr) {
  518. mGnssCbIface_1_1->unlinkToDeath(mGnssDeathRecipient);
  519. mGnssCbIface_1_1 = nullptr;
  520. }
  521. if (mGnssCbIface_2_0 != nullptr) {
  522. mGnssCbIface_2_0->unlinkToDeath(mGnssDeathRecipient);
  523. }
  524. mGnssCbIface_2_0 = callback;
  525. if (mGnssCbIface_2_0 != nullptr) {
  526. mGnssCbIface_2_0->linkToDeath(mGnssDeathRecipient, 0 /*cookie*/);
  527. }
  528. const GnssInterface* gnssInterface = getGnssInterface();
  529. if (nullptr != gnssInterface) {
  530. OdcpiRequestCallback cb = [this](const OdcpiRequestInfo& odcpiRequest) {
  531. odcpiRequestCb(odcpiRequest);
  532. };
  533. gnssInterface->odcpiInit(cb, OdcpiPrioritytype::ODCPI_HANDLER_PRIORITY_LOW);
  534. }
  535. GnssAPIClient* api = getApi();
  536. if (api != nullptr) {
  537. api->gnssUpdateCallbacks_2_0(mGnssCbIface_2_0);
  538. api->gnssEnable(LOCATION_TECHNOLOGY_TYPE_GNSS);
  539. api->requestCapabilities();
  540. }
  541. return true;
  542. }
  543. Return<sp<V2_0::IAGnss>> Gnss::getExtensionAGnss_2_0() {
  544. ENTRY_LOG_CALLFLOW();
  545. if (mAGnssIface_2_0 == nullptr) {
  546. mAGnssIface_2_0 = new AGnss(this);
  547. }
  548. return mAGnssIface_2_0;
  549. }
  550. Return<sp<V2_0::IAGnssRil>> Gnss::getExtensionAGnssRil_2_0() {
  551. if (mGnssRil == nullptr) {
  552. mGnssRil = new AGnssRil(this);
  553. }
  554. return mGnssRil;
  555. }
  556. Return<sp<V2_0::IGnssConfiguration>> Gnss::getExtensionGnssConfiguration_2_0() {
  557. ENTRY_LOG_CALLFLOW();
  558. if (mGnssConfig == nullptr) {
  559. mGnssConfig = new GnssConfiguration(this);
  560. }
  561. return mGnssConfig;
  562. }
  563. Return<sp<V2_0::IGnssMeasurement>> Gnss::getExtensionGnssMeasurement_2_0() {
  564. ENTRY_LOG_CALLFLOW();
  565. #ifdef GNSS_HIDL_LEGACY_MEASURMENTS
  566. return nullptr;
  567. #else
  568. if (mGnssMeasurement == nullptr)
  569. mGnssMeasurement = new GnssMeasurement(mGnssMeasurement);
  570. return mGnssMeasurement;
  571. #endif
  572. }
  573. Return<sp<::android::hardware::gnss::measurement_corrections::V1_0::IMeasurementCorrections>>
  574. Gnss::getExtensionMeasurementCorrections() {
  575. // We do not support, so return nullptr to pass VTS
  576. return nullptr;
  577. }
  578. Return<sp<::android::hardware::gnss::visibility_control::V1_0::IGnssVisibilityControl>>
  579. Gnss::getExtensionVisibilityControl() {
  580. ENTRY_LOG_CALLFLOW();
  581. if (mVisibCtrl == nullptr) {
  582. mVisibCtrl = new GnssVisibilityControl(this);
  583. }
  584. return mVisibCtrl;
  585. }
  586. Return<bool> Gnss::injectBestLocation_2_0(const V2_0::GnssLocation& gnssLocation) {
  587. ENTRY_LOG_CALLFLOW();
  588. const GnssInterface* gnssInterface = getGnssInterface();
  589. if (nullptr != gnssInterface) {
  590. Location location = {};
  591. convertGnssLocation(gnssLocation, location);
  592. location.techMask |= LOCATION_TECHNOLOGY_HYBRID_BIT;
  593. gnssInterface->odcpiInject(location);
  594. }
  595. return true;
  596. }
  597. Return<sp<V2_0::IGnssDebug>> Gnss::getExtensionGnssDebug_2_0() {
  598. ENTRY_LOG_CALLFLOW();
  599. if (mGnssDebug == nullptr) {
  600. mGnssDebug = new GnssDebug(this);
  601. }
  602. return mGnssDebug;
  603. }
  604. Return<sp<V2_0::IGnssBatching>> Gnss::getExtensionGnssBatching_2_0() {
  605. return nullptr;
  606. }
  607. V1_0::IGnss* HIDL_FETCH_IGnss(const char* hal) {
  608. ENTRY_LOG_CALLFLOW();
  609. V1_0::IGnss* iface = nullptr;
  610. iface = new Gnss();
  611. if (iface == nullptr) {
  612. LOC_LOGE("%s]: failed to get %s", __FUNCTION__, hal);
  613. }
  614. return iface;
  615. }
  616. } // namespace implementation
  617. } // namespace V2_0
  618. } // namespace gnss
  619. } // namespace hardware
  620. } // namespace android