MeasurementCorrections.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above
  10. * copyright notice, this list of conditions and the following
  11. * disclaimer in the documentation and/or other materials provided
  12. * with the distribution.
  13. * * Neither the name of The Linux Foundation nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  24. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  26. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  27. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #define LOG_TAG "LocSvc_MeasurementCorrectionsInterface"
  30. #include <log_util.h>
  31. #include "MeasurementCorrections.h"
  32. namespace android {
  33. namespace hardware {
  34. namespace gnss {
  35. namespace measurement_corrections {
  36. namespace V1_1 {
  37. namespace implementation {
  38. using ::android::hardware::hidl_array;
  39. using ::android::hardware::hidl_memory;
  40. using ::android::hardware::hidl_string;
  41. using ::android::hardware::hidl_vec;
  42. using ::android::hardware::Return;
  43. using ::android::hardware::Void;
  44. using ::android::sp;
  45. using ::android::hardware::gnss::measurement_corrections::V1_0::IMeasurementCorrectionsCallback;
  46. using ::android::hardware::gnss::measurement_corrections::V1_0::IMeasurementCorrections;
  47. using MeasurementCorrectionsV1_0 =
  48. ::android::hardware::gnss::measurement_corrections::V1_0::MeasurementCorrections;
  49. using MeasurementCorrectionsV1_1 =
  50. ::android::hardware::gnss::measurement_corrections::V1_1::MeasurementCorrections;
  51. static MeasurementCorrections* spMeasurementCorrections = nullptr;
  52. void MeasurementCorrections::GnssMeasurementCorrectionsDeathRecipient::serviceDied(uint64_t cookie,
  53. const wp<IBase>& who) {
  54. LOC_LOGe("service died. cookie: %llu, who: %p", static_cast<unsigned long long>(cookie), &who);
  55. // Gnss::GnssDeathrecipient will stop the session
  56. // we inform the adapter that service has died
  57. if (nullptr == spMeasurementCorrections) {
  58. LOC_LOGe("spMeasurementCorrections is nullptr");
  59. return;
  60. }
  61. if (nullptr == spMeasurementCorrections->mGnss ||
  62. nullptr == spMeasurementCorrections->mGnss->getGnssInterface()) {
  63. LOC_LOGe("Null GNSS interface");
  64. return;
  65. }
  66. spMeasurementCorrections->mGnss->getGnssInterface()->measCorrClose();
  67. }
  68. MeasurementCorrections::MeasurementCorrections(Gnss* gnss) : mGnss(gnss) {
  69. mGnssMeasurementCorrectionsDeathRecipient = new GnssMeasurementCorrectionsDeathRecipient();
  70. spMeasurementCorrections = this;
  71. }
  72. MeasurementCorrections::~MeasurementCorrections() {
  73. spMeasurementCorrections = nullptr;
  74. }
  75. void MeasurementCorrections::measCorrSetCapabilitiesCb(
  76. GnssMeasurementCorrectionsCapabilitiesMask capabilities) {
  77. if (nullptr != spMeasurementCorrections) {
  78. spMeasurementCorrections->setCapabilitiesCb(capabilities);
  79. }
  80. }
  81. void MeasurementCorrections::setCapabilitiesCb(
  82. GnssMeasurementCorrectionsCapabilitiesMask capabilities) {
  83. std::unique_lock<std::mutex> lock(mMutex);
  84. auto measCorrCbIface(mMeasurementCorrectionsCbIface);
  85. lock.unlock();
  86. if (measCorrCbIface != nullptr) {
  87. uint32_t measCorrCapabilities = 0;
  88. // Convert from one enum to another
  89. if (capabilities & GNSS_MEAS_CORR_LOS_SATS) {
  90. measCorrCapabilities |=
  91. IMeasurementCorrectionsCallback::Capabilities::LOS_SATS;
  92. }
  93. if (capabilities & GNSS_MEAS_CORR_EXCESS_PATH_LENGTH) {
  94. measCorrCapabilities |=
  95. IMeasurementCorrectionsCallback::Capabilities::EXCESS_PATH_LENGTH;
  96. }
  97. if (capabilities & GNSS_MEAS_CORR_REFLECTING_PLANE) {
  98. measCorrCapabilities |=
  99. IMeasurementCorrectionsCallback::Capabilities::REFLECTING_PLANE;
  100. }
  101. auto r = measCorrCbIface->setCapabilitiesCb(measCorrCapabilities);
  102. if (!r.isOk()) {
  103. LOC_LOGw("Error invoking setCapabilitiesCb %s", r.description().c_str());
  104. }
  105. } else {
  106. LOC_LOGw("setCallback has not been called yet");
  107. }
  108. }
  109. Return<bool> MeasurementCorrections::setCorrections(
  110. const MeasurementCorrectionsV1_0& corrections) {
  111. GnssMeasurementCorrections gnssMeasurementCorrections = {};
  112. V2_1::implementation::convertMeasurementCorrections(corrections, gnssMeasurementCorrections);
  113. return mGnss->getGnssInterface()->measCorrSetCorrections(gnssMeasurementCorrections);
  114. }
  115. Return<bool> MeasurementCorrections::setCorrections_1_1(
  116. const MeasurementCorrectionsV1_1& corrections) {
  117. GnssMeasurementCorrections gnssMeasurementCorrections = {};
  118. V2_1::implementation::convertMeasurementCorrections(
  119. corrections.v1_0, gnssMeasurementCorrections);
  120. gnssMeasurementCorrections.hasEnvironmentBearing = corrections.hasEnvironmentBearing;
  121. gnssMeasurementCorrections.environmentBearingDegrees =
  122. corrections.environmentBearingDegrees;
  123. gnssMeasurementCorrections.environmentBearingUncertaintyDegrees =
  124. corrections.environmentBearingUncertaintyDegrees;
  125. for (int i = 0; i < corrections.satCorrections.size(); i++) {
  126. GnssSingleSatCorrection gnssSingleSatCorrection = {};
  127. V2_1::implementation::convertSingleSatCorrections(
  128. corrections.satCorrections[i].v1_0, gnssSingleSatCorrection);
  129. switch (corrections.satCorrections[i].constellation) {
  130. case (::android::hardware::gnss::V2_0::GnssConstellationType::GPS):
  131. gnssSingleSatCorrection.svType = GNSS_SV_TYPE_GPS;
  132. break;
  133. case (::android::hardware::gnss::V2_0::GnssConstellationType::SBAS):
  134. gnssSingleSatCorrection.svType = GNSS_SV_TYPE_SBAS;
  135. break;
  136. case (::android::hardware::gnss::V2_0::GnssConstellationType::GLONASS):
  137. gnssSingleSatCorrection.svType = GNSS_SV_TYPE_GLONASS;
  138. break;
  139. case (::android::hardware::gnss::V2_0::GnssConstellationType::QZSS):
  140. gnssSingleSatCorrection.svType = GNSS_SV_TYPE_QZSS;
  141. break;
  142. case (::android::hardware::gnss::V2_0::GnssConstellationType::BEIDOU):
  143. gnssSingleSatCorrection.svType = GNSS_SV_TYPE_BEIDOU;
  144. break;
  145. case (::android::hardware::gnss::V2_0::GnssConstellationType::GALILEO):
  146. gnssSingleSatCorrection.svType = GNSS_SV_TYPE_GALILEO;
  147. break;
  148. case (::android::hardware::gnss::V2_0::GnssConstellationType::IRNSS):
  149. gnssSingleSatCorrection.svType = GNSS_SV_TYPE_NAVIC;
  150. break;
  151. case (::android::hardware::gnss::V2_0::GnssConstellationType::UNKNOWN):
  152. default:
  153. gnssSingleSatCorrection.svType = GNSS_SV_TYPE_UNKNOWN;
  154. break;
  155. }
  156. gnssMeasurementCorrections.satCorrections.push_back(gnssSingleSatCorrection);
  157. }
  158. return mGnss->getGnssInterface()->measCorrSetCorrections(gnssMeasurementCorrections);
  159. }
  160. Return<bool> MeasurementCorrections::setCallback(
  161. const sp<V1_0::IMeasurementCorrectionsCallback>& callback) {
  162. if (nullptr == mGnss || nullptr == mGnss->getGnssInterface()) {
  163. LOC_LOGe("Null GNSS interface");
  164. return false;
  165. }
  166. std::unique_lock<std::mutex> lock(mMutex);
  167. mMeasurementCorrectionsCbIface = callback;
  168. lock.unlock();
  169. return mGnss->getGnssInterface()->measCorrInit(measCorrSetCapabilitiesCb);
  170. }
  171. } // namespace implementation
  172. } // namespace V1_1
  173. } // namespace measurement_corrections
  174. } // namespace gnss
  175. } // namespace hardware
  176. } // namespace android