GeofenceAdapter.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* Copyright (c) 2013-2019, 2021, 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. #ifndef GEOFENCE_ADAPTER_H
  59. #define GEOFENCE_ADAPTER_H
  60. #include <LocAdapterBase.h>
  61. #include <LocContext.h>
  62. #include <LocationAPI.h>
  63. #include <map>
  64. using namespace loc_core;
  65. #define COPY_IF_NOT_NULL(dest, src, len) do { \
  66. if (NULL!=dest && NULL!=src) { \
  67. for (size_t i=0; i<len; ++i) { \
  68. dest[i] = src[i]; \
  69. } \
  70. } \
  71. } while (0)
  72. typedef struct GeofenceKey {
  73. LocationAPI* client;
  74. uint32_t id;
  75. inline GeofenceKey() :
  76. client(NULL), id(0) {}
  77. inline GeofenceKey(LocationAPI* _client, uint32_t _id) :
  78. client(_client), id(_id) {}
  79. } GeofenceKey;
  80. inline bool operator <(GeofenceKey const& left, GeofenceKey const& right) {
  81. return left.id < right.id || (left.id == right.id && left.client < right.client);
  82. }
  83. inline bool operator ==(GeofenceKey const& left, GeofenceKey const& right) {
  84. return left.id == right.id && left.client == right.client;
  85. }
  86. inline bool operator !=(GeofenceKey const& left, GeofenceKey const& right) {
  87. return left.id != right.id || left.client != right.client;
  88. }
  89. typedef struct {
  90. GeofenceKey key;
  91. GeofenceBreachTypeMask breachMask;
  92. uint32_t responsiveness;
  93. uint32_t dwellTime;
  94. GeofenceConfidence confidence;
  95. double latitude;
  96. double longitude;
  97. double radius;
  98. bool paused;
  99. } GeofenceObject;
  100. typedef std::map<uint32_t, GeofenceObject> GeofencesMap; //map of hwId to GeofenceObject
  101. typedef std::map<GeofenceKey, uint32_t> GeofenceIdMap; //map of GeofenceKey to hwId
  102. class GeofenceAdapter : public LocAdapterBase {
  103. /* ==== GEOFENCES ====================================================================== */
  104. GeofencesMap mGeofences; //map hwId to GeofenceObject
  105. GeofenceIdMap mGeofenceIds; //map of GeofenceKey to hwId
  106. PowerStateType mSystemPowerState;
  107. protected:
  108. /* ==== CLIENT ========================================================================= */
  109. virtual void updateClientsEventMask();
  110. virtual void stopClientSessions(LocationAPI* client, bool eraseSession = true);
  111. public:
  112. GeofenceAdapter();
  113. virtual ~GeofenceAdapter() {}
  114. /* ==== SSR ============================================================================ */
  115. /* ======== EVENTS ====(Called from QMI Thread)========================================= */
  116. virtual void handleEngineUpEvent();
  117. /* ======== UTILITIES ================================================================== */
  118. void restartGeofences();
  119. /* ==== GEOFENCES ====================================================================== */
  120. /* ======== COMMANDS ====(Called from Client Thread)==================================== */
  121. uint32_t* addGeofencesCommand(LocationAPI* client, size_t count,
  122. GeofenceOption* options, GeofenceInfo* info);
  123. void removeGeofencesCommand(LocationAPI* client, size_t count, uint32_t* ids);
  124. void pauseGeofencesCommand(LocationAPI* client, size_t count, uint32_t* ids);
  125. void resumeGeofencesCommand(LocationAPI* client, size_t count, uint32_t* ids);
  126. void modifyGeofencesCommand(LocationAPI* client, size_t count, uint32_t* ids,
  127. GeofenceOption* options);
  128. void updateSystemPowerStateCommand(PowerStateType systemPowerState);
  129. /* ======== RESPONSES ================================================================== */
  130. void reportResponse(LocationAPI* client, size_t count, LocationError* errs, uint32_t* ids);
  131. /* ======== UTILITIES ================================================================== */
  132. void saveGeofenceItem(LocationAPI* client,
  133. uint32_t clientId,
  134. uint32_t hwId,
  135. const GeofenceOption& options,
  136. const GeofenceInfo& info);
  137. void removeGeofenceItem(uint32_t hwId);
  138. void pauseGeofenceItem(uint32_t hwId);
  139. void resumeGeofenceItem(uint32_t hwId);
  140. void modifyGeofenceItem(uint32_t hwId, const GeofenceOption& options);
  141. LocationError getHwIdFromClient(LocationAPI* client, uint32_t clientId, uint32_t& hwId);
  142. LocationError getGeofenceKeyFromHwId(uint32_t hwId, GeofenceKey& key);
  143. void dump();
  144. /* ==== REPORTS ======================================================================== */
  145. /* ======== EVENTS ====(Called from QMI Thread)========================================= */
  146. void geofenceBreachEvent(size_t count, uint32_t* hwIds, Location& location,
  147. GeofenceBreachType breachType, uint64_t timestamp);
  148. void geofenceStatusEvent(GeofenceStatusAvailable available);
  149. /* ======== UTILITIES ================================================================== */
  150. void geofenceBreach(size_t count, uint32_t* hwIds, const Location& location,
  151. GeofenceBreachType breachType, uint64_t timestamp);
  152. void geofenceStatus(GeofenceStatusAvailable available);
  153. void pauseOrResumeGeofences(bool pauseOrResume = false /*false - pause, true - resume*/);
  154. void updateSystemPowerState(PowerStateType systemPowerState);
  155. };
  156. #endif /* GEOFENCE_ADAPTER_H */