Gnss.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2021, The Linux Foundation. All rights reserved.
  3. * Not a Contribution
  4. */
  5. /*
  6. * Copyright (C) 2020 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 "Gnss.h"
  23. #include <log_util.h>
  24. #include <android/binder_auto_utils.h>
  25. namespace android {
  26. namespace hardware {
  27. namespace gnss {
  28. namespace aidl {
  29. namespace implementation {
  30. void gnssCallbackDied(void* cookie) {
  31. LOC_LOGe("IGnssCallback AIDL service died");
  32. Gnss* iface = static_cast<Gnss*>(cookie);
  33. if (iface != nullptr) {
  34. iface->handleClientDeath();
  35. }
  36. }
  37. ndk::ScopedAStatus Gnss::setCallback(const std::shared_ptr<IGnssCallback>& callback) {
  38. if (callback == nullptr) {
  39. LOC_LOGe("Null callback ignored");
  40. return ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
  41. }
  42. mGnssCallback = callback;
  43. GnssAPIClient* api = getApi();
  44. if (mGnssCallback != nullptr) {
  45. AIBinder_DeathRecipient* recipient = AIBinder_DeathRecipient_new(&gnssCallbackDied);
  46. AIBinder_linkToDeath(callback->asBinder().get(), recipient, this);
  47. }
  48. if (api != nullptr) {
  49. api->gnssUpdateCallbacks(callback);
  50. api->gnssEnable(LOCATION_TECHNOLOGY_TYPE_GNSS);
  51. api->requestCapabilities();
  52. }
  53. return ndk::ScopedAStatus::ok();
  54. }
  55. ndk::ScopedAStatus Gnss::close() {
  56. if (mApi != nullptr) {
  57. mApi->gnssDisable();
  58. }
  59. return ndk::ScopedAStatus::ok();
  60. }
  61. Gnss::Gnss(): mGnssCallback(nullptr) {
  62. memset(&mPendingConfig, 0, sizeof(GnssConfig));
  63. ENTRY_LOG_CALLFLOW();
  64. }
  65. Gnss::~Gnss() {
  66. ENTRY_LOG_CALLFLOW();
  67. if (mApi != nullptr) {
  68. mApi->destroy();
  69. mApi = nullptr;
  70. }
  71. }
  72. void Gnss::handleClientDeath() {
  73. close();
  74. if (mApi != nullptr) {
  75. mApi->gnssUpdateCallbacks(mGnssCallback);
  76. }
  77. mGnssCallback = nullptr;
  78. }
  79. ndk::ScopedAStatus Gnss::updateConfiguration(GnssConfig& gnssConfig) {
  80. ENTRY_LOG_CALLFLOW();
  81. GnssAPIClient* api = getApi();
  82. if (api) {
  83. api->gnssConfigurationUpdate(gnssConfig);
  84. } else if (gnssConfig.flags != 0) {
  85. // api is not ready yet, update mPendingConfig with gnssConfig
  86. mPendingConfig.size = sizeof(GnssConfig);
  87. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_GPS_LOCK_VALID_BIT) {
  88. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_GPS_LOCK_VALID_BIT;
  89. mPendingConfig.gpsLock = gnssConfig.gpsLock;
  90. }
  91. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SUPL_VERSION_VALID_BIT) {
  92. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SUPL_VERSION_VALID_BIT;
  93. mPendingConfig.suplVersion = gnssConfig.suplVersion;
  94. }
  95. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SET_ASSISTANCE_DATA_VALID_BIT) {
  96. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SET_ASSISTANCE_DATA_VALID_BIT;
  97. mPendingConfig.assistanceServer.size = sizeof(GnssConfigSetAssistanceServer);
  98. mPendingConfig.assistanceServer.type = gnssConfig.assistanceServer.type;
  99. if (mPendingConfig.assistanceServer.hostName != nullptr) {
  100. free((void*)mPendingConfig.assistanceServer.hostName);
  101. mPendingConfig.assistanceServer.hostName =
  102. strdup(gnssConfig.assistanceServer.hostName);
  103. }
  104. mPendingConfig.assistanceServer.port = gnssConfig.assistanceServer.port;
  105. }
  106. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_LPP_PROFILE_VALID_BIT) {
  107. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_LPP_PROFILE_VALID_BIT;
  108. mPendingConfig.lppProfileMask = gnssConfig.lppProfileMask;
  109. }
  110. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_LPPE_CONTROL_PLANE_VALID_BIT) {
  111. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_LPPE_CONTROL_PLANE_VALID_BIT;
  112. mPendingConfig.lppeControlPlaneMask = gnssConfig.lppeControlPlaneMask;
  113. }
  114. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_LPPE_USER_PLANE_VALID_BIT) {
  115. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_LPPE_USER_PLANE_VALID_BIT;
  116. mPendingConfig.lppeUserPlaneMask = gnssConfig.lppeUserPlaneMask;
  117. }
  118. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_AGLONASS_POSITION_PROTOCOL_VALID_BIT) {
  119. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_AGLONASS_POSITION_PROTOCOL_VALID_BIT;
  120. mPendingConfig.aGlonassPositionProtocolMask = gnssConfig.aGlonassPositionProtocolMask;
  121. }
  122. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_EM_PDN_FOR_EM_SUPL_VALID_BIT) {
  123. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_EM_PDN_FOR_EM_SUPL_VALID_BIT;
  124. mPendingConfig.emergencyPdnForEmergencySupl = gnssConfig.emergencyPdnForEmergencySupl;
  125. }
  126. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SUPL_EM_SERVICES_BIT) {
  127. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SUPL_EM_SERVICES_BIT;
  128. mPendingConfig.suplEmergencyServices = gnssConfig.suplEmergencyServices;
  129. }
  130. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_SUPL_MODE_BIT) {
  131. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_SUPL_MODE_BIT;
  132. mPendingConfig.suplModeMask = gnssConfig.suplModeMask;
  133. }
  134. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_BLACKLISTED_SV_IDS_BIT) {
  135. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_BLACKLISTED_SV_IDS_BIT;
  136. mPendingConfig.blacklistedSvIds = gnssConfig.blacklistedSvIds;
  137. }
  138. if (gnssConfig.flags & GNSS_CONFIG_FLAGS_EMERGENCY_EXTENSION_SECONDS_BIT) {
  139. mPendingConfig.flags |= GNSS_CONFIG_FLAGS_EMERGENCY_EXTENSION_SECONDS_BIT;
  140. mPendingConfig.emergencyExtensionSeconds = gnssConfig.emergencyExtensionSeconds;
  141. }
  142. }
  143. return ndk::ScopedAStatus::ok();
  144. }
  145. ::ndk::ScopedAStatus Gnss::getExtensionGnssConfiguration(
  146. std::shared_ptr<::aidl::android::hardware::gnss::IGnssConfiguration>* _aidl_return) {
  147. ENTRY_LOG_CALLFLOW();
  148. if (mGnssConfiguration == nullptr) {
  149. mGnssConfiguration = SharedRefBase::make<GnssConfiguration>(this);
  150. }
  151. *_aidl_return = mGnssConfiguration;
  152. return ndk::ScopedAStatus::ok();
  153. }
  154. ::ndk::ScopedAStatus Gnss::getExtensionGnssPowerIndication(
  155. std::shared_ptr<::aidl::android::hardware::gnss::IGnssPowerIndication>* _aidl_return) {
  156. ENTRY_LOG_CALLFLOW();
  157. if (mGnssPowerIndication == nullptr) {
  158. mGnssPowerIndication = SharedRefBase::make<GnssPowerIndication>();
  159. }
  160. *_aidl_return = mGnssPowerIndication;
  161. return ndk::ScopedAStatus::ok();
  162. }
  163. ::ndk::ScopedAStatus Gnss::getExtensionGnssMeasurement(
  164. std::shared_ptr<::aidl::android::hardware::gnss::IGnssMeasurementInterface>* _aidl_return) {
  165. ENTRY_LOG_CALLFLOW();
  166. if (mGnssMeasurementInterface == nullptr) {
  167. mGnssMeasurementInterface = SharedRefBase::make<GnssMeasurementInterface>();
  168. }
  169. *_aidl_return = mGnssMeasurementInterface;
  170. return ndk::ScopedAStatus::ok();
  171. }
  172. GnssAPIClient* Gnss::getApi() {
  173. if (mApi != nullptr) {
  174. return mApi;
  175. }
  176. if (mGnssCallback != nullptr) {
  177. mApi = new GnssAPIClient(mGnssCallback);
  178. } else {
  179. LOC_LOGW("%s] GnssAPIClient is not ready", __FUNCTION__);
  180. return mApi;
  181. }
  182. if (mPendingConfig.size == sizeof(GnssConfig)) {
  183. // we have pending GnssConfig
  184. mApi->gnssConfigurationUpdate(mPendingConfig);
  185. // clear size to invalid mPendingConfig
  186. mPendingConfig.size = 0;
  187. if (mPendingConfig.assistanceServer.hostName != nullptr) {
  188. free((void*)mPendingConfig.assistanceServer.hostName);
  189. }
  190. }
  191. return mApi;
  192. }
  193. } // namespace implementation
  194. } // namespace aidl
  195. } // namespace gnss
  196. } // namespace hardware
  197. } // namespace android