GnssMeasurement.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_0 {
  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. }
  52. mGnssMeasurementCbIface = nullptr;
  53. }
  54. // Methods from ::android::hardware::gnss::V1_0::IGnssMeasurement follow.
  55. Return<IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
  56. const sp<V1_0::IGnssMeasurementCallback>& callback) {
  57. Return<IGnssMeasurement::GnssMeasurementStatus> ret =
  58. IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
  59. if (mGnssMeasurementCbIface != nullptr) {
  60. LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
  61. return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
  62. }
  63. if (callback == nullptr) {
  64. LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
  65. return ret;
  66. }
  67. if (mApi == nullptr) {
  68. LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
  69. return ret;
  70. }
  71. mGnssMeasurementCbIface = callback;
  72. if (mGnssMeasurementDeathRecipient == nullptr) {
  73. mGnssMeasurementDeathRecipient = new GnssMeasurementDeathRecipient(mSelf);
  74. }
  75. mGnssMeasurementCbIface->linkToDeath(mGnssMeasurementDeathRecipient, 0);
  76. return mApi->measurementSetCallback(callback);
  77. }
  78. Return<void> GnssMeasurement::close() {
  79. if (mApi == nullptr) {
  80. LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
  81. return Void();
  82. }
  83. if (mGnssMeasurementCbIface != nullptr) {
  84. mGnssMeasurementCbIface->unlinkToDeath(mGnssMeasurementDeathRecipient);
  85. mGnssMeasurementCbIface = nullptr;
  86. }
  87. mApi->measurementClose();
  88. return Void();
  89. }
  90. } // namespace implementation
  91. } // namespace V1_0
  92. } // namespace gnss
  93. } // namespace hardware
  94. } // namespace android