BatchingAPIClient.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are
  5. * met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above
  9. * copyright notice, this list of conditions and the following
  10. * disclaimer in the documentation and/or other materials provided
  11. * with the distribution.
  12. * * Neither the name of The Linux Foundation, nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  20. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  23. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  24. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  25. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  26. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. */
  29. #define LOG_NDEBUG 0
  30. #define LOG_TAG "LocSvc_BatchingAPIClient"
  31. #include <inttypes.h>
  32. #include <log_util.h>
  33. #include <loc_cfg.h>
  34. #include "LocationUtil.h"
  35. #include "BatchingAPIClient.h"
  36. #include "limits.h"
  37. namespace android {
  38. namespace hardware {
  39. namespace gnss {
  40. namespace V1_0 {
  41. namespace implementation {
  42. using ::android::hardware::gnss::V1_0::IGnssBatching;
  43. using ::android::hardware::gnss::V1_0::IGnssBatchingCallback;
  44. using ::android::hardware::gnss::V1_0::GnssLocation;
  45. static void convertBatchOption(const IGnssBatching::Options& in, LocationOptions& out,
  46. LocationCapabilitiesMask mask);
  47. BatchingAPIClient::BatchingAPIClient(const sp<IGnssBatchingCallback>& callback) :
  48. LocationAPIClientBase(),
  49. mGnssBatchingCbIface(callback),
  50. mDefaultId(UINT_MAX),
  51. mLocationCapabilitiesMask(0)
  52. {
  53. LOC_LOGD("%s]: (%p)", __FUNCTION__, &callback);
  54. LocationCallbacks locationCallbacks;
  55. memset(&locationCallbacks, 0, sizeof(LocationCallbacks));
  56. locationCallbacks.size = sizeof(LocationCallbacks);
  57. locationCallbacks.trackingCb = nullptr;
  58. locationCallbacks.batchingCb = nullptr;
  59. locationCallbacks.batchingCb = [this](size_t count, Location* location,
  60. BatchingOptions batchOptions) {
  61. onBatchingCb(count, location, batchOptions);
  62. };
  63. locationCallbacks.geofenceBreachCb = nullptr;
  64. locationCallbacks.geofenceStatusCb = nullptr;
  65. locationCallbacks.gnssLocationInfoCb = nullptr;
  66. locationCallbacks.gnssNiCb = nullptr;
  67. locationCallbacks.gnssSvCb = nullptr;
  68. locationCallbacks.gnssNmeaCb = nullptr;
  69. locationCallbacks.gnssMeasurementsCb = nullptr;
  70. locAPISetCallbacks(locationCallbacks);
  71. }
  72. BatchingAPIClient::~BatchingAPIClient()
  73. {
  74. LOC_LOGD("%s]: ()", __FUNCTION__);
  75. }
  76. void BatchingAPIClient::gnssUpdateCallbacks(const sp<IGnssBatchingCallback>& callback)
  77. {
  78. mMutex.lock();
  79. mGnssBatchingCbIface = callback;
  80. mMutex.unlock();
  81. }
  82. int BatchingAPIClient::getBatchSize()
  83. {
  84. LOC_LOGD("%s]: ()", __FUNCTION__);
  85. return locAPIGetBatchSize();
  86. }
  87. int BatchingAPIClient::startSession(const IGnssBatching::Options& opts)
  88. {
  89. LOC_LOGD("%s]: (%lld %d)", __FUNCTION__,
  90. static_cast<long long>(opts.periodNanos), static_cast<uint8_t>(opts.flags));
  91. int retVal = -1;
  92. LocationOptions options;
  93. convertBatchOption(opts, options, mLocationCapabilitiesMask);
  94. uint32_t mode = 0;
  95. if (opts.flags == static_cast<uint8_t>(IGnssBatching::Flag::WAKEUP_ON_FIFO_FULL)) {
  96. mode = SESSION_MODE_ON_FULL;
  97. }
  98. if (locAPIStartSession(mDefaultId, mode, options) == LOCATION_ERROR_SUCCESS) {
  99. retVal = 1;
  100. }
  101. return retVal;
  102. }
  103. int BatchingAPIClient::updateSessionOptions(const IGnssBatching::Options& opts)
  104. {
  105. LOC_LOGD("%s]: (%lld %d)", __FUNCTION__,
  106. static_cast<long long>(opts.periodNanos), static_cast<uint8_t>(opts.flags));
  107. int retVal = -1;
  108. LocationOptions options;
  109. convertBatchOption(opts, options, mLocationCapabilitiesMask);
  110. uint32_t mode = 0;
  111. if (opts.flags == static_cast<uint8_t>(IGnssBatching::Flag::WAKEUP_ON_FIFO_FULL)) {
  112. mode = SESSION_MODE_ON_FULL;
  113. }
  114. if (locAPIUpdateSessionOptions(mDefaultId, mode, options) == LOCATION_ERROR_SUCCESS) {
  115. retVal = 1;
  116. }
  117. return retVal;
  118. }
  119. int BatchingAPIClient::stopSession()
  120. {
  121. LOC_LOGD("%s]: ", __FUNCTION__);
  122. int retVal = -1;
  123. if (locAPIStopSession(mDefaultId) == LOCATION_ERROR_SUCCESS) {
  124. retVal = 1;
  125. }
  126. return retVal;
  127. }
  128. void BatchingAPIClient::getBatchedLocation(int last_n_locations)
  129. {
  130. LOC_LOGD("%s]: (%d)", __FUNCTION__, last_n_locations);
  131. locAPIGetBatchedLocations(mDefaultId, last_n_locations);
  132. }
  133. void BatchingAPIClient::flushBatchedLocations()
  134. {
  135. LOC_LOGD("%s]: ()", __FUNCTION__);
  136. locAPIGetBatchedLocations(mDefaultId, SIZE_MAX);
  137. }
  138. void BatchingAPIClient::onCapabilitiesCb(LocationCapabilitiesMask capabilitiesMask)
  139. {
  140. LOC_LOGD("%s]: (%" PRIu64 ")", __FUNCTION__, capabilitiesMask);
  141. mLocationCapabilitiesMask = capabilitiesMask;
  142. }
  143. void BatchingAPIClient::onBatchingCb(size_t count, Location* location,
  144. BatchingOptions /*batchOptions*/)
  145. {
  146. LOC_LOGD("%s]: (count: %zu)", __FUNCTION__, count);
  147. mMutex.lock();
  148. auto cbiface = mGnssBatchingCbIface;
  149. mMutex.unlock();
  150. if (cbiface != nullptr && count > 0) {
  151. hidl_vec<GnssLocation> locationVec;
  152. locationVec.resize(count);
  153. for (size_t i = 0; i < count; i++) {
  154. convertGnssLocation(location[i], locationVec[i]);
  155. }
  156. auto r = cbiface->gnssLocationBatchCb(locationVec);
  157. if (!r.isOk()) {
  158. LOC_LOGE("%s] Error from gnssLocationBatchCb description=%s",
  159. __func__, r.description().c_str());
  160. }
  161. }
  162. }
  163. static void convertBatchOption(const IGnssBatching::Options& in, LocationOptions& out,
  164. LocationCapabilitiesMask mask)
  165. {
  166. memset(&out, 0, sizeof(LocationOptions));
  167. out.size = sizeof(LocationOptions);
  168. out.minInterval = (uint32_t)(in.periodNanos / 1000000L);
  169. out.minDistance = 0;
  170. out.mode = GNSS_SUPL_MODE_STANDALONE;
  171. if (mask & LOCATION_CAPABILITIES_GNSS_MSA_BIT)
  172. out.mode = GNSS_SUPL_MODE_MSA;
  173. if (mask & LOCATION_CAPABILITIES_GNSS_MSB_BIT)
  174. out.mode = GNSS_SUPL_MODE_MSB;
  175. }
  176. } // namespace implementation
  177. } // namespace V1_0
  178. } // namespace gnss
  179. } // namespace hardware
  180. } // namespace android