GnssBatching.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_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 V1_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(nullptr);
  49. }
  50. mGnssBatchingCbIface = nullptr;
  51. }
  52. // Methods from ::android::hardware::gnss::V1_0::IGnssBatching follow.
  53. Return<bool> GnssBatching::init(const sp<IGnssBatchingCallback>& callback) {
  54. if (mGnssBatchingDeathRecipient == nullptr) {
  55. mGnssBatchingDeathRecipient = new GnssBatchingDeathRecipient(mSelf);
  56. }
  57. if (mApi != nullptr) {
  58. mApi->gnssUpdateCallbacks(callback);
  59. } else {
  60. mApi = new BatchingAPIClient(callback);
  61. }
  62. if (mGnssBatchingCbIface != nullptr) {
  63. mGnssBatchingCbIface->unlinkToDeath(mGnssBatchingDeathRecipient);
  64. }
  65. mGnssBatchingCbIface = callback;
  66. if (mGnssBatchingCbIface != nullptr) {
  67. mGnssBatchingCbIface->linkToDeath(mGnssBatchingDeathRecipient, 0 /*cookie*/);
  68. }
  69. return true;
  70. }
  71. Return<uint16_t> GnssBatching::getBatchSize() {
  72. uint16_t ret = 0;
  73. if (mApi == nullptr) {
  74. LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
  75. } else {
  76. ret = mApi->getBatchSize();
  77. }
  78. return ret;
  79. }
  80. Return<bool> GnssBatching::start(const IGnssBatching::Options& options) {
  81. bool ret = false;
  82. if (mApi == nullptr) {
  83. LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
  84. } else {
  85. ret = mApi->startSession(options);
  86. }
  87. return ret;
  88. }
  89. Return<void> GnssBatching::flush() {
  90. if (mApi == nullptr) {
  91. LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
  92. } else {
  93. mApi->flushBatchedLocations();
  94. }
  95. return Void();
  96. }
  97. Return<bool> GnssBatching::stop() {
  98. bool ret = false;
  99. if (mApi == nullptr) {
  100. LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
  101. } else {
  102. ret = mApi->stopSession();
  103. }
  104. return ret;
  105. }
  106. Return<void> GnssBatching::cleanup() {
  107. if (mGnssBatchingCbIface != nullptr) {
  108. mGnssBatchingCbIface->unlinkToDeath(mGnssBatchingDeathRecipient);
  109. }
  110. return Void();
  111. }
  112. } // namespace implementation
  113. } // namespace V1_0
  114. } // namespace gnss
  115. } // namespace hardware
  116. } // namespace android