GnssPowerIndication.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2021, The Linux Foundation. All rights reserved.
  3. * Not a Contribution
  4. */
  5. /*
  6. * Copyright (C) 2020 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 "GnssPowerIndicationAidl"
  21. #include "GnssPowerIndication.h"
  22. #include <android/binder_auto_utils.h>
  23. #include <log_util.h>
  24. #include <inttypes.h>
  25. #include "loc_misc_utils.h"
  26. typedef const GnssInterface* (getLocationInterface)();
  27. namespace android {
  28. namespace hardware {
  29. namespace gnss {
  30. namespace aidl {
  31. namespace implementation {
  32. static GnssPowerIndication* spGnssPowerIndication = nullptr;
  33. GnssPowerIndication::GnssPowerIndication() :
  34. mDeathRecipient(AIBinder_DeathRecipient_new(GnssPowerIndication::gnssPowerIndicationDied)) {
  35. spGnssPowerIndication = this;
  36. }
  37. GnssPowerIndication::~GnssPowerIndication() {
  38. spGnssPowerIndication = nullptr;
  39. }
  40. ::ndk::ScopedAStatus GnssPowerIndication::setCallback(
  41. const std::shared_ptr<IGnssPowerIndicationCallback>& in_callback) {
  42. std::unique_lock<std::mutex> lock(mMutex);
  43. if (nullptr == in_callback) {
  44. LOC_LOGe("callback is nullptr");
  45. return ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
  46. }
  47. AIBinder_linkToDeath(in_callback->asBinder().get(), mDeathRecipient, this);
  48. mGnssPowerIndicationCb = in_callback;
  49. static bool getGnssInterfaceFailed = false;
  50. if (nullptr == mGnssInterface && !getGnssInterfaceFailed) {
  51. void * libHandle = nullptr;
  52. getLocationInterface* getter = (getLocationInterface*)
  53. dlGetSymFromLib(libHandle, "libgnss.so", "getGnssInterface");
  54. if (NULL == getter) {
  55. getGnssInterfaceFailed = true;
  56. } else {
  57. mGnssInterface = (GnssInterface*)(*getter)();
  58. }
  59. }
  60. if (nullptr != mGnssInterface) {
  61. mGnssInterface->powerIndicationInit(piGnssPowerIndicationCb);
  62. } else {
  63. LOC_LOGe("mGnssInterface is nullptr");
  64. return ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
  65. }
  66. lock.unlock();
  67. mGnssPowerIndicationCb->setCapabilitiesCb(IGnssPowerIndicationCallback::CAPABILITY_TOTAL);
  68. return ndk::ScopedAStatus::ok();
  69. }
  70. void GnssPowerIndication::cleanup() {
  71. LOC_LOGd("()");
  72. if (nullptr != mGnssPowerIndicationCb) {
  73. AIBinder_unlinkToDeath(mGnssPowerIndicationCb->asBinder().get(), mDeathRecipient, this);
  74. mGnssPowerIndicationCb = nullptr;
  75. }
  76. }
  77. void GnssPowerIndication::gnssPowerIndicationDied(void* cookie) {
  78. LOC_LOGe("IGnssPowerIndicationCallback service died");
  79. GnssPowerIndication* iface = static_cast<GnssPowerIndication*>(cookie);
  80. if (iface != nullptr) {
  81. iface->cleanup();
  82. }
  83. }
  84. ndk::ScopedAStatus GnssPowerIndication::requestGnssPowerStats() {
  85. LOC_LOGd("requestGnssPowerStats");
  86. std::unique_lock<std::mutex> lock(mMutex);
  87. if (nullptr != mGnssInterface) {
  88. lock.unlock();
  89. mGnssInterface->powerIndicationRequest();
  90. } else {
  91. LOC_LOGe("mGnssInterface is nullptr");
  92. }
  93. return ndk::ScopedAStatus::ok();
  94. }
  95. void GnssPowerIndication::piGnssPowerIndicationCb(GnssPowerStatistics gnssPowerStatistics) {
  96. if (nullptr != spGnssPowerIndication) {
  97. spGnssPowerIndication->gnssPowerIndicationCb(gnssPowerStatistics);
  98. } else {
  99. LOC_LOGe("spGnssPowerIndication is nullptr");
  100. }
  101. }
  102. void GnssPowerIndication::gnssPowerIndicationCb(GnssPowerStatistics gnssPowerStatistics) {
  103. GnssPowerStats gnssPowerStats = {};
  104. gnssPowerStats.elapsedRealtime.flags |= gnssPowerStats.elapsedRealtime.HAS_TIMESTAMP_NS;
  105. gnssPowerStats.elapsedRealtime.timestampNs = gnssPowerStatistics.elapsedRealTime;
  106. gnssPowerStats.elapsedRealtime.flags |= gnssPowerStats.elapsedRealtime.HAS_TIME_UNCERTAINTY_NS;
  107. gnssPowerStats.elapsedRealtime.timeUncertaintyNs = gnssPowerStatistics.elapsedRealTimeUnc;
  108. gnssPowerStats.totalEnergyMilliJoule = gnssPowerStatistics.totalEnergyMilliJoule;
  109. LOC_LOGd("gnssPowerStats.elapsedRealtime.flags: 0x%08X"
  110. " gnssPowerStats.elapsedRealtime.timestampNs: %" PRId64", "
  111. " gnssPowerStats.elapsedRealtime.timeUncertaintyNs: %.2f,"
  112. " gnssPowerStatistics.totalEnergyMilliJoule = %.2f",
  113. gnssPowerStats.elapsedRealtime.flags,
  114. gnssPowerStats.elapsedRealtime.timestampNs,
  115. gnssPowerStats.elapsedRealtime.timeUncertaintyNs,
  116. gnssPowerStats.totalEnergyMilliJoule);
  117. std::unique_lock<std::mutex> lock(mMutex);
  118. auto gnssPowerIndicationCb = mGnssPowerIndicationCb;
  119. lock.unlock();
  120. if (nullptr != gnssPowerIndicationCb) {
  121. gnssPowerIndicationCb->gnssPowerStatsCb(gnssPowerStats);
  122. }
  123. }
  124. } // namespace implementation
  125. } // namespace aidl
  126. } // namespace gnss
  127. } // namespace hardware
  128. } // namespace android