GnssBatching.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2017-2019, 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_GnssBatchingInterface"
  21. #include <log_util.h>
  22. #include <BatchingAPIClient.h>
  23. #include "GnssBatching.h"
  24. namespace android {
  25. namespace hardware {
  26. namespace gnss {
  27. namespace V2_0 {
  28. namespace implementation {
  29. void GnssBatching::GnssBatchingDeathRecipient::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 gnssBatching = mGnssBatching.promote();
  34. if (gnssBatching != nullptr) {
  35. gnssBatching->handleClientDeath();
  36. }
  37. }
  38. GnssBatching::~GnssBatching() {
  39. if (mApi != nullptr) {
  40. mApi->destroy();
  41. mApi = nullptr;
  42. }
  43. }
  44. GnssBatching::handleClientDeath() {
  45. stop();
  46. cleanup();
  47. if (mApi != nullptr) {
  48. mApi->gnssUpdateCallbacks_2_0(nullptr);
  49. mApi->gnssUpdateCallbacks(nullptr);
  50. }
  51. mGnssBatchingCbIface_2_0 = nullptr;
  52. mGnssBatchingCbIface = nullptr;
  53. }
  54. // Methods from ::android::hardware::gnss::V1_0::IGnssBatching follow.
  55. Return<bool> GnssBatching::init(const sp<V1_0::IGnssBatchingCallback>& callback) {
  56. if (mGnssBatchingDeathRecipient == nullptr) {
  57. mGnssBatchingDeathRecipient = new GnssBatchingDeathRecipient(mSelf);
  58. }
  59. if (mApi != nullptr) {
  60. mApi->gnssUpdateCallbacks(callback);
  61. } else {
  62. mApi = new BatchingAPIClient(callback);
  63. }
  64. if (mGnssBatchingCbIface != nullptr) {
  65. mGnssBatchingCbIface->unlinkToDeath(mGnssBatchingDeathRecipient);
  66. }
  67. mGnssBatchingCbIface = callback;
  68. if (mGnssBatchingCbIface != nullptr) {
  69. mGnssBatchingCbIface->linkToDeath(mGnssBatchingDeathRecipient, 0 /*cookie*/);
  70. }
  71. return true;
  72. }
  73. Return<uint16_t> GnssBatching::getBatchSize() {
  74. uint16_t ret = 0;
  75. if (mApi == nullptr) {
  76. LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
  77. } else {
  78. ret = mApi->getBatchSize();
  79. }
  80. return ret;
  81. }
  82. Return<bool> GnssBatching::start(const IGnssBatching::Options& options) {
  83. bool ret = false;
  84. if (mApi == nullptr) {
  85. LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
  86. } else {
  87. ret = mApi->startSession(options);
  88. }
  89. return ret;
  90. }
  91. Return<void> GnssBatching::flush() {
  92. if (mApi == nullptr) {
  93. LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
  94. } else {
  95. mApi->flushBatchedLocations();
  96. }
  97. return Void();
  98. }
  99. Return<bool> GnssBatching::stop() {
  100. bool ret = false;
  101. if (mApi == nullptr) {
  102. LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
  103. } else {
  104. ret = mApi->stopSession();
  105. }
  106. return ret;
  107. }
  108. Return<void> GnssBatching::cleanup() {
  109. if (mApi != nullptr) {
  110. mApi->stopSession();
  111. }
  112. if (mGnssBatchingCbIface != nullptr) {
  113. mGnssBatchingCbIface->unlinkToDeath(mGnssBatchingDeathRecipient);
  114. mGnssBatchingCbIface = nullptr;
  115. }
  116. if (mGnssBatchingCbIface_2_0 != nullptr) {
  117. mGnssBatchingCbIface_2_0->unlinkToDeath(mGnssBatchingDeathRecipient);
  118. mGnssBatchingCbIface_2_0 = nullptr;
  119. }
  120. return Void();
  121. }
  122. // Methods from ::android::hardware::gnss::V2_0::IGnssBatching follow.
  123. Return<bool> GnssBatching::init_2_0(const sp<V2_0::IGnssBatchingCallback>& callback) {
  124. if (mGnssBatchingDeathRecipient == nullptr) {
  125. mGnssBatchingDeathRecipient = new GnssBatchingDeathRecipient(mSelf);
  126. }
  127. if (mApi != nullptr) {
  128. mApi->gnssUpdateCallbacks_2_0(callback);
  129. } else {
  130. mApi = new BatchingAPIClient(callback);
  131. }
  132. if (mGnssBatchingCbIface_2_0 != nullptr) {
  133. mGnssBatchingCbIface_2_0->unlinkToDeath(mGnssBatchingDeathRecipient);
  134. }
  135. mGnssBatchingCbIface_2_0 = callback;
  136. if (mGnssBatchingCbIface_2_0 != nullptr) {
  137. mGnssBatchingCbIface_2_0->linkToDeath(mGnssBatchingDeathRecipient, 0 /*cookie*/);
  138. }
  139. return true;
  140. }
  141. } // namespace implementation
  142. } // namespace V2_0
  143. } // namespace gnss
  144. } // namespace hardware
  145. } // namespace android