GnssGeofencing.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "GnssHal_GnssGeofencing"
  21. #include <log_util.h>
  22. #include <GeofenceAPIClient.h>
  23. #include "GnssGeofencing.h"
  24. namespace android {
  25. namespace hardware {
  26. namespace gnss {
  27. namespace V2_0 {
  28. namespace implementation {
  29. void GnssGeofencing::GnssGeofencingDeathRecipient::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 gnssGeofencing = mGnssGeofencing.promote();
  34. if (gnssGeofencing != nullptr) {
  35. gnssGeofencing->handleClientDeath();
  36. }
  37. }
  38. GnssGeofencing::~GnssGeofencing() {
  39. if (mApi != nullptr) {
  40. mApi->destroy();
  41. mApi = nullptr;
  42. }
  43. }
  44. void GnssGeofencing::handleClientDeath() {
  45. removeAllGeofences();
  46. if (mApi != nullptr) {
  47. mApi->upcateCallback(nullptr);
  48. }
  49. mGnssGeofencingCbIface = nullptr;
  50. }
  51. // Methods from ::android::hardware::gnss::V1_0::IGnssGeofencing follow.
  52. Return<void> GnssGeofencing::setCallback(const sp<IGnssGeofenceCallback>& callback) {
  53. if (mGnssGeofencingDeathRecipient == nullptr) {
  54. mGnssGeofencingDeathRecipient = new GnssGeofencingDeathRecipient(mSelf);
  55. }
  56. if (mApi != nullptr) {
  57. mApi->upcateCallback(callback);
  58. } else {
  59. mApi = new GeofenceAPIClient(callback);
  60. }
  61. if (mApi == nullptr) {
  62. LOC_LOGE("%s]: failed to create mApi", __FUNCTION__);
  63. }
  64. if (mGnssGeofencingCbIface != nullptr) {
  65. mGnssGeofencingCbIface->unlinkToDeath(mGnssGeofencingDeathRecipient);
  66. }
  67. mGnssGeofencingCbIface = callback;
  68. if (mGnssGeofencingCbIface != nullptr) {
  69. mGnssGeofencingCbIface->linkToDeath(mGnssGeofencingDeathRecipient, 0 /*cookie*/);
  70. }
  71. return Void();
  72. }
  73. Return<void> GnssGeofencing::addGeofence(
  74. int32_t geofenceId,
  75. double latitudeDegrees,
  76. double longitudeDegrees,
  77. double radiusMeters,
  78. IGnssGeofenceCallback::GeofenceTransition lastTransition,
  79. int32_t monitorTransitions,
  80. uint32_t notificationResponsivenessMs,
  81. uint32_t unknownTimerMs) {
  82. if (mApi == nullptr) {
  83. LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
  84. } else {
  85. mApi->geofenceAdd(
  86. geofenceId,
  87. latitudeDegrees,
  88. longitudeDegrees,
  89. radiusMeters,
  90. static_cast<int32_t>(lastTransition),
  91. monitorTransitions,
  92. notificationResponsivenessMs,
  93. unknownTimerMs);
  94. }
  95. return Void();
  96. }
  97. Return<void> GnssGeofencing::pauseGeofence(int32_t geofenceId) {
  98. if (mApi == nullptr) {
  99. LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
  100. } else {
  101. mApi->geofencePause(geofenceId);
  102. }
  103. return Void();
  104. }
  105. Return<void> GnssGeofencing::resumeGeofence(int32_t geofenceId, int32_t monitorTransitions) {
  106. if (mApi == nullptr) {
  107. LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
  108. } else {
  109. mApi->geofenceResume(geofenceId, monitorTransitions);
  110. }
  111. return Void();
  112. }
  113. Return<void> GnssGeofencing::removeGeofence(int32_t geofenceId) {
  114. if (mApi == nullptr) {
  115. LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
  116. } else {
  117. mApi->geofenceRemove(geofenceId);
  118. }
  119. return Void();
  120. }
  121. Return<void> GnssGeofencing::removeAllGeofences() {
  122. if (mApi == nullptr) {
  123. LOC_LOGD("%s]: mApi is nullptr, do nothing", __FUNCTION__);
  124. } else {
  125. mApi->geofenceRemoveAll();
  126. }
  127. return Void();
  128. }
  129. } // namespace implementation
  130. } // namespace V2_0
  131. } // namespace gnss
  132. } // namespace hardware
  133. } // namespace android