GnssVisibilityControl.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2019, The Linux Foundation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above
  10. * copyright notice, this list of conditions and the following
  11. * disclaimer in the documentation and/or other materials provided
  12. * with the distribution.
  13. * * Neither the name of The Linux Foundation nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  24. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  26. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  27. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <android/hardware/gnss/visibility_control/1.0/IGnssVisibilityControl.h>
  30. #include <hidl/MQDescriptor.h>
  31. #include <hidl/Status.h>
  32. #include "GnssVisibilityControl.h"
  33. #include <location_interface.h>
  34. namespace android {
  35. namespace hardware {
  36. namespace gnss {
  37. namespace visibility_control {
  38. namespace V1_0 {
  39. namespace implementation {
  40. using ::android::hardware::hidl_array;
  41. using ::android::hardware::hidl_memory;
  42. using ::android::hardware::hidl_string;
  43. using ::android::hardware::hidl_vec;
  44. using ::android::hardware::Return;
  45. using ::android::hardware::Void;
  46. using ::android::sp;
  47. static GnssVisibilityControl* spGnssVisibilityControl = nullptr;
  48. static void convertGnssNfwNotification(GnssNfwNotification& in,
  49. IGnssVisibilityControlCallback::NfwNotification& out);
  50. GnssVisibilityControl::GnssVisibilityControl(Gnss* gnss) : mGnss(gnss) {
  51. spGnssVisibilityControl = this;
  52. }
  53. GnssVisibilityControl::~GnssVisibilityControl() {
  54. spGnssVisibilityControl = nullptr;
  55. }
  56. void GnssVisibilityControl::nfwStatusCb(GnssNfwNotification notification) {
  57. if (nullptr != spGnssVisibilityControl) {
  58. spGnssVisibilityControl->statusCb(notification);
  59. }
  60. }
  61. bool GnssVisibilityControl::isInEmergencySession() {
  62. if (nullptr != spGnssVisibilityControl) {
  63. return spGnssVisibilityControl->isE911Session();
  64. }
  65. return false;
  66. }
  67. static void convertGnssNfwNotification(GnssNfwNotification& in,
  68. IGnssVisibilityControlCallback::NfwNotification& out)
  69. {
  70. memset(&out, 0, sizeof(IGnssVisibilityControlCallback::NfwNotification));
  71. out.proxyAppPackageName = in.proxyAppPackageName;
  72. out.protocolStack = (IGnssVisibilityControlCallback::NfwProtocolStack)in.protocolStack;
  73. out.otherProtocolStackName = in.otherProtocolStackName;
  74. out.requestor = (IGnssVisibilityControlCallback::NfwRequestor)in.requestor;
  75. out.requestorId = in.requestorId;
  76. out.responseType = (IGnssVisibilityControlCallback::NfwResponseType)in.responseType;
  77. out.inEmergencyMode = in.inEmergencyMode;
  78. out.isCachedLocation = in.isCachedLocation;
  79. }
  80. void GnssVisibilityControl::statusCb(GnssNfwNotification notification) {
  81. if (mGnssVisibilityControlCbIface != nullptr) {
  82. IGnssVisibilityControlCallback::NfwNotification nfwNotification;
  83. // Convert from one structure to another
  84. convertGnssNfwNotification(notification, nfwNotification);
  85. auto r = mGnssVisibilityControlCbIface->nfwNotifyCb(nfwNotification);
  86. if (!r.isOk()) {
  87. LOC_LOGw("Error invoking NFW status cb %s", r.description().c_str());
  88. }
  89. } else {
  90. LOC_LOGw("setCallback has not been called yet");
  91. }
  92. }
  93. bool GnssVisibilityControl::isE911Session() {
  94. if (mGnssVisibilityControlCbIface != nullptr) {
  95. auto r = mGnssVisibilityControlCbIface->isInEmergencySession();
  96. if (!r.isOk()) {
  97. LOC_LOGw("Error invoking NFW status cb %s", r.description().c_str());
  98. return false;
  99. } else {
  100. return (r);
  101. }
  102. } else {
  103. LOC_LOGw("setCallback has not been called yet");
  104. return false;
  105. }
  106. }
  107. // Methods from ::android::hardware::gnss::visibility_control::V1_0::IGnssVisibilityControl follow.
  108. Return<bool> GnssVisibilityControl::enableNfwLocationAccess(const hidl_vec<::android::hardware::hidl_string>& proxyApps) {
  109. if (nullptr == mGnss || nullptr == mGnss->getGnssInterface()) {
  110. LOC_LOGe("Null GNSS interface");
  111. return false;
  112. }
  113. std::vector<std::string> apps;
  114. for (auto i = 0; i < proxyApps.size(); i++) {
  115. apps.push_back((std::string)proxyApps[i]);
  116. }
  117. mGnss->getGnssInterface()->enableNfwLocationAccess(apps);
  118. return true;
  119. }
  120. /**
  121. * Registers the callback for HAL implementation to use.
  122. *
  123. * @param callback Handle to IGnssVisibilityControlCallback interface.
  124. */
  125. Return<bool> GnssVisibilityControl::setCallback(const ::android::sp<::android::hardware::gnss::visibility_control::V1_0::IGnssVisibilityControlCallback>& callback) {
  126. if (nullptr == mGnss || nullptr == mGnss->getGnssInterface()) {
  127. LOC_LOGe("Null GNSS interface");
  128. return false;
  129. }
  130. mGnssVisibilityControlCbIface = callback;
  131. NfwCbInfo cbInfo = {};
  132. cbInfo.visibilityControlCb = (void*)nfwStatusCb;
  133. cbInfo.isInEmergencySession = (void*)isInEmergencySession;
  134. mGnss->getGnssInterface()->nfwInit(cbInfo);
  135. return true;
  136. }
  137. } // namespace implementation
  138. } // namespace V1_0
  139. } // namespace visibility_control
  140. } // namespace gnss
  141. } // namespace hardware
  142. } // namespace android