Gnss.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 V1_1 {
  37. namespace implementation {
  38. static sp<Gnss> sGnss;
  39. static std::string getVersionString() {
  40. static std::string version;
  41. if (!version.empty())
  42. return version;
  43. char value[PROPERTY_VALUE_MAX] = {0};
  44. property_get("ro.hardware", value, "unknown");
  45. version.append(value).append(DELIMITER);
  46. std::ifstream in(IMAGES_INFO_FILE);
  47. std::string s;
  48. while(getline(in, s)) {
  49. std::size_t found = s.find("CRM:");
  50. if (std::string::npos == found) {
  51. continue;
  52. }
  53. // skip over space characters after "CRM:"
  54. const char* substr = s.c_str();
  55. found += 4;
  56. while (0 != substr[found] && isspace(substr[found])) {
  57. found++;
  58. }
  59. if (s.find("11:") != found) {
  60. continue;
  61. }
  62. s.erase(0, found + 3);
  63. found = s.find_first_of("\r\n");
  64. if (std::string::npos != found) {
  65. s.erase(s.begin() + found, s.end());
  66. }
  67. version.append(s).append(DELIMITER);
  68. }
  69. return version;
  70. }
  71. void Gnss::GnssDeathRecipient::serviceDied(uint64_t cookie, const wp<IBase>& who) {
  72. LOC_LOGE("%s] service died. cookie: %llu, who: %p",
  73. __FUNCTION__, static_cast<unsigned long long>(cookie), &who);
  74. auto gnss = mGnss.promote();
  75. if (gnss != nullptr) {
  76. gnss->handleClientDeath();
  77. }
  78. }
  79. void location_on_battery_status_changed(bool charging) {
  80. LOC_LOGd("battery status changed to %s charging", charging ? "" : "not");
  81. if (sGnss != nullptr) {
  82. sGnss->getGnssInterface()->updateBatteryStatus(charging);
  83. }
  84. }
  85. Gnss::Gnss() {
  86. ENTRY_LOG_CALLFLOW();
  87. sGnss = this;
  88. // initilize gnss interface at first in case needing notify battery status
  89. sGnss->getGnssInterface()->initialize();
  90. // register health client to listen on battery change
  91. loc_extn_battery_properties_listener_init(location_on_battery_status_changed);
  92. // clear pending GnssConfig
  93. memset(&mPendingConfig, 0, sizeof(GnssConfig));
  94. mGnssDeathRecipient = new GnssDeathRecipient(sGnss);
  95. }
  96. Gnss::~Gnss() {
  97. ENTRY_LOG_CALLFLOW();
  98. if (mApi != nullptr) {
  99. mApi->destroy();
  100. mApi = nullptr;
  101. }
  102. sGnss = nullptr;
  103. }
  104. void Gnss::handleClientDeath() {
  105. getGnssInterface()->resetNetworkInfo();
  106. cleanup();
  107. if (mApi != nullptr) {
  108. mApi->gnssUpdateCallbacks(nullptr, nullptr);
  109. }
  110. mGnssCbIface = nullptr;
  111. mGnssNiCbIface = nullptr;
  112. mGnssCbIface_1_1 = nullptr;
  113. }
  114. GnssAPIClient* Gnss::getApi() {
  115. if (mApi == nullptr && (mGnssCbIface != nullptr || mGnssNiCbIface != nullptr)) {
  116. mApi = new GnssAPIClient(mGnssCbIface, mGnssNiCbIface);
  117. if (mApi == nullptr) {
  118. LOC_LOGE("%s] faild to create GnssAPIClient", __FUNCTION__);
  119. return mApi;
  120. }
  121. if (mPendingConfig.size == sizeof(GnssConfig)) {
  122. // we have pending GnssConfig
  123. mApi->gnssConfigurationUpdate(mPendingConfig);
  124. // clear size to invalid mPendingConfig
  125. mPendingConfig.size = 0;
  126. if (mPendingConfig.assistanceServer.hostName != nullptr) {
  127. free((void*)mPendingConfig.assistanceServer.hostName);
  128. }
  129. }
  130. }
  131. if (mApi == nullptr) {
  132. LOC_LOGW("%s] GnssAPIClient is not ready", __FUNCTION__);
  133. }
  134. return mApi;
  135. }
  136. const GnssInterface* Gnss::getGnssInterface() {
  137. static bool getGnssInterfaceFailed = false;
  138. if (nullptr == mGnssInterface && !getGnssInterfaceFailed) {
  139. void * libHandle = nullptr;
  140. getLocationInterface* getter = (getLocationInterface*)
  141. dlGetSymFromLib(libHandle, "libgnss.so", "getGnssInterface");
  142. if (nullptr == getter) {
  143. getGnssInterfaceFailed = true;
  144. } else {
  145. mGnssInterface = (const GnssInterface*)(*getter)();
  146. }
  147. }
  148. return mGnssInterface;
  149. }
  150. Return<bool> Gnss::setCallback(const sp<V1_0::IGnssCallback>& callback) {
  151. ENTRY_LOG_CALLFLOW();
  152. if (mGnssCbIface != nullptr) {
  153. mGnssCbIface->unlinkToDeath(mGnssDeathRecipient);
  154. }
  155. mGnssCbIface = callback;
  156. if (mGnssCbIface != nullptr) {
  157. mGnssCbIface->linkToDeath(mGnssDeathRecipient, 0 /*cookie*/);
  158. }
  159. GnssAPIClient* api = getApi();
  160. if (api != nullptr) {
  161. api->gnssUpdateCallbacks(mGnssCbIface, mGnssNiCbIface);
  162. api->gnssEnable(LOCATION_TECHNOLOGY_TYPE_GNSS);
  163. api->requestCapabilities();
  164. }
  165. return true;
  166. }
  167. Return<bool> Gnss::setGnssNiCb(const sp<IGnssNiCallback>& callback) {
  168. ENTRY_LOG_CALLFLOW();
  169. mGnssNiCbIface = callback;
  170. GnssAPIClient* api = getApi();
  171. if (api != nullptr) {
  172. api->gnssUpdateCallbacks(mGnssCbIface, mGnssNiCbIface);
  173. }
  174. return true;
  175. }
  176. Return<bool> Gnss::updateConfiguration(GnssConfig& gnssConfig) {
  177. ENTRY_LOG_CALLFLOW();
  178. GnssAPIClient* api = getApi();
  179. if (api) {
  180. api->gnssConfigurationUpdate(gnssConfig);
  181. } else if (gnssConfig.flags != 0) {
  182. // api is not ready yet, update mPendingConfig with gnssConfig
  183. mPendingConfig.size = sizeof(GnssConfig);
  184. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_GPS_LOCK_VALID_BIT) {
  185. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_GPS_LOCK_VALID_BIT;
  186. mPendingConfig.gpsLock = gnssConfig.gpsLock;
  187. }
  188. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SUPL_VERSION_VALID_BIT) {
  189. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SUPL_VERSION_VALID_BIT;
  190. mPendingConfig.suplVersion = gnssConfig.suplVersion;
  191. }
  192. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SET_ASSISTANCE_DATA_VALID_BIT) {
  193. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SET_ASSISTANCE_DATA_VALID_BIT;
  194. mPendingConfig.assistanceServer.size = sizeof(GnssConfigSetAssistanceServer);
  195. mPendingConfig.assistanceServer.type = gnssConfig.assistanceServer.type;
  196. if (mPendingConfig.assistanceServer.hostName != nullptr) {
  197. free((void*)mPendingConfig.assistanceServer.hostName);
  198. mPendingConfig.assistanceServer.hostName =
  199. strdup(gnssConfig.assistanceServer.hostName);
  200. }
  201. mPendingConfig.assistanceServer.port = gnssConfig.assistanceServer.port;
  202. }
  203. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_LPP_PROFILE_VALID_BIT) {
  204. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_LPP_PROFILE_VALID_BIT;
  205. mPendingConfig.lppProfileMask = gnssConfig.lppProfileMask;
  206. }
  207. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_LPPE_CONTROL_PLANE_VALID_BIT) {
  208. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_LPPE_CONTROL_PLANE_VALID_BIT;
  209. mPendingConfig.lppeControlPlaneMask = gnssConfig.lppeControlPlaneMask;
  210. }
  211. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_LPPE_USER_PLANE_VALID_BIT) {
  212. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_LPPE_USER_PLANE_VALID_BIT;
  213. mPendingConfig.lppeUserPlaneMask = gnssConfig.lppeUserPlaneMask;
  214. }
  215. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_AGLONASS_POSITION_PROTOCOL_VALID_BIT) {
  216. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_AGLONASS_POSITION_PROTOCOL_VALID_BIT;
  217. mPendingConfig.aGlonassPositionProtocolMask = gnssConfig.aGlonassPositionProtocolMask;
  218. }
  219. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_EM_PDN_FOR_EM_SUPL_VALID_BIT) {
  220. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_EM_PDN_FOR_EM_SUPL_VALID_BIT;
  221. mPendingConfig.emergencyPdnForEmergencySupl = gnssConfig.emergencyPdnForEmergencySupl;
  222. }
  223. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SUPL_EM_SERVICES_BIT) {
  224. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SUPL_EM_SERVICES_BIT;
  225. mPendingConfig.suplEmergencyServices = gnssConfig.suplEmergencyServices;
  226. }
  227. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SUPL_MODE_BIT) {
  228. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SUPL_MODE_BIT;
  229. mPendingConfig.suplModeMask = gnssConfig.suplModeMask;
  230. }
  231. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_BLACKLISTED_SV_IDS_BIT) {
  232. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_BLACKLISTED_SV_IDS_BIT;
  233. mPendingConfig.blacklistedSvIds = gnssConfig.blacklistedSvIds;
  234. }
  235. }
  236. return true;
  237. }
  238. Return<bool> Gnss::start() {
  239. ENTRY_LOG_CALLFLOW();
  240. bool retVal = false;
  241. GnssAPIClient* api = getApi();
  242. if (api) {
  243. retVal = api->gnssStart();
  244. }
  245. return retVal;
  246. }
  247. Return<bool> Gnss::stop() {
  248. ENTRY_LOG_CALLFLOW();
  249. bool retVal = false;
  250. GnssAPIClient* api = getApi();
  251. if (api) {
  252. retVal = api->gnssStop();
  253. }
  254. return retVal;
  255. }
  256. Return<void> Gnss::cleanup() {
  257. ENTRY_LOG_CALLFLOW();
  258. if (mApi != nullptr) {
  259. mApi->gnssDisable();
  260. }
  261. return Void();
  262. }
  263. Return<bool> Gnss::injectLocation(double latitudeDegrees,
  264. double longitudeDegrees,
  265. float accuracyMeters) {
  266. ENTRY_LOG_CALLFLOW();
  267. const GnssInterface* gnssInterface = getGnssInterface();
  268. if (nullptr != gnssInterface) {
  269. gnssInterface->injectLocation(latitudeDegrees, longitudeDegrees, accuracyMeters);
  270. return true;
  271. } else {
  272. return false;
  273. }
  274. }
  275. Return<bool> Gnss::injectTime(int64_t timeMs, int64_t timeReferenceMs,
  276. int32_t uncertaintyMs) {
  277. return true;
  278. }
  279. Return<void> Gnss::deleteAidingData(V1_0::IGnss::GnssAidingData aidingDataFlags) {
  280. ENTRY_LOG_CALLFLOW();
  281. GnssAPIClient* api = getApi();
  282. if (api) {
  283. api->gnssDeleteAidingData(aidingDataFlags);
  284. }
  285. return Void();
  286. }
  287. Return<bool> Gnss::setPositionMode(V1_0::IGnss::GnssPositionMode mode,
  288. V1_0::IGnss::GnssPositionRecurrence recurrence,
  289. uint32_t minIntervalMs,
  290. uint32_t preferredAccuracyMeters,
  291. uint32_t preferredTimeMs) {
  292. ENTRY_LOG_CALLFLOW();
  293. bool retVal = false;
  294. GnssAPIClient* api = getApi();
  295. if (api) {
  296. retVal = api->gnssSetPositionMode(mode, recurrence, minIntervalMs,
  297. preferredAccuracyMeters, preferredTimeMs);
  298. }
  299. return retVal;
  300. }
  301. Return<sp<V1_0::IAGnss>> Gnss::getExtensionAGnss() {
  302. ENTRY_LOG_CALLFLOW();
  303. mAGnssIface = new AGnss(this);
  304. return mAGnssIface;
  305. }
  306. Return<sp<V1_0::IGnssNi>> Gnss::getExtensionGnssNi() {
  307. ENTRY_LOG_CALLFLOW();
  308. mGnssNi = new GnssNi(this);
  309. return mGnssNi;
  310. }
  311. Return<sp<V1_0::IGnssMeasurement>> Gnss::getExtensionGnssMeasurement() {
  312. ENTRY_LOG_CALLFLOW();
  313. if (mGnssMeasurement == nullptr)
  314. mGnssMeasurement = new GnssMeasurement(mGnssMeasurement);
  315. return mGnssMeasurement;
  316. }
  317. Return<sp<V1_0::IGnssConfiguration>> Gnss::getExtensionGnssConfiguration() {
  318. ENTRY_LOG_CALLFLOW();
  319. mGnssConfig = new GnssConfiguration(this);
  320. return mGnssConfig;
  321. }
  322. Return<sp<V1_0::IGnssGeofencing>> Gnss::getExtensionGnssGeofencing() {
  323. ENTRY_LOG_CALLFLOW();
  324. mGnssGeofencingIface = new GnssGeofencing(mGnssGeofencingIface);
  325. return mGnssGeofencingIface;
  326. }
  327. Return<sp<V1_0::IGnssBatching>> Gnss::getExtensionGnssBatching() {
  328. mGnssBatching = new GnssBatching(mGnssBatching);
  329. return mGnssBatching;
  330. }
  331. Return<sp<V1_0::IGnssDebug>> Gnss::getExtensionGnssDebug() {
  332. ENTRY_LOG_CALLFLOW();
  333. mGnssDebug = new GnssDebug(this);
  334. return mGnssDebug;
  335. }
  336. Return<sp<V1_0::IAGnssRil>> Gnss::getExtensionAGnssRil() {
  337. mGnssRil = new AGnssRil(this);
  338. return mGnssRil;
  339. }
  340. // Methods from ::android::hardware::gnss::V1_1::IGnss follow.
  341. Return<bool> Gnss::setCallback_1_1(const sp<V1_1::IGnssCallback>& callback) {
  342. ENTRY_LOG_CALLFLOW();
  343. callback->gnssNameCb(getVersionString());
  344. mGnssCbIface_1_1 = callback;
  345. const GnssInterface* gnssInterface = getGnssInterface();
  346. if (nullptr != gnssInterface) {
  347. OdcpiRequestCallback cb = [this](const OdcpiRequestInfo& odcpiRequest) {
  348. odcpiRequestCb(odcpiRequest);
  349. };
  350. gnssInterface->odcpiInit(cb, OdcpiPrioritytype::ODCPI_HANDLER_PRIORITY_LOW,
  351. (EMERGENCY_ODCPI | NON_EMERGENCY_ODCPI));
  352. }
  353. return setCallback(callback);
  354. }
  355. Return<bool> Gnss::setPositionMode_1_1(V1_0::IGnss::GnssPositionMode mode,
  356. V1_0::IGnss::GnssPositionRecurrence recurrence,
  357. uint32_t minIntervalMs,
  358. uint32_t preferredAccuracyMeters,
  359. uint32_t preferredTimeMs,
  360. bool lowPowerMode) {
  361. ENTRY_LOG_CALLFLOW();
  362. bool retVal = false;
  363. GnssAPIClient* api = getApi();
  364. if (api) {
  365. GnssPowerMode powerMode = lowPowerMode?
  366. GNSS_POWER_MODE_M4 : GNSS_POWER_MODE_M2;
  367. retVal = api->gnssSetPositionMode(mode, recurrence, minIntervalMs,
  368. preferredAccuracyMeters, preferredTimeMs, powerMode, minIntervalMs);
  369. }
  370. return retVal;
  371. }
  372. Return<sp<V1_1::IGnssMeasurement>> Gnss::getExtensionGnssMeasurement_1_1() {
  373. ENTRY_LOG_CALLFLOW();
  374. if (mGnssMeasurement == nullptr)
  375. mGnssMeasurement = new GnssMeasurement(mGnssMeasurement);
  376. return mGnssMeasurement;
  377. }
  378. Return<sp<V1_1::IGnssConfiguration>> Gnss::getExtensionGnssConfiguration_1_1() {
  379. ENTRY_LOG_CALLFLOW();
  380. if (mGnssConfig == nullptr)
  381. mGnssConfig = new GnssConfiguration(this);
  382. return mGnssConfig;
  383. }
  384. Return<bool> Gnss::injectBestLocation(const GnssLocation& gnssLocation) {
  385. ENTRY_LOG_CALLFLOW();
  386. const GnssInterface* gnssInterface = getGnssInterface();
  387. if (nullptr != gnssInterface) {
  388. Location location = {};
  389. convertGnssLocation(gnssLocation, location);
  390. location.techMask |= LOCATION_TECHNOLOGY_HYBRID_BIT;
  391. gnssInterface->odcpiInject(location);
  392. }
  393. return true;
  394. }
  395. void Gnss::odcpiRequestCb(const OdcpiRequestInfo& request) {
  396. ENTRY_LOG_CALLFLOW();
  397. if (ODCPI_REQUEST_TYPE_STOP == request.type) {
  398. return;
  399. }
  400. if (mGnssCbIface_1_1 != nullptr) {
  401. // For emergency mode, request DBH (Device based hybrid) location
  402. // Mark Independent from GNSS flag to false.
  403. if (ODCPI_REQUEST_TYPE_START == request.type) {
  404. auto r = mGnssCbIface_1_1->gnssRequestLocationCb(!request.isEmergencyMode);
  405. if (!r.isOk()) {
  406. LOC_LOGe("Error invoking gnssRequestLocationCb %s", r.description().c_str());
  407. }
  408. } else {
  409. LOC_LOGv("Unsupported ODCPI request type: %d", request.type);
  410. }
  411. } else {
  412. LOC_LOGe("ODCPI request not supported.");
  413. }
  414. }
  415. V1_0::IGnss* HIDL_FETCH_IGnss(const char* hal) {
  416. ENTRY_LOG_CALLFLOW();
  417. V1_0::IGnss* iface = nullptr;
  418. iface = new Gnss();
  419. if (iface == nullptr) {
  420. LOC_LOGE("%s]: failed to get %s", __FUNCTION__, hal);
  421. }
  422. return iface;
  423. }
  424. } // namespace implementation
  425. } // namespace V1_1
  426. } // namespace gnss
  427. } // namespace hardware
  428. } // namespace android