Gnss.cpp 27 KB

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