GnssMeasurement.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2017-2018, 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_GnssMeasurementInterface"
  21. #include <log_util.h>
  22. #include <MeasurementAPIClient.h>
  23. #include "GnssMeasurement.h"
  24. namespace android {
  25. namespace hardware {
  26. namespace gnss {
  27. namespace V1_1 {
  28. namespace implementation {
  29. void GnssMeasurement::GnssMeasurementDeathRecipient::serviceDied(
  30. uint64_t cookie, const wp<IBase>& who) {
  31. LOC_LOGE("%s] service died. cookie: %llu, who: %p",
  32. __FUNCTION__, static_cast<unsigned long long>(cookie), &who);
  33. auto gssMeasurement = mGnssMeasurement.promote();
  34. if (gssMeasurement != nullptr) {
  35. gssMeasurement->handleClientDeath();
  36. }
  37. }
  38. GnssMeasurement::GnssMeasurement(const sp<GnssMeasurement>& self) :
  39. mSelf(self), mApi(new MeasurementAPIClient()) {
  40. }
  41. GnssMeasurement::~GnssMeasurement() {
  42. if (mApi) {
  43. mApi->destroy();
  44. mApi = nullptr;
  45. }
  46. }
  47. void GnssMeasurement::handleClientDeath() {
  48. close();
  49. if (mApi != nullptr) {
  50. mApi->measurementSetCallback(nullptr);
  51. mApi->measurementSetCallback_1_1(nullptr);
  52. }
  53. mGnssMeasurementCbIface = nullptr;
  54. mGnssMeasurementCbIface_1_1 = nullptr;
  55. }
  56. // Methods from ::android::hardware::gnss::V1_0::IGnssMeasurement follow.
  57. Return<IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
  58. const sp<V1_0::IGnssMeasurementCallback>& callback) {
  59. Return<IGnssMeasurement::GnssMeasurementStatus> ret =
  60. IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
  61. if (mGnssMeasurementCbIface != nullptr) {
  62. LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
  63. return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
  64. }
  65. if (callback == nullptr) {
  66. LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
  67. return ret;
  68. }
  69. if (mApi == nullptr) {
  70. LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
  71. return ret;
  72. }
  73. mGnssMeasurementCbIface = callback;
  74. if (mGnssMeasurementDeathRecipient == nullptr) {
  75. mGnssMeasurementDeathRecipient = new GnssMeasurementDeathRecipient(mSelf);
  76. }
  77. mGnssMeasurementCbIface->linkToDeath(mGnssMeasurementDeathRecipient, 0);
  78. return mApi->measurementSetCallback(callback);
  79. }
  80. Return<void> GnssMeasurement::close() {
  81. if (mApi == nullptr) {
  82. LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
  83. return Void();
  84. }
  85. if (mGnssMeasurementCbIface != nullptr) {
  86. mGnssMeasurementCbIface->unlinkToDeath(mGnssMeasurementDeathRecipient);
  87. mGnssMeasurementCbIface = nullptr;
  88. }
  89. if (mGnssMeasurementCbIface_1_1 != nullptr) {
  90. mGnssMeasurementCbIface_1_1->unlinkToDeath(mGnssMeasurementDeathRecipient);
  91. mGnssMeasurementCbIface_1_1 = nullptr;
  92. }
  93. mApi->measurementClose();
  94. return Void();
  95. }
  96. // Methods from ::android::hardware::gnss::V1_1::IGnssMeasurement follow.
  97. Return<GnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_1_1(
  98. const sp<IGnssMeasurementCallback>& callback, bool enableFullTracking) {
  99. Return<IGnssMeasurement::GnssMeasurementStatus> ret =
  100. IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
  101. if (mGnssMeasurementCbIface_1_1 != nullptr) {
  102. LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
  103. return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
  104. }
  105. if (callback == nullptr) {
  106. LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
  107. return ret;
  108. }
  109. if (nullptr == mApi) {
  110. LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
  111. return ret;
  112. }
  113. mGnssMeasurementCbIface_1_1 = callback;
  114. if (mGnssMeasurementDeathRecipient == nullptr) {
  115. mGnssMeasurementDeathRecipient = new GnssMeasurementDeathRecipient(mSelf);
  116. }
  117. mGnssMeasurementCbIface_1_1->linkToDeath(mGnssMeasurementDeathRecipient, 0);
  118. GnssPowerMode powerMode = enableFullTracking?
  119. GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2;
  120. return mApi->measurementSetCallback_1_1(callback, powerMode);
  121. }
  122. } // namespace implementation
  123. } // namespace V1_1
  124. } // namespace gnss
  125. } // namespace hardware
  126. } // namespace android