GeofenceAPIClient.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* Copyright (c) 2017-2020, 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. /*
  30. Changes from Qualcomm Innovation Center are provided under the following license:
  31. Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  32. Redistribution and use in source and binary forms, with or without
  33. modification, are permitted (subject to the limitations in the
  34. disclaimer below) provided that the following conditions are met:
  35. * Redistributions of source code must retain the above copyright
  36. notice, this list of conditions and the following disclaimer.
  37. * Redistributions in binary form must reproduce the above
  38. copyright notice, this list of conditions and the following
  39. disclaimer in the documentation and/or other materials provided
  40. with the distribution.
  41. * Neither the name of Qualcomm Innovation Center, Inc. nor the names of its
  42. contributors may be used to endorse or promote products derived
  43. from this software without specific prior written permission.
  44. NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  45. GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
  46. HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
  47. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  48. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  49. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  50. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  51. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  52. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  53. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  54. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  55. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  56. IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  57. */
  58. #define LOG_NDEBUG 0
  59. #define LOG_TAG "LocSvc_GeofenceApiClient"
  60. #include <log_util.h>
  61. #include <loc_cfg.h>
  62. #include "LocationUtil.h"
  63. #include "GeofenceAPIClient.h"
  64. namespace android {
  65. namespace hardware {
  66. namespace gnss {
  67. namespace V2_1 {
  68. namespace implementation {
  69. using ::android::hardware::gnss::V1_0::IGnssGeofenceCallback;
  70. using ::android::hardware::gnss::V1_0::GnssLocation;
  71. GeofenceAPIClient::GeofenceAPIClient(const sp<IGnssGeofenceCallback>& callback) :
  72. LocationAPIClientBase(),
  73. mGnssGeofencingCbIface(callback)
  74. {
  75. LOC_LOGD("%s]: (%p)", __FUNCTION__, &callback);
  76. LocationCallbacks locationCallbacks;
  77. memset(&locationCallbacks, 0, sizeof(LocationCallbacks));
  78. locationCallbacks.size = sizeof(LocationCallbacks);
  79. locationCallbacks.trackingCb = nullptr;
  80. locationCallbacks.batchingCb = nullptr;
  81. locationCallbacks.geofenceBreachCb = nullptr;
  82. locationCallbacks.geofenceBreachCb =
  83. [this](GeofenceBreachNotification geofenceBreachNotification) {
  84. onGeofenceBreachCb(geofenceBreachNotification);
  85. };
  86. locationCallbacks.geofenceStatusCb =
  87. [this](GeofenceStatusNotification geofenceStatusNotification) {
  88. onGeofenceStatusCb(geofenceStatusNotification);
  89. };
  90. locationCallbacks.gnssLocationInfoCb = nullptr;
  91. locationCallbacks.gnssNiCb = nullptr;
  92. locationCallbacks.gnssSvCb = nullptr;
  93. locationCallbacks.gnssNmeaCb = nullptr;
  94. locationCallbacks.gnssMeasurementsCb = nullptr;
  95. locAPISetCallbacks(locationCallbacks);
  96. }
  97. void GeofenceAPIClient::upcateCallback(const sp<IGnssGeofenceCallback>& callback) {
  98. mMutex.lock();
  99. mGnssGeofencingCbIface = callback;
  100. mMutex.unlock();
  101. }
  102. void GeofenceAPIClient::geofenceAdd(uint32_t geofence_id, double latitude, double longitude,
  103. double radius_meters, int32_t last_transition, int32_t monitor_transitions,
  104. uint32_t notification_responsiveness_ms, uint32_t unknown_timer_ms)
  105. {
  106. LOC_LOGD("%s]: (%d %f %f %f %d %d %d %d)", __FUNCTION__,
  107. geofence_id, latitude, longitude, radius_meters,
  108. last_transition, monitor_transitions, notification_responsiveness_ms, unknown_timer_ms);
  109. GeofenceOption options;
  110. memset(&options, 0, sizeof(GeofenceOption));
  111. options.size = sizeof(GeofenceOption);
  112. if (monitor_transitions & IGnssGeofenceCallback::GeofenceTransition::ENTERED)
  113. options.breachTypeMask |= GEOFENCE_BREACH_ENTER_BIT;
  114. if (monitor_transitions & IGnssGeofenceCallback::GeofenceTransition::EXITED)
  115. options.breachTypeMask |= GEOFENCE_BREACH_EXIT_BIT;
  116. options.responsiveness = notification_responsiveness_ms;
  117. options.confidence = GEOFENCE_CONFIDENCE_HIGH;
  118. GeofenceInfo data;
  119. data.size = sizeof(GeofenceInfo);
  120. data.latitude = latitude;
  121. data.longitude = longitude;
  122. data.radius = radius_meters;
  123. LocationError err = (LocationError)locAPIAddGeofences(1, &geofence_id, &options, &data);
  124. if (LOCATION_ERROR_SUCCESS != err) {
  125. onAddGeofencesCb(1, &err, &geofence_id);
  126. }
  127. }
  128. void GeofenceAPIClient::geofencePause(uint32_t geofence_id)
  129. {
  130. LOC_LOGD("%s]: (%d)", __FUNCTION__, geofence_id);
  131. locAPIPauseGeofences(1, &geofence_id);
  132. }
  133. void GeofenceAPIClient::geofenceResume(uint32_t geofence_id, int32_t monitor_transitions)
  134. {
  135. LOC_LOGD("%s]: (%d %d)", __FUNCTION__, geofence_id, monitor_transitions);
  136. GeofenceBreachTypeMask mask = 0;
  137. if (monitor_transitions & IGnssGeofenceCallback::GeofenceTransition::ENTERED)
  138. mask |= GEOFENCE_BREACH_ENTER_BIT;
  139. if (monitor_transitions & IGnssGeofenceCallback::GeofenceTransition::EXITED)
  140. mask |= GEOFENCE_BREACH_EXIT_BIT;
  141. locAPIResumeGeofences(1, &geofence_id, &mask);
  142. }
  143. void GeofenceAPIClient::geofenceRemove(uint32_t geofence_id)
  144. {
  145. LOC_LOGD("%s]: (%d)", __FUNCTION__, geofence_id);
  146. locAPIRemoveGeofences(1, &geofence_id);
  147. }
  148. void GeofenceAPIClient::geofenceRemoveAll()
  149. {
  150. LOC_LOGD("%s]", __FUNCTION__);
  151. locAPIRemoveAllGeofences();
  152. }
  153. // callbacks
  154. void GeofenceAPIClient::onGeofenceBreachCb(GeofenceBreachNotification geofenceBreachNotification)
  155. {
  156. LOC_LOGD("%s]: (%d)", __FUNCTION__, geofenceBreachNotification.count);
  157. mMutex.lock();
  158. auto cbIface = mGnssGeofencingCbIface;
  159. mMutex.unlock();
  160. if (cbIface != nullptr) {
  161. for (size_t i = 0; i < geofenceBreachNotification.count; i++) {
  162. GnssLocation gnssLocation;
  163. convertGnssLocation(geofenceBreachNotification.location, gnssLocation);
  164. IGnssGeofenceCallback::GeofenceTransition transition;
  165. if (geofenceBreachNotification.type == GEOFENCE_BREACH_ENTER)
  166. transition = IGnssGeofenceCallback::GeofenceTransition::ENTERED;
  167. else if (geofenceBreachNotification.type == GEOFENCE_BREACH_EXIT)
  168. transition = IGnssGeofenceCallback::GeofenceTransition::EXITED;
  169. else {
  170. // continue with other breach if transition is
  171. // nether GPS_GEOFENCE_ENTERED nor GPS_GEOFENCE_EXITED
  172. continue;
  173. }
  174. auto r = cbIface->gnssGeofenceTransitionCb(
  175. geofenceBreachNotification.ids[i], gnssLocation, transition,
  176. static_cast<V1_0::GnssUtcTime>(geofenceBreachNotification.timestamp));
  177. if (!r.isOk()) {
  178. LOC_LOGE("%s] Error from gnssGeofenceTransitionCb description=%s",
  179. __func__, r.description().c_str());
  180. }
  181. }
  182. }
  183. }
  184. void GeofenceAPIClient::onGeofenceStatusCb(GeofenceStatusNotification geofenceStatusNotification)
  185. {
  186. LOC_LOGD("%s]: (%d)", __FUNCTION__, geofenceStatusNotification.available);
  187. mMutex.lock();
  188. auto cbIface = mGnssGeofencingCbIface;
  189. mMutex.unlock();
  190. if (cbIface != nullptr) {
  191. IGnssGeofenceCallback::GeofenceAvailability status =
  192. IGnssGeofenceCallback::GeofenceAvailability::UNAVAILABLE;
  193. if (geofenceStatusNotification.available == GEOFENCE_STATUS_AVAILABILE_YES) {
  194. status = IGnssGeofenceCallback::GeofenceAvailability::AVAILABLE;
  195. }
  196. GnssLocation gnssLocation;
  197. memset(&gnssLocation, 0, sizeof(GnssLocation));
  198. auto r = cbIface->gnssGeofenceStatusCb(status, gnssLocation);
  199. if (!r.isOk()) {
  200. LOC_LOGE("%s] Error from gnssGeofenceStatusCb description=%s",
  201. __func__, r.description().c_str());
  202. }
  203. }
  204. }
  205. void GeofenceAPIClient::onAddGeofencesCb(size_t count, LocationError* errors, uint32_t* ids)
  206. {
  207. LOC_LOGD("%s]: (%zu)", __FUNCTION__, count);
  208. mMutex.lock();
  209. auto cbIface = mGnssGeofencingCbIface;
  210. mMutex.unlock();
  211. if (cbIface != nullptr) {
  212. for (size_t i = 0; i < count; i++) {
  213. IGnssGeofenceCallback::GeofenceStatus status =
  214. IGnssGeofenceCallback::GeofenceStatus::ERROR_GENERIC;
  215. if (errors[i] == LOCATION_ERROR_SUCCESS)
  216. status = IGnssGeofenceCallback::GeofenceStatus::OPERATION_SUCCESS;
  217. else if (errors[i] == LOCATION_ERROR_ID_EXISTS)
  218. status = IGnssGeofenceCallback::GeofenceStatus::ERROR_ID_EXISTS;
  219. auto r = cbIface->gnssGeofenceAddCb(ids[i], status);
  220. if (!r.isOk()) {
  221. LOC_LOGE("%s] Error from gnssGeofenceAddCb description=%s",
  222. __func__, r.description().c_str());
  223. }
  224. }
  225. }
  226. }
  227. void GeofenceAPIClient::onRemoveGeofencesCb(size_t count, LocationError* errors, uint32_t* ids)
  228. {
  229. LOC_LOGD("%s]: (%zu)", __FUNCTION__, count);
  230. mMutex.lock();
  231. auto cbIface = mGnssGeofencingCbIface;
  232. mMutex.unlock();
  233. if (cbIface != nullptr) {
  234. for (size_t i = 0; i < count; i++) {
  235. IGnssGeofenceCallback::GeofenceStatus status =
  236. IGnssGeofenceCallback::GeofenceStatus::ERROR_GENERIC;
  237. if (errors[i] == LOCATION_ERROR_SUCCESS)
  238. status = IGnssGeofenceCallback::GeofenceStatus::OPERATION_SUCCESS;
  239. else if (errors[i] == LOCATION_ERROR_ID_UNKNOWN)
  240. status = IGnssGeofenceCallback::GeofenceStatus::ERROR_ID_UNKNOWN;
  241. auto r = cbIface->gnssGeofenceRemoveCb(ids[i], status);
  242. if (!r.isOk()) {
  243. LOC_LOGE("%s] Error from gnssGeofenceRemoveCb description=%s",
  244. __func__, r.description().c_str());
  245. }
  246. }
  247. }
  248. }
  249. void GeofenceAPIClient::onPauseGeofencesCb(size_t count, LocationError* errors, uint32_t* ids)
  250. {
  251. LOC_LOGD("%s]: (%zu)", __FUNCTION__, count);
  252. mMutex.lock();
  253. auto cbIface = mGnssGeofencingCbIface;
  254. mMutex.unlock();
  255. if (cbIface != nullptr) {
  256. for (size_t i = 0; i < count; i++) {
  257. IGnssGeofenceCallback::GeofenceStatus status =
  258. IGnssGeofenceCallback::GeofenceStatus::ERROR_GENERIC;
  259. if (errors[i] == LOCATION_ERROR_SUCCESS)
  260. status = IGnssGeofenceCallback::GeofenceStatus::OPERATION_SUCCESS;
  261. else if (errors[i] == LOCATION_ERROR_ID_UNKNOWN)
  262. status = IGnssGeofenceCallback::GeofenceStatus::ERROR_ID_UNKNOWN;
  263. auto r = cbIface->gnssGeofencePauseCb(ids[i], status);
  264. if (!r.isOk()) {
  265. LOC_LOGE("%s] Error from gnssGeofencePauseCb description=%s",
  266. __func__, r.description().c_str());
  267. }
  268. }
  269. }
  270. }
  271. void GeofenceAPIClient::onResumeGeofencesCb(size_t count, LocationError* errors, uint32_t* ids)
  272. {
  273. LOC_LOGD("%s]: (%zu)", __FUNCTION__, count);
  274. mMutex.lock();
  275. auto cbIface = mGnssGeofencingCbIface;
  276. mMutex.unlock();
  277. if (cbIface != nullptr) {
  278. for (size_t i = 0; i < count; i++) {
  279. IGnssGeofenceCallback::GeofenceStatus status =
  280. IGnssGeofenceCallback::GeofenceStatus::ERROR_GENERIC;
  281. if (errors[i] == LOCATION_ERROR_SUCCESS)
  282. status = IGnssGeofenceCallback::GeofenceStatus::OPERATION_SUCCESS;
  283. else if (errors[i] == LOCATION_ERROR_ID_UNKNOWN)
  284. status = IGnssGeofenceCallback::GeofenceStatus::ERROR_ID_UNKNOWN;
  285. auto r = cbIface->gnssGeofenceResumeCb(ids[i], status);
  286. if (!r.isOk()) {
  287. LOC_LOGE("%s] Error from gnssGeofenceResumeCb description=%s",
  288. __func__, r.description().c_str());
  289. }
  290. }
  291. }
  292. }
  293. } // namespace implementation
  294. } // namespace V2_1
  295. } // namespace gnss
  296. } // namespace hardware
  297. } // namespace android