GeofenceAPIClient.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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_GeofenceApiClient"
  31. #include <log_util.h>
  32. #include <loc_cfg.h>
  33. #include "LocationUtil.h"
  34. #include "GeofenceAPIClient.h"
  35. namespace android {
  36. namespace hardware {
  37. namespace gnss {
  38. namespace V1_0 {
  39. namespace implementation {
  40. using ::android::hardware::gnss::V1_0::IGnssGeofenceCallback;
  41. using ::android::hardware::gnss::V1_0::GnssLocation;
  42. GeofenceAPIClient::GeofenceAPIClient(const sp<IGnssGeofenceCallback>& callback) :
  43. LocationAPIClientBase(),
  44. mGnssGeofencingCbIface(callback)
  45. {
  46. LOC_LOGD("%s]: (%p)", __FUNCTION__, &callback);
  47. LocationCallbacks locationCallbacks;
  48. memset(&locationCallbacks, 0, sizeof(LocationCallbacks));
  49. locationCallbacks.size = sizeof(LocationCallbacks);
  50. locationCallbacks.trackingCb = nullptr;
  51. locationCallbacks.batchingCb = nullptr;
  52. locationCallbacks.geofenceBreachCb = nullptr;
  53. locationCallbacks.geofenceBreachCb =
  54. [this](GeofenceBreachNotification geofenceBreachNotification) {
  55. onGeofenceBreachCb(geofenceBreachNotification);
  56. };
  57. locationCallbacks.geofenceStatusCb =
  58. [this](GeofenceStatusNotification geofenceStatusNotification) {
  59. onGeofenceStatusCb(geofenceStatusNotification);
  60. };
  61. locationCallbacks.gnssLocationInfoCb = nullptr;
  62. locationCallbacks.gnssNiCb = nullptr;
  63. locationCallbacks.gnssSvCb = nullptr;
  64. locationCallbacks.gnssNmeaCb = nullptr;
  65. locationCallbacks.gnssMeasurementsCb = nullptr;
  66. locAPISetCallbacks(locationCallbacks);
  67. }
  68. void GeofenceAPIClient::upcateCallback(const sp<IGnssGeofenceCallback>& callback) {
  69. mMutex.lock();
  70. mGnssGeofencingCbIface = callback;
  71. mMutex.unlock();
  72. }
  73. void GeofenceAPIClient::geofenceAdd(uint32_t geofence_id, double latitude, double longitude,
  74. double radius_meters, int32_t last_transition, int32_t monitor_transitions,
  75. uint32_t notification_responsiveness_ms, uint32_t unknown_timer_ms)
  76. {
  77. LOC_LOGD("%s]: (%d %f %f %f %d %d %d %d)", __FUNCTION__,
  78. geofence_id, latitude, longitude, radius_meters,
  79. last_transition, monitor_transitions, notification_responsiveness_ms, unknown_timer_ms);
  80. GeofenceOption options;
  81. memset(&options, 0, sizeof(GeofenceOption));
  82. options.size = sizeof(GeofenceOption);
  83. if (monitor_transitions & IGnssGeofenceCallback::GeofenceTransition::ENTERED)
  84. options.breachTypeMask |= GEOFENCE_BREACH_ENTER_BIT;
  85. if (monitor_transitions & IGnssGeofenceCallback::GeofenceTransition::EXITED)
  86. options.breachTypeMask |= GEOFENCE_BREACH_EXIT_BIT;
  87. options.responsiveness = notification_responsiveness_ms;
  88. GeofenceInfo data;
  89. data.size = sizeof(GeofenceInfo);
  90. data.latitude = latitude;
  91. data.longitude = longitude;
  92. data.radius = radius_meters;
  93. LocationError err = (LocationError)locAPIAddGeofences(1, &geofence_id, &options, &data);
  94. if (LOCATION_ERROR_SUCCESS != err) {
  95. onAddGeofencesCb(1, &err, &geofence_id);
  96. }
  97. }
  98. void GeofenceAPIClient::geofencePause(uint32_t geofence_id)
  99. {
  100. LOC_LOGD("%s]: (%d)", __FUNCTION__, geofence_id);
  101. locAPIPauseGeofences(1, &geofence_id);
  102. }
  103. void GeofenceAPIClient::geofenceResume(uint32_t geofence_id, int32_t monitor_transitions)
  104. {
  105. LOC_LOGD("%s]: (%d %d)", __FUNCTION__, geofence_id, monitor_transitions);
  106. GeofenceBreachTypeMask mask = 0;
  107. if (monitor_transitions & IGnssGeofenceCallback::GeofenceTransition::ENTERED)
  108. mask |= GEOFENCE_BREACH_ENTER_BIT;
  109. if (monitor_transitions & IGnssGeofenceCallback::GeofenceTransition::EXITED)
  110. mask |= GEOFENCE_BREACH_EXIT_BIT;
  111. locAPIResumeGeofences(1, &geofence_id, &mask);
  112. }
  113. void GeofenceAPIClient::geofenceRemove(uint32_t geofence_id)
  114. {
  115. LOC_LOGD("%s]: (%d)", __FUNCTION__, geofence_id);
  116. locAPIRemoveGeofences(1, &geofence_id);
  117. }
  118. void GeofenceAPIClient::geofenceRemoveAll()
  119. {
  120. LOC_LOGD("%s]", __FUNCTION__);
  121. locAPIRemoveAllGeofences();
  122. }
  123. // callbacks
  124. void GeofenceAPIClient::onGeofenceBreachCb(GeofenceBreachNotification geofenceBreachNotification)
  125. {
  126. LOC_LOGD("%s]: (%zu)", __FUNCTION__, geofenceBreachNotification.count);
  127. mMutex.lock();
  128. auto cbIface = mGnssGeofencingCbIface;
  129. mMutex.unlock();
  130. if (cbIface != nullptr) {
  131. for (size_t i = 0; i < geofenceBreachNotification.count; i++) {
  132. GnssLocation gnssLocation;
  133. convertGnssLocation(geofenceBreachNotification.location, gnssLocation);
  134. IGnssGeofenceCallback::GeofenceTransition transition;
  135. if (geofenceBreachNotification.type == GEOFENCE_BREACH_ENTER)
  136. transition = IGnssGeofenceCallback::GeofenceTransition::ENTERED;
  137. else if (geofenceBreachNotification.type == GEOFENCE_BREACH_EXIT)
  138. transition = IGnssGeofenceCallback::GeofenceTransition::EXITED;
  139. else {
  140. // continue with other breach if transition is
  141. // nether GPS_GEOFENCE_ENTERED nor GPS_GEOFENCE_EXITED
  142. continue;
  143. }
  144. auto r = cbIface->gnssGeofenceTransitionCb(
  145. geofenceBreachNotification.ids[i], gnssLocation, transition,
  146. static_cast<V1_0::GnssUtcTime>(geofenceBreachNotification.timestamp));
  147. if (!r.isOk()) {
  148. LOC_LOGE("%s] Error from gnssGeofenceTransitionCb description=%s",
  149. __func__, r.description().c_str());
  150. }
  151. }
  152. }
  153. }
  154. void GeofenceAPIClient::onGeofenceStatusCb(GeofenceStatusNotification geofenceStatusNotification)
  155. {
  156. LOC_LOGD("%s]: (%d)", __FUNCTION__, geofenceStatusNotification.available);
  157. mMutex.lock();
  158. auto cbIface = mGnssGeofencingCbIface;
  159. mMutex.unlock();
  160. if (cbIface != nullptr) {
  161. IGnssGeofenceCallback::GeofenceAvailability status =
  162. IGnssGeofenceCallback::GeofenceAvailability::UNAVAILABLE;
  163. if (geofenceStatusNotification.available == GEOFENCE_STATUS_AVAILABILE_YES) {
  164. status = IGnssGeofenceCallback::GeofenceAvailability::AVAILABLE;
  165. }
  166. GnssLocation gnssLocation;
  167. memset(&gnssLocation, 0, sizeof(GnssLocation));
  168. auto r = cbIface->gnssGeofenceStatusCb(status, gnssLocation);
  169. if (!r.isOk()) {
  170. LOC_LOGE("%s] Error from gnssGeofenceStatusCb description=%s",
  171. __func__, r.description().c_str());
  172. }
  173. }
  174. }
  175. void GeofenceAPIClient::onAddGeofencesCb(size_t count, LocationError* errors, uint32_t* ids)
  176. {
  177. LOC_LOGD("%s]: (%zu)", __FUNCTION__, count);
  178. mMutex.lock();
  179. auto cbIface = mGnssGeofencingCbIface;
  180. mMutex.unlock();
  181. if (cbIface != nullptr) {
  182. for (size_t i = 0; i < count; i++) {
  183. IGnssGeofenceCallback::GeofenceStatus status =
  184. IGnssGeofenceCallback::GeofenceStatus::ERROR_GENERIC;
  185. if (errors[i] == LOCATION_ERROR_SUCCESS)
  186. status = IGnssGeofenceCallback::GeofenceStatus::OPERATION_SUCCESS;
  187. else if (errors[i] == LOCATION_ERROR_ID_EXISTS)
  188. status = IGnssGeofenceCallback::GeofenceStatus::ERROR_ID_EXISTS;
  189. auto r = cbIface->gnssGeofenceAddCb(ids[i], status);
  190. if (!r.isOk()) {
  191. LOC_LOGE("%s] Error from gnssGeofenceAddCb description=%s",
  192. __func__, r.description().c_str());
  193. }
  194. }
  195. }
  196. }
  197. void GeofenceAPIClient::onRemoveGeofencesCb(size_t count, LocationError* errors, uint32_t* ids)
  198. {
  199. LOC_LOGD("%s]: (%zu)", __FUNCTION__, count);
  200. mMutex.lock();
  201. auto cbIface = mGnssGeofencingCbIface;
  202. mMutex.unlock();
  203. if (cbIface != nullptr) {
  204. for (size_t i = 0; i < count; i++) {
  205. IGnssGeofenceCallback::GeofenceStatus status =
  206. IGnssGeofenceCallback::GeofenceStatus::ERROR_GENERIC;
  207. if (errors[i] == LOCATION_ERROR_SUCCESS)
  208. status = IGnssGeofenceCallback::GeofenceStatus::OPERATION_SUCCESS;
  209. else if (errors[i] == LOCATION_ERROR_ID_UNKNOWN)
  210. status = IGnssGeofenceCallback::GeofenceStatus::ERROR_ID_UNKNOWN;
  211. auto r = cbIface->gnssGeofenceRemoveCb(ids[i], status);
  212. if (!r.isOk()) {
  213. LOC_LOGE("%s] Error from gnssGeofenceRemoveCb description=%s",
  214. __func__, r.description().c_str());
  215. }
  216. }
  217. }
  218. }
  219. void GeofenceAPIClient::onPauseGeofencesCb(size_t count, LocationError* errors, uint32_t* ids)
  220. {
  221. LOC_LOGD("%s]: (%zu)", __FUNCTION__, count);
  222. mMutex.lock();
  223. auto cbIface = mGnssGeofencingCbIface;
  224. mMutex.unlock();
  225. if (cbIface != nullptr) {
  226. for (size_t i = 0; i < count; i++) {
  227. IGnssGeofenceCallback::GeofenceStatus status =
  228. IGnssGeofenceCallback::GeofenceStatus::ERROR_GENERIC;
  229. if (errors[i] == LOCATION_ERROR_SUCCESS)
  230. status = IGnssGeofenceCallback::GeofenceStatus::OPERATION_SUCCESS;
  231. else if (errors[i] == LOCATION_ERROR_ID_UNKNOWN)
  232. status = IGnssGeofenceCallback::GeofenceStatus::ERROR_ID_UNKNOWN;
  233. auto r = cbIface->gnssGeofencePauseCb(ids[i], status);
  234. if (!r.isOk()) {
  235. LOC_LOGE("%s] Error from gnssGeofencePauseCb description=%s",
  236. __func__, r.description().c_str());
  237. }
  238. }
  239. }
  240. }
  241. void GeofenceAPIClient::onResumeGeofencesCb(size_t count, LocationError* errors, uint32_t* ids)
  242. {
  243. LOC_LOGD("%s]: (%zu)", __FUNCTION__, count);
  244. mMutex.lock();
  245. auto cbIface = mGnssGeofencingCbIface;
  246. mMutex.unlock();
  247. if (cbIface != nullptr) {
  248. for (size_t i = 0; i < count; i++) {
  249. IGnssGeofenceCallback::GeofenceStatus status =
  250. IGnssGeofenceCallback::GeofenceStatus::ERROR_GENERIC;
  251. if (errors[i] == LOCATION_ERROR_SUCCESS)
  252. status = IGnssGeofenceCallback::GeofenceStatus::OPERATION_SUCCESS;
  253. else if (errors[i] == LOCATION_ERROR_ID_UNKNOWN)
  254. status = IGnssGeofenceCallback::GeofenceStatus::ERROR_ID_UNKNOWN;
  255. auto r = cbIface->gnssGeofenceResumeCb(ids[i], status);
  256. if (!r.isOk()) {
  257. LOC_LOGE("%s] Error from gnssGeofenceResumeCb description=%s",
  258. __func__, r.description().c_str());
  259. }
  260. }
  261. }
  262. }
  263. } // namespace implementation
  264. } // namespace V1_0
  265. } // namespace gnss
  266. } // namespace hardware
  267. } // namespace android