Gnss.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. namespace android {
  32. namespace hardware {
  33. namespace gnss {
  34. namespace V1_0 {
  35. namespace implementation {
  36. static sp<Gnss> sGnss;
  37. void Gnss::GnssDeathRecipient::serviceDied(uint64_t cookie, const wp<IBase>& who) {
  38. LOC_LOGE("%s] service died. cookie: %llu, who: %p",
  39. __FUNCTION__, static_cast<unsigned long long>(cookie), &who);
  40. auto gnss = mGnss.promote();
  41. if (gnss != nullptr) {
  42. gnss->handleClientDeath();
  43. }
  44. }
  45. void location_on_battery_status_changed(bool charging) {
  46. LOC_LOGd("battery status changed to %s charging", charging ? "" : "not ");
  47. if (sGnss != nullptr) {
  48. sGnss->getGnssInterface()->updateBatteryStatus(charging);
  49. }
  50. }
  51. Gnss::Gnss() {
  52. ENTRY_LOG_CALLFLOW();
  53. sGnss = this;
  54. // initilize gnss interface at first in case needing notify battery status
  55. sGnss->getGnssInterface()->initialize();
  56. // register health client to listen on battery change
  57. loc_extn_battery_properties_listener_init(location_on_battery_status_changed);
  58. // clear pending GnssConfig
  59. memset(&mPendingConfig, 0, sizeof(GnssConfig));
  60. mGnssDeathRecipient = new GnssDeathRecipient(sGnss);
  61. }
  62. Gnss::~Gnss() {
  63. ENTRY_LOG_CALLFLOW();
  64. if (mApi != nullptr) {
  65. mApi->destroy();
  66. mApi = nullptr;
  67. }
  68. sGnss = nullptr;
  69. }
  70. void Gnss::handleClientDeath() {
  71. getGnssInterface()->resetNetworkInfo();
  72. cleanup();
  73. if (mApi != nullptr) {
  74. mApi->gnssUpdateCallbacks(nullptr, nullptr);
  75. }
  76. mGnssCbIface = nullptr;
  77. mGnssNiCbIface = nullptr;
  78. }
  79. GnssAPIClient* Gnss::getApi() {
  80. if (mApi == nullptr && (mGnssCbIface != nullptr || mGnssNiCbIface != nullptr)) {
  81. mApi = new GnssAPIClient(mGnssCbIface, mGnssNiCbIface);
  82. if (mApi == nullptr) {
  83. LOC_LOGE("%s] faild to create GnssAPIClient", __FUNCTION__);
  84. return mApi;
  85. }
  86. if (mPendingConfig.size == sizeof(GnssConfig)) {
  87. // we have pending GnssConfig
  88. mApi->gnssConfigurationUpdate(mPendingConfig);
  89. // clear size to invalid mPendingConfig
  90. mPendingConfig.size = 0;
  91. if (mPendingConfig.assistanceServer.hostName != nullptr) {
  92. free((void*)mPendingConfig.assistanceServer.hostName);
  93. }
  94. }
  95. }
  96. if (mApi == nullptr) {
  97. LOC_LOGW("%s] GnssAPIClient is not ready", __FUNCTION__);
  98. }
  99. return mApi;
  100. }
  101. const GnssInterface* Gnss::getGnssInterface() {
  102. static bool getGnssInterfaceFailed = false;
  103. if (nullptr == mGnssInterface && !getGnssInterfaceFailed) {
  104. void * libHandle = nullptr;
  105. getLocationInterface* getter = (getLocationInterface*)
  106. dlGetSymFromLib(libHandle, "libgnss.so", "getGnssInterface");
  107. if (nullptr == getter) {
  108. getGnssInterfaceFailed = true;
  109. } else {
  110. mGnssInterface = (const GnssInterface*)(*getter)();
  111. }
  112. }
  113. return mGnssInterface;
  114. }
  115. Return<bool> Gnss::setCallback(const sp<V1_0::IGnssCallback>& callback) {
  116. ENTRY_LOG_CALLFLOW();
  117. if (mGnssCbIface != nullptr) {
  118. mGnssCbIface->unlinkToDeath(mGnssDeathRecipient);
  119. }
  120. mGnssCbIface = callback;
  121. if (mGnssCbIface != nullptr) {
  122. mGnssCbIface->linkToDeath(mGnssDeathRecipient, 0 /*cookie*/);
  123. }
  124. GnssAPIClient* api = getApi();
  125. if (api != nullptr) {
  126. api->gnssUpdateCallbacks(mGnssCbIface, mGnssNiCbIface);
  127. api->gnssEnable(LOCATION_TECHNOLOGY_TYPE_GNSS);
  128. api->requestCapabilities();
  129. }
  130. return true;
  131. }
  132. Return<bool> Gnss::setGnssNiCb(const sp<IGnssNiCallback>& callback) {
  133. ENTRY_LOG_CALLFLOW();
  134. mGnssNiCbIface = callback;
  135. GnssAPIClient* api = getApi();
  136. if (api != nullptr) {
  137. api->gnssUpdateCallbacks(mGnssCbIface, mGnssNiCbIface);
  138. }
  139. return true;
  140. }
  141. Return<bool> Gnss::updateConfiguration(GnssConfig& gnssConfig) {
  142. ENTRY_LOG_CALLFLOW();
  143. GnssAPIClient* api = getApi();
  144. if (api) {
  145. api->gnssConfigurationUpdate(gnssConfig);
  146. } else if (gnssConfig.flags != 0) {
  147. // api is not ready yet, update mPendingConfig with gnssConfig
  148. mPendingConfig.size = sizeof(GnssConfig);
  149. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_GPS_LOCK_VALID_BIT) {
  150. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_GPS_LOCK_VALID_BIT;
  151. mPendingConfig.gpsLock = gnssConfig.gpsLock;
  152. }
  153. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SUPL_VERSION_VALID_BIT) {
  154. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SUPL_VERSION_VALID_BIT;
  155. mPendingConfig.suplVersion = gnssConfig.suplVersion;
  156. }
  157. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SET_ASSISTANCE_DATA_VALID_BIT) {
  158. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SET_ASSISTANCE_DATA_VALID_BIT;
  159. mPendingConfig.assistanceServer.size = sizeof(GnssConfigSetAssistanceServer);
  160. mPendingConfig.assistanceServer.type = gnssConfig.assistanceServer.type;
  161. if (mPendingConfig.assistanceServer.hostName != nullptr) {
  162. free((void*)mPendingConfig.assistanceServer.hostName);
  163. mPendingConfig.assistanceServer.hostName =
  164. strdup(gnssConfig.assistanceServer.hostName);
  165. }
  166. mPendingConfig.assistanceServer.port = gnssConfig.assistanceServer.port;
  167. }
  168. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_LPP_PROFILE_VALID_BIT) {
  169. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_LPP_PROFILE_VALID_BIT;
  170. mPendingConfig.lppProfileMask = gnssConfig.lppProfileMask;
  171. }
  172. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_LPPE_CONTROL_PLANE_VALID_BIT) {
  173. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_LPPE_CONTROL_PLANE_VALID_BIT;
  174. mPendingConfig.lppeControlPlaneMask = gnssConfig.lppeControlPlaneMask;
  175. }
  176. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_LPPE_USER_PLANE_VALID_BIT) {
  177. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_LPPE_USER_PLANE_VALID_BIT;
  178. mPendingConfig.lppeUserPlaneMask = gnssConfig.lppeUserPlaneMask;
  179. }
  180. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_AGLONASS_POSITION_PROTOCOL_VALID_BIT) {
  181. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_AGLONASS_POSITION_PROTOCOL_VALID_BIT;
  182. mPendingConfig.aGlonassPositionProtocolMask = gnssConfig.aGlonassPositionProtocolMask;
  183. }
  184. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_EM_PDN_FOR_EM_SUPL_VALID_BIT) {
  185. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_EM_PDN_FOR_EM_SUPL_VALID_BIT;
  186. mPendingConfig.emergencyPdnForEmergencySupl = gnssConfig.emergencyPdnForEmergencySupl;
  187. }
  188. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SUPL_EM_SERVICES_BIT) {
  189. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SUPL_EM_SERVICES_BIT;
  190. mPendingConfig.suplEmergencyServices = gnssConfig.suplEmergencyServices;
  191. }
  192. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SUPL_MODE_BIT) {
  193. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SUPL_MODE_BIT;
  194. mPendingConfig.suplModeMask = gnssConfig.suplModeMask;
  195. }
  196. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_BLACKLISTED_SV_IDS_BIT) {
  197. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_BLACKLISTED_SV_IDS_BIT;
  198. mPendingConfig.blacklistedSvIds = gnssConfig.blacklistedSvIds;
  199. }
  200. }
  201. return true;
  202. }
  203. Return<bool> Gnss::start() {
  204. ENTRY_LOG_CALLFLOW();
  205. bool retVal = false;
  206. GnssAPIClient* api = getApi();
  207. if (api) {
  208. retVal = api->gnssStart();
  209. }
  210. return retVal;
  211. }
  212. Return<bool> Gnss::stop() {
  213. ENTRY_LOG_CALLFLOW();
  214. bool retVal = false;
  215. GnssAPIClient* api = getApi();
  216. if (api) {
  217. retVal = api->gnssStop();
  218. }
  219. return retVal;
  220. }
  221. Return<void> Gnss::cleanup() {
  222. ENTRY_LOG_CALLFLOW();
  223. if (mApi != nullptr) {
  224. mApi->gnssDisable();
  225. }
  226. return Void();
  227. }
  228. Return<bool> Gnss::injectLocation(double latitudeDegrees,
  229. double longitudeDegrees,
  230. float accuracyMeters) {
  231. ENTRY_LOG_CALLFLOW();
  232. const GnssInterface* gnssInterface = getGnssInterface();
  233. if (nullptr != gnssInterface) {
  234. gnssInterface->injectLocation(latitudeDegrees, longitudeDegrees, accuracyMeters);
  235. return true;
  236. } else {
  237. return false;
  238. }
  239. }
  240. Return<bool> Gnss::injectTime(int64_t timeMs, int64_t timeReferenceMs,
  241. int32_t uncertaintyMs) {
  242. return true;
  243. }
  244. Return<void> Gnss::deleteAidingData(V1_0::IGnss::GnssAidingData aidingDataFlags) {
  245. ENTRY_LOG_CALLFLOW();
  246. GnssAPIClient* api = getApi();
  247. if (api) {
  248. api->gnssDeleteAidingData(aidingDataFlags);
  249. }
  250. return Void();
  251. }
  252. Return<bool> Gnss::setPositionMode(V1_0::IGnss::GnssPositionMode mode,
  253. V1_0::IGnss::GnssPositionRecurrence recurrence,
  254. uint32_t minIntervalMs,
  255. uint32_t preferredAccuracyMeters,
  256. uint32_t preferredTimeMs) {
  257. ENTRY_LOG_CALLFLOW();
  258. bool retVal = false;
  259. GnssAPIClient* api = getApi();
  260. if (api) {
  261. retVal = api->gnssSetPositionMode(mode, recurrence, minIntervalMs,
  262. preferredAccuracyMeters, preferredTimeMs);
  263. }
  264. return retVal;
  265. }
  266. Return<sp<V1_0::IAGnss>> Gnss::getExtensionAGnss() {
  267. ENTRY_LOG_CALLFLOW();
  268. mAGnssIface = new AGnss(this);
  269. return mAGnssIface;
  270. }
  271. Return<sp<V1_0::IGnssNi>> Gnss::getExtensionGnssNi() {
  272. ENTRY_LOG_CALLFLOW();
  273. mGnssNi = new GnssNi(this);
  274. return mGnssNi;
  275. }
  276. Return<sp<V1_0::IGnssMeasurement>> Gnss::getExtensionGnssMeasurement() {
  277. ENTRY_LOG_CALLFLOW();
  278. if (mGnssMeasurement == nullptr)
  279. mGnssMeasurement = new GnssMeasurement(mGnssMeasurement);
  280. return mGnssMeasurement;
  281. }
  282. Return<sp<V1_0::IGnssConfiguration>> Gnss::getExtensionGnssConfiguration() {
  283. ENTRY_LOG_CALLFLOW();
  284. mGnssConfig = new GnssConfiguration(this);
  285. return mGnssConfig;
  286. }
  287. Return<sp<V1_0::IGnssGeofencing>> Gnss::getExtensionGnssGeofencing() {
  288. ENTRY_LOG_CALLFLOW();
  289. mGnssGeofencingIface = new GnssGeofencing(mGnssGeofencingIface);
  290. return mGnssGeofencingIface;
  291. }
  292. Return<sp<V1_0::IGnssBatching>> Gnss::getExtensionGnssBatching() {
  293. mGnssBatching = new GnssBatching(mGnssBatching);
  294. return mGnssBatching;
  295. }
  296. Return<sp<V1_0::IGnssDebug>> Gnss::getExtensionGnssDebug() {
  297. ENTRY_LOG_CALLFLOW();
  298. mGnssDebug = new GnssDebug(this);
  299. return mGnssDebug;
  300. }
  301. Return<sp<V1_0::IAGnssRil>> Gnss::getExtensionAGnssRil() {
  302. mGnssRil = new AGnssRil(this);
  303. return mGnssRil;
  304. }
  305. IGnss* HIDL_FETCH_IGnss(const char* hal) {
  306. ENTRY_LOG_CALLFLOW();
  307. IGnss* iface = nullptr;
  308. iface = new Gnss();
  309. if (iface == nullptr) {
  310. LOC_LOGE("%s]: failed to get %s", __FUNCTION__, hal);
  311. }
  312. return iface;
  313. }
  314. } // namespace implementation
  315. } // namespace V1_0
  316. } // namespace gnss
  317. } // namespace hardware
  318. } // namespace android