BatchingAPIClient.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* Copyright (c) 2017-2019, 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 V2_0 {
  41. namespace implementation {
  42. using ::android::hardware::gnss::V2_0::IGnssBatching;
  43. using ::android::hardware::gnss::V2_0::IGnssBatchingCallback;
  44. using ::android::hardware::gnss::V2_0::GnssLocation;
  45. static void convertBatchOption(const IGnssBatching::Options& in, LocationOptions& out,
  46. LocationCapabilitiesMask mask);
  47. BatchingAPIClient::BatchingAPIClient(const sp<V1_0::IGnssBatchingCallback>& callback) :
  48. LocationAPIClientBase(),
  49. mGnssBatchingCbIface(nullptr),
  50. mDefaultId(UINT_MAX),
  51. mLocationCapabilitiesMask(0),
  52. mGnssBatchingCbIface_2_0(nullptr)
  53. {
  54. LOC_LOGD("%s]: (%p)", __FUNCTION__, &callback);
  55. gnssUpdateCallbacks(callback);
  56. }
  57. BatchingAPIClient::BatchingAPIClient(const sp<V2_0::IGnssBatchingCallback>& callback) :
  58. LocationAPIClientBase(),
  59. mGnssBatchingCbIface(nullptr),
  60. mDefaultId(UINT_MAX),
  61. mLocationCapabilitiesMask(0),
  62. mGnssBatchingCbIface_2_0(nullptr)
  63. {
  64. LOC_LOGD("%s]: (%p)", __FUNCTION__, &callback);
  65. gnssUpdateCallbacks_2_0(callback);
  66. }
  67. BatchingAPIClient::~BatchingAPIClient()
  68. {
  69. LOC_LOGD("%s]: ()", __FUNCTION__);
  70. }
  71. int BatchingAPIClient::getBatchSize()
  72. {
  73. LOC_LOGD("%s]: ()", __FUNCTION__);
  74. return locAPIGetBatchSize();
  75. }
  76. void BatchingAPIClient::setCallbacks()
  77. {
  78. LocationCallbacks locationCallbacks;
  79. memset(&locationCallbacks, 0, sizeof(LocationCallbacks));
  80. locationCallbacks.size = sizeof(LocationCallbacks);
  81. locationCallbacks.trackingCb = nullptr;
  82. locationCallbacks.batchingCb = nullptr;
  83. locationCallbacks.batchingCb = [this](size_t count, Location* location,
  84. BatchingOptions batchOptions) {
  85. onBatchingCb(count, location, batchOptions);
  86. };
  87. locationCallbacks.geofenceBreachCb = nullptr;
  88. locationCallbacks.geofenceStatusCb = nullptr;
  89. locationCallbacks.gnssLocationInfoCb = nullptr;
  90. locationCallbacks.gnssNiCb = nullptr;
  91. locationCallbacks.gnssSvCb = nullptr;
  92. locationCallbacks.gnssNmeaCb = nullptr;
  93. locationCallbacks.gnssMeasurementsCb = nullptr;
  94. locAPISetCallbacks(locationCallbacks);
  95. }
  96. void BatchingAPIClient::gnssUpdateCallbacks(const sp<V1_0::IGnssBatchingCallback>& callback)
  97. {
  98. mMutex.lock();
  99. bool cbWasNull = (mGnssBatchingCbIface == nullptr);
  100. mGnssBatchingCbIface = callback;
  101. mGnssBatchingCbIface_2_0 = nullptr;
  102. mMutex.unlock();
  103. if (cbWasNull) {
  104. setCallbacks();
  105. }
  106. }
  107. void BatchingAPIClient::gnssUpdateCallbacks_2_0(const sp<V2_0::IGnssBatchingCallback>& callback)
  108. {
  109. mMutex.lock();
  110. bool cbWasNull = (mGnssBatchingCbIface_2_0 == nullptr);
  111. mGnssBatchingCbIface = nullptr;
  112. mGnssBatchingCbIface_2_0 = callback;
  113. mMutex.unlock();
  114. if (cbWasNull) {
  115. setCallbacks();
  116. }
  117. }
  118. int BatchingAPIClient::startSession(const IGnssBatching::Options& opts)
  119. {
  120. LOC_LOGD("%s]: (%lld %d)", __FUNCTION__,
  121. static_cast<long long>(opts.periodNanos), static_cast<uint8_t>(opts.flags));
  122. int retVal = -1;
  123. LocationOptions options;
  124. convertBatchOption(opts, options, mLocationCapabilitiesMask);
  125. uint32_t mode = 0;
  126. if (opts.flags == static_cast<uint8_t>(IGnssBatching::Flag::WAKEUP_ON_FIFO_FULL)) {
  127. mode = SESSION_MODE_ON_FULL;
  128. }
  129. if (locAPIStartSession(mDefaultId, mode, options) == LOCATION_ERROR_SUCCESS) {
  130. retVal = 1;
  131. }
  132. return retVal;
  133. }
  134. int BatchingAPIClient::updateSessionOptions(const IGnssBatching::Options& opts)
  135. {
  136. LOC_LOGD("%s]: (%lld %d)", __FUNCTION__,
  137. static_cast<long long>(opts.periodNanos), static_cast<uint8_t>(opts.flags));
  138. int retVal = -1;
  139. LocationOptions options;
  140. convertBatchOption(opts, options, mLocationCapabilitiesMask);
  141. uint32_t mode = 0;
  142. if (opts.flags == static_cast<uint8_t>(IGnssBatching::Flag::WAKEUP_ON_FIFO_FULL)) {
  143. mode = SESSION_MODE_ON_FULL;
  144. }
  145. if (locAPIUpdateSessionOptions(mDefaultId, mode, options) == LOCATION_ERROR_SUCCESS) {
  146. retVal = 1;
  147. }
  148. return retVal;
  149. }
  150. int BatchingAPIClient::stopSession()
  151. {
  152. LOC_LOGD("%s]: ", __FUNCTION__);
  153. int retVal = -1;
  154. if (locAPIStopSession(mDefaultId) == LOCATION_ERROR_SUCCESS) {
  155. retVal = 1;
  156. }
  157. return retVal;
  158. }
  159. void BatchingAPIClient::getBatchedLocation(int last_n_locations)
  160. {
  161. LOC_LOGD("%s]: (%d)", __FUNCTION__, last_n_locations);
  162. locAPIGetBatchedLocations(mDefaultId, last_n_locations);
  163. }
  164. void BatchingAPIClient::flushBatchedLocations()
  165. {
  166. LOC_LOGD("%s]: ()", __FUNCTION__);
  167. locAPIGetBatchedLocations(mDefaultId, SIZE_MAX);
  168. }
  169. void BatchingAPIClient::onCapabilitiesCb(LocationCapabilitiesMask capabilitiesMask)
  170. {
  171. LOC_LOGD("%s]: (%" PRIu64 ")", __FUNCTION__, capabilitiesMask);
  172. mLocationCapabilitiesMask = capabilitiesMask;
  173. }
  174. void BatchingAPIClient::onBatchingCb(size_t count, Location* location,
  175. BatchingOptions /*batchOptions*/)
  176. {
  177. mMutex.lock();
  178. auto gnssBatchingCbIface(mGnssBatchingCbIface);
  179. auto gnssBatchingCbIface_2_0(mGnssBatchingCbIface_2_0);
  180. mMutex.unlock();
  181. LOC_LOGD("%s]: (count: %zu)", __FUNCTION__, count);
  182. if (gnssBatchingCbIface_2_0 != nullptr && count > 0) {
  183. hidl_vec<V2_0::GnssLocation> locationVec;
  184. locationVec.resize(count);
  185. for (size_t i = 0; i < count; i++) {
  186. convertGnssLocation(location[i], locationVec[i]);
  187. }
  188. auto r = gnssBatchingCbIface_2_0->gnssLocationBatchCb(locationVec);
  189. if (!r.isOk()) {
  190. LOC_LOGE("%s] Error from gnssLocationBatchCb 2.0 description=%s",
  191. __func__, r.description().c_str());
  192. }
  193. } else if (gnssBatchingCbIface != nullptr && count > 0) {
  194. hidl_vec<V1_0::GnssLocation> locationVec;
  195. locationVec.resize(count);
  196. for (size_t i = 0; i < count; i++) {
  197. convertGnssLocation(location[i], locationVec[i]);
  198. }
  199. auto r = gnssBatchingCbIface->gnssLocationBatchCb(locationVec);
  200. if (!r.isOk()) {
  201. LOC_LOGE("%s] Error from gnssLocationBatchCb 1.0 description=%s",
  202. __func__, r.description().c_str());
  203. }
  204. }
  205. }
  206. static void convertBatchOption(const IGnssBatching::Options& in, LocationOptions& out,
  207. LocationCapabilitiesMask mask)
  208. {
  209. memset(&out, 0, sizeof(LocationOptions));
  210. out.size = sizeof(LocationOptions);
  211. out.minInterval = (uint32_t)(in.periodNanos / 1000000L);
  212. out.minDistance = 0;
  213. out.mode = GNSS_SUPL_MODE_STANDALONE;
  214. if (mask & LOCATION_CAPABILITIES_GNSS_MSA_BIT)
  215. out.mode = GNSS_SUPL_MODE_MSA;
  216. if (mask & LOCATION_CAPABILITIES_GNSS_MSB_BIT)
  217. out.mode = GNSS_SUPL_MODE_MSB;
  218. }
  219. } // namespace implementation
  220. } // namespace V2_0
  221. } // namespace gnss
  222. } // namespace hardware
  223. } // namespace android